What is a sparse file and why do we need it? The only thing that I am able to get is that it is a very large file and it is efficient(in gigabytes). How is it efficient ?
相关问题
- What is the best way to do a search in a large fil
- Spring Integration - Inbound file endpoint. How to
- php--glob for searching directories and .jpg only
- Editing how file name is displayed in JComboBox wh
- how to create files under /WEB-INF/
相关文章
- What is the correct way to declare and use a FILE
- Making new files automatically executable?
- What file sytems support Java UserDefinedFileAttri
- how to handle os.system sigkill signal inside pyth
- Transactionally writing files in Node.js
- Get file created date in node
- Generate disk usage graphs/charts with CLI only to
- How to serialize data into indented json [duplicat
A sparse file is a file that is mostly empty, i.e. it contains large blocks of bytes whose value is
0
(zero).On the disk, the content of a file is stored in blocks of fixed size (usually 4 KiB or more). When all the bytes contained in such a block are
0
, a file system that implements sparse files does not store the block on disk, instead it keeps the information somewhere in the file meta-data.Advantages of using sparse files:
0
, it just sets to0
all the bytes in the input buffer and the data is ready; there is no need to access the slow storage device;0
) and puts the block ID into the list of empty blocks (in the file meta-data); no data is written to the disk.More information about sparse files can be found on the Wikipedia page.
Say you have a file with many empty bytes
\x00
. These many empty bytes\x00
are called holes. Storing empty bytes is just not efficient, we know there are many of them in the file, so why store them on the storage device? We could instead store metadata describing those zeros. When a process reads the file those zero byte blocks get generated dynamically as opposed to being stored on physical storage (look at this schematic from Wikipedia):This is why a sparse file is efficient, because it does not store the zeros on disk, instead it holds enough data describing the zeros that will be generated.
Note: the logical file size is greater than the physical file size for sparse files. This is because we have not stored the zeros physically on a storage device.
Edit:
When you run:
The command here copies 4G blocks of null bytes to
output
. To see that:You can see that this file has 8388616 blocks allocated to it, these blocks store nothing but empty bytes copied from
/dev/zero
and they do occupy physical disk space, they're holes stored on disk (sparse zeros).dd
did what you asked for, copying blocks of data from one file to another.Now, run this command to detect the holes and make the file sparse in-place:
Do you notice something? The the number of blocks now is 0 because the blocks that were storing only empty bytes were de-allocated. Remember,
output
's blocks store nothing, only a bunch of empty zeros,fallocate -d
detected the blocks that contain only empty zeros and deallocated them, since all the blocks for this file contain zeros, they were all de-allocated.Also notice how the size remained the same. This is the logical (virtual) size of the file, not its size on disk. It's crucial to know that
output
doesn't occupy physical storage space now, it has 0 blocks allocated to it and thus I doesn't really use disk space. The size preserved after runningfallocate -d
so when you later read from the file, you get the empty bytes generated to you by the filesystem at runtime. The physical size ofoutput
however, is zero, it uses no data blocks.Remember, when you read
output
file the empty bytes are generated by the filesystem at runtime dynamically, they're not really physically stored on disk, and the file's size as reported bystat
is the logical size, and the physical size is zero foroutput
. In this case the filesystem has to generate 4G of empty bytes when a process reads the file.To generate a sparse file using
dd
:GNU
dd
internally useslseek
andftruncate
, so check truncate(2) and lseek(2).