Skip to content

The AbstractSync classes

Greg Bowler edited this page Jul 19, 2023 · 2 revisions

The functionality of this library comes from the AbstractSync implementations, of which there are currently two: DirectorySync and SymlinkSync.

The principle of any AbstractSync implementation is that there is always a sourceDirectory and a destinationDirectory, and an exec() function to perform the sync.

DirectorySync

The main functionality comes from the DirectorySync class, which is responsible for performing a filesystem sync between two directories. An additional parameter to the DirectorySync constructor is the glob, which is a pattern matching string. This allows the sync to be selective, in only synchronising files that match the pattern.

When the exec() function is called, the comparison type can optionally be passed. By default, the files are compared by their modification time, but if a more robust method is required, each file's contents can be hashed by passing in DirectorySync::COMPARE_HASH.

Once the exec() function has completed, the following functions are available to provide a summary of the operations that have completed, that all return a list of file paths as array<string>:

  • getCopiedFilesList() - a list of every file that has been actively copied from source to destination.
  • getSkippedFilesList() - a list of the files that have been skipped, due to already being present in the destination.
  • getDeletedFilesList() - a list of the files that were deleted, due to no longer being present in the source.

SymlinkSync

Rather than copying files from source to destination every time there's a change in the source directory, a symlink can be created as a one-off, cheap operation that can sometimes be a better solution than a full sync. This feature was introduced so that a directory in a mounted volume can be exposed in the public web root, without having to copy all files across from the volume.

When performing the exec() function, any nested directories that are required will be created, and existing links and files will be checked before creating the symlink.

After completing the exec() function, the following functions are available to provide a summary of the operations that have been completed, that all return a list of file paths as array<string>:

  • getLinkedFilesList() - a list of each link created from a file.
  • getLinkedDirectoryList() - a list of each link created from a directory.
  • getCombinedLinkedList() - a list of each link created, including both files and directories.
  • getFailedList() - a list of each link that could not be created.
  • getSkippedList() - a list of each link that is already present.

Next, learn about command line usage of this library.

Clone this wiki locally