fsx: Remove environment references
This commit removes the reference to the environment from the fsx package.
This commit is contained in:
parent
5a43ecfaeb
commit
3263920d6b
25 changed files with 167 additions and 183 deletions
|
|
@ -7,7 +7,6 @@ import (
|
|||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/FAU-CDI/wisski-distillery/pkg/environment"
|
||||
"github.com/tkw1536/pkglib/contextx"
|
||||
)
|
||||
|
||||
|
|
@ -16,16 +15,14 @@ var ErrCopySameFile = errors.New("src and dst must be different")
|
|||
// CopyFile copies a file from src to dst.
|
||||
// When src points to a symbolic link, will copy the symbolic link.
|
||||
//
|
||||
// When dst and src are the same file, returns ErrCopySameFile.
|
||||
// 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 {
|
||||
func CopyFile(ctx context.Context, dst, src string) error {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if SameFile(env, src, dst) {
|
||||
if SameFile(src, dst) {
|
||||
return ErrCopySameFile
|
||||
}
|
||||
|
||||
|
|
@ -56,13 +53,13 @@ func CopyFile(ctx context.Context, env environment.Environment, dst, src string)
|
|||
|
||||
// CopyLink copies a link from src to dst.
|
||||
// If dst already exists, it is deleted and then re-created.
|
||||
func CopyLink(ctx context.Context, env environment.Environment, dst, src string) error {
|
||||
func CopyLink(ctx context.Context, dst, src string) error {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// if they're the same file that is an error
|
||||
if SameFile(env, dst, src) {
|
||||
if SameFile(dst, src) {
|
||||
return ErrCopySameFile
|
||||
}
|
||||
|
||||
|
|
@ -73,7 +70,7 @@ func CopyLink(ctx context.Context, env environment.Environment, dst, src string)
|
|||
}
|
||||
|
||||
// delete it if it already exists
|
||||
if Exists(env, dst) {
|
||||
if Exists(dst) {
|
||||
if err := os.Remove(dst); err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
@ -92,12 +89,12 @@ var ErrDstFile = errors.New("dst is a file")
|
|||
// When a directory already exists, additional files are not deleted.
|
||||
//
|
||||
// onCopy, when not nil, is called for each file or directory being copied.
|
||||
func CopyDirectory(ctx context.Context, env environment.Environment, dst, src string, onCopy func(dst, src string)) error {
|
||||
func CopyDirectory(ctx context.Context, dst, src string, onCopy func(dst, src string)) error {
|
||||
// sanity checks
|
||||
if SameFile(env, src, dst) {
|
||||
if SameFile(src, dst) {
|
||||
return ErrCopySameFile
|
||||
}
|
||||
if IsFile(env, dst) {
|
||||
if IsFile(dst) {
|
||||
return ErrDstFile
|
||||
}
|
||||
|
||||
|
|
@ -133,18 +130,18 @@ func CopyDirectory(ctx context.Context, env environment.Environment, dst, src st
|
|||
|
||||
// if we have a symbolic link, copy the link!
|
||||
if info.Mode()&fs.ModeSymlink != 0 {
|
||||
return CopyLink(ctx, env, dst, path)
|
||||
return CopyLink(ctx, dst, path)
|
||||
}
|
||||
|
||||
// if we got a file, we should copy it normally
|
||||
if !d.IsDir() {
|
||||
return CopyFile(ctx, env, dst, path)
|
||||
return CopyFile(ctx, dst, path)
|
||||
}
|
||||
|
||||
// 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 = Mkdir(dst, info.Mode())
|
||||
if os.IsExist(err) && IsDirectory(env, dst) {
|
||||
if os.IsExist(err) && IsDirectory(dst) {
|
||||
err = nil
|
||||
}
|
||||
|
||||
|
|
|
|||
56
pkg/fsx/create.go
Normal file
56
pkg/fsx/create.go
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
package fsx
|
||||
|
||||
import (
|
||||
"io/fs"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Create is like [os.Create] with an additional mode argument.
|
||||
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)
|
||||
}
|
||||
|
||||
// WriteFile is like [os.WriteFile].
|
||||
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
|
||||
}
|
||||
|
||||
// Touch touches a file.
|
||||
// It is similar to the unix 'touch' command.
|
||||
//
|
||||
// If the file does not exist, it is created using [Create].
|
||||
// If the file does exist, its' access and modification times are updated to the current time.
|
||||
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)
|
||||
}
|
||||
}
|
||||
25
pkg/fsx/dir.go
Normal file
25
pkg/fsx/dir.go
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
package fsx
|
||||
|
||||
import (
|
||||
"io/fs"
|
||||
"os"
|
||||
)
|
||||
|
||||
// DefaultDirPerm should be used by callers to use a consistent mode for new directories.
|
||||
const DefaultDirPerm fs.FileMode = fs.ModeDir | fs.ModePerm
|
||||
|
||||
// Mkdir is like [os.Mkdir].
|
||||
func Mkdir(path string, mode fs.FileMode) error {
|
||||
m.Lock()
|
||||
defer m.Unlock()
|
||||
|
||||
return os.Mkdir(path, fs.ModeDir|mode)
|
||||
}
|
||||
|
||||
// MkdirAll is like [os.MkdirAll].
|
||||
func MkdirAll(path string, mode fs.FileMode) error {
|
||||
m.Lock()
|
||||
defer m.Unlock()
|
||||
|
||||
return os.MkdirAll(path, fs.ModeDir|mode)
|
||||
}
|
||||
|
|
@ -1,16 +1,37 @@
|
|||
// 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.
|
||||
// All functions in this package ignore the umask.
|
||||
// As such it is not safe to use otherwise equivalent functions provided by the standard go library concurrently with this package.
|
||||
// Users should take care that no other code in their application uses these functions.
|
||||
package fsx
|
||||
|
||||
import "io/fs"
|
||||
import (
|
||||
"io/fs"
|
||||
"sync"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
// 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
|
||||
// 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()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,17 +3,15 @@ package fsx
|
|||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/FAU-CDI/wisski-distillery/pkg/environment"
|
||||
)
|
||||
|
||||
// SameFile checks if path1 and path2 refer to the same file.
|
||||
// If both files exist, they are compared using [env.SameFile].
|
||||
// If both files exist, they are compared using [os.SameFile].
|
||||
// If both files do not exist, the paths are first compared syntactically and then via recursion on [filepath.Dir].
|
||||
func SameFile(env environment.Environment, path1, path2 string) bool {
|
||||
func SameFile(path1, path2 string) bool {
|
||||
|
||||
// initial attempt: check if directly
|
||||
same, certain := couldBeSameFile(env, path1, path2)
|
||||
same, certain := couldBeSameFile(path1, path2)
|
||||
if certain {
|
||||
return same
|
||||
}
|
||||
|
|
@ -30,19 +28,19 @@ func SameFile(env environment.Environment, path1, path2 string) bool {
|
|||
|
||||
// compare the base names!
|
||||
{
|
||||
same, _ := couldBeSameFile(env, d1, d2)
|
||||
same, _ := couldBeSameFile(d1, d2)
|
||||
return same
|
||||
}
|
||||
}
|
||||
|
||||
// couldBeSameFile checks if path1 might be the same as path2.
|
||||
//
|
||||
// If both files exist, compares using [env.SameFile].
|
||||
// If both files exist, compares using [os.SameFile].
|
||||
// Otherwise compares absolute paths using string comparison.
|
||||
//
|
||||
// same indicates if they might be the same file.
|
||||
// authorative indiciates if the result is authorative.
|
||||
func couldBeSameFile(env environment.Environment, path1, path2 string) (same, authorative bool) {
|
||||
func couldBeSameFile(path1, path2 string) (same, authorative bool) {
|
||||
{
|
||||
// stat both files
|
||||
info1, err1 := os.Stat(path1)
|
||||
|
|
|
|||
|
|
@ -4,30 +4,28 @@ package fsx
|
|||
import (
|
||||
"io/fs"
|
||||
"os"
|
||||
|
||||
"github.com/FAU-CDI/wisski-distillery/pkg/environment"
|
||||
)
|
||||
|
||||
// Exists checks if the given path exists
|
||||
func Exists(env environment.Environment, path string) bool {
|
||||
func Exists(path string) bool {
|
||||
_, err := os.Lstat(path)
|
||||
return err == nil
|
||||
}
|
||||
|
||||
// IsDirectory checks if the provided path exists and is a directory
|
||||
func IsDirectory(env environment.Environment, path string) bool {
|
||||
func IsDirectory(path string) bool {
|
||||
info, err := os.Stat(path)
|
||||
return err == nil && info.Mode().IsDir()
|
||||
}
|
||||
|
||||
// IsFile checks if the provided path exists and is a regular file
|
||||
func IsFile(env environment.Environment, path string) bool {
|
||||
func IsFile(path string) bool {
|
||||
info, err := os.Stat(path)
|
||||
return err == nil && info.Mode().IsRegular()
|
||||
}
|
||||
|
||||
// IsLink checks if the provided path exists and is a symlink
|
||||
func IsLink(env environment.Environment, path string) bool {
|
||||
func IsLink(path string) bool {
|
||||
info, err := os.Lstat(path)
|
||||
return err == nil && info.Mode()&fs.ModeSymlink != 0
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,101 +0,0 @@
|
|||
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)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue