-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcowtool.c
96 lines (76 loc) · 1.77 KB
/
cowtool.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <unistd.h>
#include "storage.h"
#include "slist.h"
#include "util.h"
#include "root_list.h"
slist*
image_ls_tree(const char* base)
{
struct stat st;
slist* zs = 0;
slist* xs = storage_list(base);
for (; xs; xs = xs->next) {
char* path = path_join(base, xs->data);
zs = s_cons(path, zs);
}
return zs;
}
void
print_usage(const char* name)
{
fprintf(stderr, "Usage: %s cmd ...\n", name);
exit(1);
}
int
main(int argc, char* argv[])
{
if (argc < 3) {
print_usage(argv[0]);
}
const char* cmd = argv[1];
const char* img = argv[2];
if (streq(cmd, "new")) {
assert(argc == 3);
storage_init(img); // 1
printf("Created disk image: %s\n", img);
return 0;
}
if (access(img, R_OK) == -1) {
fprintf(stderr, "No such image: %s\n", img);
return 1;
}
storage_init(img); // 0
if (streq(cmd, "ls")) {
printf("List for %s\n", img);
slist* xs = image_ls_tree("/");
for (slist* it = xs; it != 0; it = it->next) {
printf("%s\n", it->data);
}
s_free(xs);
return 0;
}
if (streq(cmd, "versions")) {
printf("Versions for %s\n", img);
slist* xs = rootlist_version_table();
for (slist* it = xs; it != 0; it = it->next) {
printf("%s\n", it->data);
}
s_free(xs);
return 0;
return 0;
}
if(argc < 4) {
print_usage(argv[0]);
}
if (streq(cmd, "rollback")) {
int vnum = atoi(argv[3]);
printf("Rollback %s to version %d\n", img, vnum);
swap_root(vnum);
return 0;
}
print_usage(argv[0]);
}