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 * # General overview of the tar extension
23 * All tar_xxx() functions work on a `struct tar` which store most of
24 * the context of the backup process.
26 * The current tar context can be accessed via the global variable
27 * `tar_ctx`. It's publicly exported as an opaque handle via
30 * A tar context is first configured through tar_parse_args() which
31 * can be called from either the CLI (in client.c) or the interactive
32 * session (via the cmd_tar() callback).
34 * Once the configuration is done (successfully), the context is ready
35 * for processing and tar_to_process() returns true.
37 * The next step is to call tar_process() which dispatch the
38 * processing to either tar_create() or tar_extract(), depending on
43 * tar_create() creates an archive using the libarchive API then
45 * - iterates on the requested paths if the context is in inclusion
46 * mode with tar_create_from_list()
48 * - or iterates on the whole share (starting from the current dir) if
49 * in exclusion mode or if no specific path were requested
51 * The do_list() function from client.c is used to list recursively
52 * the share. In particular it takes a DOS path mask (eg. \mydir\*)
53 * and a callback function which will be called with each file name
54 * and attributes. The tar callback function is get_file_callback().
56 * The callback function checks whether the file should be skipped
57 * according the the configuration via tar_create_skip_path(). If it's
58 * not skipped it's downloaded and written to the archive in
61 * ## Archive extraction
63 * tar_extract() opens the archive and iterates on each file in
64 * it. For each file tar_extract_skip_path() checks whether it should
65 * be skipped according to the config. If it's not skipped it's
66 * uploaded on the server in tar_send_file().
70 #include "system/filesys.h"
71 #include "client/client_proto.h"
72 #include "client/clitar_proto.h"
73 #include "libsmb/libsmb.h"
75 #ifdef HAVE_LIBARCHIVE
78 #include <archive_entry.h>
80 /* prepend module name and line number to debug messages */
81 #define DBG(a, b) (DEBUG(a, ("tar:%-4d ", __LINE__)), DEBUG(a, b))
83 /* preprocessor magic to strigify __LINE__ (int) */
85 #define STR2(x) STR1(x)
87 /* helper macro to die in case of NULL pointer */
88 #define PANIC_IF_NULL(x) \
89 _panic_if_null(x, __FILE__ ":" STR2(__LINE__) " (" #x ") == NULL\n")
91 /* prototype to silent gcc warning */
92 static inline void* _panic_if_null(void *p, const char *expr);
93 static inline void* _panic_if_null(void *p, const char *expr)
102 * Number of byte in a block unit.
104 #define TAR_BLOCK_UNIT 512
107 * Default tar block size in TAR_BLOCK_UNIT.
109 #define TAR_DEFAULT_BLOCK_SIZE 20
112 * Maximum value for the blocksize field
114 #define TAR_MAX_BLOCK_SIZE 0xffff
117 * Size of the buffer used when downloading a file
119 #define TAR_CLI_READ_SIZE 0xff00
121 #define TAR_DO_LIST_ATTR (FILE_ATTRIBUTE_DIRECTORY \
122 | FILE_ATTRIBUTE_SYSTEM \
123 | FILE_ATTRIBUTE_HIDDEN)
128 TAR_CREATE, /* c flag */
129 TAR_EXTRACT, /* x flag */
134 TAR_INCLUDE, /* I and F flag, default */
135 TAR_EXCLUDE, /* X flag */
144 TALLOC_CTX *talloc_ctx;
146 /* in state that needs/can be processed? */
151 enum tar_operation operation; /* create, extract */
152 enum tar_selection selection; /* include, exclude */
153 int blocksize; /* size in TAR_BLOCK_UNIT of a tar file block */
154 bool hidden; /* backup hidden file? */
155 bool system; /* backup system file? */
156 bool incremental; /* backup _only_ archived file? */
157 bool reset; /* unset archive bit? */
158 bool dry; /* don't write tar file? */
159 bool regex; /* XXX: never actually using regex... */
160 bool verbose; /* XXX: ignored */
163 /* nb of bytes received */
166 /* path to tar archive name */
169 /* list of path to include or exclude */
174 struct archive *archive;
178 * Global context imported in client.c when needed.
182 struct tar tar_ctx = {
183 .mode.selection = TAR_INCLUDE,
184 .mode.blocksize = TAR_DEFAULT_BLOCK_SIZE,
187 .mode.incremental = false,
191 .mode.verbose = false,
194 /* tar, local function */
195 static int tar_create(struct tar* t);
196 static int tar_create_from_list(struct tar *t);
197 static int tar_extract(struct tar *t);
198 static int tar_read_inclusion_file (struct tar *t, const char* filename);
199 static int tar_send_file(struct tar *t, struct archive_entry *entry);
200 static int tar_set_blocksize(struct tar *t, int size);
201 static int tar_set_newer_than(struct tar *t, const char *filename);
202 static void tar_add_selection_path(struct tar *t, const char *path);
203 static void tar_dump(struct tar *t);
204 static bool tar_extract_skip_path(struct tar *t, struct archive_entry *entry);
205 static TALLOC_CTX *tar_reset_mem_context(struct tar *t);
206 static void tar_free_mem_context(struct tar *t);
207 static bool tar_create_skip_path(struct tar *t,
208 const char *fullpath,
209 const struct file_info *finfo);
211 static bool tar_path_in_list(struct tar *t,
215 static int tar_get_file(struct tar *t,
216 const char *full_dos_path,
217 struct file_info *finfo);
219 static NTSTATUS get_file_callback(struct cli_state *cli,
220 struct file_info *finfo,
224 static char *fix_unix_path (char *path, bool removeprefix);
225 static char *path_base_name (const char *path);
226 static const char* skip_useless_char_in_path(const char *p);
227 static int make_remote_path(const char *full_path);
228 static int max_token (const char *str);
229 static bool is_subpath(const char *sub, const char *full);
230 static int set_remote_attr(const char *filename, uint16 new_attr, int mode);
233 * tar_get_ctx - retrieve global tar context handle
235 struct tar *tar_get_ctx()
241 * cmd_block - interactive command to change tar blocksize
243 * Read a size from the client command line and update the current
248 /* XXX: from client.c */
249 const extern char *cmd_ptr;
251 TALLOC_CTX *ctx = PANIC_IF_NULL(talloc_new(NULL));
255 ok = next_token_talloc(ctx, &cmd_ptr, &buf, NULL);
257 DBG(0, ("blocksize <n>\n"));
262 ok = tar_set_blocksize(&tar_ctx, atoi(buf));
264 DBG(0, ("invalid blocksize\n"));
269 DBG(2, ("blocksize is now %d\n", tar_ctx.mode.blocksize));
277 * cmd_tarmode - interactive command to change tar behaviour
279 * Read one or more modes from the client command line and update the
282 int cmd_tarmode(void)
284 const extern char *cmd_ptr;
287 TALLOC_CTX *ctx = PANIC_IF_NULL(talloc_new(NULL));
294 {"full", &tar_ctx.mode.incremental, false},
295 {"inc", &tar_ctx.mode.incremental, true },
296 {"reset", &tar_ctx.mode.reset, true },
297 {"noreset", &tar_ctx.mode.reset, false},
298 {"system", &tar_ctx.mode.system, true },
299 {"nosystem", &tar_ctx.mode.system, false},
300 {"hidden", &tar_ctx.mode.hidden, true },
301 {"nohidden", &tar_ctx.mode.hidden, false},
302 {"verbose", &tar_ctx.mode.verbose, true },
303 {"noquiet", &tar_ctx.mode.verbose, true },
304 {"quiet", &tar_ctx.mode.verbose, false},
305 {"noverbose", &tar_ctx.mode.verbose, false},
308 while (next_token_talloc(ctx, &cmd_ptr, &buf, NULL)) {
309 for (i = 0; i < ARRAY_SIZE(table); i++) {
310 if (strequal(table[i].cmd, buf)) {
311 *table[i].p = table[i].value;
316 if (i == ARRAY_SIZE(table))
317 DBG(0, ("tarmode: unrecognised option %s\n", buf));
320 DBG(0, ("tarmode is now %s, %s, %s, %s, %s\n",
321 tar_ctx.mode.incremental ? "incremental" : "full",
322 tar_ctx.mode.system ? "system" : "nosystem",
323 tar_ctx.mode.hidden ? "hidden" : "nohidden",
324 tar_ctx.mode.reset ? "reset" : "noreset",
325 tar_ctx.mode.verbose ? "verbose" : "quiet"));
332 * cmd_tar - interactive command to start a tar backup/restoration
334 * Check presence of argument, parse them and handle the request.
338 TALLOC_CTX *ctx = PANIC_IF_NULL(talloc_new(NULL));
339 const extern char *cmd_ptr;
343 int maxtok = max_token(cmd_ptr);
349 ok = next_token_talloc(ctx, &cmd_ptr, &buf, NULL);
351 DBG(0, ("tar <c|x>[IXFbganN] [options] <tar file> [path list]\n"));
357 val = PANIC_IF_NULL(talloc_array(ctx, const char*, maxtok));
359 while (next_token_talloc(ctx, &cmd_ptr, &buf, NULL)) {
363 rc = tar_parse_args(&tar_ctx, flag, val, i);
365 DBG(0, ("parse_args failed\n"));
370 rc = tar_process(&tar_ctx);
372 DBG(0, ("tar_process failed\n"));
383 * cmd_setmode - interactive command to set DOS attributes
385 * Read a filename and mode from the client command line and update
386 * the file DOS attributes.
388 int cmd_setmode(void)
390 const extern char *cmd_ptr;
393 uint16 attr[2] = {0};
395 TALLOC_CTX *ctx = PANIC_IF_NULL(talloc_new(NULL));
400 ok = next_token_talloc(ctx, &cmd_ptr, &buf, NULL);
402 DBG(0, ("setmode <filename> <[+|-]rsha>\n"));
407 fname = PANIC_IF_NULL(talloc_asprintf(ctx,
409 client_get_cur_dir(),
416 while (next_token_talloc(ctx, &cmd_ptr, &buf, NULL)) {
428 attr[mode] |= FILE_ATTRIBUTE_READONLY;
431 attr[mode] |= FILE_ATTRIBUTE_HIDDEN;
434 attr[mode] |= FILE_ATTRIBUTE_SYSTEM;
437 attr[mode] |= FILE_ATTRIBUTE_ARCHIVE;
440 DBG(0, ("setmode <filename> <perm=[+|-]rsha>\n"));
447 if (attr[ATTR_SET] == 0 && attr[ATTR_UNSET] == 0) {
448 DBG(0, ("setmode <filename> <[+|-]rsha>\n"));
453 DBG(2, ("perm set %d %d\n", attr[ATTR_SET], attr[ATTR_UNSET]));
455 /* ignore return value: server might not store DOS attributes */
456 set_remote_attr(fname, attr[ATTR_SET], ATTR_SET);
457 set_remote_attr(fname, attr[ATTR_UNSET], ATTR_UNSET);
464 * tar_parse_args - parse and set tar command line arguments
465 * @flag: string pointing to tar options
466 * @val: number of tar arguments
467 * @valsize: table of arguments after the flags (number of element in val)
469 * tar arguments work in a weird way. For each flag f that takes a
470 * value v, the user is supposed to type:
473 * -Tf1f2f3 v1 v2 v3 TARFILE PATHS...
475 * in the interactive session:
476 * tar f1f2f3 v1 v2 v3 TARFILE PATHS...
478 * @flag has only flags (eg. "f1f2f3") and @val has the arguments
479 * (values) following them (eg. ["v1", "v2", "v3", "TARFILE", "PATH1",
482 * There are only 2 flags that take an arg: b and N. The other flags
483 * just change the semantic of PATH or TARFILE.
485 * PATH can be a list of included/excluded paths, the path to a file
486 * containing a list of included/excluded paths to use (F flag). If no
487 * PATH is provided, the whole share is used (/).
489 int tar_parse_args(struct tar* t, const char *flag,
490 const char **val, int valsize)
493 bool do_read_list = false;
494 /* index of next value to use */
499 DBG(0, ("Invalid tar context\n"));
503 ctx = tar_reset_mem_context(t);
505 * Reset back some options - could be from interactive version
506 * all other modes are left as they are
508 t->mode.operation = TAR_NO_OPERATION;
509 t->mode.selection = TAR_NO_SELECTION;
511 t->to_process = false;
514 while (flag[0] != '\0') {
518 if (t->mode.operation != TAR_NO_OPERATION) {
519 printf("Tar must be followed by only one of c or x.\n");
522 t->mode.operation = TAR_CREATE;
525 if (t->mode.operation != TAR_NO_OPERATION) {
526 printf("Tar must be followed by only one of c or x.\n");
529 t->mode.operation = TAR_EXTRACT;
534 if (t->mode.selection != TAR_NO_SELECTION) {
535 DBG(0,("Only one of I,X,F must be specified\n"));
538 t->mode.selection = TAR_INCLUDE;
541 if (t->mode.selection != TAR_NO_SELECTION) {
542 DBG(0,("Only one of I,X,F must be specified\n"));
545 t->mode.selection = TAR_EXCLUDE;
548 if (t->mode.selection != TAR_NO_SELECTION) {
549 DBG(0,("Only one of I,X,F must be specified\n"));
552 t->mode.selection = TAR_INCLUDE;
558 if (ival >= valsize) {
559 DBG(0, ("Option b must be followed by a blocksize\n"));
563 if (tar_set_blocksize(t, atoi(val[ival]))) {
564 DBG(0, ("Option b must be followed by a valid blocksize\n"));
571 /* incremental mode */
573 t->mode.incremental = true;
578 if (ival >= valsize) {
579 DBG(0, ("Option N must be followed by valid file name\n"));
583 if (tar_set_newer_than(t, val[ival])) {
584 DBG(0,("Error setting newer-than time\n"));
593 t->mode.reset = true;
598 t->mode.verbose = true;
603 t->mode.regex = true;
608 if (t->mode.operation != TAR_CREATE) {
609 DBG(0, ("n is only meaningful when creating a tar-file\n"));
614 DBG(0, ("dry_run set\n"));
618 DBG(0,("Unknown tar option\n"));
625 /* no selection given? default selection is include */
626 if (t->mode.selection == TAR_NO_SELECTION) {
627 t->mode.selection = TAR_INCLUDE;
630 if (valsize - ival < 1) {
631 DBG(0, ("No tar file given.\n"));
636 t->tar_path = PANIC_IF_NULL(talloc_strdup(ctx, val[ival]));
640 * Make sure that dbf points to stderr if we are using stdout for
643 if (t->mode.operation == TAR_CREATE && strequal(t->tar_path, "-")) {
644 setup_logging("smbclient", DEBUG_STDERR);
647 /* handle PATHs... */
649 /* flag F -> read file list */
651 if (valsize - ival != 1) {
652 DBG(0,("Option F must be followed by exactly one filename.\n"));
656 rc = tar_read_inclusion_file(t, val[ival]);
663 /* otherwise store all the PATHs on the command line */
666 for (i = ival; i < valsize; i++) {
667 tar_add_selection_path(t, val[i]);
671 t->to_process = true;
677 * tar_process - start processing archive
679 * The talloc context of the fields is freed at the end of the call.
681 int tar_process(struct tar *t)
686 DBG(0, ("Invalid tar context\n"));
690 switch(t->mode.operation) {
698 DBG(0, ("Invalid tar state\n"));
702 t->to_process = false;
703 tar_free_mem_context(t);
704 DBG(5, ("tar_process done, err = %d\n", rc));
709 * tar_create - create archive and fetch files
711 static int tar_create(struct tar* t)
713 TALLOC_CTX *ctx = PANIC_IF_NULL(talloc_new(NULL));
719 t->archive = archive_write_new();
722 const int bsize = t->mode.blocksize * TAR_BLOCK_UNIT;
723 r = archive_write_set_bytes_per_block(t->archive, bsize);
724 if (r != ARCHIVE_OK) {
725 DBG(0, ("Can't use a block size of %d bytes", bsize));
731 * Use PAX restricted format which is not the most
732 * conservative choice but has useful extensions and is widely
735 r = archive_write_set_format_pax_restricted(t->archive);
736 if (r != ARCHIVE_OK) {
737 DBG(0, ("Can't use pax restricted format: %s\n",
738 archive_error_string(t->archive)));
743 if (strequal(t->tar_path, "-")) {
744 r = archive_write_open_fd(t->archive, STDOUT_FILENO);
746 r = archive_write_open_filename(t->archive, t->tar_path);
749 if (r != ARCHIVE_OK) {
750 DBG(0, ("Can't open %s: %s\n", t->tar_path,
751 archive_error_string(t->archive)));
758 * In inclusion mode, iterate on the inclusion list
760 if (t->mode.selection == TAR_INCLUDE && t->path_list_size > 0) {
761 if (tar_create_from_list(t)) {
766 mask = PANIC_IF_NULL(talloc_asprintf(ctx, "%s\\*",
767 client_get_cur_dir()));
768 DBG(5, ("tar_process do_list with mask: %s\n", mask));
769 status = do_list(mask, TAR_DO_LIST_ATTR, get_file_callback, false, true);
770 if (!NT_STATUS_IS_OK(status)) {
771 DBG(0, ("do_list fail %s\n", nt_errstr(status)));
778 DBG(0, ("Total bytes received: %" PRIu64 "\n", t->total_size));
781 r = archive_write_close(t->archive);
782 if (r != ARCHIVE_OK) {
783 DBG(0, ("Fatal: %s\n", archive_error_string(t->archive)));
789 archive_write_free(t->archive);
795 * tar_create_from_list - fetch from path list in include mode
797 static int tar_create_from_list(struct tar *t)
799 TALLOC_CTX *ctx = PANIC_IF_NULL(talloc_new(NULL));
802 const char *path, *mask, *base, *start_dir;
805 start_dir = talloc_strdup(ctx, client_get_cur_dir());
807 for (i = 0; i < t->path_list_size; i++) {
808 path = t->path_list[i];
809 base = path_base_name(path);
810 mask = PANIC_IF_NULL(talloc_asprintf(ctx, "%s\\%s",
811 client_get_cur_dir(), path));
813 DBG(5, ("incl. path='%s', base='%s', mask='%s'\n",
814 path, base ? base : "NULL", mask));
817 base = talloc_asprintf(ctx, "%s%s\\",
818 client_get_cur_dir(), path_base_name(path));
819 DBG(5, ("cd '%s' before do_list\n", base));
820 client_set_cur_dir(base);
822 status = do_list(mask, TAR_DO_LIST_ATTR, get_file_callback, false, true);
824 client_set_cur_dir(start_dir);
826 if (!NT_STATUS_IS_OK(status)) {
827 DBG(0, ("do_list failed on %s (%s)\n", path, nt_errstr(status)));
839 * get_file_callback - do_list callback
841 * Callback for client.c do_list(). Called for each file found on the
842 * share matching do_list mask. Recursively call do_list() with itself
843 * as callback when the current file is a directory.
845 static NTSTATUS get_file_callback(struct cli_state *cli,
846 struct file_info *finfo,
849 TALLOC_CTX *ctx = PANIC_IF_NULL(talloc_new(NULL));
850 NTSTATUS err = NT_STATUS_OK;
852 const char *initial_dir = client_get_cur_dir();
855 remote_name = PANIC_IF_NULL(talloc_asprintf(ctx, "%s%s",
856 initial_dir, finfo->name));
858 if (strequal(finfo->name, "..") || strequal(finfo->name, ".")) {
862 rc = tar_create_skip_path(&tar_ctx, remote_name, finfo);
864 DBG(5, ("--- %s\n", remote_name));
868 if (finfo->mode & FILE_ATTRIBUTE_DIRECTORY) {
873 old_dir = PANIC_IF_NULL(talloc_strdup(ctx, initial_dir));
874 new_dir = PANIC_IF_NULL(talloc_asprintf(ctx, "%s%s\\",
875 initial_dir, finfo->name));
876 mask = PANIC_IF_NULL(talloc_asprintf(ctx, "%s*", new_dir));
878 rc = tar_get_file(&tar_ctx, remote_name, finfo);
880 err = NT_STATUS_UNSUCCESSFUL;
884 client_set_cur_dir(new_dir);
885 do_list(mask, TAR_DO_LIST_ATTR, get_file_callback, false, true);
886 client_set_cur_dir(old_dir);
888 rc = tar_get_file(&tar_ctx, remote_name, finfo);
890 err = NT_STATUS_UNSUCCESSFUL;
901 * tar_get_file - fetch a remote file to the local archive
902 * @full_dos_path: path to the file to fetch
903 * @finfo: attributes of the file to fetch
905 static int tar_get_file(struct tar *t, const char *full_dos_path,
906 struct file_info *finfo)
908 extern struct cli_state *cli;
909 TALLOC_CTX *ctx = PANIC_IF_NULL(talloc_new(NULL));
911 struct archive_entry *entry;
912 char *full_unix_path;
913 char buf[TAR_CLI_READ_SIZE];
916 uint16_t remote_fd = (uint16_t)-1;
918 const bool isdir = finfo->mode & FILE_ATTRIBUTE_DIRECTORY;
920 DBG(5, ("+++ %s\n", full_dos_path));
922 t->total_size += finfo->size;
929 /* ignore return value: server might not store DOS attributes */
930 set_remote_attr(full_dos_path, FILE_ATTRIBUTE_ARCHIVE, ATTR_UNSET);
933 full_unix_path = PANIC_IF_NULL(talloc_asprintf(ctx, ".%s", full_dos_path));
934 string_replace(full_unix_path, '\\', '/');
935 entry = archive_entry_new();
936 archive_entry_copy_pathname(entry, full_unix_path);
937 archive_entry_set_filetype(entry, isdir ? AE_IFDIR : AE_IFREG);
938 archive_entry_set_atime(entry,
939 finfo->atime_ts.tv_sec,
940 finfo->atime_ts.tv_nsec);
941 archive_entry_set_mtime(entry,
942 finfo->mtime_ts.tv_sec,
943 finfo->mtime_ts.tv_nsec);
944 archive_entry_set_ctime(entry,
945 finfo->ctime_ts.tv_sec,
946 finfo->ctime_ts.tv_nsec);
947 archive_entry_set_perm(entry, isdir ? 0755 : 0644);
949 * check if we can safely cast unsigned file size to libarchive
950 * signed size. Very unlikely problem (>9 exabyte file)
952 if (finfo->size > INT64_MAX) {
953 DBG(0, ("Remote file %s too big\n", full_dos_path));
957 archive_entry_set_size(entry, (int64_t)finfo->size);
959 r = archive_write_header(t->archive, entry);
960 if (r != ARCHIVE_OK) {
961 DBG(0, ("Fatal: %s\n", archive_error_string(t->archive)));
967 DBG(5, ("get_file skip dir %s\n", full_dos_path));
971 status = cli_open(cli, full_dos_path, O_RDONLY, DENY_NONE, &remote_fd);
972 if (!NT_STATUS_IS_OK(status)) {
973 DBG(0,("%s opening remote file %s\n",
974 nt_errstr(status), full_dos_path));
979 status = cli_read(cli, remote_fd, buf, off, sizeof(buf), &len);
980 if (!NT_STATUS_IS_OK(status)) {
981 DBG(0,("Error reading file %s : %s\n",
982 full_dos_path, nt_errstr(status)));
989 r = archive_write_data(t->archive, buf, len);
991 DBG(0, ("Fatal: %s\n", archive_error_string(t->archive)));
996 } while (off < finfo->size);
999 cli_close(cli, remote_fd);
1002 archive_entry_free(entry);
1010 * tar_extract - open archive and send files.
1012 static int tar_extract(struct tar *t)
1016 struct archive_entry *entry;
1017 const size_t bsize = t->mode.blocksize * TAR_BLOCK_UNIT;
1020 t->archive = archive_read_new();
1021 archive_read_support_format_all(t->archive);
1022 archive_read_support_filter_all(t->archive);
1024 if (strequal(t->tar_path, "-")) {
1025 r = archive_read_open_fd(t->archive, STDIN_FILENO, bsize);
1027 r = archive_read_open_filename(t->archive, t->tar_path, bsize);
1030 if (r != ARCHIVE_OK) {
1031 DBG(0, ("Can't open %s : %s\n", t->tar_path,
1032 archive_error_string(t->archive)));
1038 r = archive_read_next_header(t->archive, &entry);
1039 if (r == ARCHIVE_EOF) {
1042 if (r == ARCHIVE_WARN) {
1043 DBG(0, ("Warning: %s\n", archive_error_string(t->archive)));
1045 if (r == ARCHIVE_FATAL) {
1046 DBG(0, ("Fatal: %s\n", archive_error_string(t->archive)));
1051 rc = tar_extract_skip_path(t, entry);
1053 DBG(5, ("--- %s\n", archive_entry_pathname(entry)));
1057 DBG(5, ("+++ %s\n", archive_entry_pathname(entry)));
1059 rc = tar_send_file(t, entry);
1067 r = archive_read_free(t->archive);
1068 if (r != ARCHIVE_OK) {
1069 DBG(0, ("Can't close %s : %s\n", t->tar_path,
1070 archive_error_string(t->archive)));
1077 * tar_send_file - send @entry to the remote server
1078 * @entry: current archive entry
1080 * Handle the creation of the parent directories and transfer the
1081 * entry to a new remote file.
1083 static int tar_send_file(struct tar *t, struct archive_entry *entry)
1085 extern struct cli_state *cli;
1086 TALLOC_CTX *ctx = PANIC_IF_NULL(talloc_new(NULL));
1090 uint16_t remote_fd = (uint16_t) -1;
1092 int flags = O_RDWR | O_CREAT | O_TRUNC;
1093 mode_t mode = archive_entry_filetype(entry);
1096 dos_path = PANIC_IF_NULL(talloc_strdup(ctx, archive_entry_pathname(entry)));
1097 fix_unix_path(dos_path, true);
1099 full_path = PANIC_IF_NULL(talloc_strdup(ctx, client_get_cur_dir()));
1100 full_path = PANIC_IF_NULL(talloc_strdup_append(full_path, dos_path));
1102 if (mode != AE_IFREG && mode != AE_IFDIR) {
1103 DBG(0, ("Skipping non-dir & non-regular file %s\n", full_path));
1107 rc = make_remote_path(full_path);
1113 if (mode == AE_IFDIR) {
1117 status = cli_open(cli, full_path, flags, DENY_NONE, &remote_fd);
1118 if (!NT_STATUS_IS_OK(status)) {
1119 DBG(0, ("Error opening remote file %s: %s\n",
1120 full_path, nt_errstr(status)));
1131 r = archive_read_data_block(t->archive, &buf, &len, &off);
1132 if (r == ARCHIVE_EOF) {
1135 if (r == ARCHIVE_WARN) {
1136 DBG(0, ("Warning: %s\n", archive_error_string(t->archive)));
1138 if (r == ARCHIVE_FATAL) {
1139 DBG(0, ("Fatal: %s\n", archive_error_string(t->archive)));
1144 status = cli_writeall(cli, remote_fd, 0, buf, off, len, NULL);
1145 if (!NT_STATUS_IS_OK(status)) {
1146 DBG(0, ("Error writing remote file %s: %s\n",
1147 full_path, nt_errstr(status)));
1154 status = cli_close(cli, remote_fd);
1155 if (!NT_STATUS_IS_OK(status)) {
1156 DBG(0, ("Error losing remote file %s: %s\n",
1157 full_path, nt_errstr(status)));
1167 * tar_add_selection_path - add a path to the path list
1168 * @path: path to add
1170 static void tar_add_selection_path(struct tar *t, const char *path)
1172 TALLOC_CTX *ctx = t->talloc_ctx;
1173 if (!t->path_list) {
1174 t->path_list = PANIC_IF_NULL(str_list_make_empty(ctx));
1175 t->path_list_size = 0;
1178 /* cast to silent gcc const-qual warning */
1179 t->path_list = PANIC_IF_NULL(str_list_add((void*)t->path_list,
1181 t->path_list_size++;
1182 fix_unix_path(t->path_list[t->path_list_size - 1], true);
1186 * tar_set_blocksize - set block size in TAR_BLOCK_UNIT
1188 static int tar_set_blocksize(struct tar *t, int size)
1190 if (size <= 0 || size > TAR_MAX_BLOCK_SIZE) {
1194 t->mode.blocksize = size;
1200 * tar_set_newer_than - set date threshold of saved files
1201 * @filename: local path to a file
1203 * Only files newer than the modification time of @filename will be
1206 * Note: this function set the global variable newer_than from
1207 * client.c. Thus the time is not a field of the tar structure. See
1208 * cmd_newer() to change its value from an interactive session.
1210 static int tar_set_newer_than(struct tar *t, const char *filename)
1212 extern time_t newer_than;
1213 SMB_STRUCT_STAT stbuf;
1216 rc = sys_stat(filename, &stbuf, false);
1218 DBG(0, ("Error setting newer-than time\n"));
1222 newer_than = convert_timespec_to_time_t(stbuf.st_ex_mtime);
1223 DBG(1, ("Getting files newer than %s\n", time_to_asc(newer_than)));
1228 * tar_read_inclusion_file - set path list from file
1229 * @filename: path to the list file
1231 * Read and add each line of @filename to the path list.
1233 static int tar_read_inclusion_file (struct tar *t, const char* filename)
1236 TALLOC_CTX *ctx = PANIC_IF_NULL(talloc_new(NULL));
1240 fd = open(filename, O_RDONLY);
1242 DBG(0, ("Can't open inclusion file '%s': %s\n", filename, strerror(errno)));
1247 for (line = afdgets(fd, ctx, 0);
1249 line = afdgets(fd, ctx, 0)) {
1250 tar_add_selection_path(t, line);
1261 * tar_path_in_list - return true if @path is in the path list
1262 * @path: path to find
1263 * @reverse: when true also try to find path list element in @path
1265 * Look at each path of the path list and return true if @path is a
1266 * subpath of one of them.
1268 * If you want /path to be in the path list (path/a/, path/b/) set
1269 * @reverse to true to try to match the other way around.
1271 static bool tar_path_in_list(struct tar *t, const char *path, bool is_reverse)
1275 const char *pattern;
1277 if (path == NULL || path[0] == '\0') {
1281 p = skip_useless_char_in_path(path);
1283 for (i = 0; i < t->path_list_size; i++) {
1286 pattern = skip_useless_char_in_path(t->path_list[i]);
1287 is_in_list = is_subpath(p, pattern);
1289 is_in_list = is_in_list || is_subpath(pattern, p);
1300 * tar_extract_skip_path - return true if @entry should be skipped
1301 * @entry: current tar entry
1303 * Skip predicate for tar extraction (archive to server) only.
1305 static bool tar_extract_skip_path(struct tar *t,
1306 struct archive_entry *entry)
1308 const bool skip = true;
1309 const char *fullpath = archive_entry_pathname(entry);
1312 if (t->path_list_size <= 0) {
1316 if (t->mode.regex) {
1317 in = mask_match_list(fullpath, t->path_list, t->path_list_size, true);
1319 in = tar_path_in_list(t, fullpath, false);
1322 if (t->mode.selection == TAR_EXCLUDE) {
1326 return in ? !skip : skip;
1330 * tar_create_skip_path - return true if @fullpath shoud be skipped
1331 * @fullpath: full remote path of the current file
1332 * @finfo: remote file attributes
1334 * Skip predicate for tar creation (server to archive) only.
1336 static bool tar_create_skip_path(struct tar *t,
1337 const char *fullpath,
1338 const struct file_info *finfo)
1340 /* syntaxic sugar */
1341 const bool skip = true;
1342 const mode_t mode = finfo->mode;
1343 const bool isdir = mode & FILE_ATTRIBUTE_DIRECTORY;
1344 const bool exclude = t->mode.selection == TAR_EXCLUDE;
1349 /* 1. if we dont want X and we have X, skip */
1350 if (!t->mode.system && (mode & FILE_ATTRIBUTE_SYSTEM)) {
1354 if (!t->mode.hidden && (mode & FILE_ATTRIBUTE_HIDDEN)) {
1358 /* 2. if we only want archive and it's not, skip */
1360 if (t->mode.incremental && !(mode & FILE_ATTRIBUTE_ARCHIVE)) {
1365 /* 3. is it in the selection list? */
1368 * tar_create_from_list() use the include list as a starting
1369 * point, no need to check
1375 /* we are now in exclude mode */
1377 /* no matter the selection, no list => include everything */
1378 if (t->path_list_size <= 0) {
1382 if (t->mode.regex) {
1383 in = mask_match_list(fullpath, t->path_list, t->path_list_size, true);
1385 in = tar_path_in_list(t, fullpath, isdir && !exclude);
1388 return in ? skip : !skip;
1392 * tar_to_process - return true if @t is ready to be processed
1394 * @t is ready if it properly parsed command line arguments.
1396 bool tar_to_process (struct tar *t)
1399 DBG(0, ("Invalid tar context\n"));
1402 return t->to_process;
1406 * skip_useless_char_in_path - skip leading slashes/dots
1408 * Skip leading slashes, backslashes and dot-slashes.
1410 static const char* skip_useless_char_in_path(const char *p)
1413 if (*p == '/' || *p == '\\') {
1416 else if (p[0] == '.' && (p[1] == '/' || p[1] == '\\')) {
1426 * is_subpath - return true if the path @sub is a subpath of @full.
1427 * @sub: path to test
1428 * @full: container path
1430 * String comparaison is case-insensitive.
1432 * Return true if @sub = @full
1434 static bool is_subpath(const char *sub, const char *full)
1436 TALLOC_CTX *tmp_ctx = PANIC_IF_NULL(talloc_new(NULL));
1440 f = PANIC_IF_NULL(strlower_talloc(tmp_ctx, full));
1441 string_replace(f, '\\', '/');
1442 s = PANIC_IF_NULL(strlower_talloc(tmp_ctx, sub));
1443 string_replace(s, '\\', '/');
1445 /* find the point where sub and full diverge */
1446 while ((*f != '\0') && (*s != '\0') && (*f == *s)) {
1452 if ((*f == '\0') && (*s == '\0')) {
1453 return true; /* sub and full match */
1456 if ((*f == '\0') && (len > 0) && (*(f - 1) == '/')) {
1457 /* sub diverges from full at path separator */
1461 if ((*s == '\0') && (strcmp(f, "/") == 0)) {
1462 /* full diverges from sub with trailing slash only */
1466 if ((*s == '/') && (*f == '\0')) {
1467 /* sub diverges from full with extra path component */
1475 * set_remote_attr - set DOS attributes of a remote file
1476 * @filename: path to the file name
1477 * @new_attr: attribute bit mask to use
1478 * @mode: one of ATTR_SET or ATTR_UNSET
1480 * Update the file attributes with the one provided.
1482 static int set_remote_attr(const char *filename, uint16 new_attr, int mode)
1484 extern struct cli_state *cli;
1488 status = cli_getatr(cli, filename, &old_attr, NULL, NULL);
1489 if (!NT_STATUS_IS_OK(status)) {
1490 DBG(0, ("cli_getatr failed: %s\n", nt_errstr(status)));
1494 if (mode == ATTR_SET) {
1495 new_attr |= old_attr;
1497 new_attr = old_attr & ~new_attr;
1500 status = cli_setatr(cli, filename, new_attr, 0);
1501 if (!NT_STATUS_IS_OK(status)) {
1502 DBG(1, ("cli_setatr failed: %s\n", nt_errstr(status)));
1511 * make_remote_path - recursively make remote dirs
1512 * @full_path: full hierarchy to create
1514 * Create @full_path and each parent directories as needed.
1516 static int make_remote_path(const char *full_path)
1518 extern struct cli_state *cli;
1519 TALLOC_CTX *ctx = PANIC_IF_NULL(talloc_new(NULL));
1523 char *last_backslash;
1529 subpath = PANIC_IF_NULL(talloc_strdup(ctx, full_path));
1530 path = PANIC_IF_NULL(talloc_strdup(ctx, full_path));
1531 len = talloc_get_size(path) - 1;
1533 last_backslash = strrchr_m(path, '\\');
1534 if (last_backslash == NULL) {
1538 *last_backslash = 0;
1541 p = strtok_r(path, "\\", &state);
1544 strlcat(subpath, p, len);
1545 status = cli_chkpath(cli, subpath);
1546 if (!NT_STATUS_IS_OK(status)) {
1547 status = cli_mkdir(cli, subpath);
1548 if (!NT_STATUS_IS_OK(status)) {
1549 DBG(0, ("Can't mkdir %s: %s\n", subpath, nt_errstr(status)));
1553 DBG(3, ("mkdir %s\n", subpath));
1556 strlcat(subpath, "\\", len);
1557 p = strtok_r(NULL, "/\\", &state);
1567 * tar_reset_mem_context - reset talloc context associated with @t
1569 * At the start of the program the context is NULL so a new one is
1570 * allocated. On the following runs (interactive session only), simply
1571 * free the children.
1573 static TALLOC_CTX *tar_reset_mem_context(struct tar *t)
1575 tar_free_mem_context(t);
1576 t->talloc_ctx = PANIC_IF_NULL(talloc_new(NULL));
1577 return t->talloc_ctx;
1581 * tar_free_mem_context - free talloc context associated with @t
1583 static void tar_free_mem_context(struct tar *t)
1585 if (t->talloc_ctx) {
1586 talloc_free(t->talloc_ctx);
1587 t->talloc_ctx = NULL;
1588 t->path_list_size = 0;
1589 t->path_list = NULL;
1594 #define XSET(v) [v] = #v
1595 #define XTABLE(v, t) DBG(2, ("DUMP:%-20.20s = %s\n", #v, t[v]))
1596 #define XBOOL(v) DBG(2, ("DUMP:%-20.20s = %d\n", #v, v ? 1 : 0))
1597 #define XSTR(v) DBG(2, ("DUMP:%-20.20s = %s\n", #v, v ? v : "NULL"))
1598 #define XINT(v) DBG(2, ("DUMP:%-20.20s = %d\n", #v, v))
1599 #define XUINT64(v) DBG(2, ("DUMP:%-20.20s = %" PRIu64 "\n", #v, v))
1602 * tar_dump - dump tar structure on stdout
1604 static void tar_dump(struct tar *t)
1607 const char* op[] = {
1608 XSET(TAR_NO_OPERATION),
1613 const char* sel[] = {
1614 XSET(TAR_NO_SELECTION),
1619 XBOOL(t->to_process);
1620 XTABLE(t->mode.operation, op);
1621 XTABLE(t->mode.selection, sel);
1622 XINT(t->mode.blocksize);
1623 XBOOL(t->mode.hidden);
1624 XBOOL(t->mode.system);
1625 XBOOL(t->mode.incremental);
1626 XBOOL(t->mode.reset);
1628 XBOOL(t->mode.verbose);
1629 XUINT64(t->total_size);
1631 XINT(t->path_list_size);
1633 for (i = 0; t->path_list && t->path_list[i]; i++) {
1634 DBG(2, ("DUMP: t->path_list[%2d] = %s\n", i, t->path_list[i]));
1637 DBG(2, ("DUMP:t->path_list @ %p (%d elem)\n", t->path_list, i));
1646 * max_token - return upper limit for the number of token in @str
1648 * The result is not exact, the actual number of token might be less
1649 * than what is returned.
1651 static int max_token (const char *str)
1661 while (s[0] != '\0') {
1662 if (isspace((int)s[0])) {
1674 * fix_unix_path - convert @path to a DOS path
1675 * @path: path to convert
1676 * @removeprefix: if true, remove leading ./ or /.
1678 static char *fix_unix_path (char *path, bool do_remove_prefix)
1680 char *from = path, *to = path;
1682 if (path == NULL || path[0] == '\0') {
1690 if (do_remove_prefix) {
1692 if (path[0] == '/' || path[0] == '\\') {
1697 if (path[1] != '\0' && path[0] == '.' && (path[1] == '/' || path[1] == '\\')) {
1702 /* replace / with \ */
1703 while (from[0] != '\0') {
1704 if (from[0] == '/') {
1719 * path_base_name - return @path basename
1721 * If @path doesn't contain any directory separator return NULL.
1723 static char *path_base_name (const char *path)
1725 TALLOC_CTX *ctx = PANIC_IF_NULL(talloc_tos());
1730 for (i = 0; path[i]; i++) {
1731 if (path[i] == '\\' || path[i] == '/') {
1737 base = PANIC_IF_NULL(talloc_strdup(ctx, path));
1746 #define NOT_IMPLEMENTED DEBUG(0, ("tar mode not compiled. build with --with-libarchive\n"))
1754 int cmd_tarmode(void)
1760 int cmd_setmode(void)
1772 int tar_process(struct tar* tar)
1778 int tar_parse_args(struct tar *tar, const char *flag, const char **val, int valsize)
1784 bool tar_to_process(struct tar *tar)
1790 struct tar *tar_get_ctx()