Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 102 additions & 0 deletions test/other/test_readdir_r_silly.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#include <cassert>
#include <iostream>
#include <cstring>
#include <cerrno>
#include <unistd.h>
#include <fcntl.h>
#include <cstdlib>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>

using std::endl;

// Do a recursive directory listing of the directory whose path is specified
// by \a name.
void ls(const std::string& name, size_t indent = 0) {
DIR *dir;
struct dirent *entry;
if (indent == 0) {
std::cout << name << endl;
++indent;
}
// Make sure we can open the directory. This should also catch cases where
// the empty string is passed in.
if (not (dir = opendir(name.c_str()))) {
const int error = errno;
std::cout
<< "Failed to open directory: " << name << "; " << error << endl;
return;
}
// Just checking the sanity.
if (name.empty()) {
std::cout
<< "Managed to open a directory whose name was the empty string.."
<< endl;
assert(closedir(dir) != -1);
return;
}
// Iterate over the entries in the directory.
while ((entry = readdir(dir))) {
std::string entryName(entry->d_name);
if (entryName == "." || entryName == "..") {
// Skip the dot entries.
continue;
}
std::string indentStr(indent * 2, ' ');
if (entryName.empty()) {
std::cout
<< indentStr << "\"\": Found empty string as a "
<< (entry->d_type == DT_DIR ? "directory" : "file")
<< " entry!" << endl;
continue;
} else {
std::cout << indentStr << entryName
<< (entry->d_type == DT_DIR ? "/" : "") << endl;
}
if (entry->d_type == DT_DIR) {
// We found a subdirectory; recurse.
ls(std::string(name + (name == "/" ? "" : "/" ) + entryName),
indent + 1);
}
}
// Close our handle.
assert(closedir(dir) != -1);
}

void touch(const char* path) {
int fd = open(path, O_CREAT | O_TRUNC, 0644);
assert(fd != -1);
assert(close(fd) != -1);
}

int main() {
assert(mkdir("dir", 0755) == 0);
touch("dir/a");
touch("dir/b");
touch("dir/c");
touch("dir/d");
touch("dir/e");
std::cout << "Before:" << endl;
ls("dir");
std::cout << endl;
// Attempt to delete entries as we walk the (single) directory.
DIR* dir = opendir("dir");
assert(dir != NULL);
struct dirent *entry;
while ((entry = readdir(dir)) != NULL) {
std::string name(entry->d_name);
// Skip "." and "..".
if (name == "." || name == "..") {
continue;
}
// Unlink it.
std::cout << "Unlinking " << name << endl;
assert(unlink(("dir/" + name).c_str()) != -1);
}
assert(closedir(dir) != -1);
std::cout << "After:" << endl;
ls("dir");
std::cout << "done" << endl;
return 0;
}
16 changes: 16 additions & 0 deletions test/other/test_readdir_r_silly.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Before:
dir
a
b
c
d
e

Unlinking a
Unlinking b
Unlinking c
Unlinking d
Unlinking e
After:
dir
done
100 changes: 100 additions & 0 deletions test/other/test_truncate_from_0.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#include <cerrno>
#include <cstring>
#include <iostream>

#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

using std::endl;

//============================================================================
// :: Helpers

namespace
{
// Returns the size of the regular file specified as 'path'.
::off_t getSize(const char* const path)
{
// Stat the file and make sure that it's the expected size.
struct ::stat path_stat;
if (::stat(path, &path_stat) != 0) {
const int error = errno;
std::cout
<< "Failed to lstat path: " << path << "; errno=" << error << "; "
<< std::strerror(error) << endl;
return -1;
}

std::cout
<< "Size of file is: " << path_stat.st_size << endl;
return path_stat.st_size;
}

// Causes the regular file specified in 'path' to have a size of 'length'
// bytes.
void resize(const char* const path,
const ::off_t length)
{
std::cout
<< "Truncating file=" << path << " to length=" << length << endl;
if (::truncate(path, length) == -1)
{
const int error = errno;
std::cout
<< "Failed to truncate file=" << path << "; errno=" << error
<< "; " << std::strerror(error) << endl;
}

const ::off_t size = getSize(path);
if (size != length) {
std::cout
<< "Failed to truncate file=" << path << " to length=" << length
<< "; got size=" << size << endl;
}
}

// Helper to create a file with the given content.
void createFile(const std::string& path, const std::string& content)
{
std::cout
<< "Creating file: " << path << " with content=" << content << endl;

const int fd = ::open(path.c_str(), O_CREAT | O_WRONLY, 0644);
if (fd == -1) {
const int error = errno;
std::cout
<< "Failed to open file for writing: " << path << "; errno=" << error
<< "; " << std::strerror(error) << endl;
return;
}

if (::write(fd, content.c_str(), content.size()) != content.size()) {
const int error = errno;
std::cout
<< "Failed to write content=" << content << " to file=" << path
<< "; errno=" << error << "; " << std::strerror(error) << endl;

// Fall through to close FD.
}

::close(fd);
}
}

//============================================================================
// :: Entry Point
int main()
{
const char* const file = "/tmp/file";
createFile(file, "This is some content");
getSize(file);
resize(file, 32);
resize(file, 17);
resize(file, 0);

// This throws a JS exception.
resize(file, 32);
return 0;
}
10 changes: 10 additions & 0 deletions test/other/test_truncate_from_0.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Creating file: /tmp/file with content=This is some content
Size of file is: 20
Truncating file=/tmp/file to length=32
Size of file is: 32
Truncating file=/tmp/file to length=17
Size of file is: 17
Truncating file=/tmp/file to length=0
Size of file is: 0
Truncating file=/tmp/file to length=32
Size of file is: 32
Loading