Skip to content

Commit

Permalink
Refactor path handling for conditional symlink handling at all levels…
Browse files Browse the repository at this point in the history
…, not just at root path level
  • Loading branch information
emcrisostomo committed Jan 1, 2025
1 parent d7dfc6c commit 07cb12f
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 3 deletions.
2 changes: 1 addition & 1 deletion libfswatch/src/libfswatch/c++/kqueue_monitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ namespace fsw

// TODO: C++17 doesn't provide a single, comparable, type to represent st_mode
struct stat fd_stat;
if (!lstat_path(path, fd_stat)) return;
if (!stat_path(path, fd_stat, follow_symlinks)) return;
if (!add_watch(path, fd_stat)) return;
if (!recursive || !is_dir) return;

Expand Down
18 changes: 17 additions & 1 deletion libfswatch/src/libfswatch/c++/path_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,30 @@ namespace fsw
return entries;
}

bool stat_path(const std::string& path, struct stat& fd_stat, bool follow_symlink)
{
if (follow_symlink)
{
if (lstat(path.c_str(), &fd_stat) == 0)
return true;
}
else
{
if (stat(path.c_str(), &fd_stat) == 0)
return true;
}

fsw_logf_perror(_("Cannot stat %s"), path.c_str());
return false;
}

bool stat_path(const std::string& path, struct stat& fd_stat)
{
if (stat(path.c_str(), &fd_stat) == 0)
return true;

fsw_logf_perror(_("Cannot stat %s"), path.c_str());
return false;

}

bool lstat_path(const std::string& path, struct stat& fd_stat)
Expand Down
14 changes: 14 additions & 0 deletions libfswatch/src/libfswatch/c++/path_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,19 @@ namespace fsw
* @return @c true if the function succeeds, @c false otherwise.
*/
bool stat_path(const std::string& path, struct stat& fd_stat);

/**
* @brief Wraps a @c stat(path, fd_stat) call or a @c lstat(path, fd_stat)
* call, depending on the value of @p follow_symlink. The function invokes
* @c perror() if it fails.
*
* @param path The path to @c stat() or @c lstat().
* @param fd_stat The @c stat structure where @c stat() or @c lstat() writes
* its results.
* @param follow_symlink @c true if the function should call @c lstat(),
* @c false if it should call @c stat().
* @return @c true if the function succeeds, @c false otherwise.
*/
bool stat_path(const std::string& path, struct stat& fd_stat, bool follow_symlink);
}
#endif /* FSW_PATH_UTILS_H */
2 changes: 1 addition & 1 deletion libfswatch/src/libfswatch/c++/poll_monitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ namespace fsw
// TODO: C++17 doesn't standardize access to ctime, so we need to keep
// using lstat for now.
struct stat fd_stat;
if (!lstat_path(path, fd_stat)) return;
if (!stat_path(path, fd_stat, follow_symlinks)) return;

if (!add_path(path, fd_stat, fn)) return;
if (!recursive) return;
Expand Down

0 comments on commit 07cb12f

Please sign in to comment.