-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
182 lines (152 loc) · 3.95 KB
/
main.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
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
#include <stdio.h>
#include <stdlib.h>
#include <libgen.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
#include <dirent.h>
#include <stdarg.h>
#include <getopt.h>
#include <libtar.h>
#include <fcntl.h>
#define BUFFER_SIZE 256*4
const char *ignored_directories[] = {
".git",
".venv",
".zig-cache",
"node_modules",
};
int num_ignored_directories = sizeof(ignored_directories) / sizeof(ignored_directories[0]);
typedef struct {
TAR *tar;
int verbose;
const char *project_name;
const char *tag;
const char *directory_path;
char snapshot_file[BUFFER_SIZE];
} Snapshot;
const char *get_cwd_basename(const char *arg0) {
char buffer[BUFFER_SIZE];
char *cwd = getcwd(buffer, BUFFER_SIZE);
if (cwd == NULL) {
fprintf(stderr, "Error: getting current directory %s.\n", arg0);
exit(1);
}
char *base = basename(buffer);
char *b = malloc(strlen(base) + 1);
if (b == NULL) {
fprintf(stderr, "Error: memory allocation failed.\n");
exit(1);
}
strncpy(b, base, strlen(base) + 1);
return b;
}
void add_to_snapshot(TAR *tar, const char *directory_path, int verbose) {
struct dirent *entry;
DIR *dir = opendir(directory_path);
if (dir == NULL) {
fprintf(stderr, "Error: could not open dir %s.\n", directory_path);
exit(1);
}
struct stat s;
char file_path[BUFFER_SIZE];
while ((entry = readdir(dir)) != NULL) {
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
continue;
}
snprintf(file_path, sizeof(file_path), "%s/%s", directory_path, entry->d_name);
stat(file_path, &s);
if (S_ISDIR(s.st_mode)) {
int skip = 0;
for (int i = 0; i < num_ignored_directories; i++) {
if (strcmp(basename(file_path), ignored_directories[i]) == 0) {
skip = 1;
}
}
if (skip == 0) {
add_to_snapshot(tar, file_path, verbose);
} else {
if (verbose > 0) {
fprintf(stdout, "- %s\n", file_path);
}
}
} else {
if (verbose > 0) {
fprintf(stdout, "+ %s\n", file_path);
}
if (tar_append_file(tar, file_path, file_path) == -1) {
fprintf(stderr, "Error: could not append file %s.\n", file_path);
exit(1);
}
}
}
closedir(dir);
}
void help(const char *argv0) {
printf("Usage: %s [options]\n"
"\nAvailable options:\n"
" -t,--tag=1.4 set tag of the snapshot\n"
" -h,--help this help\n"
" -v,--verbose show detailed log\n",
argv0);
}
int main(int argc, char **argv) {
Snapshot sn = {0};
sn.tag = NULL;
const char short_options[] = "t:vh";
const struct option long_options[] = {
{ "tag", 1, NULL, 't' },
{ "help", 0, NULL, 'h' },
{ "verbose", 0, NULL, 'v' },
{ 0 },
};
int opt;
while ((opt = getopt_long(argc, argv, short_options, long_options, NULL)) != -1) {
switch (opt) {
case 't': {
sn.tag = optarg;
} break;
case 'v': {
sn.verbose = 1;
} break;
case 'h': {
help(argv[0]);
return 0;
} break;
default: {
fprintf(stdout, "Missing options. Check help.\n");
return 1;
} break;
}
}
if (sn.tag == NULL) {
/* free((void*)sn.project_name); */
help(argv[0]);
return 0;
}
sn.directory_path = ".";
sn.project_name = get_cwd_basename(argv[0]);
snprintf(sn.snapshot_file, sizeof(sn.snapshot_file), "../%s-v%s.tar", sn.project_name, sn.tag);
fprintf(stdout, "tag: %s\n", sn.tag);
fprintf(stdout, "snapshot: %s\n", sn.snapshot_file);
if (remove(sn.snapshot_file) == 0) {
if (sn.verbose > 0) {
fprintf(stdout, "! snapshot file %s deleted\n", sn.snapshot_file);
}
}
if (tar_open(&sn.tar, sn.snapshot_file, NULL, O_WRONLY | O_CREAT, 0644, TAR_GNU) == -1) {
fprintf(stderr, "Error: could not create tar file %s.\n", sn.snapshot_file);
return 1;
}
add_to_snapshot(sn.tar, sn.directory_path, sn.verbose);
if (tar_append_eof(sn.tar) == -1) {
fprintf(stderr, "Error: could not append EOF to tar.\n");
return 1;
}
if (tar_close(sn.tar) == -1) {
fprintf(stderr, "Error: could not close tar file.\n");
return 1;
}
/* free((void*)sn.project_name); */
return 0;
}