我想使用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)
我正在传递一个struct呼叫my_fstore,该呼叫与fcntl呼叫执行f_preallocate时所需的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 struct中的" L"格式化器。关于我可以做些不同的建议?
您可以使用python fallocate库轻松地执行此操作,该库在Linux和OSX上导入Fallocate Call: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)