io-watch 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0fc26487e8282e4fb61d521424ea49b98606676fb04768fbe94e5244a44b4642
4
- data.tar.gz: '08666b566a234b2e64c49ea40ff878070d12cb53398cf3b883bff22ef1542bbb'
3
+ metadata.gz: bff7e54dec1f3d84af735a50615748de8496fe626400b4a4abe1904893368353
4
+ data.tar.gz: 709deefe15939338d0f37581061479868f3a33f7893508818a4f20ec3529b6c6
5
5
  SHA512:
6
- metadata.gz: 51ee161b28baae33c5745658a77a82dd01bca7b6b350a60af90a19d38c3bbb329b00c242a81ae58134ea91445101c719f6b6b2a85013bb668390f31c52d7580c
7
- data.tar.gz: 9224e4d78d0509c987aba237df97babd0ed6caef5cc1f7012b4151c3ac84cd81ba2f67aa473163ce542d6325cbbf79c87ffeffb53b43de2c1142d5bc4596d618
6
+ metadata.gz: 90502cc34507f0687fdc141b44cf1afe67f20691e69caebfc65769408444fcbd8f68ad1a597c529f607afdb9388489a7f76bdc0413c3aed0381b2315f8748dbe
7
+ data.tar.gz: a69f88857c1cdd5211a75be9792b3b338fdb82f7e4442fd682eb20c479ff2e1d4be48f1d9edab547785cea67758c8d40ceb6e7c0b5f71988031f21fd7a99b35e
checksums.yaml.gz.sig CHANGED
Binary file
data/ext/Makefile ADDED
@@ -0,0 +1,31 @@
1
+ TARGET = io-watch
2
+ CC = cc
3
+ CFLAGS = -Wall -Wextra
4
+ LDFLAGS = -framework CoreServices
5
+ SOURCES = io/watch.c io/watch/fsevent.c
6
+ OBJECTS = $(SOURCES:.c=.o)
7
+ PREFIX = /Users/samuel/Developer/socketry/io-watch/ext
8
+
9
+ all: $(TARGET)
10
+
11
+ $(TARGET): $(OBJECTS)
12
+ $(CC) $(LDFLAGS) -o $@ $^
13
+
14
+ # To create object files
15
+ %.o: %.c
16
+ $(CC) $(CFLAGS) -c $< -o $@
17
+
18
+ # Clean Target
19
+ clean:
20
+ rm -f $(TARGET) $(OBJECTS)
21
+
22
+ install: $(TARGET)
23
+ @echo "Installing $(TARGET) to $(PREFIX)/bin"
24
+ mkdir -p $(PREFIX)/bin
25
+ install -m 755 $(TARGET) $(PREFIX)/bin
26
+
27
+ uninstall:
28
+ @echo "Removing $(TARGET) from $(PREFIX)/bin"
29
+ rm -f $(PREFIX)/bin/$(TARGET)
30
+
31
+ .PHONY: all clean install uninstall
data/ext/bin/io-watch ADDED
Binary file
@@ -48,7 +48,7 @@ void IO_Watch_FSEvent_callback(
48
48
  struct IO_Watch *watch = context;
49
49
 
50
50
  for (size_t i = 0; i < numberOfEvents; i++) {
51
- if (DEBUG) fprintf(stderr, "Event: %s\n", eventPaths[i]);
51
+ if (DEBUG) fprintf(stderr, "io-watch:IO_Watch_FSEvent_callback: Event %s\n", eventPaths[i]);
52
52
 
53
53
  // Find the index of the path in the paths array
54
54
  ssize_t index = IO_Watch_find_path(watch, eventPaths[i]);
@@ -57,7 +57,7 @@ void IO_Watch_FSEvent_callback(
57
57
  // Output event data as newline-delimited JSON
58
58
  printf("{\"index\":%zu,\"flags\":%u,\"id\":%llu}\n", index, eventFlags[i], eventIds[i]);
59
59
  } else {
60
- fprintf(stderr, "Path not found in paths array: %s\n", eventPaths[i]);
60
+ fprintf(stderr, "io-watch:IO_Watch_FSEvent_callback: Path not found %s\n", eventPaths[i]);
61
61
  }
62
62
  }
63
63
 
Binary file
@@ -33,7 +33,7 @@ void IO_Watch_Watch_Array_initialize(struct IO_Watch_Watch_Array *array) {
33
33
  array->capacity = 16;
34
34
  array->watches = malloc(array->capacity * sizeof(struct IO_Watch_Watch));
35
35
  if (!array->watches) {
36
- perror("malloc");
36
+ perror("io-watch:IO_Watch_Watch_Array_initialize:malloc");
37
37
  exit(EXIT_FAILURE);
38
38
  }
39
39
  }
@@ -42,7 +42,7 @@ void IO_Watch_Watch_Array_resize(struct IO_Watch_Watch_Array *array) {
42
42
  array->capacity *= 2;
43
43
  array->watches = realloc(array->watches, array->capacity * sizeof(struct IO_Watch_Watch));
44
44
  if (!array->watches) {
45
- perror("realloc");
45
+ perror("io-watch:IO_Watch_Watch_Array_resize:realloc");
46
46
  exit(EXIT_FAILURE);
47
47
  }
48
48
  }
@@ -67,21 +67,30 @@ ssize_t IO_Watch_Watch_Array_find(struct IO_Watch_Watch_Array *array, int watch_
67
67
  }
68
68
 
69
69
  void IO_Watch_Watch_Array_watch(int fd, struct IO_Watch_Watch_Array *watch_array, char *path, int index) {
70
- int watch_descriptor = inotify_add_watch(fd, path, IN_ALL_EVENTS);
70
+ int mask =
71
+ IN_MODIFY | // File was modified.
72
+ IN_ATTRIB | // Metadata (permissions, timestamps, etc.) changed.
73
+ IN_CLOSE_WRITE | // Writable file was closed.
74
+ IN_MOVE_SELF | // Watched file/directory was moved.
75
+ IN_CREATE | // File or directory was created within the watched directory.
76
+ IN_DELETE; // File or directory was deleted within the watched directory.
77
+
78
+ int watch_descriptor = inotify_add_watch(fd, path, mask);
79
+
71
80
  if (watch_descriptor == -1) {
72
- perror("inotify_add_watch");
81
+ perror("io-watch:IO_Watch_Watch_Array_watch:inotify_add_watch");
73
82
  exit(EXIT_FAILURE);
74
83
  }
75
84
 
76
85
  IO_Watch_Watch_Array_add(watch_array, watch_descriptor, path, index);
77
86
 
78
- if (DEBUG) fprintf(stderr, "Added watch: %s\n", path);
87
+ if (DEBUG) fprintf(stderr, "io-watch:IO_Watch_Watch_Array_watch: Added watch %s\n", path);
79
88
  }
80
89
 
81
90
  void IO_Watch_Watch_Array_scan(int fd, struct IO_Watch_Watch_Array *watch_array, const char *root, int index) {
82
91
  DIR *dir = opendir(root);
83
92
  if (!dir) {
84
- perror("opendir");
93
+ perror("io-watch:IO_Watch_Watch_Array_scan:opendir");
85
94
  return;
86
95
  }
87
96
 
@@ -94,13 +103,13 @@ void IO_Watch_Watch_Array_scan(int fd, struct IO_Watch_Watch_Array *watch_array,
94
103
  size_t size = strlen(root) + 1 + strlen(entry->d_name) + 1;
95
104
  char *path = malloc(size);
96
105
  snprintf(path, size, "%s/%s", root, entry->d_name);
97
-
106
+
98
107
  struct stat statbuf;
99
108
  if (stat(path, &statbuf) == -1) {
100
- perror("stat");
109
+ perror("io-watch:IO_Watch_Watch_Array_scan:stat");
101
110
  continue;
102
111
  }
103
-
112
+
104
113
  if (S_ISDIR(statbuf.st_mode)) {
105
114
  IO_Watch_Watch_Array_watch(fd, watch_array, path, index);
106
115
  IO_Watch_Watch_Array_scan(fd, watch_array, path, index);
@@ -108,7 +117,7 @@ void IO_Watch_Watch_Array_scan(int fd, struct IO_Watch_Watch_Array *watch_array,
108
117
  free(path);
109
118
  }
110
119
  }
111
-
120
+
112
121
  closedir(dir);
113
122
  }
114
123
 
@@ -116,7 +125,7 @@ void IO_Watch_Watch_Array_add_subdirectory(int fd, struct IO_Watch_Watch_Array *
116
125
  size_t size = strlen(watch.path) + 1 + strlen(name) + 1;
117
126
  char *path = malloc(size);
118
127
  snprintf(path, size, "%s/%s", watch.path, name);
119
-
128
+
120
129
  IO_Watch_Watch_Array_watch(fd, watch_array, path, watch.index);
121
130
  IO_Watch_Watch_Array_scan(fd, watch_array, path, watch.index);
122
131
  }
@@ -124,7 +133,7 @@ void IO_Watch_Watch_Array_add_subdirectory(int fd, struct IO_Watch_Watch_Array *
124
133
  void IO_Watch_Watch_Array_remove(int fd, struct IO_Watch_Watch_Array *watch_array, size_t index) {
125
134
  struct IO_Watch_Watch watch = watch_array->watches[index];
126
135
 
127
- if (DEBUG) fprintf(stderr, "Removing watch: %s\n", watch.path);
136
+ if (DEBUG) fprintf(stderr, "io-watch:IO_Watch_Watch_Array_add_subdirectory: Removing watch %s\n", watch.path);
128
137
 
129
138
  inotify_rm_watch(fd, watch.watch_descriptor);
130
139
  free(watch.path);
@@ -138,7 +147,7 @@ void IO_Watch_Watch_Array_remove(int fd, struct IO_Watch_Watch_Array *watch_arra
138
147
 
139
148
  static
140
149
  void IO_Watch_INotify_print_event(struct inotify_event *event) {
141
- fprintf(stderr, "Event: wd=%d", event->wd);
150
+ fprintf(stderr, "Event wd=%d", event->wd);
142
151
 
143
152
  uint32_t mask = event->mask;
144
153
  if (mask & IN_ACCESS) fprintf(stderr, " ACCESS");
@@ -170,41 +179,54 @@ void IO_Watch_INotify_print_event(struct inotify_event *event) {
170
179
  void IO_Watch_run(struct IO_Watch *watch) {
171
180
  int fd = inotify_init1(IN_NONBLOCK);
172
181
  if (fd == -1) {
173
- perror("inotify_init1");
182
+ perror("io-watch:IO_Watch_run:inotify_init1");
174
183
  exit(EXIT_FAILURE);
175
184
  }
176
-
185
+
177
186
  struct IO_Watch_Watch_Array watch_array;
178
187
  IO_Watch_Watch_Array_initialize(&watch_array);
179
-
188
+
180
189
  for (size_t i = 0; i < watch->size; i++) {
181
190
  char *path = strdup(watch->paths[i]);
182
191
 
183
192
  IO_Watch_Watch_Array_watch(fd, &watch_array, path, i);
184
193
  IO_Watch_Watch_Array_scan(fd, &watch_array, path, i);
185
194
  }
186
-
195
+
187
196
  printf("{\"status\":\"started\"}\n");
188
197
  fflush(stdout);
189
-
198
+
190
199
  char buffer[BUFFER_SIZE] __attribute__ ((aligned(8)));
191
200
 
192
201
  while (1) {
193
202
  ssize_t result = read(fd, buffer, BUFFER_SIZE);
194
203
  if (result == -1 && errno != EAGAIN) {
195
- perror("read");
204
+ perror("(io-watch:IO_Watch_run:read)");
196
205
  exit(EXIT_FAILURE);
197
206
  }
198
-
207
+
199
208
  for (ssize_t offset = 0; offset < result;) {
200
209
  struct inotify_event *event = (struct inotify_event *) &buffer[offset];
201
- if (DEBUG) IO_Watch_INotify_print_event(event);
210
+ if (DEBUG) {
211
+ fprintf(stderr, "io-watch:IO_Watch_run: ");
212
+ IO_Watch_INotify_print_event(event);
213
+ }
214
+
215
+ if (event->wd == -1) {
216
+ if (event->mask & IN_Q_OVERFLOW) {
217
+ fprintf(stderr, "io-watch:IO_Watch_run: Queue overflow\n");
218
+ } else {
219
+ fprintf(stderr, "io-watch:IO_Watch_run: Unknown error ");
220
+ IO_Watch_INotify_print_event(event);
221
+ }
222
+ break;
223
+ }
202
224
 
203
225
  ssize_t index = IO_Watch_Watch_Array_find(&watch_array, event->wd);
204
-
226
+
205
227
  if (index != -1) {
206
228
  printf("{\"index\":%d,\"mask\":%u}\n", watch_array.watches[index].index, event->mask);
207
-
229
+
208
230
  // If a new directory is created, add a watch for it
209
231
  if (event->mask & IN_CREATE && event->mask & IN_ISDIR) {
210
232
  IO_Watch_Watch_Array_add_subdirectory(fd, &watch_array, watch_array.watches[index], event->name);
@@ -212,9 +234,9 @@ void IO_Watch_run(struct IO_Watch *watch) {
212
234
  IO_Watch_Watch_Array_remove(fd, &watch_array, index);
213
235
  }
214
236
  } else {
215
- fprintf(stderr, "Watch descriptor not found: %d\n", event->wd);
237
+ fprintf(stderr, "io-watch:IO_Watch_run: Watch descriptor %d not found!\n", event->wd);
216
238
  }
217
-
239
+
218
240
  offset += sizeof(struct inotify_event) + event->len;
219
241
  }
220
242
  fflush(stdout);
data/ext/io/watch.c CHANGED
@@ -27,10 +27,10 @@ int main(int argc, char **argv) {
27
27
  for (size_t i = 0; i < watch.size; i++) {
28
28
  char *real_path = realpath(watch.paths[i], NULL);
29
29
  if (real_path == NULL) {
30
- fprintf(stderr, "Error: realpath failed for %s\n", watch.paths[i]);
30
+ fprintf(stderr, "io-watch:main: Realpath failed for %s\n", watch.paths[i]);
31
31
  return 1;
32
32
  } else {
33
- if (DEBUG) fprintf(stderr, "Watching: %s\n", real_path);
33
+ if (DEBUG) fprintf(stderr, "io-watch:main: Watching %s\n", real_path);
34
34
  watch.paths[i] = real_path;
35
35
  }
36
36
  }
data/ext/io/watch.o ADDED
Binary file
data/ext/io-watch ADDED
Binary file
@@ -5,6 +5,6 @@
5
5
 
6
6
  class IO
7
7
  module Watch
8
- VERSION = '0.5.0'
8
+ VERSION = '0.6.0'
9
9
  end
10
10
  end
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: io-watch
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams
@@ -37,7 +37,7 @@ cert_chain:
37
37
  Q2K9NVun/S785AP05vKkXZEFYxqG6EW012U4oLcFl5MySFajYXRYbuUpH6AY+HP8
38
38
  voD0MPg1DssDLKwXyt1eKD/+Fq0bFWhwVM/1XiAXL7lyYUyOq24KHgQ2Csg=
39
39
  -----END CERTIFICATE-----
40
- date: 2024-06-28 00:00:00.000000000 Z
40
+ date: 2024-08-11 00:00:00.000000000 Z
41
41
  dependencies: []
42
42
  description:
43
43
  email:
@@ -48,9 +48,14 @@ extensions:
48
48
  extra_rdoc_files: []
49
49
  files:
50
50
  - bin/io-watch
51
+ - ext/Makefile
52
+ - ext/bin/io-watch
51
53
  - ext/configure
54
+ - ext/io-watch
52
55
  - ext/io/watch.c
56
+ - ext/io/watch.o
53
57
  - ext/io/watch/fsevent.c
58
+ - ext/io/watch/fsevent.o
54
59
  - ext/io/watch/inotify.c
55
60
  - ext/io/watch/watch.h
56
61
  - lib/io/watch.rb
metadata.gz.sig CHANGED
Binary file