2 Copyright (C) 2009 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. */
19 /* This file is designed to test both unlink(n) and
20 unlinkat(AT_FDCWD,n,0). FUNC is the function to test. Assumes
21 that BASE and ASSERT are already defined, and that appropriate
22 headers are already included. If PRINT, then warn before returning
23 status 77 when symlinks are unsupported. */
26 test_unlink_func (int (*func) (char const *name), bool print)
28 /* Remove any leftovers from a previous partial run. */
29 ASSERT (system ("rm -rf " BASE "*") == 0);
32 ASSERT (mkdir (BASE "dir", 0700) == 0);
33 ASSERT (close (creat (BASE "dir/file", 0600)) == 0);
35 /* Basic error conditions. */
37 ASSERT (func ("") == -1);
38 ASSERT (errno == ENOENT);
40 ASSERT (func (BASE "nosuch") == -1);
41 ASSERT (errno == ENOENT);
43 ASSERT (func (BASE "nosuch/") == -1);
44 ASSERT (errno == ENOENT);
45 /* Resulting errno after directories is rather varied across
46 implementations (EPERM, EINVAL, EACCES, EBUSY, EISDIR, ENOTSUP);
47 however, we must be careful to not attempt unlink on a directory
48 unless we know it must fail. */
49 if (cannot_unlink_dir ())
51 ASSERT (func (".") == -1);
52 ASSERT (func ("..") == -1);
53 ASSERT (func ("/") == -1);
54 ASSERT (func (BASE "dir") == -1);
55 ASSERT (mkdir (BASE "dir1", 0700) == 0);
56 ASSERT (func (BASE "dir1") == -1);
57 ASSERT (rmdir (BASE "dir1") == 0);
60 ASSERT (func (BASE "dir/file/") == -1);
61 ASSERT (errno == ENOTDIR);
63 /* Test symlink behavior. Specifying trailing slash will attempt
64 unlink of a directory, so only attempt it if we know it must
66 if (symlink (BASE "dir", BASE "link") != 0)
68 ASSERT (func (BASE "dir/file") == 0);
69 ASSERT (rmdir (BASE "dir") == 0);
71 fputs ("skipping test: symlinks not supported on this filesystem\n",
75 if (cannot_unlink_dir ())
76 ASSERT (func (BASE "link/") == -1);
77 ASSERT (func (BASE "link") == 0);
78 ASSERT (symlink (BASE "dir/file", BASE "link") == 0);
79 /* Order here proves unlink of a symlink does not follow through to
81 ASSERT (func (BASE "link") == 0);
82 ASSERT (func (BASE "dir/file") == 0);
83 ASSERT (rmdir (BASE "dir") == 0);