pkg/environment: Migrate fs functions to fsx

This commit is contained in:
Tom Wiesing 2023-03-02 11:38:30 +01:00
parent 45540ab253
commit 5a43ecfaeb
No known key found for this signature in database
21 changed files with 155 additions and 199 deletions

View file

@ -18,6 +18,8 @@ var ErrCopySameFile = errors.New("src and dst must be different")
//
// When dst and src are the same file, returns ErrCopySameFile.
// When ctx is closed, the file is not copied.
//
// Ignores the umask.
func CopyFile(ctx context.Context, env environment.Environment, dst, src string) error {
if err := ctx.Err(); err != nil {
return err
@ -41,7 +43,7 @@ func CopyFile(ctx context.Context, env environment.Environment, dst, src string)
}
// open or create the destination
dstFile, err := env.Create(dst, srcStat.Mode())
dstFile, err := Create(dst, srcStat.Mode())
if err != nil {
return err
}
@ -141,8 +143,8 @@ func CopyDirectory(ctx context.Context, env environment.Environment, dst, src st
// create the directory, but ignore an error if the directory already exists.
// this is so that we can copy one tree into another tree.
err = env.Mkdir(dst, info.Mode())
if environment.IsExist(err) && IsDirectory(env, dst) {
err = Mkdir(dst, info.Mode())
if os.IsExist(err) && IsDirectory(env, dst) {
err = nil
}

16
pkg/fsx/fsx.go Normal file
View file

@ -0,0 +1,16 @@
// Package fsx provides additional file system functionality.
//
// Several functions in this package provide umask-ignoring functions.
// Using these functions intervenes with the global umask.
//
// It is not safe to use functions provided by the standard go library concurrently with this function.
// Users should take care that no other code in their application uses these functions.
package fsx
import "io/fs"
// DefaultFilePerm should be used by callers to use a consistent file mode for new files.
const DefaultFilePerm fs.FileMode = 0666
// DefaultDirPerm should be used by callers to use a consistent mode for new directories.
const DefaultDirPerm fs.FileMode = fs.ModeDir | fs.ModePerm

View file

@ -62,7 +62,7 @@ func couldBeSameFile(env environment.Environment, path1, path2 string) (same, au
}
// only 1 file does not exist => they could be different
if environment.IsNotExist(err1) != environment.IsNotExist(err2) {
if os.IsNotExist(err1) != os.IsNotExist(err2) {
return
}
}

View file

@ -1,35 +0,0 @@
package fsx
import (
"io/fs"
"os"
"time"
"github.com/FAU-CDI/wisski-distillery/pkg/environment"
)
// Touch touches a file.
// It is similar to the unix 'touch' command.
//
// If the file does not exist exists, it is created using [env.Create].
// If the file does exist, it's access and modification times are updated to the current time.
func Touch(env environment.Environment, path string, perm fs.FileMode) error {
if perm == 0 {
perm = environment.DefaultFilePerm
}
_, err := os.Stat(path)
switch {
case environment.IsNotExist(err):
f, err := env.Create(path, perm)
if err != nil {
return err
}
defer f.Close()
return nil
case err != nil:
return err
default:
now := time.Now().Local()
return os.Chtimes(path, now, now)
}
}

101
pkg/fsx/unmasked.go Normal file
View file

@ -0,0 +1,101 @@
package fsx
import (
"io/fs"
"os"
"sync"
"syscall"
"time"
)
// mask is the global mask lock
var m mask
// mask allows disabling and re-enabling the global umask.
// it is used by allow functions of this package.
type mask struct {
l sync.Mutex // locked?
umask int // previous mask
}
// Lock blocks until no other function is using this umask
// and then sets it to 0.
func (mask *mask) Lock() {
mask.l.Lock()
mask.umask = syscall.Umask(0)
}
func (mask *mask) Unlock() {
mask.umask = syscall.Umask(mask.umask)
mask.l.Unlock()
}
// WriteFile is like [os.WriteFile], but ignores the umask.
func WriteFile(path string, data []byte, mode fs.FileMode) error {
handle, err := Create(path, mode)
if err != nil {
return err
}
defer handle.Close()
if _, err := handle.Write(data); err != nil {
return err
}
return nil
}
// Create creates a new file with the given mode.
// This function ignores the umask.
func Create(path string, mode fs.FileMode) (*os.File, error) {
m.Lock()
defer m.Unlock()
return os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, mode)
}
// Mkdir creates a new directory with the given mode.
// This function ignores the umask.
func Mkdir(path string, mode fs.FileMode) error {
m.Lock()
defer m.Unlock()
return os.Mkdir(path, fs.ModeDir|mode)
}
// MkdirAll creates a new directory and all potentially missing parent directories.
// This function ignores the umask.
func MkdirAll(path string, mode fs.FileMode) error {
m.Lock()
defer m.Unlock()
return os.MkdirAll(path, fs.ModeDir|mode)
}
// Touch touches a file.
// It is similar to the unix 'touch' command.
//
// If the file does not exist exists, it is created using [Create].
// If the file does exist, it's access and modification times are updated to the current time.
//
// This function ignores the umask.
func Touch(path string, perm fs.FileMode) error {
if perm == 0 {
perm = DefaultFilePerm
}
_, err := os.Stat(path)
switch {
case os.IsNotExist(err):
f, err := Create(path, perm)
if err != nil {
return err
}
defer f.Close()
return nil
case err != nil:
return err
default:
now := time.Now().Local()
return os.Chtimes(path, now, now)
}
}