Friday, 15 February 2013

c++ - Does posix_fallocate work with files opened with appened mode? -


I am trying to undo the disk space for file operations, however, I get a strange gesture that posix_fallocate Allocates only one byte, I ask it to allocate disk space for files opened with embedded mode and file contents are also unpredictable. Does anyone know this problem? And my test code is,


  #include & lt; Cstdio & gt; # Include & lt; Fcntl.h & gt; # Include & lt; Unistd.h & gt; # Include & lt; Sys / stat.h & gt; # Include & lt; Cerrno & gt; Int main (int argc, char ** argv) {FILE * fp = fopen ("append.txt", "w"); For (int i = 0; i & lt; 5; ++ i) fprintf (FP, "## Test Loop% d \ n", i); Fclose (fp); Sleep (1); Int fid = open ("append.txt", O_WRONLY | O_APPEND); Structure status status; Fstat (fid, and position); Printf ("Notice: sizeof 'append.txt' is% ld bytes. \ N", status.st_size); Int ret = posix_fallocate (feeds, (off-t) status.st_size, 1024); If (rate) {switch (rate) {case EBADF: fprintf (stderr, "error:% d is not a valid file descriptor, or is not opened for writing. \ N", fid); break; Case EFBIG: fprintf (stderr, "Error: max file size exceeded. \ N"); break; Case ENOSPC: fprintf (stderr, "Error: Not enough space left on device \ n"); break; Default: Break; }} Fstat (fid, and position); Printf ("Notice: sizeof 'append.txt' is% ld bytes. \ N", status.st_size); Char * hello = "Hello World \ n"; Write (FID, Hello, 12); Close (FID); Return 0; }   

and should have expected results,

  ## Test Loop 0 ## Test Loop 1 ## Test Loop 2 ## Test Loop 3 ## Test Loop 4 Hello World   

However, the result of the above program is,

  ## Test Loop 0 ## Test Loop 1 # # Test Loop 2 ## Test Loop 3 ## Test Loop 4 ^ @ Hello World   

So, what is "^ @"?

Show more messages,

  info: sizeof 'append.txt' is 75 bytes info: sizeof 'append.txt' is 76 bytes   

any clue?

Thanks

quick reply

Yes, the appand mode does to work with files opened in If your file system supports degradation system call, if your file system does not support it then glibc emulation adds a single 0 byte to the end in append mode.

More info

This is a strange thing and really surprised me. I got an answer using the stress program that shows how the system calls are being made.

Check it:

Decrement (3, 0, 74, 1000) = -1 EOPNOTSUPP (Operation not supported)
fstat (3, {st_mode = S_ifreg | 0664, Sat_saij = 75, ...}) = 0
Fsttfs (3, {F_taip = 0xfl5f, F_bsize = 4096, F_bloks = 56,777,565, F_bfri = 27,551,380, F_fails = 14,426,112, F_ffri = 13,172,614, F_fsid = {1863489073, -1456395543}, f_namelen = 143, f_frsize = 4096}) = 0
pwrite (3, "\ 0", 1, 1073) = 1

It seems That GNU C library is trying to help you here help . down system call on your file system is not implemented, so GLibC using pwrite to write a 0 byte at the end of the requested allocation is to copy it, Thus file extension

It works fine in normal writing mode but write Aepend mode is always the end of the file, so writes a 0 byte pwrite end.

The purpose of this could not be that there is a GNU C Library bug.

It seems that ext4 support supports fallocate . And if I write the file in / tmp if it works it'm fails because in my home directory I use encrypted home directory in Ubuntu with Ankrippt file system I

No comments:

Post a Comment