mirror of
https://github.com/containers/podman.git
synced 2025-12-02 02:58:03 +08:00
Update c/common and avoid setting umask
We can now use the new API for creating files and directories without setting the umask to allow parallel usage of those methods. This patch also bumps c/common for that. [NO NEW TESTS NEEDED] Signed-off-by: Sascha Grunert <sgrunert@redhat.com>
This commit is contained in:
58
vendor/github.com/containers/common/pkg/umask/umask.go
generated
vendored
Normal file
58
vendor/github.com/containers/common/pkg/umask/umask.go
generated
vendored
Normal file
@@ -0,0 +1,58 @@
|
||||
package umask
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
// MkdirAllIgnoreUmask creates a directory by ignoring the currently set umask.
|
||||
func MkdirAllIgnoreUmask(dir string, mode os.FileMode) error {
|
||||
parent := dir
|
||||
dirs := []string{}
|
||||
|
||||
// Find all parent directories which would have been created by MkdirAll
|
||||
for {
|
||||
if _, err := os.Stat(parent); err == nil {
|
||||
break
|
||||
} else if !os.IsNotExist(err) {
|
||||
return fmt.Errorf("cannot stat %s: %w", dir, err)
|
||||
}
|
||||
|
||||
dirs = append(dirs, parent)
|
||||
newParent := filepath.Dir(parent)
|
||||
|
||||
// Only possible if the root paths are not existing, which would be odd
|
||||
if parent == newParent {
|
||||
break
|
||||
}
|
||||
|
||||
parent = newParent
|
||||
}
|
||||
|
||||
if err := os.MkdirAll(dir, mode); err != nil {
|
||||
return fmt.Errorf("create directory %s: %w", dir, err)
|
||||
}
|
||||
|
||||
for _, d := range dirs {
|
||||
if err := os.Chmod(d, mode); err != nil {
|
||||
return fmt.Errorf("chmod directory %s: %w", d, err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// WriteFileIgnoreUmask write the provided data to the path by ignoring the
|
||||
// currently set umask.
|
||||
func WriteFileIgnoreUmask(path string, data []byte, mode os.FileMode) error {
|
||||
if err := os.WriteFile(path, data, mode); err != nil {
|
||||
return fmt.Errorf("write file: %w", err)
|
||||
}
|
||||
|
||||
if err := os.Chmod(path, mode); err != nil {
|
||||
return fmt.Errorf("chmod file: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user