System call lseek() sets position in file, associated with file descriptor. But it has some funny using. lseek() can be used for 'fast forwarding' file beyond its end. If than we write in current position, space between file end and position fills by zeros.
So we can create files with (almost)any size. For example, 16 terabytes(limit for ext4 filesystem):
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
int fd = open(argv[1], O_WRONLY | O_CREAT | O_LARGEFILE, 0644);
int ret;
off64_t ret64;
off64_t kilo = 1024;
off64_t megabyte = kilo * kilo;
off64_t terabyte = megabyte * megabyte;
off64_t offset = 16 * terabyte - megabyte;
if (fd < 1) {
perror("open");
return -1;
}
ret64 = lseek64(fd, offset, SEEK_END);
if (ret64 < 1) {
perror("lseek");
return -1;
}
ret = write(fd, "0", 1);
if (ret < 1) {
perror("write");
return -1;
}
write(STDOUT_FILENO, "Success!\n", 10);
return 0;
}
For compiling, set flag -D_GNU_SOURCE to compiler. Users of 32-bit systems can rewrite code, but them'll be limited by 2 gigabytes - size of off_t. But probably them can use fsetpos().
No comments:
Post a Comment