| 1 |
package desync |
| 2 |
|
| 3 |
import ( |
| 4 |
gnutar "archive/tar" |
| 5 |
"io" |
| 6 |
"os" |
| 7 |
"path" |
| 8 |
"strings" |
| 9 |
) |
| 10 |
|
| 11 |
// paxSchilyXattr is the PAX-record key prefix GNU tar / star (and casync) use |
| 12 |
// for extended attributes. It matches the prefix archive/tar uses internally |
| 13 |
// for the deprecated Header.Xattrs field, so storing xattrs via PAXRecords |
| 14 |
// directly keeps the tar wire format byte-for-byte unchanged. |
| 15 |
const paxSchilyXattr = "SCHILY.xattr." |
| 16 |
|
| 17 |
// xattrsToPAXRecords converts a set of extended attributes into the PAX |
| 18 |
// records archive/tar writes them as. Returns nil when there are none so the |
| 19 |
// header stays in the same (non-PAX) format as before. |
| 20 |
func xattrsToPAXRecords(x Xattrs) map[string]string { |
| 21 |
if len(x) == 0 { |
| 22 |
return nil |
| 23 |
} |
| 24 |
r := make(map[string]string, len(x)) |
| 25 |
for k, v := range x { |
| 26 |
r[paxSchilyXattr+k] = v |
| 27 |
} |
| 28 |
return r |
| 29 |
} |
| 30 |
|
| 31 |
// paxRecordsToXattrs extracts extended attributes from PAX records, mirroring |
| 32 |
// how archive/tar populates the deprecated Header.Xattrs field on read. The |
| 33 |
// map is allocated lazily so the result is nil when no xattrs are present. |
| 34 |
func paxRecordsToXattrs(r map[string]string) map[string]string { |
| 35 |
var x map[string]string |
| 36 |
for k, v := range r { |
| 37 |
if name, ok := strings.CutPrefix(k, paxSchilyXattr); ok { |
| 38 |
if x == nil { |
| 39 |
x = make(map[string]string) |
| 40 |
} |
| 41 |
x[name] = v |
| 42 |
} |
| 43 |
} |
| 44 |
return x |
| 45 |
} |
| 46 |
|
| 47 |
// TarWriter uses a GNU tar archive for tar/untar operations of a catar archive. |
| 48 |
type TarWriter struct { |
| 49 |
w *gnutar.Writer |
| 50 |
format gnutar.Format |
| 51 |
} |
| 52 |
|
| 53 |
var _ FilesystemWriter = TarWriter{} |
| 54 |
|
| 55 |
// NewTarFS initializes a new instance of a GNU tar archive that can be used |
| 56 |
// for catar archive tar/untar operations. |
| 57 |
func NewTarWriter(w io.Writer) TarWriter { |
| 58 |
return TarWriter{gnutar.NewWriter(w), gnutar.FormatGNU} |
| 59 |
} |
| 60 |
|
| 61 |
func (fs TarWriter) CreateDir(n NodeDirectory) error { |
| 62 |
hdr := &gnutar.Header{ |
| 63 |
Typeflag: gnutar.TypeDir, |
| 64 |
Name: n.Name, |
| 65 |
Uid: n.UID, |
| 66 |
Gid: n.GID, |
| 67 |
Mode: int64(n.Mode), |
| 68 |
ModTime: n.MTime, |
| 69 |
PAXRecords: xattrsToPAXRecords(n.Xattrs), |
| 70 |
Format: fs.format, |
| 71 |
} |
| 72 |
return fs.w.WriteHeader(hdr) |
| 73 |
} |
| 74 |
|
| 75 |
func (fs TarWriter) CreateFile(n NodeFile) error { |
| 76 |
hdr := &gnutar.Header{ |
| 77 |
Typeflag: gnutar.TypeReg, |
| 78 |
Name: n.Name, |
| 79 |
Uid: n.UID, |
| 80 |
Gid: n.GID, |
| 81 |
Mode: int64(n.Mode), |
| 82 |
ModTime: n.MTime, |
| 83 |
Size: int64(n.Size), |
| 84 |
PAXRecords: xattrsToPAXRecords(n.Xattrs), |
| 85 |
Format: fs.format, |
| 86 |
} |
| 87 |
if err := fs.w.WriteHeader(hdr); err != nil { |
| 88 |
return err |
| 89 |
} |
| 90 |
_, err := io.Copy(fs.w, n.Data) |
| 91 |
return err |
| 92 |
} |
| 93 |
|
| 94 |
func (fs TarWriter) CreateSymlink(n NodeSymlink) error { |
| 95 |
hdr := &gnutar.Header{ |
| 96 |
Typeflag: gnutar.TypeSymlink, |
| 97 |
Linkname: n.Target, |
| 98 |
Name: n.Name, |
| 99 |
Uid: n.UID, |
| 100 |
Gid: n.GID, |
| 101 |
Mode: int64(n.Mode), |
| 102 |
ModTime: n.MTime, |
| 103 |
PAXRecords: xattrsToPAXRecords(n.Xattrs), |
| 104 |
Format: fs.format, |
| 105 |
} |
| 106 |
return fs.w.WriteHeader(hdr) |
| 107 |
} |
| 108 |
|
| 109 |
// We're not using os.Filemode here but the low-level system modes where the mode bits |
| 110 |
// are in the lower half. Can't use os.ModeCharDevice here. |
| 111 |
const modeChar = 0x4000 |
| 112 |
|
| 113 |
func (fs TarWriter) CreateDevice(n NodeDevice) error { |
| 114 |
var typ byte = gnutar.TypeBlock |
| 115 |
if n.Mode&modeChar != 0 { |
| 116 |
typ = gnutar.TypeChar |
| 117 |
} |
| 118 |
hdr := &gnutar.Header{ |
| 119 |
Typeflag: typ, |
| 120 |
Name: n.Name, |
| 121 |
Uid: n.UID, |
| 122 |
Gid: n.GID, |
| 123 |
Mode: int64(n.Mode), |
| 124 |
ModTime: n.MTime, |
| 125 |
PAXRecords: xattrsToPAXRecords(n.Xattrs), |
| 126 |
Devmajor: int64(n.Major), |
| 127 |
Devminor: int64(n.Minor), |
| 128 |
} |
| 129 |
return fs.w.WriteHeader(hdr) |
| 130 |
} |
| 131 |
|
| 132 |
func (fs TarWriter) Close() error { |
| 133 |
return fs.w.Close() |
| 134 |
} |
| 135 |
|
| 136 |
// TarReader uses a GNU tar archive as source for a tar operation (to produce |
| 137 |
// a catar). |
| 138 |
type TarReader struct { |
| 139 |
r *gnutar.Reader |
| 140 |
root *File |
| 141 |
} |
| 142 |
|
| 143 |
type TarReaderOptions struct { |
| 144 |
AddRoot bool |
| 145 |
} |
| 146 |
|
| 147 |
var _ FilesystemReader = &TarReader{} |
| 148 |
|
| 149 |
// NewTarFS initializes a new instance of a GNU tar archive that can be used |
| 150 |
// for catar archive tar/untar operations. |
| 151 |
func NewTarReader(r io.Reader, opts TarReaderOptions) *TarReader { |
| 152 |
var root *File |
| 153 |
if opts.AddRoot { |
| 154 |
root = &File{ |
| 155 |
Name: ".", |
| 156 |
Path: ".", |
| 157 |
Mode: os.ModeDir | 0755, |
| 158 |
} |
| 159 |
} |
| 160 |
return &TarReader{ |
| 161 |
r: gnutar.NewReader(r), |
| 162 |
root: root, |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
// Next returns the next filesystem entry or io.EOF when done. The caller is responsible |
| 167 |
// for closing the returned File object. |
| 168 |
func (fs *TarReader) Next() (f *File, err error) { |
| 169 |
if fs.root != nil { |
| 170 |
f = fs.root |
| 171 |
fs.root = nil |
| 172 |
return f, nil |
| 173 |
} |
| 174 |
|
| 175 |
h, err := fs.r.Next() |
| 176 |
if err != nil { |
| 177 |
return nil, err |
| 178 |
} |
| 179 |
|
| 180 |
info := h.FileInfo() |
| 181 |
|
| 182 |
f = &File{ |
| 183 |
Name: info.Name(), |
| 184 |
Path: path.Clean(h.Name), |
| 185 |
Mode: info.Mode(), |
| 186 |
ModTime: info.ModTime(), |
| 187 |
Size: uint64(info.Size()), |
| 188 |
LinkTarget: h.Linkname, |
| 189 |
Uid: h.Uid, |
| 190 |
Gid: h.Gid, |
| 191 |
Xattrs: paxRecordsToXattrs(h.PAXRecords), |
| 192 |
DevMajor: uint64(h.Devmajor), |
| 193 |
DevMinor: uint64(h.Devminor), |
| 194 |
Data: io.NopCloser(fs.r), |
| 195 |
} |
| 196 |
|
| 197 |
return f, nil |
| 198 |
} |