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

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: Simplify watching by removing FileSystemWatcher and adding FileSystemEntity.watch. Created 7 years, 3 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::WatchPath(const char* path,
18 int events,
19 bool recursive) {
20 int fd = TEMP_FAILURE_RETRY(inotify_init1(IN_NONBLOCK | IN_CLOEXEC));
21 if (fd < 0) return -1;
22 int list_events = 0;
23 if (events & kCreate) list_events |= IN_CREATE;
24 if (events & kModifyContent) list_events |= IN_MODIFY | IN_ATTRIB;
25 if (events & kDelete) list_events |= IN_DELETE;
26 if (events & kMove) list_events |= IN_MOVE;
27 int path_fd = TEMP_FAILURE_RETRY(inotify_add_watch(fd, path, list_events));
28 if (path_fd < 0) {
29 close(fd);
30 return -1;
31 }
32 return fd;
33 }
34
35
36 void FileSystemWatcher::UnwatchPath(intptr_t id) {
37 // Nothing to do.
38 }
39
40
41 intptr_t FileSystemWatcher::GetSocketId(intptr_t id) {
42 return id;
43 }
44
45
46 intptr_t FileSystemWatcher::ReadEvents(intptr_t id, Event** events) {
Søren Gjesse 2013/08/26 07:51:55 Here we should just pass in the _NativeSocket obje
Anders Johnsen 2013/09/03 11:36:23 I've tried to clean up as much as possible by usin
47 const intptr_t kEventSize = sizeof(struct inotify_event);
48 const intptr_t kBufferSize = kEventSize + NAME_MAX + 1;
49 uint8_t buffer[kBufferSize];
50 intptr_t bytes = TEMP_FAILURE_RETRY(read(id, buffer, kBufferSize));
51 if (bytes < 0) return -1;
52 const intptr_t kMaxCount = kBufferSize / kEventSize + 1;
53 *events = new Event[kMaxCount];
Søren Gjesse 2013/08/26 07:51:55 I think we should just do the Dart allocation in h
Anders Johnsen 2013/09/03 11:36:23 Done.
54 intptr_t offset = 0;
55 intptr_t i = 0;
56 while (offset < bytes) {
57 struct inotify_event* e =
58 reinterpret_cast<struct inotify_event*>(buffer + offset);
59 Event* event = *events + i;
60 event->event = 0;
61 event->link = e->cookie;
62 if (e->mask & IN_MODIFY) event->event |= kModifyContent;
63 if (e->mask & IN_ATTRIB) event->event |= kModefyAttribute;
64 if (e->mask & IN_CREATE) event->event |= kCreate;
65 if (e->mask & IN_MOVE) event->event |= kMove;
66 if (e->mask & IN_DELETE) event->event |= kDelete;
67 if (e->len > 0) {
68 event->filename = strdup(e->name);
69 } else {
70 event->filename = NULL;
71 }
72 i++;
73 offset += kEventSize + e->len;
74 }
75 return i;
76 }
77
78 } // namespace bin
79 } // namespace dart
80
81 #endif // defined(TARGET_OS_LINUX)
82
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698