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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
|
/*
* Copyright (c) 2026, Chloe M., et al.
* Provided under the BSD-3 clause.
*/
#include <dirent.h>
#include <stdint.h>
#include <stdlib.h>
#include <stddef.h>
#include <fcntl.h>
#include <stdbool.h>
#include <strings.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include "bop/hash.h"
#include "bop/common.h"
#include "bop/build.h"
/*
* Build parameters
*
* @cflags: List of CFLAGS
* @hashfd: Manifest file descriptor
* @n_file: Number of files to build
* @gen_manifest: If true, generate a manifest
*/
struct build_params {
char *cflags;
int hashfd;
size_t n_file;
bool gen_manifest;
};
/*
* Obtain the file extension of a file
*
* @filename: Filename to obtain extension of
*/
static const char *
get_extension(const char *filename)
{
const char *p;
bool have_dot = false;
if (filename == NULL) {
return NULL;
}
p = filename;
while (*p != '\0') {
if (*p == '.') {
have_dot = true;
} else if (have_dot) {
--p;
return p;
}
++p;
}
return NULL;
}
/*
* Truncate a file extension
*
* @filename: Filename to truncate file extension from
*
* XXX: Returns memory allocated with strdup(), remember to free!
*/
static char *
trunc_extension(const char *filename)
{
char *p, *p1;
if ((p = strdup(filename)) == NULL) {
return NULL;
}
p1 = p;
while (*p1 != '\0') {
if (*p1 == '.') {
*p1 = '\0';
return p;
}
++p1;
}
free(p);
return NULL;
}
/*
* Obtain the filename from a manifest line
*
* XXX: Returns memory allocate with strdup(), remember to free!
*/
static char *
manifest_filename(const char *manifest_line)
{
char *p, *p1;
if (manifest_line == NULL) {
return NULL;
}
if ((p = strdup(manifest_line)) == NULL) {
printf("fatal: out of memory\n");
return NULL;
}
p1 = p;
while (*p1 != '\0') {
if (*p1++ == ':') {
*--p1 = '\0';
return p;
}
}
free(p);
return NULL;
}
/*
* Returns true if the given file is accepted as a buildable entity
*
* @filename: Filename to check
*/
static bool
file_accepted(const char *filename)
{
const char *ext;
if (filename == NULL) {
return false;
}
ext = get_extension(filename);
if (strcmp(ext, ".c") == 0)
return true;
return false;
}
static int
invoke_cc(const char *cfile_path, struct build_params *params)
{
char *trunc_ext, *cflags;
char cmdbuf[256];
char outpath[256];
char outbuf[32];
FILE *pipe;
if (cfile_path == NULL || params == NULL) {
return -1;
}
trunc_ext = trunc_extension(cfile_path);
if (trunc_ext == NULL) {
return -1;
}
cflags = (params->cflags == NULL) ? "" : params->cflags;
snprintf(outpath, sizeof(outpath), "%s.o", trunc_ext);
snprintf(cmdbuf, sizeof(cmdbuf), "%s -c %s %s -o %s", BOP_CC, cflags, cfile_path, outpath);
if ((pipe = popen(cmdbuf, "r")) == NULL) {
printf("fatal: failed to invoke cc\n");
return -1;
}
printf("[CC]\t\t%s\n", cfile_path);
while ((fgets(outbuf, sizeof(outbuf), pipe)) != NULL) {
printf("%s", outbuf);
}
pclose(pipe);
return 0;
}
static int
compile(const char *dirpath, const char *filename, struct build_params *params)
{
const char *ext;
char pathbuf[256];
char hash[BOP_HASH_LEN];
char manifest_entry[128];
int error;
if (dirpath == NULL || filename == NULL) {
return -1;
}
if (params == NULL) {
return -1;
}
ext = get_extension(filename);
/* Ignore files with no extensions */
if (ext == NULL) {
return 0;
}
snprintf(pathbuf, sizeof(pathbuf), "%s/%s", dirpath, filename);
if ((error = bop_hash_file(pathbuf, hash)) < 0) {
printf("fatal: failed to hash %s\n", pathbuf);
return error;
}
/* Used per accepted file to write hashes */
#define WRITE_HASH() \
snprintf(manifest_entry, sizeof(manifest_entry), "%s:%s\n", filename, hash); \
write(params->hashfd, manifest_entry, strlen(manifest_entry));
/* Build all CFILES */
if (strcmp(ext, ".c") == 0) {
error = invoke_cc(pathbuf, params);
if (error < 0) {
printf("fatal: cc invocation failure\n");
return error;
}
if (params->gen_manifest) {
WRITE_HASH();
}
return error;
}
#undef WRITE_HASH
return 0;
}
static void
clean_object(const char *dirpath, struct dirent *dirent)
{
char pathbuf[256];
const char *filename, *ext;
if (dirpath == NULL || dirent == NULL) {
return;
}
/* Ignore non regulars */
if (dirent->d_type != DT_REG) {
return;
}
/* Ignore files without an extension */
filename = dirent->d_name;
ext = get_extension(filename);
if (ext == NULL) {
return;
}
/* Ignore non object files */
if (strcmp(ext, ".o") != 0) {
return;
}
snprintf(pathbuf, sizeof(pathbuf), "%s/%s", dirpath, dirent->d_name);
printf("clean %s\n", pathbuf);
remove(pathbuf);
}
/*
* Initialize the cflags field within build parameters
*
* @dirpath: Target directory path
* @params: Build parameters
*
* Returns zero on success
*/
static int
build_init_cflags(const char *dirpath, struct build_params *params)
{
char pathbuf[256], *databuf;
size_t len, fsize;
int fd;
if (params == NULL) {
return -1;
}
snprintf(pathbuf, sizeof(pathbuf), "%s/.bop/cflags", dirpath);
if ((fd = open(pathbuf, O_RDONLY)) < 0) {
/* Fake success, cflags are optional */
return 0;
}
/* Grab the file size */
fsize = lseek(fd, 0, SEEK_END);
lseek(fd, 0, SEEK_SET);
/* Allocate the data buffer */
if ((databuf = malloc(fsize)) == NULL) {
printf("fatal: out of memory\n");
close(fd);
return -1;
}
if ((len = read(fd, databuf, fsize)) < 0) {
perror("read");
printf("fatal: failed to read %s\n", pathbuf);
close(fd);
return -1;
}
databuf[fsize - 1] = '\0';
params->cflags = databuf;
close(fd);
return 0;
}
static int
build_from_dir(const char *dirpath, DIR *dir, build_op_t bop, struct build_params *params)
{
struct dirent *dirent;
int error;
if (dir == NULL) {
return -1;
}
/* Iterate each file */
while ((dirent = readdir(dir)) != NULL) {
if (dirent->d_name[0] == '.') {
continue;
}
switch (bop) {
case BUILD_OP_BUILD:
error = compile(dirpath, dirent->d_name, params);
if (error < 0) {
printf("fatal: failed to build '%s'\n", dirent->d_name);
return error;
}
break;
case BUILD_OP_CLEAN:
clean_object(dirpath, dirent);
break;
}
}
return 0;
}
static int
try_mf_make(const char *dirpath, const char *manifest_entry,
struct build_params *params, char hashres[BOP_HASH_LEN],
bool *rehash)
{
char *p, *tok, *name;
char *save;
char pathbuf[256];
char hash[BOP_HASH_LEN];
int error;
if (dirpath == NULL || manifest_entry == NULL) {
return -1;
}
if (rehash == NULL) {
return -1;
}
*rehash = false;
if ((p = strdup(manifest_entry)) == NULL) {
printf("fatal: out of memory\n");
return -1;
}
/* Grab the filename and construct a path */
tok = strtok_r(p, ":", &save);
snprintf(pathbuf, sizeof(pathbuf), "%s/%s", dirpath, tok);
/* Copy the name */
if ((name = strdup(tok)) == NULL) {
printf("fatal: out of memory\n");
return -1;
}
/* Grab the old hash and create a new hash */
tok = strtok_r(NULL, ":", &save);
if ((error = bop_hash_file(pathbuf, hash)) < 0) {
printf("[!] failed to hash %s\n", pathbuf);
free(p);
free(name);
return -1;
}
tok[strlen(tok) - 1] = '\0';
if (strncmp(tok, hash, sizeof(hash)) != 0) {
error = compile(dirpath, name, params);
if (error < 0) {
printf("fatal: failed to build '%s'\n", name);
free(p);
free(name);
return -1;
}
*rehash = true;
}
memcpy(hashres, hash, sizeof(hash));
free(p);
free(name);
return 0;
}
static int
build_from_manifest(const char *dirpath, int hashfd, struct build_params *params)
{
char line[128], *filename;
char updated_line[128];
char newhash[BOP_HASH_LEN];
FILE *fp;
size_t exp_n_file, pos;
size_t oldpos;
bool rehash;
if (dirpath == NULL || params == NULL) {
return -1;
}
if ((fp = fdopen(hashfd, "r+")) == NULL) {
perror("fdopen");
return -1;
}
if (fgets(line, sizeof(line), fp) == NULL) {
printf("fatal: unable to read manifest\n");
fclose(fp);
return -1;
}
/*
* The first line is the expected file count, if they do not match then we are to
* rebuild the entire manifest.
*/
exp_n_file = atoi(line);
if (params->n_file != exp_n_file) {
return -1;
}
params->gen_manifest = false;
pos = ftell(fp);
while (fgets(line, sizeof(line), fp) != NULL) {
if (try_mf_make(dirpath, line, params, newhash, &rehash) < 0) {
fclose(fp);
return -1;
}
if (rehash) {
oldpos = ftell(fp);
if ((filename = manifest_filename(line)) == NULL)
return -1;
/* Update the line */
fseek(fp, pos, SEEK_SET);
snprintf(updated_line, sizeof(updated_line), "%s:%s", filename, newhash);
fwrite(updated_line, 1, strlen(updated_line), fp);
/* Reset position and free the filename */
fseek(fp, oldpos, SEEK_SET);
free(filename);
}
pos = ftell(fp);
}
fclose(fp);
return 0;
}
int
bop_build_dir(const char *dirpath, build_op_t bop)
{
struct build_params params;
DIR *dir;
struct dirent *dirent;
char pathbuf[128];
char countbuf[16];
int error, hashfd = -1;
bool build_dirs = true;
dir = opendir(dirpath);
if (dir == NULL) {
perror("opendir");
return -1;
}
bzero(¶ms, sizeof(params));
if ((error = build_init_cflags(dirpath, ¶ms)) < 0) {
closedir(dir);
return error;
}
/* Count the number of directory entries */
while ((dirent = readdir(dir)) != NULL) {
if (dirent->d_name[0] == '.') {
continue;
}
if (file_accepted(dirent->d_name)) {
++params.n_file;
}
}
rewinddir(dir);
params.gen_manifest = true;
/*
* If a manifest is not present then we should create one, otherwise
* we are to read it build based on it.
*/
snprintf(pathbuf, sizeof(pathbuf), "%s/.bop/manifest.hash", dirpath);
if (access(pathbuf, F_OK) != 0 && bop != BUILD_OP_CLEAN) {
hashfd = open(pathbuf, O_RDWR | O_CREAT, 0666);
if (hashfd < 0) {
perror("open");
printf("fatal: failed to open manifest\n");
return -1;
}
params.hashfd = hashfd;
snprintf(countbuf, sizeof(countbuf), "%zu\n", params.n_file);
write(hashfd, countbuf, strlen(countbuf));
} else if (bop != BUILD_OP_CLEAN) {
hashfd = open(pathbuf, O_RDWR);
if (hashfd < 0) {
perror("open");
printf("fatal: failed to open manifest\n");
return -1;
}
build_dirs = false;
params.hashfd = hashfd;
error = build_from_manifest(dirpath, hashfd, ¶ms);
/* Remove manifest and build from dir if error */
if (error < 0) {
printf("[*] flushing manifest\n");
remove(pathbuf);
build_dirs = true;
}
}
/* Remove the manifest if we are cleaning */
if (bop == BUILD_OP_CLEAN) {
build_dirs = true;
remove(pathbuf);
}
/* Build from directory if not manifest */
if (build_dirs) {
error = build_from_dir(dirpath, dir, bop, ¶ms);
if (error < 0)
return error;
}
if (params.cflags != NULL) {
free(params.cflags);
params.cflags = NULL;
}
close(hashfd);
return 0;
}
|