mirror of
https://github.com/NVIDIA/nvidia-container-toolkit
synced 2024-11-22 16:29:18 +00:00
nvidia-ctk hook chmod: Only chmod if desired permissions are different
This is to avoid any unnecessary potential errors (e.g. due to permissions). Fixes: #143 Signed-off-by: Ievgen Popovych <jmennius@gmail.com>
This commit is contained in:
parent
c25376afa0
commit
f1d32f2cd3
@ -18,8 +18,10 @@ package chmod
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/fs"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
@ -112,7 +114,13 @@ func (m command) run(c *cli.Context, cfg *config) error {
|
|||||||
return fmt.Errorf("empty container root detected")
|
return fmt.Errorf("empty container root detected")
|
||||||
}
|
}
|
||||||
|
|
||||||
paths := m.getPaths(containerRoot, cfg.paths.Value())
|
desiredModeInt, err := strconv.ParseUint(cfg.mode, 8, 32)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to parse mode as octal: %v", err)
|
||||||
|
}
|
||||||
|
desiredMode := fs.FileMode(desiredModeInt)
|
||||||
|
|
||||||
|
paths := m.getPaths(containerRoot, cfg.paths.Value(), desiredMode)
|
||||||
if len(paths) == 0 {
|
if len(paths) == 0 {
|
||||||
m.logger.Debugf("No paths specified; exiting")
|
m.logger.Debugf("No paths specified; exiting")
|
||||||
return nil
|
return nil
|
||||||
@ -132,14 +140,19 @@ func (m command) run(c *cli.Context, cfg *config) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// getPaths updates the specified paths relative to the root.
|
// getPaths updates the specified paths relative to the root.
|
||||||
func (m command) getPaths(root string, paths []string) []string {
|
func (m command) getPaths(root string, paths []string, desiredMode fs.FileMode) []string {
|
||||||
var pathsInRoot []string
|
var pathsInRoot []string
|
||||||
for _, f := range paths {
|
for _, f := range paths {
|
||||||
path := filepath.Join(root, f)
|
path := filepath.Join(root, f)
|
||||||
if _, err := os.Stat(path); err != nil {
|
stat, err := os.Stat(path)
|
||||||
|
if err != nil {
|
||||||
m.logger.Debugf("Skipping path %q: %v", path, err)
|
m.logger.Debugf("Skipping path %q: %v", path, err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
if (stat.Mode()&(fs.ModePerm|fs.ModeSetuid|fs.ModeSetgid|fs.ModeSticky))^desiredMode == 0 {
|
||||||
|
m.logger.Debugf("Skipping path %q: already desired mode", path)
|
||||||
|
continue
|
||||||
|
}
|
||||||
pathsInRoot = append(pathsInRoot, path)
|
pathsInRoot = append(pathsInRoot, path)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user