| 1 |
/* Tests of rmdir. |
| 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 |
/* This file is designed to test both rmdir(n) and |
| 20 |
unlinkat(AT_FDCWD,n,AT_REMOVEDIR). FUNC is the function to test. |
| 21 |
Assumes that BASE and ASSERT are already defined, and that |
| 22 |
appropriate headers are already included. If PRINT, then warn |
| 23 |
before returning status 77 when symlinks are unsupported. */ |
| 24 |
|
| 25 |
static int |
| 26 |
test_rmdir_func (int (*func) (char const *name), bool print) |
| 27 |
{ |
| 28 |
/* Setup. */ |
| 29 |
ASSERT (mkdir (BASE "dir", 0700) == 0); |
| 30 |
ASSERT (close (creat (BASE "dir/file", 0600)) == 0); |
| 31 |
|
| 32 |
/* Basic error conditions. */ |
| 33 |
errno = 0; |
| 34 |
ASSERT (func ("") == -1); |
| 35 |
ASSERT (errno == ENOENT); |
| 36 |
errno = 0; |
| 37 |
ASSERT (func (BASE "nosuch") == -1); |
| 38 |
ASSERT (errno == ENOENT); |
| 39 |
errno = 0; |
| 40 |
ASSERT (func (BASE "nosuch/") == -1); |
| 41 |
ASSERT (errno == ENOENT); |
| 42 |
errno = 0; |
| 43 |
ASSERT (func (".") == -1); |
| 44 |
ASSERT (errno == EINVAL || errno == EBUSY); |
| 45 |
/* Resulting errno after ".." or "/" is too varied to test; it is |
| 46 |
reasonable to see any of EINVAL, EBUSY, EEXIST, ENOTEMPTY, |
| 47 |
EACCES, EPERM. */ |
| 48 |
ASSERT (func ("..") == -1); |
| 49 |
ASSERT (func ("/") == -1); |
| 50 |
ASSERT (func ("///") == -1); |
| 51 |
errno = 0; |
| 52 |
ASSERT (func (BASE "dir/file/") == -1); |
| 53 |
ASSERT (errno == ENOTDIR); |
| 54 |
|
| 55 |
/* Non-empty directory. */ |
| 56 |
errno = 0; |
| 57 |
ASSERT (func (BASE "dir") == -1); |
| 58 |
ASSERT (errno == EEXIST || errno == ENOTEMPTY); |
| 59 |
|
| 60 |
/* Non-directory. */ |
| 61 |
errno = 0; |
| 62 |
ASSERT (func (BASE "dir/file") == -1); |
| 63 |
ASSERT (errno == ENOTDIR); |
| 64 |
|
| 65 |
/* Empty directory. */ |
| 66 |
ASSERT (unlink (BASE "dir/file") == 0); |
| 67 |
errno = 0; |
| 68 |
ASSERT (func (BASE "dir/.//") == -1); |
| 69 |
ASSERT (errno == EINVAL || errno == EBUSY || errno == EEXIST |
| 70 |
|| errno == ENOTEMPTY); |
| 71 |
ASSERT (func (BASE "dir") == 0); |
| 72 |
|
| 73 |
/* Test symlink behavior. Specifying trailing slash should remove |
| 74 |
referent directory (POSIX), or cause ENOTDIR failure (Linux), but |
| 75 |
not touch symlink. We prefer the Linux behavior for its |
| 76 |
intuitiveness (especially compared to rmdir("symlink-to-file/")), |
| 77 |
but not enough to penalize POSIX systems with an rpl_rmdir. */ |
| 78 |
if (symlink (BASE "dir", BASE "link") != 0) |
| 79 |
{ |
| 80 |
if (print) |
| 81 |
fputs ("skipping test: symlinks not supported on this file system\n", |
| 82 |
stderr); |
| 83 |
return 77; |
| 84 |
} |
| 85 |
ASSERT (mkdir (BASE "dir", 0700) == 0); |
| 86 |
errno = 0; |
| 87 |
if (func (BASE "link/") == 0) |
| 88 |
{ |
| 89 |
struct stat st; |
| 90 |
errno = 0; |
| 91 |
ASSERT (stat (BASE "link", &st) == -1); |
| 92 |
ASSERT (errno == ENOENT); |
| 93 |
} |
| 94 |
else |
| 95 |
{ |
| 96 |
ASSERT (errno == ENOTDIR); |
| 97 |
ASSERT (func (BASE "dir") == 0); |
| 98 |
} |
| 99 |
ASSERT (unlink (BASE "link") == 0); |
| 100 |
|
| 101 |
return 0; |
| 102 |
} |