How to create a small file (size <= 2GB) file using application compiled with _FILE_OFFSET_BITS 64?

103 Views Asked by At

When an application is compiled with _FILE_OFFSET_BITS 64 feature test macro, then all old 32-bit interface system calls (like creat/open/lseek, etc) will be replaced with new 64-bit interface system calls (like creat64/open64/lseek64,etc). So, the creat() call in my application creates a large file (size > 2GB). But, is it possible to create the small file (size <= 2GB) using same application compiled with _FILE_OFFSET_BITS 64?.

2

There are 2 best solutions below

3
On

You could write a creat wrapper function which is specifically compiled with -D_FILE_OFFSET_BITS=32. This would give you a file descriptor which is limited to 32-bit file sizes. However, this only works with certain 32-bit environments.

There is also RLIMIT_FSIZE, which can be set using setrlimit, but I have no idea how reliable it is.

3
On

So, the creat() call in my application creates a large file (size > 2GB).

No, it doesn't. creat() always creates an empty file (or truncates an existing file).