| 1 |
/* Tests of fstatat. |
| 2 |
Copyright (C) 2009-2025 Free Software Foundation, Inc. |
| 3 |
|
| 4 |
This program is free software: you can redistribute it and/or modify |
| 5 |
it under the terms of the GNU General Public License as published by |
| 6 |
the Free Software Foundation, either version 3 of the License, or |
| 7 |
(at your option) any later version. |
| 8 |
|
| 9 |
This program is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
GNU General Public License for more details. |
| 13 |
|
| 14 |
You should have received a copy of the GNU General Public License |
| 15 |
along with this program. If not, see <https://www.gnu.org/licenses/>. */ |
| 16 |
|
| 17 |
/* Written by Eric Blake <[email protected]>, 2009. */ |
| 18 |
|
| 19 |
#include <config.h> |
| 20 |
|
| 21 |
#include <sys/stat.h> |
| 22 |
|
| 23 |
#include "signature.h" |
| 24 |
SIGNATURE_CHECK (fstatat, int, (int, char const *, struct stat *, int)); |
| 25 |
|
| 26 |
#include <fcntl.h> |
| 27 |
#include <errno.h> |
| 28 |
#include <stdio.h> |
| 29 |
#include <stdlib.h> |
| 30 |
#include <unistd.h> |
| 31 |
|
| 32 |
#include "openat.h" |
| 33 |
#include "same-inode.h" |
| 34 |
#include "ignore-value.h" |
| 35 |
#include "macros.h" |
| 36 |
|
| 37 |
/* This program tests deprecated functions 'statat' and 'lstatat'. */ |
| 38 |
#if _GL_GNUC_PREREQ (4, 3) |
| 39 |
# pragma GCC diagnostic ignored "-Wdeprecated-declarations" |
| 40 |
#endif |
| 41 |
|
| 42 |
#ifndef BASE |
| 43 |
# define BASE "test-fstatat.t" |
| 44 |
#endif |
| 45 |
|
| 46 |
#include "test-lstat.h" |
| 47 |
#include "test-stat.h" |
| 48 |
|
| 49 |
static int dfd = AT_FDCWD; |
| 50 |
|
| 51 |
/* Wrapper around fstatat to test stat behavior. */ |
| 52 |
static int |
| 53 |
do_stat (char const *name, struct stat *st) |
| 54 |
{ |
| 55 |
#ifdef TEST_STATAT |
| 56 |
return statat (dfd, name, st); |
| 57 |
#else |
| 58 |
return fstatat (dfd, name, st, 0); |
| 59 |
#endif |
| 60 |
} |
| 61 |
|
| 62 |
/* Wrapper around fstatat to test lstat behavior. */ |
| 63 |
static int |
| 64 |
do_lstat (char const *name, struct stat *st) |
| 65 |
{ |
| 66 |
#ifdef TEST_STATAT |
| 67 |
return lstatat (dfd, name, st); |
| 68 |
#else |
| 69 |
return fstatat (dfd, name, st, AT_SYMLINK_NOFOLLOW); |
| 70 |
#endif |
| 71 |
} |
| 72 |
|
| 73 |
int |
| 74 |
main (_GL_UNUSED int argc, _GL_UNUSED char *argv[]) |
| 75 |
{ |
| 76 |
int result; |
| 77 |
|
| 78 |
/* Remove any leftovers from a previous partial run. */ |
| 79 |
ignore_value (system ("rm -rf " BASE "*")); |
| 80 |
|
| 81 |
/* Test behaviour for invalid file descriptors. */ |
| 82 |
{ |
| 83 |
struct stat statbuf; |
| 84 |
|
| 85 |
errno = 0; |
| 86 |
ASSERT (fstatat (-1, "foo", &statbuf, 0) == -1); |
| 87 |
ASSERT (errno == EBADF); |
| 88 |
} |
| 89 |
{ |
| 90 |
struct stat statbuf; |
| 91 |
|
| 92 |
close (99); |
| 93 |
errno = 0; |
| 94 |
ASSERT (fstatat (99, "foo", &statbuf, 0) == -1); |
| 95 |
ASSERT (errno == EBADF); |
| 96 |
} |
| 97 |
|
| 98 |
result = test_stat_func (do_stat, false); |
| 99 |
ASSERT (test_lstat_func (do_lstat, false) == result); |
| 100 |
dfd = open (".", O_RDONLY); |
| 101 |
ASSERT (0 <= dfd); |
| 102 |
ASSERT (test_stat_func (do_stat, false) == result); |
| 103 |
ASSERT (test_lstat_func (do_lstat, false) == result); |
| 104 |
ASSERT (close (dfd) == 0); |
| 105 |
|
| 106 |
/* FIXME - add additional tests of dfd not at current directory. */ |
| 107 |
|
| 108 |
if (result == 77) |
| 109 |
fputs ("skipping test: symlinks not supported on this file system\n", |
| 110 |
stderr); |
| 111 |
return (result ? result : test_exit_status); |
| 112 |
} |