2 Unix SMB/CIFS implementation.
3 Tar backup command extension
4 Copyright (C) Aurélien Aptel 2013
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "system/filesys.h"
22 #include "client/client_proto.h"
23 #include "client/clitar_proto.h"
24 #include "libsmb/libsmb.h"
26 #include <archive_entry.h>
28 #define LEN(x) (sizeof(x)/sizeof((x)[0]))
31 * Maximum value for the blocksize field
33 #define TAR_MAX_BLOCK_SIZE 0xffff
36 * Default tar block size in bytes. Hasn't changed since the first
39 * A more adequate size will be used for better performance unless
40 * we're dealing with a tape device with a fixed read/write block
43 * The actual choice is made by libarchive.
45 #define TAR_DEFAULT_BLOCK_SIZE 20
49 TAR_CREATE, /* c flag */
50 TAR_EXTRACT, /* x flag */
55 TAR_INCLUDE, /* I flag, default */
56 TAR_INCLUDE_LIST, /* F flag */
57 TAR_EXCLUDE, /* X flag */
66 /* in state that needs/can be processed? */
71 enum tar_operation operation; /* create, extract */
72 enum tar_selection selection; /* inc, inc from file, exclude */
73 int blocksize; /* size in bytes of a block in the tar file */
74 bool hidden; /* backup hidden file? */
75 bool system; /* backup system file? */
76 bool incremental; /* backup _only_ archived file? */
77 bool reset; /* unset archive bit? */
78 bool dry; /* don't write tar file? */
79 bool regex; /* XXX: never actually using regex... */
83 /* path to tar archive name */
86 /* file descriptor of tar file */
89 /* list of path to include or exclude */
93 struct archive *archive;
96 struct tar tar_ctx = {
97 .mode.selection = TAR_INCLUDE,
98 .mode.blocksize = TAR_DEFAULT_BLOCK_SIZE,
101 .mode.incremental = false,
105 static char *fix_unix_path (char *path, bool removeprefix)
107 char *from = path, *to = path;
118 if (path[0] == '/' || path[0] == '\\') {
123 if (path[1] && path[0] == '.' && (path[1] == '/' || path[1] == '\\')) {
128 /* replace \ with / */
142 #define XSET(v) [v] = #v
143 #define XTABLE(v, t) DEBUG(2, ("DUMP:%-20.20s = %s\n", #v, t[v]))
144 #define XBOOL(v) DEBUG(2, ("DUMP:%-20.20s = %d\n", #v, v ? 1 : 0))
145 #define XSTR(v) DEBUG(2, ("DUMP:%-20.20s = %s\n", #v, v ? v : "NULL"))
146 #define XINT(v) DEBUG(2, ("DUMP:%-20.20s = %d\n", #v, v))
147 static void tar_dump(struct tar *t)
151 XSET(TAR_NO_OPERATION),
156 const char* sel[] = {
157 XSET(TAR_NO_SELECTION),
159 XSET(TAR_INCLUDE_LIST),
163 XBOOL(t->to_process);
164 XTABLE(t->mode.operation, op);
165 XTABLE(t->mode.selection, sel);
166 XINT(t->mode.blocksize);
167 XBOOL(t->mode.hidden);
168 XBOOL(t->mode.system);
169 XBOOL(t->mode.incremental);
170 XBOOL(t->mode.reset);
172 XBOOL(t->mode.verbose);
176 for(i = 0; t->path_list && t->path_list[i]; i++) {
177 DEBUG(2, ("DUMP: t->path_list[%2d] = %s\n", i, t->path_list[i]));
180 DEBUG(2, ("DUMP:t->path_list @ %p (%d elem)\n", t->path_list, i));
188 static int tar_set_blocksize(struct tar *t, int size)
190 if (size <= 0 || size > TAR_MAX_BLOCK_SIZE) {
194 t->mode.blocksize = size;
199 static bool tar_set_newer_than(struct tar *t, const char *filename)
201 extern time_t newer_than;
202 SMB_STRUCT_STAT stbuf;
204 if (sys_stat(filename, &stbuf, false) != 0) {
205 DEBUG(0, ("Error setting newer-than time\n"));
209 newer_than = convert_timespec_to_time_t(stbuf.st_ex_mtime);
210 DEBUG(1, ("Getting files newer than %s\n", time_to_asc(newer_than)));
214 static bool tar_read_inclusion_file (struct tar *t, const char* filename)
218 TALLOC_CTX *ctx = talloc_tos();
219 int fd = open(filename, O_RDONLY);
222 DEBUG(0, ("Can't open inclusion file '%s': %s\n", filename, strerror(errno)));
226 list = str_list_make_empty(ctx);
228 while ((line = afdgets(fd, ctx, 0))) {
229 list = str_list_add((const char **)list, fix_unix_path(line, true));
237 bool tar_to_process (struct tar *t)
239 return t->to_process;
243 * cmd_block - interactive command to change tar blocksize
245 * Read a size from the client command line and update the current
250 /* XXX: from client.c */
251 const extern char *cmd_ptr;
253 TALLOC_CTX *ctx = talloc_tos();
255 if (!next_token_talloc(ctx, &cmd_ptr, &buf, NULL)) {
256 DEBUG(0, ("blocksize <n>\n"));
260 if(!tar_set_blocksize(&tar_ctx, atoi(buf))) {
261 DEBUG(0, ("invalid blocksize\n"));
264 DEBUG(2, ("blocksize is now %d\n", tar_ctx.mode.blocksize));
270 * cmd_tarmode - interactive command to change tar behaviour
272 * Read one or more modes from the client command line and update the
275 int cmd_tarmode(void)
277 const extern char *cmd_ptr;
280 TALLOC_CTX *ctx = talloc_tos();
287 {"full", &tar_ctx.mode.incremental, false},
288 {"inc", &tar_ctx.mode.incremental, true },
289 {"reset", &tar_ctx.mode.reset, true },
290 {"noreset", &tar_ctx.mode.reset, false},
291 {"system", &tar_ctx.mode.system, true },
292 {"nosystem", &tar_ctx.mode.system, false},
293 {"hidden", &tar_ctx.mode.hidden, true },
294 {"nohidden", &tar_ctx.mode.hidden, false},
295 {"verbose", &tar_ctx.mode.verbose, true },
296 {"noquiet", &tar_ctx.mode.verbose, true },
297 {"quiet", &tar_ctx.mode.verbose, false},
298 {"noverbose", &tar_ctx.mode.verbose, false},
301 while (next_token_talloc(ctx, &cmd_ptr, &buf, NULL)) {
302 for (i = 0; i < LEN(table); i++) {
303 if (strequal(table[i].cmd, buf)) {
304 *table[i].p = table[i].value;
310 DEBUG(0, ("tarmode: unrecognised option %s\n", buf));
315 DEBUG(0, ("tarmode is now %s, %s, %s, %s, %s\n",
316 tar_ctx.mode.incremental ? "incremental" : "full",
317 tar_ctx.mode.system ? "system" : "nosystem",
318 tar_ctx.mode.hidden ? "hidden" : "nohidden",
319 tar_ctx.mode.reset ? "reset" : "noreset",
320 tar_ctx.mode.verbose ? "verbose" : "quiet"));
325 * set_remote_attr - set DOS attributes of a remote file
326 * @filename: path to the file name
327 * @new_attr: attribute bit mask to use
328 * @mode: one of ATTR_SET or ATTR_UNSET
330 * Update the file attributes with the one provided.
332 static void set_remote_attr(char *filename, uint16 new_attr, int mode)
334 extern struct cli_state *cli;
338 if (!NT_STATUS_IS_OK(cli_getatr(cli, filename, &old_attr, NULL, NULL))) {
339 /* XXX: debug message */
343 if (mode == ATTR_SET) {
344 new_attr |= old_attr;
346 new_attr = old_attr & ~new_attr;
349 status = cli_setatr(cli, filename, new_attr, 0);
350 if (!NT_STATUS_IS_OK(status)) {
351 DEBUG(1, ("setatr failed: %s\n", nt_errstr(status)));
356 * cmd_setmode - interactive command to set DOS attributes
358 * Read a filename and mode from the client command line and update
359 * the file DOS attributes.
361 int cmd_setmode(void)
363 const extern char *cmd_ptr;
366 uint16 attr[2] = {0};
368 TALLOC_CTX *ctx = talloc_tos();
371 if (!next_token_talloc(ctx, &cmd_ptr, &buf, NULL)) {
372 DEBUG(0, ("setmode <filename> <[+|-]rsha>\n"));
376 fname = talloc_asprintf(ctx,
378 client_get_cur_dir(),
384 while (next_token_talloc(ctx, &cmd_ptr, &buf, NULL)) {
396 attr[mode] |= FILE_ATTRIBUTE_READONLY;
399 attr[mode] |= FILE_ATTRIBUTE_HIDDEN;
402 attr[mode] |= FILE_ATTRIBUTE_SYSTEM;
405 attr[mode] |= FILE_ATTRIBUTE_ARCHIVE;
408 DEBUG(0, ("setmode <filename> <perm=[+|-]rsha>\n"));
414 if (attr[ATTR_SET] == 0 && attr[ATTR_UNSET] == 0) {
415 DEBUG(0, ("setmode <filename> <[+|-]rsha>\n"));
419 DEBUG(2, ("\nperm set %d %d\n", attr[ATTR_SET], attr[ATTR_UNSET]));
420 set_remote_attr(fname, attr[ATTR_SET], ATTR_SET);
421 set_remote_attr(fname, attr[ATTR_UNSET], ATTR_UNSET);
427 * cmd_tar - interactive command to start a tar backup/restoration
429 * Check presence of argument, parse them and handle the request.
436 static int tar_extract(struct tar *t)
440 struct archive_entry *entry;
442 t->archive = archive_read_new();
443 archive_read_support_format_all(t->archive);
444 archive_read_support_filter_all(t->archive);
446 if (strequal(t->tar_path, "-")) {
447 r = archive_read_open_fd(t->archive, STDIN_FILENO, t->mode.blocksize);
449 r = archive_read_open_filename(t->archive, t->tar_path,
453 if (r != ARCHIVE_OK) {
454 DEBUG(0, ("Can't open %s : %s\n", t->tar_path,
455 archive_error_string(t->archive)));
460 r = archive_read_next_header(t->archive, &entry);
461 if (r == ARCHIVE_EOF) {
464 if (r == ARCHIVE_WARN) {
465 DEBUG(0, ("Warning: %s", archive_error_string(t->archive)));
467 if (r == ARCHIVE_FATAL) {
468 DEBUG(0, ("Fatal: %s", archive_error_string(t->archive)));
473 DEBUG(0, ("Processing %s...\n", archive_entry_pathname(entry)));
477 r = archive_read_free(t->archive);
478 if (r != ARCHIVE_OK) {
479 DEBUG(0, ("Can't close %s : %s\n", t->tar_path,
480 archive_error_string(t->archive)));
486 int tar_process(struct tar *t)
490 switch(t->mode.operation) {
498 DEBUG(0, ("Invalid tar state\n"));
505 * tar_parse_args - parse and set tar command line arguments
506 * @flag: string pointing to tar options
507 * @val: number of tar arguments
508 * @valsize: table of arguments after the flags (number of element in val)
510 * tar arguments work in a weird way. For each flag f that takes a
511 * value v, the user is supposed to type:
514 * -Tf1f2f3 v1 v2 v3 TARFILE PATHS...
516 * in the interactive session:
517 * tar f1f2f3 v1 v2 v3 TARFILE PATHS...
519 * opt has only flags (eg. "f1f2f3") and val has the arguments
520 * (values) following them (eg. ["v1", "v2", "v3", "TARFILE", "PATH1",
523 * There are only 2 flags that take an arg: b and N. The other flags
524 * just change the semantic of PATH or TARFILE.
526 * PATH can be a list of included/excluded paths, the path to a file
527 * containing a list of included/excluded paths to use (F flag). If no
528 * PATH is provided, the whole share is used (/).
530 int tar_parse_args(struct tar* t, const char *flag, const char **val, int valsize)
532 TALLOC_CTX *ctx = talloc_tos();
534 /* index of next value to use */
538 * Reset back some options - could be from interactive version
539 * all other modes are left as they are
541 t->mode.operation = TAR_NO_OPERATION;
542 t->mode.selection = TAR_NO_SELECTION;
544 t->to_process = false;
550 if (t->mode.operation != TAR_NO_OPERATION) {
551 printf("Tar must be followed by only one of c or x.\n");
554 t->mode.operation = TAR_CREATE;
557 if (t->mode.operation != TAR_NO_OPERATION) {
558 printf("Tar must be followed by only one of c or x.\n");
561 t->mode.operation = TAR_EXTRACT;
566 if (t->mode.selection != TAR_NO_SELECTION) {
567 DEBUG(0,("Only one of I,X,F must be specified\n"));
570 t->mode.selection = TAR_INCLUDE;
573 if (t->mode.selection != TAR_NO_SELECTION) {
574 DEBUG(0,("Only one of I,X,F must be specified\n"));
577 t->mode.selection = TAR_EXCLUDE;
580 if (t->mode.selection != TAR_NO_SELECTION) {
581 DEBUG(0,("Only one of I,X,F must be specified\n"));
584 t->mode.selection = TAR_INCLUDE_LIST;
589 if (ival >= valsize) {
590 DEBUG(0, ("Option b must be followed by a blocksize\n"));
594 if (!tar_set_blocksize(t, atoi(val[ival]))) {
595 DEBUG(0, ("Option b must be followed by a valid blocksize\n"));
602 /* incremental mode */
604 t->mode.incremental = true;
609 if (ival >= valsize) {
610 DEBUG(0, ("Option N must be followed by valid file name\n"));
614 if (!tar_set_newer_than(t, val[ival])) {
615 DEBUG(0,("Error setting newer-than time\n"));
624 t->mode.reset = true;
629 t->mode.verbose = true;
634 t->mode.regex = true;
639 if (t->mode.operation != TAR_CREATE) {
640 DEBUG(0, ("n is only meaningful when creating a tar-file\n"));
645 DEBUG(0, ("dry_run set\n"));
649 DEBUG(0,("Unknown tar option\n"));
654 /* no selection given? default selection is include */
655 if (t->mode.selection == TAR_NO_SELECTION) {
656 t->mode.selection = TAR_INCLUDE;
659 if (valsize - ival < 1) {
660 DEBUG(0, ("No tar file given.\n"));
665 t->tar_path = talloc_strdup(ctx, val[ival]);
668 /* handle PATHs... */
669 tar_ctx.path_list = str_list_make_empty(ctx);
671 /* flag F -> read file list */
672 if (t->mode.selection == TAR_INCLUDE_LIST) {
673 if (valsize - ival != 1) {
674 DEBUG(0,("Option F must be followed by exactly one filename.\n"));
678 if (!tar_read_inclusion_file(t, val[ival])) {
684 /* otherwise store all the PATHs on the command line */
687 for (i = ival; i < valsize; i++) {
688 t->path_list = str_list_add((const char**)t->path_list, val[i]);
689 fix_unix_path(t->path_list[i-ival], true);
693 t->to_process = true;