Skip to content

Commit eae523a

Browse files
author
Per Goncalves da Silva
committed
fixes
Signed-off-by: Per Goncalves da Silva <[email protected]>
1 parent bb99ba7 commit eae523a

File tree

1 file changed

+45
-51
lines changed

1 file changed

+45
-51
lines changed

internal/rukpak/source/helpers.go

Lines changed: 45 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -8,61 +8,26 @@ import (
88
"time"
99
)
1010

11-
// SetReadOnlyRecursive sets directory with path given by `root` as read-only
12-
func SetReadOnlyRecursive(root string) error {
13-
return filepath.WalkDir(root, func(path string, d os.DirEntry, err error) error {
14-
if err != nil {
15-
return err
16-
}
17-
18-
fi, err := d.Info()
19-
if err != nil {
20-
return err
21-
}
11+
const (
12+
OwnerWritableFileMode os.FileMode = 0700
13+
OwnerWritableDirMode os.FileMode = 0700
14+
OwnerReadOnlyFileMode os.FileMode = 0400
15+
OwnerReadOnlyDirMode os.FileMode = 0500
16+
)
2217

23-
if err := func() error {
24-
switch typ := fi.Mode().Type(); typ {
25-
case os.ModeSymlink:
26-
// do not follow symlinks
27-
// 1. if they resolve to other locations in the root, we'll find them anyway
28-
// 2. if they resolve to other locations outside the root, we don't want to change their permissions
29-
return nil
30-
case os.ModeDir:
31-
return os.Chmod(path, 0500)
32-
case 0: // regular file
33-
return os.Chmod(path, 0400)
34-
default:
35-
return fmt.Errorf("refusing to change ownership of file %q with type %v", path, typ.String())
36-
}
37-
}(); err != nil {
38-
return err
39-
}
40-
return nil
41-
})
18+
// SetReadOnlyRecursive recursively sets files and directories under the path given by `root` as read-only
19+
func SetReadOnlyRecursive(root string) error {
20+
return setModeRecursive(root, OwnerReadOnlyFileMode, OwnerReadOnlyDirMode)
4221
}
4322

44-
// UnsetReadOnlyRecursive unsets directory with path given by `root` as read-only
45-
func UnsetReadOnlyRecursive(root string) error {
46-
return filepath.WalkDir(root, func(path string, d os.DirEntry, err error) error {
47-
if os.IsNotExist(err) {
48-
return nil
49-
}
50-
if err != nil {
51-
return err
52-
}
53-
if !d.IsDir() {
54-
return nil
55-
}
56-
if err := os.Chmod(path, 0700); err != nil {
57-
return err
58-
}
59-
return nil
60-
})
23+
// SetWritableRecursive recursively sets files and directories under the path given by `root` as writable
24+
func SetWritableRecursive(root string) error {
25+
return setModeRecursive(root, OwnerWritableFileMode, OwnerWritableDirMode)
6126
}
6227

6328
// DeleteReadOnlyRecursive deletes read-only directory with path given by `root`
6429
func DeleteReadOnlyRecursive(root string) error {
65-
if err := UnsetReadOnlyRecursive(root); err != nil {
30+
if err := SetWritableRecursive(root); err != nil {
6631
return fmt.Errorf("error making directory writable for deletion: %w", err)
6732
}
6833
return os.RemoveAll(root)
@@ -73,14 +38,43 @@ func DeleteReadOnlyRecursive(root string) error {
7338
// If `unpackPath` is a file, it will be deleted and false will be returned without an error.
7439
func IsImageUnpacked(unpackPath string) (bool, time.Time, error) {
7540
unpackStat, err := os.Stat(unpackPath)
41+
if errors.Is(err, os.ErrNotExist) {
42+
return false, time.Time{}, nil
43+
}
7644
if err != nil {
77-
if errors.Is(err, os.ErrNotExist) {
78-
return false, time.Time{}, nil
79-
}
8045
return false, time.Time{}, err
8146
}
8247
if !unpackStat.IsDir() {
8348
return false, time.Time{}, os.Remove(unpackPath)
8449
}
8550
return true, unpackStat.ModTime(), nil
8651
}
52+
53+
func setModeRecursive(path string, fileMode os.FileMode, dirMode os.FileMode) error {
54+
return filepath.WalkDir(path, func(path string, d os.DirEntry, err error) error {
55+
if os.IsNotExist(err) {
56+
return nil
57+
}
58+
if err != nil {
59+
return err
60+
}
61+
fi, err := d.Info()
62+
if err != nil {
63+
return err
64+
}
65+
66+
switch typ := fi.Mode().Type(); typ {
67+
case os.ModeSymlink:
68+
// do not follow symlinks
69+
// 1. if they resolve to other locations in the root, we'll find them anyway
70+
// 2. if they resolve to other locations outside the root, we don't want to change their permissions
71+
return nil
72+
case os.ModeDir:
73+
return os.Chmod(path, dirMode)
74+
case 0: // regular file
75+
return os.Chmod(path, fileMode)
76+
default:
77+
return fmt.Errorf("refusing to change ownership of file %q with type %v", path, typ.String())
78+
}
79+
})
80+
}

0 commit comments

Comments
 (0)