diff options
Diffstat (limited to 'io')
-rw-r--r-- | io/Makefile | 51 | ||||
-rw-r--r-- | io/Versions | 3 | ||||
-rw-r--r-- | io/fcntl.h | 9 | ||||
-rw-r--r-- | io/fstatat.c | 58 | ||||
-rw-r--r-- | io/fstatat64.c | 58 | ||||
-rw-r--r-- | io/sys/stat.h | 47 | ||||
-rw-r--r-- | io/tst-fchownat.c | 145 | ||||
-rw-r--r-- | io/tst-fstatat.c | 143 | ||||
-rw-r--r-- | io/tst-futimesat.c | 147 | ||||
-rw-r--r-- | io/tst-openat.c | 6 | ||||
-rw-r--r-- | io/tst-renameat.c | 149 | ||||
-rw-r--r-- | io/tst-unlinkat.c | 178 |
12 files changed, 969 insertions, 25 deletions
diff --git a/io/Makefile b/io/Makefile index b1ca468..9339ffe 100644 --- a/io/Makefile +++ b/io/Makefile @@ -26,40 +26,43 @@ headers := sys/stat.h bits/stat.h sys/statfs.h bits/statfs.h sys/vfs.h \ poll.h sys/poll.h bits/poll.h \ utime.h ftw.h fts.h sys/sendfile.h -routines := \ - utime \ - mkfifo \ - stat fstat lstat mknod stat64 fstat64 lstat64 \ - xstat fxstat lxstat xmknod xstat64 fxstat64 lxstat64 \ - statfs fstatfs statfs64 fstatfs64 \ - statvfs fstatvfs statvfs64 fstatvfs64 \ - umask chmod fchmod lchmod mkdir \ - open open64 openat openat64 close \ - read write lseek lseek64 access euidaccess \ - fcntl flock lockf lockf64 \ - dup dup2 pipe \ - creat creat64 \ - chdir fchdir \ - getcwd getwd getdirname \ - chown fchown lchown \ - ttyname ttyname_r isatty \ - link symlink readlink \ - unlink rmdir \ - ftw ftw64 fts poll \ - posix_fadvise posix_fadvise64 \ - posix_fallocate posix_fallocate64 \ +routines := \ + utime \ + mkfifo \ + stat fstat lstat mknod stat64 fstat64 lstat64 fstatat fstatat64 \ + xstat fxstat lxstat xmknod xstat64 fxstat64 lxstat64 \ + fxstatat fxstatat64 \ + statfs fstatfs statfs64 fstatfs64 \ + statvfs fstatvfs statvfs64 fstatvfs64 \ + umask chmod fchmod lchmod mkdir \ + open open64 openat openat64 close \ + read write lseek lseek64 access euidaccess \ + fcntl flock lockf lockf64 \ + dup dup2 pipe \ + creat creat64 \ + chdir fchdir \ + getcwd getwd getdirname \ + chown fchown lchown fchownat \ + ttyname ttyname_r isatty \ + link symlink readlink \ + unlink unlinkat rmdir \ + ftw ftw64 fts poll \ + posix_fadvise posix_fadvise64 \ + posix_fallocate posix_fallocate64 \ sendfile sendfile64 # These routines will be omitted from the libc shared object. # Instead the static object files will be included in a special archive # linked against when the shared library will be used. -static-only-routines = stat fstat lstat mknod stat64 fstat64 lstat64 +static-only-routines = stat fstat lstat mknod stat64 fstat64 lstat64 \ + fstatat fstatat64 others := pwd test-srcs := ftwtest tests := test-utime test-stat test-stat2 test-lfs tst-getcwd \ tst-fcntl bug-ftw1 bug-ftw2 bug-ftw3 bug-ftw4 tst-statvfs \ - tst-openat + tst-openat tst-unlinkat tst-fstatat tst-futimesat \ + tst-renameat tst-fchownat distribute := ftwtest-sh diff --git a/io/Versions b/io/Versions index f5cd8d5..65ee771 100644 --- a/io/Versions +++ b/io/Versions @@ -98,6 +98,9 @@ libc { nftw; nftw64; } GLIBC_2.4 { + fchownat; + __fxstatat; __fxstatat64; openat; openat64; + unlinkat; } } @@ -56,6 +56,15 @@ __BEGIN_DECLS # define SEEK_END 2 /* Seek from end of file. */ #endif /* XPG */ +#ifdef __USE_GNU +# define AT_FDCWD -100 /* Special value used to indicate + openat should use the current + working directory. */ +# define AT_SYMLINK_NOFOLLOW 0x100 /* Do not follow symbolic links. */ +# define AT_REMOVEDIR 0x200 /* Remove directory instead of + unlinking file. */ +#endif + /* Do the file control operation described by CMD on FD. The remaining arguments are interpreted depending on CMD. diff --git a/io/fstatat.c b/io/fstatat.c new file mode 100644 index 0000000..1ac8059 --- /dev/null +++ b/io/fstatat.c @@ -0,0 +1,58 @@ +/* Copyright (C) 2005 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + In addition to the permissions in the GNU Lesser General Public + License, the Free Software Foundation gives you unlimited + permission to link the compiled version of this file with other + programs, and to distribute those programs without any restriction + coming from the use of this file. (The GNU Lesser General Public + License restrictions do apply in other respects; for example, they + cover modification of the file, and distribution when not linked + into another program.) + + Note that people who make modified versions of this file are not + obligated to grant this special exception for their modified + versions; it is their choice whether to do so. The GNU Lesser + General Public License gives permission to release a modified + version without this exception; this exception also makes it + possible to release a modified version which carries forward this + exception. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, write to the Free + Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA + 02111-1307 USA. */ + +#include <sys/stat.h> + +/* This definition is only used if inlining fails for this function; see + the last page of <sys/stat.h>. The real work is done by the `x' + function which is passed a version number argument. We arrange in the + makefile that when not inlined this function is always statically + linked; that way a dynamically-linked executable always encodes the + version number corresponding to the data structures it uses, so the `x' + functions in the shared library can adapt without needing to recompile + all callers. */ + +#undef fstatat +int +fstatat (int fd, const char *file, struct stat *buf, int flag) +{ + return __fxstatat (_STAT_VER, fd, file, buf, flag); +} + +/* Hide the symbol so that no definition but the one locally in the + executable or DSO is used. */ +#ifdef HAVE_DOT_HIDDEN +asm (".hidden\tfstatat"); +#endif diff --git a/io/fstatat64.c b/io/fstatat64.c new file mode 100644 index 0000000..a14b42d --- /dev/null +++ b/io/fstatat64.c @@ -0,0 +1,58 @@ +/* Copyright (C) 2005 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + In addition to the permissions in the GNU Lesser General Public + License, the Free Software Foundation gives you unlimited + permission to link the compiled version of this file with other + programs, and to distribute those programs without any restriction + coming from the use of this file. (The GNU Lesser General Public + License restrictions do apply in other respects; for example, they + cover modification of the file, and distribution when not linked + into another program.) + + Note that people who make modified versions of this file are not + obligated to grant this special exception for their modified + versions; it is their choice whether to do so. The GNU Lesser + General Public License gives permission to release a modified + version without this exception; this exception also makes it + possible to release a modified version which carries forward this + exception. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, write to the Free + Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA + 02111-1307 USA. */ + +#include <sys/stat.h> + +/* This definition is only used if inlining fails for this function; see + the last page of <sys/stat.h>. The real work is done by the `x' + function which is passed a version number argument. We arrange in the + makefile that when not inlined this function is always statically + linked; that way a dynamically-linked executable always encodes the + version number corresponding to the data structures it uses, so the `x' + functions in the shared library can adapt without needing to recompile + all callers. */ + +#undef fstatat64 +int +fstatat64 (int fd, const char *file, struct stat64 *buf, int flag) +{ + return __fxstatat64 (_STAT_VER, fd, file, buf, flag); +} + +/* Hide the symbol so that no definition but the one locally in the + executable or DSO is used. */ +#ifdef HAVE_DOT_HIDDEN +asm (".hidden\tfstatat64"); +#endif diff --git a/io/sys/stat.h b/io/sys/stat.h index 7075003..0a82ef3 100644 --- a/io/sys/stat.h +++ b/io/sys/stat.h @@ -1,4 +1,4 @@ -/* Copyright (C) 1991,1992,1995-2002,2003,2004 Free Software Foundation, Inc. +/* Copyright (C) 1991,1992,1995-2004,2005 Free Software Foundation, Inc. This file is part of the GNU C Library. The GNU C Library is free software; you can redistribute it and/or @@ -228,6 +228,23 @@ extern int stat64 (__const char *__restrict __file, extern int fstat64 (int __fd, struct stat64 *__buf) __THROW __nonnull ((2)); #endif +#ifdef __USE_GNU +/* Similar to stat, get the attributes for FILE and put them in BUF. + Relative path names are interpreted relative to FD unless FD is + AT_FDCWD. */ +# ifndef __USE_FILE_OFFSET64 +extern int fstatat (int __fd, const char *__file, struct stat *__buf, + int __flag) __THROW __nonnull ((2, 3)); +# else +extern int __REDIRECT_NTH (fstatat, (int __fd, const char *__file, + struct stat *__buf, int __flag), + fstatat64) __THROW __nonnull ((2, 3)); +# endif + +extern int fstatat64 (int __fd, const char *__file, struct stat64 *__buf, + int __flag) __THROW __nonnull ((2, 3)); +#endif + #if defined __USE_BSD || defined __USE_XOPEN_EXTENDED # ifndef __USE_FILE_OFFSET64 /* Get file attributes about FILE and put them in BUF. @@ -327,6 +344,9 @@ extern int __xstat (int __ver, __const char *__filename, struct stat *__stat_buf) __THROW __nonnull ((2, 3)); extern int __lxstat (int __ver, __const char *__filename, struct stat *__stat_buf) __THROW __nonnull ((2, 3)); +extern int __fxstatat (int __ver, int __fildes, __const char *__filename, + struct stat *__stat_buf, int __flag) + __THROW __nonnull ((3, 4)); #else # ifdef __REDIRECT_NTH extern int __REDIRECT_NTH (__fxstat, (int __ver, int __fildes, @@ -338,6 +358,10 @@ extern int __REDIRECT_NTH (__xstat, (int __ver, __const char *__filename, extern int __REDIRECT_NTH (__lxstat, (int __ver, __const char *__filename, struct stat *__stat_buf), __lxstat64) __nonnull ((2, 3)); +extern int __REDIRECT_NTH (__fxstatat, (int __ver, int __fildes, + __const char *__filename, + struct stat *__stat_buf, int __flag), + __fxstatat64) __nonnull ((3, 4)); # else # define __fxstat __fxstat64 @@ -353,6 +377,9 @@ extern int __xstat64 (int __ver, __const char *__filename, struct stat64 *__stat_buf) __THROW __nonnull ((2, 3)); extern int __lxstat64 (int __ver, __const char *__filename, struct stat64 *__stat_buf) __THROW __nonnull ((2, 3)); +extern int __fxstatat64 (int __ver, int __fildes, __const char *__filename, + struct stat64 *__stat_buf, int __flag) + __THROW __nonnull ((3, 4)); #endif extern int __xmknod (int __ver, __const char *__path, __mode_t __mode, __dev_t *__dev) __THROW __nonnull ((2, 4)); @@ -380,6 +407,15 @@ __NTH (fstat (int __fd, struct stat *__statbuf)) return __fxstat (_STAT_VER, __fd, __statbuf); } +# ifdef __USE_GNU +extern __inline__ int +__NTH (fstatat (int __fd, __const char *__filename, struct stat *__statbuf, + int __flag)) +{ + return __fxstatat (_STAT_VER, __fd, __filename, __statbuf, __flag); +} +# endif + # if defined __USE_MISC || defined __USE_BSD extern __inline__ int __NTH (mknod (__const char *__path, __mode_t __mode, __dev_t __dev)) @@ -412,6 +448,15 @@ __NTH (fstat64 (int __fd, struct stat64 *__statbuf)) } # endif +# ifdef __USE_GNU +extern __inline__ int +__NTH (fstatat64 (int __fd, __const char *__filename, struct stat64 *__statbuf, + int __flag)) +{ + return __fxstatat64 (_STAT_VER, __fd, __filename, __statbuf, __flag); +} +# endif + #endif __END_DECLS diff --git a/io/tst-fchownat.c b/io/tst-fchownat.c new file mode 100644 index 0000000..0cbf78b --- /dev/null +++ b/io/tst-fchownat.c @@ -0,0 +1,145 @@ +#include <dirent.h> +#include <fcntl.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + + +static void prepare (void); +#define PREPARE(argc, argv) prepare () + +static int do_test (void); +#define TEST_FUNCTION do_test () + +#include "../test-skeleton.c" + +static int dir_fd; + +static void +prepare (void) +{ +#if _POSIX_CHOWN_RESTRICTED > 0 + uid_t uid = getuid (); + if (uid != 0) + { + puts ("need root privileges"); + exit (0); + } +#endif + + size_t test_dir_len = strlen (test_dir); + static const char dir_name[] = "/tst-fchownat.XXXXXX"; + + size_t dirbuflen = test_dir_len + sizeof (dir_name); + char *dirbuf = malloc (dirbuflen); + if (dirbuf == NULL) + { + puts ("out of memory"); + exit (1); + } + + snprintf (dirbuf, dirbuflen, "%s%s", test_dir, dir_name); + if (mkdtemp (dirbuf) == NULL) + { + puts ("cannot create temporary directory"); + exit (1); + } + + add_temp_file (dirbuf); + + dir_fd = open (dirbuf, O_RDONLY | O_DIRECTORY); + if (dir_fd == -1) + { + puts ("cannot open directory"); + exit (1); + } +} + + +static int +do_test (void) +{ + /* fdopendir takes over the descriptor, make a copy. */ + int dupfd = dup (dir_fd); + if (dupfd == -1) + { + puts ("dup failed"); + return 1; + } + if (lseek (dupfd, 0, SEEK_SET) != 0) + { + puts ("1st lseek failed"); + return 1; + } + + /* The directory should be empty safe the . and .. files. */ + DIR *dir = fdopendir (dupfd); + if (dir == NULL) + { + puts ("fdopendir failed"); + return 1; + } + struct dirent64 *d; + while ((d = readdir64 (dir)) != NULL) + if (strcmp (d->d_name, ".") != 0 && strcmp (d->d_name, "..") != 0) + { + printf ("temp directory contains file \"%s\"\n", d->d_name); + return 1; + } + closedir (dir); + + /* Try to create a file. */ + int fd = openat (dir_fd, "some-file", O_CREAT|O_RDWR|O_EXCL, 0666); + if (fd == -1) + { + if (errno == ENOSYS) + { + puts ("*at functions not supported"); + return 0; + } + + puts ("file creation failed"); + return 1; + } + write (fd, "hello", 5); + puts ("file created"); + + struct stat64 st1; + if (fstat64 (fd, &st1) != 0) + { + puts ("fstat64 failed"); + return 1; + } + + close (fd); + + if (fchownat (dir_fd, "some-file", st1.st_uid + 1, st1.st_gid + 1, 0) != 0) + { + puts ("fchownat failed"); + return 1; + } + + struct stat64 st2; + if (fstatat64 (dir_fd, "some-file", &st2, 0) != 0) + { + puts ("fstatat64 failed"); + return 1; + } + + if (st1.st_uid + 1 != st2.st_uid || st1.st_gid + 1 != st2.st_gid) + { + puts ("owner change failed"); + return 1; + } + + if (unlinkat (dir_fd, "some-file", 0) != 0) + { + puts ("unlinkat failed"); + return 1; + } + + close (dir_fd); + + return 0; +} diff --git a/io/tst-fstatat.c b/io/tst-fstatat.c new file mode 100644 index 0000000..6ddf30d --- /dev/null +++ b/io/tst-fstatat.c @@ -0,0 +1,143 @@ +#include <dirent.h> +#include <fcntl.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + + +static void prepare (void); +#define PREPARE(argc, argv) prepare () + +static int do_test (void); +#define TEST_FUNCTION do_test () + +#include "../test-skeleton.c" + +static int dir_fd; + +static void +prepare (void) +{ + size_t test_dir_len = strlen (test_dir); + static const char dir_name[] = "/tst-fstatat.XXXXXX"; + + size_t dirbuflen = test_dir_len + sizeof (dir_name); + char *dirbuf = malloc (dirbuflen); + if (dirbuf == NULL) + { + puts ("out of memory"); + exit (1); + } + + snprintf (dirbuf, dirbuflen, "%s%s", test_dir, dir_name); + if (mkdtemp (dirbuf) == NULL) + { + puts ("cannot create temporary directory"); + exit (1); + } + + add_temp_file (dirbuf); + + dir_fd = open (dirbuf, O_RDONLY | O_DIRECTORY); + if (dir_fd == -1) + { + puts ("cannot open directory"); + exit (1); + } +} + + +static int +do_test (void) +{ + /* fdopendir takes over the descriptor, make a copy. */ + int dupfd = dup (dir_fd); + if (dupfd == -1) + { + puts ("dup failed"); + return 1; + } + if (lseek (dupfd, 0, SEEK_SET) != 0) + { + puts ("1st lseek failed"); + return 1; + } + + /* The directory should be empty safe the . and .. files. */ + DIR *dir = fdopendir (dupfd); + if (dir == NULL) + { + puts ("fdopendir failed"); + return 1; + } + struct dirent64 *d; + while ((d = readdir64 (dir)) != NULL) + if (strcmp (d->d_name, ".") != 0 && strcmp (d->d_name, "..") != 0) + { + printf ("temp directory contains file \"%s\"\n", d->d_name); + return 1; + } + closedir (dir); + + /* Try to create a file. */ + int fd = openat (dir_fd, "some-file", O_CREAT|O_RDWR|O_EXCL, 0666); + if (fd == -1) + { + if (errno == ENOSYS) + { + puts ("*at functions not supported"); + return 0; + } + + puts ("file creation failed"); + return 1; + } + write (fd, "hello", 5); + puts ("file created"); + + struct stat64 st1; + if (fstat64 (fd, &st1) != 0) + { + puts ("fstat64 failed"); + return 1; + } + + close (fd); + + struct stat64 st2; + if (fstatat64 (dir_fd, "some-file", &st2, 0) != 0) + { + puts ("fstatat64 failed"); + return 1; + } + + if (st1.st_dev != st2.st_dev + || st1.st_ino != st2.st_ino + || st1.st_size != st2.st_size) + { + puts ("stat results do not match"); + return 1; + } + + if (unlinkat (dir_fd, "some-file", 0) != 0) + { + puts ("unlinkat failed"); + return 1; + } + + if (fstatat64 (dir_fd, "some-file", &st2, 0) == 0) + { + puts ("second fstatat64 succeeded"); + return 1; + } + if (errno != ENOENT) + { + puts ("second fstatat64 did not fail with ENOENT"); + return 1; + } + + close (dir_fd); + + return 0; +} diff --git a/io/tst-futimesat.c b/io/tst-futimesat.c new file mode 100644 index 0000000..c1e8d93 --- /dev/null +++ b/io/tst-futimesat.c @@ -0,0 +1,147 @@ +#include <dirent.h> +#include <fcntl.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <sys/time.h> + + +static void prepare (void); +#define PREPARE(argc, argv) prepare () + +static int do_test (void); +#define TEST_FUNCTION do_test () + +#include "../test-skeleton.c" + +static int dir_fd; + +static void +prepare (void) +{ + size_t test_dir_len = strlen (test_dir); + static const char dir_name[] = "/tst-futimesat.XXXXXX"; + + size_t dirbuflen = test_dir_len + sizeof (dir_name); + char *dirbuf = malloc (dirbuflen); + if (dirbuf == NULL) + { + puts ("out of memory"); + exit (1); + } + + snprintf (dirbuf, dirbuflen, "%s%s", test_dir, dir_name); + if (mkdtemp (dirbuf) == NULL) + { + puts ("cannot create temporary directory"); + exit (1); + } + + add_temp_file (dirbuf); + + dir_fd = open (dirbuf, O_RDONLY | O_DIRECTORY); + if (dir_fd == -1) + { + puts ("cannot open directory"); + exit (1); + } +} + + +static int +do_test (void) +{ + /* fdopendir takes over the descriptor, make a copy. */ + int dupfd = dup (dir_fd); + if (dupfd == -1) + { + puts ("dup failed"); + return 1; + } + if (lseek (dupfd, 0, SEEK_SET) != 0) + { + puts ("1st lseek failed"); + return 1; + } + + /* The directory should be empty safe the . and .. files. */ + DIR *dir = fdopendir (dupfd); + if (dir == NULL) + { + puts ("fdopendir failed"); + return 1; + } + struct dirent64 *d; + while ((d = readdir64 (dir)) != NULL) + if (strcmp (d->d_name, ".") != 0 && strcmp (d->d_name, "..") != 0) + { + printf ("temp directory contains file \"%s\"\n", d->d_name); + return 1; + } + closedir (dir); + + /* Try to create a file. */ + int fd = openat (dir_fd, "some-file", O_CREAT|O_RDWR|O_EXCL, 0666); + if (fd == -1) + { + if (errno == ENOSYS) + { + puts ("*at functions not supported"); + return 0; + } + + puts ("file creation failed"); + return 1; + } + write (fd, "hello", 5); + puts ("file created"); + + struct stat64 st1; + if (fstat64 (fd, &st1) != 0) + { + puts ("fstat64 failed"); + return 1; + } + + close (fd); + + struct timeval tv[2]; + tv[0].tv_sec = st1.st_atime + 1; + tv[0].tv_usec = 0; + tv[1].tv_sec = st1.st_mtime + 1; + tv[1].tv_usec = 0; + if (futimesat (dir_fd, "some-file", tv) != 0) + { + puts ("futimesat failed"); + return 1; + } + + struct stat64 st2; + if (fstatat64 (dir_fd, "some-file", &st2, 0) != 0) + { + puts ("fstatat64 failed"); + return 1; + } + + if (st2.st_mtime != tv[1].tv_sec +#ifdef _STATBUF_ST_NSEC + || st2.st_mtim.tv_nsec != 0 +#endif + ) + { + puts ("stat shows different mtime"); + return 1; + } + + + if (unlinkat (dir_fd, "some-file", 0) != 0) + { + puts ("unlinkat failed"); + return 1; + } + + close (dir_fd); + + return 0; +} diff --git a/io/tst-openat.c b/io/tst-openat.c index c20c95a..d10b654 100644 --- a/io/tst-openat.c +++ b/io/tst-openat.c @@ -84,6 +84,12 @@ do_test (void) int fd = openat (dir_fd, "some-file", O_CREAT|O_RDWR|O_EXCL, 0666); if (fd == -1) { + if (errno == ENOSYS) + { + puts ("*at functions not supported"); + return 0; + } + puts ("file creation failed"); return 1; } diff --git a/io/tst-renameat.c b/io/tst-renameat.c new file mode 100644 index 0000000..fb49459 --- /dev/null +++ b/io/tst-renameat.c @@ -0,0 +1,149 @@ +#include <dirent.h> +#include <fcntl.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + + +static void prepare (void); +#define PREPARE(argc, argv) prepare () + +static int do_test (void); +#define TEST_FUNCTION do_test () + +#include "../test-skeleton.c" + +static int dir_fd; + +static void +prepare (void) +{ + size_t test_dir_len = strlen (test_dir); + static const char dir_name[] = "/tst-renameat.XXXXXX"; + + size_t dirbuflen = test_dir_len + sizeof (dir_name); + char *dirbuf = malloc (dirbuflen); + if (dirbuf == NULL) + { + puts ("out of memory"); + exit (1); + } + + snprintf (dirbuf, dirbuflen, "%s%s", test_dir, dir_name); + if (mkdtemp (dirbuf) == NULL) + { + puts ("cannot create temporary directory"); + exit (1); + } + + add_temp_file (dirbuf); + + dir_fd = open (dirbuf, O_RDONLY | O_DIRECTORY); + if (dir_fd == -1) + { + puts ("cannot open directory"); + exit (1); + } +} + + +static int +do_test (void) +{ + /* fdopendir takes over the descriptor, make a copy. */ + int dupfd = dup (dir_fd); + if (dupfd == -1) + { + puts ("dup failed"); + return 1; + } + if (lseek (dupfd, 0, SEEK_SET) != 0) + { + puts ("1st lseek failed"); + return 1; + } + + /* The directory should be empty safe the . and .. files. */ + DIR *dir = fdopendir (dupfd); + if (dir == NULL) + { + puts ("fdopendir failed"); + return 1; + } + struct dirent64 *d; + while ((d = readdir64 (dir)) != NULL) + if (strcmp (d->d_name, ".") != 0 && strcmp (d->d_name, "..") != 0) + { + printf ("temp directory contains file \"%s\"\n", d->d_name); + return 1; + } + closedir (dir); + + /* Try to create a file. */ + int fd = openat (dir_fd, "some-file", O_CREAT|O_RDWR|O_EXCL, 0666); + if (fd == -1) + { + if (errno == ENOSYS) + { + puts ("*at functions not supported"); + return 0; + } + + puts ("file creation failed"); + return 1; + } + write (fd, "hello", 5); + puts ("file created"); + + struct stat64 st1; + if (fstat64 (fd, &st1) != 0) + { + puts ("fstat64 failed"); + return 1; + } + + close (fd); + + if (renameat (dir_fd, "some-file", dir_fd, "another-file") != 0) + { + puts ("renameat failed"); + return 1; + } + + struct stat64 st2; + if (fstatat64 (dir_fd, "some-file", &st2, 0) == 0) + { + puts ("fstatat64 succeeded"); + return 1; + } + if (errno != ENOENT) + { + puts ("fstatat64 did not fail with ENOENT"); + return 1; + } + + if (fstatat64 (dir_fd, "another-file", &st2, 0) != 0) + { + puts ("2nd fstatat64 failed"); + return 1; + } + + if (st1.st_dev != st2.st_dev + || st1.st_ino != st2.st_ino + || st1.st_size != st2.st_size) + { + puts ("stat results do not match"); + return 1; + } + + if (unlinkat (dir_fd, "another-file", 0) != 0) + { + puts ("unlinkat failed"); + return 1; + } + + close (dir_fd); + + return 0; +} diff --git a/io/tst-unlinkat.c b/io/tst-unlinkat.c new file mode 100644 index 0000000..c25443c --- /dev/null +++ b/io/tst-unlinkat.c @@ -0,0 +1,178 @@ +#include <dirent.h> +#include <fcntl.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + + +static void prepare (void); +#define PREPARE(argc, argv) prepare () + +static int do_test (void); +#define TEST_FUNCTION do_test () + +#include "../test-skeleton.c" + +static int dir_fd; + +static void +prepare (void) +{ + size_t test_dir_len = strlen (test_dir); + static const char dir_name[] = "/tst-unlinkat.XXXXXX"; + + size_t dirbuflen = test_dir_len + sizeof (dir_name); + char *dirbuf = malloc (dirbuflen); + if (dirbuf == NULL) + { + puts ("out of memory"); + exit (1); + } + + snprintf (dirbuf, dirbuflen, "%s%s", test_dir, dir_name); + if (mkdtemp (dirbuf) == NULL) + { + puts ("cannot create temporary directory"); + exit (1); + } + + add_temp_file (dirbuf); + + dir_fd = open (dirbuf, O_RDONLY | O_DIRECTORY); + if (dir_fd == -1) + { + puts ("cannot open directory"); + exit (1); + } +} + + +static int +do_test (void) +{ + /* fdopendir takes over the descriptor, make a copy. */ + int dupfd = dup (dir_fd); + if (dupfd == -1) + { + puts ("dup failed"); + return 1; + } + if (lseek (dupfd, 0, SEEK_SET) != 0) + { + puts ("1st lseek failed"); + return 1; + } + + /* The directory should be empty safe the . and .. files. */ + DIR *dir = fdopendir (dupfd); + if (dir == NULL) + { + puts ("fdopendir failed"); + return 1; + } + struct dirent64 *d; + while ((d = readdir64 (dir)) != NULL) + if (strcmp (d->d_name, ".") != 0 && strcmp (d->d_name, "..") != 0) + { + printf ("temp directory contains file \"%s\"\n", d->d_name); + return 1; + } + closedir (dir); + + /* Try to create a file. */ + int fd = openat (dir_fd, "some-file", O_CREAT|O_RDWR|O_EXCL, 0666); + if (fd == -1) + { + if (errno == ENOSYS) + { + puts ("*at functions not supported"); + return 0; + } + + puts ("file creation failed"); + return 1; + } + write (fd, "hello", 5); + close (fd); + puts ("file created"); + + /* fdopendir takes over the descriptor, make a copy. */ + dupfd = dup (dir_fd); + if (dupfd == -1) + { + puts ("2nd dup failed"); + return 1; + } + if (lseek (dupfd, 0, SEEK_SET) != 0) + { + puts ("2nd lseek failed"); + return 1; + } + + /* The directory should be empty safe the . and .. files. */ + dir = fdopendir (dupfd); + if (dir == NULL) + { + puts ("2nd fdopendir failed"); + return 1; + } + bool seen_file = false; + while ((d = readdir64 (dir)) != NULL) + if (strcmp (d->d_name, ".") != 0 && strcmp (d->d_name, "..") != 0) + { + if (strcmp (d->d_name, "some-file") != 0) + { + printf ("temp directory contains file \"%s\"\n", d->d_name); + return 1; + } + + seen_file = true; + } + closedir (dir); + + if (!seen_file) + { + puts ("file not created in correct directory"); + return 1; + } + + /* Remove the file now. */ + if (unlinkat (dir_fd, "some-file", 0) != 0) + { + puts ("unlinkat failed"); + return 1; + } + + /* We won't need dir_fd anymore after this, so use it. */ + if (lseek (dir_fd, 0, SEEK_SET) != 0) + { + puts ("3rd lseek failed"); + return 1; + } + + /* The directory should be empty safe the . and .. files. */ + dir = fdopendir (dir_fd); + if (dir == NULL) + { + puts ("3rd fdopendir failed"); + return 1; + } + while ((d = readdir64 (dir)) != NULL) + if (strcmp (d->d_name, ".") != 0 && strcmp (d->d_name, "..") != 0) + { + if (strcmp (d->d_name, "some-file") == 0) + { + puts ("some-file not removed"); + return 1; + } + else + { + printf ("temp directory contains file \"%s\"\n", d->d_name); + return 1; + } + } + closedir (dir); + + return 0; +} |