diff options
author | Alon Zakai <alonzakai@gmail.com> | 2013-07-13 12:38:31 -0700 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2013-07-13 12:38:31 -0700 |
commit | 673ea2ec24decdcfd5321f13dd68603b158dc5c5 (patch) | |
tree | 3d987c45b45fd95a4c89c9de715abbed19091a60 | |
parent | 4db73843b367c57ad900a739bf8dec5f1799552c (diff) | |
parent | 49e6272b097fc930717998213d5c52c96fd6e3e6 (diff) |
Merge pull request #1372 from inolen/dirent_fixes
minor fixes to return codes of closedir, telldir
-rw-r--r-- | src/library.js | 6 | ||||
-rw-r--r-- | system/include/libc/sys/dirent.h | 1 | ||||
-rw-r--r-- | tests/dirent/test_readdir.c | 132 | ||||
-rwxr-xr-x | tests/runner.py | 113 |
4 files changed, 140 insertions, 112 deletions
diff --git a/src/library.js b/src/library.js index 5697481f..3597891e 100644 --- a/src/library.js +++ b/src/library.js @@ -737,7 +737,8 @@ LibraryManager.library = { // int closedir(DIR *dirp); // http://pubs.opengroup.org/onlinepubs/007908799/xsh/closedir.html if (!FS.streams[dirp] || !FS.streams[dirp].object.isFolder) { - return ___setErrNo(ERRNO_CODES.EBADF); + ___setErrNo(ERRNO_CODES.EBADF); + return -1; } else { _free(FS.streams[dirp].currentEntry); FS.streams[dirp] = null; @@ -749,7 +750,8 @@ LibraryManager.library = { // long int telldir(DIR *dirp); // http://pubs.opengroup.org/onlinepubs/007908799/xsh/telldir.html if (!FS.streams[dirp] || !FS.streams[dirp].object.isFolder) { - return ___setErrNo(ERRNO_CODES.EBADF); + ___setErrNo(ERRNO_CODES.EBADF); + return -1; } else { return FS.streams[dirp].position; } diff --git a/system/include/libc/sys/dirent.h b/system/include/libc/sys/dirent.h index 0d8b02b5..e6ce831e 100644 --- a/system/include/libc/sys/dirent.h +++ b/system/include/libc/sys/dirent.h @@ -24,6 +24,7 @@ DIR *opendir(const char *); void seekdir(DIR *, long); long telldir(DIR *); DIR *readdir(DIR *); +int readdir_r(DIR *, struct dirent *, struct dirent **); int closedir(DIR *dirp); void rewinddir(DIR *dirp); int scandir(const char *dirp, diff --git a/tests/dirent/test_readdir.c b/tests/dirent/test_readdir.c new file mode 100644 index 00000000..9f7b12e8 --- /dev/null +++ b/tests/dirent/test_readdir.c @@ -0,0 +1,132 @@ +#include <assert.h> +#include <dirent.h> +#include <errno.h> +#include <fcntl.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <sys/stat.h> + +static void create_file(const char *path, const char *buffer, int mode) { + int fd = open(path, O_WRONLY | O_CREAT | O_EXCL, mode); + assert(fd >= 0); + + int err = write(fd, buffer, sizeof(char) * strlen(buffer)); + assert(err == (sizeof(char) * strlen(buffer))); + + close(fd); +} + +void setup() { + mkdir("nocanread", 0111); + mkdir("foobar", 0777); + create_file("foobar/file.txt", "ride into the danger zone", 0666); +} + +void cleanup() { + rmdir("nocanread"); + unlink("foobar/file.txt"); + rmdir("foobar"); +} + +void test() { + int err; + int loc; + DIR *dir; + struct dirent *ent; + struct dirent ent_r; + struct dirent *result; + + // check bad opendir input + dir = opendir("noexist"); + assert(!dir); + assert(errno == ENOENT); + dir = opendir("nocanread"); + assert(!dir); + assert(errno == EACCES); + dir = opendir("foobar/file.txt"); + assert(!dir); + assert(errno == ENOTDIR); + + // check bad readdir input + dir = opendir("foobar"); + closedir(dir); + ent = readdir(dir); + assert(!ent); + assert(errno == EBADF); + + // check bad readdir_r input + dir = opendir("foobar"); + closedir(dir); + err = readdir_r(dir, NULL, &result); + assert(err == EBADF); + + // + // do a normal read with readdir + // + dir = opendir("foobar"); + assert(dir); + ent = readdir(dir); + assert(!strcmp(ent->d_name, ".")); + assert(ent->d_type & DT_DIR); + ent = readdir(dir); + assert(!strcmp(ent->d_name, "..")); + assert(ent->d_type & DT_DIR); + ent = readdir(dir); + assert(!strcmp(ent->d_name, "file.txt")); + assert(ent->d_type & DT_REG); + ent = readdir(dir); + assert(!ent); + + // test rewinddir + rewinddir(dir); + ent = readdir(dir); + assert(!strcmp(ent->d_name, ".")); + + // test seek / tell + rewinddir(dir); + ent = readdir(dir); + assert(!strcmp(ent->d_name, ".")); + loc = telldir(dir); + ent = readdir(dir); + assert(!strcmp(ent->d_name, "..")); + ent = readdir(dir); + assert(!strcmp(ent->d_name, "file.txt")); + seekdir(dir, loc); + ent = readdir(dir); + assert(!strcmp(ent->d_name, "..")); + + // + // do a normal read with readdir_r + // + rewinddir(dir); + err = readdir_r(dir, &ent_r, &result); + assert(!err); + assert(&ent_r == result); + assert(!strcmp(ent_r.d_name, ".")); + err = readdir_r(dir, &ent_r, &result); + assert(!err); + assert(&ent_r == result); + assert(!strcmp(ent_r.d_name, "..")); + err = readdir_r(dir, &ent_r, &result); + assert(!err); + assert(&ent_r == result); + assert(!strcmp(ent_r.d_name, "file.txt")); + err = readdir_r(dir, &ent_r, &result); + assert(!err); + assert(!result); + + err = closedir(dir); + assert(!err); + + puts("success"); +} + +int main() { + atexit(cleanup); + signal(SIGABRT, cleanup); + setup(); + test(); + return EXIT_SUCCESS; +}
\ No newline at end of file diff --git a/tests/runner.py b/tests/runner.py index d358a97f..96b00c50 100755 --- a/tests/runner.py +++ b/tests/runner.py @@ -6794,84 +6794,9 @@ def process(filename): self.emcc_args += ['--embed-file', 'three_numbers.txt'] self.do_run(src, 'match = 3\nx = -1.0, y = 0.1, z = -0.1\n') - def test_folders(self): - add_pre_run = ''' -def process(filename): - src = open(filename, 'r').read().replace( - '// {{PRE_RUN_ADDITIONS}}', - \'\'\' - FS.createFolder('/', 'test', true, false); - FS.createPath('/', 'test/hello/world/', true, false); - FS.createPath('/test', 'goodbye/world/', true, false); - FS.createPath('/test/goodbye', 'noentry', false, false); - FS.createDataFile('/test', 'freeforall.ext', 'abc', true, true); - FS.createDataFile('/test', 'restricted.ext', 'def', false, false); - \'\'\' - ) - open(filename, 'w').write(src) -''' - src = r''' - #include <stdio.h> - #include <dirent.h> - #include <errno.h> - - int main() { - struct dirent *e; - - // Basic correct behaviour. - DIR* d = opendir("/test"); - printf("--E: %d\n", errno); - while ((e = readdir(d))) puts(e->d_name); - printf("--E: %d\n", errno); - - // Empty folder; tell/seek. - puts("****"); - d = opendir("/test/hello/world/"); - e = readdir(d); - puts(e->d_name); - int pos = telldir(d); - e = readdir(d); - puts(e->d_name); - seekdir(d, pos); - e = readdir(d); - puts(e->d_name); - - // Errors. - puts("****"); - printf("--E: %d\n", errno); - d = opendir("/test/goodbye/noentry"); - printf("--E: %d, D: %d\n", errno, d); - d = opendir("/i/dont/exist"); - printf("--E: %d, D: %d\n", errno, d); - d = opendir("/test/freeforall.ext"); - printf("--E: %d, D: %d\n", errno, d); - while ((e = readdir(d))) puts(e->d_name); - printf("--E: %d\n", errno); - - return 0; - } - ''' - expected = ''' - --E: 0 - . - .. - hello - goodbye - freeforall.ext - restricted.ext - --E: 0 - **** - . - .. - .. - **** - --E: 0 - --E: 13, D: 0 - --E: 2, D: 0 - --E: 20, D: 0 - --E: 9 - ''' - self.do_run(src, re.sub('(^|\n)\s+', '\\1', expected), post_build=add_pre_run) + def test_readdir(self): + src = open(path_from_root('tests', 'dirent', 'test_readdir.c'), 'r').read() + self.do_run(src, 'success', force_c=True) def test_stat(self): add_pre_run = ''' @@ -7180,38 +7105,6 @@ def process(filename): Settings.LINKABLE = linkable # regression check for issue #273 self.do_run(src, "1 2 3") - def test_readdir(self): - add_pre_run = ''' -def process(filename): - src = open(filename, 'r').read().replace( - '// {{PRE_RUN_ADDITIONS}}', - "FS.createFolder('', 'test', true, true);\\nFS.createLazyFile( 'test', 'some_file', 'http://localhost/some_file', true, false);\\nFS.createFolder('test', 'some_directory', true, true);" - ) - open(filename, 'w').write(src) -''' - - src = ''' - #include <dirent.h> - #include <stdio.h> - - int main() - { - DIR * dir; - dirent * entity; - - dir = opendir( "test" ); - - while( ( entity = readdir( dir ) ) ) - { - printf( "%s is a %s\\n", entity->d_name, entity->d_type & DT_DIR ? "directory" : "file" ); - } - - return 0; - } - - ''' - self.do_run(src, ". is a directory\n.. is a directory\nsome_file is a file\nsome_directory is a directory", post_build=add_pre_run) - def test_fs_base(self): Settings.INCLUDE_FULL_LIBRARY = 1 try: |