| 1 |
package desync |
| 2 |
|
| 3 |
import ( |
| 4 |
"crypto" |
| 5 |
"fmt" |
| 6 |
"io" |
| 7 |
"strings" |
| 8 |
) |
| 9 |
|
| 10 |
// MtreeFS prints the filesystem operations to a writer (which can be os.Stdout) |
| 11 |
// in mtree format. |
| 12 |
type MtreeFS struct { |
| 13 |
w io.Writer |
| 14 |
} |
| 15 |
|
| 16 |
var _ FilesystemWriter = MtreeFS{} |
| 17 |
|
| 18 |
// NewMtreeFS initializes a new instance of an mtree decoder that |
| 19 |
// writes its output into the provided stream. |
| 20 |
func NewMtreeFS(w io.Writer) (MtreeFS, error) { |
| 21 |
_, err := fmt.Fprintln(w, "#mtree v1.0") |
| 22 |
return MtreeFS{w: w}, err |
| 23 |
} |
| 24 |
|
| 25 |
func (fs MtreeFS) CreateDir(n NodeDirectory) error { |
| 26 |
attr := []string{mtreeFilename(n.Name), "type=dir"} |
| 27 |
attr = append(attr, fmt.Sprintf("mode=%04o", n.Mode.Perm())) |
| 28 |
attr = append(attr, fmt.Sprintf("uid=%d", n.UID)) |
| 29 |
attr = append(attr, fmt.Sprintf("gid=%d", n.GID)) |
| 30 |
attr = append(attr, fmt.Sprintf("time=%d.%9d", n.MTime.Unix(), n.MTime.Nanosecond())) |
| 31 |
fmt.Fprintln(fs.w, strings.Join(attr, " ")) |
| 32 |
return nil |
| 33 |
} |
| 34 |
|
| 35 |
func (fs MtreeFS) CreateFile(n NodeFile) error { |
| 36 |
attr := []string{mtreeFilename(n.Name), "type=file"} |
| 37 |
attr = append(attr, fmt.Sprintf("mode=%04o", n.Mode.Perm())) |
| 38 |
attr = append(attr, fmt.Sprintf("uid=%d", n.UID)) |
| 39 |
attr = append(attr, fmt.Sprintf("gid=%d", n.GID)) |
| 40 |
attr = append(attr, fmt.Sprintf("size=%d", n.Size)) |
| 41 |
attr = append(attr, fmt.Sprintf("time=%d.%09d", n.MTime.Unix(), n.MTime.Nanosecond())) |
| 42 |
|
| 43 |
switch Digest.Algorithm() { |
| 44 |
case crypto.SHA512_256: |
| 45 |
h := Digest.Algorithm().New() |
| 46 |
if _, err := io.Copy(h, n.Data); err != nil { |
| 47 |
return err |
| 48 |
} |
| 49 |
attr = append(attr, fmt.Sprintf("sha512256digest=%x", h.Sum(nil))) |
| 50 |
case crypto.SHA256: |
| 51 |
h := Digest.Algorithm().New() |
| 52 |
if _, err := io.Copy(h, n.Data); err != nil { |
| 53 |
return err |
| 54 |
} |
| 55 |
attr = append(attr, fmt.Sprintf("sha256digest=%x", h.Sum(nil))) |
| 56 |
default: |
| 57 |
return fmt.Errorf("unsupported mtree hash algorithm %d", Digest.Algorithm()) |
| 58 |
} |
| 59 |
fmt.Fprintln(fs.w, strings.Join(attr, " ")) |
| 60 |
return nil |
| 61 |
} |
| 62 |
|
| 63 |
func (fs MtreeFS) CreateSymlink(n NodeSymlink) error { |
| 64 |
attr := []string{mtreeFilename(n.Name), "type=link"} |
| 65 |
attr = append(attr, fmt.Sprintf("mode=%04o", n.Mode.Perm())) |
| 66 |
attr = append(attr, fmt.Sprintf("target=%s", mtreeFilename(n.Target))) |
| 67 |
attr = append(attr, fmt.Sprintf("uid=%d", n.UID)) |
| 68 |
attr = append(attr, fmt.Sprintf("gid=%d", n.GID)) |
| 69 |
attr = append(attr, fmt.Sprintf("time=%d.%9d", n.MTime.Unix(), n.MTime.Nanosecond())) |
| 70 |
fmt.Fprintln(fs.w, strings.Join(attr, " ")) |
| 71 |
return nil |
| 72 |
} |
| 73 |
|
| 74 |
func (fs MtreeFS) CreateDevice(n NodeDevice) error { |
| 75 |
attr := []string{mtreeFilename(n.Name)} |
| 76 |
if n.Mode&modeChar != 0 { |
| 77 |
attr = append(attr, "type=char") |
| 78 |
} else { |
| 79 |
attr = append(attr, "type=block") |
| 80 |
} |
| 81 |
attr = append(attr, fmt.Sprintf("mode=%04o", n.Mode.Perm())) |
| 82 |
attr = append(attr, fmt.Sprintf("uid=%d", n.UID)) |
| 83 |
attr = append(attr, fmt.Sprintf("gid=%d", n.GID)) |
| 84 |
attr = append(attr, fmt.Sprintf("time=%d.%9d", n.MTime.Unix(), n.MTime.Nanosecond())) |
| 85 |
fmt.Fprintln(fs.w, strings.Join(attr, " ")) |
| 86 |
return nil |
| 87 |
} |
| 88 |
|
| 89 |
// Converts filenames into an mtree-compatible format following the rules outlined in mtree(5): |
| 90 |
// |
| 91 |
// When encoding file or pathnames, any backslash character or character outside of the 95 |
| 92 |
// printable ASCII characters must be encoded as a backslash followed by three octal digits. |
| 93 |
// When reading mtree files, any appearance of a backslash followed by three octal digits should |
| 94 |
// be converted into the corresponding character. |
| 95 |
// |
| 96 |
// The space character is also escaped even though it is printable ASCII. mtree(5) lines are |
| 97 |
// space-delimited keyword=value pairs following the filename, so an unescaped space in an |
| 98 |
// untrusted entry name would let it inject keywords (e.g. "ignore", "optional", "nochange") |
| 99 |
// that suppress integrity checks. This matches the escaped set used by libarchive and BSD |
| 100 |
// mtree (non-printable, '\', ' ', '\t', '#'); tab and other control bytes are covered by c < 32. |
| 101 |
func mtreeFilename(s string) string { |
| 102 |
var b strings.Builder |
| 103 |
for _, c := range []byte(s) { |
| 104 |
switch { |
| 105 |
case c == '\\' || c == '#' || c == ' ' || c < 32 || c > 126: |
| 106 |
b.WriteString(fmt.Sprintf("\\%03o", c)) |
| 107 |
default: |
| 108 |
b.WriteByte(c) |
| 109 |
} |
| 110 |
} |
| 111 |
return b.String() |
| 112 |
} |