Creating sparse files on Windows/ Linux
Sparse file is a type of computer file, which generally has file size (logical) higher than allocated size (clusters allocated to data of the file). Most File systems support this type of file, but Operating system suppresses them underneath command interpreters’ command or API calls.
Therefore, these could not be accessed using generic copy/paste/create methods, rather they are exposed via specialized commands in command processor of operating system or API calls. In this article, we will take look at commands offered by command processor of OS’es to create sparse files.
Windows provides command line program named fsutilI (File system utility) which allows for creation/allocation/query of sparse files. To create sparse file on windows, execute the following command on command interpreter (cmd.
fsutil File CreateNew applese 1000
The above command creates new file (filled with zeros) under name of applese. But file isn’t sparse as of now. To make this file sparse file execute following commands, after previous one.
fsutil Sparse SetFlag applese fsutil Sparse SetRange applese 0 1000
The first command enables sparse flag on our newly created file. This allows for execution of second command which sets first 1000 bytes of our file (from 0 – 1000) to sparse range. Therefore, we get a completely sparse file. The confirmation of which is provided by layout command.
Stream : 0x080 ::$DATA Attributes : 0x00000200: Sparse Flags : 0x00000018: No clusters allocated | Has Parsed Information Size : 1, 000 (1.0 KB) Allocated Size : 0 (0.0 KB) Vdl : 0 (0.0 KB) Extents : 1 Extents
Clear from above section of layout command, our file has 0 allocated sizes but 1000 bytes of virtual size. This could also be seen in properties of the file.
On Linux, the process of creating sparse file is really trivial, as there exist several commands for creation of sparse files. We would be using truncate command for creation of sparse files. The command is as follows.
truncate -s 5M temp
The above command creates sparse file named temp, which is 5 megabytes (~5 million bytes) in size.
Linux OS variants provides extensive support for using sparse files, such as sparse files modification, copy, archiving, resizing, file growth etc. Most of which could be found here.
Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.