File Manager Module#

The file manager module handles file system operations for the FTP server.

File handling utilities for thinFTP

This module defines the FileHandler class used by the thinFTP server to manage filesystem operations securely within a defined root directory. It ensures safe access to files and directories and provides support for FTP-style commands such as directory navigation, file listing, reading/writing files, deletion, and renaming.

Classes:

FileHandler: Encapsulates all file operations, enforcing confinement to the FTP server’s root directory. Raises appropriate exceptions for invalid actions.

Exceptions:

FileHandlerError: Raised for invalid file-related operations such as attempting to get the size of or delete a non-file.

Dependencies:
  • pathlib.Path: For file path resolution and operations.

  • stat: For interpreting file permission modes.

  • time: For formatting file modification times.

  • .errors.FileHandlerError: Custom exception used in this module.

class thinftp.fileman.FileHandler(root_dir)[source]#

Bases: object

Custom File Handler class for thinFTP.

Handles file operations such as navigating directories, listing contents, reading and writing files, and renaming or deleting files and directories.

__init__(root_dir)[source]#

Initialize the FileHandler with a root directory.

Parameters:

root_dir (str) – The root directory for file operations.

resolve_path(path)[source]#

Resolve a given path to an absolute path within the root directory.

Parameters:

path (str) – The path to resolve.

Returns:

An absolute Path object.

Return type:

Path

pwd()[source]#

Get the current working directory relative to the root directory.

Returns:

The current directory as a string.

Return type:

str

get_abs(path)[source]#

Get the absolute path of a given path relative to the root directory.

Parameters:

path (str) – The path to resolve.

Returns:

The absolute path as a string.

Return type:

str

cwd(path)[source]#

Change the current working directory.

Parameters:

path (str) – The directory to change to.

Raises:
cd_up()[source]#

Change to the parent directory.

Raises:
mkdir(path)[source]#

Create a new directory.

Parameters:

path (str) – Path of directory to create.

name_ls(path)[source]#

List the names of entries in a directory.

Parameters:

path (str) – The directory to list.

Returns:

A list of Path objects representing entries in the directory.

Return type:

list

Raises:

PermissionError – If attempting to move outside the root directory.

ls(path)[source]#

List the contents of the specified directory.

Parameters:

path (str) – The directory to list.

Returns:

A list of strings representing the contents of the directory, in the format:

<permissions> <nlinks> <owner> <group> <size> <modtime> <name>

If the directory does not exist, an empty list is returned.

Return type:

list

read(fname, type)[source]#

Read a file in chunks.

Parameters:
  • fname (str) – The file to read.

  • type (str) – The transfer type (‘A’ for ASCII, ‘I’ for binary).

Yields:

bytes – Chunks of the file content.

Raises:
size(fname)[source]#

Get the size of a file.

Parameters:

fname (str) – The file to check.

Returns:

The size of the file in bytes.

Return type:

int

Raises:
delete(fname)[source]#

Delete a file.

Parameters:

fname (str) – The file to delete.

Raises:
rmdir(path)[source]#

Remove a directory.

Parameters:

path (str) – The directory to remove.

Raises:
rename_from(old)[source]#

Mark a file or directory for renaming.

Parameters:

old (str) – Existing file/directory name.

Raises:
rename_to(new)[source]#

Rename a previously marked file or directory.

Parameters:

new (str) – New file/directory name.

write(fname, data)[source]#

Write data to a file.

Parameters:
  • fname (str) – The file to write to.

  • data (iterable) – An iterable of bytes to write.

Raises:

PermissionError – If attempting to move outside the root directory.