Use fsx package and friends from pkglib

This commit is contained in:
Tom Wiesing 2023-04-08 17:51:17 +02:00
parent 1f8c55da7c
commit 0f6803f890
No known key found for this signature in database
35 changed files with 91 additions and 493 deletions

View file

@ -1,150 +0,0 @@
package fsx
import (
"context"
"errors"
"io/fs"
"os"
"path/filepath"
"github.com/tkw1536/pkglib/contextx"
)
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 ctx is closed, the file is not copied.
func CopyFile(ctx context.Context, dst, src string) error {
if err := ctx.Err(); err != nil {
return err
}
if SameFile(src, dst) {
return ErrCopySameFile
}
// open the source
srcFile, err := os.Open(src)
if err != nil {
return err
}
defer srcFile.Close()
// stat it to get the mode!
srcStat, err := srcFile.Stat()
if err != nil {
return err
}
// open or create the destination
dstFile, err := Create(dst, srcStat.Mode())
if err != nil {
return err
}
defer dstFile.Close()
// and do the copy!
_, err = contextx.Copy(ctx, dstFile, srcFile)
return err
}
// CopyLink copies a link from src to dst.
// If dst already exists, it is deleted and then re-created.
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(dst, src) {
return ErrCopySameFile
}
// read the link target
target, err := os.Readlink(src)
if err != nil {
return err
}
// delete it if it already exists
if Exists(dst) {
if err := os.Remove(dst); err != nil {
return err
}
}
// make the symbolic link!
return os.Symlink(target, dst)
}
var ErrDstFile = errors.New("dst is a file")
// CopyDirectory copies the directory src to dst recursively.
// Copying is aborted when ctx is closed.
//
// Existing files and directories are overwritten.
// 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, dst, src string, onCopy func(dst, src string)) error {
// sanity checks
if SameFile(src, dst) {
return ErrCopySameFile
}
if IsFile(dst) {
return ErrDstFile
}
return filepath.WalkDir(src, func(path string, d fs.DirEntry, err error) error {
// someone previously returned an error
if err != nil {
return err
}
// context was closed
if err := ctx.Err(); err != nil {
return err
}
// determine the real target path
var relpath string
relpath, err = filepath.Rel(src, path)
if err != nil {
return err
}
dst := filepath.Join(dst, relpath)
// call the hook
if onCopy != nil {
onCopy(dst, src)
}
// stat the directory, so that we can get mode, and info later!
info, err := d.Info()
if err != nil {
return err
}
// if we have a symbolic link, copy the link!
if info.Mode()&fs.ModeSymlink != 0 {
return CopyLink(ctx, dst, path)
}
// if we got a file, we should copy it normally
if !d.IsDir() {
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 errors.Is(err, fs.ErrExist) && IsDirectory(dst) {
err = nil
}
return err
})
}

View file

@ -1,57 +0,0 @@
package fsx
import (
"errors"
"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 errors.Is(err, fs.ErrNotExist):
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)
}
}

View file

@ -1,25 +0,0 @@
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)
}

View file

@ -1,37 +0,0 @@
// Package fsx provides additional file system functionality.
//
// 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"
"sync"
"syscall"
)
// DefaultFilePerm should be used by callers to use a consistent file mode for new files.
const DefaultFilePerm fs.FileMode = 0666
// 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()
}

View file

@ -1,89 +0,0 @@
package fsx
import (
"errors"
"io/fs"
"os"
"path/filepath"
)
// SameFile checks if path1 and path2 refer to the same file.
// 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(path1, path2 string) bool {
// initial attempt: check if directly
same, certain := couldBeSameFile(path1, path2)
if certain {
return same
}
// second attempt: find the directory names and base paths
d1, n1 := filepath.Dir(path1), filepath.Base(path1)
d2, n2 := filepath.Dir(path2), filepath.Base(path2)
// if we have different file names (and they don't exist)
// we don't need to continue
if n1 != n2 {
return false
}
// compare the base names!
{
same, _ := couldBeSameFile(d1, d2)
return same
}
}
// couldBeSameFile checks if path1 might be the same as path2.
//
// 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(path1, path2 string) (same, authorative bool) {
{
// stat both files
info1, err1 := os.Stat(path1)
info2, err2 := os.Stat(path2)
// both files exist => check using env.SameFile
// the result is always authorative
if err1 == nil && err2 == nil {
same = os.SameFile(info1, info2)
authorative = true
return
}
// only 1 file errored => they could be different
if (err1 == nil) != (err2 == nil) {
return
}
// only 1 file does not exist => they could be different
if errors.Is(err1, fs.ErrNotExist) != errors.Is(err2, fs.ErrNotExist) {
return
}
}
{
// resolve paths absolutely
rpath1, err1 := filepath.Abs(path1)
rpath2, err2 := filepath.Abs(path2)
// if either path could not be resolved absolutely
// fallback to just using clean!
if err1 != nil {
rpath1 = filepath.Clean(path1)
}
if err2 != nil {
rpath2 = filepath.Clean(path2)
}
// compare using strings
same = rpath1 == rpath2
authorative = same // positive result is authorative!
return
}
}

View file

@ -1,31 +0,0 @@
// Package fsx provides convenient abstractions to work with the filesystem.
package fsx
import (
"io/fs"
"os"
)
// Exists checks if the given path exists
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(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(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(path string) bool {
info, err := os.Lstat(path)
return err == nil && info.Mode()&fs.ModeSymlink != 0
}

View file

@ -9,7 +9,7 @@ import (
"os"
"path/filepath"
"github.com/FAU-CDI/wisski-distillery/pkg/fsx"
"github.com/tkw1536/pkglib/fsx/umaskfree"
)
// Package packages the source directory into a 'tar.gz' file into destination.
@ -18,7 +18,7 @@ import (
// onCopy, when not nil, is called for each file being copied into the archive.
func Package(dst, src string, onCopy func(rel string, src string)) (count int64, err error) {
// create the target archive
archive, err := fsx.Create(dst, fsx.DefaultFilePerm)
archive, err := umaskfree.Create(dst, umaskfree.DefaultFilePerm)
if err != nil {
return 0, err
}

View file

@ -6,8 +6,8 @@ import (
"os"
"path/filepath"
"github.com/FAU-CDI/wisski-distillery/pkg/fsx"
"github.com/pkg/errors"
"github.com/tkw1536/pkglib/fsx/umaskfree"
)
var errExpectedFileButGotDirectory = errors.New("expected a file, but got a directory")
@ -84,7 +84,7 @@ func installDir(dst string, srcInfo fs.FileInfo, srcFile fs.ReadDirFile, src str
dstStat, dstErr := os.Stat(dst)
switch {
case errors.Is(dstErr, fs.ErrNotExist):
if err := fsx.MkdirAll(dst, srcInfo.Mode()); err != nil {
if err := umaskfree.MkdirAll(dst, srcInfo.Mode()); err != nil {
return errors.Wrapf(err, "Error creating destination directory %s", dst)
}
case dstErr != nil:
@ -120,7 +120,7 @@ func installDir(dst string, srcInfo fs.FileInfo, srcFile fs.ReadDirFile, src str
func installFile(dst string, srcInfo fs.FileInfo, src fs.File) error {
// create the file using the right mode!
file, err := fsx.Create(dst, srcInfo.Mode())
file, err := umaskfree.Create(dst, srcInfo.Mode())
if err != nil {
return err
}

View file

@ -8,7 +8,7 @@ import (
"io/fs"
"strings"
"github.com/FAU-CDI/wisski-distillery/pkg/fsx"
"github.com/tkw1536/pkglib/fsx/umaskfree"
"golang.org/x/exp/maps"
"golang.org/x/exp/slices"
)
@ -222,7 +222,7 @@ func InstallTemplate(dst string, context map[string]string, src string, fsys fs.
}
// open the destination file
file, err := fsx.Create(dst, srcInfo.Mode())
file, err := umaskfree.Create(dst, srcInfo.Mode())
if err != nil {
return err
}