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]))
29 #define DBG(a, b) (DEBUG(a, ("tar:%-4d ", __LINE__)), DEBUG(a, b))
32 * Maximum value for the blocksize field
34 #define TAR_MAX_BLOCK_SIZE 0xffff
37 * Default tar block size in bytes. Hasn't changed since the first
40 * A more adequate size will be used for better performance unless
41 * we're dealing with a tape device with a fixed read/write block
44 * The actual choice is made by libarchive.
46 #define TAR_DEFAULT_BLOCK_SIZE 20
48 #define TAR_CLI_READ_SIZE 0xff00
50 #define TAR_DO_LIST_ATTR (FILE_ATTRIBUTE_DIRECTORY \
51 | FILE_ATTRIBUTE_SYSTEM \
52 | FILE_ATTRIBUTE_HIDDEN)
57 TAR_CREATE, /* c flag */
58 TAR_EXTRACT, /* x flag */
63 TAR_INCLUDE, /* I and F flag, default */
64 TAR_EXCLUDE, /* X flag */
73 /* in state that needs/can be processed? */
78 enum tar_operation operation; /* create, extract */
79 enum tar_selection selection; /* inc, inc from file, exclude */
80 int blocksize; /* size in bytes of a block in the tar file */
81 bool hidden; /* backup hidden file? */
82 bool system; /* backup system file? */
83 bool incremental; /* backup _only_ archived file? */
84 bool reset; /* unset archive bit? */
85 bool dry; /* don't write tar file? */
86 bool regex; /* XXX: never actually using regex... */
90 /* nb of bytes received */
93 /* path to tar archive name */
96 /* list of path to include or exclude */
101 struct archive *archive;
104 struct tar tar_ctx = {
105 .mode.selection = TAR_INCLUDE,
106 .mode.blocksize = TAR_DEFAULT_BLOCK_SIZE,
109 .mode.incremental = false,
113 static char *fix_unix_path (char *path, bool removeprefix)
115 char *from = path, *to = path;
126 if (path[0] == '/' || path[0] == '\\') {
131 if (path[1] && path[0] == '.' && (path[1] == '/' || path[1] == '\\')) {
136 /* replace / with \ */
150 static char *path_base_name (const char *path)
152 TALLOC_CTX *ctx = talloc_tos();
157 for (i = 0; path[i]; i++) {
158 if (path[i] == '\\' || path[i] == '/') {
164 base = talloc_strdup(ctx, path);
171 #define XSET(v) [v] = #v
172 #define XTABLE(v, t) DBG(2, ("DUMP:%-20.20s = %s\n", #v, t[v]))
173 #define XBOOL(v) DBG(2, ("DUMP:%-20.20s = %d\n", #v, v ? 1 : 0))
174 #define XSTR(v) DBG(2, ("DUMP:%-20.20s = %s\n", #v, v ? v : "NULL"))
175 #define XINT(v) DBG(2, ("DUMP:%-20.20s = %d\n", #v, v))
176 #define XUINT64(v) DBG(2, ("DUMP:%-20.20s = %" PRIu64 "\n", #v, v))
177 static void tar_dump(struct tar *t)
181 XSET(TAR_NO_OPERATION),
186 const char* sel[] = {
187 XSET(TAR_NO_SELECTION),
192 XBOOL(t->to_process);
193 XTABLE(t->mode.operation, op);
194 XTABLE(t->mode.selection, sel);
195 XINT(t->mode.blocksize);
196 XBOOL(t->mode.hidden);
197 XBOOL(t->mode.system);
198 XBOOL(t->mode.incremental);
199 XBOOL(t->mode.reset);
201 XBOOL(t->mode.verbose);
202 XUINT64(t->total_size);
204 XINT(t->path_list_size);
206 for (i = 0; t->path_list && t->path_list[i]; i++) {
207 DBG(2, ("DUMP: t->path_list[%2d] = %s\n", i, t->path_list[i]));
210 DBG(2, ("DUMP:t->path_list @ %p (%d elem)\n", t->path_list, i));
218 static void tar_add_selection_path(struct tar *t, const char *path)
220 TALLOC_CTX *ctx = talloc_tos();
222 t->path_list = str_list_make_empty(ctx);
223 t->path_list_size = 0;
226 t->path_list = str_list_add((const char**)t->path_list, path);
228 fix_unix_path(t->path_list[t->path_list_size - 1], true);
231 static int tar_set_blocksize(struct tar *t, int size)
233 if (size <= 0 || size > TAR_MAX_BLOCK_SIZE) {
237 t->mode.blocksize = size;
242 static bool tar_set_newer_than(struct tar *t, const char *filename)
244 extern time_t newer_than;
245 SMB_STRUCT_STAT stbuf;
247 if (sys_stat(filename, &stbuf, false) != 0) {
248 DBG(0, ("Error setting newer-than time\n"));
252 newer_than = convert_timespec_to_time_t(stbuf.st_ex_mtime);
253 DBG(1, ("Getting files newer than %s\n", time_to_asc(newer_than)));
257 static bool tar_read_inclusion_file (struct tar *t, const char* filename)
260 TALLOC_CTX *ctx = talloc_tos();
261 int fd = open(filename, O_RDONLY);
264 DBG(0, ("Can't open inclusion file '%s': %s\n", filename, strerror(errno)));
268 while ((line = afdgets(fd, ctx, 0))) {
269 tar_add_selection_path(t, line);
276 /* skip leading slashes or dots */
277 static const char* skip_useless_char_in_path(const char *p)
280 if (*p == '/' || *p == '\\') {
283 else if (p[0] == '.' && (p[1] == '/' || p[1] == '\\')) {
294 * return true if the path @sub is a subpath of @full.
296 * case-insensitive, true if @sub = @full
298 static bool is_subpath(const char *sub, const char *full)
300 const char *full_copy = full;
302 while (*full && *sub &&
303 (*full == *sub || tolower_m(*full) == tolower_m(*sub) ||
304 (*full == '\\' && *sub=='/') || (*full == '/' && *sub=='\\'))) {
308 /* if full has a trailing slash, it compared equal, so full is an "initial"
311 if (!*full && full != full_copy && (*(full-1) == '/' || *(full-1) == '\\'))
314 /* ignore trailing slash on full */
315 if (!*sub && (*full == '/' || *full == '\\') && !*(full+1))
318 /* check for full is an "initial" string of sub */
319 if ((*sub == '/' || *sub == '\\') && !*full)
322 return *full == *sub;
325 static bool tar_path_in_list(struct tar *t, const char *path, bool reverse)
328 const char *p = path;
335 p = skip_useless_char_in_path(p);
337 for (i = 0; i < t->path_list_size; i++) {
338 pattern = skip_useless_char_in_path(t->path_list[i]);
339 res = is_subpath(p, pattern);
341 res = res || is_subpath(pattern, p);
351 static bool tar_extract_skip_path(struct tar *t,
352 struct archive_entry *entry)
354 const bool skip = true;
355 const char *fullpath = archive_entry_pathname(entry);
358 in = t->path_list_size > 0 ? tar_path_in_list(t, fullpath, false) : true;
360 if (t->mode.selection == TAR_EXCLUDE) {
364 return in ? !skip : skip;
367 static bool tar_create_skip_path(struct tar *t,
368 const char *fullpath,
369 const struct file_info *finfo)
372 const bool skip = true;
373 const mode_t mode = finfo->mode;
374 const bool isdir = mode & FILE_ATTRIBUTE_DIRECTORY;
375 const bool exclude = t->mode.selection == TAR_EXCLUDE;
380 /* 1. if we dont want X and we have X, skip */
381 if (!t->mode.system && (mode & FILE_ATTRIBUTE_SYSTEM)) {
385 if (!t->mode.hidden && (mode & FILE_ATTRIBUTE_HIDDEN)) {
389 /* 2. if we only want archive and it's not, skip */
391 if (t->mode.incremental && !(mode & FILE_ATTRIBUTE_ARCHIVE)) {
396 /* 3. is it in the selection list? */
399 * tar_create_from_list() use the include list as a starting
400 * point, no need to check
406 /* we are now in exclude mode */
407 if (t->path_list_size > 0) {
408 in = tar_path_in_list(t, fullpath, isdir && !exclude);
411 return in ? skip : !skip;
414 bool tar_to_process (struct tar *t)
416 return t->to_process;
420 * cmd_block - interactive command to change tar blocksize
422 * Read a size from the client command line and update the current
427 /* XXX: from client.c */
428 const extern char *cmd_ptr;
430 TALLOC_CTX *ctx = talloc_tos();
432 if (!next_token_talloc(ctx, &cmd_ptr, &buf, NULL)) {
433 DBG(0, ("blocksize <n>\n"));
437 if (!tar_set_blocksize(&tar_ctx, atoi(buf))) {
438 DBG(0, ("invalid blocksize\n"));
441 DBG(2, ("blocksize is now %d\n", tar_ctx.mode.blocksize));
447 * cmd_tarmode - interactive command to change tar behaviour
449 * Read one or more modes from the client command line and update the
452 int cmd_tarmode(void)
454 const extern char *cmd_ptr;
457 TALLOC_CTX *ctx = talloc_tos();
464 {"full", &tar_ctx.mode.incremental, false},
465 {"inc", &tar_ctx.mode.incremental, true },
466 {"reset", &tar_ctx.mode.reset, true },
467 {"noreset", &tar_ctx.mode.reset, false},
468 {"system", &tar_ctx.mode.system, true },
469 {"nosystem", &tar_ctx.mode.system, false},
470 {"hidden", &tar_ctx.mode.hidden, true },
471 {"nohidden", &tar_ctx.mode.hidden, false},
472 {"verbose", &tar_ctx.mode.verbose, true },
473 {"noquiet", &tar_ctx.mode.verbose, true },
474 {"quiet", &tar_ctx.mode.verbose, false},
475 {"noverbose", &tar_ctx.mode.verbose, false},
478 while (next_token_talloc(ctx, &cmd_ptr, &buf, NULL)) {
479 for (i = 0; i < LEN(table); i++) {
480 if (strequal(table[i].cmd, buf)) {
481 *table[i].p = table[i].value;
487 DBG(0, ("tarmode: unrecognised option %s\n", buf));
492 DBG(0, ("tarmode is now %s, %s, %s, %s, %s\n",
493 tar_ctx.mode.incremental ? "incremental" : "full",
494 tar_ctx.mode.system ? "system" : "nosystem",
495 tar_ctx.mode.hidden ? "hidden" : "nohidden",
496 tar_ctx.mode.reset ? "reset" : "noreset",
497 tar_ctx.mode.verbose ? "verbose" : "quiet"));
502 * set_remote_attr - set DOS attributes of a remote file
503 * @filename: path to the file name
504 * @new_attr: attribute bit mask to use
505 * @mode: one of ATTR_SET or ATTR_UNSET
507 * Update the file attributes with the one provided.
509 static void set_remote_attr(const char *filename, uint16 new_attr, int mode)
511 extern struct cli_state *cli;
515 if (!NT_STATUS_IS_OK(cli_getatr(cli, filename, &old_attr, NULL, NULL))) {
516 /* XXX: debug message */
520 if (mode == ATTR_SET) {
521 new_attr |= old_attr;
523 new_attr = old_attr & ~new_attr;
526 status = cli_setatr(cli, filename, new_attr, 0);
527 if (!NT_STATUS_IS_OK(status)) {
528 DBG(1, ("setatr failed: %s\n", nt_errstr(status)));
533 * cmd_setmode - interactive command to set DOS attributes
535 * Read a filename and mode from the client command line and update
536 * the file DOS attributes.
538 int cmd_setmode(void)
540 const extern char *cmd_ptr;
543 uint16 attr[2] = {0};
545 TALLOC_CTX *ctx = talloc_tos();
548 if (!next_token_talloc(ctx, &cmd_ptr, &buf, NULL)) {
549 DBG(0, ("setmode <filename> <[+|-]rsha>\n"));
553 fname = talloc_asprintf(ctx,
555 client_get_cur_dir(),
561 while (next_token_talloc(ctx, &cmd_ptr, &buf, NULL)) {
573 attr[mode] |= FILE_ATTRIBUTE_READONLY;
576 attr[mode] |= FILE_ATTRIBUTE_HIDDEN;
579 attr[mode] |= FILE_ATTRIBUTE_SYSTEM;
582 attr[mode] |= FILE_ATTRIBUTE_ARCHIVE;
585 DBG(0, ("setmode <filename> <perm=[+|-]rsha>\n"));
591 if (attr[ATTR_SET] == 0 && attr[ATTR_UNSET] == 0) {
592 DBG(0, ("setmode <filename> <[+|-]rsha>\n"));
596 DBG(2, ("perm set %d %d\n", attr[ATTR_SET], attr[ATTR_UNSET]));
597 set_remote_attr(fname, attr[ATTR_SET], ATTR_SET);
598 set_remote_attr(fname, attr[ATTR_UNSET], ATTR_UNSET);
602 static int make_remote_path(const char *full_path)
604 extern struct cli_state *cli;
605 TALLOC_CTX *ctx = talloc_tos();
609 char *last_backslash;
615 subpath = talloc_strdup(ctx, full_path);
616 path = talloc_strdup(ctx, full_path);
617 len = talloc_get_size(path) - 1;
619 last_backslash = strrchr_m(path, '\\');
621 if (!last_backslash) {
628 p = strtok_r(path, "\\", &state);
631 strlcat(subpath, p, len);
632 status = cli_chkpath(cli, subpath);
633 if (!NT_STATUS_IS_OK(status)) {
634 status = cli_mkdir(cli, subpath);
635 if (!NT_STATUS_IS_OK(status)) {
636 DBG(0, ("Can't mkdir %s: %s\n", subpath, nt_errstr(status)));
640 DBG(3, ("mkdir %s\n", subpath));
643 strlcat(subpath, "\\", len);
644 p = strtok_r(NULL, "/\\", &state);
652 static int tar_send_file(struct tar *t, struct archive_entry *entry)
654 extern struct cli_state *cli;
655 TALLOC_CTX *ctx = talloc_tos();
659 uint16_t remote_fd = (uint16_t) -1;
661 int flags = O_RDWR | O_CREAT | O_TRUNC;
662 mode_t mode = archive_entry_filetype(entry);
664 dos_path = talloc_strdup(ctx, archive_entry_pathname(entry));
665 fix_unix_path(dos_path, true);
667 full_path = talloc_strdup(ctx, client_get_cur_dir());
668 full_path = talloc_strdup_append(full_path, dos_path);
670 if (mode != AE_IFREG && mode != AE_IFDIR) {
671 DBG(0, ("Skipping non-dir & non-regular file %s\n", full_path));
675 if (make_remote_path(full_path)) {
680 if (mode == AE_IFDIR) {
684 status = cli_open(cli, full_path, flags, DENY_NONE, &remote_fd);
685 if (!NT_STATUS_IS_OK(status)) {
686 DBG(0, ("Error opening remote file %s: %s\n",
687 full_path, nt_errstr(status)));
698 r = archive_read_data_block(t->archive, &buf, &len, &off);
699 if (r == ARCHIVE_EOF) {
702 if (r == ARCHIVE_WARN) {
703 DBG(0, ("Warning: %s\n", archive_error_string(t->archive)));
705 if (r == ARCHIVE_FATAL) {
706 DBG(0, ("Fatal: %s\n", archive_error_string(t->archive)));
711 status = cli_writeall(cli, remote_fd, 0, buf, off, len, NULL);
712 if (!NT_STATUS_IS_OK(status)) {
713 DBG(0, ("Error writing remote file %s: %s\n",
714 full_path, nt_errstr(status)));
721 status = cli_close(cli, remote_fd);
722 if (!NT_STATUS_IS_OK(status)) {
723 DBG(0, ("Error losing remote file %s: %s\n",
724 full_path, nt_errstr(status)));
732 static int tar_get_file(struct tar *t, const char *full_dos_path,
733 struct file_info *finfo)
735 extern struct cli_state *cli;
736 TALLOC_CTX *ctx = talloc_tos();
738 struct archive_entry *entry;
739 char *full_unix_path;
740 char buf[TAR_CLI_READ_SIZE];
743 uint16_t remote_fd = (uint16_t)-1;
745 const bool isdir = finfo->mode & FILE_ATTRIBUTE_DIRECTORY;
747 DBG(5, ("+++ %s\n", full_dos_path));
749 t->total_size += finfo->size;
756 set_remote_attr(full_dos_path, FILE_ATTRIBUTE_ARCHIVE, ATTR_UNSET);
759 full_unix_path = talloc_asprintf(ctx, ".%s", full_dos_path);
760 string_replace(full_unix_path, '\\', '/');
761 entry = archive_entry_new();
762 archive_entry_copy_pathname(entry, full_unix_path);
763 archive_entry_set_filetype(entry, isdir ? AE_IFDIR : AE_IFREG);
764 archive_entry_set_atime(entry,
765 finfo->atime_ts.tv_sec,
766 finfo->atime_ts.tv_nsec);
767 archive_entry_set_mtime(entry,
768 finfo->mtime_ts.tv_sec,
769 finfo->mtime_ts.tv_nsec);
770 archive_entry_set_ctime(entry,
771 finfo->ctime_ts.tv_sec,
772 finfo->ctime_ts.tv_nsec);
773 archive_entry_set_perm(entry, isdir ? 0755 : 0644);
775 * check if we can safely cast unsigned file size to libarchive
776 * signed size. Very unlikely problem (>9 exabyte file)
778 if (finfo->size > INT64_MAX) {
779 DBG(0, ("Remote file %s too big\n", full_dos_path));
783 archive_entry_set_size(entry, (int64_t)finfo->size);
785 r = archive_write_header(t->archive, entry);
786 if (r != ARCHIVE_OK) {
787 DBG(0, ("Fatal: %s\n", archive_error_string(t->archive)));
793 DBG(5, ("get_file skip dir %s\n", full_dos_path));
797 status = cli_open(cli, full_dos_path, O_RDONLY, DENY_NONE, &remote_fd);
798 if (!NT_STATUS_IS_OK(status)) {
799 DBG(0,("%s opening remote file %s\n",
800 nt_errstr(status), full_dos_path));
805 status = cli_read(cli, remote_fd, buf, off, sizeof(buf), &len);
806 if (!NT_STATUS_IS_OK(status)) {
807 DBG(0,("Error reading file %s : %s\n",
808 full_dos_path, nt_errstr(status)));
815 r = archive_write_data(t->archive, buf, len);
817 DBG(0, ("Fatal: %s\n", archive_error_string(t->archive)));
822 } while (off < finfo->size);
825 cli_close(cli, remote_fd);
828 archive_entry_free(entry);
834 static NTSTATUS get_file_callback(struct cli_state *cli,
835 struct file_info *finfo,
838 TALLOC_CTX *ctx = talloc_tos();
839 NTSTATUS err = NT_STATUS_OK;
841 const char *initial_dir = client_get_cur_dir();
843 remote_name = talloc_asprintf(ctx, "%s%s", initial_dir, finfo->name);
845 if (strequal(finfo->name, "..") || strequal(finfo->name, ".")) {
849 if (tar_create_skip_path(&tar_ctx, remote_name, finfo)) {
850 DBG(5, ("--- %s\n", remote_name));
854 if (finfo->mode & FILE_ATTRIBUTE_DIRECTORY) {
859 old_dir = talloc_strdup(ctx, initial_dir);
860 new_dir = talloc_asprintf(ctx, "%s%s\\", initial_dir, finfo->name);
861 mask = talloc_asprintf(ctx, "%s*", new_dir);
863 if (tar_get_file(&tar_ctx, remote_name, finfo)) {
864 err = NT_STATUS_UNSUCCESSFUL;
868 client_set_cur_dir(new_dir);
869 do_list(mask, TAR_DO_LIST_ATTR, get_file_callback, false, true);
870 client_set_cur_dir(old_dir);
874 if (tar_get_file(&tar_ctx, remote_name, finfo)) {
875 err = NT_STATUS_UNSUCCESSFUL;
884 static int tar_create_from_list(struct tar *t)
886 TALLOC_CTX *ctx = talloc_tos();
889 const char *path, *mask, *base, *start_dir;
892 start_dir = talloc_strdup(ctx, client_get_cur_dir());
894 for (i = 0; i < t->path_list_size; i++) {
895 path = t->path_list[i];
896 base = path_base_name(path);
897 mask = talloc_asprintf(ctx, "%s\\%s", client_get_cur_dir(), path);
899 DBG(5, ("incl. path='%s', base='%s', mask='%s'\n",
900 path, base ? base : "NULL", mask));
903 base = talloc_asprintf(ctx, "%s%s\\",
904 client_get_cur_dir(), path_base_name(path));
905 DBG(5, ("cd '%s' before do_list\n", base));
906 client_set_cur_dir(base);
908 status = do_list(mask, TAR_DO_LIST_ATTR, get_file_callback, false, true);
910 client_set_cur_dir(start_dir);
912 if (!NT_STATUS_IS_OK(status)) {
913 DBG(0, ("do_list failed on %s (%s)\n", path, nt_errstr(status)));
923 static int tar_create(struct tar* t)
925 TALLOC_CTX *ctx = talloc_tos();
931 t->archive = archive_write_new();
934 r = archive_write_set_format_pax_restricted(t->archive);
935 if (r != ARCHIVE_OK) {
936 DBG(0, ("Can't open %s: %s\n", t->tar_path,
937 archive_error_string(t->archive)));
940 if (strequal(t->tar_path, "-")) {
941 r = archive_write_open_fd(t->archive, STDOUT_FILENO);
943 r = archive_write_open_filename(t->archive, t->tar_path);
946 if (r != ARCHIVE_OK) {
947 DBG(0, ("Can't open %s: %s\n", t->tar_path,
948 archive_error_string(t->archive)));
955 * In inclusion mode, iterate on the inclusion list
957 if (t->mode.selection == TAR_INCLUDE && t->path_list_size > 0) {
958 if (tar_create_from_list(t)) {
963 mask = talloc_asprintf(ctx, "%s\\*", client_get_cur_dir());
964 DBG(5, ("tar_process do_list with mask: %s\n", mask));
965 status = do_list(mask, TAR_DO_LIST_ATTR, get_file_callback, false, true);
966 if (!NT_STATUS_IS_OK(status)) {
967 DBG(0, ("do_list fail %s\n", nt_errstr(status)));
974 DBG(0, ("Total bytes received: %" PRIu64 "\n", t->total_size));
977 r = archive_write_close(t->archive);
978 if (r != ARCHIVE_OK) {
979 DBG(0, ("Fatal: %s\n", archive_error_string(t->archive)));
985 archive_write_free(t->archive);
990 * Return upper limit for the number of token in @str.
992 * The result is not exact, the actual number of token might be less
993 * than what is returned.
995 static int max_token (const char *str)
1017 * cmd_tar - interactive command to start a tar backup/restoration
1019 * Check presence of argument, parse them and handle the request.
1023 TALLOC_CTX *ctx = talloc_tos();
1024 const extern char *cmd_ptr;
1028 int maxtok = max_token(cmd_ptr);
1032 if (!next_token_talloc(ctx, &cmd_ptr, &buf, NULL)) {
1033 DBG(0, ("tar <c|x>[IXFbganN] [options] <tar file> [path list]\n"));
1038 val = talloc_array(ctx, const char*, maxtok);
1040 while (next_token_talloc(ctx, &cmd_ptr, &buf, NULL)) {
1044 if (!tar_parse_args(&tar_ctx, flag, val, i)) {
1045 DBG(0, ("parse_args failed\n"));
1050 if (tar_process(&tar_ctx)) {
1051 DBG(0, ("tar_process failed\n"));
1060 static int tar_extract(struct tar *t)
1064 struct archive_entry *entry;
1066 t->archive = archive_read_new();
1067 archive_read_support_format_all(t->archive);
1068 archive_read_support_filter_all(t->archive);
1070 if (strequal(t->tar_path, "-")) {
1071 r = archive_read_open_fd(t->archive, STDIN_FILENO, t->mode.blocksize);
1073 r = archive_read_open_filename(t->archive, t->tar_path,
1077 if (r != ARCHIVE_OK) {
1078 DBG(0, ("Can't open %s : %s\n", t->tar_path,
1079 archive_error_string(t->archive)));
1085 r = archive_read_next_header(t->archive, &entry);
1086 if (r == ARCHIVE_EOF) {
1089 if (r == ARCHIVE_WARN) {
1090 DBG(0, ("Warning: %s\n", archive_error_string(t->archive)));
1092 if (r == ARCHIVE_FATAL) {
1093 DBG(0, ("Fatal: %s\n", archive_error_string(t->archive)));
1098 if (tar_extract_skip_path(t, entry)) {
1099 DBG(5, ("--- %s\n", archive_entry_pathname(entry)));
1103 DBG(5, ("+++ %s\n", archive_entry_pathname(entry)));
1105 if (tar_send_file(t, entry)) {
1112 r = archive_read_free(t->archive);
1113 if (r != ARCHIVE_OK) {
1114 DBG(0, ("Can't close %s : %s\n", t->tar_path,
1115 archive_error_string(t->archive)));
1121 int tar_process(struct tar *t)
1125 switch(t->mode.operation) {
1127 rc = tar_extract(t);
1133 DBG(0, ("Invalid tar state\n"));
1137 DBG(5, ("tar_process done, err = %d\n", rc));
1142 * tar_parse_args - parse and set tar command line arguments
1143 * @flag: string pointing to tar options
1144 * @val: number of tar arguments
1145 * @valsize: table of arguments after the flags (number of element in val)
1147 * tar arguments work in a weird way. For each flag f that takes a
1148 * value v, the user is supposed to type:
1151 * -Tf1f2f3 v1 v2 v3 TARFILE PATHS...
1153 * in the interactive session:
1154 * tar f1f2f3 v1 v2 v3 TARFILE PATHS...
1156 * opt has only flags (eg. "f1f2f3") and val has the arguments
1157 * (values) following them (eg. ["v1", "v2", "v3", "TARFILE", "PATH1",
1160 * There are only 2 flags that take an arg: b and N. The other flags
1161 * just change the semantic of PATH or TARFILE.
1163 * PATH can be a list of included/excluded paths, the path to a file
1164 * containing a list of included/excluded paths to use (F flag). If no
1165 * PATH is provided, the whole share is used (/).
1167 int tar_parse_args(struct tar* t, const char *flag,
1168 const char **val, int valsize)
1170 TALLOC_CTX *ctx = talloc_tos();
1173 /* index of next value to use */
1177 * Reset back some options - could be from interactive version
1178 * all other modes are left as they are
1180 t->mode.operation = TAR_NO_OPERATION;
1181 t->mode.selection = TAR_NO_SELECTION;
1182 t->mode.dry = false;
1183 t->to_process = false;
1190 if (t->mode.operation != TAR_NO_OPERATION) {
1191 printf("Tar must be followed by only one of c or x.\n");
1194 t->mode.operation = TAR_CREATE;
1197 if (t->mode.operation != TAR_NO_OPERATION) {
1198 printf("Tar must be followed by only one of c or x.\n");
1201 t->mode.operation = TAR_EXTRACT;
1206 if (t->mode.selection != TAR_NO_SELECTION) {
1207 DBG(0,("Only one of I,X,F must be specified\n"));
1210 t->mode.selection = TAR_INCLUDE;
1213 if (t->mode.selection != TAR_NO_SELECTION) {
1214 DBG(0,("Only one of I,X,F must be specified\n"));
1217 t->mode.selection = TAR_EXCLUDE;
1220 if (t->mode.selection != TAR_NO_SELECTION) {
1221 DBG(0,("Only one of I,X,F must be specified\n"));
1224 t->mode.selection = TAR_INCLUDE;
1230 if (ival >= valsize) {
1231 DBG(0, ("Option b must be followed by a blocksize\n"));
1235 if (!tar_set_blocksize(t, atoi(val[ival]))) {
1236 DBG(0, ("Option b must be followed by a valid blocksize\n"));
1243 /* incremental mode */
1245 t->mode.incremental = true;
1250 if (ival >= valsize) {
1251 DBG(0, ("Option N must be followed by valid file name\n"));
1255 if (!tar_set_newer_than(t, val[ival])) {
1256 DBG(0,("Error setting newer-than time\n"));
1265 t->mode.reset = true;
1270 t->mode.verbose = true;
1275 t->mode.regex = true;
1280 if (t->mode.operation != TAR_CREATE) {
1281 DBG(0, ("n is only meaningful when creating a tar-file\n"));
1286 DBG(0, ("dry_run set\n"));
1290 DBG(0,("Unknown tar option\n"));
1295 /* no selection given? default selection is include */
1296 if (t->mode.selection == TAR_NO_SELECTION) {
1297 t->mode.selection = TAR_INCLUDE;
1300 if (valsize - ival < 1) {
1301 DBG(0, ("No tar file given.\n"));
1305 /* handle TARFILE */
1306 t->tar_path = talloc_strdup(ctx, val[ival]);
1310 * Make sure that dbf points to stderr if we are using stdout for
1313 if (t->mode.operation == TAR_CREATE && strequal(t->tar_path, "-")) {
1314 setup_logging("smbclient", DEBUG_STDERR);
1317 /* handle PATHs... */
1319 /* flag F -> read file list */
1321 if (valsize - ival != 1) {
1322 DBG(0,("Option F must be followed by exactly one filename.\n"));
1326 if (!tar_read_inclusion_file(t, val[ival])) {
1332 /* otherwise store all the PATHs on the command line */
1335 for (i = ival; i < valsize; i++) {
1336 tar_add_selection_path(t, val[i]);
1340 t->to_process = true;