2 Copyright (C) 2009-2020 Free Software Foundation, Inc.
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.
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.
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/>. */
17 /* Written by Eric Blake <ebb9@byu.net>, 2009. */
23 #include "signature.h"
24 SIGNATURE_CHECK (remove
, int, (char const *));
33 #include "ignore-value.h"
36 #define BASE "test-remove.t"
41 /* Remove any leftovers from a previous partial run. */
42 ignore_value (system ("rm -rf " BASE
"*"));
45 ASSERT (mkdir (BASE
"dir", 0700) == 0);
46 ASSERT (close (creat (BASE
"dir/file", 0600)) == 0);
48 /* Basic error conditions. */
50 ASSERT (remove ("") == -1);
51 ASSERT (errno
== ENOENT
);
53 ASSERT (remove ("nosuch") == -1);
54 ASSERT (errno
== ENOENT
);
56 ASSERT (remove ("nosuch/") == -1);
57 ASSERT (errno
== ENOENT
);
59 ASSERT (remove (".") == -1);
60 ASSERT (errno
== EINVAL
|| errno
== EBUSY
);
61 /* Resulting errno after ".." or "/" is too varied to test; it is
62 reasonable to see any of EINVAL, EEXIST, ENOTEMPTY, EACCES. */
63 ASSERT (remove ("..") == -1);
64 ASSERT (remove ("/") == -1);
65 ASSERT (remove ("///") == -1);
67 ASSERT (remove (BASE
"dir/file/") == -1);
68 ASSERT (errno
== ENOTDIR
);
70 /* Non-empty directory. */
72 ASSERT (remove (BASE
"dir") == -1);
73 ASSERT (errno
== EEXIST
|| errno
== ENOTEMPTY
);
76 ASSERT (remove (BASE
"dir/file") == 0);
78 /* Empty directory. */
80 ASSERT (remove (BASE
"dir/.//") == -1);
81 ASSERT (errno
== EINVAL
|| errno
== EBUSY
|| errno
== EEXIST
);
82 ASSERT (remove (BASE
"dir") == 0);
84 /* Test symlink behavior. Specifying trailing slash should remove
85 referent directory, or cause ENOTDIR failure, but not touch
87 if (symlink (BASE
"dir", BASE
"link") != 0)
89 fputs ("skipping test: symlinks not supported on this file system\n",
93 ASSERT (mkdir (BASE
"dir", 0700) == 0);
95 if (remove (BASE
"link/") == 0)
99 ASSERT (stat (BASE
"link", &st
) == -1);
100 ASSERT (errno
== ENOENT
);
103 ASSERT (remove (BASE
"dir") == 0);
106 ASSERT (lstat (BASE
"link", &st
) == 0);
107 ASSERT (S_ISLNK (st
.st_mode
));
109 ASSERT (remove (BASE
"link") == 0);
110 /* Trailing slash on symlink to non-directory is an error. */
111 ASSERT (symlink (BASE
"loop", BASE
"loop") == 0);
113 ASSERT (remove (BASE
"loop/") == -1);
114 ASSERT (errno
== ELOOP
|| errno
== ENOTDIR
);
115 ASSERT (remove (BASE
"loop") == 0);
116 ASSERT (close (creat (BASE
"file", 0600)) == 0);
117 ASSERT (symlink (BASE
"file", BASE
"link") == 0);
119 ASSERT (remove (BASE
"link/") == -1);
120 ASSERT (errno
== ENOTDIR
);
121 ASSERT (remove (BASE
"link") == 0);
122 ASSERT (remove (BASE
"file") == 0);