Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File system watcher #20

Merged
merged 28 commits into from
Jan 6, 2019
Merged
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
9e2a31b
Fix comments in FileIterator
sbusch42 Nov 24, 2018
0b91777
Make filesystem accessible through interfaces (FileHandle, FileIterator)
sbusch42 Nov 24, 2018
299d797
Create structure for file system watchers and first implementation fo…
sbusch42 Nov 24, 2018
9d08a58
Remove clone() from file watcher interface
sbusch42 Nov 24, 2018
89e4dd8
Create file event handlers (analog to FileVisitor)
sbusch42 Nov 24, 2018
a8b0d3d
Finish file watcher interface
sbusch42 Nov 25, 2018
ea51205
Add mode to watch files/directories recursively
sbusch42 Nov 25, 2018
85a6575
Clean up file watcher interface
sbusch42 Nov 25, 2018
2f068b1
Clean up file watcher interface
sbusch42 Nov 25, 2018
79b88ec
Create convenience function for creating a watcher on a single file/d…
sbusch42 Nov 25, 2018
2fad581
Fix FileHandle::watch() in case of other file systems than the system…
sbusch42 Nov 25, 2018
773cb63
Remove bit field logic where not necessary
sbusch42 Nov 25, 2018
9d2cf4e
Fix handling empty name field (happens when the watched object itself…
sbusch42 Nov 25, 2018
ca00053
Adds windows file watcher, updates watch function prototype
robiwano Nov 26, 2018
022be82
Windows add of folder to watch is now thread safe
robiwano Nov 27, 2018
f24bba7
Merge pull request #22 from robiwano/watcher
sbusch42 Dec 3, 2018
e363bb0
Small style related changes
sbusch42 Dec 3, 2018
f8b16fd
cppfs-watch: As file watchers are only available for the local file s…
sbusch42 Jan 6, 2019
f3c27ff
cppfs-watch: use single threading in example
sbusch42 Jan 6, 2019
d8cf3ce
Clarify in interface and documentation that only directories can be w…
sbusch42 Jan 6, 2019
60c8fb1
Remove null implementation of FileWatcher, check for backend nullptr …
sbusch42 Jan 6, 2019
b45312a
Move LocalFileWatcher to linux directory, as it is specific for Linux
sbusch42 Jan 6, 2019
b55db4a
Implement new event type FileAttribChanged
sbusch42 Jan 6, 2019
4ff0fa4
cppfs-watch: allow multiple paths to be specified and add --recursive…
sbusch42 Jan 6, 2019
cd2fabd
Fix compilation on Windows
sbusch42 Jan 6, 2019
dc0ebb5
Windows backend: add handling renaming of files and creating and
sbusch42 Jan 6, 2019
68c41aa
Review, clean up and comment Windows file watcher implementation
sbusch42 Jan 6, 2019
e2f78b6
Adjust contributors list
sbusch42 Jan 6, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions AUTHORS
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@

Stefan Buschmann <stefan.buschmann@hpi.de> <buschmann@cginternals.com>
Daniel Limberger <daniel.limberger@hpi.de> <limberger@cginternals.com>
Daniel Limberger <daniel.limberger@hpi.de> <limberger@cginternals.com>
Willy Scheibel <willy.scheibel@hpi.de> <scheibel@cginternals.com>

Thanks to all Contributors

Thanks to all Contributors:

Robert Bielik (https://github.com/robiwano)
30 changes: 29 additions & 1 deletion source/cppfs/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -56,9 +56,13 @@ set(headers
${include_path}/FileIterator.h
${include_path}/FileVisitor.h
${include_path}/FunctionalFileVisitor.h
${include_path}/FileWatcher.h
${include_path}/FileEventHandler.h
${include_path}/FunctionalFileEventHandler.h
${include_path}/AbstractFileSystem.h
${include_path}/AbstractFileHandleBackend.h
${include_path}/AbstractFileIteratorBackend.h
${include_path}/AbstractFileWatcherBackend.h
${include_path}/InputStream.h
${include_path}/OutputStream.h
${include_path}/LoginCredentials.h
@@ -81,9 +85,13 @@ set(sources
${source_path}/FileIterator.cpp
${source_path}/FileVisitor.cpp
${source_path}/FunctionalFileVisitor.cpp
${source_path}/FileWatcher.cpp
${source_path}/FileEventHandler.cpp
${source_path}/FunctionalFileEventHandler.cpp
${source_path}/AbstractFileSystem.cpp
${source_path}/AbstractFileHandleBackend.cpp
${source_path}/AbstractFileIteratorBackend.cpp
${source_path}/AbstractFileWatcherBackend.cpp
${source_path}/InputStream.cpp
${source_path}/OutputStream.cpp
${source_path}/LoginCredentials.cpp
@@ -98,6 +106,26 @@ set(sources
${source_path}/${localfs}/LocalFileIterator.cpp
)

if("${CMAKE_SYSTEM_NAME}" MATCHES "Linux")
set(headers ${headers}
${include_path}/linux/LocalFileWatcher.h
)

set(sources ${sources}
${source_path}/linux/LocalFileWatcher.cpp
)
endif()

if("${CMAKE_SYSTEM_NAME}" MATCHES "Windows")
set(headers ${headers}
${include_path}/windows/LocalFileWatcher.h
)

set(sources ${sources}
${source_path}/windows/LocalFileWatcher.cpp
)
endif()

if (OPTION_BUILD_SSH_BACKEND)
set(headers ${headers}
${include_path}/ssh/SshFileSystem.h
@@ -114,7 +142,7 @@ if (OPTION_BUILD_SSH_BACKEND)
${source_path}/ssh/SshInputStreamBuffer.cpp
${source_path}/ssh/SshOutputStreamBuffer.cpp
)
endif ()
endif()

# Group source files
set(header_group "Header Files (API)")
15 changes: 15 additions & 0 deletions source/cppfs/include/cppfs/AbstractFileSystem.h
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@
#pragma once


#include <memory>
#include <string>

#include <cppfs/cppfs_api.h>
@@ -12,6 +13,8 @@ namespace cppfs


class FileHandle;
class FileWatcher;
class AbstractFileWatcherBackend;


/**
@@ -50,6 +53,18 @@ class CPPFS_API AbstractFileSystem
* Path to file or directory
*/
virtual FileHandle open(std::string && path) = 0;

/**
* @brief
* Create a watcher for the file system
*
* @param[in] fileWatcher
* File watcher that owns the backend
*
* @return
* Watcher backend (must NOT be null!)
*/
virtual std::unique_ptr<AbstractFileWatcherBackend> createFileWatcher(FileWatcher & fileWatcher) = 0;
};


101 changes: 101 additions & 0 deletions source/cppfs/include/cppfs/AbstractFileWatcherBackend.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@

#pragma once


#include <memory>

#include <cppfs/cppfs.h>


namespace cppfs
{


class FileHandle;
class FileWatcher;
class AbstractFileSystem;


/**
* @brief
* Interface for file watchers
*/
class CPPFS_API AbstractFileWatcherBackend
{
friend class FileWatcher;


public:
/**
* @brief
* Constructor
*
* @param[in] fileWatcher
* File watcher that owns the backend (must NOT be null!)
*/
AbstractFileWatcherBackend(FileWatcher * fileWatcher);

/**
* @brief
* Destructor
*/
virtual ~AbstractFileWatcherBackend();

/**
* @brief
* Get file system
*
* @return
* File system (must NOT be null)
*/
virtual AbstractFileSystem * fs() const = 0;

/**
* @brief
* Watch directory
*
* @param[in] dir
* Handle to directory that shall be watched
* @param[in] events
* Events that are watched (combination of FileEvent values)
* @param[in] recursive
* Watch file system recursively?
*/
virtual void add(FileHandle & dir, unsigned int events, RecursiveMode recursive) = 0;

/**
* @brief
* Start watching files
*
* @param[in] timeout
* Timeout value in milliseconds. If less than zero, timeout is infinite and the function blocks.
*
* @remarks
* This function shall watch the file system and block until one or more
* events have occured, or if the timeout has been exceeded (timeout >= 0).
* For each event, onFileEvent has to be called with the type of the event and
* a file handle to the file or directory. After all events have been
* processed, the function shall return.
*/
virtual void watch(int timeout) = 0;


protected:
/**
* @brief
* Called on each file event
*
* @param[in] fh
* File handle
* @param[in] event
* Type of event that has occured
*/
void onFileEvent(FileHandle & fh, FileEvent event);


protected:
FileWatcher * m_fileWatcher; ///< File watcher that owns the backend (never null)
};


} // namespace cppfs
91 changes: 91 additions & 0 deletions source/cppfs/include/cppfs/FileEventHandler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@

#pragma once


#include <cppfs/cppfs.h>


namespace cppfs
{


class FileHandle;


/**
* @brief
* Handler that is informed about file system events
*
* @remarks
* Use FileWatcher to register file event handlers.
*/
class CPPFS_API FileEventHandler
{
friend class FileWatcher;


public:
/**
* @brief
* Constructor
*/
FileEventHandler();

/**
* @brief
* Destructor
*/
virtual ~FileEventHandler();


protected:
/**
* @brief
* Called on file event
*
* @param[in] fh
* Handle to file or directory
* @param[in] event
* Type of event that has occured
*/
virtual void onFileEvent(FileHandle & fh, FileEvent event);

/**
* @brief
* Called when a file or directory has been created
*
* @param[in] fh
* Handle to file or directory
*/
virtual void onFileCreated(FileHandle & fh);

/**
* @brief
* Called when a file or directory has been removed
*
* @param[in] fh
* Handle to file or directory
*/
virtual void onFileRemoved(FileHandle & fh);

/**
* @brief
* Called when a file or directory has been modified
*
* @param[in] fh
* Handle to file or directory
*/
virtual void onFileModified(FileHandle & fh);

/**
* @brief
* Called when file attributes have been modified
*
* @param[in] fh
* Handle to file or directory
*/
virtual void onFileAttrChanged(FileHandle & fh);
};


} // namespace cppfs
33 changes: 32 additions & 1 deletion source/cppfs/include/cppfs/FileHandle.h
Original file line number Diff line number Diff line change
@@ -8,17 +8,19 @@
#include <vector>
#include <string>

#include <cppfs/cppfs_api.h>
#include <cppfs/cppfs.h>
#include <cppfs/AbstractFileHandleBackend.h>


namespace cppfs
{


class AbstractFileSystem;
class FileIterator;
class FileVisitor;
class Tree;
class FileWatcher;


/**
@@ -104,6 +106,15 @@ class CPPFS_API FileHandle
*/
FileHandle & operator=(FileHandle && fileHandle);

/**
* @brief
* Get file system
*
* @return
* File system (can be null)
*/
AbstractFileSystem * fs() const;

/**
* @brief
* Get path
@@ -478,6 +489,26 @@ class CPPFS_API FileHandle
*/
bool remove();

/**
* @brief
* Create file system watcher for this file handle
*
* @param[in] events
* Events that are watched (combination of FileEvent values)
* @param[in] recursive
* Watch file system recursively?
*
* @return
* File watcher
*
* @remarks
* This is a shortcut for creating a FileWatcher and adding file handles to watch.
* It will only work if the file handle points to a valid directory.
* To watch more than one directory at a time, use FileWatcher and add.
* Avoid creating more than one FileWatcher, as OS limits can be reached.
*/
FileWatcher watch(unsigned int events = FileCreated | FileRemoved | FileModified | FileAttrChanged, RecursiveMode recursive = Recursive);

/**
* @brief
* Create input stream to read from the file
Loading