Involved Source Files Package txtar implements a trivial text-based file archive format.
The goals for the format are:
- be trivial enough to create and edit by hand.
- be able to store trees of text files describing go command test cases.
- diff nicely in git history and code reviews.
Non-goals include being a completely general archive format,
storing binary data, storing file modes, storing special files like
symbolic links, and so on.
# Txtar format
A txtar archive is zero or more comment lines and then a sequence of file entries.
Each file entry begins with a file marker line of the form "-- FILENAME --"
and is followed by zero or more file content lines making up the file data.
The comment or file content ends at the next file marker line.
The file marker line must begin with the three-byte sequence "-- "
and end with the three-byte sequence " --", but the enclosed
file name can be surrounding by additional white space,
all of which is stripped.
If the txtar file is missing a trailing newline on the final line,
parsers should consider a final newline to be present anyway.
There are no possible syntax errors in a txtar archive.fs.go
Package-Level Type Names (total 7, in which 2 are exported)
A filesystem is a simple in-memory file system for txtar archives,
represented as a map from valid path names to information about the
files or directories they represent.
File system operations are read only. Modifications to the underlying
*Archive may race. To help prevent this, the filesystem tries
to detect modification during Open and return ErrModified if it
is able to detect a modification.ar*Archivenodesmap[string]*node(*filesystem) Open(name string) (fs.File, error)(*filesystem) ReadFile(name string) ([]byte, error)
*filesystem : io/fs.FS
*filesystem : io/fs.ReadFileFS
func dataOf(fsys *filesystem, n *node) ([]byte, error)
func directory(fsys *filesystem, dir string) (*node, error)
func initFiles(fsys *filesystem) error
func insert(fsys *filesystem, n *node) error
Package-Level Functions (total 11, in which 4 are exported)
Format returns the serialized form of an Archive.
It is assumed that the Archive data structure is well-formed:
a.Comment and all a.File[i].Data contain no file marker lines,
and all a.File[i].Name is non-empty.
FS returns the file system form of an Archive.
It returns an error if any of the file names in the archive
are not valid file system names.
The archive must not be modified while the FS is in use.
If the file system detects that it has been modified, calls to the
file system return an ErrModified error.
Parse parses the serialized form of an Archive.
The returned Archive holds slices of data.
ParseFile parses the named file as an archive.
dataOf returns the data associated with the file t.
May return ErrModified if fsys.ar has been modified.
directory returns the directory node with the path dir and lazily-creates it
if it does not exist.
findFileMarker finds the next file marker in data,
extracts the file name, and returns the data before the marker,
the file name, and the data after the marker.
If there is no next marker, findFileMarker returns before = fixNL(data), name = "", after = nil.
If data is empty or ends in \n, fixNL returns data.
Otherwise fixNL returns a new slice consisting of data with a final \n added.
initFiles initializes fsys from fsys.ar.Files. Returns an error if there are any
invalid file names or collisions between file or directories.
insert adds node n as an entry to its parent directory within the filesystem.
isMarker checks whether data begins with a file marker line.
If so, it returns the name from the line and the data after the line.
Otherwise it returns name == "" with an unspecified after.
Package-Level Variables (total 4, in which 1 is exported)
ErrModified indicates that file system returned by FS
noticed that the underlying archive has been modified
since the call to FS. Detection of modification is best effort,
to help diagnose misuse of the API, and is not guaranteed.