fs

package
v1.5.6 Latest Latest
Warning

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

Go to latest
Published: May 26, 2025 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CopyFile added in v1.5.1

func CopyFile(
	ctx context.Context,
	errCh chan<- error,
	file types.Path,
)

CopyFile copies a file from the source path to the destination path, taking into account the context cancellation, existing file handling mode (`existsMode`), and optional conversion of file content.

The function checks if the destination file exists and takes action based on the specified `existsMode`:

  • If `Skip`, it does nothing if the file already exists.
  • If `ReplaceIfNewer`, it replaces the file only if the source file is newer than the destination file.
  • If neither of the above, it always replaces the file.

If the `convert` flag is set to `true` and the file is convertible (determined by the `isConvertable` function), it converts the file content using Windows-1251 encoding before writing it to the destination.

The function handles errors by sending them to the provided error channel and checks the context at various stages to support cancellation during file copying.

Parameters:

  • ctx (context.Context): The context to control the execution and cancellation of the operation.
  • errCh (chan<- error): A channel for reporting errors encountered during the operation.
  • file (types.Path): Path params.

Returns:

  • None: Errors are sent to the error channel `errCh`, and no return value is provided.

func IsEmptyDir

func IsEmptyDir(path string) bool

IsEmptyDir checks whether the specified directory exists and is empty.

Parameters:

  • path: The path to the directory to check.

Returns:

  • true if the directory exists and is empty.
  • false if the directory does not exist, is not accessible, or contains at least one entry.

Notes:

  • If the directory cannot be opened (e.g., due to permission issues), the function returns false.
  • If an error occurs while reading directory entries, it is assumed to be empty (e.g., when the directory does not exist).
  • Logs an error if closing the directory fails.

func IsFileExists added in v1.4.4

func IsFileExists(path string) (bool, int64)

IsFileExists checks whether the given path exists and points to a regular file.

The function first cleans the input path using filepath.Clean. It then checks if the file exists and is a regular file (not a directory, symlink, etc.).

Parameters:

  • path: The filesystem path to check.

Returns:

  • bool: true if the file exists and is a regular file; false otherwise.
  • int64: The size of the file in bytes if it exists, or 0 if it does not.

func MkDir

func MkDir(path string) (string, error)

MkDir creates a directory at the specified path, including any necessary parent directories. It first ensures the provided path is an absolute and clean path, and checks whether the path is valid. If the path does not exist, it will be created with permissions 0750. If the directory already exists, it does nothing.

Parameters:

  • path (string): The path where the directory should be created. The function will resolve this to an absolute path and ensure it is valid before attempting to create the directory.

Returns:

  • string: The absolute path of the created directory.
  • error: If an error occurs during path resolution or directory creation, an error is returned.

func PathProcessing added in v1.5.1

func PathProcessing(
	ctx context.Context,
	filesCh chan<- types.Path,
	cfg interfaces.ModuleConfig,
	path types.Path,
	filterRules []string,
) error

PathProcessing walks through the source directory specified in `path.From` and submits file copy tasks to the provided `filesCh` channel.

It applies ignore rules, change tracking (if enabled), and pattern-based filtering before sending any file to be processed. All context cancellations are respected and short-circuit the operation early.

Parameters:

  • ctx: Context used to control cancellation and timeouts.
  • filesCh: Channel to which valid file copy tasks are submitted (type: types.Path).
  • cfg: Module configuration providing access to ignore rules and optional change tracking.
  • path: Describes the copy task, including source, destination, overwrite mode, and encoding settings.
  • filterRules: A list of file path patterns to include (e.g., ["*.php", "*.tpl"]).

Returns:

  • error: If an error occurs during directory walking or context cancellation.

func RemoveEmptyDirs

func RemoveEmptyDirs(root string) (bool, error)

RemoveEmptyDirs recursively removes empty directories within the specified root directory.

Parameters:

  • root: The path of the directory to start the Cleanup.

Returns:

  • A boolean indicating whether the directory itself is empty after processing.
  • An error if any issue occurs while reading or removing directories.

Behavior:

  • Traverses all subdirectories recursively.
  • If a subdirectory becomes empty after processing its contents, it gets removed.
  • If any error occurs (e.g., permission issues), the error is returned.
  • The function ensures that only empty directories are removed, leaving files untouched.

Example:

_, err := RemoveEmptyDirs("/path/to/root")
if err != nil {
    log.Fatalf("Failed to remove empty directories: %v", err)
}

func ZipIt

func ZipIt(dirPath, archivePath string) error

ZipIt creates a ZIP archive from the specified directory, including all its files and subdirectories. It recursively walks through the directory, adding files and directories to the ZIP archive, preserving the relative paths of the files.

Parameters:

  • dirPath (string): The path to the directory to be archived. It is walked recursively, and all files and subdirectories are included in the ZIP archive.
  • archivePath (string): The path where the ZIP archive should be created. The function will resolve this to an absolute path and ensure the archive is saved at that location.

Returns:

  • error: If an error occurs during any part of the zipping process (such as file opening, writing, or walking), an error is returned.

Types

This section is empty.

Jump to

Keyboard shortcuts

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