-
Notifications
You must be signed in to change notification settings - Fork 1
/
ntar.c
executable file
·299 lines (254 loc) · 7.78 KB
/
ntar.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
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
/* Standard headers C99 */
#include <stdarg.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define VER "1.0"
/* Platform-specific header */
#include "os.h"
/* Microtar library header */
#include "microtar.h"
#define BLOCK_LEN (1024 * 4) // 4 KB
#define IFARG(str, dop) if (!strcmp(argv[1], (str)) && dop)
typedef struct {
char **names;
size_t fnum;
} ntar_flist_t;
typedef enum {
NTAR_LIST,
NTAR_EXTRACT,
} ntar_content_act_t;
const char *NO_MEM_MSG = "Not enough memory";
const char *UNABLE_TO_OPEN_TAR_MSG = "Unable to open tar '%s'! %s\n";
const char *UNABLE_TO_OPEN_MSG = "Unable to open file '%s!'\n";
bool mkdir_parent(const char *full_path)
{
const char *SEP = "\\/";
bool status = true;
char *tmp_str = calloc(OS_PATH_MAX, sizeof(char));
char *full_path_dup = strdup(full_path);
if (!full_path_dup || !tmp_str) {
return false;
}
#if 0
if (full_path[0] == '/' || full_path[0] == '\\') {
tmp_str[0] = '\0';
} else {
strcpy(tmp_str, ".");
}
#endif
char *pch = strtok(full_path_dup, SEP);
while (pch != NULL) {
strcat(tmp_str, "/");
strcat(tmp_str, pch);
puts(tmp_str);
if (!os_mkdir(tmp_str)) {
status = false;
break;
}
pch = strtok(NULL, SEP);
}
free(tmp_str);
free(full_path_dup);
return status;
}
void ntar_list(mtar_t *tar, mtar_header_t *header)
{
if (header->type == MTAR_TDIR) {
printf("Folder: %s\n", header->name);
} else {
printf("File: %s (%zu bytes)\n", header->name, header->size);
}
}
void ntar_extract(mtar_t *tar, mtar_header_t *header)
{
char *full_name = strdup(header->name);
char *dir_name = header->name;
if (header->type == MTAR_TREG) {
dir_name = dirname(full_name);
}
if (!os_mkdir(dir_name)) {
printf("Error creating folder '%s'!\n", header->name);
goto exit;
}
if (header->type == MTAR_TREG) {
FILE *fd = fopen(header->name, MTAR_FMODE_W);
if (!fd) {
printf(UNABLE_TO_OPEN_MSG, header->name);
goto exit;
}
size_t new_size = 0;
size_t readed = 0;
void *data = malloc(BLOCK_LEN);
if (!data) {
puts(NO_MEM_MSG);
goto exit;
}
while (readed < header->size) {
new_size = header->size - readed;
if (new_size >= BLOCK_LEN) {
new_size = BLOCK_LEN;
}
int rd_st = mtar_read_data(tar, data, new_size);
if (rd_st) {
printf("Error reading file '%s' from tar! %s", header->name, mtar_strerror(rd_st));
break;
}
if (fwrite(data, new_size, 1, fd) != 1) {
printf("Error writing to file '%s'!\n", header->name);
break;
}
readed += new_size;
}
free(data);
}
printf("Extracted %s\n", header->name);
exit:
free(full_name);
}
int ntar_work_content(const char *tar_fname, ntar_flist_t flist, ntar_content_act_t act)
{
mtar_t tar;
mtar_header_t tar_header;
int status = MTAR_ESUCCESS;
status = mtar_open(&tar, tar_fname, MTAR_FMODE_R);
if (status) {
printf(UNABLE_TO_OPEN_TAR_MSG, tar_fname, mtar_strerror(status));
return status;
}
if (flist.fnum > 0) {
for (size_t i = 0; i < flist.fnum; i++) {
status = mtar_find(&tar, flist.names[i], &tar_header);
if (status != MTAR_ESUCCESS) {
printf("Error extracting file '%s'! %s\n", flist.names[i], mtar_strerror(status));
goto close;
}
ntar_extract(&tar, &tar_header);
}
goto close;
}
void (*action_fn)(mtar_t * tar, mtar_header_t * header);
if (act == NTAR_EXTRACT) {
action_fn = ntar_extract;
} else {
action_fn = ntar_list;
}
while (!(status = mtar_read_header(&tar, &tar_header))) {
if (status != MTAR_ENULLRECORD && status) {
printf("Error: %s!\n", mtar_strerror(status));
break;
}
(*action_fn)(&tar, &tar_header);
mtar_next(&tar);
}
close:
mtar_close(&tar);
return status;
}
int ntar_add_files(const char *tar_fname, bool new_tar, ntar_flist_t flist)
{
mtar_t tar;
mtar_header_t tar_header;
int tar_err_h = 0;
int tar_err_d = 0;
const char *mode = MTAR_FMODE_W_PLUS;
if (!new_tar) {
mode = MTAR_FMODE_A_PLUS;
}
int status = mtar_open(&tar, tar_fname, mode);
if (status) {
printf(UNABLE_TO_OPEN_TAR_MSG, tar_fname, mtar_strerror(status));
return status;
}
for (size_t i = 0; i < flist.fnum; i++) {
size_t size = 0;
if (mtar_find(&tar, flist.names[i], &tar_header) == MTAR_ESUCCESS) {
printf("'%s' already added. Pass!\n", flist.names[i]);
continue;
}
if (os_is_dir(flist.names[i])) {
tar_err_h = mtar_write_dir_header(&tar, flist.names[i]);
} else {
FILE *fd = fopen(flist.names[i], MTAR_FMODE_R);
if (!fd) {
printf(UNABLE_TO_OPEN_MSG, flist.names[i]);
continue;
}
size = os_get_fsize(flist.names[i]);
tar_err_h = mtar_write_file_header(&tar, flist.names[i], size);
while (!feof(fd)) {
void *file_data = malloc(BLOCK_LEN);
if (!file_data) {
puts(NO_MEM_MSG);
break;
}
size_t rsize = fread(file_data, 1, BLOCK_LEN, fd);
tar_err_d = mtar_write_data(&tar, file_data, rsize);
free(file_data);
}
fclose(fd);
}
if (tar_err_h || tar_err_d) {
printf("Not added '%s'!\n", flist.names[i]);
} else {
printf("Added '%s'\n", flist.names[i]);
}
}
mtar_close(&tar);
return status;
}
void show_help(void)
{
printf(
" _\n"
" _ _| |_ __ _ _ _ ntar (NANO tar) - v" VER "\n"
"| ' \\ _/ _` | '_| License: GPL-2.0-or-later\n"
"|_||_\\__\\__,_|_| Author - Maxim Logaev (2022 year)\n"
"\n"
"Usage: ntar [options] [tar-file] [files ...]\n"
"Examples:\n"
" ntar -c test.tar foo bar # Create archive.tar from files foo and bar."
"\n"
" ntar -l test.tar # List all files in test.tar.\n"
" ntar -x test.tar # Extract all files from test.tar.\n"
" ntar -x test.tar foo bar # Extract foo and bar from test.tar.\n"
"\n"
"Options:\n"
" -c Create tar file.\n"
" -a Add files to archive.\n"
" -l Show all files from archive.\n"
" -x Extract files.\n\n");
}
/* clang-format off */
int main(int argc, char **argv)
{
OS_CON_SET_TITLE("ntar");
int status = MTAR_ESUCCESS;
if (argc < 3) {
show_help();
goto exit;
}
char *tar_fname = argv[2];
ntar_flist_t flist_in;
flist_in.names = &argv[3];
flist_in.fnum = argc - 3;
IFARG("-h", true)
show_help();
else IFARG("-x", argc >= 3)
status = ntar_work_content(tar_fname, flist_in, NTAR_EXTRACT);
else IFARG("-l", argc == 3)
status = ntar_work_content(tar_fname, flist_in, NTAR_LIST);
else IFARG("-c", argc > 3)
status = ntar_add_files(tar_fname, true, flist_in);
else IFARG("-a", argc > 3)
status = ntar_add_files(tar_fname, false, flist_in);
else
puts("Use '-h' for help.");
exit:
OS_CON_EXIT(0);
return status;
}
/* clang-format on */