Package org.bluezoo.gumdrop.ftp.file
Class BasicFTPFileSystem
java.lang.Object
org.bluezoo.gumdrop.ftp.file.BasicFTPFileSystem
- All Implemented Interfaces:
FTPFileSystem
Concrete
FTPFileSystem backed by the local OS file system.
Implements the NVFS (RFC 959 section 2.2) and TVFS (RFC 3659 section 6)
semantics with a chrooted root directory.
Security Features:
- Path normalization prevents ../ directory traversal
- Symbolic links are resolved securely
- Access is limited to the configured root directory tree
- File permissions are checked before operations
Performance Features:
- Uses NIO for file metadata operations
- Efficient directory listing with lazy loading
- Stream-based file I/O for memory efficiency
- Skip-ahead support for resumed downloads
- Author:
- Chris Burdess
-
Nested Class Summary
Nested classes/interfaces inherited from interface org.bluezoo.gumdrop.ftp.FTPFileSystem
FTPFileSystem.DirectoryChangeResult, FTPFileSystem.UniqueNameResult -
Constructor Summary
ConstructorsConstructorDescriptionBasicFTPFileSystem(String rootDirectory) Creates a read-write BasicFTPFileSystem.BasicFTPFileSystem(String rootDirectory, boolean readOnly) Creates a new BasicFTPFileSystem with the specified root directory.BasicFTPFileSystem(Path rootDirectory, boolean readOnly) Creates a new BasicFTPFileSystem with the specified root directory. -
Method Summary
Modifier and TypeMethodDescriptionallocateSpace(String path, long size, FTPConnectionMetadata metadata) Allocates space for a file (optional operation).changeDirectory(String path, String currentDirectory, FTPConnectionMetadata metadata) Changes the current working directory.createDirectory(String path, FTPConnectionMetadata metadata) Creates a new directory.deleteFile(String path, FTPConnectionMetadata metadata) Deletes a file.generateUniqueName(String basePath, String suggestedName, FTPConnectionMetadata metadata) Generates a unique file name for store-unique operations.getFileInfo(String path, FTPConnectionMetadata metadata) Gets information about a specific file or directory.Gets the root directory path for this file system.booleanChecks if this file system is read-only.listDirectory(String path, FTPConnectionMetadata metadata) Lists the contents of a directory.openForReading(String path, long restartOffset, FTPConnectionMetadata metadata) Opens a file for reading (download) using NIO channels.openForWriting(String path, boolean append, FTPConnectionMetadata metadata) Opens a file for writing (upload) using NIO channels.removeDirectory(String path, FTPConnectionMetadata metadata) Removes an empty directory.rename(String fromPath, String toPath, FTPConnectionMetadata metadata) Renames or moves a file or directory.protected PathresolveSecurePath(String ftpPath) Converts an FTP path to a secure local file system path.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.bluezoo.gumdrop.ftp.FTPFileSystem
resolvePathForAsyncRead, resolvePathForAsyncWrite
-
Constructor Details
-
BasicFTPFileSystem
Creates a new BasicFTPFileSystem with the specified root directory.- Parameters:
rootDirectory- the root directory for this file systemreadOnly- true to make this file system read-only- Throws:
IllegalArgumentException- if rootDirectory doesn't exist or isn't a directory
-
BasicFTPFileSystem
Creates a new BasicFTPFileSystem with the specified root directory.- Parameters:
rootDirectory- the root directory path for this file systemreadOnly- true to make this file system read-only- Throws:
IllegalArgumentException- if rootDirectory doesn't exist or isn't a directory
-
BasicFTPFileSystem
Creates a read-write BasicFTPFileSystem.
-
-
Method Details
-
resolveSecurePath
Converts an FTP path to a secure local file system path. Prevents directory traversal attacks and ensures the path stays within the root.- Parameters:
ftpPath- FTP path (Unix-style, starting with /)- Returns:
- resolved local path within the root directory
- Throws:
SecurityException- if path tries to escape the root directory
-
listDirectory
Description copied from interface:FTPFileSystemLists the contents of a directory. Used for FTP LIST and NLST commands.- Specified by:
listDirectoryin interfaceFTPFileSystem- Parameters:
path- the directory path to list (Unix-style, starting with "/")metadata- connection metadata for authorization context- Returns:
- list of file information, or null if the operation failed
-
changeDirectory
public FTPFileSystem.DirectoryChangeResult changeDirectory(String path, String currentDirectory, FTPConnectionMetadata metadata) Description copied from interface:FTPFileSystemChanges the current working directory. Used for FTP CWD command.- Specified by:
changeDirectoryin interfaceFTPFileSystem- Parameters:
path- the new directory path (can be relative or absolute)currentDirectory- the current directory before the changemetadata- connection metadata for authorization context- Returns:
- operation result and the new absolute directory path
-
getFileInfo
Description copied from interface:FTPFileSystemGets information about a specific file or directory. Used for various FTP commands that need to check file existence/properties.- Specified by:
getFileInfoin interfaceFTPFileSystem- Parameters:
path- the file or directory pathmetadata- connection metadata for authorization context- Returns:
- file information, or null if the file/directory does not exist
-
createDirectory
Description copied from interface:FTPFileSystemCreates a new directory. Used for FTP MKD command.- Specified by:
createDirectoryin interfaceFTPFileSystem- Parameters:
path- the directory path to createmetadata- connection metadata for authorization context- Returns:
- operation result
-
removeDirectory
Description copied from interface:FTPFileSystemRemoves an empty directory. Used for FTP RMD command.- Specified by:
removeDirectoryin interfaceFTPFileSystem- Parameters:
path- the directory path to removemetadata- connection metadata for authorization context- Returns:
- operation result
-
deleteFile
Description copied from interface:FTPFileSystemDeletes a file. Used for FTP DELE command.- Specified by:
deleteFilein interfaceFTPFileSystem- Parameters:
path- the file path to deletemetadata- connection metadata for authorization context- Returns:
- operation result
-
rename
public FTPFileOperationResult rename(String fromPath, String toPath, FTPConnectionMetadata metadata) Description copied from interface:FTPFileSystemRenames or moves a file or directory. Used for FTP RNFR/RNTO command sequence.- Specified by:
renamein interfaceFTPFileSystem- Parameters:
fromPath- the current path of the file/directorytoPath- the new path for the file/directorymetadata- connection metadata for authorization context- Returns:
- operation result
-
openForReading
public ReadableByteChannel openForReading(String path, long restartOffset, FTPConnectionMetadata metadata) Description copied from interface:FTPFileSystemOpens a file for reading (download) using NIO channels. Used for FTP RETR command.This method provides high-performance file reading with:
- Zero-copy transfers when possible
- Direct integration with SocketChannel
- Memory-efficient streaming for large files
- Restart offset support for resumed downloads
- Specified by:
openForReadingin interfaceFTPFileSystem- Parameters:
path- the file path to readrestartOffset- byte offset to start reading from (for REST command)metadata- connection metadata for authorization context- Returns:
- readable channel for the file data, or null if the operation failed
-
openForWriting
public WritableByteChannel openForWriting(String path, boolean append, FTPConnectionMetadata metadata) Description copied from interface:FTPFileSystemOpens a file for writing (upload) using NIO channels. Used for FTP STOR command.This method provides high-performance file writing with:
- Zero-copy transfers when possible
- Direct integration with SocketChannel
- Memory-efficient streaming for large files
- Append mode support
- Specified by:
openForWritingin interfaceFTPFileSystem- Parameters:
path- the file path to writeappend- true to append to existing file, false to overwritemetadata- connection metadata for authorization context- Returns:
- writable channel for writing file data, or null if the operation failed
-
generateUniqueName
public FTPFileSystem.UniqueNameResult generateUniqueName(String basePath, String suggestedName, FTPConnectionMetadata metadata) Description copied from interface:FTPFileSystemGenerates a unique file name for store-unique operations. Used for FTP STOU command.- Specified by:
generateUniqueNamein interfaceFTPFileSystem- Parameters:
basePath- the base directory pathsuggestedName- suggested file name (can be null)metadata- connection metadata for authorization context- Returns:
- unique file name result
-
allocateSpace
Description copied from interface:FTPFileSystemAllocates space for a file (optional operation). Used for FTP ALLO command - many implementations can ignore this.- Specified by:
allocateSpacein interfaceFTPFileSystem- Parameters:
path- the file path that will need spacesize- the number of bytes to allocatemetadata- connection metadata for authorization context- Returns:
- operation result
-
getRootPath
Gets the root directory path for this file system. -
isReadOnly
public boolean isReadOnly()Checks if this file system is read-only.
-