| 1 | //go:build !windows |
| 2 | // +build !windows |
| 3 | |
| 4 | package fsutil |
| 5 | |
| 6 | import ( |
| 7 | "os" |
| 8 | "syscall" |
| 9 | |
| 10 | "github.com/containerd/continuity/sysx" |
| 11 | "github.com/pkg/errors" |
| 12 | "github.com/tonistiigi/fsutil/types" |
| 13 | ) |
| 14 | |
| 15 | func rewriteMetadata(p string, stat *types.Stat) error { |
| 16 | for key, value := range stat.Xattrs { |
| 17 | sysx.Setxattr(p, key, value, 0) |
| 18 | } |
| 19 | |
| 20 | if err := os.Lchown(p, int(stat.Uid), int(stat.Gid)); err != nil { |
| 21 | return errors.WithStack(err) |
| 22 | } |
| 23 | |
| 24 | if os.FileMode(stat.Mode)&os.ModeSymlink == 0 { |
| 25 | if err := os.Chmod(p, os.FileMode(stat.Mode)); err != nil { |
| 26 | return errors.WithStack(err) |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | if err := chtimes(p, stat.ModTime); err != nil { |
| 31 | return err |
| 32 | } |
| 33 | |
| 34 | return nil |
| 35 | } |
| 36 | |
| 37 | // handleTarTypeBlockCharFifo is an OS-specific helper function used by |
| 38 | // createTarFile to handle the following types of header: Block; Char; Fifo |
| 39 | func handleTarTypeBlockCharFifo(path string, stat *types.Stat) error { |
| 40 | mode := uint32(stat.Mode & 07777) |
| 41 | if os.FileMode(stat.Mode)&os.ModeCharDevice != 0 { |
| 42 | mode |= syscall.S_IFCHR |
| 43 | } else if os.FileMode(stat.Mode)&os.ModeNamedPipe != 0 { |
| 44 | mode |= syscall.S_IFIFO |
| 45 | } else { |
| 46 | mode |= syscall.S_IFBLK |
| 47 | } |
| 48 | |
| 49 | if err := createSpecialFile(path, mode, stat); err != nil { |
| 50 | return errors.WithStack(err) |
| 51 | } |
| 52 | return nil |
| 53 | } |
| 54 | |
| 55 | func renameFile(src, dst string) error { |
| 56 | if err := os.Rename(src, dst); err != nil { |
| 57 | return errors.Wrapf(err, "failed to rename %s to %s", src, dst) |
| 58 | } |
| 59 | return nil |
| 60 | } |