Skip to content

Commit f9e4785

Browse files
authored
Split some larger other tests into separate files. NFC (#25320)
1 parent db46642 commit f9e4785

File tree

5 files changed

+230
-347
lines changed

5 files changed

+230
-347
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#include <cassert>
2+
#include <iostream>
3+
#include <cstring>
4+
#include <cerrno>
5+
#include <unistd.h>
6+
#include <fcntl.h>
7+
#include <cstdlib>
8+
#include <dirent.h>
9+
#include <sys/stat.h>
10+
#include <sys/types.h>
11+
12+
using std::endl;
13+
14+
// Do a recursive directory listing of the directory whose path is specified
15+
// by \a name.
16+
void ls(const std::string& name, size_t indent = 0) {
17+
DIR *dir;
18+
struct dirent *entry;
19+
if (indent == 0) {
20+
std::cout << name << endl;
21+
++indent;
22+
}
23+
// Make sure we can open the directory. This should also catch cases where
24+
// the empty string is passed in.
25+
if (not (dir = opendir(name.c_str()))) {
26+
const int error = errno;
27+
std::cout
28+
<< "Failed to open directory: " << name << "; " << error << endl;
29+
return;
30+
}
31+
// Just checking the sanity.
32+
if (name.empty()) {
33+
std::cout
34+
<< "Managed to open a directory whose name was the empty string.."
35+
<< endl;
36+
assert(closedir(dir) != -1);
37+
return;
38+
}
39+
// Iterate over the entries in the directory.
40+
while ((entry = readdir(dir))) {
41+
std::string entryName(entry->d_name);
42+
if (entryName == "." || entryName == "..") {
43+
// Skip the dot entries.
44+
continue;
45+
}
46+
std::string indentStr(indent * 2, ' ');
47+
if (entryName.empty()) {
48+
std::cout
49+
<< indentStr << "\"\": Found empty string as a "
50+
<< (entry->d_type == DT_DIR ? "directory" : "file")
51+
<< " entry!" << endl;
52+
continue;
53+
} else {
54+
std::cout << indentStr << entryName
55+
<< (entry->d_type == DT_DIR ? "/" : "") << endl;
56+
}
57+
if (entry->d_type == DT_DIR) {
58+
// We found a subdirectory; recurse.
59+
ls(std::string(name + (name == "/" ? "" : "/" ) + entryName),
60+
indent + 1);
61+
}
62+
}
63+
// Close our handle.
64+
assert(closedir(dir) != -1);
65+
}
66+
67+
void touch(const char* path) {
68+
int fd = open(path, O_CREAT | O_TRUNC, 0644);
69+
assert(fd != -1);
70+
assert(close(fd) != -1);
71+
}
72+
73+
int main() {
74+
assert(mkdir("dir", 0755) == 0);
75+
touch("dir/a");
76+
touch("dir/b");
77+
touch("dir/c");
78+
touch("dir/d");
79+
touch("dir/e");
80+
std::cout << "Before:" << endl;
81+
ls("dir");
82+
std::cout << endl;
83+
// Attempt to delete entries as we walk the (single) directory.
84+
DIR* dir = opendir("dir");
85+
assert(dir != NULL);
86+
struct dirent *entry;
87+
while ((entry = readdir(dir)) != NULL) {
88+
std::string name(entry->d_name);
89+
// Skip "." and "..".
90+
if (name == "." || name == "..") {
91+
continue;
92+
}
93+
// Unlink it.
94+
std::cout << "Unlinking " << name << endl;
95+
assert(unlink(("dir/" + name).c_str()) != -1);
96+
}
97+
assert(closedir(dir) != -1);
98+
std::cout << "After:" << endl;
99+
ls("dir");
100+
std::cout << "done" << endl;
101+
return 0;
102+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
Before:
2+
dir
3+
a
4+
b
5+
c
6+
d
7+
e
8+
9+
Unlinking a
10+
Unlinking b
11+
Unlinking c
12+
Unlinking d
13+
Unlinking e
14+
After:
15+
dir
16+
done
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#include <cerrno>
2+
#include <cstring>
3+
#include <iostream>
4+
5+
#include <fcntl.h>
6+
#include <sys/stat.h>
7+
#include <sys/types.h>
8+
#include <unistd.h>
9+
10+
using std::endl;
11+
12+
//============================================================================
13+
// :: Helpers
14+
15+
namespace
16+
{
17+
// Returns the size of the regular file specified as 'path'.
18+
::off_t getSize(const char* const path)
19+
{
20+
// Stat the file and make sure that it's the expected size.
21+
struct ::stat path_stat;
22+
if (::stat(path, &path_stat) != 0) {
23+
const int error = errno;
24+
std::cout
25+
<< "Failed to lstat path: " << path << "; errno=" << error << "; "
26+
<< std::strerror(error) << endl;
27+
return -1;
28+
}
29+
30+
std::cout
31+
<< "Size of file is: " << path_stat.st_size << endl;
32+
return path_stat.st_size;
33+
}
34+
35+
// Causes the regular file specified in 'path' to have a size of 'length'
36+
// bytes.
37+
void resize(const char* const path,
38+
const ::off_t length)
39+
{
40+
std::cout
41+
<< "Truncating file=" << path << " to length=" << length << endl;
42+
if (::truncate(path, length) == -1)
43+
{
44+
const int error = errno;
45+
std::cout
46+
<< "Failed to truncate file=" << path << "; errno=" << error
47+
<< "; " << std::strerror(error) << endl;
48+
}
49+
50+
const ::off_t size = getSize(path);
51+
if (size != length) {
52+
std::cout
53+
<< "Failed to truncate file=" << path << " to length=" << length
54+
<< "; got size=" << size << endl;
55+
}
56+
}
57+
58+
// Helper to create a file with the given content.
59+
void createFile(const std::string& path, const std::string& content)
60+
{
61+
std::cout
62+
<< "Creating file: " << path << " with content=" << content << endl;
63+
64+
const int fd = ::open(path.c_str(), O_CREAT | O_WRONLY, 0644);
65+
if (fd == -1) {
66+
const int error = errno;
67+
std::cout
68+
<< "Failed to open file for writing: " << path << "; errno=" << error
69+
<< "; " << std::strerror(error) << endl;
70+
return;
71+
}
72+
73+
if (::write(fd, content.c_str(), content.size()) != content.size()) {
74+
const int error = errno;
75+
std::cout
76+
<< "Failed to write content=" << content << " to file=" << path
77+
<< "; errno=" << error << "; " << std::strerror(error) << endl;
78+
79+
// Fall through to close FD.
80+
}
81+
82+
::close(fd);
83+
}
84+
}
85+
86+
//============================================================================
87+
// :: Entry Point
88+
int main()
89+
{
90+
const char* const file = "/tmp/file";
91+
createFile(file, "This is some content");
92+
getSize(file);
93+
resize(file, 32);
94+
resize(file, 17);
95+
resize(file, 0);
96+
97+
// This throws a JS exception.
98+
resize(file, 32);
99+
return 0;
100+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Creating file: /tmp/file with content=This is some content
2+
Size of file is: 20
3+
Truncating file=/tmp/file to length=32
4+
Size of file is: 32
5+
Truncating file=/tmp/file to length=17
6+
Size of file is: 17
7+
Truncating file=/tmp/file to length=0
8+
Size of file is: 0
9+
Truncating file=/tmp/file to length=32
10+
Size of file is: 32

0 commit comments

Comments
 (0)