Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Sparse file

From Wikipedia, the free encyclopedia
Type of computer file
A sparse file: The empty bytes do not need to be saved and can be represented instead bymetadata.

Incomputer science, asparse file is a type ofcomputer file that attempts to usefile system space more efficiently when the file itself is partially empty. This is achieved by writing brief information (metadata)representing the empty blocks to the data storage media instead of the actual "empty" space which makes up the block, thus consuming less storage space. The full block is written to the media as the actual size only when the block contains "real" (non-empty) data.

Most commonly, sparse files are created when blocks of the file are never written to. This is typical for random-access files like databases. Some operating systems or utilities go further by "sparsifying" files when writing or copying them: if a block contains only null bytes, it is not written to storage but rather marked as empty.

When reading sparse files, the file system transparently converts metadata representing empty blocks into "real" blocks filled with null bytes at runtime. The application is unaware of this conversion.

Most modern file systems support sparse files, including mostUnix variants andNTFS.[1] Apple'sHFS+ does not provide support for sparse files, but in OS X, thevirtual file system layer supports storing them in any supported file system, including HFS+.[citation needed]Apple File System (APFS) also supports them.[2] Sparse files are commonly used fordisk images,database snapshots,log files and in scientific applications.

Advantages

[edit]

The advantage of sparse files is that storage space is only allocated when actually needed: Storage capacity is conserved, and large files can occasionally be created even if insufficient free space for the original file is available on the storage media. This also reduces the time of the first write as the system does not have to allocate blocks for the "skipped" space. If the initial allocation requires writing all zeros to the space, it also keeps the system from having to write over the "skipped" space twice.

For example, avirtual machine image with max size of 100 GB that has 2 GB of files actually written would require the full 100 GB when backed by pre-allocated storage, yet only 2 GB on a sparse file. If the file system supports hole punching and the guest operating system issuesTRIM commands, deleting files on the guest will accordingly reduce the space needed.

Disadvantages

[edit]

Disadvantages are that sparse files may becomefragmented; file system free space reports may be misleading; filling up file systems containing sparse files can have unexpected effects (such as disk-full or quota-exceeded errors when merely overwriting an existing portion of a file that happened to have been sparse); and copying a sparse file with aprogram that does not explicitly support them may copy the entire, uncompressed size of the file, including the zero sections which are not allocated on the storage media—losing the benefits of the sparse property in the file. Sparse files are also not fully supported by all backup software or applications. However, theVFS implementation sidesteps[citation needed] the prior two disadvantages. Loadingexecutables on 32 bit Windows (exe or dll) which are sparse takes a much longer time since the file cannot be memory mapped in the limited 4 GB address space, and are not cached as there is no codepath for caching 32 bit sparse executables (Windows on 64 bit architectures can map sparse executables).[citation needed] On NTFS sparse files (or rather their non-zero areas) cannot be compressed. NTFS implements sparseness as a special kind of compression so a file may be either sparse or compressed.

Sparse files in Unix

[edit]

Sparse files are typically handled transparently to the user. But the differences between a normal file and sparse file become apparent in some situations.

Creation

[edit]

TheUnix command

ddof=sparse-filebs=5Mseek=1count=0

will create a file of fivemebibytes in size, but with no data stored on the media (onlymetadata). (GNUdd has this behavior because it callsftruncate to set the file size; other implementations may merely create an empty file.)

Similarly the truncate command may be used, if available:

truncate-s5M<filename>

OnLinux, an existing file can be converted to sparse by:

fallocate-d<filename>

There is no portable system call to punch holes; Linux providesfallocate(FALLOC_FL_PUNCH_HOLE), andSolaris providesfcntl(F_FREESP).

Detection

[edit]

The-s option of thels command shows the occupied space in blocks.

ls-lssparse-file

Alternatively, thedu command prints the occupied space, whilels prints the apparent size.In some non-standard versions ofdu, the option--block-size=1 prints the occupied space in bytes instead of blocks, so that it can be compared to thels output:

du--block-size=1sparse-filels-lsparse-file

Note the above du usage has the abbreviated option syntax format "du -B 1 sf", itself equivalent to the shortest version "du -b sf" as stated in the du manual:[3]-b, --bytes is equivalent to--apparent-size --block-size=1.

Also, the toolfilefrag frome2fsprogs package can be used to show block allocation details of the file.

filefrag-vsparse-file

Copying

[edit]

Normally, the GNU version ofcp is good at detecting whether a file is sparse, so

cp sparse-file new-file

creates new-file, which will be sparse. GNU also has a--sparse option,[4] which is especially useful if a file containing long zero blocks is saved in a non-sparse way (i.e. the zero blocks have been written to the storage media in full). Storage space can be conserved by doing:

cp --sparse=always file1 file1_sparsed

Some cp implementations, likeFreeBSD's cp, do not support the--sparse option and will always expand sparse files. A partially viable alternative on those systems is to usersync with its own--sparse option[5] instead of cp. Unfortunately, older versions of rsync do not support--sparse combined with--inplace.[6][7][8]

Viastandard input, sparse file copying is achieved as follows:

cp--sparse=always/dev/fd/0new-sparse-file<somefile

See also

[edit]

References

[edit]
  1. ^Giampaolo, Dominic (1999).Practical File System Design with the Be File System(PDF).Morgan Kaufmann Publishers.ISBN 9781558604971.
  2. ^"Apple File System Guide".Apple's Developer Site. Apple Inc. Retrieved27 April 2017.
  3. ^"Du(1) – Linux manual page".
  4. ^Meyering, Jim (1995-12-21)."GNU coreutils/cp: Accept new option, --sparse={never,auto,always}, to control creation of sparse files". Retrieved2016-06-17.
  5. ^Tridgell, Andrew (1996-06-29)."rsync: hard links, better sparse handling, FERROR and FINFO". Retrieved2016-06-17.
  6. ^Tridgell, Andrew (2016-06-30)."rsync manpage". Retrieved2017-01-19.
  7. ^Davison, Wayne (2005-08-30)."rsync: Reject attempts to combine --sparse with --inplace". Retrieved2017-01-19.
  8. ^Davison, Wayne."Support --sparse combined with --preallocate or --inplace".GitHub.

External links

[edit]
Types
Properties
Organisation
Operations
Linking
Management
Retrieved from "https://en.wikipedia.org/w/index.php?title=Sparse_file&oldid=1323401048"
Category:
Hidden categories:

[8]ページ先頭

©2009-2026 Movatter.jp