(参考Advanced Programming in the Unix environment)
通过给文件制造Hole可以达到效果。
也就是说,通过lseek把文件的offset整到很大,写点东西。这个时候内存实际上没有占用,但是显示文件很大。
能干什么?吓人~
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(void)
{
int fd = open("/home/xili2/test/tmp.dat", O_WRONLY | O_CREAT | O_TRUNC);
if(write(fd,"abcdefg/n",8)!=8){
printf("write first buff error");
}
if(lseek(fd,20000,SEEK_SET) == -1){
printf("lseek error");
}
if(write(fd, "ABCDEFG/n",8)!=8){
printf("write to second buff error");
}
return 0;
}
------------console----------
-bash-3.2$ ls -lart tmp.dat
-rwxrwxrwx 1 xili2 root 20008 Nov 13 18:17 tmp.dat
-bash-3.2$ cat tmp.dat
abcdefg
ABCDEFG
-bash-3.2$
-bash-3.2$ od -c tmp.dat
0000000 a b c d e f g /n /0 /0 /0 /0 /0 /0 /0 /0
0000020 /0 /0 /0 /0 /0 /0 /0 /0 /0 /0 /0 /0 /0 /0 /0 /0
*
0047040 A B C D E F G /n
0047050
-bash-3.2$