-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathcheck.c
51 lines (41 loc) · 1.09 KB
/
check.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
#define USAGE "usage: plash check IMAGE_ID PATH\n"
#define _GNU_SOURCE
#include <libgen.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <time.h>
#include <plash.h>
int check_main(int argc, char *argv[]) {
if (argc != 3) {
fputs(USAGE, stderr);
return EXIT_FAILURE;
}
char *image_id = argv[1];
char *path = argv[2];
struct stat attr;
if (stat(path, &attr) == -1) {
pl_fatal("stat(%s)", path);
}
char *cache_key = NULL;
if (asprintf(&cache_key, "check:%s:%lu:%ld:%ld",
image_id,
(unsigned long)attr.st_mtime,
(unsigned long)attr.st_ctime,
(long)attr.st_ino) == -1) {
pl_fatal("asprintf");
}
char *existing_image_id = plash("map", cache_key);
if (strcmp(existing_image_id, "") != 0) {
puts(existing_image_id);
return EXIT_SUCCESS;
} else {
char *fresh_image_id = plash("stack", image_id, plash("mkdtemp"));
plash("map", cache_key, fresh_image_id);
puts(fresh_image_id);
}
return EXIT_SUCCESS;
}