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. */
30 #include "unlinkdir.h"
33 # define symlink(a,b) (-1)
36 #define ASSERT(expr) \
41 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
48 #define BASE "test-unlink.t"
53 /* Remove any leftovers from a previous partial run. */
54 ASSERT (system ("rm -rf " BASE "*") == 0);
57 ASSERT (mkdir (BASE "dir", 0700) == 0);
58 ASSERT (close (creat (BASE "dir/file", 0600)) == 0);
60 /* Basic error conditions. */
62 ASSERT (unlink ("") == -1);
63 ASSERT (errno == ENOENT);
65 ASSERT (unlink (BASE "nosuch") == -1);
66 ASSERT (errno == ENOENT);
68 ASSERT (unlink (BASE "nosuch/") == -1);
69 ASSERT (errno == ENOENT);
70 /* Resulting errno after directories is rather varied across
71 implementations (EPERM, EINVAL, EACCES, EBUSY, EISDIR, ENOTSUP);
72 however, we must be careful to not attempt unlink on a directory
73 unless we know it must fail. */
74 if (cannot_unlink_dir ())
76 ASSERT (unlink (".") == -1);
77 ASSERT (unlink ("..") == -1);
78 ASSERT (unlink ("/") == -1);
79 ASSERT (unlink (BASE "dir") == -1);
80 ASSERT (mkdir (BASE "dir1", 0700) == 0);
81 ASSERT (unlink (BASE "dir1") == -1);
82 ASSERT (rmdir (BASE "dir1") == 0);
85 ASSERT (unlink (BASE "dir/file/") == -1);
86 ASSERT (errno == ENOTDIR);
88 /* Test symlink behavior. Specifying trailing slash will attempt
89 unlink of a directory, so only attempt it if we know it must
91 if (symlink (BASE "dir", BASE "link") != 0)
93 ASSERT (unlink (BASE "dir/file") == 0);
94 ASSERT (rmdir (BASE "dir") == 0);
95 fputs ("skipping test: symlinks not supported on this filesystem\n",
99 if (cannot_unlink_dir ())
100 ASSERT (unlink (BASE "link/") == -1);
101 ASSERT (unlink (BASE "link") == 0);
102 ASSERT (symlink (BASE "dir/file", BASE "link") == 0);
103 /* Order here proves unlink of a symlink does not follow through to
105 ASSERT (unlink (BASE "link") == 0);
106 ASSERT (unlink (BASE "dir/file") == 0);
107 ASSERT (rmdir (BASE "dir") == 0);