This repository has been archived by the owner on Oct 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_obnammodule.c
376 lines (322 loc) · 10 KB
/
_obnammodule.c
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*
* _obnammodule.c -- Python extensions for Obna
*
* Copyright (C) 2008, 2009 Lars Wirzenius <[email protected]>
*
* 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 3 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, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
/*
* This is a Python extension module written for Obnam, the backup
* software.
*
* This module provides a way to call the posix_fadvise function from
* Python. Obnam uses this to use set the POSIX_FADV_SEQUENTIAL and
* POSIX_FADV_DONTNEED flags, to make sure the kernel knows that it will
* read files sequentially and that the data does not need to be cached.
* This makes Obnam not trash the disk buffer cache, which is nice.
*/
#define _FILE_OFFSET_BITS 64
#include <Python.h>
#ifndef _XOPEN_SOURCE
#define _XOPEN_SOURCE 600
#endif
#ifdef __linux__
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <linux/fs.h>
#include <sys/ioctl.h>
#endif
#define _POSIX_C_SOURCE 200809L
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#ifdef __FreeBSD__
#include <sys/extattr.h>
#define NO_NANOSECONDS 1
#else
#include <sys/xattr.h>
#define NO_NANOSECONDS 0
#endif
static PyObject *
fadvise_dontneed(PyObject *self, PyObject *args)
{
#if POSIX_FADV_DONTNEED
int fd;
/* Can't use off_t for offset and len, since PyArg_ParseTuple
doesn't know it. */
unsigned long long offset;
unsigned long long len;
int ret;
if (!PyArg_ParseTuple(args, "iLL", &fd, &offset, &len))
return NULL;
ret = posix_fadvise(fd, offset, len, POSIX_FADV_DONTNEED);
return Py_BuildValue("i", ret);
#else
return Py_BuildValue("i", 0);
#endif
}
static PyObject *
utimensat_wrapper(PyObject *self, PyObject *args)
{
int ret;
const char *filename;
long atime_sec, atime_nsec;
long mtime_sec, mtime_nsec;
#if NO_NANOSECONDS
struct timeval tv[2];
#else
struct timespec tv[2];
#endif
if (!PyArg_ParseTuple(args, "sllll",
&filename,
&atime_sec,
&atime_nsec,
&mtime_sec,
&mtime_nsec))
return NULL;
#if NO_NANOSECONDS
tv[0].tv_sec = atime_sec;
tv[0].tv_usec = atime_nsec / 1000;
tv[1].tv_sec = mtime_sec;
tv[1].tv_usec = mtime_nsec / 1000;
ret = lutimes(filename, tv);
#else
tv[0].tv_sec = atime_sec;
tv[0].tv_nsec = atime_nsec;
tv[1].tv_sec = mtime_sec;
tv[1].tv_nsec = mtime_nsec;
ret = utimensat(AT_FDCWD, filename, tv, AT_SYMLINK_NOFOLLOW);
#endif
if (ret == -1)
ret = errno;
return Py_BuildValue("i", ret);
}
/*
* Since we can't set nanosecond mtime and atimes on some platforms, also
* don't retrieve that level of precision from lstat(), so comparisons
* work.
*/
static unsigned long long
remove_precision(unsigned long long nanoseconds)
{
#if NO_NANOSECONDS
return nanoseconds - (nanoseconds % 1000);
#else
return nanoseconds;
#endif
}
static PyObject *
lstat_wrapper(PyObject *self, PyObject *args)
{
int ret;
const char *filename;
struct stat st = {0};
if (!PyArg_ParseTuple(args, "s", &filename))
return NULL;
ret = lstat(filename, &st);
if (ret == -1)
ret = errno;
return Py_BuildValue("iKKKKKKKLLLLKLKLK",
ret,
(unsigned long long) st.st_dev,
(unsigned long long) st.st_ino,
(unsigned long long) st.st_mode,
(unsigned long long) st.st_nlink,
(unsigned long long) st.st_uid,
(unsigned long long) st.st_gid,
(unsigned long long) st.st_rdev,
(long long) st.st_size,
(long long) st.st_blksize,
(long long) st.st_blocks,
(long long) st.st_atim.tv_sec,
remove_precision(st.st_atim.tv_nsec),
(long long) st.st_mtim.tv_sec,
remove_precision(st.st_mtim.tv_nsec),
(long long) st.st_ctim.tv_sec,
remove_precision(st.st_ctim.tv_nsec));
}
static PyObject *
llistxattr_wrapper(PyObject *self, PyObject *args)
{
const char *filename;
size_t bufsize;
PyObject *o;
char* buf;
ssize_t n;
if (!PyArg_ParseTuple(args, "s", &filename))
return NULL;
#ifdef __FreeBSD__
bufsize = extattr_list_link(filename, EXTATTR_NAMESPACE_USER, NULL, 0);
buf = malloc(bufsize);
if (buf == NULL) {
Py_INCREF(Py_None);
return Py_None;
}
n = extattr_list_link(filename, EXTATTR_NAMESPACE_USER, buf, bufsize);
if (n >= 0) {
/* Convert from length-prefixed BSD style to '\0'-suffixed
Linux style. */
size_t i = 0;
while (i < n) {
unsigned char length = (unsigned char) buf[i];
memmove(buf + i, buf + i + 1, length);
buf[i + length] = '\0';
i += length + 1;
}
o = Py_BuildValue("s#", buf, (int) n);
} else {
o = Py_BuildValue("i", errno);
}
free(buf);
#else
bufsize = 0;
o = NULL;
do {
bufsize += 1024;
buf = malloc(bufsize);
if (buf == NULL) {
Py_INCREF(Py_None);
return Py_None;
}
n = llistxattr(filename, buf, bufsize);
if (n >= 0)
o = Py_BuildValue("s#", buf, (int) n);
else if (n == -1 && errno != ERANGE)
o = Py_BuildValue("i", errno);
free(buf);
} while (o == NULL);
#endif
return o;
}
static PyObject *
lgetxattr_wrapper(PyObject *self, PyObject *args)
{
const char *filename;
const char *attrname;
size_t bufsize;
PyObject *o;
if (!PyArg_ParseTuple(args, "ss", &filename, &attrname))
return NULL;
bufsize = 0;
o = NULL;
do {
bufsize += 1024;
char *buf = malloc(bufsize);
if (buf == NULL) {
Py_INCREF(Py_None);
return Py_None;
}
#ifdef __FreeBSD__
int n = extattr_get_link(filename, EXTATTR_NAMESPACE_USER, attrname, buf, bufsize);
#else
ssize_t n = lgetxattr(filename, attrname, buf, bufsize);
#endif
if (n >= 0)
o = Py_BuildValue("s#", buf, (int) n);
else if (n == -1 && errno != ERANGE)
o = Py_BuildValue("i", errno);
free(buf);
} while (o == NULL);
return o;
}
static PyObject *
lsetxattr_wrapper(PyObject *self, PyObject *args)
{
const char *filename;
const char *name;
const char *value;
int size;
int ret;
if (!PyArg_ParseTuple(args, "sss#", &filename, &name, &value, &size))
return NULL;
#ifdef __FreeBSD__
ret = extattr_set_link(filename, EXTATTR_NAMESPACE_USER, name, value, size);
#else
ret = lsetxattr(filename, name, value, size, 0);
#endif
if (ret == -1)
ret = errno;
return Py_BuildValue("i", ret);
}
#if defined __linux__ && defined FS_IOC_GETFLAGS
static PyObject *
isnodump_wrapper(PyObject *self, PyObject *args)
{
const char *filename;
int flags, fd;
if (!PyArg_ParseTuple(args, "s", &filename))
return NULL;
{
struct stat stat_buf;
/* First, check that the file is a regular file or a directory. Flags may not appear on anything else. */
if (lstat(filename, &stat_buf) == -1)
return PyErr_SetFromErrnoWithFilename(PyExc_OSError, filename);
if (!(S_ISREG(stat_buf.st_mode) || S_ISDIR(stat_buf.st_mode)))
Py_RETURN_FALSE;
}
fd = open(filename, O_RDONLY | O_NOATIME | O_NOCTTY | O_NONBLOCK | O_NOFOLLOW);
/* If permission to use O_NOATIME is denied, try again without it. */
if (fd == -1 && errno == EPERM)
fd = open(filename, O_RDONLY | O_NOCTTY | O_NONBLOCK | O_NOFOLLOW);
if (fd == -1)
return PyErr_SetFromErrnoWithFilename(PyExc_OSError, filename);
if (ioctl(fd, FS_IOC_GETFLAGS, &flags) == -1) {
if (errno == ENOTTY) {
/* This error is raised if the filesystem in question doesn't support flags. */
close(fd);
Py_RETURN_FALSE;
}
else {
PyErr_SetFromErrnoWithFilename(PyExc_OSError, filename);
close(fd);
return NULL;
}
}
close(fd);
if (flags & FS_NODUMP_FL)
Py_RETURN_TRUE;
else
Py_RETURN_FALSE;
}
#endif
static PyMethodDef methods[] = {
{"fadvise_dontneed", fadvise_dontneed, METH_VARARGS,
"Call posix_fadvise(2) with POSIX_FADV_DONTNEED argument."},
{"utimensat", utimensat_wrapper, METH_VARARGS,
"utimensat(2) wrapper."},
{"lstat", lstat_wrapper, METH_VARARGS,
"lstat(2) wrapper; arg is filename, returns tuple."},
{"llistxattr", llistxattr_wrapper, METH_VARARGS,
"llistxattr(2) wrapper; arg is filename, returns tuple."},
{"lgetxattr", lgetxattr_wrapper, METH_VARARGS,
"lgetxattr(2) wrapper; arg is filename, returns tuple."},
{"lsetxattr", lsetxattr_wrapper, METH_VARARGS,
"lsetxattr(2) wrapper; arg is filename, returns errno."},
#if defined __linux__ && defined FS_IOC_GETFLAGS
{"isnodump", isnodump_wrapper, METH_VARARGS,
"checks if a file has the Linux nodump flag set; arg is filename, returns boolean."},
#endif
{NULL, NULL, 0, NULL} /* Sentinel */
};
PyMODINIT_FUNC
init_obnam(void)
{
(void) Py_InitModule("_obnam", methods);
}