Skip to content

Commit

Permalink
[DirectoryWatcher][NFC] clang-format
Browse files Browse the repository at this point in the history
  • Loading branch information
jkorous-apple committed Feb 20, 2019
1 parent f27d6c0 commit 67a5087
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 119 deletions.
14 changes: 8 additions & 6 deletions include/clang/DirectoryWatcher/DirectoryWatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,24 @@ class DirectoryWatcher {
llvm::sys::TimePoint<> ModTime;
};

typedef std::function<void(ArrayRef<Event> Events, bool isInitial)> EventReceiver;
typedef std::function<void(ArrayRef<Event> Events, bool isInitial)>
EventReceiver;

~DirectoryWatcher();

static std::unique_ptr<DirectoryWatcher>
create(StringRef Path, EventReceiver Receiver, bool waitInitialSync,
std::string &Error);
static std::unique_ptr<DirectoryWatcher> create(StringRef Path,
EventReceiver Receiver,
bool waitInitialSync,
std::string &Error);

private:
struct Implementation;
Implementation &Impl;

DirectoryWatcher();

DirectoryWatcher(const DirectoryWatcher&) = delete;
DirectoryWatcher &operator =(const DirectoryWatcher&) = delete;
DirectoryWatcher(const DirectoryWatcher &) = delete;
DirectoryWatcher &operator=(const DirectoryWatcher &) = delete;
};

} // namespace clang
Expand Down
29 changes: 14 additions & 15 deletions lib/DirectoryWatcher/DirectoryWatcher-linux.inc.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
//===----------------------------------------------------------------------===//

#include "llvm/Support/Errno.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/Mutex.h"
#include "llvm/Support/Path.h"
#include <sys/inotify.h>
#include <thread>
#include <unistd.h>
#include <sys/inotify.h>

namespace {

Expand All @@ -30,7 +30,8 @@ class EventQueue {

DirectoryWatcher::Event toDirEvent(const INotifyEvent &evt) {
llvm::sys::TimePoint<> modTime{};
if (evt.Status.hasValue()) modTime = evt.Status->getLastModificationTime();
if (evt.Status.hasValue())
modTime = evt.Status->getLastModificationTime();
return DirectoryWatcher::Event{evt.K, evt.Filename, modTime};
}

Expand Down Expand Up @@ -76,14 +77,12 @@ class EventQueue {
PendingEvents.clear();
}
};
} // namespace
} // namespace

struct DirectoryWatcher::Implementation {
bool initialize(StringRef Path, EventReceiver Receiver,
bool waitInitialSync, std::string &Error);
~Implementation() {
stopListening();
};
bool initialize(StringRef Path, EventReceiver Receiver, bool waitInitialSync,
std::string &Error);
~Implementation() { stopListening(); };

private:
int inotifyFD = -1;
Expand All @@ -93,8 +92,8 @@ struct DirectoryWatcher::Implementation {

static void runWatcher(std::string pathToWatch, int inotifyFD,
std::shared_ptr<EventQueue> evtQueue) {
#define EVT_BUF_LEN (30 * (sizeof(struct inotify_event) + NAME_MAX + 1))
char buf[EVT_BUF_LEN] __attribute__ ((aligned(8)));
#define EVT_BUF_LEN (30 * (sizeof(struct inotify_event) + NAME_MAX + 1))
char buf[EVT_BUF_LEN] __attribute__((aligned(8)));

while (1) {
ssize_t numRead = read(inotifyFD, buf, EVT_BUF_LEN);
Expand Down Expand Up @@ -155,16 +154,16 @@ bool DirectoryWatcher::Implementation::initialize(StringRef Path,
return true;
};

auto evtQueue = std::make_shared<EventQueue>(std::move(Receiver));
auto evtQueue = std::make_shared<EventQueue>(std::move(Receiver));

inotifyFD = inotify_init();
if (inotifyFD == -1)
return error("inotify_init failed");

std::string pathToWatch = Path;
int wd = inotify_add_watch(
inotifyFD, pathToWatch.c_str(),
IN_MOVED_TO | IN_DELETE | IN_MODIFY | IN_DELETE_SELF | IN_ONLYDIR);
int wd = inotify_add_watch(inotifyFD, pathToWatch.c_str(),
IN_MOVED_TO | IN_DELETE | IN_MODIFY |
IN_DELETE_SELF | IN_ONLYDIR);
if (wd == -1)
return error("inotify_add_watch failed");

Expand Down
84 changes: 42 additions & 42 deletions lib/DirectoryWatcher/DirectoryWatcher-mac.inc.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@
#include <CoreServices/CoreServices.h>

struct DirectoryWatcher::Implementation {
bool initialize(StringRef Path, EventReceiver Receiver,
bool waitInitialSync, std::string &Error);
~Implementation() {
stopFSEventStream();
};
bool initialize(StringRef Path, EventReceiver Receiver, bool waitInitialSync,
std::string &Error);
~Implementation() { stopFSEventStream(); };

private:
FSEventStreamRef EventStream = nullptr;

Expand All @@ -30,35 +29,35 @@ struct EventStreamContextData {
DirectoryWatcher::EventReceiver Receiver;
std::shared_ptr<DirectoryScan> InitialScan;

EventStreamContextData(std::string watchedPath, DirectoryWatcher::EventReceiver receiver,
EventStreamContextData(std::string watchedPath,
DirectoryWatcher::EventReceiver receiver,
std::shared_ptr<DirectoryScan> initialScanPtr)
: WatchedPath(std::move(watchedPath)),
Receiver(std::move(receiver)),
InitialScan(std::move(initialScanPtr)) {
}
: WatchedPath(std::move(watchedPath)), Receiver(std::move(receiver)),
InitialScan(std::move(initialScanPtr)) {}

static void dispose(const void *ctx) {
delete static_cast<const EventStreamContextData*>(ctx);
delete static_cast<const EventStreamContextData *>(ctx);
}
};
}
} // namespace

static void eventStreamCallback(
ConstFSEventStreamRef stream,
void *clientCallBackInfo,
size_t numEvents,
void *eventPaths,
const FSEventStreamEventFlags eventFlags[],
const FSEventStreamEventId eventIds[]) {
auto *ctx = static_cast<EventStreamContextData*>(clientCallBackInfo);
static void eventStreamCallback(ConstFSEventStreamRef stream,
void *clientCallBackInfo, size_t numEvents,
void *eventPaths,
const FSEventStreamEventFlags eventFlags[],
const FSEventStreamEventId eventIds[]) {
auto *ctx = static_cast<EventStreamContextData *>(clientCallBackInfo);

std::vector<DirectoryWatcher::Event> Events;
for (size_t i = 0; i < numEvents; ++i) {
StringRef path = ((const char **)eventPaths)[i];
const FSEventStreamEventFlags flags = eventFlags[i];
if (!(flags & kFSEventStreamEventFlagItemIsFile)) {
if ((flags & kFSEventStreamEventFlagItemRemoved) && path == ctx->WatchedPath) {
DirectoryWatcher::Event Evt{DirectoryWatcher::EventKind::DirectoryDeleted, path, llvm::sys::TimePoint<>{} };
if ((flags & kFSEventStreamEventFlagItemRemoved) &&
path == ctx->WatchedPath) {
DirectoryWatcher::Event Evt{
DirectoryWatcher::EventKind::DirectoryDeleted, path,
llvm::sys::TimePoint<>{}};
Events.push_back(Evt);
break;
}
Expand Down Expand Up @@ -113,15 +112,17 @@ static void eventStreamCallback(
}
}

bool DirectoryWatcher::Implementation::setupFSEventStream(StringRef path,
EventReceiver receiver,
dispatch_queue_t queue,
std::shared_ptr<DirectoryScan> initialScanPtr) {
bool DirectoryWatcher::Implementation::setupFSEventStream(
StringRef path, EventReceiver receiver, dispatch_queue_t queue,
std::shared_ptr<DirectoryScan> initialScanPtr) {
if (path.empty())
return true;

CFMutableArrayRef pathsToWatch = CFArrayCreateMutable(nullptr, 0, &kCFTypeArrayCallBacks);
CFStringRef cfPathStr = CFStringCreateWithBytes(nullptr, (const UInt8 *)path.data(), path.size(), kCFStringEncodingUTF8, false);
CFMutableArrayRef pathsToWatch =
CFArrayCreateMutable(nullptr, 0, &kCFTypeArrayCallBacks);
CFStringRef cfPathStr =
CFStringCreateWithBytes(nullptr, (const UInt8 *)path.data(), path.size(),
kCFStringEncodingUTF8, false);
CFArrayAppendValue(pathsToWatch, cfPathStr);
CFRelease(cfPathStr);
CFAbsoluteTime latency = 0.0; // Latency in seconds.
Expand All @@ -138,24 +139,19 @@ bool DirectoryWatcher::Implementation::setupFSEventStream(StringRef path,
realPath = path;
}

EventStreamContextData *ctxData =
new EventStreamContextData(std::move(realPath), std::move(receiver),
std::move(initialScanPtr));
EventStreamContextData *ctxData = new EventStreamContextData(
std::move(realPath), std::move(receiver), std::move(initialScanPtr));
FSEventStreamContext context;
context.version = 0;
context.info = ctxData;
context.retain = nullptr;
context.release = EventStreamContextData::dispose;
context.copyDescription = nullptr;

EventStream = FSEventStreamCreate(nullptr,
eventStreamCallback,
&context,
pathsToWatch,
kFSEventStreamEventIdSinceNow,
latency,
kFSEventStreamCreateFlagFileEvents |
kFSEventStreamCreateFlagNoDefer);
EventStream = FSEventStreamCreate(
nullptr, eventStreamCallback, &context, pathsToWatch,
kFSEventStreamEventIdSinceNow, latency,
kFSEventStreamCreateFlagFileEvents | kFSEventStreamCreateFlagNoDefer);
CFRelease(pathsToWatch);
if (!EventStream) {
return true;
Expand All @@ -175,10 +171,13 @@ void DirectoryWatcher::Implementation::stopFSEventStream() {
}

bool DirectoryWatcher::Implementation::initialize(StringRef Path,
EventReceiver Receiver, bool waitInitialSync, std::string &Error) {
EventReceiver Receiver,
bool waitInitialSync,
std::string &Error) {
auto initialScan = std::make_shared<DirectoryScan>();

dispatch_queue_t queue = dispatch_queue_create("DirectoryWatcher", DISPATCH_QUEUE_SERIAL);
dispatch_queue_t queue =
dispatch_queue_create("DirectoryWatcher", DISPATCH_QUEUE_SERIAL);
dispatch_semaphore_t initScanSema = dispatch_semaphore_create(0);
dispatch_semaphore_t setupFSEventsSema = dispatch_semaphore_create(0);

Expand Down Expand Up @@ -206,7 +205,8 @@ bool DirectoryWatcher::Implementation::initialize(StringRef Path,
dispatch_release(queue);

if (fsErr) {
raw_string_ostream(Error) << "failed to setup FSEvents stream for path: " << Path;
raw_string_ostream(Error)
<< "failed to setup FSEvents stream for path: " << Path;
return true;
}

Expand Down
44 changes: 23 additions & 21 deletions lib/DirectoryWatcher/DirectoryWatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,12 @@ template <> struct DenseMapInfo<sys::fs::UniqueID> {
std::make_pair(val.getDevice(), val.getFile()));
}

static bool isEqual(const sys::fs::UniqueID &LHS, const sys::fs::UniqueID &RHS) {
static bool isEqual(const sys::fs::UniqueID &LHS,
const sys::fs::UniqueID &RHS) {
return LHS == RHS;
}
};
}
} // namespace llvm

namespace {
/// Used for initial directory scan.
Expand All @@ -65,59 +66,59 @@ struct DirectoryScan {
using namespace llvm::sys;

std::error_code EC;
for (auto It = fs::directory_iterator(Path, EC), End = fs::directory_iterator();
!EC && It != End; It.increment(EC)) {
for (auto It = fs::directory_iterator(Path, EC),
End = fs::directory_iterator();
!EC && It != End; It.increment(EC)) {
auto status = getFileStatus(It->path());
if (!status.hasValue())
continue;
Files.push_back(std::make_tuple(It->path(), status->getLastModificationTime()));
Files.push_back(
std::make_tuple(It->path(), status->getLastModificationTime()));
FileIDSet.insert(status->getUniqueID());
}
}

std::vector<DirectoryWatcher::Event> getAsFileEvents() const {
std::vector<DirectoryWatcher::Event> Events;
for (const auto &info : Files) {
DirectoryWatcher::Event Event{DirectoryWatcher::EventKind::Added, std::get<0>(info), std::get<1>(info)};
DirectoryWatcher::Event Event{DirectoryWatcher::EventKind::Added,
std::get<0>(info), std::get<1>(info)};
Events.push_back(std::move(Event));
}
return Events;
}
};
}
} // namespace

// Add platform-specific functionality.

#if !defined(__has_include)
# define __has_include(x) 0
#define __has_include(x) 0
#endif

#if __has_include(<CoreServices/CoreServices.h>)
# include "DirectoryWatcher-mac.inc.h"
#include "DirectoryWatcher-mac.inc.h"
#elif __has_include(<sys/inotify.h>)
# include "DirectoryWatcher-linux.inc.h"
#include "DirectoryWatcher-linux.inc.h"
#else

struct DirectoryWatcher::Implementation {
bool initialize(StringRef Path, EventReceiver Receiver,
bool waitInitialSync, std::string &Error) {
bool initialize(StringRef Path, EventReceiver Receiver, bool waitInitialSync,
std::string &Error) {
Error = "directory listening not supported for this platform";
return true;
}
};

#endif

DirectoryWatcher::DirectoryWatcher() : Impl(*new Implementation()) {}

DirectoryWatcher::DirectoryWatcher()
: Impl(*new Implementation()) {}

DirectoryWatcher::~DirectoryWatcher() {
delete &Impl;
}
DirectoryWatcher::~DirectoryWatcher() { delete &Impl; }

std::unique_ptr<DirectoryWatcher> DirectoryWatcher::create(StringRef Path,
EventReceiver Receiver, bool waitInitialSync, std::string &Error) {
std::unique_ptr<DirectoryWatcher>
DirectoryWatcher::create(StringRef Path, EventReceiver Receiver,
bool waitInitialSync, std::string &Error) {
using namespace llvm::sys;

if (!fs::exists(Path)) {
Expand All @@ -143,7 +144,8 @@ std::unique_ptr<DirectoryWatcher> DirectoryWatcher::create(StringRef Path,
std::unique_ptr<DirectoryWatcher> DirWatch;
DirWatch.reset(new DirectoryWatcher());
auto &Impl = DirWatch->Impl;
bool hasError = Impl.initialize(Path, std::move(Receiver), waitInitialSync, Error);
bool hasError =
Impl.initialize(Path, std::move(Receiver), waitInitialSync, Error);
if (hasError)
return nullptr;

Expand Down
Loading

0 comments on commit 67a5087

Please sign in to comment.