File: parse.go

package info (click to toggle)
golang-github-vbatts-go-mtree 0.5.4%2Bds-1~exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 796 kB
  • sloc: sh: 198; makefile: 80
file content (112 lines) | stat: -rw-r--r-- 2,589 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package mtree

import (
	"bufio"
	"io"
	"path/filepath"
	"strings"
)

// ParseSpec reads a stream of an mtree specification, and returns the DirectoryHierarchy
func ParseSpec(r io.Reader) (*DirectoryHierarchy, error) {
	s := bufio.NewScanner(r)
	i := int(0)
	creator := dhCreator{
		DH: &DirectoryHierarchy{},
	}
	for s.Scan() {
		str := s.Text()
		trimmedStr := strings.TrimLeftFunc(str, func(c rune) bool {
			return c == ' ' || c == '\t'
		})
		e := Entry{Pos: i}
		switch {
		case strings.HasPrefix(trimmedStr, "#"):
			e.Raw = str
			if strings.HasPrefix(trimmedStr, "#mtree") {
				e.Type = SignatureType
			} else {
				e.Type = CommentType
				// from here, the comment could be "# key: value" metadata
				// or a relative path hint
			}
		case str == "":
			e.Type = BlankType
			// nothing else to do here
		case strings.HasPrefix(str, "/"):
			e.Type = SpecialType
			// collapse any escaped newlines
			for {
				if strings.HasSuffix(str, `\`) {
					str = str[:len(str)-1]
					s.Scan()
					str += s.Text()
				} else {
					break
				}
			}
			// parse the options
			f := strings.Fields(str)
			e.Name = f[0]
			e.Keywords = StringToKeyVals(f[1:])
			if e.Name == "/set" {
				creator.curSet = &e
			} else if e.Name == "/unset" {
				creator.curSet = nil
			}
		case len(strings.Fields(str)) > 0 && strings.Fields(str)[0] == "..":
			e.Type = DotDotType
			e.Raw = str
			if creator.curDir != nil {
				creator.curDir = creator.curDir.Parent
			}
			// nothing else to do here
		case len(strings.Fields(str)) > 0:
			// collapse any escaped newlines
			for {
				if strings.HasSuffix(str, `\`) {
					str = str[:len(str)-1]
					s.Scan()
					str += s.Text()
				} else {
					break
				}
			}

			// parse the options
			f := strings.Fields(str)
			e.Name = f[0]
			e.Keywords = StringToKeyVals(f[1:])
			// TODO: gather keywords if using tar stream
			var isDir bool
			for _, kv := range e.Keywords {
				if kv.Keyword() == "type" {
					isDir = kv.Value() == "dir"
				}
			}
			if strings.Contains(e.Name, "/") {
				e.Type = FullType
			} else {
				e.Type = RelativeType
				e.Parent = creator.curDir
				if isDir {
					creator.curDir = &e
				}
			}
			if !isDir {
				creator.curEnt = &e
			}
			e.Set = creator.curSet
			// we need to clean the filepath at the end because '/'s can be
			// stripped, which would cause FullTypes to be treated as
			// RelativeTypes above
			e.Name = filepath.Clean(e.Name)
		default:
			// TODO(vbatts) log a warning?
			continue
		}
		creator.DH.Entries = append(creator.DH.Entries, e)
		i++
	}
	return creator.DH, s.Err()
}