-
Notifications
You must be signed in to change notification settings - Fork 0
/
dskill.c
406 lines (357 loc) · 9.3 KB
/
dskill.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <dirent.h>
#include <libgen.h>
#include <time.h>
#include <limits.h>
#include <wordexp.h>
#include <sys/stat.h>
#include <mach-o/dyld.h>
#include <CoreServices/CoreServices.h>
#define EVIL ".DS_Store"
#define LATENCY 0.1
#define ID "xyz.space55.dskill"
#define PLIST_PATH "~/Library/LaunchAgents/"ID".plist"
#define NUM_EXCLUDES 16
#define NUM_PATHS 16
char *help_msg =
"Kill all .DS_Store!\n"
"\n"
"USAGE\n"
"\n"
" $ dskill <cmd> [opts]\n"
"\n"
"COMMANDS\n"
"\n"
" kill remove all .DS_Store recursively under current directory\n"
" guard start a process to prevent creation of .DS_Store recursively\n"
" under current directory\n"
" service manage launchd service for dskill guard\n"
"\n"
"OPTIONS\n"
"\n"
" -e, --exclude exclude a directory pattern\n"
" -h, --help display help message\n";
char *service_help_msg =
"Manage dskill guard service\n"
"\n"
"USAGE\n"
"\n"
" $ dskill service <cmd> -- [flags]\n"
"\n"
"COMMANDS\n"
"\n"
" start install and start the launchd service, run `dskill guard` in the\n"
" background\n"
" stop stop the service\n"
" install install the service plist to ~/Library/LaunchAgents\n"
" uninstall stop and remove the service plist from ~/Library/LaunchAgents\n";
typedef struct flags {
int num_excludes;
char *excludes[NUM_EXCLUDES];
int num_paths;
char *paths[NUM_PATHS];
bool help;
} flags;
flags flags_new() {
return (flags) {
.num_excludes = 0,
.excludes = {},
.num_paths = 0,
.paths = {},
.help = false,
};
}
void flags_free(flags *f) {
for (int i = 0; i < f->num_excludes; i++) {
free(f->excludes[i]);
}
for (int i = 0; i < f->num_paths; i++) {
free(f->paths[i]);
}
f->num_excludes = 0;
}
bool is_evil(char *path) {
return strcmp(path + strlen(path) - strlen(EVIL), EVIL) == 0;
}
void error(const char *fmt, ...) {
va_list args;
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
exit(EXIT_FAILURE);
}
void warn(const char *fmt, ...) {
va_list args;
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
}
void systemf(const char *fmt, ...) {
char cmd[1024];
va_list args;
va_start(args, fmt);
vsprintf(cmd, fmt, args);
va_end(args);
system(cmd);
}
char *expand(char *path) {
wordexp_t exp_result;
wordexp(path, &exp_result, 0);
char *expanded = strdup(exp_result.we_wordv[0]);
wordfree(&exp_result);
return expanded;
}
bool is_excluded(char *path, flags *f) {
char *path2 = strdup(path);
char *dir = dirname(path2);
for (int i = 0; i < f->num_excludes; i++) {
char *pat = f->excludes[i];
// TODO
if (strcmp(dir, pat) == 0) {
return true;
}
}
free(path2);
return false;
}
void clean_dir(char *dir, flags *f) {
DIR *d = opendir(dir);
struct dirent *entry;
if (!d) {
warn("failed to open directory \"%s\"\n", dir);
return;
}
while ((entry = readdir(d))) {
if (strcmp(entry->d_name, ".") == 0
|| strcmp(entry->d_name, "..") == 0) {
continue;
}
char path[PATH_MAX];
if (strcmp(dir, ".") == 0) {
sprintf(path, "%s", entry->d_name);
} else {
sprintf(path, "%s/%s", dir, entry->d_name);
}
if (strcmp(entry->d_name, EVIL) == 0) {
printf("%s\n", path);
remove(path);
} else if (entry->d_type == DT_DIR) {
clean_dir(path, f);
}
}
closedir(d);
}
void fsevent_callback(
ConstFSEventStreamRef ref,
void *data,
size_t num_events,
void *event_paths,
const FSEventStreamEventFlags *event_flags,
const FSEventStreamEventId *event_ids
) {
flags *f = data;
for (int i = 0; i < num_events; i++) {
char *path = ((char**)event_paths)[i];
if (!is_evil(path)) {
continue;
}
if (is_excluded(path, f)) {
continue;
}
FSEventStreamEventFlags flags = event_flags[i];
if (flags & kFSEventStreamEventFlagItemRemoved) {
continue;
}
if (flags & kFSEventStreamEventFlagItemCreated) {
printf("%s\n", path);
remove(path);
}
}
}
void guard(char **paths, int num_paths, flags *f) {
CFStringRef cf_paths[num_paths];
for (int i = 0; i < num_paths; i++) {
DIR *d = opendir(paths[i]);
if (!d) return error("failed to open directory \"%s\"\n", paths[i]);
closedir(d);
cf_paths[i] = CFStringCreateWithCString(
NULL,
paths[i],
kCFStringEncodingUTF8
);
}
CFArrayRef cf_paths_arr = CFArrayCreate(
NULL,
(const void **)cf_paths,
num_paths,
NULL
);
FSEventStreamRef stream = FSEventStreamCreate(
NULL,
fsevent_callback,
&(FSEventStreamContext) {
.info = f,
.copyDescription = NULL,
.version = 0,
.retain = NULL,
.release = NULL,
},
cf_paths_arr,
kFSEventStreamEventIdSinceNow,
LATENCY,
kFSEventStreamCreateFlagFileEvents
| kFSEventStreamCreateFlagNoDefer
);
for (int i = 0; i < num_paths; i++) {
CFRelease(cf_paths[i]);
}
CFRelease(cf_paths_arr);
if (stream == NULL) {
return error("failed to create fsevent stream\n");
}
dispatch_queue_t queue = dispatch_queue_create(ID, NULL);
FSEventStreamSetDispatchQueue(stream, queue);
if (!FSEventStreamStart(stream)) {
return error("failed to start fsevent stream\n");
}
sigset_t ss;
sigemptyset(&ss);
sigsuspend(&ss);
FSEventStreamStop(stream);
FSEventStreamRelease(stream);
dispatch_release(queue);
}
void service_install(char **args, int num_args) {
char *real_plist_path = expand(PLIST_PATH);
char exe_path[PATH_MAX];
uint32_t exe_path_size = sizeof(exe_path);
if (_NSGetExecutablePath(exe_path, &exe_path_size) != 0) {
return error("failed to get executable path\n");
}
char plist[2048];
char *c = plist;
c += sprintf(c, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
c += sprintf(c, "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n");
c += sprintf(c, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
c += sprintf(c, "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n");
c += sprintf(c, "<plist version=\"1.0\">\n");
c += sprintf(c, "<dict>\n");
c += sprintf(c, " <key>Label</key>\n");
c += sprintf(c, " <string>"ID"</string>\n");
c += sprintf(c, " <key>ProgramArguments</key>\n");
c += sprintf(c, " <array>\n");
c += sprintf(c, " <string>%s</string>\n", exe_path);
c += sprintf(c, " <string>guard</string>\n");
for (int i = 0; i < num_args; i++) {
c += sprintf(c, " <string>%s</string>\n", args[i]);
}
c += sprintf(c, " </array>\n");
c += sprintf(c, " <key>RunAtLoad</key>\n");
c += sprintf(c, " <true/>\n");
c += sprintf(c, " <key>KeepAlive</key>\n");
c += sprintf(c, " <true/>\n");
c += sprintf(c, " <key>StandardOutPath</key>\n");
c += sprintf(c, " <string>/tmp/dskill.out.log</string>\n");
c += sprintf(c, " <key>StandardErrorPath</key>\n");
c += sprintf(c, " <string>/tmp/dskill.err.log</string>\n");
c += sprintf(c, "</dict>\n");
c += sprintf(c, "</plist>");
FILE *f = fopen(real_plist_path, "w");
fwrite(plist, 1, strlen(plist), f);
fclose(f);
free(real_plist_path);
}
void service_start(char **args, int num_args) {
char *real_plist_path = expand(PLIST_PATH);
if (access(real_plist_path, F_OK) != 0) {
service_install(args, num_args);
}
systemf("launchctl load -w %s", PLIST_PATH);
free(real_plist_path);
}
void service_stop() {
systemf("launchctl unload %s", PLIST_PATH);
}
void service_uninstall() {
service_stop();
char *real_plist_path = expand(PLIST_PATH);
remove(real_plist_path);
free(real_plist_path);
}
int main(int argc, char **argv) {
if (argc < 2) {
printf("%s", help_msg);
return EXIT_SUCCESS;
}
flags f = flags_new();
char *args[16] = {0};
int num_args = 0;
int forward_pos = -1;
// TODO: cmd / opt / path order
for (int i = 2; i < argc; i++) {
if (strcmp(argv[i], "--exclude") == 0 || strcmp(argv[i], "-e") == 0) {
if (i + 1 >= argc) {
error("--exclude requires path followed\n");
return EXIT_FAILURE;
}
if (f.num_excludes + 1 > NUM_EXCLUDES) {
error("cannot have more than %d excludes\n", NUM_EXCLUDES);
return EXIT_FAILURE;
}
f.excludes[f.num_excludes++] = strdup(argv[i + 1]);
i++;
continue;
} else if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) {
f.help = true;
continue;
} else if (strcmp(argv[i], "--") == 0) {
if (i < argc - 1) {
forward_pos = i + 1;
}
break;
} else {
args[num_args++] = argv[i];
}
}
char *cmd = argv[1];
if (strcmp(cmd, "guard") == 0) {
if (num_args > 0) {
guard(args, num_args, &f);
} else {
guard((char*[]){ "." }, 1, &f);
}
} else if (strcmp(cmd, "kill") == 0) {
clean_dir(num_args > 0 ? args[0] : ".", &f);
} else if (strcmp(cmd, "service") == 0) {
if (argc < 3 || f.help) {
printf("%s", service_help_msg);
return EXIT_SUCCESS;
}
char *service_cmd = argv[2];
if (strcmp(service_cmd, "start") == 0) {
if (forward_pos == -1) {
service_start(NULL, 0);
} else {
service_start(argv + forward_pos, argc - forward_pos);
}
} else if (strcmp(service_cmd, "stop") == 0) {
service_stop();
} else if (strcmp(service_cmd, "install") == 0) {
if (forward_pos == -1) {
service_install(NULL, 0);
} else {
service_install(argv + forward_pos, argc - forward_pos);
}
} else if (strcmp(service_cmd, "uninstall") == 0) {
service_uninstall();
}
return EXIT_SUCCESS;
} else {
printf("%s", help_msg);
}
flags_free(&f);
return EXIT_SUCCESS;
}