Documentation
¶
Overview ¶
Package unsparse provides tooling to Unsparse a sparse file.
Some tools are unable to correctly represent sparse files, as such you can use this package to reformat the sparse file to be represented in the Unsparsed binary format.
To move an Unsparsed file to a sparse file, you can use the Sparse function.
Index ¶
Constants ¶
const Version uint16 = 1
Version is the current version of the Unsparsed format.
Variables ¶
var ErrNotUnsparsed = errors.New("unsparse: not an unsparsed file (bad magic)")
ErrNotUnsparsed is returned, wrapped, when data does not start with Magic.
var Magic = [8]byte{0x89, 'U', 'N', 'S', 'P', 'R', 'S', '\n'}
Magic identifies a file as being in the Unsparsed format. Like PNG's, it starts with a non-ASCII byte so text tools don't mistake it for text, and ends in a newline so line-ending translation is detectable.
For file(1), the equivalent magic(5) entry is:
0 string \x89UNSPRS\n Unsparsed sparse file data >8 beshort x \b, version %d >12 bequad x \b, apparent size %lld bytes
Functions ¶
func Sparse ¶
func Sparse(dst TruncateWriter, src io.Reader) error
Sparse is the opposite of Unsparse: it reads the Unsparsed format from src and writes the original file to dst.
dst is truncated before writing, and regions between extents are left as holes. Sparse may read past the end of the Unsparsed data in src.
Example:
src, err := os.Open("myunsparsedfile")
if err != nil {
panic(err)
}
defer src.Close()
dst, err := os.Create("mysparsefile")
if err != nil {
panic(err)
}
defer dst.Close()
if err := unsparse.Sparse(dst, src); err != nil {
panic(err)
}
// dst is now the original sparse file, holes included
func Unsparse ¶
func Unsparse(dst io.Writer, src SparseReader) error
Unsparse reads the sparse file src and writes it to dst in the Unsparsed format, leaving out its holes. See SparseReader for which sources have their holes detected.
Some tools are unable to correctly understand sparse files. Such as Golang's [tar.Writer]
Example:
f, err := os.Open("mysparsefile")
if err != nil {
panic(err)
}
sr, err := unsparse.SparseReaderFile(f)
if err != nil {
panic(err)
}
buf := bytes.NewBuffer(nil)
if err := unsparse.Unsparse(buf, sr); err != nil {
panic(err)
}
// buf now holds the [Unsparsed] format
Types ¶
type Buffer ¶
type Buffer struct {
// contains filtered or unexported fields
}
Buffer is an in-memory sparse file. Only written regions use memory; everything else reads back as zeroes.
It implements SparseReader, TruncateWriter and DataFinder, so it can be used as either side of Unsparse and Sparse.
The zero value is an empty buffer ready to use. A Buffer is not safe for concurrent use.
Example:
// A 1 TiB buffer with two small regions of data uses only a few bytes.
b := unsparse.NewBuffer(1 << 40)
b.WriteAt([]byte("hello"), 0)
b.WriteAt([]byte("world"), 1<<39)
var packed bytes.Buffer
if err := unsparse.Unsparse(&packed, b); err != nil {
panic(err)
}
// packed holds the [Unsparsed] format: a header and two extents
restored := unsparse.NewBuffer(0)
if err := unsparse.Sparse(restored, &packed); err != nil {
panic(err)
}
// restored.Size() == 1<<40, with the same two regions of data
func NewBuffer ¶
NewBuffer returns a Buffer of the given size that is entirely a hole. No memory is allocated for the hole, however large size is.
It panics if size is negative.
Example:
b := unsparse.NewBuffer(4096)
b.WriteAt([]byte("data"), 1024)
start, end, _ := b.NextData(0)
// start == 1024, end == 1028
func (*Buffer) NextData ¶
NextData implements DataFinder.
func (*Buffer) ReadAt ¶
ReadAt implements io.ReaderAt.
type DataFinder ¶
type DataFinder interface {
// NextData returns the next data region [start, end) at or after off,
// or io.EOF if only a hole remains.
NextData(off int64) (start, end int64, err error)
}
DataFinder is implemented by sources that know where their data is, such as *Buffer. Unsparse uses it to skip holes.
type Extent ¶ added in v0.1.3
type Extent struct {
// Offset is where the data starts in the original file.
Offset int64
// Size is the number of data bytes that follow.
Size int64
}
Extent is the header for one extent of data in the Unsparsed format. It is followed by Size bytes of data.
type SparseReader ¶
type SparseReader interface {
io.ReaderAt
// Size returns the apparent size, including holes.
Size() int64
}
SparseReader is a source for Unsparse, such as *Buffer, *io.SectionReader or *bytes.Reader. Holes are detected when it implements DataFinder, or is an *io.SectionReader over one or over an *os.File; any other source is treated as all data. Use SparseReaderFile to get one for an *os.File.
func SparseReaderFile ¶
func SparseReaderFile(f *os.File) (SparseReader, error)
SparseReaderFile is a convenience function to return a SparseReader from an *os.File.
type TruncateWriter ¶
type TruncateWriter interface {
// Truncate changes the size of the destination. Growing it must leave
// the new region reading as zeroes, ideally as a hole.
Truncate(size int64) error
io.WriterAt
}
TruncateWriter is a destination for Sparse, such as *os.File or *Buffer.
type Unsparsed ¶
type Unsparsed struct {
// Magic is always [Magic].
Magic [8]byte
// Version is the format version, currently [Version].
Version uint16
// Flags is reserved; it is zero in version 1 and readers must reject
// flags they don't understand.
Flags uint16
// ApparentSize is the size of the original file, including holes.
ApparentSize int64
}
Unsparsed is the header of the binary format written by Unsparse and read by Sparse.
Since a sparse file is represented with data and holes, the format simply removes the holes. It is an Unsparsed header followed by zero or more extents, each an Extent header followed by its data:
[8]byte // Unsparsed.Magic [2]byte // Unsparsed.Version [2]byte // Unsparsed.Flags [8]byte // Unsparsed.ApparentSize [8]byte // Extant.Offset [8]byte // Extant.Size [Size]byte // extent data ... // repeated for each extent, until EOF
All integers are big-endian. Extents are in increasing offset order and never overlap; anything not covered by an extent is a hole.
func ReadHeader ¶
ReadHeader reads and validates an Unsparsed header from r, leaving r positioned at the first extent. It returns an error wrapping ErrNotUnsparsed if r doesn't start with Magic, so it can be used to detect the format.