fallocate vs posix_fallocate

前端 未结 3 816
-上瘾入骨i
-上瘾入骨i 2020-12-11 16:24

I am debating which function to use between posix_fallocate and fallocate. posix_fallocate writes a file right away (initializes the

3条回答
  •  有刺的猬
    2020-12-11 17:06

    I take it you didn't look at the documentation that says

       The mode argument determines the operation to be performed on the given range.
       Currently only one flag is supported for mode:
    
       FALLOC_FL_KEEP_SIZE
              This flag allocates and initializes to zero the disk space within the
              range specified by offset and len.  After a successful call, subsequent
              writes into this range are guaranteed not to fail because of lack of
              disk space.  Preallocating zeroed blocks beyond the end of the file is
              useful for optimizing append workloads.  Preallocating blocks does not
              change the file size (as reported by stat(2)) even if it is less than
              offset+len.
    
       If FALLOC_FL_KEEP_SIZE flag is not specified in mode, the default behavior is
       almost same as when this flag is specified.  The only difference is that on
       success, the file size will be changed if offset + len is greater than the
       file size.  This default behavior closely resembles the behavior of the
       posix_fallocate(3) library function, and is intended as a method of optimally
       implementing that function.
    

    The man page for posix_fallocate() doesn't appear to have the same thing mentioned, but instead, looking at the source here, it seems to write each block of the file (line 88).

    man fallocate man posix_fallocate

提交回复
热议问题