This source file includes following definitions.
- rpl_unlinkat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 #include <config.h>
21
22 #include <unistd.h>
23
24 #include <errno.h>
25 #include <fcntl.h>
26 #include <string.h>
27 #include <sys/stat.h>
28
29 #include <stdlib.h>
30
31 #include "filename.h"
32 #include "openat.h"
33
34 #if HAVE_UNLINKAT
35
36 # undef unlinkat
37
38
39
40
41
42
43
44
45 int
46 rpl_unlinkat (int fd, char const *name, int flag)
47 {
48 size_t len;
49 int result = 0;
50
51 if (flag & AT_REMOVEDIR)
52 return unlinkat (fd, name, flag);
53
54 len = strlen (name);
55 if (len && ISSLASH (name[len - 1]))
56 {
57
58
59
60 struct stat st;
61 result = lstatat (fd, name, &st);
62 if (result == 0 || errno == EOVERFLOW)
63 {
64
65 char *short_name = malloc (len);
66 if (!short_name)
67 {
68 errno = EPERM;
69 return -1;
70 }
71 memcpy (short_name, name, len);
72 while (len && ISSLASH (short_name[len - 1]))
73 short_name[--len] = '\0';
74 if (len && (lstatat (fd, short_name, &st) || S_ISLNK (st.st_mode)))
75 {
76 free (short_name);
77 errno = EPERM;
78 return -1;
79 }
80 free (short_name);
81 result = 0;
82 }
83 }
84 if (!result)
85 {
86 # if UNLINK_PARENT_BUG
87 if (len >= 2 && name[len - 1] == '.' && name[len - 2] == '.'
88 && (len == 2 || ISSLASH (name[len - 3])))
89 {
90 errno = EISDIR;
91 return -1;
92 }
93 # endif
94 result = unlinkat (fd, name, flag);
95 }
96 return result;
97 }
98
99 #else
100
101
102
103
104
105
106
107
108
109 # define AT_FUNC_NAME unlinkat
110 # define AT_FUNC_F1 rmdir
111 # define AT_FUNC_F2 unlink
112 # define AT_FUNC_USE_F1_COND AT_REMOVEDIR
113 # define AT_FUNC_POST_FILE_PARAM_DECLS , int flag
114 # define AT_FUNC_POST_FILE_ARGS
115 # include "at-func.c"
116 # undef AT_FUNC_NAME
117 # undef AT_FUNC_F1
118 # undef AT_FUNC_F2
119 # undef AT_FUNC_USE_F1_COND
120 # undef AT_FUNC_POST_FILE_PARAM_DECLS
121 # undef AT_FUNC_POST_FILE_ARGS
122
123 #endif