Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <assert.h>
- #include <cstdio>
- #include <unistd.h>
- #include <fcntl.h>
- int main(int argc, char** argv) {
- FILE* file = fopen("myfile.dat", "wb");
- // uint8_t* buf = (uint8_t*)malloc(1024 * 1024);
- // fwrite(buf, 1, 1024*1024, file);
- fstore_t fst = { F_ALLOCATECONTIG, F_PEOFPOSMODE, 0, 1024 * 1024 /* 1 MB */, 0 };
- if (fcntl(fileno(file), F_PREALLOCATE, &fst) == -1) {
- fst.fst_flags = F_ALLOCATEALL;
- assert(fcntl(fileno(file), F_PREALLOCATE, &fst) != -1);
- }
- // $ ls -la myfile.dat
- // -rw-r--r-- 1 me staff 128 Jan 13 13:43 myfile.dat
- // $ du -ch myfile.dat
- // 1.0M myfile.dat
- // 1.0M total
- fpunchhole_t fph = { 0, 0, 4096, 1024 * 1024 - 4096 };
- assert(-1 != fcntl(fileno(file), F_PUNCHHOLE, &fph));
- ftruncate(fileno(file), 128); // truncate file down to 128 bytes
- assert(-1 != fcntl(fileno(file), F_PUNCHHOLE, &fph));
- fclose(file);
- // $ ls -la myfile.dat
- // -rw-r--r-- 1 me staff 128 Jan 13 13:43 myfile.dat
- // $ du -ch myfile.dat
- // 1.0M myfile.dat
- // 1.0M total
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement