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) {
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!