File: stat_unix.go

package info (click to toggle)
golang-github-tonistiigi-fsutil 0.0~git20200331.f427cf1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 400 kB
  • sloc: makefile: 7
file content (71 lines) | stat: -rw-r--r-- 1,384 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// +build !windows

package fsutil

import (
	"os"
	"syscall"

	"github.com/containerd/continuity/sysx"
	"github.com/pkg/errors"
	"github.com/tonistiigi/fsutil/types"
)

func loadXattr(origpath string, stat *types.Stat) error {
	xattrs, err := sysx.LListxattr(origpath)
	if err != nil {
		if errors.Cause(err) == syscall.ENOTSUP {
			return nil
		}
		return errors.Wrapf(err, "failed to xattr %s", origpath)
	}
	if len(xattrs) > 0 {
		m := make(map[string][]byte)
		for _, key := range xattrs {
			v, err := sysx.LGetxattr(origpath, key)
			if err == nil {
				m[key] = v
			}
		}
		stat.Xattrs = m
	}
	return nil
}

func setUnixOpt(fi os.FileInfo, stat *types.Stat, path string, seenFiles map[uint64]string) {
	s := fi.Sys().(*syscall.Stat_t)

	stat.Uid = s.Uid
	stat.Gid = s.Gid

	if !fi.IsDir() {
		if s.Mode&syscall.S_IFBLK != 0 ||
			s.Mode&syscall.S_IFCHR != 0 {
			stat.Devmajor = int64(major(uint64(s.Rdev)))
			stat.Devminor = int64(minor(uint64(s.Rdev)))
		}

		ino := s.Ino
		linked := false
		if seenFiles != nil {
			if s.Nlink > 1 {
				if oldpath, ok := seenFiles[ino]; ok {
					stat.Linkname = oldpath
					stat.Size_ = 0
					linked = true
				}
			}
			if !linked {
				seenFiles[ino] = path
			}
		}
	}
}

func major(device uint64) uint64 {
	return (device >> 8) & 0xfff
}

func minor(device uint64) uint64 {
	return (device & 0xff) | ((device >> 12) & 0xfff00)
}