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:
objectCustom 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:
- cwd(path)[source]#
Change the current working directory.
- Parameters:
path (str) – The directory to change to.
- Raises:
FileNotFoundError – If the directory does not exist.
NotADirectoryError – If the path is not a directory.
PermissionError – If attempting to move outside the root directory.
- cd_up()[source]#
Change to the parent directory.
- Raises:
FileNotFoundError – If the parent directory does not exist.
PermissionError – If attempting to move outside the root directory.
- 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:
- Raises:
PermissionError – If attempting to move outside the root directory.
- read(fname, type)[source]#
Read a file in chunks.
- Parameters:
- Yields:
bytes – Chunks of the file content.
- Raises:
FileNotFoundError – If the file does not exist.
PermissionError – If attempting to move outside the root directory.
- 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:
- Raises:
PermissionError – If attempting to move outside the root directory.
FileHandlerError – If the path is not a file.
- delete(fname)[source]#
Delete a file.
- Parameters:
fname (str) – The file to delete.
- Raises:
PermissionError – If attempting to move outside the root directory.
FileHandlerError – If the path is not a file.
- rmdir(path)[source]#
Remove a directory.
- Parameters:
path (str) – The directory to remove.
- Raises:
FileNotFoundError – If the directory does not exist.
NotADirectoryError – If the path is not a directory.
PermissionError – If attempting to move outside the root directory.
- rename_from(old)[source]#
Mark a file or directory for renaming.
- Parameters:
old (str) – Existing file/directory name.
- Raises:
FileNotFoundError – If the original path does not exist.
PermissionError – If attempting to move outside the root directory.
- 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.