Skip to content

Modern C++ application designed for Linux that uses the inotify API to monitor file system events in a specified directory

Notifications You must be signed in to change notification settings

AniketDhar/file_system_watcher

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

File System Watcher

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.

Features

  • 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 the onFileEvent() 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 (via Config.h and Config.cpp).

  • Test Suite:
    Includes unit tests (with a separate CMake configuration in the tests/ folder) for FileWatcher functionality and observer interactions.

Folder Structure

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.

Build Instructions

This project uses CMake for building. Ensure you have CMake (minimum version 3.10) and a C++17 compatible compiler installed.

Clone the Repository:

git clone <repository_url>
cd file_system_watcher

Create a Build Directory:

It’s best practice to build in a separate directory:

mkdir build
cd build

Generate Build Files:

Run CMake from the build directory:

cmake ..

Compile the Project:

make

This will build the filesystem_watcher executable.

(Optional) Build Tests:

If tests are enabled, you can build and run them:

ctest

Usage

The executable accepts command-line arguments to customize its behavior. The typical arguments are:

Directory Path:

The path of the directory to be monitored. If not specified, the current directory is watched.

Observer Type:

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.

Examples

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.

Observers

LoggerObserver

Purpose: Logs file system events (creation, deletion, modification, renames) to standard output.

Usage: Does not require additional parameters in its constructor.

BackupObserver

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.

Contributing

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.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Acknowledgments

Linux inotify Documentation for details on file system event monitoring.

Inspiration from modern C++ design patterns and examples for building modular applications.

About

Modern C++ application designed for Linux that uses the inotify API to monitor file system events in a specified directory

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published