2 Copyright (C) 2009, 2010 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 <http://www.gnu.org/licenses/>. */
17 /* Written by Eric Blake <ebb9@byu.net>, 2009. */
23 #include "signature.h"
24 SIGNATURE_CHECK (remove, int, (char const *));
35 #define BASE "test-remove.t"
40 /* Remove any leftovers from a previous partial run. */
41 system ("rm -rf " BASE "*");
44 ASSERT (mkdir (BASE "dir", 0700) == 0);
45 ASSERT (close (creat (BASE "dir/file", 0600)) == 0);
47 /* Basic error conditions. */
49 ASSERT (remove ("") == -1);
50 ASSERT (errno == ENOENT);
52 ASSERT (remove ("nosuch") == -1);
53 ASSERT (errno == ENOENT);
55 ASSERT (remove ("nosuch/") == -1);
56 ASSERT (errno == ENOENT);
58 ASSERT (remove (".") == -1);
59 ASSERT (errno == EINVAL || errno == EBUSY);
60 /* Resulting errno after ".." or "/" is too varied to test; it is
61 reasonable to see any of EINVAL, EEXIST, ENOTEMPTY, EACCES. */
62 ASSERT (remove ("..") == -1);
63 ASSERT (remove ("/") == -1);
64 ASSERT (remove ("///") == -1);
66 ASSERT (remove (BASE "dir/file/") == -1);
67 ASSERT (errno == ENOTDIR);
69 /* Non-empty directory. */
71 ASSERT (remove (BASE "dir") == -1);
72 ASSERT (errno == EEXIST || errno == ENOTEMPTY);
75 ASSERT (remove (BASE "dir/file") == 0);
77 /* Empty directory. */
79 ASSERT (remove (BASE "dir/.//") == -1);
80 ASSERT (errno == EINVAL || errno == EBUSY);
81 ASSERT (remove (BASE "dir") == 0);
83 /* Test symlink behavior. Specifying trailing slash should remove
84 referent directory, or cause ENOTDIR failure, but not touch
86 if (symlink (BASE "dir", BASE "link") != 0)
88 fputs ("skipping test: symlinks not supported on this file system\n",
92 ASSERT (mkdir (BASE "dir", 0700) == 0);
94 if (remove (BASE "link/") == 0)
98 ASSERT (stat (BASE "link", &st) == -1);
99 ASSERT (errno == ENOENT);
102 ASSERT (remove (BASE "dir") == 0);
105 ASSERT (lstat (BASE "link", &st) == 0);
106 ASSERT (S_ISLNK (st.st_mode));
108 ASSERT (remove (BASE "link") == 0);
109 /* Trailing slash on symlink to non-directory is an error. */
110 ASSERT (symlink (BASE "loop", BASE "loop") == 0);
112 ASSERT (remove (BASE "loop/") == -1);
113 ASSERT (errno == ELOOP || errno == ENOTDIR);
114 ASSERT (remove (BASE "loop") == 0);
115 ASSERT (close (creat (BASE "file", 0600)) == 0);
116 ASSERT (symlink (BASE "file", BASE "link") == 0);
118 ASSERT (remove (BASE "link/") == -1);
119 ASSERT (errno == ENOTDIR);
120 ASSERT (remove (BASE "link") == 0);
121 ASSERT (remove (BASE "file") == 0);