-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremake.c
More file actions
491 lines (466 loc) · 14.5 KB
/
remake.c
File metadata and controls
491 lines (466 loc) · 14.5 KB
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
#define _XOPEN_SOURCE 700
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <errno.h>
#include <limits.h>
#define MAX_REMOTES 8
typedef struct {
char name[64];
char host[256];
char user[256];
char path[PATH_MAX];
} Remote;
typedef struct {
Remote remotes[MAX_REMOTES];
int remote_count;
char exclude[1024];
char build_cmd[512];
char test_cmd[512];
} Config;
static void trim(char *s) {
size_t len;
char *p = s;
while (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r') {
p++;
}
if (p != s) {
memmove(s, p, strlen(p) + 1);
}
len = strlen(s);
while (len > 0) {
char c = s[len - 1];
if (c == ' ' || c == '\t' || c == '\n' || c == '\r') {
s[len - 1] = '\0';
len--;
} else {
break;
}
}
}
static int file_exists(const char *path) {
struct stat st;
return stat(path, &st) == 0 && S_ISREG(st.st_mode);
}
static char *find_config_file(void) {
char cwd[PATH_MAX];
char current[PATH_MAX];
char candidate[PATH_MAX];
if (getcwd(cwd, sizeof(cwd)) == NULL) {
perror("getcwd");
return NULL;
}
strncpy(current, cwd, sizeof(current) - 1);
current[sizeof(current) - 1] = '\0';
for (;;) {
int n = snprintf(candidate, sizeof(candidate), "%s/%s", current, ".remake.conf");
if (n < 0 || (size_t)n >= sizeof(candidate)) {
fprintf(stderr, "Path too long while searching for .remake.conf\n");
return NULL;
}
if (file_exists(candidate)) {
char *result = malloc(strlen(candidate) + 1);
if (!result) {
return NULL;
}
strcpy(result, candidate);
return result;
}
if (strcmp(current, "/") == 0) {
break;
}
char *slash = strrchr(current, '/');
if (!slash || slash == current) {
strcpy(current, "/");
} else {
*slash = '\0';
}
}
return NULL;
}
static void init_config(Config *cfg) {
int i;
memset(cfg, 0, sizeof(*cfg));
for (i = 0; i < MAX_REMOTES; i++) {
cfg->remotes[i].name[0] = '\0';
cfg->remotes[i].host[0] = '\0';
cfg->remotes[i].user[0] = '\0';
cfg->remotes[i].path[0] = '\0';
}
cfg->remote_count = 0;
cfg->exclude[0] = '\0';
cfg->build_cmd[0] = '\0';
cfg->test_cmd[0] = '\0';
}
static int ensure_remote(Config *cfg, const char *name) {
int i;
if (!name || name[0] == '\0') {
name = "default";
}
for (i = 0; i < cfg->remote_count; i++) {
if (strcmp(cfg->remotes[i].name, name) == 0) {
return i;
}
}
if (cfg->remote_count >= MAX_REMOTES) {
fprintf(stderr, "Too many [remote] entries, max is %d\n", MAX_REMOTES);
return -1;
}
strncpy(cfg->remotes[cfg->remote_count].name, name, sizeof(cfg->remotes[cfg->remote_count].name) - 1);
cfg->remotes[cfg->remote_count].name[sizeof(cfg->remotes[cfg->remote_count].name) - 1] = '\0';
cfg->remotes[cfg->remote_count].host[0] = '\0';
cfg->remotes[cfg->remote_count].user[0] = '\0';
cfg->remotes[cfg->remote_count].path[0] = '\0';
cfg->remote_count++;
return cfg->remote_count - 1;
}
static Remote *find_remote(Config *cfg, const char *name) {
int i;
if (!name || name[0] == '\0') {
name = "default";
}
for (i = 0; i < cfg->remote_count; i++) {
if (strcmp(cfg->remotes[i].name, name) == 0) {
return &cfg->remotes[i];
}
}
return NULL;
}
static int parse_config_file(const char *path, Config *cfg) {
FILE *f = fopen(path, "r");
char line[2048];
enum { SEC_NONE, SEC_REMOTE, SEC_SYNC, SEC_COMMANDS } section = SEC_NONE;
int current_remote = -1;
if (!f) {
perror("fopen");
return -1;
}
while (fgets(line, sizeof(line), f) != NULL) {
char *p = line;
trim(p);
if (p[0] == '\0') {
continue;
}
if (p[0] == '#' || p[0] == ';') {
continue;
}
if (p[0] == '[') {
char *end = strchr(p, ']');
if (end) {
char namebuf[128];
*end = '\0';
strncpy(namebuf, p + 1, sizeof(namebuf) - 1);
namebuf[sizeof(namebuf) - 1] = '\0';
trim(namebuf);
if (strncmp(namebuf, "remote", 6) == 0) {
char *colon = strchr(namebuf, ':');
if (colon) {
*colon = '\0';
colon++;
trim(colon);
current_remote = ensure_remote(cfg, colon);
} else {
current_remote = ensure_remote(cfg, "default");
}
section = SEC_REMOTE;
} else if (strcmp(namebuf, "sync") == 0) {
section = SEC_SYNC;
current_remote = -1;
} else if (strcmp(namebuf, "commands") == 0) {
section = SEC_COMMANDS;
current_remote = -1;
} else {
section = SEC_NONE;
current_remote = -1;
}
}
continue;
}
if (section == SEC_NONE) {
continue;
}
{
char *eq = strchr(p, '=');
if (!eq) {
continue;
}
*eq = '\0';
char *key = p;
char *value = eq + 1;
trim(key);
trim(value);
if (section == SEC_REMOTE && current_remote >= 0) {
Remote *r = &cfg->remotes[current_remote];
if (strcmp(key, "host") == 0) {
strncpy(r->host, value, sizeof(r->host) - 1);
r->host[sizeof(r->host) - 1] = '\0';
} else if (strcmp(key, "user") == 0) {
strncpy(r->user, value, sizeof(r->user) - 1);
r->user[sizeof(r->user) - 1] = '\0';
} else if (strcmp(key, "path") == 0) {
strncpy(r->path, value, sizeof(r->path) - 1);
r->path[sizeof(r->path) - 1] = '\0';
}
} else if (section == SEC_SYNC) {
if (strcmp(key, "exclude") == 0) {
strncpy(cfg->exclude, value, sizeof(cfg->exclude) - 1);
cfg->exclude[sizeof(cfg->exclude) - 1] = '\0';
}
} else if (section == SEC_COMMANDS) {
if (strcmp(key, "build") == 0) {
strncpy(cfg->build_cmd, value, sizeof(cfg->build_cmd) - 1);
cfg->build_cmd[sizeof(cfg->build_cmd) - 1] = '\0';
} else if (strcmp(key, "test") == 0) {
strncpy(cfg->test_cmd, value, sizeof(cfg->test_cmd) - 1);
cfg->test_cmd[sizeof(cfg->test_cmd) - 1] = '\0';
}
}
}
}
fclose(f);
return 0;
}
static int run_rsync(const Config *cfg, const Remote *remote) {
pid_t pid;
int status;
char remote_spec[512];
char dest[1024];
if (remote->host[0] == '\0' || remote->path[0] == '\0') {
fprintf(stderr, "Missing remote host or path in config\n");
return -1;
}
if (remote->user[0] != '\0') {
if (snprintf(remote_spec, sizeof(remote_spec), "%s@%s", remote->user, remote->host) >= (int)sizeof(remote_spec)) {
fprintf(stderr, "Remote spec too long\n");
return -1;
}
} else {
if (snprintf(remote_spec, sizeof(remote_spec), "%s", remote->host) >= (int)sizeof(remote_spec)) {
fprintf(stderr, "Remote spec too long\n");
return -1;
}
}
if (snprintf(dest, sizeof(dest), "%s:%s", remote_spec, remote->path) >= (int)sizeof(dest)) {
fprintf(stderr, "Destination too long\n");
return -1;
}
printf("[remake] syncing to %s\n", dest);
fflush(stdout);
pid = fork();
if (pid < 0) {
perror("fork");
return -1;
}
if (pid == 0) {
char *argv[128];
int ai = 0;
argv[ai++] = "rsync";
argv[ai++] = "-az";
argv[ai++] = "--delete";
if (cfg->exclude[0] != '\0') {
char exclude_copy[1024];
strncpy(exclude_copy, cfg->exclude, sizeof(exclude_copy) - 1);
exclude_copy[sizeof(exclude_copy) - 1] = '\0';
char *token = strtok(exclude_copy, ",");
while (token && ai < 120) {
trim(token);
if (token[0] != '\0') {
argv[ai++] = "--exclude";
argv[ai++] = token;
}
token = strtok(NULL, ",");
}
}
argv[ai++] = "./";
argv[ai++] = dest;
argv[ai] = NULL;
execvp("rsync", argv);
perror("execvp rsync");
_exit(127);
}
if (waitpid(pid, &status, 0) < 0) {
perror("waitpid");
return -1;
}
if (WIFEXITED(status)) {
int code = WEXITSTATUS(status);
if (code != 0) {
fprintf(stderr, "[remake] rsync failed with exit code %d\n", code);
return code;
}
} else {
fprintf(stderr, "[remake] rsync terminated abnormally\n");
return -1;
}
printf("[remake] sync complete\n");
return 0;
}
static int run_remote_command(const Remote *remote, const char *label, const char *cmd) {
pid_t pid;
int status;
char remote_spec[512];
char remote_cmd[1024];
if (remote->host[0] == '\0' || remote->path[0] == '\0') {
fprintf(stderr, "Missing remote host or path in config\n");
return -1;
}
if (remote->user[0] != '\0') {
if (snprintf(remote_spec, sizeof(remote_spec), "%s@%s", remote->user, remote->host) >= (int)sizeof(remote_spec)) {
fprintf(stderr, "Remote spec too long\n");
return -1;
}
} else {
if (snprintf(remote_spec, sizeof(remote_spec), "%s", remote->host) >= (int)sizeof(remote_spec)) {
fprintf(stderr, "Remote spec too long\n");
return -1;
}
}
if (snprintf(remote_cmd, sizeof(remote_cmd), "cd %s && %s", remote->path, cmd) >= (int)sizeof(remote_cmd)) {
fprintf(stderr, "Remote command too long\n");
return -1;
}
printf("[remake] === %s (%s) ===\n", label, cmd);
fflush(stdout);
pid = fork();
if (pid < 0) {
perror("fork");
return -1;
}
if (pid == 0) {
char *argv[8];
int ai = 0;
argv[ai++] = "ssh";
argv[ai++] = remote_spec;
argv[ai++] = remote_cmd;
argv[ai] = NULL;
execvp("ssh", argv);
perror("execvp ssh");
_exit(127);
}
if (waitpid(pid, &status, 0) < 0) {
perror("waitpid");
return -1;
}
if (WIFEXITED(status)) {
int code = WEXITSTATUS(status);
if (code != 0) {
fprintf(stderr, "[remake] %s failed with exit code %d\n", label, code);
return code;
}
printf("[remake] %s done\n", label);
return 0;
} else {
fprintf(stderr, "[remake] %s terminated abnormally\n", label);
return -1;
}
}
static void usage(const char *prog) {
fprintf(stderr, "Usage: %s [-t target] [build|test|run <command...>]\n", prog);
}
int main(int argc, char **argv) {
Config cfg;
char *config_path;
int rc;
const char *mode = "test";
const char *target_name = "default";
int i = 1;
char cmd_buf[1024];
Remote *remote;
init_config(&cfg);
if (i < argc && strcmp(argv[i], "-t") == 0) {
if (i + 1 >= argc) {
usage(argv[0]);
return 1;
}
target_name = argv[i + 1];
i += 2;
}
if (i < argc) {
if (strcmp(argv[i], "build") == 0) {
mode = "build";
i++;
} else if (strcmp(argv[i], "test") == 0) {
mode = "test";
i++;
} else if (strcmp(argv[i], "run") == 0) {
mode = "run";
i++;
} else {
usage(argv[0]);
return 1;
}
}
config_path = find_config_file();
if (!config_path) {
fprintf(stderr, "Could not find .remake.conf in this directory or any parent\n");
return 1;
}
rc = parse_config_file(config_path, &cfg);
free(config_path);
if (rc != 0) {
fprintf(stderr, "Failed to parse config\n");
return 1;
}
remote = find_remote(&cfg, target_name);
if (!remote) {
fprintf(stderr, "No [remote] configuration found for target '%s'\n", target_name);
return 1;
}
if (remote->host[0] == '\0' || remote->path[0] == '\0') {
fprintf(stderr, "Config for target '%s' must define host and path\n", target_name);
return 1;
}
rc = run_rsync(&cfg, remote);
if (rc != 0) {
return rc;
}
if (strcmp(mode, "run") == 0) {
if (i >= argc) {
fprintf(stderr, "run mode requires a command\n");
return 1;
}
cmd_buf[0] = '\0';
for (; i < argc; i++) {
size_t len = strlen(cmd_buf);
size_t remaining = sizeof(cmd_buf) - len;
if (remaining <= 1) {
fprintf(stderr, "Command too long\n");
return 1;
}
if (len > 0) {
strncat(cmd_buf, " ", remaining - 1);
len = strlen(cmd_buf);
remaining = sizeof(cmd_buf) - len;
if (remaining <= 1) {
fprintf(stderr, "Command too long\n");
return 1;
}
}
strncat(cmd_buf, argv[i], remaining - 1);
}
return run_remote_command(remote, "RUN", cmd_buf);
}
if (strcmp(mode, "build") == 0 || strcmp(mode, "test") == 0) {
if (cfg.build_cmd[0] == '\0') {
fprintf(stderr, "No build command defined in [commands] build\n");
return 1;
}
rc = run_remote_command(remote, "BUILD", cfg.build_cmd);
if (rc != 0) {
return rc;
}
if (strcmp(mode, "test") == 0) {
if (cfg.test_cmd[0] == '\0') {
fprintf(stderr, "No test command defined in [commands] test\n");
return 1;
}
return run_remote_command(remote, "TEST", cfg.test_cmd);
}
return 0;
}
usage(argv[0]);
return 1;
}