-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathdirect.c
More file actions
316 lines (263 loc) · 7.18 KB
/
direct.c
File metadata and controls
316 lines (263 loc) · 7.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/* fuse-overlayfs: Overlay Filesystem in Userspace
Copyright (C) 2018 Giuseppe Scrivano <giuseppe@scrivano.org>
Copyright (C) 2018-2019 Red Hat Inc.
Copyright (C) 2001-2007 Miklos Szeredi <miklos@szeredi.hu>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define _GNU_SOURCE
#define _FILE_OFFSET_BITS 64
#include <config.h>
#include "fuse-overlayfs.h"
#include "limits.h"
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/xattr.h>
#include "utils.h"
static int
direct_file_exists (struct ovl_layer *l, const char *pathname)
{
return file_exists_at (l->fd, pathname);
}
static int
direct_listxattr (struct ovl_layer *l, const char *path, char *buf, size_t size)
{
char full_path[PATH_MAX];
int ret;
ret = snprintf (full_path, sizeof (full_path), "/proc/self/fd/%d/%s", l->fd, path);
if (ret >= sizeof (full_path))
{
errno = ENAMETOOLONG;
return -1;
}
return llistxattr (full_path, buf, size);
}
static int
direct_getxattr (struct ovl_layer *l, const char *path, const char *name, char *buf, size_t size)
{
char full_path[PATH_MAX];
int ret;
ret = snprintf (full_path, sizeof (full_path), "/proc/self/fd/%d/%s", l->fd, path);
if (ret >= sizeof (full_path))
{
errno = ENAMETOOLONG;
return -1;
}
return lgetxattr (full_path, name, buf, size);
}
static int
direct_fstat (struct ovl_layer *l, int fd, const char *path, unsigned int mask, struct stat *st)
{
int ret;
#ifdef HAVE_STATX
struct statx stx;
ret = statx (fd, "", AT_STATX_DONT_SYNC | AT_EMPTY_PATH, mask, &stx);
if (ret < 0 && (errno == ENOSYS || errno == EINVAL))
goto fallback;
if (ret == 0)
{
statx_to_stat (&stx, st);
return override_mode (l, fd, NULL, path, st);
}
return ret;
#endif
fallback:
ret = fstat (fd, st);
if (ret != 0)
return ret;
return override_mode (l, fd, NULL, path, st);
}
static int
direct_statat (struct ovl_layer *l, const char *path, struct stat *st, int flags, unsigned int mask)
{
int ret;
#ifdef HAVE_STATX
struct statx stx;
ret = statx (l->fd, path, AT_STATX_DONT_SYNC | flags, mask, &stx);
if (ret < 0 && (errno == ENOSYS || errno == EINVAL))
goto fallback;
if (ret == 0)
{
statx_to_stat (&stx, st);
return override_mode (l, -1, NULL, path, st);
}
return ret;
#endif
fallback:
ret = fstatat (l->fd, path, st, flags);
if (ret != 0)
return ret;
return override_mode (l, -1, NULL, path, st);
}
static struct dirent *
direct_readdir (void *dirp)
{
return readdir (dirp);
}
static void *
direct_opendir (struct ovl_layer *l, const char *path)
{
cleanup_close int cleanup_fd = -1;
DIR *dp = NULL;
cleanup_fd = TEMP_FAILURE_RETRY (safe_openat (l->fd, path, O_DIRECTORY, 0));
if (cleanup_fd < 0)
return NULL;
dp = fdopendir (cleanup_fd);
if (dp == NULL)
return NULL;
cleanup_fd = -1;
return dp;
}
static int
direct_closedir (void *dirp)
{
return closedir (dirp);
}
static int
direct_openat (struct ovl_layer *l, const char *path, int flags, mode_t mode)
{
return TEMP_FAILURE_RETRY (safe_openat (l->fd, path, flags, mode));
}
static ssize_t
direct_readlinkat (struct ovl_layer *l, const char *path, char *buf, size_t bufsiz)
{
return TEMP_FAILURE_RETRY (readlinkat (l->fd, path, buf, bufsiz));
}
static ino_t
direct_get_nfs_filehandle (const struct ovl_layer *l, const char *path)
{
int mount_id;
int ret = name_to_handle_at (l->fd, path, l->fh, &mount_id, 0);
if (ret == -1)
return 0;
ino_t h = 0xcbf29ce484222325ULL;
for (size_t i = 0; i < l->fh->handle_bytes; i++)
{
h ^= l->fh->f_handle[i];
h *= 0x100000001b3ULL;
}
return h;
}
/* Returns:
-1 - Error
0 - NFS filehandles not supported
1 - Can use NFS filehandles */
static int
has_nfs_filehandles (struct ovl_layer *l, const char *path)
{
struct file_handle *tmp_fh;
int mount_id;
int ret;
tmp_fh = malloc (sizeof (*tmp_fh));
if (tmp_fh == NULL)
return -1;
tmp_fh->handle_bytes = 0;
ret = name_to_handle_at (AT_FDCWD, path, tmp_fh, &mount_id, 0);
if (ret == -1 && errno == ENOTSUP)
{
free (tmp_fh);
return 0;
}
/* previous call should fail with EOVERFLOW and handle_bytes replaced with
* the size of the handle. EOVERFLOW can also occur if no filehandle is
* available in a system that does support file-handle lookup.
*/
if (ret != -1 || errno != EOVERFLOW || tmp_fh->handle_bytes == 0)
{
free (tmp_fh);
return -1;
}
l->fh = realloc (tmp_fh, tmp_fh->handle_bytes + sizeof (*l->fh));
if (! l->fh)
{
free (tmp_fh);
return -1;
}
ret = name_to_handle_at (AT_FDCWD, path, l->fh, &mount_id, 0);
if (ret == -1)
{
free (l->fh);
l->fh = NULL;
return 0;
}
return 1;
}
static int
direct_load_data_source (struct ovl_layer *l, const char *opaque, const char *path, int n_layer)
{
char tmp[64];
struct stat st;
l->path = realpath (path, NULL);
if (l->path == NULL)
{
fprintf (stderr, "cannot resolve path %s\n", path);
return -1;
}
l->fd = open (path, O_DIRECTORY);
if (l->fd < 0)
{
free (l->path);
l->path = NULL;
return l->fd;
}
if (fstat (l->fd, &st) == -1)
{
close (l->fd);
free (l->path);
l->path = NULL;
return -1;
}
else
l->st_dev = st.st_dev;
l->nfs_filehandles = has_nfs_filehandles (l, l->path);
if (fgetxattr (l->fd, XATTR_PRIVILEGED_OVERRIDE_STAT, tmp, sizeof (tmp)) >= 0)
l->stat_override_mode = STAT_OVERRIDE_PRIVILEGED;
else if (fgetxattr (l->fd, XATTR_OVERRIDE_CONTAINERS_STAT, tmp, sizeof (tmp)) >= 0)
l->stat_override_mode = STAT_OVERRIDE_CONTAINERS;
else if (fgetxattr (l->fd, XATTR_OVERRIDE_STAT, tmp, sizeof (tmp)) >= 0)
l->stat_override_mode = STAT_OVERRIDE_USER;
return 0;
}
static int
direct_cleanup (struct ovl_layer *l)
{
return 0;
}
static int
direct_num_of_layers (const char *opaque, const char *path)
{
return 1;
}
static bool
direct_support_acls (struct ovl_layer *l)
{
char value[32];
return fgetxattr (l->fd, ACL_XATTR, value, sizeof (value)) >= 0
|| errno != ENOTSUP;
}
struct data_source direct_access_ds = {
.num_of_layers = direct_num_of_layers,
.load_data_source = direct_load_data_source,
.cleanup = direct_cleanup,
.file_exists = direct_file_exists,
.statat = direct_statat,
.fstat = direct_fstat,
.opendir = direct_opendir,
.readdir = direct_readdir,
.closedir = direct_closedir,
.openat = direct_openat,
.getxattr = direct_getxattr,
.listxattr = direct_listxattr,
.readlinkat = direct_readlinkat,
.support_acls = direct_support_acls,
.get_nfs_filehandle = direct_get_nfs_filehandle,
};