For more info visitIf you would ask me this question, I would say, this hole is the only one of its kind which is not visible to a human, unless they really know file handling. Otherwise, a hole in anything is always visible, doesn't matter how much small it is, it always has a physical existence.
Hole in the file doesn't mean that your file is perforated , i.e you opened a file, read some data, and then damn, your file is perforated so you couldn't read some data, and then again you started reading after the hole. There is nothing like loss of data due to a hole in the file. There is also no need for the hole to occupy any disk block. Although this statement is not totally true. In some cases holes may occupy some disk space. At the end of this post, I will let you know the scenario in which holes may occupy some disk space.
Now how can a file with a hole is created.
Given below is a simple program in c which will generate a file with a hole.
#include<sys/stat.h>
#include<stdio.h>
#include<fcntl.h>
int main()
{
char databfhole[]="abcdefghij";
char dataafhole[]="klmnopqrst";
int fd;
fd=creat("filewithhole",S_IRUSR|S_IWUSR);
write(fd,databfhole,10);
lseek(fd,20000,SEEK_CUR);
write(fd,dataafhole,10);
close(fd);
return 0;
}
In the above code we are creating a file "filewithhole". S_IRUSR and S_IWUSR give read and write permission to the owner of file. After the file is created, file pointer will point at the beginning of the file. WRITE function writes a string of characters stored in the array
databfhole. lseek is used to seek 20000 position past the current position of the file pointer. After that we again write some data, in array
dataafhole, to the file
Note: lseek doesn't write anything in the file, it just moves the position of current offset in the file table.
Compile and run the program
filewithhole is created. Now run the following command to see the contents of file
cat filewithhole
And the output will be
abcdefghijklmnopqrst
But, when we check the size of the file it is 20020 bytes. So, what are these other bytes, if not visible characters?
Let's check it with the use of od command. Run
od -c filewithhole
0000000 a b c d e f g h i j \0 \0 \0 \0 \0 \0
0000020 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0
*
0047040 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 k l m n o p
0047060 q r s t
We can see that all other hidden characters are null characters or 0. Depending on the file system and data block size, these holes may or may not occupy any area in disk. How?
Suppose my data block size is 2K, i.e. 2048 bytes. I have a file with hole which has a size of 8000 bytes. Now, let's say my first 1000 bytes are full of data, then there are holes after that, and then again I have last 1000 bytes of data. In this scenario my first data block will have 1000 bytes of data and 1048 bytes with value 0. So, these 1048 bytes hole would occupy some space in the disk. After this 2 data blocks of holes will be discarded i.e. they will not occupy any space on the disk, and then again starting from fouth data block i.e 6144 byte, holes will occupy some disk space till 6999. The last 1000 bytes will then be occupied by the data. So, we can see that only two data blocks were used to store this file.
No comments:
Post a Comment