remotefs

package
v2.0.0-alpha.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 7, 2024 License: Apache-2.0 Imports: 26 Imported by: 0

Documentation

Overview

Package remotefs provides fs.FS implementations for remote filesystems.

Index

Constants

View Source
const (
	OpClose    = "close"     // OpClose Close operation
	OpOpen     = "open"      // OpOpen Open operation
	OpRead     = "read"      // OpRead Read operation
	OpSeek     = "seek"      // OpSeek Seek operation
	OpStat     = "stat"      // OpStat Stat operation
	OpWrite    = "write"     // OpWrite Write operation
	OpCopyTo   = "copy-to"   // OpCopyTo CopyTo operation
	OpCopyFrom = "copy-from" // OpCopyFrom CopyFrom operation
)

Variables

View Source
var DefaultProvider = sync.OnceValue(NewProvider)

DefaultProvider is the default Repository for remote filesystem implementations.

View Source
var ErrChecksumMismatch = errors.New("checksum mismatch")

ErrChecksumMismatch is returned when the checksum of the uploaded file does not match the local checksum.

View Source
var (

	// ErrNotSupported is returned when a function is not supported on Windows.
	ErrNotSupported = errors.New("not supported on windows")
)

Functions

func PathError

func PathError(op, path string, err error) *fs.PathError

PathError returns a fs.PathError with the given operation, path and error.

func PathErrorf

func PathErrorf(op, path string, template string, args ...any) *fs.PathError

PathErrorf returns a fs.PathError with the given operation, path and error created using a sprintf style format string and arguments.

func Upload

func Upload(fs FS, src, dst string) error

Upload a file to the remote host.

Types

type Copier

type Copier interface {
	CopyFrom(src io.Reader) (int64, error)
	CopyTo(dst io.Writer) (int64, error)
}

Copier is a file-like struct that can copy data to and from io.Reader and io.Writer.

type FS

FS is a filesystem on the remote host.

func NewFS

func NewFS(c cmd.Runner) FS

NewFS returns a fs.FS compatible implementation for access to remote filesystems.

type File

type File interface {
	Name() string
	fs.File
	io.Seeker
	io.ReadCloser
	io.Writer
	Copier
}

File is a file in the remote filesystem.

func CreateTemp

func CreateTemp(fs FS, dir, pattern string) (File, error)

CreateTemp creates a new temporary file in the directory dir with a name built using the pattern, opens the file for reading and writing, and returns the resulting File. If dir is the empty string, CreateTemp uses the default directory for temporary files.

type FileInfo

type FileInfo struct {
	FName    string      `json:"name"`
	FSize    int64       `json:"size"`
	FMode    fs.FileMode `json:"mode"`
	FUnix    fs.FileMode `json:"unixMode"`
	FModTime time.Time   `json:"-"`
	FIsDir   bool        `json:"isDir"`
	ModtimeS int64       `json:"modTime"`
	// contains filtered or unexported fields
}

FileInfo implements fs.FileInfo for stat on remote files.

func (*FileInfo) FullPath

func (f *FileInfo) FullPath() string

FullPath returns the full path.

func (*FileInfo) Info

func (f *FileInfo) Info() (fs.FileInfo, error)

Info returns self. It's here to satisfy fs.DirEntry interface.

func (*FileInfo) IsDir

func (f *FileInfo) IsDir() bool

IsDir returns true if the file path points to a directory.

func (*FileInfo) ModTime

func (f *FileInfo) ModTime() time.Time

ModTime returns the last modification time of a file.

func (*FileInfo) Mode

func (f *FileInfo) Mode() fs.FileMode

Mode returns the file permission mode.

func (*FileInfo) Name

func (f *FileInfo) Name() string

Name returns the file name.

func (*FileInfo) Size

func (f *FileInfo) Size() int64

Size returns the file size.

func (*FileInfo) Sys

func (f *FileInfo) Sys() any

Sys returns the underlying data source.

func (*FileInfo) Type

func (f *FileInfo) Type() fs.FileMode

Type returns the file type. It's here to satisfy fs.DirEntry interface.

func (*FileInfo) UnmarshalJSON

func (f *FileInfo) UnmarshalJSON(b []byte) error

UnmarshalJSON implements json.Unmarshaler.

type OS

type OS interface {
	Remove(path string) error
	RemoveAll(path string) error
	Mkdir(path string, perm fs.FileMode) error
	MkdirAll(path string, perm fs.FileMode) error
	MkdirTemp(dir, prefix string) (string, error)
	WriteFile(path string, data []byte, perm fs.FileMode) error
	FileExist(path string) bool
	LookPath(cmd string) (string, error)
	Join(elem ...string) string
	Chmod(path string, mode fs.FileMode) error
	Chown(path string, uid, gid int) error
	Chtimes(path string, atime, mtime int64) error
	Touch(path string) error
	Truncate(path string, size int64) error
	Getenv(key string) string
	Rename(oldpath, newpath string) error
	Hostname() (string, error)
	LongHostname() (string, error)
	TempDir() string
	UserCacheDir() string
	UserConfigDir() string
	UserHomeDir() string
}

OS is a os/filesystem utility interface, these operations are modeled after stdlib's OS package.

type Opener

type Opener interface {
	OpenFile(path string, flag int, perm fs.FileMode) (File, error)
}

Opener is a file opener interface, modeled after stdlib's OS package.

type PosixDir

type PosixDir struct {
	PosixFile
	// contains filtered or unexported fields
}

PosixDir implements fs.ReadDirFile for a remote directory.

func (*PosixDir) Name

func (w *PosixDir) Name() string

func (*PosixDir) ReadDir

func (f *PosixDir) ReadDir(n int) ([]fs.DirEntry, error)

ReadDir returns a list of directory entries.

type PosixFS

type PosixFS struct {
	cmd.Runner
	log.LoggerInjectable
	// contains filtered or unexported fields
}

PosixFS implements fs.FS for a remote filesystem that uses POSIX commands for access.

func NewPosixFS

func NewPosixFS(conn cmd.Runner) *PosixFS

NewPosixFS returns a fs.FS implementation for a remote filesystem that uses POSIX commands for access.

func (*PosixFS) Chmod

func (s *PosixFS) Chmod(name string, mode fs.FileMode) error

Chmod changes the mode of the named file to mode.

func (*PosixFS) Chown

func (s *PosixFS) Chown(name string, uid, gid int) error

Chown changes the numeric uid and gid of the named file.

func (*PosixFS) Chtimes

func (s *PosixFS) Chtimes(name string, atime, mtime int64) error

Chtimes changes the access and modification times of the named file.

func (*PosixFS) FileExist

func (s *PosixFS) FileExist(name string) bool

FileExist checks if a file exists on the host.

func (*PosixFS) Getenv

func (s *PosixFS) Getenv(key string) string

Getenv returns the value of the environment variable named by the key.

func (*PosixFS) Hostname

func (s *PosixFS) Hostname() (string, error)

Hostname returns the name of the host.

func (*PosixFS) Join

func (s *PosixFS) Join(elem ...string) string

Join joins any number of path elements into a single path, adding a separating slash if necessary.

func (*PosixFS) LongHostname

func (s *PosixFS) LongHostname() (string, error)

LongHostname returns the FQDN of the host.

func (*PosixFS) LookPath

func (s *PosixFS) LookPath(name string) (string, error)

LookPath checks if a command exists on the host.

func (*PosixFS) Mkdir

func (s *PosixFS) Mkdir(name string, perm fs.FileMode) error

Mkdir creates a new directory with the specified name and permission bits.

func (*PosixFS) MkdirAll

func (s *PosixFS) MkdirAll(name string, perm fs.FileMode) error

MkdirAll creates a new directory structure with the specified name and permission bits. If the directory already exists, MkDirAll does nothing and returns nil.

func (*PosixFS) MkdirTemp

func (s *PosixFS) MkdirTemp(dir, prefix string) (string, error)

MkdirTemp creates a new temporary directory in the directory dir with a name beginning with prefix and returns the path of the new directory.

func (*PosixFS) Open

func (s *PosixFS) Open(name string) (fs.File, error)

Open opens the named file for reading.

func (*PosixFS) OpenFile

func (s *PosixFS) OpenFile(name string, flags int, perm fs.FileMode) (File, error)

OpenFile is used to open a file with access/creation flags for reading or writing. For info on flags, see https://pkg.go.dev/os#pkg-constants

func (*PosixFS) ReadDir

func (s *PosixFS) ReadDir(name string) ([]fs.DirEntry, error)

ReadDir reads the directory named by dirname and returns a list of directory entries.

func (*PosixFS) ReadFile

func (s *PosixFS) ReadFile(filename string) ([]byte, error)

ReadFile reads the file named by filename and returns the contents.

func (*PosixFS) Remove

func (s *PosixFS) Remove(name string) error

Remove deletes the named file or (empty) directory.

func (*PosixFS) RemoveAll

func (s *PosixFS) RemoveAll(name string) error

RemoveAll removes path and any children it contains.

func (*PosixFS) Rename

func (s *PosixFS) Rename(oldpath, newpath string) error

Rename renames (moves) oldpath to newpath.

func (*PosixFS) Sha256

func (s *PosixFS) Sha256(name string) (string, error)

Sha256 returns the sha256 checksum of the file at path.

func (*PosixFS) Stat

func (s *PosixFS) Stat(name string) (fs.FileInfo, error)

Stat returns the FileInfo structure describing file.

func (*PosixFS) TempDir

func (s *PosixFS) TempDir() string

TempDir returns the default directory to use for temporary files.

func (*PosixFS) Touch

func (s *PosixFS) Touch(name string) error

Touch creates a new empty file at path or updates the timestamp of an existing file to the current time.

func (*PosixFS) Truncate

func (s *PosixFS) Truncate(name string, size int64) error

Truncate changes the size of the named file or creates a new file if it doesn't exist.

func (*PosixFS) UserCacheDir

func (s *PosixFS) UserCacheDir() string

UserCacheDir returns the default root directory to use for user-specific cached data.

func (*PosixFS) UserConfigDir

func (s *PosixFS) UserConfigDir() string

UserConfigDir returns the default root directory to use for user-specific configuration data.

func (*PosixFS) UserHomeDir

func (s *PosixFS) UserHomeDir() string

UserHomeDir returns the current user's home directory.

func (*PosixFS) WriteFile

func (s *PosixFS) WriteFile(filename string, data []byte, perm fs.FileMode) error

WriteFile writes data to a file named by filename.

type PosixFile

type PosixFile struct {
	// contains filtered or unexported fields
}

PosixFile implements fs.File for a remote file.

func (*PosixFile) Close

func (f *PosixFile) Close() error

Close closes the file, rendering it unusable for I/O. It returns an error, if any.

func (*PosixFile) CopyFrom

func (f *PosixFile) CopyFrom(src io.Reader) (int64, error)

CopyFrom copies the local reader src to the remote file.

func (*PosixFile) CopyTo

func (f *PosixFile) CopyTo(dst io.Writer) (int64, error)

CopyTo copies the remote file to the writer dst.

func (*PosixFile) Name

func (w *PosixFile) Name() string

func (*PosixFile) Read

func (f *PosixFile) Read(p []byte) (int, error)

Read reads up to len(p) bytes into p. It returns the number of bytes read (0 <= n <= len(p)) and any error encountered.

func (*PosixFile) Seek

func (f *PosixFile) Seek(offset int64, whence int) (int64, error)

Seek sets the offset for the next Read or Write to offset, interpreted according to whence: io.SeekStart means relative to the origin of the file, io.SeekCurrent means relative to the current offset, and io.SeekEnd means relative to the end. Seek returns the new offset relative to the start of the file and an error, if any.

func (*PosixFile) Stat

func (f *PosixFile) Stat() (fs.FileInfo, error)

Stat returns a FileInfo describing the named file.

func (*PosixFile) Write

func (f *PosixFile) Write(p []byte) (int, error)

type Provider

type Provider struct{}

Provider is a factory for remote filesystem implementations.

func NewProvider

func NewProvider() *Provider

NewProvider returns a new Repository.

func (*Provider) Get

func (r *Provider) Get(c cmd.Runner) (FS, error)

Get returns Windows or Unix FS depending on the remote OS. Currently it never returns an error.

type RemoteFSProvider

type RemoteFSProvider interface {
	Get(runner cmd.Runner) (FS, error)
}

RemoteFSProvider is a factory for remote filesystem implementations.

type Service

type Service struct {
	// contains filtered or unexported fields
}

Service provides a unified interface to interact with the filesystem on a remote host. It ensures that a suitable remotefs.FS implementation is lazily initialized and made available for filesystem operations. It supports operations like opening files and implements io/fs.FS.

func NewRemoteFSService

func NewRemoteFSService(provider RemoteFSProvider, runner cmd.Runner) *Service

NewRemoteFSService creates a new instance of Service with the provided remotefs Provider and runner.

func (*Service) FS

func (p *Service) FS() FS

FS provides easy access to the remote filesystem instance. It initializes the filesystem implementation if it has not been initialized yet. If the initialization fails, a FS implementation that errors out on all operations will be returned instead.

func (*Service) GetFS

func (p *Service) GetFS() (FS, error)

GetFS returns a FS or an error if a filesystem client could not be initialized.

type Sha256summer

type Sha256summer interface {
	Sha256(path string) (string, error)
}

Sha256summer implementing struct can calculate sha256 checksum of a file.

type WinFS

type WinFS struct {
	cmd.Runner
	log.LoggerInjectable
}

WinFS is a fs.FS implemen{.

func NewWindowsFS

func NewWindowsFS(conn cmd.Runner) *WinFS

NewWindowsFS returns a new fs.FS implementing filesystem for Windows targets.

func (*WinFS) Chmod

func (s *WinFS) Chmod(name string, mode fs.FileMode) error

Chmod changes the mode of the named file to mode. On Windows, only the 0200 bit (owner writable) of mode is used; it controls whether the file's read-only attribute is set or cleared.

func (*WinFS) Chown

func (s *WinFS) Chown(name string, _, _ int) error

Chown changes the numeric uid and gid of the named file. On windows it returns an error.

func (*WinFS) Chtimes

func (s *WinFS) Chtimes(name string, atime, mtime int64) error

Chtimes changes the access and modification times of the named file, similar to the Unix utime() or utimes() functions.

func (*WinFS) FileExist

func (s *WinFS) FileExist(name string) bool

FileExist checks if a file exists on the host. It is a more efficient shortcut for something like:

	if _, err := fs.Stat(name); os.IsNotExist(err) { ... }
 if !fs.FileExist(name) { ... }

func (*WinFS) Getenv

func (s *WinFS) Getenv(key string) string

Getenv retrieves the value of the environment variable named by the key.

func (*WinFS) Hostname

func (s *WinFS) Hostname() (string, error)

Hostname returns the hostname of the remote host.

func (*WinFS) Join

func (s *WinFS) Join(elem ...string) string

Join joins any number of path elements into a single path, adding a separating slash if necessary.

func (*WinFS) LongHostname

func (s *WinFS) LongHostname() (string, error)

LongHostname resolves the FQDN (long) hostname.

func (*WinFS) LookPath

func (s *WinFS) LookPath(name string) (string, error)

LookPath checks if a command exists on the host.

func (*WinFS) Mkdir

func (s *WinFS) Mkdir(name string, _ fs.FileMode) error

Mkdir creates a new directory with the specified name and permission bits. The permission bits are ignored on Windows.

func (*WinFS) MkdirAll

func (s *WinFS) MkdirAll(name string, _ fs.FileMode) error

MkdirAll creates a directory named path, along with any necessary parents. The permission bits are ignored on Windows.

func (*WinFS) MkdirTemp

func (s *WinFS) MkdirTemp(dir, prefix string) (string, error)

MkdirTemp creates a new temporary directory in the directory dir with a name beginning with prefix and returns the path of the new directory.

func (*WinFS) Open

func (s *WinFS) Open(name string) (fs.File, error)

Open opens the named file for reading and returns fs.File. Use OpenFile to get a file that can be written to or if you need any of the methods not available on fs.File interface without type assertion.

func (*WinFS) OpenFile

func (s *WinFS) OpenFile(name string, flags int, _ fs.FileMode) (File, error)

OpenFile opens the named remote file with the specified flags. os.O_EXCL and permission bits are ignored on Windows. For a description of the flags, see https://pkg.go.dev/os#pkg-constants

func (*WinFS) ReadDir

func (s *WinFS) ReadDir(name string) ([]fs.DirEntry, error)

ReadDir reads the directory named by dirname and returns a list of directory entries.

func (*WinFS) ReadFile

func (s *WinFS) ReadFile(name string) ([]byte, error)

ReadFile reads the named file and returns its contents.

func (*WinFS) Remove

func (s *WinFS) Remove(name string) error

Remove deletes the named file or (empty) directory.

func (*WinFS) RemoveAll

func (s *WinFS) RemoveAll(name string) error

RemoveAll deletes the named file or directory and all its child items.

func (*WinFS) Rename

func (s *WinFS) Rename(oldpath, newpath string) error

Rename renames (moves) oldpath to newpath.

func (*WinFS) Sha256

func (s *WinFS) Sha256(name string) (string, error)

Sha256 returns the SHA256 hash of the remote file.

func (*WinFS) Stat

func (s *WinFS) Stat(name string) (fs.FileInfo, error)

Stat returns fs.FileInfo for the remote file.

func (*WinFS) TempDir

func (s *WinFS) TempDir() string

TempDir returns the default directory to use for temporary files.

func (*WinFS) Touch

func (s *WinFS) Touch(name string) error

Touch creates a new file with the given name if it does not exist. If the file exists, the access and modification times are set to the current time.

func (*WinFS) Truncate

func (s *WinFS) Truncate(name string, size int64) error

Truncate changes the size of the named file.

func (*WinFS) UserCacheDir

func (s *WinFS) UserCacheDir() string

UserCacheDir returns the default root directory to use for user-specific non-essential data files.

func (*WinFS) UserConfigDir

func (s *WinFS) UserConfigDir() string

UserConfigDir returns the default root directory to use for user-specific configuration data.

func (*WinFS) UserHomeDir

func (s *WinFS) UserHomeDir() string

UserHomeDir returns the current user's home directory.

func (*WinFS) WriteFile

func (s *WinFS) WriteFile(name string, data []byte, mode fs.FileMode) error

WriteFile writes data to the named file, creating it if necessary.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL