Skip to content

Commit e3d1947

Browse files
committed
MkdirAllNewAs,CopyDirectory: ensure dir perms
Because of umask, sometimes newly created files and directories can have permission bits different than expected. An easy solution would be to call os.Chmod after creation -- this is how CopyFile already works. Now, let's do the same for directories. PS The alternative to that would be to temporary set umask to 0, but it is very problematic in Go as it affects other threads. Signed-off-by: Kir Kolyshkin <[email protected]>
1 parent d7fdd64 commit e3d1947

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

fileutils.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ func CopyDirectory(source string, dest string) error {
125125
if err != nil {
126126
return nil
127127
}
128+
destPath := filepath.Join(dest, relPath)
128129

129130
if info.IsDir() {
130131
// Skip the source directory.
@@ -138,18 +139,20 @@ func CopyDirectory(source string, dest string) error {
138139
uid := int(st.Uid)
139140
gid := int(st.Gid)
140141

141-
if err := os.Mkdir(filepath.Join(dest, relPath), info.Mode()); err != nil {
142+
if err := os.Mkdir(destPath, info.Mode()); err != nil {
142143
return err
143144
}
144-
145-
if err := os.Lchown(filepath.Join(dest, relPath), uid, gid); err != nil {
145+
if err := os.Lchown(destPath, uid, gid); err != nil {
146+
return err
147+
}
148+
if err := os.Chmod(destPath, info.Mode()); err != nil {
146149
return err
147150
}
148151
}
149152
return nil
150153
}
151154

152-
return CopyFile(path, filepath.Join(dest, relPath))
155+
return CopyFile(path, destPath)
153156
})
154157
}
155158

idtools.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ func MkdirAllNewAs(path string, mode os.FileMode, ownerUID, ownerGID int) error
4949
if err := os.Chown(pathComponent, ownerUID, ownerGID); err != nil {
5050
return err
5151
}
52+
if err := os.Chmod(pathComponent, mode); err != nil {
53+
return err
54+
}
5255
}
5356
return nil
5457
}

0 commit comments

Comments
 (0)