Skip to content

Commit

Permalink
Merge pull request #169 from solarispika/fix/macos-path-handling
Browse files Browse the repository at this point in the history
fix/macos path handling
  • Loading branch information
SpartanJ authored Nov 11, 2023
2 parents 9f6caa0 + 5ffeeef commit aae269e
Show file tree
Hide file tree
Showing 12 changed files with 69 additions and 51 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ target_compile_definitions(efsw PRIVATE $<IF:$<CONFIG:Debug>,DEBUG,NDEBUG>)
if(APPLE)
set(MAC_LIBS "-framework CoreFoundation" "-framework CoreServices")
target_link_libraries(efsw PRIVATE ${MAC_LIBS})
if(BUILD_STATIC_LIBS)
target_link_libraries(efsw-static PRIVATE ${MAC_LIBS})
endif()
elseif(NOT(${CMAKE_SYSTEM_NAME} MATCHES "Haiku") AND NOT WIN32)
target_link_libraries(efsw PRIVATE Threads::Threads)
endif()
Expand Down
30 changes: 24 additions & 6 deletions src/efsw/FileSystem.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <efsw/FileSystem.hpp>
#include <efsw/platform/platformimpl.hpp>
#include <cstring>

#if EFSW_OS == EFSW_OS_MACOSX
#include <CoreFoundation/CoreFoundation.h>
Expand Down Expand Up @@ -91,13 +92,30 @@ std::string FileSystem::precomposeFileName( const std::string& name ) {

CFStringNormalize( cfMutable, kCFStringNormalizationFormC );

char c_str[255 + 1];
CFStringGetCString( cfMutable, c_str, sizeof( c_str ) - 1, kCFStringEncodingUTF8 );

CFRelease( cfStringRef );
CFRelease( cfMutable );
const char* c_str = CFStringGetCStringPtr( cfMutable, kCFStringEncodingUTF8 );
if ( c_str != NULL ) {
std::string result( c_str );
CFRelease( cfStringRef );
CFRelease( cfMutable );
return result;
}
CFIndex length = CFStringGetLength( cfMutable );
CFIndex maxSize = CFStringGetMaximumSizeForEncoding( length, kCFStringEncodingUTF8 );
if ( maxSize == kCFNotFound ) {
CFRelease( cfStringRef );
CFRelease( cfMutable );
return std::string();
}

return std::string( c_str );
std::string result( maxSize, '\0' );
if ( CFStringGetCString( cfMutable, &result[0], result.size(), kCFStringEncodingUTF8 ) ) {
result.resize( std::strlen( result.c_str() ) );
CFRelease( cfStringRef );
CFRelease( cfMutable );
} else {
result.clear();
}
return result;
#else
return name;
#endif
Expand Down
14 changes: 7 additions & 7 deletions src/efsw/FileWatcherFSEvents.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,23 +56,23 @@ class FileWatcherFSEvents : public FileWatcherImpl {
/// Add a directory watch
/// On error returns WatchID with Error type.
WatchID addWatch( const std::string& directory, FileWatchListener* watcher, bool recursive,
const std::vector<WatcherOption> &options );
const std::vector<WatcherOption> &options ) override;

/// Remove a directory watch. This is a brute force lazy search O(nlogn).
void removeWatch( const std::string& directory );
void removeWatch( const std::string& directory ) override;

/// Remove a directory watch. This is a map lookup O(logn).
void removeWatch( WatchID watchid );
void removeWatch( WatchID watchid ) override;

/// Updates the watcher. Must be called often.
void watch();
void watch() override;

/// Handles the action
void handleAction( Watcher* watch, const std::string& filename, unsigned long action,
std::string oldFilename = "" );
std::string oldFilename = "" ) override;

/// @return Returns a list of the directories that are being watched
std::vector<std::string> directories();
std::vector<std::string> directories() override;

protected:
static void FSEventCallback( ConstFSEventStreamRef streamRef, void* userData, size_t numEvents,
Expand All @@ -87,7 +87,7 @@ class FileWatcherFSEvents : public FileWatcherImpl {

Mutex mWatchesLock;

bool pathInWatches( const std::string& path );
bool pathInWatches( const std::string& path ) override;

std::mutex mWatchesMutex;
std::condition_variable mWatchCond;
Expand Down
14 changes: 7 additions & 7 deletions src/efsw/FileWatcherGeneric.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,23 @@ class FileWatcherGeneric : public FileWatcherImpl {
/// Add a directory watch
/// On error returns WatchID with Error type.
WatchID addWatch( const std::string& directory, FileWatchListener* watcher, bool recursive,
const std::vector<WatcherOption> &options );
const std::vector<WatcherOption> &options ) override;

/// Remove a directory watch. This is a brute force lazy search O(nlogn).
void removeWatch( const std::string& directory );
void removeWatch( const std::string& directory ) override;

/// Remove a directory watch. This is a map lookup O(logn).
void removeWatch( WatchID watchid );
void removeWatch( WatchID watchid ) override;

/// Updates the watcher. Must be called often.
void watch();
void watch() override;

/// Handles the action
void handleAction( Watcher* watch, const std::string& filename, unsigned long action,
std::string oldFilename = "" );
std::string oldFilename = "" ) override;

/// @return Returns a list of the directories that are being watched
std::vector<std::string> directories();
std::vector<std::string> directories() override;

protected:
Thread* mThread;
Expand All @@ -50,7 +50,7 @@ class FileWatcherGeneric : public FileWatcherImpl {

Mutex mWatchesLock;

bool pathInWatches( const std::string& path );
bool pathInWatches( const std::string& path ) override;

private:
void run();
Expand Down
14 changes: 7 additions & 7 deletions src/efsw/FileWatcherInotify.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,23 @@ class FileWatcherInotify : public FileWatcherImpl {
/// Add a directory watch
/// On error returns WatchID with Error type.
WatchID addWatch( const std::string& directory, FileWatchListener* watcher, bool recursive,
const std::vector<WatcherOption>& options );
const std::vector<WatcherOption>& options ) override;

/// Remove a directory watch. This is a brute force lazy search O(nlogn).
void removeWatch( const std::string& directory );
void removeWatch( const std::string& directory ) override;

/// Remove a directory watch. This is a map lookup O(logn).
void removeWatch( WatchID watchid );
void removeWatch( WatchID watchid ) override;

/// Updates the watcher. Must be called often.
void watch();
void watch() override;

/// Handles the action
void handleAction( Watcher* watch, const std::string& filename, unsigned long action,
std::string oldFilename = "" );
std::string oldFilename = "" ) override;

/// @return Returns a list of the directories that are being watched
std::vector<std::string> directories();
std::vector<std::string> directories() override;

protected:
/// Map of WatchID to WatchStruct pointers
Expand All @@ -67,7 +67,7 @@ class FileWatcherInotify : public FileWatcherImpl {
WatchID addWatch( const std::string& directory, FileWatchListener* watcher, bool recursive,
WatcherInotify* parent = NULL );

bool pathInWatches( const std::string& path );
bool pathInWatches( const std::string& path ) override;

private:
void run();
Expand Down
14 changes: 7 additions & 7 deletions src/efsw/FileWatcherKqueue.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,23 @@ class FileWatcherKqueue : public FileWatcherImpl {
/// Add a directory watch
/// On error returns WatchID with Error type.
WatchID addWatch( const std::string& directory, FileWatchListener* watcher, bool recursive,
const std::vector<WatcherOption> &options );
const std::vector<WatcherOption> &options ) override;

/// Remove a directory watch. This is a brute force lazy search O(nlogn).
void removeWatch( const std::string& directory );
void removeWatch( const std::string& directory ) override;

/// Remove a directory watch. This is a map lookup O(logn).
void removeWatch( WatchID watchid );
void removeWatch( WatchID watchid ) override;

/// Updates the watcher. Must be called often.
void watch();
void watch() override;

/// Handles the action
void handleAction( Watcher* watch, const std::string& filename, unsigned long action,
std::string oldFilename = "" );
std::string oldFilename = "" ) override;

/// @return Returns a list of the directories that are being watched
std::vector<std::string> directories();
std::vector<std::string> directories() override;

protected:
/// Map of WatchID to WatchStruct pointers
Expand All @@ -62,7 +62,7 @@ class FileWatcherKqueue : public FileWatcherImpl {

bool isAddingWatcher() const;

bool pathInWatches( const std::string& path );
bool pathInWatches( const std::string& path ) override;

void addFD();

Expand Down
14 changes: 7 additions & 7 deletions src/efsw/FileWatcherWin32.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,23 @@ class FileWatcherWin32 : public FileWatcherImpl {
/// Add a directory watch
/// On error returns WatchID with Error type.
WatchID addWatch( const std::string& directory, FileWatchListener* watcher, bool recursive,
const std::vector<WatcherOption> &options );
const std::vector<WatcherOption> &options ) override;

/// Remove a directory watch. This is a brute force lazy search O(nlogn).
void removeWatch( const std::string& directory );
void removeWatch( const std::string& directory ) override;

/// Remove a directory watch. This is a map lookup O(logn).
void removeWatch( WatchID watchid );
void removeWatch( WatchID watchid ) override;

/// Updates the watcher. Must be called often.
void watch();
void watch() override;

/// Handles the action
void handleAction( Watcher* watch, const std::string& filename, unsigned long action,
std::string oldFilename = "" );
std::string oldFilename = "" ) override;

/// @return Returns a list of the directories that are being watched
std::vector<std::string> directories();
std::vector<std::string> directories() override;

protected:
HANDLE mIOCP;
Expand All @@ -53,7 +53,7 @@ class FileWatcherWin32 : public FileWatcherImpl {
Thread* mThread;
Mutex mWatchesLock;

bool pathInWatches( const std::string& path );
bool pathInWatches( const std::string& path ) override;

/// Remove all directory watches.
void removeAllWatches();
Expand Down
2 changes: 1 addition & 1 deletion src/efsw/WatcherGeneric.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class WatcherGeneric : public Watcher {

~WatcherGeneric();

void watch();
void watch() override;

void watchDir( std::string dir );

Expand Down
8 changes: 2 additions & 6 deletions src/efsw/WatcherKqueue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,6 @@ void WatcherKqueue::moveDirectory( std::string oldPath, std::string newPath, boo

WatchID WatcherKqueue::addWatch( const std::string& directory, FileWatchListener* watcher,
bool recursive, WatcherKqueue* parent ) {
static long s_fc = 0;
static bool s_ug = false;

std::string dir( directory );
Expand Down Expand Up @@ -478,8 +477,6 @@ WatchID WatcherKqueue::addWatch( const std::string& directory, FileWatchListener

watch->addAll();

s_fc++;

// if failed to open the directory... erase the watcher
if ( !watch->initOK() ) {
int le = watch->lastErrno();
Expand All @@ -502,9 +499,8 @@ WatchID WatcherKqueue::addWatch( const std::string& directory, FileWatchListener
}
} else {
if ( !s_ug ) {
efDEBUG( "Started using WatcherGeneric, reached file descriptors limit: %ld. Folders "
"added: %ld\n",
mWatcher->mFileDescriptorCount, s_fc );
efDEBUG( "Started using WatcherGeneric, reached file descriptors limit: %ld.\n",
mWatcher->mFileDescriptorCount );
s_ug = true;
}

Expand Down
2 changes: 1 addition & 1 deletion src/efsw/WatcherKqueue.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class WatcherKqueue : public Watcher {

WatchID watchingDirectory( std::string dir );

void watch();
void watch() override;

WatchID addWatch( const std::string& directory, FileWatchListener* watcher, bool recursive,
WatcherKqueue* parent );
Expand Down
3 changes: 2 additions & 1 deletion src/test/efsw-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>

Expand Down Expand Up @@ -74,7 +75,7 @@ efsw_watchid handleWatchID( efsw_watchid watchid ) {
break;
}
default: {
printf( "Added WatchID: %d\n", watchid );
printf( "Added WatchID: %ld\n", watchid );
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/efsw-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class UpdateListener : public efsw::FileWatchListener {

void handleFileAction( efsw::WatchID watchid, const std::string& dir,
const std::string& filename, efsw::Action action,
std::string oldFilename = "" ) {
std::string oldFilename = "" ) override {
std::cout << "Watch ID " << watchid << " DIR ("
<< dir + ") FILE (" +
( oldFilename.empty() ? "" : "from file " + oldFilename + " to " ) +
Expand Down

0 comments on commit aae269e

Please sign in to comment.