| 1 |
package desync |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"fmt" |
| 6 |
"io" |
| 7 |
"os" |
| 8 |
"path" |
| 9 |
"sort" |
| 10 |
) |
| 11 |
|
| 12 |
// TarFeatureFlags are used as feature flags in the header of catar archives. These |
| 13 |
// should be used in index files when chunking a catar as well. TODO: Find out what |
| 14 |
// CaFormatWithPermissions is as that's not set in casync-produced catar archives. |
| 15 |
const TarFeatureFlags uint64 = CaFormatWith32BitUIDs | |
| 16 |
CaFormatWithNSecTime | |
| 17 |
CaFormatWithPermissions | |
| 18 |
CaFormatWithSymlinks | |
| 19 |
CaFormatWithDeviceNodes | |
| 20 |
CaFormatWithFIFOs | |
| 21 |
CaFormatWithSockets | |
| 22 |
CaFormatWithXattrs | |
| 23 |
CaFormatSHA512256 | |
| 24 |
CaFormatExcludeNoDump | |
| 25 |
CaFormatExcludeFile |
| 26 |
|
| 27 |
// Tar implements the tar command which recursively parses a directory tree, |
| 28 |
// and produces a stream of encoded casync format elements (catar file). |
| 29 |
func Tar(ctx context.Context, w io.Writer, fs FilesystemReader) error { |
| 30 |
enc := NewFormatEncoder(w) |
| 31 |
buf := &fsBufReader{fs, nil} |
| 32 |
_, err := tar(ctx, enc, buf, nil) |
| 33 |
return err |
| 34 |
} |
| 35 |
|
| 36 |
func tar(ctx context.Context, enc FormatEncoder, fs *fsBufReader, f *File) (n int64, err error) { |
| 37 |
// See if we're meant to stop |
| 38 |
select { |
| 39 |
case <-ctx.Done(): |
| 40 |
return n, Interrupted{} |
| 41 |
default: |
| 42 |
} |
| 43 |
|
| 44 |
// Read very first entry |
| 45 |
if f == nil { |
| 46 |
f, err := fs.Next() |
| 47 |
if err != nil { |
| 48 |
return 0, err |
| 49 |
} |
| 50 |
return tar(ctx, enc, fs, f) |
| 51 |
} |
| 52 |
|
| 53 |
// Skip (and warn about) things we can't encode properly |
| 54 |
if !(f.IsDir() || f.IsRegular() || f.IsSymlink() || f.IsDevice()) { |
| 55 |
fmt.Fprintf(os.Stderr, "skipping '%s' : unsupported node type\n", f.Name) |
| 56 |
return 0, nil |
| 57 |
} |
| 58 |
|
| 59 |
// CaFormatEntry |
| 60 |
entry := FormatEntry{ |
| 61 |
FormatHeader: FormatHeader{Size: 64, Type: CaFormatEntry}, |
| 62 |
FeatureFlags: TarFeatureFlags, |
| 63 |
UID: f.Uid, |
| 64 |
GID: f.Gid, |
| 65 |
Mode: f.Mode, |
| 66 |
MTime: f.ModTime, |
| 67 |
} |
| 68 |
nn, err := enc.Encode(entry) |
| 69 |
n += nn |
| 70 |
if err != nil { |
| 71 |
return n, err |
| 72 |
} |
| 73 |
|
| 74 |
// CaFormatXattrs - Write extended attributes elements. These have to be sorted by key. |
| 75 |
keys := make([]string, 0, len(f.Xattrs)) |
| 76 |
for key := range f.Xattrs { |
| 77 |
keys = append(keys, key) |
| 78 |
} |
| 79 |
sort.Strings(keys) |
| 80 |
for _, key := range keys { |
| 81 |
value := f.Xattrs[key] |
| 82 |
x := FormatXAttr{ |
| 83 |
FormatHeader: FormatHeader{Size: uint64(len(key)) + 1 + uint64(len(value)) + 1 + 16, Type: CaFormatXAttr}, |
| 84 |
NameAndValue: key + "\000" + string(value), |
| 85 |
} |
| 86 |
nn, err = enc.Encode(x) |
| 87 |
n += nn |
| 88 |
if err != nil { |
| 89 |
return n, err |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
switch { |
| 94 |
case f.IsDir(): |
| 95 |
dir := f.Path |
| 96 |
|
| 97 |
var items []FormatGoodbyeItem |
| 98 |
for { |
| 99 |
f, err := fs.Next() |
| 100 |
if err != nil { |
| 101 |
if err == io.EOF { |
| 102 |
break |
| 103 |
} |
| 104 |
return n, err |
| 105 |
} |
| 106 |
|
| 107 |
// End of the current dir? |
| 108 |
if !(path.Dir(f.Path) == dir) { |
| 109 |
fs.Buffer(f) |
| 110 |
break |
| 111 |
} |
| 112 |
|
| 113 |
start := n |
| 114 |
// CaFormatFilename - Write the filename element, then recursively encode |
| 115 |
// the items in the directory |
| 116 |
name := path.Base(f.Name) |
| 117 |
filename := FormatFilename{ |
| 118 |
FormatHeader: FormatHeader{Size: uint64(16 + len(name) + 1), Type: CaFormatFilename}, |
| 119 |
Name: name, |
| 120 |
} |
| 121 |
nn, err = enc.Encode(filename) |
| 122 |
n += nn |
| 123 |
if err != nil { |
| 124 |
return n, err |
| 125 |
} |
| 126 |
nn, err = tar(ctx, enc, fs, f) |
| 127 |
n += nn |
| 128 |
if err != nil { |
| 129 |
return n, err |
| 130 |
} |
| 131 |
|
| 132 |
items = append(items, FormatGoodbyeItem{ |
| 133 |
Offset: uint64(start), // This is temporary, it needs to be re-calculated later as offset from the goodbye marker |
| 134 |
Size: uint64(n - start), |
| 135 |
Hash: SipHash([]byte(name)), |
| 136 |
}) |
| 137 |
} |
| 138 |
|
| 139 |
// Fix the offsets in the item list, it needs to be the offset (backwards) |
| 140 |
// from the start of FormatGoodbye |
| 141 |
for i := range items { |
| 142 |
items[i].Offset = uint64(n) - items[i].Offset |
| 143 |
} |
| 144 |
|
| 145 |
// Turn the list of Goodbye items into a complete BST |
| 146 |
items = makeGoodbyeBST(items) |
| 147 |
|
| 148 |
// Append the tail marker |
| 149 |
items = append(items, FormatGoodbyeItem{ |
| 150 |
Offset: uint64(n), |
| 151 |
Size: uint64(16 + len(items)*24 + 24), |
| 152 |
Hash: CaFormatGoodbyeTailMarker, |
| 153 |
}) |
| 154 |
|
| 155 |
// Build the complete goodbye element and encode it |
| 156 |
goodbye := FormatGoodbye{ |
| 157 |
FormatHeader: FormatHeader{Size: uint64(16 + len(items)*24), Type: CaFormatGoodbye}, |
| 158 |
Items: items, |
| 159 |
} |
| 160 |
nn, err = enc.Encode(goodbye) |
| 161 |
n += nn |
| 162 |
if err != nil { |
| 163 |
return n, err |
| 164 |
} |
| 165 |
|
| 166 |
case f.IsRegular(): |
| 167 |
defer f.Close() |
| 168 |
payload := FormatPayload{ |
| 169 |
FormatHeader: FormatHeader{Size: 16 + uint64(f.Size), Type: CaFormatPayload}, |
| 170 |
Data: f.Data, |
| 171 |
} |
| 172 |
nn, err = enc.Encode(payload) |
| 173 |
n += nn |
| 174 |
if err != nil { |
| 175 |
return n, err |
| 176 |
} |
| 177 |
|
| 178 |
case f.IsSymlink(): |
| 179 |
symlink := FormatSymlink{ |
| 180 |
FormatHeader: FormatHeader{Size: uint64(16 + len(f.LinkTarget) + 1), Type: CaFormatSymlink}, |
| 181 |
Target: f.LinkTarget, |
| 182 |
} |
| 183 |
nn, err = enc.Encode(symlink) |
| 184 |
n += nn |
| 185 |
if err != nil { |
| 186 |
return n, err |
| 187 |
} |
| 188 |
|
| 189 |
case f.IsDevice(): |
| 190 |
device := FormatDevice{ |
| 191 |
FormatHeader: FormatHeader{Size: 32, Type: CaFormatDevice}, |
| 192 |
Major: f.DevMajor, |
| 193 |
Minor: f.DevMinor, |
| 194 |
} |
| 195 |
nn, err := enc.Encode(device) |
| 196 |
n += nn |
| 197 |
if err != nil { |
| 198 |
return n, err |
| 199 |
} |
| 200 |
|
| 201 |
default: |
| 202 |
return n, fmt.Errorf("unable to determine node type of '%s'", f.Name) |
| 203 |
} |
| 204 |
return |
| 205 |
} |
| 206 |
|
| 207 |
// Wrapper for filesystem reader to allow returning elements into a buffer |
| 208 |
type fsBufReader struct { |
| 209 |
fs FilesystemReader |
| 210 |
buf *File |
| 211 |
} |
| 212 |
|
| 213 |
func (b *fsBufReader) Next() (*File, error) { |
| 214 |
if b.buf != nil { |
| 215 |
f := b.buf |
| 216 |
b.buf = nil |
| 217 |
return f, nil |
| 218 |
} |
| 219 |
return b.fs.Next() |
| 220 |
} |
| 221 |
|
| 222 |
func (b *fsBufReader) Buffer(f *File) { |
| 223 |
if b.buf != nil { |
| 224 |
panic("can only unbuffer one file") |
| 225 |
} |
| 226 |
b.buf = f |
| 227 |
} |