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:
parent
39207a1cb5
commit
45540ab253
20 changed files with 52 additions and 120 deletions
|
|
@ -3,7 +3,6 @@ package environment
|
|||
import (
|
||||
"io"
|
||||
"io/fs"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Environment represents an environment that a program can run it.
|
||||
|
|
@ -11,31 +10,9 @@ import (
|
|||
type Environment interface {
|
||||
isEnv()
|
||||
|
||||
GetEnv(name string) string
|
||||
|
||||
Stat(path string) (fs.FileInfo, error)
|
||||
Lstat(path string) (fs.FileInfo, error)
|
||||
|
||||
Readlink(path string) (string, error)
|
||||
Symlink(oldname, newname string) error
|
||||
|
||||
ReadDir(name string) ([]fs.DirEntry, error)
|
||||
|
||||
Open(path string) (fs.File, error)
|
||||
Chtimes(name string, atime time.Time, mtime time.Time) error
|
||||
SameFile(f1, f2 fs.FileInfo) bool
|
||||
|
||||
Create(path string, mode fs.FileMode) (WritableFile, error)
|
||||
|
||||
Mkdir(path string, mode fs.FileMode) error
|
||||
MkdirAll(path string, mode fs.FileMode) error
|
||||
|
||||
Remove(path string) error
|
||||
RemoveAll(path string) error
|
||||
|
||||
WalkDir(root string, fn fs.WalkDirFunc) error
|
||||
|
||||
Abs(path string) (string, error)
|
||||
}
|
||||
|
||||
type WritableFile interface {
|
||||
|
|
|
|||
|
|
@ -8,15 +8,6 @@ import (
|
|||
"github.com/tkw1536/pkglib/pools"
|
||||
)
|
||||
|
||||
// ExecCommandError is returned by Exec when a command could not be executed.
|
||||
// This typically hints that the executable cannot be found, but may have other causes.
|
||||
const ExecCommandError = 127
|
||||
|
||||
// ExecCommandErrorFunc always returns ExecCommandError.
|
||||
func ExecCommandErrorFunc() int {
|
||||
return ExecCommandError
|
||||
}
|
||||
|
||||
// DefaultFilePerm is the default mode to use for files
|
||||
const DefaultFilePerm fs.FileMode = 0666
|
||||
|
||||
|
|
@ -51,7 +42,7 @@ func WriteFile(env Environment, path string, data []byte, mode fs.FileMode) erro
|
|||
// ReadFile is like [os.ReadFile]
|
||||
func ReadFile(env Environment, path string) ([]byte, error) {
|
||||
// open the file!
|
||||
file, err := env.Open(path)
|
||||
file, err := os.Open(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,7 +3,6 @@ package environment
|
|||
import (
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
"syscall"
|
||||
"time"
|
||||
|
|
@ -26,46 +25,6 @@ func (n *Native) resetMask() {
|
|||
n.ulock.Unlock()
|
||||
}
|
||||
|
||||
func (*Native) GetEnv(name string) string {
|
||||
return os.Getenv(name)
|
||||
}
|
||||
|
||||
func (*Native) Stat(path string) (fs.FileInfo, error) {
|
||||
return os.Stat(path)
|
||||
}
|
||||
|
||||
func (*Native) Lstat(path string) (fs.FileInfo, error) {
|
||||
return os.Lstat(path)
|
||||
}
|
||||
|
||||
func (*Native) Readlink(path string) (string, error) {
|
||||
return os.Readlink(path)
|
||||
}
|
||||
|
||||
func (*Native) Symlink(oldname, newname string) error {
|
||||
return os.Symlink(oldname, newname)
|
||||
}
|
||||
|
||||
func (*Native) ReadDir(name string) ([]fs.DirEntry, error) {
|
||||
return os.ReadDir(name)
|
||||
}
|
||||
|
||||
func (*Native) SameFile(f1, f2 fs.FileInfo) bool {
|
||||
return os.SameFile(f1, f2)
|
||||
}
|
||||
|
||||
func (*Native) WalkDir(path string, f fs.WalkDirFunc) error {
|
||||
return filepath.WalkDir(path, f)
|
||||
}
|
||||
|
||||
func (*Native) Executable() (string, error) {
|
||||
return os.Executable() // TODO: not sure this works with the remote concepts
|
||||
}
|
||||
|
||||
func (*Native) Open(path string) (fs.File, error) {
|
||||
return os.Open(path)
|
||||
}
|
||||
|
||||
func (n *Native) Create(path string, mode fs.FileMode) (WritableFile, error) {
|
||||
n.ulock.Lock()
|
||||
defer n.ulock.Unlock()
|
||||
|
|
@ -90,15 +49,3 @@ func (n *Native) MkdirAll(path string, mode fs.FileMode) error {
|
|||
|
||||
return os.MkdirAll(path, fs.ModeDir|mode)
|
||||
}
|
||||
|
||||
func (*Native) Remove(path string) error {
|
||||
return os.Remove(path)
|
||||
}
|
||||
|
||||
func (*Native) RemoveAll(path string) error {
|
||||
return os.RemoveAll(path)
|
||||
}
|
||||
|
||||
func (*Native) Abs(path string) (string, error) {
|
||||
return filepath.Abs(path)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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!
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ import (
|
|||
"compress/gzip"
|
||||
"io"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/FAU-CDI/wisski-distillery/pkg/environment"
|
||||
|
|
@ -32,7 +33,7 @@ func Package(env environment.Environment, dst, src string, onCopy func(rel strin
|
|||
defer tarHandle.Close()
|
||||
|
||||
// and walk through it!
|
||||
err = env.WalkDir(src, func(path string, entry fs.DirEntry, err error) error {
|
||||
err = filepath.WalkDir(src, func(path string, entry fs.DirEntry, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
@ -75,7 +76,7 @@ func Package(env environment.Environment, dst, src string, onCopy func(rel strin
|
|||
}
|
||||
|
||||
// open the file
|
||||
handle, err := env.Open(path)
|
||||
handle, err := os.Open(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ package unpack
|
|||
import (
|
||||
"io"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/FAU-CDI/wisski-distillery/pkg/environment"
|
||||
|
|
@ -80,7 +81,7 @@ func installResource(env environment.Environment, dst string, src string, fsys f
|
|||
|
||||
func installDir(env environment.Environment, dst string, srcInfo fs.FileInfo, srcFile fs.ReadDirFile, src string, fsys fs.FS, onInstallFile func(dst, src string)) error {
|
||||
// create the destination
|
||||
dstStat, dstErr := env.Stat(dst)
|
||||
dstStat, dstErr := os.Stat(dst)
|
||||
switch {
|
||||
case environment.IsNotExist(dstErr):
|
||||
if err := env.MkdirAll(dst, srcInfo.Mode()); err != nil {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue