Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ExtraConfig ¶
type ExtraConfig struct { //Username is the username that is used to connect to the ftp server Username string //Password is the password that is used to connect to the ftp server Password string //LocalDir is the local directory that is used to sync with the remote directory LocalDir string //RemoteDir is the remote directory that is used to sync with the local directory RemoteDir string //Retries is the number of retries that the ftp client will try to upload/download a file Retries int //MaxRetries is the number of retries that the ftp client will try to upload/download a file MaxRetries int }
ExtraConfig is the struct that holds the extra config for the ftp connection
type FTP ¶
type FTP struct { sync.Mutex //Direction is the direction of the sync (LocalToRemote or RemoteToLocal) Direction SyncDirection //Watcher is the fsnotify watcher that is used to watch the local directory Watcher *fsnotify.Watcher //Pool is the worker pool that is used to process the fsnotify events Pool *worker.Pool // contains filtered or unexported fields }
FTP is the struct that holds the ftp client and the sync direction
func Connect ¶
func Connect(address string, port int, direction SyncDirection, config *ExtraConfig) (*FTP, error)
Connect is a function used to establish a connection to an FTP server and return an FTP client for file synchronization.
- address is the address of the FTP server.
- port is the port of the FTP server.
- direction is the direction of the synchronization, which can be either LocalToRemote or RemoteToLocal.
- config is a pointer to the ExtraConfig struct that holds additional configuration settings for the FTP connection, including FTP server credentials (username and password), local and remote directories, and synchronization retries.
Example:
ftp, err := ftp.Connect("localhost", 21, ftp.LocalToRemote, &ftp.ExtraConfig{ Username: "username", Password: "password", LocalDir: "localDir", RemoteDir: "remoteDir", Retries: 3, MaxRetries: 3, }) if err != nil { log.Fatal(err) }
func (*FTP) AddDirectoriesToWatcher ¶
AddDirectoriesToWatcher is a method of the FTP struct that adds directories and their subdirectories to the fsnotify watcher.
- watcher is a pointer to the fsnotify.Watcher that will be used to watch for file system events.
- rootDir is the root directory from which directories and subdirectories will be added to the watcher.
The method behaves differently based on the sync direction specified in f.Direction:
LocalToRemote: It walks the local directory tree starting from rootDir and adds all directories to the fsnotify watcher. Each time a new directory is added, the method logs the event and starts watching for file system events in that directory.
RemoteToLocal: It continuously reads the remote directory tree and its subdirectories and compares it with the previous state. When new files are detected or files are modified on the remote server, the method enqueues tasks to the worker pool for processing. If files are removed from the remote server, the method enqueues tasks to the worker pool to handle the file removal. The method keeps monitoring for changes in the remote directory tree until the context (f.ctx) is canceled or an error occurs.
- Returns an error if there is a problem while adding directories to the fsnotify watcher or monitoring the remote directory tree.
func (*FTP) Stat ¶
Stat is a method of the FTP struct that retrieves file information (os.FileInfo) for a remote file on the FTP server.
- path is the path of the remote file for which file information is required.
The method calculates the remote file path by joining the remote directory (f.config.RemoteDir) with the base name of the specified path. It then fetches the file information from the FTP server using the f.client.Stat method.
- Returns the file information (os.FileInfo) for the remote file if the operation is successful.
- Returns an error if there is a problem retrieving the file information from the FTP server.
func (*FTP) WatchDirectory ¶
func (f *FTP) WatchDirectory()
WatchDirectory is a method of the FTP struct that sets up a file system watcher to monitor changes in the local directory. It starts a worker pool and performs an initial synchronization between the local directory and the remote directory based on the specified synchronization direction (LocalToRemote or RemoteToLocal).
The method uses fsnotify package to monitor file system events such as file creations, modifications, and deletions. When a file system event is detected, it creates a worker task and adds it to the worker pool for processing. The worker tasks are handled by the Worker method, which performs the necessary file transfers to keep the directories in sync.
The synchronization is bidirectional, meaning that changes made in the local directory will be propagated to the remote directory, and changes made in the remote directory will be reflected in the local directory.
- Please note that this method enters an infinite loop to continuously monitor file system events until the context is canceled. The method will block until the context is done or an error occurs during the synchronization process.
func (*FTP) Worker ¶
func (f *FTP) Worker()
Worker starts a new worker goroutine that processes tasks received from the worker pool.
The method listens for tasks on the f.Pool.Tasks channel, which is a buffered channel used for queuing tasks. Each task contains an EventType (fsnotify.Write, fsnotify.Remove, fsnotify.Rename, fsnotify.Chmod) and a Name (the file path of the task).
Depending on the EventType and the sync direction (LocalToRemote or RemoteToLocal), the method performs different actions:
- For fsnotify.Write events:
- LocalToRemote: Calls f.uploadFile to upload the modified or newly created file to the remote FTP server.
- RemoteToLocal: Calls f.downloadFile to download the modified or newly created file from the remote FTP server to the local machine.
- For fsnotify.Remove events:
- LocalToRemote: Calls f.removeRemoteFile to delete the specified file from the remote FTP server.
- RemoteToLocal: Calls f.removeLocalFile to delete the specified file from the local machine.
- For fsnotify.Rename events:
- LocalToRemote: Calls f.uploadFile to upload the renamed file to the remote FTP server, then calls f.removeRemoteFile to delete the original file from the server.
- RemoteToLocal: Calls f.downloadFile to download the renamed file from the remote FTP server to the local machine, then calls f.removeLocalFile to delete the original file from the local machine.
- For fsnotify.Chmod events: The method logs a message indicating that the permissions of a file have changed.
After processing each task, the method marks it as done using f.Pool.WG.Done(), which decrements the worker pool's WaitGroup counter.
type SyncDirection ¶
type SyncDirection int
SyncDirection is the direction of the sync (LocalToRemote or RemoteToLocal)
const ( //LocalToRemote is the direction of the sync from local to remote pc/server LocalToRemote SyncDirection = iota //RemoteToLocal is the direction of the sync from remote to local pc/server RemoteToLocal )