Skip to content

Commit da1c4b9

Browse files
authoredJun 27, 2024··
Avoid chowning files with expected ownership (#1333)
This speeds up git checkouts after fix-buildkite-agent-builds-permissions has run.
1 parent 2cc9d59 commit da1c4b9

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
 

‎internal/fixperms/fdfs/fdfs.go

+20
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,16 @@ func (s *FS) Lchown(path string, uid, gid int) error {
5757
return nil
5858
}
5959

60+
// Stat wraps fstatat(2) (with AT_SYMLINK_NOFOLLOW).
61+
func (s *FS) Stat(path string) (*unix.Stat_t, error) {
62+
var stat unix.Stat_t
63+
if err := unix.Fstatat(int(s.file.Fd()), path, &stat, unix.AT_SYMLINK_NOFOLLOW); err != nil {
64+
return nil, fmt.Errorf("fstatat(%d, %q): %w", s.file.Fd(), path, err)
65+
}
66+
67+
return &stat, nil
68+
}
69+
6070
// Sub wraps openat2(2) (with O_RDONLY+O_DIRECTORY+O_NOFOLLOW+O_CLOEXEC), and
6171
// returns an FS.
6272
func (s *FS) Sub(dir string) (*FS, error) {
@@ -102,6 +112,16 @@ func (s *FS) RecursiveChown(uid, gid int) error {
102112
}
103113
for _, d := range ds {
104114
if !d.IsDir() {
115+
// Skip lchown if the uid and gid already match. This avoids updating
116+
// the ctime of files unnecessarily.
117+
stat, err := s.Stat(d.Name())
118+
if err != nil {
119+
return err
120+
}
121+
if int(stat.Uid) == uid && int(stat.Gid) == gid {
122+
continue
123+
}
124+
105125
if err := s.Lchown(d.Name(), uid, gid); err != nil {
106126
return err
107127
}

0 commit comments

Comments
 (0)
Please sign in to comment.