我想使用python为文件预分配存储空间。使用fcntl,我可以在C:
下预分配存储空间 int fd = myFileHandle;
fstore_t store = {F_ALLOCATECONTIG, F_PEOFPOSMODE, 0, aLength};
int ret = fcntl(fd, F_PREALLOCATE, &store);
if(-1 == ret){
store.fst_flags = F_ALLOCATEALL;
ret = fcntl(fd, F_PREALLOCATE, &store);
if (-1 == ret)
return false;
当我尝试在Python下执行类似的操作时,我收到错误22:
F_ALLOCATECONTIG = 2
F_PEOFPOSMODE = 3
F_PREALLOCATE = 42
f = open(source, 'r')
f.seek(0, os.SEEK_END)
size = f.tell()
f.seek(0, os.SEEK_SET)
my_fstore = struct.pack('lllll', F_ALLOCATECONTIG, F_PEOFPOSMODE, 0, size, 0)
d = open(destination, 'w')
fcntl.fcntl(d.fileno(), F_PREALLOCATE, my_fstore)
我正在传递一个结构调用my_fstore,它应该与执行F_PREALLOCATE时fcntl调用所需的c结构相同。
/* fstore_t type used by F_DEALLOCATE and F_PREALLOCATE commands */
typedef struct fstore {
unsigned int fst_flags; /* IN: flags word */
int fst_posmode; /* IN: indicates use of offset field */
off_t fst_offset; /* IN: start of the region */
off_t fst_length; /* IN: size of the region */
off_t fst_bytesalloc; /* OUT: number of bytes allocated */
} fstore_t;
结构中的所有元素都应该是64位长度,因此python结构中的'l'格式化程序。关于我可以做些什么不同的任何建议?
答案 0 :(得分:0)
事实证明你可以使用python fallocate库轻松完成这个工作,该库在linux和osx上导入fallocate调用: https://pypi.python.org/pypi/fallocate/1.6.1
话虽这么说,我能够在OSX上使用以下fcntl配置来完成此任务:
F_ALLOCATECONTIG = 2
F_PEOFPOSMODE = 3
F_PREALLOCATE = 42
f = open(source, 'r')
f.seek(0, os.SEEK_END)
size = f.tell()
f.seek(0, os.SEEK_SET)
d = open(destination, 'w')
params = struct.pack('Iiqq', F_ALLOCATECONTIG, F_PEOFPOSMODE, 0, size)
fcntl.fcntl(d.fileno(), F_PREALLOCATE, params)