-
Notifications
You must be signed in to change notification settings - Fork 15
/
recall.c
85 lines (73 loc) · 1.99 KB
/
recall.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
#define USAGE "usage: plash recall PLASH_CMD ...\n"
#define _GNU_SOURCE
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <plash.h>
int command_accepts_image_id(char *cmd) {
return (
(strcmp(cmd, "build") == 0) ||
(strcmp(cmd, "nodepath") == 0) ||
(strcmp(cmd, "parent") == 0) ||
(strcmp(cmd, "rm") == 0) ||
(strcmp(cmd, "run") == 0) ||
(strcmp(cmd, "run:persist") == 0) ||
(strcmp(cmd, "check") == 0) ||
(strcmp(cmd, "stack") == 0) ||
(strcmp(cmd, "id") == 0)
);
}
int command_prints_image_id(char *cmd) {
return (
(strcmp(cmd, "pull:docker") == 0) ||
(strcmp(cmd, "pull:lxc") == 0) ||
(strcmp(cmd, "pull:tarfile") == 0) ||
(strcmp(cmd, "pull:url") == 0) ||
(strcmp(cmd, "build") == 0) ||
(strcmp(cmd, "check") == 0) ||
// (strcmp(cmd, "id") == 0) ||
(strcmp(cmd, "parent") == 0)
);
}
int recall_main(int argc, char *argv[]) {
char *cache_key;
char *cmd;
if (asprintf(&cache_key, "recall:%d", getsid(0)) == -1)
pl_fatal("asprintf");
if (argc <= 1) {
fputs(USAGE, stderr);
return EXIT_FAILURE;
}
pl_array_add("/proc/self/exe");
if (strcmp(argv[1], "cache") == 0) {
pl_array_add(argv[1]);
pl_array_add(argv[2]);
cmd = argv[2];
argv++;
} else {
pl_array_add(argv[1]);
cmd = argv[1];
}
if (command_accepts_image_id(cmd)) {
char *input_image_id = plash("map", cache_key);
if (strcmp(input_image_id, "") == 0) {
errno = 0;
pl_fatal("Cannot recall any image id. Try` plash recall pull...`");
}
pl_array_add(input_image_id);
}
argv++; while (*(argv++))
pl_array_add(*argv);
pl_array_add(NULL);
if (command_prints_image_id(cmd)) {
char *output_image_id = pl_firstline(pl_check_output(pl_array));
plash("map", cache_key, output_image_id);
} else {
// noo deed to call a subprocess, we can exec right away
execvp(pl_array[0], pl_array);
pl_fatal("execvp");
}
return EXIT_SUCCESS;
}