filesys

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AtomicCopyFileContent

func AtomicCopyFileContent(src string, dest string) error

AtomicCopyFileContent copies the contents from a source file to a destination file atomically. The operation ensures that the destination file is either fully updated or remains unchanged if an error occurs.

Params:

src (string): Path to the source file.
dest (string): Path to the destination file.

Returns:

error: Returns an error if the copying process fails at any step.

The function performs the copy by creating a temporary file at the destination. This temporary file is used to prevent partial writes to the destination file in case of errors. After copying the content successfully, the temporary file is synchronized and then renamed to the destination file name, ensuring that the file appears to be updated atomically from the perspective of other processes.

func CreateDirIfNotExists

func CreateDirIfNotExists(path string, perm os.FileMode) error

CreateDirIfNotExists ensures that a directory exists at the specified path. If the directory does not exist, it creates it with the specified permissions. If the path exists but is not a directory, an error is returned.

Parameters:

  • path: The file system path where the directory should exist.
  • perm: The file mode permissions to use when creating the directory.

Returns:

  • error: An error if the path exists but is not a directory, if there is an issue creating the directory, or any other file system-related error.

func CreateZipFile

func CreateZipFile(source, target string) error

CreateZipFile creates a zip archive from all files and directories within a specified source directory. The resulting zip file is stored at the 'target' path. Each entry in the zip file corresponds to a file or directory from the source, where directory entries end with a path separator.

The function walks through the source directory, compresses files using the Deflate algorithm, and adds them to the zip archive. Directories are added to the archive with appropriate markers but no content. Errors during file handling (e.g., file cannot be opened, read, or written) or archive creation are returned.

Parameters:

  • source: The directory whose contents are to be zipped.
  • target: The file path where the zip archive will be saved.

Returns:

  • error: Non-nil error if there was a problem creating the zip file or processing its contents.

func GetFileChecksum

func GetFileChecksum(path string) (string, error)

GetFileChecksum computes and returns the SHA-256 checksum of the file at the given path.

The function reads the entire content of the file and calculates its SHA-256 hash, returning the result as a hexadecimal-encoded string.

Parameters:

  • path: The full path to the file to be hashed.

Returns:

  • string: The SHA-256 hash of the file as a hex string.
  • error: An error if the file cannot be opened or read.

func GlobWithDepth

func GlobWithDepth(root string, pattern string) ([]string, error)

GlobWithDepth finds files within a specified root directory that match a given pattern. This function walks the directory tree starting from 'root' and adds paths to the returned slice if they match the 'pattern'. The pattern matching respects the syntax of filepath.Match.

Only non-directory file paths that match the pattern are included in the results. Directories are traversed but not included in the result set. Any errors encountered during directory traversal or pattern matching are returned.

Parameters:

  • root: The root directory from which the file tree traversal begins.
  • pattern: The pattern used to match file names, not file paths.

Returns:

  • []string: A slice of matched file paths.
  • error: Non-nil error if an error occurred during file tree traversal or pattern matching.

func IsDirectory

func IsDirectory(path string) (bool, error)

IsDirectory checks if the specified path refers to a directory. It returns a boolean indicating whether the path is a directory, and any error encountered during the operation. If the path does not exist or cannot be accessed, the function returns an error, and the boolean is false.

Parameters:

  • path: The file system path to check.

Returns:

  • bool: True if the path is a directory, false otherwise.
  • error: Non-nil error if there was an issue accessing the path.

func IsReadableFile

func IsReadableFile(path string) (bool, error)

IsReadableFile checks if file exists, and is readable. Params:

  • path: path of the file to be checked.

Returns:

  • bool: true if file is readable, false if not file is not readable
  • error: Non-nil error if the file is not readable due to various reasons
func IsSymlink(path string) (bool, error)

IsSymlink checks whether the given path is a symbolic link.

The function uses os.Lstat to inspect the file metadata without following symlinks. If the file mode includes os.ModeSymlink, it returns true.

Parameters:

  • path: The file or directory path to check.

Returns:

  • bool: True if the path is a symbolic link.
  • error: An error if the path does not exist or cannot be inspected.

func SanitizeFilename

func SanitizeFilename(name, defaultName string) (safe string, encoded string)

SanitizeFilename keeps UTF-8 characters but removes/neutralizes header-unsafe ones, returning a safe filename and a percent-encoded variant suitable for `filename*`. - Replaces: " \ \r \n → '_' - Strips: control chars (<0x20) and DEL (0x7F) - Falls back to defaultName if nothing remains

func UnzipFile

func UnzipFile(source, target string) error

UnzipFile extracts the contents of a zip archive to a specified destination directory. It creates directories and files as needed to match the structure of the archive.

Parameters:

  • source: The path to the zip archive to be extracted.
  • target: The directory where the contents of the archive will be extracted.

Returns:

  • error: Non-nil error if there was a problem reading the archive or writing its contents.

func WriteFile

func WriteFile(path string, content []byte, umask os.FileMode) error

WriteFile writes the given content to the specified path. It creates the necessary directories in the path if they do not already exist.

The function accepts three parameters:

  • path: A string representing the path where the file should be written. If the directories in the path do not exist, they are created.
  • content: A byte slice ([]byte) that contains the content to be written to the file.
  • umask: An os.FileMode value representing the permissions to use when creating the file and any necessary directories.

Types

This section is empty.

Jump to

Keyboard shortcuts

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