file

package
v0.4.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 15, 2024 License: Apache-2.0 Imports: 13 Imported by: 3

Documentation

Overview

Package file implements a File API over a content-addressable blob.Store.

A File as defined by this package differs from the POSIX file model in that any File may have both binary content and "children". Thus, any File is also a directory, which can contain other files in a Merkle tree structure.

A File is addressed by a storage key, corresponding to the current state of its content, metadata, and children (recursively). File metadata are stored as wire-format protocol buffers, as defined in file/wiretype/wiretype.proto.

Basic usage:

ctx := context.Background()

f := file.New(cas, nil)   // create a new, empty file
f.WriteAt(ctx, data, 0)   // write some data to the file
key, err := f.Flush(ctx)  // commit the file to storage

To open an existing file,

f, err := file.Open(ctx, cas, key)

The I/O methods of a File require a context argument. For compatibility with the standard interfaces in the io package, a File provides a wrapper for a request-scoped context:

_, err := io.Copy(dst, f.Cursor(ctx))

A value of the file.Cursor type should not be used outside the dynamic extent of the request whose context it captures.

Metadata

A File supports a subset of POSIX style data metadata, including mode, modification time, and owner/group identity. These metadata are not interpreted by the API, but will be persisted if they are set.

By default, a File does not persist stat metadata. To enable stat persistence, you may either set the Stat field of file.NewOptions when the File is created, or use the Persist method of the Stat value to enable or disable persistence:

s := f.Stat()
s.ModTime = time.Now()
s.Update().Persist(true)

The file.Stat type defines the stat attributes that can be persisted.

Synchronization

The exported methods of *File and the views of its data (Child, Data, Stat, XAttr) are safe for concurrent use by multiple goroutines.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrChildNotFound indicates that a requested child file does not exist.
	ErrChildNotFound = errors.New("child file not found")
)

Functions

func Encode

func Encode(f *File) *wiretype.Object

Encode translates f as a protobuf message for storage.

Types

type Child

type Child struct {
	// contains filtered or unexported fields
}

Child provides access to the children of a file.

func (Child) Has

func (c Child) Has(name string) bool

Has reports whether the file has a child with the given name.

func (Child) Len

func (c Child) Len() int

Len returns the number of children of the file.

func (Child) Names

func (c Child) Names() []string

Names returns a lexicographically ordered slice of the names of all the children of the file.

func (Child) Release

func (c Child) Release() int

Release discards all up-to-date cached children of the file. It returns the number of records that were released.

func (Child) Remove

func (c Child) Remove(name string) bool

Remove removes name as a child of f, and reports whether a change was made.

func (Child) Set

func (c Child) Set(name string, kid *File)

Set makes kid a child of f under the given name. Set will panic if kid == nil.

type Cursor

type Cursor struct {
	// contains filtered or unexported fields
}

A Cursor bundles a *File with a context so that the file can be used with the standard interfaces defined by the io package. A Cursor value may be used only during the lifetime of the request whose context it binds.

Each Cursor maintains a separate offset position on the underlying file, affecting Read, Write, and Seek operations on that cursor. The offset value for a newly-created cursor is always 0.

func (*Cursor) Close

func (c *Cursor) Close() error

Close implements the io.Closer interface. A File does not have a system descriptor, so "closing" performs a flush but does not invalidate the file.

func (*Cursor) Read

func (c *Cursor) Read(data []byte) (int, error)

Read reads up to len(data) bytes into data from the current offset, and reports the number of bytes successfully read, as io.Reader.

func (*Cursor) ReadAt

func (c *Cursor) ReadAt(data []byte, offset int64) (int, error)

ReadAt implements the io.ReaderAt interface.

func (*Cursor) Seek

func (c *Cursor) Seek(offset int64, whence int) (int64, error)

Seek sets the starting offset for the next Read or Write, as io.Seeker.

func (*Cursor) Stat

func (c *Cursor) Stat() (fs.FileInfo, error)

Stat implements part of the fs.File interface.

func (*Cursor) Write

func (c *Cursor) Write(data []byte) (int, error)

Write writes len(data) bytes from data to the current offset, and reports the number of bytes successfully written, as io.Writer.

func (*Cursor) WriteAt

func (c *Cursor) WriteAt(data []byte, offset int64) (int, error)

WriteAt implments the io.WriterAt interface.

type Data

type Data struct {
	// contains filtered or unexported fields
}

Data is a view of the data associated with a file.

func (Data) Keys

func (d Data) Keys() []string

Keys returns the storage keys of the data blocks for the file. If the file has no binary data, the slice is empty.

func (Data) Len

func (d Data) Len() int

Len returns the number of data blocks for the file.

func (Data) Size

func (d Data) Size() int64

Size returns the effective size of the file content in bytes.

type File

type File struct {
	// contains filtered or unexported fields
}

A File represents a writable file stored in a content-addressable blobstore.

func New

func New(s blob.CAS, opts *NewOptions) *File

New constructs a new, empty File with the given options and backed by s. The caller must call the new file's Flush method to ensure it is written to storage. If opts == nil, defaults are chosen.

func Open

func Open(ctx context.Context, s blob.CAS, key string) (*File, error)

Open opens an existing file given its storage key in s.

func (*File) Child

func (f *File) Child() Child

Child returns a view of the children of f.

func (*File) Cursor

func (f *File) Cursor(ctx context.Context) *Cursor

Cursor binds f with a context so that it can be used to satisfy the standard interfaces defined by the io package. The resulting cursor may be used only during the lifetime of the request whose context it binds.

func (*File) Data

func (f *File) Data() Data

Data returns a view of the file content for f.

func (*File) Flush

func (f *File) Flush(ctx context.Context) (string, error)

Flush flushes the current state of the file to storage if necessary, and returns the resulting storage key. This is the canonical way to obtain the storage key for a file.

func (*File) Key

func (f *File) Key() string

Key returns the storage key of f if it is known, or "" if the file has not been flushed to storage in its current form.

func (*File) Name

func (f *File) Name() string

Name reports the attributed name of f, which may be "" if f is not a child file and was not assigned a name at creation.

func (*File) New

func (f *File) New(opts *NewOptions) *File

New constructs a new empty node backed by the same store as f. If f persists stat metadata, then the new file does also.

func (*File) Open

func (f *File) Open(ctx context.Context, name string) (*File, error)

Open opens the specified child file of f, or returns ErrChildNotFound if no such child exists.

func (*File) ReadAt

func (f *File) ReadAt(ctx context.Context, data []byte, offset int64) (int, error)

ReadAt reads up to len(data) bytes into data from the given offset, and reports the number of bytes successfully read, as io.ReaderAt.

func (*File) Scan

func (f *File) Scan(ctx context.Context, visit func(ScanItem) bool) error

Scan recursively visits f and all its descendants in depth-first left-to-right order, calling visit for each file. If visit returns false, no descendants of f are visited.

func (*File) SetData

func (f *File) SetData(ctx context.Context, r io.Reader) error

SetData fully reads r replaces the binary contents of f with its data. On success, any existing data for f are discarded. In case of error, the contents of f are not changed.

func (*File) Stat

func (f *File) Stat() Stat

Stat returns the current stat metadata for f. Calling this method does not change stat persistence for f, use the Clear and Update methods of the Stat value to do that.

func (*File) Truncate

func (f *File) Truncate(ctx context.Context, offset int64) error

Truncate modifies the length of f to end at offset, extending or contracting it as necessary.

func (*File) WriteAt

func (f *File) WriteAt(ctx context.Context, data []byte, offset int64) (int, error)

WriteAt writes len(data) bytes from data at the given offset, and reports the number of bytes successfully written, as io.WriterAt.

func (*File) XAttr

func (f *File) XAttr() XAttr

XAttr returns a view of the extended attributes of f.

type FileInfo

type FileInfo struct {
	// contains filtered or unexported fields
}

FileInfo implements the fs.FileInfo interface. The underlying data source has concrete type *File.

func (FileInfo) IsDir

func (n FileInfo) IsDir() bool

func (FileInfo) ModTime

func (n FileInfo) ModTime() time.Time

func (FileInfo) Mode

func (n FileInfo) Mode() fs.FileMode

func (FileInfo) Name

func (n FileInfo) Name() string

func (FileInfo) Size

func (n FileInfo) Size() int64

func (FileInfo) Sys

func (n FileInfo) Sys() any

Sys returns the the *file.File whose stat record n carries.

type NewOptions

type NewOptions struct {
	// The name to attribute to the new file. The name of a File is not
	// persisted in storage.
	Name string

	// Initial file metadata to associate with the file. If not nil, the new
	// file will persist stat metadata to storage. However, the contents are not
	// otherwise interpreted.
	Stat *Stat

	// The block splitter configuration to use. If omitted, the default values
	// from the split package are used. Split configurations are not persisted
	// in storage, but descendants created from a file (via the New method) will
	// inherit the parent file config if they do not specify their own.
	Split *block.SplitConfig
}

NewOptions control the creation of new files.

type ScanItem

type ScanItem struct {
	*File // the current file being visited

	Parent *File  // the parent file (nil at the root)
	Name   string // the name of File within its parent ("" at the root)
}

A ScanItem is the argument to the Scan callback.

type Stat

type Stat struct {
	Mode    fs.FileMode `json:"mode,omitempty"`
	ModTime time.Time   `json:"mod_time,omitempty"`

	// Numeric ID and name of file owner.
	OwnerID   int    `json:"owner_id,omitempty"`
	OwnerName string `json:"owner_name,omitempty"`

	// Numeric ID and name of file group.
	GroupID   int    `json:"group_id,omitempty"`
	GroupName string `json:"group_name,omitempty"`
	// contains filtered or unexported fields
}

A Stat is a view into the stat metadata for a file. Modifying fields of the Stat value does not affect the underlying file unless the caller explicitly calls Update.

func (Stat) Clear

func (s Stat) Clear() Stat

Clear clears the current stat metadata for the file associated with s. Calling this method does not change whether stat is persisted, nor does it modify the current contents of s, so calling Update on the same s will restore the cleared values. Clear returns s.

func (Stat) FileInfo

func (s Stat) FileInfo() FileInfo

FileInfo returns a fs.FileInfo wrapper for s.

func (Stat) Persist

func (s Stat) Persist(ok bool) Stat

Persist enables (ok == true) or disables (ok == false) stat persistence for the file associated with s. The contents of s are not changed. It returns s.

func (Stat) Persistent

func (s Stat) Persistent() bool

Persistent reports whether the file associated with s persists stat.

func (Stat) Update

func (s Stat) Update() Stat

Update updates the stat metadata for the file associated with s to the current contents of s. Calling this method does not change whether stat is persisted. Update returns s.

type XAttr

type XAttr struct {
	// contains filtered or unexported fields
}

XAttr provides access to the extended attributes of a file.

func (XAttr) Clear

func (x XAttr) Clear()

Clear removes all the extended attributes set on the file.

func (XAttr) Get

func (x XAttr) Get(key string) string

Get returns the value corresponding to the given key, or "" if the key is not defined.

func (XAttr) Has

func (x XAttr) Has(key string) bool

Has reports whether the specified attribute is defined.

func (XAttr) Len

func (x XAttr) Len() int

Len reports the number of extended attributes defined on f.

func (XAttr) Names

func (x XAttr) Names() []string

Names returns a slice of the names of all the extended attributes defined.

func (XAttr) Remove

func (x XAttr) Remove(key string)

Remove removes the specified xattr.

func (XAttr) Set

func (x XAttr) Set(key, value string)

Set sets the specified xattr.

Directories

Path Synopsis
Package root defines a storage representation for pointers to file trees and associated metadata.
Package root defines a storage representation for pointers to file trees and associated metadata.
Package wiretype defines the encoding types for the ffs package.
Package wiretype defines the encoding types for the ffs package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL