Add 'environment' package
This commit adds a new environment package that manages all calls to the underlying operating system.
This commit is contained in:
parent
822c70cd69
commit
f19619ef9f
60 changed files with 539 additions and 308 deletions
|
|
@ -4,8 +4,9 @@ import (
|
|||
"errors"
|
||||
"io"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/FAU-CDI/wisski-distillery/pkg/environment"
|
||||
)
|
||||
|
||||
var ErrCopySameFile = errors.New("src and dst must be different")
|
||||
|
|
@ -14,13 +15,13 @@ var ErrCopySameFile = errors.New("src and dst must be different")
|
|||
// When src points to a symbolic link, will copy the symbolic link.
|
||||
//
|
||||
// When dst and src are the same file, returns ErrCopySameFile.
|
||||
func CopyFile(dst, src string) error {
|
||||
if SameFile(src, dst) {
|
||||
func CopyFile(env environment.Environment, dst, src string) error {
|
||||
if SameFile(env, src, dst) {
|
||||
return ErrCopySameFile
|
||||
}
|
||||
|
||||
// open the source
|
||||
srcFile, err := os.Open(src)
|
||||
srcFile, err := env.Open(src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
@ -33,7 +34,7 @@ func CopyFile(dst, src string) error {
|
|||
}
|
||||
|
||||
// open or create the destination
|
||||
dstFile, err := os.OpenFile(dst, os.O_WRONLY|os.O_CREATE, srcStat.Mode())
|
||||
dstFile, err := env.Create(dst, srcStat.Mode())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
@ -46,27 +47,27 @@ func CopyFile(dst, src string) error {
|
|||
|
||||
// CopyLink copies a link from src to dst.
|
||||
// If dst already exists, it is deleted and then re-created.
|
||||
func CopyLink(dst, src string) error {
|
||||
func CopyLink(env environment.Environment, dst, src string) error {
|
||||
// if they're the same file that is an error
|
||||
if SameFile(dst, src) {
|
||||
if SameFile(env, dst, src) {
|
||||
return ErrCopySameFile
|
||||
}
|
||||
|
||||
// read the link target
|
||||
target, err := os.Readlink(src)
|
||||
target, err := env.Readlink(src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// delete it if it already exists
|
||||
if Exists(dst) {
|
||||
if err := os.Remove(dst); err != nil {
|
||||
if Exists(env, dst) {
|
||||
if err := env.Remove(dst); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// make the symbolic link!
|
||||
return os.Symlink(target, dst)
|
||||
return env.Symlink(target, dst)
|
||||
}
|
||||
|
||||
var ErrDstFile = errors.New("dst is a file")
|
||||
|
|
@ -77,16 +78,16 @@ 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(dst, src string, onCopy func(dst, src string)) error {
|
||||
func CopyDirectory(env environment.Environment, dst, src string, onCopy func(dst, src string)) error {
|
||||
// sanity checks
|
||||
if SameFile(src, dst) {
|
||||
if SameFile(env, src, dst) {
|
||||
return ErrCopySameFile
|
||||
}
|
||||
if IsFile(dst) {
|
||||
if IsFile(env, dst) {
|
||||
return ErrDstFile
|
||||
}
|
||||
|
||||
return filepath.WalkDir(src, func(path string, d fs.DirEntry, err error) error {
|
||||
return env.WalkDir(src, func(path string, d fs.DirEntry, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
@ -111,19 +112,19 @@ func CopyDirectory(dst, src string, onCopy func(dst, src string)) error {
|
|||
}
|
||||
|
||||
// if we have a symbolic link, copy the link!
|
||||
if info.Mode()&os.ModeSymlink != 0 {
|
||||
return CopyLink(dst, path)
|
||||
if info.Mode()&fs.ModeSymlink != 0 {
|
||||
return CopyLink(env, dst, path)
|
||||
}
|
||||
|
||||
// if we got a file, we should copy it normally
|
||||
if !d.IsDir() {
|
||||
return CopyFile(dst, path)
|
||||
return CopyFile(env, 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 = os.Mkdir(dst, info.Mode())
|
||||
if os.IsExist(err) && IsDirectory(dst) {
|
||||
err = env.Mkdir(dst, info.Mode())
|
||||
if environment.IsExist(err) && IsDirectory(env, dst) {
|
||||
err = nil
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,17 +1,18 @@
|
|||
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 [os.SameFile].
|
||||
// If both files exist, they are compared using [env.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 {
|
||||
func SameFile(env environment.Environment, path1, path2 string) bool {
|
||||
|
||||
// initial attempt: check if directly
|
||||
same, certain := couldBeSameFile(path1, path2)
|
||||
same, certain := couldBeSameFile(env, path1, path2)
|
||||
if certain {
|
||||
return same
|
||||
}
|
||||
|
|
@ -28,28 +29,28 @@ func SameFile(path1, path2 string) bool {
|
|||
|
||||
// compare the base names!
|
||||
{
|
||||
same, _ := couldBeSameFile(d1, d2)
|
||||
same, _ := couldBeSameFile(env, d1, d2)
|
||||
return same
|
||||
}
|
||||
}
|
||||
|
||||
// couldBeSameFile checks if path1 might be the same as path2.
|
||||
//
|
||||
// If both files exist, compares using [os.SameFile].
|
||||
// If both files exist, compares using [env.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) {
|
||||
func couldBeSameFile(env environment.Environment, path1, path2 string) (same, authorative bool) {
|
||||
{
|
||||
// stat both files
|
||||
info1, err1 := os.Stat(path1)
|
||||
info2, err2 := os.Stat(path2)
|
||||
info1, err1 := env.Stat(path1)
|
||||
info2, err2 := env.Stat(path2)
|
||||
|
||||
// both files exist => check using os.SameFile
|
||||
// both files exist => check using env.SameFile
|
||||
// the result is always authorative
|
||||
if err1 == nil && err2 == nil {
|
||||
same = os.SameFile(info1, info2)
|
||||
same = env.SameFile(info1, info2)
|
||||
authorative = true
|
||||
return
|
||||
}
|
||||
|
|
@ -60,15 +61,15 @@ func couldBeSameFile(path1, path2 string) (same, authorative bool) {
|
|||
}
|
||||
|
||||
// only 1 file does not exist => they could be different
|
||||
if os.IsNotExist(err1) != os.IsNotExist(err2) {
|
||||
if environment.IsNotExist(err1) != environment.IsNotExist(err2) {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
// resolve paths absolutely
|
||||
rpath1, err1 := filepath.Abs(path1)
|
||||
rpath2, err2 := filepath.Abs(path2)
|
||||
rpath1, err1 := env.Abs(path1)
|
||||
rpath2, err2 := env.Abs(path2)
|
||||
|
||||
// if either path could not be resolved absolutely
|
||||
// fallback to just using clean!
|
||||
|
|
|
|||
|
|
@ -1,20 +1,21 @@
|
|||
package fsx
|
||||
|
||||
import (
|
||||
"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 [os.Create].
|
||||
// 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(path string) error {
|
||||
_, err := os.Stat(path)
|
||||
func Touch(env environment.Environment, path string) error {
|
||||
_, err := env.Stat(path)
|
||||
switch {
|
||||
case os.IsNotExist(err):
|
||||
f, err := os.Create(path)
|
||||
case environment.IsNotExist(err):
|
||||
f, err := env.Create(path, environment.DefaultFilePerm)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
@ -24,6 +25,6 @@ func Touch(path string) error {
|
|||
return err
|
||||
default:
|
||||
now := time.Now().Local()
|
||||
return os.Chtimes(path, now, now)
|
||||
return env.Chtimes(path, now, now)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,29 +2,31 @@
|
|||
package fsx
|
||||
|
||||
import (
|
||||
"os"
|
||||
"io/fs"
|
||||
|
||||
"github.com/FAU-CDI/wisski-distillery/pkg/environment"
|
||||
)
|
||||
|
||||
// Exists checks if the given path exists
|
||||
func Exists(path string) bool {
|
||||
_, err := os.Lstat(path)
|
||||
func Exists(env environment.Environment, path string) bool {
|
||||
_, err := env.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)
|
||||
func IsDirectory(env environment.Environment, path string) bool {
|
||||
info, err := env.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)
|
||||
func IsFile(env environment.Environment, path string) bool {
|
||||
info, err := env.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()&os.ModeSymlink != 0
|
||||
func IsLink(env environment.Environment, path string) bool {
|
||||
info, err := env.Lstat(path)
|
||||
return err == nil && info.Mode()&fs.ModeSymlink != 0
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue