summaryrefslogtreecommitdiff
path: root/io
diff options
context:
space:
mode:
authorJakub Jelinek <jakub@redhat.com>2005-11-15 08:22:03 +0000
committerJakub Jelinek <jakub@redhat.com>2005-11-15 08:22:03 +0000
commit22d7fbdbae4032639461a7a081762a05deee0529 (patch)
treeff70bd4613f1f05823b2417b2687449d73ac45b0 /io
parent1beea155aecf2e7d6bfa1399b7c0f4f53dde408c (diff)
Updated to fedora-glibc-20051115T0809
Diffstat (limited to 'io')
-rw-r--r--io/Makefile53
-rw-r--r--io/Versions6
-rw-r--r--io/fcntl.h38
-rw-r--r--io/fstatat.c58
-rw-r--r--io/fstatat64.c58
-rw-r--r--io/sys/stat.h47
-rw-r--r--io/tst-fchownat.c145
-rw-r--r--io/tst-fstatat.c143
-rw-r--r--io/tst-futimesat.c147
-rw-r--r--io/tst-openat.c169
-rw-r--r--io/tst-renameat.c149
-rw-r--r--io/tst-unlinkat.c178
12 files changed, 1165 insertions, 26 deletions
diff --git a/io/Makefile b/io/Makefile
index 906d1e3301..9339ffe699 100644
--- a/io/Makefile
+++ b/io/Makefile
@@ -1,4 +1,4 @@
-# Copyright (C) 1992-2002, 2003 Free Software Foundation, Inc.
+# Copyright (C) 1992-2002, 2003, 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
@@ -26,38 +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 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-fcntl bug-ftw1 bug-ftw2 bug-ftw3 bug-ftw4 tst-statvfs \
+ tst-openat tst-unlinkat tst-fstatat tst-futimesat \
+ tst-renameat tst-fchownat
distribute := ftwtest-sh
diff --git a/io/Versions b/io/Versions
index 5b311ee03b..65ee77182e 100644
--- a/io/Versions
+++ b/io/Versions
@@ -97,4 +97,10 @@ libc {
# n*
nftw; nftw64;
}
+ GLIBC_2.4 {
+ fchownat;
+ __fxstatat; __fxstatat64;
+ openat; openat64;
+ unlinkat;
+ }
}
diff --git a/io/fcntl.h b/io/fcntl.h
index ef9d5f9b81..8e13d33dcc 100644
--- a/io/fcntl.h
+++ b/io/fcntl.h
@@ -1,4 +1,5 @@
-/* Copyright (C) 1991,1992,1994-2001,2003,2004 Free Software Foundation, Inc.
+/* Copyright (C) 1991,1992,1994-2001,2003,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
@@ -55,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.
@@ -82,6 +92,32 @@ extern int __REDIRECT (open, (__const char *__file, int __oflag, ...), open64)
extern int open64 (__const char *__file, int __oflag, ...) __nonnull ((1));
#endif
+#ifdef __USE_GNU
+/* Similar to OPEN but a relative path name is interpreted relative to
+ the directory for which FD is a descriptor.
+
+ NOTE: some other OPENAT implementation support additional functionality
+ through this interface, especially using the O_XATTR flag. This is not
+ yet supported here.
+
+ This function is a cancellation point and therefore not marked with
+ __THROW. */
+# ifndef __USE_FILE_OFFSET64
+extern int openat (int __fd, __const char *__file, int __oflag, ...)
+ __nonnull ((2));
+# else
+# ifdef __REDIRECT
+extern int __REDIRECT (openat, (int __fd, __const char *__file, int __oflag,
+ ...), openat64) __nonnull ((2));
+# else
+# define openat openat64
+# endif
+# endif
+
+extern int openat64 (int __fd, __const char *__file, int __oflag, ...)
+ __nonnull ((2));
+#endif
+
/* Create and open FILE, with mode MODE. This takes an `int' MODE
argument because that is what `mode_t' will be widened to.
diff --git a/io/fstatat.c b/io/fstatat.c
new file mode 100644
index 0000000000..1ac80597a0
--- /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 0000000000..a14b42d42e
--- /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 7075003922..0a82ef39e8 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 0000000000..0cbf78b2b0
--- /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 0000000000..6ddf30d87b
--- /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 0000000000..c1e8d93f41
--- /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
new file mode 100644
index 0000000000..d10b654fa9
--- /dev/null
+++ b/io/tst-openat.c
@@ -0,0 +1,169 @@
+#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-openat.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 ("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 ("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;
+ }
+
+ int cwdfd = open (".", O_RDONLY | O_DIRECTORY);
+ if (cwdfd == -1)
+ {
+ puts ("cannot get descriptor for cwd");
+ return 1;
+ }
+
+ if (fchdir (dir_fd) != 0)
+ {
+ puts ("1st fchdir failed");
+ return 1;
+ }
+
+ if (unlink ("some-file") != 0)
+ {
+ puts ("unlink failed");
+ return 1;
+ }
+
+ if (fchdir (cwdfd) != 0)
+ {
+ puts ("2nd fchdir failed");
+ return 1;
+ }
+
+ close (dir_fd);
+ close (cwdfd);
+
+ return 0;
+}
diff --git a/io/tst-renameat.c b/io/tst-renameat.c
new file mode 100644
index 0000000000..fb494594f5
--- /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 0000000000..c25443c27f
--- /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;
+}