| 1 |
package desync |
| 2 |
|
| 3 |
import ( |
| 4 |
"io" |
| 5 |
"os" |
| 6 |
"syscall" |
| 7 |
"time" |
| 8 |
) |
| 9 |
|
| 10 |
func isDevice(m os.FileMode) bool { |
| 11 |
return m&os.ModeDevice != 0 |
| 12 |
} |
| 13 |
|
| 14 |
// FilesystemWriter is a filesystem implementation that supports untar'ing |
| 15 |
// a catar archive to. |
| 16 |
type FilesystemWriter interface { |
| 17 |
CreateDir(n NodeDirectory) error |
| 18 |
CreateFile(n NodeFile) error |
| 19 |
CreateSymlink(n NodeSymlink) error |
| 20 |
CreateDevice(n NodeDevice) error |
| 21 |
} |
| 22 |
|
| 23 |
// FilesystemFinalizer is implemented by filesystems that defer part of their |
| 24 |
// work, such as directory metadata that can only be applied once a directory |
| 25 |
// has been populated, until the whole archive has been written. UnTar calls |
| 26 |
// Finalize when it reaches the end of the archive. It is not called when the |
| 27 |
// archive can't be read to the end, so implementations that leave the target |
| 28 |
// in an intermediate state until then should apply what they can on Close(). |
| 29 |
type FilesystemFinalizer interface { |
| 30 |
Finalize() error |
| 31 |
} |
| 32 |
|
| 33 |
// FilesystemReader is an interface for source filesystem to be used during |
| 34 |
// tar operations. Next() is expected to return files and directories in a |
| 35 |
// consistent and stable order and return io.EOF when no further files are available. |
| 36 |
type FilesystemReader interface { |
| 37 |
Next() (*File, error) |
| 38 |
} |
| 39 |
|
| 40 |
// File represents a filesystem object such as directory, file, symlink or device. |
| 41 |
// It's used when creating archives from a source filesystem which can be a real |
| 42 |
// OS filesystem, or another archive stream such as tar. |
| 43 |
type File struct { |
| 44 |
Name string |
| 45 |
Path string |
| 46 |
Mode os.FileMode |
| 47 |
|
| 48 |
Size uint64 |
| 49 |
|
| 50 |
// Link target for symlinks |
| 51 |
LinkTarget string |
| 52 |
|
| 53 |
// Modification time |
| 54 |
ModTime time.Time |
| 55 |
|
| 56 |
// User/group IDs |
| 57 |
Uid int |
| 58 |
Gid int |
| 59 |
|
| 60 |
// Major/Minor for character or block devices |
| 61 |
DevMajor uint64 |
| 62 |
DevMinor uint64 |
| 63 |
|
| 64 |
// Extended attributes |
| 65 |
Xattrs map[string]string |
| 66 |
|
| 67 |
// File content. Nil for non-regular files. |
| 68 |
Data io.ReadCloser |
| 69 |
} |
| 70 |
|
| 71 |
func (f *File) IsDir() bool { |
| 72 |
return f.Mode.IsDir() |
| 73 |
} |
| 74 |
|
| 75 |
func (f *File) IsRegular() bool { |
| 76 |
return f.Mode.IsRegular() |
| 77 |
} |
| 78 |
|
| 79 |
func (f *File) IsSymlink() bool { |
| 80 |
return f.Mode&os.ModeSymlink != 0 |
| 81 |
} |
| 82 |
|
| 83 |
func (f *File) IsDevice() bool { |
| 84 |
return f.Mode&os.ModeDevice != 0 |
| 85 |
} |
| 86 |
|
| 87 |
// Close closes the file data reader if any. It's safe to call |
| 88 |
// for non-regular files as well. |
| 89 |
func (f *File) Close() error { |
| 90 |
if f.Data != nil { |
| 91 |
return f.Data.Close() |
| 92 |
} |
| 93 |
return nil |
| 94 |
} |
| 95 |
|
| 96 |
// StatModeToFilemode converts syscall mode to Go's os.Filemode value. |
| 97 |
func StatModeToFilemode(mode uint32) os.FileMode { |
| 98 |
fm := os.FileMode(mode & 0777) |
| 99 |
switch mode & syscall.S_IFMT { |
| 100 |
case syscall.S_IFBLK: |
| 101 |
fm |= os.ModeDevice |
| 102 |
case syscall.S_IFCHR: |
| 103 |
fm |= os.ModeDevice | os.ModeCharDevice |
| 104 |
case syscall.S_IFDIR: |
| 105 |
fm |= os.ModeDir |
| 106 |
case syscall.S_IFIFO: |
| 107 |
fm |= os.ModeNamedPipe |
| 108 |
case syscall.S_IFLNK: |
| 109 |
fm |= os.ModeSymlink |
| 110 |
case syscall.S_IFSOCK: |
| 111 |
fm |= os.ModeSocket |
| 112 |
} |
| 113 |
if mode&syscall.S_ISGID != 0 { |
| 114 |
fm |= os.ModeSetgid |
| 115 |
} |
| 116 |
if mode&syscall.S_ISUID != 0 { |
| 117 |
fm |= os.ModeSetuid |
| 118 |
} |
| 119 |
if mode&syscall.S_ISVTX != 0 { |
| 120 |
fm |= os.ModeSticky |
| 121 |
} |
| 122 |
return fm |
| 123 |
} |
| 124 |
|
| 125 |
// FilemodeToStatMode converts Go's os.Filemode value into the syscall equivalent. |
| 126 |
func FilemodeToStatMode(mode os.FileMode) uint32 { |
| 127 |
o := uint32(mode.Perm()) |
| 128 |
switch m := mode & os.ModeType; m { |
| 129 |
case os.ModeDevice: |
| 130 |
o |= syscall.S_IFBLK |
| 131 |
case os.ModeDevice | os.ModeCharDevice: |
| 132 |
o |= syscall.S_IFCHR |
| 133 |
case os.ModeDir: |
| 134 |
o |= syscall.S_IFDIR |
| 135 |
case os.ModeNamedPipe: |
| 136 |
o |= syscall.S_IFIFO |
| 137 |
case os.ModeSymlink: |
| 138 |
o |= syscall.S_IFLNK |
| 139 |
case os.ModeSocket: |
| 140 |
o |= syscall.S_IFSOCK |
| 141 |
default: |
| 142 |
o |= syscall.S_IFREG |
| 143 |
} |
| 144 |
|
| 145 |
if mode&os.ModeSetuid != 0 { |
| 146 |
o |= syscall.S_ISUID |
| 147 |
} |
| 148 |
if mode&os.ModeSetgid != 0 { |
| 149 |
o |= syscall.S_ISGID |
| 150 |
} |
| 151 |
if mode&os.ModeSticky != 0 { |
| 152 |
o |= syscall.S_ISVTX |
| 153 |
} |
| 154 |
return o |
| 155 |
} |