Documentation ¶
Index ¶
- func ExtractNameFromArchive(f archiver.File) string
- func IsErrorCode(err error, code ErrorCode) bool
- func IsFilesystemError(err error) bool
- func NewBadPathResolution(path string, resolved string) error
- type Archive
- type Error
- type ErrorCode
- type Filesystem
- func (fs *Filesystem) CachedUsage() int64
- func (fs *Filesystem) Chmod(path string, mode os.FileMode) error
- func (fs *Filesystem) Chown(path string) error
- func (fs *Filesystem) Chtimes(path string, atime, mtime time.Time) error
- func (fs *Filesystem) CompressFiles(dir string, paths []string) (os.FileInfo, error)
- func (fs *Filesystem) Copy(p string) error
- func (fs *Filesystem) CreateDirectory(name string, p string) error
- func (fs *Filesystem) DecompressFile(ctx context.Context, dir string, file string) error
- func (fs *Filesystem) DecompressFileUnsafe(ctx context.Context, dir string, file string) error
- func (fs *Filesystem) Delete(p string) error
- func (fs *Filesystem) DirectorySize(dir string) (int64, error)
- func (fs *Filesystem) DiskUsage(allowStaleValue bool) (int64, error)
- func (fs *Filesystem) ExtractStreamUnsafe(ctx context.Context, dir string, r io.Reader) error
- func (fs *Filesystem) File(p string) (*os.File, Stat, error)
- func (fs *Filesystem) HasSpaceAvailable(allowStaleValue bool) bool
- func (fs *Filesystem) HasSpaceErr(allowStaleValue bool) error
- func (fs *Filesystem) HasSpaceFor(size int64) error
- func (fs *Filesystem) IsIgnored(paths ...string) error
- func (fs *Filesystem) ListDirectory(p string) ([]Stat, error)
- func (fs *Filesystem) MaxDisk() int64
- func (fs *Filesystem) ParallelSafePath(paths []string) ([]string, error)
- func (fs *Filesystem) Path() string
- func (fs *Filesystem) Rename(from string, to string) error
- func (fs *Filesystem) SafePath(p string) (string, error)
- func (fs *Filesystem) SetDiskLimit(i int64)
- func (fs *Filesystem) SpaceAvailableForDecompression(ctx context.Context, dir string, file string) error
- func (fs *Filesystem) Stat(p string) (Stat, error)
- func (fs *Filesystem) Touch(p string, flag int) (*os.File, error)
- func (fs *Filesystem) TruncateRootDirectory() error
- func (fs *Filesystem) Writefile(p string, r io.Reader) error
- type SpaceCheckingOpts
- type Stat
- type TarProgress
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ExtractNameFromArchive ¶ added in v1.4.1
func ExtractNameFromArchive(f archiver.File) string
ExtractNameFromArchive looks at an archive file to try and determine the name for a given element in an archive. Because of... who knows why, each file type uses different methods to determine the file name.
If there is a archiver.File#Sys() value present we will try to use the name present in there, otherwise falling back to archiver.File#Name() if all else fails. Without this logic present, some archive types such as zip/tars/etc. will write all of the files to the base directory, rather than the nested directory that is expected.
For files like ".rar" types, there is no f.Sys() value present, and the value of archiver.File#Name() will be what you need.
func IsErrorCode ¶ added in v1.2.0
IsErrorCode checks if "err" is a filesystem Error type. If so, it will then drop in and check that the error code is the same as the provided ErrorCode passed in "code".
func IsFilesystemError ¶ added in v1.2.0
IsFilesystemError checks if the given error is one of the Filesystem errors.
func NewBadPathResolution ¶ added in v1.1.0
NewBadPathResolution returns a new BadPathResolution error.
Types ¶
type Archive ¶ added in v1.2.0
type Archive struct { // BasePath is the absolute path to create the archive from where Files and Ignore are // relative to. BasePath string // Ignore is a gitignore string (most likely read from a file) of files to ignore // from the archive. Ignore string // Files specifies the files to archive, this takes priority over the Ignore option, if // unspecified, all files in the BasePath will be archived unless Ignore is set. Files []string // Progress wraps the writer of the archive to pass through the progress tracker. Progress *progress.Progress }
type Error ¶ added in v1.2.0
type Error struct {
// contains filtered or unexported fields
}
type ErrorCode ¶ added in v1.2.0
type ErrorCode string
const ( ErrCodeIsDirectory ErrorCode = "E_ISDIR" ErrCodeDiskSpace ErrorCode = "E_NODISK" ErrCodeUnknownArchive ErrorCode = "E_UNKNFMT" ErrCodePathResolution ErrorCode = "E_BADPATH" ErrCodeDenylistFile ErrorCode = "E_DENYLIST" ErrCodeUnknownError ErrorCode = "E_UNKNOWN" ErrNotExist ErrorCode = "E_NOTEXIST" )
type Filesystem ¶
type Filesystem struct {
// contains filtered or unexported fields
}
func New ¶
func New(root string, size int64, denylist []string) *Filesystem
New creates a new Filesystem instance for a given server.
func (*Filesystem) CachedUsage ¶
func (fs *Filesystem) CachedUsage() int64
Returns the cached value for the amount of disk space used by the filesystem. Do not rely on this function for critical logical checks. It should only be used in areas where the actual disk usage does not need to be perfect, e.g. API responses for server resource usage.
func (*Filesystem) Chmod ¶ added in v1.1.3
func (fs *Filesystem) Chmod(path string, mode os.FileMode) error
func (*Filesystem) Chown ¶
func (fs *Filesystem) Chown(path string) error
Recursively iterates over a file or directory and sets the permissions on all of the underlying files. Iterate over all of the files and directories. If it is a file just go ahead and perform the chown operation. Otherwise dig deeper into the directory until we've run out of directories to dig into.
func (*Filesystem) Chtimes ¶ added in v1.5.0
func (fs *Filesystem) Chtimes(path string, atime, mtime time.Time) error
func (*Filesystem) CompressFiles ¶
CompressFiles compresses all the files matching the given paths in the specified directory. This function also supports passing nested paths to only compress certain files and folders when working in a larger directory. This effectively creates a local backup, but rather than ignoring specific files and folders, it takes an allow-list of files and folders.
All paths are relative to the dir that is passed in as the first argument, and the compressed file will be placed at that location named `archive-{date}.tar.gz`.
func (*Filesystem) Copy ¶
func (fs *Filesystem) Copy(p string) error
Copies a given file to the same location and appends a suffix to the file to indicate that it has been copied.
func (*Filesystem) CreateDirectory ¶
func (fs *Filesystem) CreateDirectory(name string, p string) error
Creates a new directory (name) at a specified path (p) for the server.
func (*Filesystem) DecompressFile ¶
DecompressFile will decompress a file in a given directory by using the archiver tool to infer the file type and go from there. This will walk over all the files within the given archive and ensure that there is not a zip-slip attack being attempted by validating that the final path is within the server data directory.
func (*Filesystem) DecompressFileUnsafe ¶ added in v1.11.0
DecompressFileUnsafe will decompress any file on the local disk without checking if it is owned by the server. The file will be SAFELY decompressed and extracted into the server's directory.
func (*Filesystem) Delete ¶
func (fs *Filesystem) Delete(p string) error
Delete removes a file or folder from the system. Prevents the user from accidentally (or maliciously) removing their root server data directory.
func (*Filesystem) DirectorySize ¶
func (fs *Filesystem) DirectorySize(dir string) (int64, error)
Determines the directory size of a given location by running parallel tasks to iterate through all of the folders. Returns the size in bytes. This can be a fairly taxing operation on locations with tons of files, so it is recommended that you cache the output.
func (*Filesystem) DiskUsage ¶
func (fs *Filesystem) DiskUsage(allowStaleValue bool) (int64, error)
Internal helper function to allow other parts of the codebase to check the total used disk space as needed without overly taxing the system. This will prioritize the value from the cache to avoid excessive IO usage. We will only walk the filesystem and determine the size of the directory if there is no longer a cached value.
If "allowStaleValue" is set to true, a stale value MAY be returned to the caller if there is an expired cache value AND there is currently another lookup in progress. If there is no cached value but no other lookup is in progress, a fresh disk space response will be returned to the caller.
This is primarily to avoid a bunch of I/O operations from piling up on the server, especially on servers with a large amount of files.
func (*Filesystem) ExtractStreamUnsafe ¶ added in v1.11.0
ExtractStreamUnsafe .
func (*Filesystem) File ¶ added in v1.2.0
File returns a reader for a file instance as well as the stat information.
func (*Filesystem) HasSpaceAvailable ¶
func (fs *Filesystem) HasSpaceAvailable(allowStaleValue bool) bool
Determines if the directory a file is trying to be added to has enough space available for the file to be written to.
Because determining the amount of space being used by a server is a taxing operation we will load it all up into a cache and pull from that as long as the key is not expired.
This operation will potentially block unless allowStaleValue is set to true. See the documentation on DiskUsage for how this affects the call.
func (*Filesystem) HasSpaceErr ¶ added in v1.2.0
func (fs *Filesystem) HasSpaceErr(allowStaleValue bool) error
The same concept as HasSpaceAvailable however this will return an error if there is no space, rather than a boolean value.
func (*Filesystem) HasSpaceFor ¶ added in v1.2.0
func (fs *Filesystem) HasSpaceFor(size int64) error
Helper function to determine if a server has space available for a file of a given size. If space is available, no error will be returned, otherwise an ErrNotEnoughSpace error will be raised.
func (*Filesystem) IsIgnored ¶ added in v1.3.0
func (fs *Filesystem) IsIgnored(paths ...string) error
Checks if the given file or path is in the server's file denylist. If so, an Error is returned, otherwise nil is returned.
func (*Filesystem) ListDirectory ¶
func (fs *Filesystem) ListDirectory(p string) ([]Stat, error)
ListDirectory lists the contents of a given directory and returns stat information about each file and folder within it.
func (*Filesystem) MaxDisk ¶
func (fs *Filesystem) MaxDisk() int64
Returns the maximum amount of disk space that this Filesystem instance is allowed to use.
func (*Filesystem) ParallelSafePath ¶
func (fs *Filesystem) ParallelSafePath(paths []string) ([]string, error)
Executes the fs.SafePath function in parallel against an array of paths. If any of the calls fails an error will be returned.
func (*Filesystem) Path ¶
func (fs *Filesystem) Path() string
Path returns the root path for the Filesystem instance.
func (*Filesystem) Rename ¶
func (fs *Filesystem) Rename(from string, to string) error
Rename moves (or renames) a file or directory.
func (*Filesystem) SafePath ¶
func (fs *Filesystem) SafePath(p string) (string, error)
Normalizes a directory being passed in to ensure the user is not able to escape from their data directory. After normalization if the directory is still within their home path it is returned. If they managed to "escape" an error will be returned.
This logic is actually copied over from the SFTP server code. Ideally that eventually either gets ported into this application, or is able to make use of this package.
func (*Filesystem) SetDiskLimit ¶
func (fs *Filesystem) SetDiskLimit(i int64)
Sets the disk space limit for this Filesystem instance.
func (*Filesystem) SpaceAvailableForDecompression ¶
func (fs *Filesystem) SpaceAvailableForDecompression(ctx context.Context, dir string, file string) error
SpaceAvailableForDecompression looks through a given archive and determines if decompressing it would put the server over its allocated disk space limit.
func (*Filesystem) Stat ¶
func (fs *Filesystem) Stat(p string) (Stat, error)
Stat stats a file or folder and returns the base stat object from go along with the MIME data that can be used for editing files.
func (*Filesystem) Touch ¶ added in v1.3.0
Touch acts by creating the given file and path on the disk if it is not present already. If it is present, the file is opened using the defaults which will truncate the contents. The opened file is then returned to the caller.
func (*Filesystem) TruncateRootDirectory ¶ added in v1.3.0
func (fs *Filesystem) TruncateRootDirectory() error
TruncateRootDirectory removes _all_ files and directories from a server's data directory and resets the used disk space to zero.
func (*Filesystem) Writefile ¶
func (fs *Filesystem) Writefile(p string, r io.Reader) error
Writefile writes a file to the system. If the file does not already exist one will be created. This will also properly recalculate the disk space used by the server when writing new files or modifying existing ones.
type SpaceCheckingOpts ¶
type SpaceCheckingOpts struct {
AllowStaleResponse bool
}
type Stat ¶
func (*Stat) MarshalJSON ¶
type TarProgress ¶ added in v1.11.0
TarProgress .
func NewTarProgress ¶ added in v1.11.0
func NewTarProgress(w *tar.Writer, p *progress.Progress) *TarProgress
NewTarProgress .