Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(734)

Side by Side Diff: runtime/bin/file_system_watcher_linux.cc

Issue 19263003: Add FileSystemWatcher class to dart:io. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Cleanup Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4
5 #include "platform/globals.h"
6 #if defined(TARGET_OS_LINUX)
7
8 #include "bin/file_system_watcher.h"
9
10 #include <errno.h> // NOLINT
11 #include <sys/inotify.h> // NOLINT
12
13
14 namespace dart {
15 namespace bin {
16
17 intptr_t FileSystemWatcher::Init() {
18 int fd = TEMP_FAILURE_RETRY(inotify_init1(IN_NONBLOCK | IN_CLOEXEC));
19 if (fd < 0) return -1;
20 return fd;
21 }
22
23
24 void FileSystemWatcher::Stop(intptr_t id) {
25 // Don't close it manually, the eventhandler will do it for us.
26 }
27
28
29 intptr_t FileSystemWatcher::GetSocketId(intptr_t id, intptr_t path_id) {
30 if (path_id != -1) return -1;
31 return id;
32 }
33
34
35 intptr_t FileSystemWatcher::AddPath(intptr_t id,
36 const char* path,
37 int events,
38 bool recursive) {
39 int list_events = 0;
40 if (events & kCreate) list_events |= IN_CREATE;
41 if (events & kModifyContent) list_events |= IN_MODIFY | IN_ATTRIB;
42 if (events & kDelete) list_events |= IN_DELETE;
43 if (events & kMove) list_events |= IN_MOVE;
44 int fd = TEMP_FAILURE_RETRY(inotify_add_watch(id, path, list_events));
45 if (fd < 0) return -1;
46 return fd;
47 }
48
49
50 bool FileSystemWatcher::RemovePath(intptr_t id, intptr_t path_id) {
51 return TEMP_FAILURE_RETRY(inotify_rm_watch(id, path_id)) == 0;
52 }
53
54
55 intptr_t FileSystemWatcher::ReadEvents(intptr_t id,
56 intptr_t path_id,
57 Event** events) {
58 const intptr_t kEventSize = sizeof(struct inotify_event);
59 const intptr_t kBufferSize = kEventSize + NAME_MAX + 1;
60 uint8_t buffer[kBufferSize];
61 intptr_t bytes = TEMP_FAILURE_RETRY(read(id, buffer, kBufferSize));
62 if (bytes < 0) return -1;
63 const intptr_t kMaxCount = kBufferSize / kEventSize + 1;
64 *events = new Event[kMaxCount];
65 intptr_t offset = 0;
66 intptr_t i = 0;
67 while (offset < bytes) {
68 struct inotify_event* e =
69 reinterpret_cast<struct inotify_event*>(buffer + offset);
70 Event* event = *events + i;
71 event->event = 0;
72 event->path_id = e->wd;
73 event->link = e->cookie;
74 if (e->mask & IN_MODIFY) event->event |= kModifyContent;
75 if (e->mask & IN_ATTRIB) event->event |= kModefyAttribute;
76 if (e->mask & IN_CREATE) event->event |= kCreate;
77 if (e->mask & IN_MOVE) event->event |= kMove;
78 if (e->mask & IN_DELETE) event->event |= kDelete;
79 if (e->len > 0) {
80 event->filename = strdup(e->name);
81 } else {
82 event->filename = NULL;
83 }
84 i++;
85 offset += kEventSize + e->len;
86 }
87 return i;
88 }
89
90 } // namespace bin
91 } // namespace dart
92
93 #endif // defined(TARGET_OS_LINUX)
94
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698