diff options
author | Ingo Schwarze <schwarze@cvs.openbsd.org> | 2013-11-03 00:20:25 +0000 |
---|---|---|
committer | Ingo Schwarze <schwarze@cvs.openbsd.org> | 2013-11-03 00:20:25 +0000 |
commit | 0c5b8399b0d65aa1c36c58fcf6a3b8d58fca6fa4 (patch) | |
tree | 170cb0a7de9a763ce054c5e293bbe37ae84ce97f /regress | |
parent | 34855443478427ed447a74a4d0f8955f6bb895bd (diff) |
Add a second test, shortseek(), to make sure that seekdir() also works
correctly when moving the directory pointer by small distances.
This is currently failing, i will send an updated libc/gen patch
to fix this right afterwards.
Move the functions createfiles() and delfiles() to utils.{h,c} for reuse.
Minor cleanup in telldir.c.
Diffstat (limited to 'regress')
-rw-r--r-- | regress/lib/libc/telldir/Makefile | 8 | ||||
-rw-r--r-- | regress/lib/libc/telldir/main.c | 14 | ||||
-rw-r--r-- | regress/lib/libc/telldir/shortseek.c | 91 | ||||
-rw-r--r-- | regress/lib/libc/telldir/telldir.c | 73 | ||||
-rw-r--r-- | regress/lib/libc/telldir/utils.c | 53 | ||||
-rw-r--r-- | regress/lib/libc/telldir/utils.h | 6 |
6 files changed, 189 insertions, 56 deletions
diff --git a/regress/lib/libc/telldir/Makefile b/regress/lib/libc/telldir/Makefile index 0927779663c..728ae90e1e0 100644 --- a/regress/lib/libc/telldir/Makefile +++ b/regress/lib/libc/telldir/Makefile @@ -1,5 +1,9 @@ -# $OpenBSD: Makefile,v 1.1 2006/03/25 20:27:11 otto Exp $ +# $OpenBSD: Makefile,v 1.2 2013/11/03 00:20:24 schwarze Exp $ -PROG= telldir +PROG = telldir +SRCS = utils.c shortseek.c telldir.c main.c + +cleandir: + rm -rf d .include <bsd.regress.mk> diff --git a/regress/lib/libc/telldir/main.c b/regress/lib/libc/telldir/main.c new file mode 100644 index 00000000000..23ffa65dbf0 --- /dev/null +++ b/regress/lib/libc/telldir/main.c @@ -0,0 +1,14 @@ +/* $OpenBSD: main.c,v 1.1 2013/11/03 00:20:24 schwarze Exp $ */ + +/* Written by Ingo Schwarze, 2013, Public domain. */ + +void shortseek(void); +void longseek(void); + +int +main(void) +{ + shortseek(); + longseek(); + return(0); +} diff --git a/regress/lib/libc/telldir/shortseek.c b/regress/lib/libc/telldir/shortseek.c new file mode 100644 index 00000000000..9b2141ab4c8 --- /dev/null +++ b/regress/lib/libc/telldir/shortseek.c @@ -0,0 +1,91 @@ +/* $OpenBSD: shortseek.c,v 1.1 2013/11/03 00:20:24 schwarze Exp $ */ + +/* Written by Otto Moerbeek, 2006, Public domain. */ +/* Modified by Ingo Schwarze, 2013, Public domain. */ + +#include <sys/types.h> +#include <dirent.h> +#include <err.h> +#include <limits.h> +#include <stdio.h> +#include <string.h> + +#include "utils.h" + +#define NFILES 5 + +static void +shortloop(DIR *dp, int iend, int iback) +{ + struct dirent *f; + char fend[PATH_MAX], fback[PATH_MAX]; + long pos, t, remember = -1; + int i; + + rewinddir(dp); + snprintf(fend, sizeof fend, "%d", iend); + snprintf(fback, sizeof fback, "%d", iback); + + /* Scan to iend, remember where iback is. */ + + for (;;) { + pos = telldir(dp); + f = readdir(dp); + if (f == NULL) + errx(1, "file %s not found", fend); + if (strcmp(fback, f->d_name) == 0) + remember = pos; + if (strcmp(fend, f->d_name) == 0) + break; + } + if (remember == -1) + errx(1, "file %s not found", fback); + + /* Go back to iback, checking seekdir, telldir and readdir. */ + + seekdir(dp, remember); + if ((t = telldir(dp)) != remember) + errx(1, "tell after seek %s %ld != %ld", fback, t, remember); + if ((t = telldir(dp)) != remember) + errx(1, "tell after tell %s %ld != %ld", fback, t, remember); + f = readdir(dp); + if (f == NULL) + errx(1, "readdir %s at %ld", fback, remember); + + if (strcmp(f->d_name, fback)) + errx(1, "name mismatch: %s != %s", f->d_name, fback); + + /* Check that readdir can iterate the remaining files. */ + + for (i = iback + 1; i < NFILES; i++) { + f = readdir(dp); + if (f == NULL) + errx(1, "readdir %i failed", i); + } + + /* Check that readdir stops at the right place. */ + + f = readdir(dp); + if (f != NULL) + errx(1, "readdir %i returned %s", NFILES, f->d_name); +} + +void +shortseek(void) +{ + DIR *dp; + int iend, iback; + + createfiles(NFILES); + + dp = opendir("d"); + if (dp == NULL) + err(1, "shortseek: opendir"); + + for (iend = 0; iend < NFILES; iend++) + for (iback = 0; iback <= iend; iback++) + shortloop(dp, iend, iback); + + closedir(dp); + delfiles(); +} diff --git a/regress/lib/libc/telldir/telldir.c b/regress/lib/libc/telldir/telldir.c index 9de0eb5e969..d2879cc701b 100644 --- a/regress/lib/libc/telldir/telldir.c +++ b/regress/lib/libc/telldir/telldir.c @@ -1,60 +1,20 @@ -/* $OpenBSD: telldir.c,v 1.3 2013/08/20 01:13:54 guenther Exp $ */ +/* $OpenBSD: telldir.c,v 1.4 2013/11/03 00:20:24 schwarze Exp $ */ /* Written by Otto Moerbeek, 2006, Public domain. */ #include <sys/types.h> -#include <sys/stat.h> #include <dirent.h> #include <err.h> #include <limits.h> -#include <fcntl.h> #include <stdio.h> #include <string.h> -#include <unistd.h> +#include "utils.h" #define NFILES 1000 -void -createfiles(void) -{ - int i, fd; - char file[PATH_MAX]; - - mkdir("d", 0755); - for (i = 0; i < NFILES; i++) { - snprintf(file, sizeof file, "d/%d", i); - if ((fd = open(file, O_CREAT | O_WRONLY, 0600)) == -1) - err(1, "open %s", file); - close(fd); - } -} - -void -delfiles(void) -{ - DIR *dp; - struct dirent *f; - char file[PATH_MAX]; - - dp = opendir("d"); - if (dp == NULL) - err(1, "opendir"); - while (f = readdir(dp)) { - if (strcmp(f->d_name, ".") == 0 || - strcmp(f->d_name, "..") == 0) - continue; - snprintf(file, sizeof file, "d/%s", f->d_name); - if (unlink(file) == -1) - err(1, "unlink %s", f->d_name); - } - closedir(dp); - if (rmdir("d") == -1) - err(1, "rmdir"); -} - -void -loop(DIR *dp, int i) +static void +longloop(DIR *dp, int i) { struct dirent *f; char file[PATH_MAX]; @@ -62,6 +22,9 @@ loop(DIR *dp, int i) rewinddir(dp); snprintf(file, sizeof file, "%d", i); + + /* Scan through all files, remember where file i is. */ + for (;;) { pos = telldir(dp); f = readdir(dp); @@ -72,35 +35,37 @@ loop(DIR *dp, int i) } if (remember == -1) errx(1, "remember %s", file); + + /* Go back to i, checking seekdir, telldir and readdir. */ + seekdir(dp, remember); if ((t = telldir(dp)) != remember) - errx(1, "tell after seek %s %ld != %ld", file, t, pos); + errx(1, "tell after seek %s %ld != %ld", file, t, remember); if ((t = telldir(dp)) != remember) - errx(1, "tell after tell %s %ld != %ld", file, t, pos); + errx(1, "tell after tell %s %ld != %ld", file, t, remember); f = readdir(dp); if (f == NULL) - err(1, "seek to %s %ld", file, remember); + errx(1, "readdir %s at %ld", file, remember); if (strcmp(f->d_name, file) != 0) - err(1, "name mismatch: %s != %s\n", f->d_name, file); + errx(1, "name mismatch: %s != %s", f->d_name, file); } -int -main(void) +void +longseek(void) { DIR *dp; int i; - createfiles(); + createfiles(NFILES); dp = opendir("d"); if (dp == NULL) - err(1, "opendir"); + err(1, "longseek: opendir"); for (i = 0; i < NFILES; i++) - loop(dp, (i + NFILES/2) % NFILES); + longloop(dp, (i + NFILES/2) % NFILES); closedir(dp); delfiles(); - return 0; } diff --git a/regress/lib/libc/telldir/utils.c b/regress/lib/libc/telldir/utils.c new file mode 100644 index 00000000000..6a4101aba3f --- /dev/null +++ b/regress/lib/libc/telldir/utils.c @@ -0,0 +1,53 @@ +/* $OpenBSD: utils.c,v 1.1 2013/11/03 00:20:24 schwarze Exp $ */ + +/* Written by Otto Moerbeek, 2006, Public domain. */ + +#include <sys/types.h> +#include <sys/stat.h> +#include <dirent.h> +#include <err.h> +#include <fcntl.h> +#include <limits.h> +#include <stdio.h> +#include <string.h> +#include <unistd.h> + +#include "utils.h" + +void +createfiles(int nfiles) +{ + int i, fd; + char file[PATH_MAX]; + + mkdir("d", 0755); + for (i = 0; i < nfiles; i++) { + snprintf(file, sizeof file, "d/%d", i); + if ((fd = open(file, O_CREAT | O_WRONLY, 0600)) == -1) + err(1, "open %s", file); + close(fd); + } +} + +void +delfiles(void) +{ + DIR *dp; + struct dirent *f; + char file[PATH_MAX]; + + dp = opendir("d"); + if (dp == NULL) + err(1, "opendir"); + while (f = readdir(dp)) { + if (strcmp(f->d_name, ".") == 0 || + strcmp(f->d_name, "..") == 0) + continue; + snprintf(file, sizeof file, "d/%s", f->d_name); + if (unlink(file) == -1) + err(1, "unlink %s", f->d_name); + } + closedir(dp); + if (rmdir("d") == -1) + err(1, "rmdir"); +} diff --git a/regress/lib/libc/telldir/utils.h b/regress/lib/libc/telldir/utils.h new file mode 100644 index 00000000000..74ffcd4641e --- /dev/null +++ b/regress/lib/libc/telldir/utils.h @@ -0,0 +1,6 @@ +/* $OpenBSD: utils.h,v 1.1 2013/11/03 00:20:24 schwarze Exp $ */ + +/* Written by Ingo Schwarze, 2013, Public domain. */ + +void createfiles(int); +void delfiles(void); |