int main(int argc, char **argv) {
if (argc < 2) return -1;
int fd = open64(argv[1], O_LARGEFILE, S_IRUSR | S_IWUSR);
struct stat64 statbuf;
fstat64(fd, &statbuf);
printf("file size: %d bytes\n", statbuf.st_size);
off64_t size = statbuf.st_size + 1024;
printf("file size needs: %d bytes\n", size);
if (ftruncate64(fd, size) != 0) {
perror("Cannot ftruncate64()");
printf("Error code: %d\n", errno);
}
close(fd);
return 0;
}
$ ./a.out file.txt
file size: 1 bytes
file size needs: 1025 bytes
Cannot ftruncate64(): Invalid argument
Error code: 22
Почему выдается код 22?
Вот здесь - http://lists.apple.com/archives/darwin-development/2003/Oct/msg00186.html нашел что-то по теме: «After shm_open and before mmap you should call ftruncate(fd, sz). Don't ask me why, I don't know. I assume that the file is created with no size, and is given a size by the ftruncate code while mmap, well, just map the requested size to the fd.. Also, I noticed that if you call ftruncate() on a shm which has already be opened by another process and that you shm_open with the O_CREAT flag set, ftruncate returns an errno = EINVAL (22). Here again, I don't know why. But it seems that it doesn't prevent the shared memory segment to be operational for both processes. I've a working example where I use neither MAP_FILE nor MAP_ANON, only MAP_SHARED.»
Программа проверяется на файловой системе ext4:
т.к. http://publib.boulder.ibm.com/infocenter/iseries/v5r3/index.jsp?topic=/apis/f... : «For file systems that do not support large files, this function will fail with the [EINVAL] error if the length specified is greater than 2GB minus 1 byte.»