Skip to content

Commit aab5d18

Browse files
author
Will
committed
watcher-c: remove pipe-based implementation because it is unused, for now
1 parent 792fec9 commit aab5d18

File tree

3 files changed

+7
-93
lines changed

3 files changed

+7
-93
lines changed

watcher-c/include/wtr/watcher-c.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,6 @@ void* wtr_watcher_open(char const* const path, wtr_watcher_callback callback, vo
6262

6363
bool wtr_watcher_close(void* watcher);
6464

65-
/* The user, or the language we're working with,
66-
might not prefer a callback-style API.
67-
We provide a pipe-based API for these cases.
68-
Instead of forwarding events to a callback,
69-
we write json-serialized events to a pipe. */
70-
void* wtr_watcher_open_pipe(char const* const path, int* read_fd, int* write_fd);
71-
72-
bool wtr_watcher_close_pipe(void* watcher, int read_fd, int write_fd);
73-
7465
#ifdef __cplusplus
7566
}
7667
#endif

watcher-c/src/test-watcher-c.c

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -74,38 +74,19 @@ void do_poll_read_loop(int read_fd)
7474
int main(int argc, char** argv)
7575
{
7676
char path[4096];
77-
char op[32];
7877
memset(path, 0, sizeof(path));
79-
memset(op, 0, sizeof(op));
8078
bool badargs = argc != 2 && argc != 3;
8179
bool ishelp = ! badargs && strcmp(argv[1], "-h") == 0;
8280
if (badargs || ishelp) {
83-
fprintf(stderr, "Usage: %s <op> [path]\n", argv[0]);
84-
fprintf(stderr, " op: pipe, cb-with-ctx\n");
81+
fprintf(stderr, "Usage: %s [path]\n", argv[0]);
8582
fprintf(stderr, " path: path to watch\n");
8683
return 1;
8784
}
88-
if (argc >= 2) strncpy(op, argv[1], sizeof(op) - 1);
8985
if (argc == 3) strncpy(path, argv[2], sizeof(path) - 1);
90-
if (strcmp(op, "cb-with-ctx") == 0) {
91-
int count = 0;
92-
void* watcher = wtr_watcher_open(path, callback, &count);
93-
if (! watcher) return 1;
94-
getchar();
95-
if (! wtr_watcher_close(watcher)) return 1;
96-
return 0;
97-
} else if (strcmp(op, "pipe") == 0) {
98-
int read_fd = -1;
99-
int write_fd = -1;
100-
void* watcher = wtr_watcher_open_pipe(path, &read_fd, &write_fd);
101-
if (! watcher) return 1;
102-
if (read_fd < 0) return 1;
103-
if (write_fd < 0) return 1;
104-
do_poll_read_loop(read_fd);
105-
if (! wtr_watcher_close_pipe(watcher, read_fd, write_fd)) return 1;
106-
return 0;
107-
} else {
108-
fprintf(stderr, "Unknown op: %s\n", op);
109-
return 1;
110-
}
86+
int count = 0;
87+
void* watcher = wtr_watcher_open(path, callback, &count);
88+
if (! watcher) return 1;
89+
getchar();
90+
if (! wtr_watcher_close(watcher)) return 1;
91+
return 0;
11192
}

watcher-c/src/watcher-c.cpp

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,6 @@
77
#define PATH_BUF_LEN 4096
88
#include <windows.h>
99
#include <stringapiset.h>
10-
#else
11-
// We support a pipe-based API for non-Windows platforms.
12-
#include <unistd.h>
13-
#include <fcntl.h>
14-
#include <string>
1510
#endif
1611

1712
extern "C" {
@@ -79,57 +74,4 @@ bool wtr_watcher_close(void* watcher)
7974
return true;
8075
}
8176

82-
#ifdef _WIN32
83-
void* wtr_watcher_open_pipe(char const* const path, int* read_fd, int* write_fd)
84-
{
85-
return NULL;
86-
}
87-
88-
bool wtr_watcher_close_pipe(void* watcher, int read_fd, int write_fd)
89-
{
90-
return false;
91-
}
92-
#else
93-
void* wtr_watcher_open_pipe(char const* const path, int* read_fd, int* write_fd)
94-
{
95-
if (! path) { fprintf(stderr, "Path is null.\n"); return NULL; }
96-
if (! read_fd) { fprintf(stderr, "Read fd is null.\n"); return NULL; }
97-
if (! write_fd) { fprintf(stderr, "Write fd is null.\n"); return NULL; }
98-
if (*read_fd > 0) { fprintf(stderr, "Read fd is already open.\n"); return NULL; }
99-
if (*write_fd > 0) { fprintf(stderr, "Write fd is already open.\n"); return NULL; }
100-
int pipe_fds[2];
101-
memset(pipe_fds, 0, sizeof(pipe_fds));
102-
if (pipe(pipe_fds) == -1) { perror("pipe"); return NULL; }
103-
if (pipe_fds[0] <= 0) { fprintf(stderr, "Read fd is invalid.\n"); return NULL; }
104-
if (pipe_fds[1] <= 0) { fprintf(stderr, "Write fd is invalid.\n"); return NULL; }
105-
fcntl(pipe_fds[0], F_SETFL, O_NONBLOCK);
106-
fcntl(pipe_fds[1], F_SETFL, O_NONBLOCK);
107-
*read_fd = pipe_fds[0];
108-
*write_fd = pipe_fds[1];
109-
auto json_serialize_event_to_pipe = [pipe_fds](wtr::watcher::event event) {
110-
auto json = wtr::to<std::string>(event) + "\n";
111-
write(pipe_fds[1], json.c_str(), json.size());
112-
};
113-
auto w = new wtr::watcher::watch(path, json_serialize_event_to_pipe);
114-
if (! w) {
115-
perror("new wtr::watcher::watch");
116-
close(pipe_fds[0]);
117-
close(pipe_fds[1]);
118-
return NULL;
119-
}
120-
*read_fd = pipe_fds[0];
121-
return (void*)w;
122-
}
123-
124-
bool wtr_watcher_close_pipe(void* watcher, int read_fd, int write_fd)
125-
{
126-
bool ok = false;
127-
ok |= watcher && ((wtr::watcher::watch*)watcher)->close();
128-
ok |= write_fd > 0 && close(write_fd);
129-
ok |= read_fd > 0 && close(read_fd);
130-
if (watcher) delete (wtr::watcher::watch*)watcher;
131-
return ok;
132-
}
133-
#endif
134-
13577
} // extern "C"

0 commit comments

Comments
 (0)