2017-09-25 109 views
0

我想为使用python的文件预先分配存储空间。随着的fcntl,我可以℃下在预先分配存储:适用于Python下fcntl的fstore格式

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

事实证明你可以做到这一点很容易地使用这些进口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)