2017-09-25 85 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)