Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/pkg
Path: blob/main/compat/file_at.c
2065 views
1
/*
2
* Copyright (c) 2014 Landon Fuller <[email protected]>
3
* All rights reserved.
4
*
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
7
* are met:
8
* 1. Redistributions of source code must retain the above copyright
9
* notice, this list of conditions and the following disclaimer
10
* in this position and unchanged.
11
* 2. Redistributions in binary form must reproduce the above copyright
12
* notice, this list of conditions and the following disclaimer in the
13
* documentation and/or other materials provided with the distribution.
14
*
15
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18
* IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
*/
26
27
#include <sys/param.h>
28
29
#include <bsd_compat.h>
30
#include <assert.h>
31
#include <fcntl.h>
32
#include <pthread.h>
33
#include <stdarg.h>
34
#include <unistd.h>
35
36
#if !HAVE_UNLINKAT || !HAVE_FSTATAT
37
38
static pthread_mutex_t file_at_lock = PTHREAD_MUTEX_INITIALIZER;
39
static int file_at_dfd = -1;
40
static char saved_cwd[MAXPATHLEN];
41
42
/**
43
* Acquire the cwd mutex and perform fchdir(dfd).
44
*
45
* On error, the mutex will be released automatically
46
* and a non-zero value will be returned.
47
*
48
* @param dfd The directory file descriptor to be passed to fchdir(), or
49
* AT_FDCWD to use the current working directory.
50
* @return The fchdir() result.
51
*/
52
static int
53
file_chdir_lock(int dfd)
54
{
55
int ret;
56
57
pthread_mutex_lock(&file_at_lock);
58
59
if (getcwd(saved_cwd, sizeof(saved_cwd)) == NULL)
60
saved_cwd[0] = '\0';
61
62
assert(file_at_dfd == -1);
63
file_at_dfd = dfd;
64
65
if (dfd == AT_FDCWD)
66
return 0;
67
68
ret = fchdir(dfd);
69
if (ret != 0) {
70
pthread_mutex_unlock(&file_at_lock);
71
return ret;
72
} else {
73
return ret;
74
}
75
}
76
77
/**
78
* Release the cwd mutex.
79
*/
80
static void
81
file_chdir_unlock(int dfd)
82
{
83
assert(file_at_dfd == dfd);
84
file_at_dfd = -1;
85
86
if (dfd == AT_FDCWD)
87
return;
88
89
if (saved_cwd[0] != '\0')
90
chdir(saved_cwd);
91
pthread_mutex_unlock(&file_at_lock);
92
}
93
#endif
94
95
#if !HAVE_FACCESSAT
96
int
97
faccessat(int fd, const char *path, int mode, int flag)
98
{
99
int ret;
100
char saved_cwd[MAXPATHLEN];
101
const char *cwd;
102
103
if ((cwd = getcwd(saved_cwd, sizeof(saved_cwd))) == NULL)
104
return (-1);
105
106
if ((ret = file_chdir_lock(fd) != 0))
107
return ret;
108
109
if (flag & AT_EACCESS) {
110
ret = eaccess(path, mode);
111
} else {
112
ret = access(path, mode);
113
}
114
115
file_chdir_unlock(fd);
116
return ret;
117
}
118
#endif
119
120
#if !HAVE_READLINKAT
121
ssize_t
122
readlinkat(int fd, const char *restrict path, char *restrict buf,
123
size_t bufsize)
124
{
125
int ret;
126
127
if ((ret = file_chdir_lock(fd) != 0))
128
return ret;
129
130
ret = readlink(path, buf, bufsize);
131
132
file_chdir_unlock(fd);
133
return ret;
134
}
135
#endif
136
137
#if !HAVE_FSTATAT
138
int
139
fstatat(int fd, const char *path, struct stat *buf, int flag)
140
{
141
int ret;
142
143
if ((ret = file_chdir_lock(fd) != 0))
144
return ret;
145
146
if (flag & AT_SYMLINK_NOFOLLOW) {
147
ret = lstat(path, buf);
148
} else {
149
ret = stat(path, buf);
150
}
151
152
file_chdir_unlock(fd);
153
return ret;
154
}
155
#endif
156
157
#if !HAVE_OPENAT
158
int
159
openat(int fd, const char *path, int flags, ...)
160
{
161
int ret;
162
va_list ap;
163
164
if ((ret = file_chdir_lock(fd) != 0))
165
return ret;
166
167
if (flags & O_CREAT) {
168
va_start(ap, flags);
169
ret = open(path, flags, va_arg(ap, int));
170
va_end(ap);
171
} else {
172
ret = open(path, flags);
173
}
174
175
file_chdir_unlock(fd);
176
return ret;
177
}
178
#endif
179
180
#if !HAVE_UNLINKAT
181
int
182
unlinkat(int fd, const char *path, int flag)
183
{
184
int ret;
185
186
if ((ret = file_chdir_lock(fd) != 0))
187
return ret;
188
189
if (flag & AT_REMOVEDIR) {
190
ret = rmdir(path);
191
} else {
192
ret = unlink(path);
193
}
194
195
file_chdir_unlock(fd);
196
return ret;
197
}
198
#endif
199
200