Debian Codegraph @

stat.go

raw symbols SCIP
1 package fsutil
2
3 import (
4 "os"
5 "path/filepath"
6 "runtime"
7
8 "github.com/pkg/errors"
9 "github.com/tonistiigi/fsutil/types"
10 )
11
12 // constructs a Stat object. path is where the path can be found right
13 // now, relpath is the desired path to be recorded in the stat (so
14 // relative to whatever base dir is relevant). fi is the os.Stat
15 // info. inodemap is used to calculate hardlinks over a series of
16 // mkstat calls and maps inode to the canonical (aka "first") path for
17 // a set of hardlinks to that inode.
18 func mkstat(path, relpath string, fi os.FileInfo, inodemap map[uint64]string) (*types.Stat, error) {
19 relpath = filepath.ToSlash(relpath)
20
21 stat := &types.Stat{
22 Path: filepath.FromSlash(relpath),
23 Mode: uint32(fi.Mode()),
24 ModTime: fi.ModTime().UnixNano(),
25 }
26
27 setUnixOpt(fi, stat, relpath, inodemap)
28
29 if !fi.IsDir() {
30 stat.Size = fi.Size()
31 if fi.Mode()&os.ModeSymlink != 0 {
32 link, err := os.Readlink(path)
33 if err != nil {
34 return nil, errors.WithStack(err)
35 }
36 stat.Linkname = link
37 }
38 }
39 if err := loadXattr(path, stat); err != nil {
40 return nil, err
41 }
42
43 if runtime.GOOS == "windows" {
44 permPart := stat.Mode & uint32(os.ModePerm)
45 noPermPart := stat.Mode &^ uint32(os.ModePerm)
46 // Add the x bit: make everything +x from windows
47 permPart |= 0111
48 permPart &= 0755
49 stat.Mode = noPermPart | permPart
50 }
51
52 // Clear the socket bit since archive/tar.FileInfoHeader does not handle it
53 stat.Mode &^= uint32(os.ModeSocket)
54
55 return stat, nil
56 }
57
58 func Stat(path string) (*types.Stat, error) {
59 fi, err := os.Lstat(path)
60 if err != nil {
61 return nil, errors.WithStack(err)
62 }
63 return mkstat(path, filepath.Base(path), fi, nil)
64 }