测试代码:
1 #define _LARGEFILE_SOURCE 2 #define _LARGEFILE64_SOURCE 3 #define _FILE_OFFSET_BITS 64 4 5 #include <sys/types.h> 6 #include <sys/stst.h> 7 #include <fcntl.h> 8 #include <unistd.h> 9 10 int main(){ 11 int fd; 12 __off64_t offset; 13 fd = open("test", O_RDWR | O_CREAT | O_LARDEFILE,0666); 14 offset = 25*65536*65536; //100GB 15 int err = ftruncate64(fd,offset); 16 if(err < 0){ 17 printf("ftruncate err "); 18 19 } 20 21 struct stat64 filestat; 22 fstat64(fd, &filestat); 23 printf("file size: %ld ",filestat.st_size); 24 //打印__off64_t 大小:
printf("__off64_t is %d bytes ",sizeof(__off64_t)); 25 26 return 0; 27 28 }
使用gcc - D_FILE_OFFSET_BITS=64 -D_LARGE_FILE test.c; 编译警告:iteger overflow "offset = 25*65536*65536;"。运行sudo ./a.out, ftruncate64()返回值为0, __0ff64_t 大小为8字节,但是test大小只有1GB.
有没有大牛知道原因?
PS: 在终端下,执行truncate -s 100GB test 或者 代码中执行 system(" truncate -s 100GB test ")后,都能将test大小设置为100GB.