rmdir: work around cygwin 1.5.x and mingw bugs
[pspp] / tests / test-rmdir.c
1 /* Tests of rmdir.
2    Copyright (C) 2009 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 <http://www.gnu.org/licenses/>.  */
16
17 /* Written by Eric Blake <ebb9@byu.net>, 2009.  */
18
19 #include <config.h>
20
21 #include <unistd.h>
22
23 #include <fcntl.h>
24 #include <errno.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <sys/stat.h>
28
29 #if !HAVE_SYMLINK
30 # define symlink(a,b) (-1)
31 #endif
32
33 #define ASSERT(expr) \
34   do                                                                         \
35     {                                                                        \
36       if (!(expr))                                                           \
37         {                                                                    \
38           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__);  \
39           fflush (stderr);                                                   \
40           abort ();                                                          \
41         }                                                                    \
42     }                                                                        \
43   while (0)
44
45 #define BASE "test-rmdir.t"
46
47 int
48 main ()
49 {
50   /* Remove any leftovers from a previous partial run.  */
51   ASSERT (system ("rm -rf " BASE "*") == 0);
52
53   /* Setup.  */
54   ASSERT (mkdir (BASE "dir", 0700) == 0);
55   ASSERT (close (creat (BASE "dir/file", 0600)) == 0);
56
57   /* Basic error conditions.  */
58   errno = 0;
59   ASSERT (rmdir ("") == -1);
60   ASSERT (errno == ENOENT);
61   errno = 0;
62   ASSERT (rmdir (BASE "nosuch") == -1);
63   ASSERT (errno == ENOENT);
64   errno = 0;
65   ASSERT (rmdir (BASE "nosuch/") == -1);
66   ASSERT (errno == ENOENT);
67   errno = 0;
68   ASSERT (rmdir (".") == -1);
69   ASSERT (errno == EINVAL || errno == EBUSY);
70   /* Resulting errno after ".." or "/" is too varied to test; it is
71      reasonable to see any of EINVAL, EBUSY, EEXIST, ENOTEMPTY,
72      EACCES, EPERM.  */
73   ASSERT (rmdir ("..") == -1);
74   ASSERT (rmdir ("/") == -1);
75   ASSERT (rmdir ("///") == -1);
76   errno = 0;
77   ASSERT (rmdir (BASE "dir/file/") == -1);
78   ASSERT (errno == ENOTDIR);
79
80   /* Non-empty directory.  */
81   errno = 0;
82   ASSERT (rmdir (BASE "dir") == -1);
83   ASSERT (errno == EEXIST || errno == ENOTEMPTY);
84
85   /* Non-directory.  */
86   errno = 0;
87   ASSERT (rmdir (BASE "dir/file") == -1);
88   ASSERT (errno == ENOTDIR);
89
90   /* Empty directory.  */
91   ASSERT (unlink (BASE "dir/file") == 0);
92   errno = 0;
93   ASSERT (rmdir (BASE "dir/./") == -1);
94   ASSERT (errno == EINVAL || errno == EBUSY);
95   ASSERT (rmdir (BASE "dir") == 0);
96
97   /* Test symlink behavior.  Specifying trailing slash should remove
98      referent directory (POSIX), or cause ENOTDIR failure (Linux), but
99      not touch symlink.  We prefer the Linux behavior for its
100      intuitiveness (especially compared to rmdir("symlink-to-file/")),
101      but not enough to penalize POSIX systems with an rpl_rmdir.  */
102   if (symlink (BASE "dir", BASE "link") != 0)
103     {
104       fputs ("skipping test: symlinks not supported on this filesystem\n",
105              stderr);
106       return 77;
107     }
108   ASSERT (mkdir (BASE "dir", 0700) == 0);
109   errno = 0;
110   if (rmdir (BASE "link/") == 0)
111     {
112       struct stat st;
113       errno = 0;
114       ASSERT (stat (BASE "link", &st) == -1);
115       ASSERT (errno == ENOENT);
116     }
117   else
118     {
119       ASSERT (errno == ENOTDIR);
120       ASSERT (rmdir (BASE "dir") == 0);
121     }
122   ASSERT (unlink (BASE "link") == 0);
123
124   return 0;
125 }