File System Watcher is a modern C++ application designed for Linux that uses the inotify API to monitor file system events in a specified directory. It demonstrates key design patterns (such as the Observer and Factory patterns) while leveraging modern C++ features (C++17). The project is built using CMake and provides a modular design that can be extended with custom observers.
-
Directory Monitoring:
Uses Linux’s inotify API to detect file creation, deletion, modification, and file moves (renames or moves from external directories). -
Observer Pattern Implementation:
Decouples file event detection from event handling. Observers receive file events uniformly through theonFileEvent()
method. -
Built-in Observers:
- LoggerObserver: Logs file events (creation, deletion, modification, and renames) to the console.
- BackupObserver: Automatically backs up files when a create event is detected.
-
Configuration Management:
A simple configuration system to parse command-line arguments or configuration files (viaConfig.h
andConfig.cpp
). -
Test Suite:
Includes unit tests (with a separate CMake configuration in thetests/
folder) for FileWatcher functionality and observer interactions.
file_system_watcher/
├── CMakeLists.txt # Top-level CMake build configuration.
├── README.md # Project overview and usage instructions.
├── docs/ # (Optional) Documentation files.
├── include/
│ ├── FileWatcher.h # Class that encapsulates inotify operations.
│ ├── Observer.h # Interface for Observer pattern.
│ ├── ObserverFactory.h # Factory interface to create observers.
│ ├── LoggerObserver.h # Observer that logs file events to the console.
│ ├── BackupObserver.h # Observer that backs up files (e.g., on create events).
│ ├── Config.h # Configuration management (e.g., CLI args or config files).
├── src/
│ ├── main.cpp # Entry point: sets up, initializes, and starts the watcher.
│ ├── FileWatcher.cpp # Implementation of the FileWatcher class.
│ ├── Config.cpp # Implementation of configuration parsing.
├── tests/
│ ├── CMakeLists.txt # Separate CMake build configuration for tests.
│ ├── test_file_watcher.cpp # Unit tests for FileWatcher and observer functionalities.
└── examples/ # (Optional) Example configuration files or scripts to run the app.
This project uses CMake for building. Ensure you have CMake (minimum version 3.10) and a C++17 compatible compiler installed.
git clone <repository_url>
cd file_system_watcher
It’s best practice to build in a separate directory:
mkdir build
cd build
Run CMake from the build directory:
cmake ..
make
This will build the filesystem_watcher executable.
If tests are enabled, you can build and run them:
ctest
The executable accepts command-line arguments to customize its behavior. The typical arguments are:
The path of the directory to be monitored. If not specified, the current directory is watched.
The type of observer to instantiate, e.g., "logger" for the LoggerObserver or "backup" for the BackupObserver. For the BackupObserver, an additional parameter for the backup directory may be required.
Monitor the current directory using LoggerObserver (default):
./filesystem_watcher
Monitor /var/log using LoggerObserver:
./filesystem_watcher /var/log logger
Monitor /home/user/workspace using BackupObserver (with backup directory /home/user/workspace/backup):
./filesystem_watcher /home/user/workspace backup /home/user/workspace/backup
In this example, the observer factory will create a BackupObserver that receives the backup directory as an argument.
Purpose: Logs file system events (creation, deletion, modification, renames) to standard output.
Usage: Does not require additional parameters in its constructor.
Purpose: Backs up files on deletion events by copying the file to a specified backup directory.
Usage: Requires the backup directory (and optionally the watched directory) as constructor parameters. When a delete event is detected, the BackupObserver attempts to copy the file from the watched directory to the backup location.
Contributions are welcome! If you have suggestions for new observers or improvements to the FileWatcher functionality, please open an issue or submit a pull request.
This project is licensed under the MIT License. See the LICENSE file for details.
Linux inotify Documentation for details on file system event monitoring.
Inspiration from modern C++ design patterns and examples for building modular applications.