pkg/environment: Remove some file-based functions

This commit removes certain file-based functions from 'pkg/environment',
continuing the migration to entirely remove the package.
This commit is contained in:
Tom Wiesing 2023-03-02 11:15:15 +01:00
parent 39207a1cb5
commit 45540ab253
No known key found for this signature in database
20 changed files with 52 additions and 120 deletions

View file

@ -4,6 +4,7 @@ import (
"context"
"errors"
"io/fs"
"os"
"path/filepath"
"github.com/FAU-CDI/wisski-distillery/pkg/environment"
@ -27,7 +28,7 @@ func CopyFile(ctx context.Context, env environment.Environment, dst, src string)
}
// open the source
srcFile, err := env.Open(src)
srcFile, err := os.Open(src)
if err != nil {
return err
}
@ -64,20 +65,20 @@ func CopyLink(ctx context.Context, env environment.Environment, dst, src string)
}
// read the link target
target, err := env.Readlink(src)
target, err := os.Readlink(src)
if err != nil {
return err
}
// delete it if it already exists
if Exists(env, dst) {
if err := env.Remove(dst); err != nil {
if err := os.Remove(dst); err != nil {
return err
}
}
// make the symbolic link!
return env.Symlink(target, dst)
return os.Symlink(target, dst)
}
var ErrDstFile = errors.New("dst is a file")
@ -98,7 +99,7 @@ func CopyDirectory(ctx context.Context, env environment.Environment, dst, src st
return ErrDstFile
}
return env.WalkDir(src, func(path string, d fs.DirEntry, err error) error {
return filepath.WalkDir(src, func(path string, d fs.DirEntry, err error) error {
// someone previously returned an error
if err != nil {
return err

View file

@ -1,6 +1,7 @@
package fsx
import (
"os"
"path/filepath"
"github.com/FAU-CDI/wisski-distillery/pkg/environment"
@ -44,13 +45,13 @@ func SameFile(env environment.Environment, path1, path2 string) bool {
func couldBeSameFile(env environment.Environment, path1, path2 string) (same, authorative bool) {
{
// stat both files
info1, err1 := env.Stat(path1)
info2, err2 := env.Stat(path2)
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 = env.SameFile(info1, info2)
same = os.SameFile(info1, info2)
authorative = true
return
}
@ -68,8 +69,8 @@ func couldBeSameFile(env environment.Environment, path1, path2 string) (same, au
{
// resolve paths absolutely
rpath1, err1 := env.Abs(path1)
rpath2, err2 := env.Abs(path2)
rpath1, err1 := filepath.Abs(path1)
rpath2, err2 := filepath.Abs(path2)
// if either path could not be resolved absolutely
// fallback to just using clean!

View file

@ -2,6 +2,7 @@ package fsx
import (
"io/fs"
"os"
"time"
"github.com/FAU-CDI/wisski-distillery/pkg/environment"
@ -16,7 +17,7 @@ func Touch(env environment.Environment, path string, perm fs.FileMode) error {
if perm == 0 {
perm = environment.DefaultFilePerm
}
_, err := env.Stat(path)
_, err := os.Stat(path)
switch {
case environment.IsNotExist(err):
f, err := env.Create(path, perm)
@ -29,6 +30,6 @@ func Touch(env environment.Environment, path string, perm fs.FileMode) error {
return err
default:
now := time.Now().Local()
return env.Chtimes(path, now, now)
return os.Chtimes(path, now, now)
}
}

View file

@ -3,30 +3,31 @@ 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 {
_, err := env.Lstat(path)
_, 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 {
info, err := env.Stat(path)
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 {
info, err := env.Stat(path)
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 {
info, err := env.Lstat(path)
info, err := os.Lstat(path)
return err == nil && info.Mode()&fs.ModeSymlink != 0
}