forked from distributed-system-analysis/smallfile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fallocate.py
61 lines (43 loc) · 1.48 KB
/
fallocate.py
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import ctypes
import ctypes.util
import os
# reserve space for the contents of file before you write it
# (hope to disable preallocation)
OK = 0
NOTOK = 0x01
# the mode argument to fallocate is defined in /usr/include/linux/falloc.h
FALLOC_FL_KEEP_SIZE = 0x01 # default is extend size
FALLOC_FL_PUNCH_HOLE = 0x02 # de-allocates range
# this function is used if we can't load the real libc function
def noop_libc_function(*args):
return OK
# I have no idea what this code really does, but strace says it works.
# does this code work under Cygwin?
def load_libc_function(func_name):
func = noop_libc_function
try:
libc = ctypes.CDLL(ctypes.util.find_library('c'))
func = getattr(libc, func_name)
except AttributeError:
# print("Unable to locate %s in libc. Leaving as a no-op."% func_name)
pass
return func
# do this at module load time
_posix_fallocate = load_libc_function('fallocate64')
# mode is one of FALLOC constants above
def fallocate(fd, mode, offset, length):
return _posix_fallocate(fd,
mode,
ctypes.c_uint64(offset),
ctypes.c_uint64(length))
# unit test
if __name__ == '__main__':
fd = os.open('/tmp/foo', os.O_WRONLY | os.O_CREAT)
assert fd > 0x02
ret = fallocate(fd, FALLOC_FL_KEEP_SIZE, 0, 8)
assert ret == OK
ret = os.write(fd, 'hi there')
assert ret == 8
os.close(fd)