Documentation
¶
Overview ¶
Package contentaddressable contains tools for reading and writing content addressable files. Files are written to a temporary location, and only renamed to the final location after the file's OID (Object ID) has been verified.
filename := "path/to/01ba4719c80b6fe911b091a7c05124b64eeece964e09c058ef8f9805daca546b"
file, err := contentaddressable.NewFile(filename)
if err != nil {
panic(err)
}
defer file.Close()
file.Oid // 01ba4719c80b6fe911b091a7c05124b64eeece964e09c058ef8f9805daca546b
written, err := io.Copy(file, someReader)
if err == nil {
// Move file to final location if OID is verified.
err = file.Accept()
}
if err != nil {
panic(err)
}
Currently SHA-256 is used for a file's OID.
You can also read files, while verifying that they are not corrupt.
filename := "path/to/01ba4719c80b6fe911b091a7c05124b64eeece964e09c058ef8f9805daca546b"
// get this from doing an os.Stat() or something
expectedSize := 123
// returns an io.ReadCloser
reader, err := contentaddressable.Open(filename, expectedSize)
if err != nil {
panic(err)
}
defer file.Close()
// A contentaddressable.ReadCloser ensures that exactly the expectedSize
// number of bytes were read, and that the content matches the OID in the
// filename.
written, err := io.Copy(ioutil.Discard, reader)
if err != nil {
panic(err)
}
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( AlreadyClosed = errors.New("Already closed.") HasData = errors.New("Destination file already has data.") DefaultSuffix = "-temp" )
Functions ¶
func Reader ¶
func Reader(reader io.ReadCloser, oid string) io.ReadCloser
Types ¶
type File ¶
type File struct {
Oid string
// contains filtered or unexported fields
}
File handles the atomic writing of a content addressable file. It writes to a temp file, and then renames to the final location after Accept().
func NewFile ¶
NewFile initializes a content addressable file for writing. It is identical to NewWithSuffix, except it uses DefaultSuffix as the suffix.
func NewWithSuffix ¶
NewWithSuffix initializes a content addressable file for writing. It opens both the given filename, and a temp filename in exclusive mode. The *File OID is taken from the base name of the given filename.
func (*File) Accept ¶
Accept verifies the written content SHA-256 signature matches the given OID. If it matches, the temp file is renamed to the original filename. If not, an error is returned.