/
main.c
350 lines (320 loc) · 9.85 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
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/* File: main.c
Copyright (c) 2011 David Hauweele <david@hauweele.net>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the University nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE. */
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <getopt.h>
#include <sysexits.h>
#include <errno.h>
#include <time.h>
#include <err.h>
#include <gawen/help.h>
#include "sar.h"
#include "common.h"
enum mode { MD_NONE = 0,
MD_INFORMATION,
MD_CREATE,
MD_EXTRACT,
MD_LIST };
struct opts_val {
unsigned int verbose;
enum mode mode;
bool use_file;
bool no_crc;
bool no_nano;
char *cwd; /* original cwd */
const char *tmp_cwd; /* new cwd */
const char *compress;
const char *file;
const char *source;
};
static struct opts_val *opt_value;
static void clean_exit()
{
/* revert to original working directory */
if(opt_value->cwd) {
xchdir(opt_value->cwd);
free(opt_value->cwd);
}
}
static void version()
{
printf(PACKAGE " " PACKAGE_VERSION "\n");
exit(EXIT_SUCCESS);
}
static void except_archive(int argc, int optind, char *argv[],
struct opts_val *val)
{
if(val->use_file) {
if(argc - optind != 1)
errx(EXIT_FAILURE, "except archive name");
val->file = argv[optind];
}
else if(argc - optind)
errx(EXIT_FAILURE, "except no arguments");
else
val->file = NULL;
}
static void except_more(int argc, int optind, char *argv[],
struct opts_val *val)
{
if(val->use_file) {
if(argc - optind != 2)
errx(EXIT_FAILURE, "except archive name and a path to archive");
val->file = argv[optind++];
val->source = argv[optind];
}
else if(argc - optind != 1)
errx(EXIT_FAILURE, "except a path to archive");
else {
val->file = NULL;
val->source = argv[optind];
}
}
static void cmdline(int argc, char *argv[], struct opts_val *val)
{
int exit_status = EXIT_FAILURE;
enum opt { OPT_COMPRESS,
OPT_LZMA,
OPT_LZIP,
OPT_LZOP,
#ifdef COMMIT
OPT_COMMIT,
#endif /* COMMIT */
OPT_DIRECTORY = 'd',
OPT_LZW = 'Z',
OPT_GZIP = 'z',
OPT_BZIP2 = 'j',
OPT_XZ = 'J',
OPT_VERSION = 'V',
OPT_HELP = 'h',
OPT_VERBOSE = 'v',
OPT_INFORMATION = 'i',
OPT_CREATE = 'c',
OPT_EXTRACT = 'x',
OPT_LIST = 't',
OPT_FILE = 'f',
OPT_NO_CRC = 'C',
OPT_NO_NANO = 'N' };
struct opt_help names[] = {
{ 'V', "version", "Print version information" },
#ifdef COMMIT
{ 0 , "commit", "Display commit information" },
#endif /* COMMIT */
{ 'h', "help", "Print this message" },
{ 'v', "verbose", "Be verbose (may be used multiple times)" },
{ 'd', "directory", "Change to directory DIR" },
{ 0 , "compress", "Compress using the specified executable" },
{ 'Z', "lzw", "Alias for '--compress compress'" },
{ 'z', "gzip", "Alias for '--compress gzip'" },
{ 'j', "bzip2", "Alias for '--compress bzip2'" },
{ 'J', "xz", "Alias for '--compress xz'" },
{ 0 , "lzma", "Alias for '--compress lzma'" },
{ 0 , "lzip", "Alias for '--compress lzip'" },
{ 0 , "lzop", "Alias for '--compress lzop'" },
{ 'i', "information", "Display basic informations about an archive" },
{ 'c', "create", "Create a new archive" },
{ 'x', "extract", "Extract all files from an archive" },
{ 't', "list", "List all files in an archive" },
{ 'f', "file", "Use a file instead of standard input/output" },
{ 'C', "no-crc", "Disable integrity checks" },
{ 'N', "no-nano", "Disable timestamps precision (upto nanoseconds)" },
{ 0, NULL, NULL }
};
struct option opts[] = {
{ "version", no_argument, NULL, OPT_VERSION },
#ifdef COMMIT
{ "commit", no_argument, NULL, OPT_COMMIT },
#endif /* COMMIT */
{ "help", no_argument, NULL, OPT_HELP },
{ "verbose", no_argument, NULL, OPT_VERBOSE },
{ "directory", required_argument, NULL, OPT_DIRECTORY },
{ "compress", required_argument, NULL, OPT_COMPRESS },
{ "lzw", no_argument, NULL, OPT_LZW },
{ "gzip", no_argument, NULL, OPT_GZIP },
{ "bzip2", no_argument, NULL, OPT_BZIP2 },
{ "xz", no_argument, NULL, OPT_XZ },
{ "lzma", no_argument, NULL, OPT_LZMA },
{ "lzip", no_argument, NULL, OPT_LZIP },
{ "lzop", no_argument, NULL, OPT_LZOP },
{ "information", no_argument, NULL, OPT_INFORMATION },
{ "create", no_argument, NULL, OPT_CREATE },
{ "extract", no_argument, NULL, OPT_EXTRACT },
{ "list", no_argument, NULL, OPT_LIST },
{ "file", no_argument, NULL, OPT_FILE },
{ "no-crc", no_argument, NULL, OPT_NO_CRC },
{ "no-nano", no_argument, NULL, OPT_NO_NANO },
{ NULL, 0, NULL, 0 }
};
/* retrieve program's name */
const char *pgn = (const char *)strrchr(argv[0], '/');
pgn = pgn ? (pgn + 1) : argv[0];
while(1) {
int c = getopt_long(argc, argv, "Vhvd:ZzjJicxtfCN", opts, NULL);
if(c == -1)
break;
switch(c) {
case OPT_VERBOSE:
val->verbose++;
break;
case OPT_COMPRESS:
val->compress = optarg;
break;
case OPT_DIRECTORY:
val->cwd = xmalloc(PATH_MAX);
val->cwd = xgetcwd(val->cwd, PATH_MAX);
val->tmp_cwd = optarg;
break;
case OPT_GZIP:
val->compress = "gzip";
break;
case OPT_BZIP2:
val->compress = "bzip2";
break;
case OPT_XZ:
val->compress = "xz";
break;
case OPT_LZMA:
val->compress = "lzma";
break;
case OPT_LZIP:
val->compress = "lzip";
break;
case OPT_LZOP:
val->compress = "lzop";
break;
case OPT_LZW:
val->compress = "compress";
break;
case OPT_INFORMATION:
val->mode = MD_INFORMATION;
break;
case OPT_CREATE:
val->mode = MD_CREATE;
break;
case OPT_EXTRACT:
val->mode = MD_EXTRACT;
break;
case OPT_LIST:
val->mode = MD_LIST;
val->verbose++;
break;
case OPT_FILE:
val->use_file = true;
break;
case OPT_NO_CRC:
val->no_crc = true;
break;
case OPT_NO_NANO:
val->no_nano = true;
break;
#ifdef COMMIT
case OPT_COMMIT:
printf("Commit-Id SHA1 : " COMMIT "\n");
exit(EXIT_SUCCESS);
#endif /* COMMIT */
case OPT_VERSION:
version();
exit(EXIT_SUCCESS);
case OPT_HELP:
exit_status = EXIT_SUCCESS;
default:
help(pgn, "[options] [archive] [files]", names);
exit(exit_status);
}
}
/* consider remaining arguments */
switch(val->mode) {
case(MD_NONE):
errx(EXIT_SUCCESS, "You must specify one of the 'cxti' options\n"
"Try '%s --help'", pgn);
case(MD_INFORMATION):
if(val->use_file) {
if(argc - optind != 1)
errx(EXIT_FAILURE, "except archive name");
val->file = argv[optind];
}
else if(argc - optind)
errx(EXIT_FAILURE, "except no arguments");
else
val->file = NULL;
break;
case(MD_CREATE):
except_more(argc, optind, argv, val);
break;
case(MD_LIST):
case(MD_EXTRACT):
except_archive(argc, optind, argv, val);
break;
}
if((val->no_crc || val->no_nano) &&
!(val->mode == MD_CREATE))
errx(EXIT_FAILURE, "Options 'CN' are only availables with 'c' option\n"
"Try '%s --help'", pgn);
}
int main(int argc, char *argv[])
{
struct opts_val val = {0};
struct sar_file *f = NULL;
opt_value = &val;
atexit(clean_exit);
cmdline(argc, argv, &val);
switch(val.mode) {
case(MD_NONE):
break;
case(MD_INFORMATION):
f = sar_read(val.file, val.compress, val.verbose);
sar_info(f);
break;
case(MD_CREATE):
f = sar_creat(val.file,
val.compress,
!val.no_crc,
!val.no_nano,
val.verbose);
if(val.tmp_cwd)
xchdir(val.tmp_cwd);
sar_add(f, val.source);
break;
case(MD_EXTRACT):
f = sar_read(val.file, val.compress, val.verbose);
if(val.tmp_cwd)
xchdir(val.tmp_cwd);
sar_extract(f);
break;
case(MD_LIST):
f = sar_read(val.file, val.compress, val.verbose);
if(val.tmp_cwd)
xchdir(val.tmp_cwd);
sar_list(f);
break;
}
if(val.mode != MD_NONE)
sar_close(f);
exit(EXIT_SUCCESS);
}