Skip to content

Commit

Permalink
Finish file watcher interface
Browse files Browse the repository at this point in the history
  • Loading branch information
sbusch42 committed Nov 25, 2018
1 parent 89e4dd8 commit a8b0d3d
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 37 deletions.
13 changes: 7 additions & 6 deletions source/cppfs/include/cppfs/AbstractFileWatcherBackend.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace cppfs
{


class FileHandle;
class FileWatcher;
class AbstractFileSystem;

Expand Down Expand Up @@ -50,12 +51,12 @@ class CPPFS_API AbstractFileWatcherBackend
* @brief
* Watch file handle
*
* @param[in] path
* File path
* @param[in] fileHandle
* File handle
* @param[in] mode
* Watch mode (combination of FileEvent values)
*/
virtual void add(const std::string & path, unsigned int mode) = 0;
virtual void add(const FileHandle & fileHandle, unsigned int mode) = 0;

/**
* @brief
Expand All @@ -74,12 +75,12 @@ class CPPFS_API AbstractFileWatcherBackend
* @brief
* Called on each file event
*
* @param[in] path
* Path to file or directory
* @param[in] fileHandle
* File handle
* @param[in] event
* Type of event that has occured
*/
void onFileEvent(const std::string & path, FileEvent event);
void onFileEvent(FileHandle & fileHandle, FileEvent event);


protected:
Expand Down
6 changes: 3 additions & 3 deletions source/cppfs/include/cppfs/FileWatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,16 +188,16 @@ class CPPFS_API FileWatcher
* @brief
* Called on file event
*
* @param[in] path
* Path to file or directory
* @param[in] fileHandle
* File handle
* @param[in] event
* Type of event that has occured
*
* @remarks
* The default implementation checks the event and calls
* the registered callback functions.
*/
virtual void onFileEvent(const std::string & path, FileEvent event);
virtual void onFileEvent(FileHandle & fileHandle, FileEvent event);


protected:
Expand Down
2 changes: 1 addition & 1 deletion source/cppfs/include/cppfs/null/NullFileWatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class CPPFS_API NullFileWatcher : public AbstractFileWatcherBackend

// Virtual AbstractFileWatcherBackend functions
virtual AbstractFileSystem * fs() const override;
virtual void add(const std::string & path, unsigned int mode) override;
virtual void add(const FileHandle & fileHandle, unsigned int mode) override;
virtual void watch() override;


Expand Down
6 changes: 3 additions & 3 deletions source/cppfs/include/cppfs/posix/LocalFileWatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


#include <memory>
#include <vector>
#include <map>

#include <cppfs/AbstractFileWatcherBackend.h>

Expand Down Expand Up @@ -41,14 +41,14 @@ class CPPFS_API LocalFileWatcher : public AbstractFileWatcherBackend

// Virtual AbstractFileWatcherBackend functions
virtual AbstractFileSystem * fs() const override;
virtual void add(const std::string & path, unsigned int mode) override;
virtual void add(const FileHandle & fileHandle, unsigned int mode) override;
virtual void watch() override;


protected:
std::shared_ptr<LocalFileSystem> m_fs; ///< File system that created this watcher
int m_inotify; ///< File handle for the inotify instance
std::vector<int> m_watchers; ///< List of watcher handles
std::map<int, FileHandle> m_watchers; ///< Map of watch handle -> file handle
};


Expand Down
2 changes: 1 addition & 1 deletion source/cppfs/include/cppfs/windows/LocalFileWatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class CPPFS_API LocalFileWatcher : public AbstractFileWatcherBackend

// Virtual AbstractFileWatcherBackend functions
virtual AbstractFileSystem * fs() const override;
virtual void add(const std::string & path, unsigned int mode) override;
virtual void add(const FileHandle & fileHandle, unsigned int mode) override;
virtual void watch() override;


Expand Down
4 changes: 2 additions & 2 deletions source/cppfs/source/AbstractFileWatcherBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ AbstractFileWatcherBackend::~AbstractFileWatcherBackend()
{
}

void AbstractFileWatcherBackend::onFileEvent(const std::string & path, FileEvent event)
void AbstractFileWatcherBackend::onFileEvent(FileHandle & fileHandle, FileEvent event)
{
m_fileWatcher.onFileEvent(path, event);
m_fileWatcher.onFileEvent(fileHandle, event);
}


Expand Down
12 changes: 3 additions & 9 deletions source/cppfs/source/FileWatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ void FileWatcher::add(const FileHandle & fileHandle, unsigned int mode)
}

// Add file to watcher
m_backend->add(fileHandle.path(), mode);
m_backend->add(fileHandle, mode);
}

void FileWatcher::addHandler(FileEventHandler * eventHandler)
Expand Down Expand Up @@ -105,17 +105,11 @@ void FileWatcher::watch()
m_backend->watch();
}

void FileWatcher::onFileEvent(const std::string & path, FileEvent event)
void FileWatcher::onFileEvent(FileHandle & fileHandle, FileEvent event)
{
// [TODO] Pass FileHandle instead of string
// [TODO] Make sure that path is fully qualified

// Open file
FileHandle fh = fs::open(path);

// Call file event handlers
for (auto * eventHandler : m_eventHandlers) {
eventHandler->onFileEvent(fh, event);
eventHandler->onFileEvent(fileHandle, event);
}
}

Expand Down
2 changes: 1 addition & 1 deletion source/cppfs/source/null/NullFileWatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ AbstractFileSystem * NullFileWatcher::fs() const
return m_fs.get();
}

void NullFileWatcher::add(const std::string &, unsigned int)
void NullFileWatcher::add(const FileHandle &, unsigned int)
{
}

Expand Down
22 changes: 12 additions & 10 deletions source/cppfs/source/posix/LocalFileWatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include <cppfs/cppfs.h>
#include <cppfs/FilePath.h>
#include <cppfs/FileHandle.h>
#include <cppfs/FileWatcher.h>
#include <cppfs/posix/LocalFileSystem.h>
#include <cppfs/posix/LocalFileIterator.h>
Expand All @@ -28,8 +29,8 @@ LocalFileWatcher::LocalFileWatcher(FileWatcher & fileWatcher, std::shared_ptr<Lo
LocalFileWatcher::~LocalFileWatcher()
{
// Close watch handles
for (int handle : m_watchers) {
inotify_rm_watch(m_inotify, handle);
for (auto it : m_watchers) {
inotify_rm_watch(m_inotify, it.first);
}

// Close inotify instance
Expand All @@ -42,7 +43,7 @@ AbstractFileSystem * LocalFileWatcher::fs() const
return static_cast<AbstractFileSystem *>(m_fs.get());
}

void LocalFileWatcher::add(const std::string & path, unsigned int mode)
void LocalFileWatcher::add(const FileHandle & fileHandle, unsigned int mode)
{
// Get watch mode
uint32_t flags = 0;
Expand All @@ -51,13 +52,13 @@ void LocalFileWatcher::add(const std::string & path, unsigned int mode)
if (mode & FileModified) flags |= IN_MODIFY;

// Create watcher
int handle = inotify_add_watch(m_inotify, path.c_str(), flags);
int handle = inotify_add_watch(m_inotify, fileHandle.path().c_str(), flags);
if (handle < 0) {
return;
}

// Add watcher handle
m_watchers.push_back(handle);
// Associate watcher handle with file handle
m_watchers[handle] = fileHandle;
}

void LocalFileWatcher::watch()
Expand All @@ -78,17 +79,18 @@ void LocalFileWatcher::watch()
// Get event
auto * event = reinterpret_cast<inotify_event *>(&buffer.data()[i]);
if (event->len) {
// Get path
std::string path = event->name;

// Get event
FileEvent eventType = (FileEvent)0;
if (event->mask & IN_CREATE) eventType = FileCreated;
else if (event->mask & IN_DELETE) eventType = FileRemoved;
else if (event->mask & IN_MODIFY) eventType = FileModified;

// Get file handle
std::string path = event->name;
FileHandle fh = m_watchers[event->wd].open(path);

// Invoke callback function
onFileEvent(path, eventType);
onFileEvent(fh, eventType);
}

// Next event
Expand Down
2 changes: 1 addition & 1 deletion source/cppfs/source/windows/LocalFileWatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ AbstractFileSystem * LocalFileWatcher::fs() const
return static_cast<AbstractFileSystem *>(m_fs.get());
}

void LocalFileWatcher::add(const std::string &, unsigned int)
void LocalFileWatcher::add(const FileHandle &, unsigned int)
{
// [TODO] Implement for Windows
}
Expand Down

0 comments on commit a8b0d3d

Please sign in to comment.