1 // Copyright 2018 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // Package txtar implements a trivial text-based file archive format.
7 // The goals for the format are:
9 // - be trivial enough to create and edit by hand.
10 // - be able to store trees of text files describing go command test cases.
11 // - diff nicely in git history and code reviews.
13 // Non-goals include being a completely general archive format,
14 // storing binary data, storing file modes, storing special files like
15 // symbolic links, and so on.
19 // A txtar archive is zero or more comment lines and then a sequence of file entries.
20 // Each file entry begins with a file marker line of the form "-- FILENAME --"
21 // and is followed by zero or more file content lines making up the file data.
22 // The comment or file content ends at the next file marker line.
23 // The file marker line must begin with the three-byte sequence "-- "
24 // and end with the three-byte sequence " --", but the enclosed
25 // file name can be surrounding by additional white space,
26 // all of which is stripped.
28 // If the txtar file is missing a trailing newline on the final line,
29 // parsers should consider a final newline to be present anyway.
31 // There are no possible syntax errors in a txtar archive.
41 // An Archive is a collection of files.
47 // A File is a single file in an archive.
49 Name string // name of file ("foo/bar.txt")
50 Data []byte // text content of file
53 // Format returns the serialized form of an Archive.
54 // It is assumed that the Archive data structure is well-formed:
55 // a.Comment and all a.File[i].Data contain no file marker lines,
56 // and all a.File[i].Name is non-empty.
57 func Format(a *Archive) []byte {
59 buf.Write(fixNL(a.Comment))
60 for _, f := range a.Files {
61 fmt.Fprintf(&buf, "-- %s --\n", f.Name)
62 buf.Write(fixNL(f.Data))
67 // ParseFile parses the named file as an archive.
68 func ParseFile(file string) (*Archive, error) {
69 data, err := ioutil.ReadFile(file)
73 return Parse(data), nil
76 // Parse parses the serialized form of an Archive.
77 // The returned Archive holds slices of data.
78 func Parse(data []byte) *Archive {
81 a.Comment, name, data = findFileMarker(data)
84 f.Data, name, data = findFileMarker(data)
85 a.Files = append(a.Files, f)
91 newlineMarker = []byte("\n-- ")
92 marker = []byte("-- ")
93 markerEnd = []byte(" --")
96 // findFileMarker finds the next file marker in data,
97 // extracts the file name, and returns the data before the marker,
98 // the file name, and the data after the marker.
99 // If there is no next marker, findFileMarker returns before = fixNL(data), name = "", after = nil.
100 func findFileMarker(data []byte) (before []byte, name string, after []byte) {
103 if name, after = isMarker(data[i:]); name != "" {
104 return data[:i], name, after
106 j := bytes.Index(data[i:], newlineMarker)
108 return fixNL(data), "", nil
110 i += j + 1 // positioned at start of new possible marker
114 // isMarker checks whether data begins with a file marker line.
115 // If so, it returns the name from the line and the data after the line.
116 // Otherwise it returns name == "" with an unspecified after.
117 func isMarker(data []byte) (name string, after []byte) {
118 if !bytes.HasPrefix(data, marker) {
121 if i := bytes.IndexByte(data, '\n'); i >= 0 {
122 data, after = data[:i], data[i+1:]
124 if !bytes.HasSuffix(data, markerEnd) {
127 return strings.TrimSpace(string(data[len(marker) : len(data)-len(markerEnd)])), after
130 // If data is empty or ends in \n, fixNL returns data.
131 // Otherwise fixNL returns a new slice consisting of data with a final \n added.
132 func fixNL(data []byte) []byte {
133 if len(data) == 0 || data[len(data)-1] == '\n' {
136 d := make([]byte, len(data)+1)