Select a file to disk without zeroing

I need to select a huge file without nullifying its contents. I do the following steps fopen => ftruncate => fclose => mmap => (...work...) => munmap with huge file sizes (hundreds of gigabytes). The application freezes at the end for several minutes while the system tries to reset the file bytes - IMHO due to the use of ftruncate .

 ftruncate(ofd, 0); #ifdef HAVE_FALLOCATE int ret = fallocate(ofd, 0, 0, cache_size); if (ret == -1) { printf("Failed to expand file to size %llu (errno %d - %s).\n", cache_size, errno, strerror(errno)); exit(-1); } #elif defined(HAVE_POSIX_FALLOCATE) int ret = posix_fallocate(ofd, 0, cache_size); if (ret == -1) { printf("Failed to expand file to size %llu (errno %d - %s).\n", cache_size, errno, strerror(errno)); exit(-1); } #elif defined(__APPLE__) fstore_t store = {F_ALLOCATECONTIG, F_PEOFPOSMODE, 0, cache_size, 0}; int ret = fcntl(ofd, F_PREALLOCATE, &store); if (ret == -1) { store.fst_flags = F_ALLOCATEALL; ret = fcntl(ofd, F_PREALLOCATE, &store); } if (ret == -1) { // read fcntl docs - must test against -1 printf("Failed to expand file to size %llu (errno %d - %s).\n", cache_size, errno, strerror(errno)); exit(-1); } struct stat sb; ret = fstat(ofd, &sb); if (ret != 0) { printf("Failed to write to file to establish the size.\n"); exit(-1); } //ftruncate(ofd, cache_size); <-- [1] #endif 

This does not seem to work with the commented line [1] . But uncommenting this line leads to zeroing the files that I am trying to avoid. I really don't care about the dirty contents of the file before writing. I just want to avoid the hang of application termination.

DECISION:

According to @torfo , replaced all of my Apple related code with a few lines:

 unsigned long long result_size = cache_size; int ret = fcntl(ofd, F_SETSIZE, &result_size); if(ret == -1) { printf("Failed set size %llu (errno %d - %s).\n", cache_size, errno, strerror(errno)); exit(-1); } 

But it works only for the superuser!

+5
source share
1 answer

This is apparently MacOS X.

You can try replacing the ftruncate call with

 fcntl(ofd, F_SETSIZE, &size); 

(A note requires root privileges and can create a security hole, since it can provide access to previously saved file contents, so it should be handled with extreme care. β€œNot sufficiently dirty file contents”, be the passwords of the user account that he deleted a week ago. ..)

MacOS X does not actually support sparse files - it creates and supports them, but its file system driver really wants to fill the holes as soon as possible.

+5
source

Source: https://habr.com/ru/post/1268460/


All Articles