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

Fix typo & warnings, listen for IN_ATTRIB on Linux #20

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
27 changes: 21 additions & 6 deletions FileWatch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@

#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#ifndef NOMINMAX
#define NOMINMAX
#endif // NOMINMAX
#define _UNICODE
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <tchar.h>
#include <Pathcch.h>
#include <shlwapi.h>
#endif // WIN32

Expand Down Expand Up @@ -88,9 +90,9 @@ namespace filewatch {
FileWatch(T path, UnderpinningRegex pattern, std::function<void(const T& file, const Event event_type)> callback) :
_path(path),
_pattern(pattern),
_callback(callback),
_directory(get_directory(path))
_callback(callback)
{
_directory = get_directory(path);
init();
}

Expand Down Expand Up @@ -183,7 +185,7 @@ namespace filewatch {

FolderInfo _directory;

const std::uint32_t _listen_filters = IN_MODIFY | IN_CREATE | IN_DELETE;
const std::uint32_t _listen_filters = IN_MODIFY | IN_CREATE | IN_DELETE | IN_ATTRIB;

const static std::size_t event_size = (sizeof(struct inotify_event));
#endif // __unix__
Expand Down Expand Up @@ -281,7 +283,11 @@ namespace filewatch {
#ifdef _WIN32
HANDLE get_directory(const T& path)
{
#if defined(UNICODE) || defined(_UNICODE)
auto file_info = GetFileAttributesW(path.c_str());
#else
auto file_info = GetFileAttributes(path.c_str());
#endif

if (file_info == INVALID_FILE_ATTRIBUTES)
{
Expand All @@ -302,7 +308,11 @@ namespace filewatch {
}
}();

#if defined(UNICODE) || defined(_UNICODE)
HANDLE directory = ::CreateFileW(
#else
HANDLE directory = ::CreateFile(
#endif
watch_path.c_str(), // pointer to the file name
FILE_LIST_DIRECTORY, // access (read/write) mode
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, // share mode
Expand All @@ -321,7 +331,7 @@ namespace filewatch {
{
std::vector<BYTE> buffer(_buffer_size);
DWORD bytes_returned = 0;
OVERLAPPED overlapped_buffer{ 0 };
OVERLAPPED overlapped_buffer{ };

overlapped_buffer.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (!overlapped_buffer.hEvent) {
Expand Down Expand Up @@ -433,7 +443,7 @@ namespace filewatch {
}
}();

const auto watch = inotify_add_watch(folder, watch_path.c_str(), IN_MODIFY | IN_CREATE | IN_DELETE);
const auto watch = inotify_add_watch(folder, watch_path.c_str(), listen_filters);
if (watch < 0)
{
throw std::system_error(errno, std::system_category());
Expand Down Expand Up @@ -473,6 +483,11 @@ namespace filewatch {
{
parsed_information.emplace_back(T{ changed_file }, Event::modified);
}
else if (event->mask & IN_ATTRIB)
{
// touch, for example, only generates ATTRIB, not MODIFY
parsed_information.emplace_back(T{ changed_file }, Event::modified);
}
}
}
i += event_size + event->len;
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ filewatch::FileWatch<std::wstring> watch(
case filewatch::Event::renamed_old:
std::cout << "The file was renamed and this is the old name." << '\n';
break;
case filewatch::ChangeType::renamed_new:
case filewatch::Event::renamed_new:
std::cout << "The file was renamed and this is the new name." << '\n';
break;
};
Expand Down