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 stringify __LINE__ (int) */
85 #define STR2(x) STR1(x)
88 * Number of byte in a block unit.
90 #define TAR_BLOCK_UNIT 512
93 * Default tar block size in TAR_BLOCK_UNIT.
95 #define TAR_DEFAULT_BLOCK_SIZE 20
98 * Maximum value for the blocksize field
100 #define TAR_MAX_BLOCK_SIZE 0xffff
103 * Size of the buffer used when downloading a file
105 #define TAR_CLI_READ_SIZE 0xff00
107 #define TAR_DO_LIST_ATTR (FILE_ATTRIBUTE_DIRECTORY \
108 | FILE_ATTRIBUTE_SYSTEM \
109 | FILE_ATTRIBUTE_HIDDEN)
114 TAR_CREATE, /* c flag */
115 TAR_EXTRACT, /* x flag */
120 TAR_INCLUDE, /* I and F flag, default */
121 TAR_EXCLUDE, /* X flag */
125 TALLOC_CTX *talloc_ctx;
127 /* in state that needs/can be processed? */
132 enum tar_operation operation; /* create, extract */
133 enum tar_selection selection; /* include, exclude */
134 int blocksize; /* size in TAR_BLOCK_UNIT of a tar file block */
135 bool hidden; /* backup hidden file? */
136 bool system; /* backup system file? */
137 bool incremental; /* backup _only_ archived file? */
138 bool reset; /* unset archive bit? */
139 bool dry; /* don't write tar file? */
140 bool regex; /* XXX: never actually using regex... */
141 bool verbose; /* XXX: ignored */
144 /* nb of bytes received */
147 /* path to tar archive name */
150 /* list of path to include or exclude */
155 struct archive *archive;
163 * Global context imported in client.c when needed.
167 struct tar tar_ctx = {
168 .mode.selection = TAR_INCLUDE,
169 .mode.blocksize = TAR_DEFAULT_BLOCK_SIZE,
172 .mode.incremental = false,
176 .mode.verbose = false,
179 /* tar, local function */
180 static int tar_create(struct tar* t);
181 static int tar_create_from_list(struct tar *t);
182 static int tar_extract(struct tar *t);
183 static int tar_read_inclusion_file(struct tar *t, const char* filename);
184 static int tar_send_file(struct tar *t, struct archive_entry *entry);
185 static int tar_set_blocksize(struct tar *t, int size);
186 static int tar_set_newer_than(struct tar *t, const char *filename);
187 static NTSTATUS tar_add_selection_path(struct tar *t, const char *path);
188 static void tar_dump(struct tar *t);
189 static NTSTATUS tar_extract_skip_path(struct tar *t,
190 struct archive_entry *entry,
192 static TALLOC_CTX *tar_reset_mem_context(struct tar *t);
193 static void tar_free_mem_context(struct tar *t);
194 static NTSTATUS tar_create_skip_path(struct tar *t,
195 const char *fullpath,
196 const struct file_info *finfo,
199 static NTSTATUS tar_path_in_list(struct tar *t, const char *path,
200 bool reverse, bool *_is_in_list);
202 static int tar_get_file(struct tar *t,
203 const char *full_dos_path,
204 struct file_info *finfo);
206 static NTSTATUS get_file_callback(struct cli_state *cli,
207 struct file_info *finfo,
211 static char *fix_unix_path(char *path, bool removeprefix);
212 static NTSTATUS path_base_name(TALLOC_CTX *ctx, const char *path, char **_base);
213 static const char* skip_useless_char_in_path(const char *p);
214 static int make_remote_path(const char *full_path);
215 static int max_token (const char *str);
216 static NTSTATUS is_subpath(const char *sub, const char *full,
217 bool *_subpath_match);
219 * tar_get_ctx - retrieve global tar context handle
221 struct tar *tar_get_ctx()
227 * cmd_block - interactive command to change tar blocksize
229 * Read a size from the client command line and update the current
234 /* XXX: from client.c */
235 const extern char *cmd_ptr;
239 TALLOC_CTX *ctx = talloc_new(NULL);
244 ok = next_token_talloc(ctx, &cmd_ptr, &buf, NULL);
246 DBG(0, ("blocksize <n>\n"));
251 ok = tar_set_blocksize(&tar_ctx, atoi(buf));
253 DBG(0, ("invalid blocksize\n"));
258 DBG(2, ("blocksize is now %d\n", tar_ctx.mode.blocksize));
266 * cmd_tarmode - interactive command to change tar behaviour
268 * Read one or more modes from the client command line and update the
271 int cmd_tarmode(void)
273 const extern char *cmd_ptr;
283 {"full", &tar_ctx.mode.incremental, false},
284 {"inc", &tar_ctx.mode.incremental, true },
285 {"reset", &tar_ctx.mode.reset, true },
286 {"noreset", &tar_ctx.mode.reset, false},
287 {"system", &tar_ctx.mode.system, true },
288 {"nosystem", &tar_ctx.mode.system, false},
289 {"hidden", &tar_ctx.mode.hidden, true },
290 {"nohidden", &tar_ctx.mode.hidden, false},
291 {"verbose", &tar_ctx.mode.verbose, false},
292 {"noverbose", &tar_ctx.mode.verbose, true },
295 ctx = talloc_new(NULL);
300 while (next_token_talloc(ctx, &cmd_ptr, &buf, NULL)) {
301 for (i = 0; i < ARRAY_SIZE(table); i++) {
302 if (strequal(table[i].cmd, buf)) {
303 *table[i].p = table[i].value;
308 if (i == ARRAY_SIZE(table))
309 d_printf("tarmode: unrecognised option %s\n", buf);
312 d_printf("tarmode is now %s, %s, %s, %s, %s\n",
313 tar_ctx.mode.incremental ? "incremental" : "full",
314 tar_ctx.mode.system ? "system" : "nosystem",
315 tar_ctx.mode.hidden ? "hidden" : "nohidden",
316 tar_ctx.mode.reset ? "reset" : "noreset",
317 tar_ctx.mode.verbose ? "verbose" : "noverbose");
324 * cmd_tar - interactive command to start a tar backup/restoration
326 * Check presence of argument, parse them and handle the request.
330 const extern char *cmd_ptr;
334 int maxtok = max_token(cmd_ptr);
339 TALLOC_CTX *ctx = talloc_new(NULL);
344 ok = next_token_talloc(ctx, &cmd_ptr, &buf, NULL);
346 d_printf("tar <c|x>[IXFbgvanN] [options] <tar file> [path list]\n");
352 val = talloc_array(ctx, const char *, maxtok);
358 while (next_token_talloc(ctx, &cmd_ptr, &buf, NULL)) {
362 rc = tar_parse_args(&tar_ctx, flag, val, i);
364 d_printf("parse_args failed\n");
369 rc = tar_process(&tar_ctx);
371 d_printf("tar_process failed\n");
383 * tar_parse_args - parse and set tar command line arguments
384 * @flag: string pointing to tar options
385 * @val: number of tar arguments
386 * @valsize: table of arguments after the flags (number of element in val)
388 * tar arguments work in a weird way. For each flag f that takes a
389 * value v, the user is supposed to type:
392 * -Tf1f2f3 v1 v2 v3 TARFILE PATHS...
394 * in the interactive session:
395 * tar f1f2f3 v1 v2 v3 TARFILE PATHS...
397 * @flag has only flags (eg. "f1f2f3") and @val has the arguments
398 * (values) following them (eg. ["v1", "v2", "v3", "TARFILE", "PATH1",
401 * There are only 2 flags that take an arg: b and N. The other flags
402 * just change the semantic of PATH or TARFILE.
404 * PATH can be a list of included/excluded paths, the path to a file
405 * containing a list of included/excluded paths to use (F flag). If no
406 * PATH is provided, the whole share is used (/).
408 int tar_parse_args(struct tar* t,
414 bool do_read_list = false;
415 /* index of next value to use */
420 DBG_WARNING("Invalid tar context\n");
424 ctx = tar_reset_mem_context(t);
429 * Reset back some options - could be from interactive version
430 * all other modes are left as they are
432 t->mode.operation = TAR_NO_OPERATION;
433 t->mode.selection = TAR_NO_SELECTION;
435 t->to_process = false;
438 while (flag[0] != '\0') {
442 if (t->mode.operation != TAR_NO_OPERATION) {
443 d_printf("Tar must be followed by only one of c or x.\n");
446 t->mode.operation = TAR_CREATE;
449 if (t->mode.operation != TAR_NO_OPERATION) {
450 d_printf("Tar must be followed by only one of c or x.\n");
453 t->mode.operation = TAR_EXTRACT;
458 if (t->mode.selection != TAR_NO_SELECTION) {
459 d_printf("Only one of I,X,F must be specified\n");
462 t->mode.selection = TAR_INCLUDE;
465 if (t->mode.selection != TAR_NO_SELECTION) {
466 d_printf("Only one of I,X,F must be specified\n");
469 t->mode.selection = TAR_EXCLUDE;
472 if (t->mode.selection != TAR_NO_SELECTION) {
473 d_printf("Only one of I,X,F must be specified\n");
476 t->mode.selection = TAR_INCLUDE;
482 if (ival >= valsize) {
483 d_printf("Option b must be followed by a blocksize\n");
487 if (tar_set_blocksize(t, atoi(val[ival]))) {
488 d_printf("Option b must be followed by a valid blocksize\n");
495 /* incremental mode */
497 t->mode.incremental = true;
502 if (ival >= valsize) {
503 d_printf("Option N must be followed by valid file name\n");
507 if (tar_set_newer_than(t, val[ival])) {
508 d_printf("Error setting newer-than time\n");
517 t->mode.reset = true;
522 t->mode.verbose = true;
527 t->mode.regex = true;
532 if (t->mode.operation != TAR_CREATE) {
533 d_printf("n is only meaningful when creating a tar-file\n");
538 d_printf("dry_run set\n");
542 d_printf("Unknown tar option\n");
549 /* no selection given? default selection is include */
550 if (t->mode.selection == TAR_NO_SELECTION) {
551 t->mode.selection = TAR_INCLUDE;
554 if (valsize - ival < 1) {
555 d_printf("No tar file given.\n");
560 t->tar_path = talloc_strdup(ctx, val[ival]);
561 if (t->tar_path == NULL) {
567 * Make sure that dbf points to stderr if we are using stdout for
570 if (t->mode.operation == TAR_CREATE && strequal(t->tar_path, "-")) {
571 setup_logging("smbclient", DEBUG_STDERR);
574 /* handle PATHs... */
576 /* flag F -> read file list */
578 if (valsize - ival != 1) {
579 d_printf("Option F must be followed by exactly one filename.\n");
583 rc = tar_read_inclusion_file(t, val[ival]);
588 /* otherwise store all the PATHs on the command line */
591 for (i = ival; i < valsize; i++) {
593 status = tar_add_selection_path(t, val[i]);
594 if (!NT_STATUS_IS_OK(status)) {
600 t->to_process = true;
606 * tar_process - start processing archive
608 * The talloc context of the fields is freed at the end of the call.
610 int tar_process(struct tar *t)
615 DBG_WARNING("Invalid tar context\n");
619 switch(t->mode.operation) {
627 DBG_WARNING("Invalid tar state\n");
631 t->to_process = false;
632 tar_free_mem_context(t);
633 DBG(5, ("tar_process done, err = %d\n", rc));
638 * tar_create - create archive and fetch files
640 static int tar_create(struct tar* t)
646 struct timespec tp_start, tp_end;
647 TALLOC_CTX *ctx = talloc_new(NULL);
652 clock_gettime_mono(&tp_start);
656 t->archive = archive_write_new();
659 const int bsize = t->mode.blocksize * TAR_BLOCK_UNIT;
660 r = archive_write_set_bytes_per_block(t->archive, bsize);
661 if (r != ARCHIVE_OK) {
662 d_printf("Can't use a block size of %d bytes", bsize);
668 * Use PAX restricted format which is not the most
669 * conservative choice but has useful extensions and is widely
672 r = archive_write_set_format_pax_restricted(t->archive);
673 if (r != ARCHIVE_OK) {
674 d_printf("Can't use pax restricted format: %s\n",
675 archive_error_string(t->archive));
680 if (strequal(t->tar_path, "-")) {
681 r = archive_write_open_fd(t->archive, STDOUT_FILENO);
683 r = archive_write_open_filename(t->archive, t->tar_path);
686 if (r != ARCHIVE_OK) {
687 d_printf("Can't open %s: %s\n", t->tar_path,
688 archive_error_string(t->archive));
695 * In inclusion mode, iterate on the inclusion list
697 if (t->mode.selection == TAR_INCLUDE && t->path_list_size > 0) {
698 if (tar_create_from_list(t)) {
703 mask = talloc_asprintf(ctx, "%s\\*", client_get_cur_dir());
708 mask = client_clean_name(ctx, mask);
713 DBG(5, ("tar_process do_list with mask: %s\n", mask));
714 status = do_list(mask, TAR_DO_LIST_ATTR, get_file_callback, false, true);
715 if (!NT_STATUS_IS_OK(status)) {
716 DBG(0, ("do_list fail %s\n", nt_errstr(status)));
722 clock_gettime_mono(&tp_end);
723 d_printf("tar: dumped %"PRIu64" files and %"PRIu64" directories\n",
724 t->numfile, t->numdir);
725 d_printf("Total bytes written: %"PRIu64" (%.1f MiB/s)\n",
727 t->total_size/timespec_elapsed2(&tp_start, &tp_end)/1024/1024);
732 r = archive_write_close(t->archive);
733 if (r != ARCHIVE_OK) {
734 d_printf("Fatal: %s\n", archive_error_string(t->archive));
740 #ifdef HAVE_ARCHIVE_READ_FREE
741 archive_write_free(t->archive);
743 archive_write_finish(t->archive);
750 * tar_create_from_list - fetch from path list in include mode
752 static int tar_create_from_list(struct tar *t)
757 const char *path, *mask, *start_dir;
759 TALLOC_CTX *ctx = talloc_new(NULL);
764 start_dir = talloc_strdup(ctx, client_get_cur_dir());
765 if (start_dir == NULL) {
770 for (i = 0; i < t->path_list_size; i++) {
771 path = t->path_list[i];
773 status = path_base_name(ctx, path, &base);
774 if (!NT_STATUS_IS_OK(status)) {
778 mask = talloc_asprintf(ctx, "%s\\%s",
779 client_get_cur_dir(), path);
784 mask = client_clean_name(ctx, mask);
790 DBG(5, ("incl. path='%s', base='%s', mask='%s'\n",
791 path, base ? base : "NULL", mask));
794 base = talloc_asprintf(ctx, "%s%s\\",
795 client_get_cur_dir(), base);
800 base = client_clean_name(ctx, base);
806 DBG(5, ("cd '%s' before do_list\n", base));
807 client_set_cur_dir(base);
809 status = do_list(mask, TAR_DO_LIST_ATTR, get_file_callback, false, true);
811 client_set_cur_dir(start_dir);
813 if (!NT_STATUS_IS_OK(status)) {
814 DBG(0, ("do_list failed on %s (%s)\n", path, nt_errstr(status)));
826 * get_file_callback - do_list callback
828 * Callback for client.c do_list(). Called for each file found on the
829 * share matching do_list mask. Recursively call do_list() with itself
830 * as callback when the current file is a directory.
832 static NTSTATUS get_file_callback(struct cli_state *cli,
833 struct file_info *finfo,
836 NTSTATUS status = NT_STATUS_OK;
838 char *old_dir = NULL;
839 char *new_dir = NULL;
840 const char *initial_dir = client_get_cur_dir();
844 TALLOC_CTX *ctx = talloc_new(NULL);
846 return NT_STATUS_NO_MEMORY;
849 remote_name = talloc_asprintf(ctx, "%s%s", initial_dir, finfo->name);
850 if (remote_name == NULL) {
851 status = NT_STATUS_NO_MEMORY;
854 remote_name = client_clean_name(ctx, remote_name);
855 if (remote_name == NULL) {
856 status = NT_STATUS_NO_MEMORY;
860 if (strequal(finfo->name, "..") || strequal(finfo->name, ".")) {
864 isdir = finfo->attr & FILE_ATTRIBUTE_DIRECTORY;
866 old_dir = talloc_strdup(ctx, initial_dir);
867 new_dir = talloc_asprintf(ctx, "%s\\", remote_name);
868 if ((old_dir == NULL) || (new_dir == NULL)) {
869 status = NT_STATUS_NO_MEMORY;
874 status = tar_create_skip_path(&tar_ctx,
875 isdir ? new_dir : remote_name,
877 if (!NT_STATUS_IS_OK(status)) {
882 DBG(5, ("--- %s\n", remote_name));
883 status = NT_STATUS_OK;
889 mask = talloc_asprintf(ctx, "%s*", new_dir);
891 status = NT_STATUS_NO_MEMORY;
894 mask = client_clean_name(ctx, mask);
896 status = NT_STATUS_NO_MEMORY;
900 rc = tar_get_file(&tar_ctx, remote_name, finfo);
902 status = NT_STATUS_UNSUCCESSFUL;
906 client_set_cur_dir(new_dir);
907 do_list(mask, TAR_DO_LIST_ATTR, get_file_callback, false, true);
908 client_set_cur_dir(old_dir);
911 rc = tar_get_file(&tar_ctx, remote_name, finfo);
913 status = NT_STATUS_UNSUCCESSFUL;
924 * tar_get_file - fetch a remote file to the local archive
925 * @full_dos_path: path to the file to fetch
926 * @finfo: attributes of the file to fetch
928 static int tar_get_file(struct tar *t,
929 const char *full_dos_path,
930 struct file_info *finfo)
932 extern struct cli_state *cli;
934 struct archive_entry *entry;
935 char *full_unix_path;
936 char buf[TAR_CLI_READ_SIZE];
939 uint16_t remote_fd = (uint16_t)-1;
941 const bool isdir = finfo->attr & FILE_ATTRIBUTE_DIRECTORY;
942 TALLOC_CTX *ctx = talloc_new(NULL);
948 DBG(5, ("+++ %s\n", full_dos_path));
950 t->total_size += finfo->size;
957 /* ignore return value: server might not store DOS attributes */
958 set_remote_attr(full_dos_path, FILE_ATTRIBUTE_ARCHIVE, ATTR_UNSET);
961 full_unix_path = talloc_asprintf(ctx, ".%s", full_dos_path);
962 if (full_unix_path == NULL) {
966 string_replace(full_unix_path, '\\', '/');
967 entry = archive_entry_new();
968 archive_entry_copy_pathname(entry, full_unix_path);
969 archive_entry_set_filetype(entry, isdir ? AE_IFDIR : AE_IFREG);
970 archive_entry_set_atime(entry,
971 finfo->atime_ts.tv_sec,
972 finfo->atime_ts.tv_nsec);
973 archive_entry_set_mtime(entry,
974 finfo->mtime_ts.tv_sec,
975 finfo->mtime_ts.tv_nsec);
976 archive_entry_set_ctime(entry,
977 finfo->ctime_ts.tv_sec,
978 finfo->ctime_ts.tv_nsec);
979 archive_entry_set_perm(entry, isdir ? 0755 : 0644);
981 * check if we can safely cast unsigned file size to libarchive
982 * signed size. Very unlikely problem (>9 exabyte file)
984 if (finfo->size > INT64_MAX) {
985 d_printf("Remote file %s too big\n", full_dos_path);
989 archive_entry_set_size(entry, (int64_t)finfo->size);
992 /* It's a directory just write a header */
993 r = archive_write_header(t->archive, entry);
994 if (r != ARCHIVE_OK) {
995 d_printf("Fatal: %s\n", archive_error_string(t->archive));
998 if (t->mode.verbose) {
999 d_printf("a %s\\\n", full_dos_path);
1001 DBG(5, ("get_file skip dir %s\n", full_dos_path));
1005 status = cli_open(cli, full_dos_path, O_RDONLY, DENY_NONE, &remote_fd);
1006 if (!NT_STATUS_IS_OK(status)) {
1007 d_printf("%s opening remote file %s\n",
1008 nt_errstr(status), full_dos_path);
1012 /* don't make tar file entry until after the file is open */
1013 r = archive_write_header(t->archive, entry);
1014 if (r != ARCHIVE_OK) {
1015 d_printf("Fatal: %s\n", archive_error_string(t->archive));
1020 if (t->mode.verbose) {
1021 d_printf("a %s\n", full_dos_path);
1025 status = cli_read(cli, remote_fd, buf, off, sizeof(buf), &len);
1026 if (!NT_STATUS_IS_OK(status)) {
1027 d_printf("Error reading file %s : %s\n",
1028 full_dos_path, nt_errstr(status));
1035 r = archive_write_data(t->archive, buf, len);
1037 d_printf("Fatal: %s\n", archive_error_string(t->archive));
1042 } while (off < finfo->size);
1046 cli_close(cli, remote_fd);
1049 archive_entry_free(entry);
1057 * tar_extract - open archive and send files.
1059 static int tar_extract(struct tar *t)
1063 struct archive_entry *entry;
1064 const size_t bsize = t->mode.blocksize * TAR_BLOCK_UNIT;
1067 t->archive = archive_read_new();
1068 archive_read_support_format_all(t->archive);
1069 #ifdef HAVE_ARCHIVE_READ_SUPPORT_FILTER_ALL
1070 archive_read_support_filter_all(t->archive);
1073 if (strequal(t->tar_path, "-")) {
1074 r = archive_read_open_fd(t->archive, STDIN_FILENO, bsize);
1076 r = archive_read_open_filename(t->archive, t->tar_path, bsize);
1079 if (r != ARCHIVE_OK) {
1080 d_printf("Can't open %s : %s\n", t->tar_path,
1081 archive_error_string(t->archive));
1089 r = archive_read_next_header(t->archive, &entry);
1090 if (r == ARCHIVE_EOF) {
1093 if (r == ARCHIVE_WARN) {
1094 d_printf("Warning: %s\n", archive_error_string(t->archive));
1096 if (r == ARCHIVE_FATAL) {
1097 d_printf("Fatal: %s\n", archive_error_string(t->archive));
1102 status = tar_extract_skip_path(t, entry, &skip);
1103 if (!NT_STATUS_IS_OK(status)) {
1108 DBG(5, ("--- %s\n", archive_entry_pathname(entry)));
1111 DBG(5, ("+++ %s\n", archive_entry_pathname(entry)));
1113 if (t->mode.verbose) {
1114 d_printf("x %s\n", archive_entry_pathname(entry));
1117 rc = tar_send_file(t, entry);
1125 #ifdef HAVE_ARCHIVE_READ_FREE
1126 r = archive_read_free(t->archive);
1128 r = archive_read_finish(t->archive);
1130 if (r != ARCHIVE_OK) {
1131 d_printf("Can't close %s : %s\n", t->tar_path,
1132 archive_error_string(t->archive));
1139 * tar_send_file - send @entry to the remote server
1140 * @entry: current archive entry
1142 * Handle the creation of the parent directories and transfer the
1143 * entry to a new remote file.
1145 static int tar_send_file(struct tar *t, struct archive_entry *entry)
1147 extern struct cli_state *cli;
1151 uint16_t remote_fd = (uint16_t) -1;
1153 int flags = O_RDWR | O_CREAT | O_TRUNC;
1154 mode_t mode = archive_entry_filetype(entry);
1156 TALLOC_CTX *ctx = talloc_new(NULL);
1161 dos_path = talloc_strdup(ctx, archive_entry_pathname(entry));
1162 if (dos_path == NULL) {
1166 fix_unix_path(dos_path, true);
1168 full_path = talloc_strdup(ctx, client_get_cur_dir());
1169 if (full_path == NULL) {
1173 full_path = talloc_strdup_append(full_path, dos_path);
1174 if (full_path == NULL) {
1178 full_path = client_clean_name(ctx, full_path);
1179 if (full_path == NULL) {
1184 if (mode != AE_IFREG && mode != AE_IFDIR) {
1185 d_printf("Skipping non-dir & non-regular file %s\n", full_path);
1189 rc = make_remote_path(full_path);
1195 if (mode == AE_IFDIR) {
1199 status = cli_open(cli, full_path, flags, DENY_NONE, &remote_fd);
1200 if (!NT_STATUS_IS_OK(status)) {
1201 d_printf("Error opening remote file %s: %s\n",
1202 full_path, nt_errstr(status));
1213 r = archive_read_data_block(t->archive, &buf, &len, &off);
1214 if (r == ARCHIVE_EOF) {
1217 if (r == ARCHIVE_WARN) {
1218 d_printf("Warning: %s\n", archive_error_string(t->archive));
1220 if (r == ARCHIVE_FATAL) {
1221 d_printf("Fatal: %s\n", archive_error_string(t->archive));
1226 status = cli_writeall(cli, remote_fd, 0, buf, off, len, NULL);
1227 if (!NT_STATUS_IS_OK(status)) {
1228 d_printf("Error writing remote file %s: %s\n",
1229 full_path, nt_errstr(status));
1236 status = cli_close(cli, remote_fd);
1237 if (!NT_STATUS_IS_OK(status)) {
1238 d_printf("Error closing remote file %s: %s\n",
1239 full_path, nt_errstr(status));
1249 * tar_add_selection_path - add a path to the path list
1250 * @path: path to add
1252 static NTSTATUS tar_add_selection_path(struct tar *t, const char *path)
1255 TALLOC_CTX *ctx = t->talloc_ctx;
1256 if (!t->path_list) {
1257 t->path_list = str_list_make_empty(ctx);
1258 if (t->path_list == NULL) {
1259 return NT_STATUS_NO_MEMORY;
1261 t->path_list_size = 0;
1264 /* cast to silence gcc const-qual warning */
1265 list = str_list_add((void *)t->path_list, path);
1267 return NT_STATUS_NO_MEMORY;
1269 t->path_list = discard_const_p(char *, list);
1270 t->path_list_size++;
1271 fix_unix_path(t->path_list[t->path_list_size - 1], true);
1273 return NT_STATUS_OK;
1277 * tar_set_blocksize - set block size in TAR_BLOCK_UNIT
1279 static int tar_set_blocksize(struct tar *t, int size)
1281 if (size <= 0 || size > TAR_MAX_BLOCK_SIZE) {
1285 t->mode.blocksize = size;
1291 * tar_set_newer_than - set date threshold of saved files
1292 * @filename: local path to a file
1294 * Only files newer than the modification time of @filename will be
1297 * Note: this function set the global variable newer_than from
1298 * client.c. Thus the time is not a field of the tar structure. See
1299 * cmd_newer() to change its value from an interactive session.
1301 static int tar_set_newer_than(struct tar *t, const char *filename)
1303 extern time_t newer_than;
1304 SMB_STRUCT_STAT stbuf;
1307 rc = sys_stat(filename, &stbuf, false);
1309 d_printf("Error setting newer-than time\n");
1313 newer_than = convert_timespec_to_time_t(stbuf.st_ex_mtime);
1314 DBG(1, ("Getting files newer than %s\n", time_to_asc(newer_than)));
1319 * tar_read_inclusion_file - set path list from file
1320 * @filename: path to the list file
1322 * Read and add each line of @filename to the path list.
1324 static int tar_read_inclusion_file(struct tar *t, const char* filename)
1329 TALLOC_CTX *ctx = talloc_new(NULL);
1334 fd = open(filename, O_RDONLY);
1336 d_printf("Can't open inclusion file '%s': %s\n", filename, strerror(errno));
1341 for (line = afdgets(fd, ctx, 0);
1343 line = afdgets(fd, ctx, 0)) {
1345 status = tar_add_selection_path(t, line);
1346 if (!NT_STATUS_IS_OK(status)) {
1361 * tar_path_in_list - check whether @path is in the path list
1362 * @path: path to find
1363 * @reverse: when true also try to find path list element in @path
1364 * @_is_in_list: set if @path is in the path list
1366 * Look at each path of the path list and set @_is_in_list if @path is a
1367 * subpath of one of them.
1369 * If you want /path to be in the path list (path/a/, path/b/) set
1370 * @reverse to true to try to match the other way around.
1372 static NTSTATUS tar_path_in_list(struct tar *t, const char *path,
1373 bool reverse, bool *_is_in_list)
1377 const char *pattern;
1379 if (path == NULL || path[0] == '\0') {
1380 *_is_in_list = false;
1381 return NT_STATUS_OK;
1384 p = skip_useless_char_in_path(path);
1386 for (i = 0; i < t->path_list_size; i++) {
1390 pattern = skip_useless_char_in_path(t->path_list[i]);
1391 status = is_subpath(p, pattern, &is_in_list);
1392 if (!NT_STATUS_IS_OK(status)) {
1395 if (reverse && !is_in_list) {
1396 status = is_subpath(pattern, p, &is_in_list);
1397 if (!NT_STATUS_IS_OK(status)) {
1402 *_is_in_list = true;
1403 return NT_STATUS_OK;
1407 *_is_in_list = false;
1408 return NT_STATUS_OK;
1412 * tar_extract_skip_path - check if @entry should be skipped
1413 * @entry: current tar entry
1414 * @_skip: set true if path should be skipped, otherwise false
1416 * Skip predicate for tar extraction (archive to server) only.
1418 static NTSTATUS tar_extract_skip_path(struct tar *t,
1419 struct archive_entry *entry,
1422 const char *fullpath = archive_entry_pathname(entry);
1425 if (t->path_list_size <= 0) {
1427 return NT_STATUS_OK;
1430 if (t->mode.regex) {
1431 in = mask_match_list(fullpath, t->path_list, t->path_list_size, true);
1433 NTSTATUS status = tar_path_in_list(t, fullpath, false, &in);
1434 if (!NT_STATUS_IS_OK(status)) {
1439 if (t->mode.selection == TAR_EXCLUDE) {
1445 return NT_STATUS_OK;
1449 * tar_create_skip_path - check if @fullpath shoud be skipped
1450 * @fullpath: full remote path of the current file
1451 * @finfo: remote file attributes
1452 * @_skip: returned skip not
1454 * Skip predicate for tar creation (server to archive) only.
1456 static NTSTATUS tar_create_skip_path(struct tar *t,
1457 const char *fullpath,
1458 const struct file_info *finfo,
1461 /* syntaxic sugar */
1462 const mode_t mode = finfo->attr;
1463 const bool isdir = mode & FILE_ATTRIBUTE_DIRECTORY;
1464 const bool exclude = t->mode.selection == TAR_EXCLUDE;
1469 /* 1. if we don't want X and we have X, skip */
1470 if (!t->mode.system && (mode & FILE_ATTRIBUTE_SYSTEM)) {
1472 return NT_STATUS_OK;
1475 if (!t->mode.hidden && (mode & FILE_ATTRIBUTE_HIDDEN)) {
1477 return NT_STATUS_OK;
1480 /* 2. if we only want archive and it's not, skip */
1482 if (t->mode.incremental && !(mode & FILE_ATTRIBUTE_ARCHIVE)) {
1484 return NT_STATUS_OK;
1488 /* 3. is it in the selection list? */
1491 * tar_create_from_list() use the include list as a starting
1492 * point, no need to check
1496 return NT_STATUS_OK;
1499 /* we are now in exclude mode */
1501 /* no matter the selection, no list => include everything */
1502 if (t->path_list_size <= 0) {
1504 return NT_STATUS_OK;
1507 if (t->mode.regex) {
1508 in = mask_match_list(fullpath, t->path_list, t->path_list_size, true);
1510 bool reverse = isdir && !exclude;
1511 NTSTATUS status = tar_path_in_list(t, fullpath, reverse, &in);
1512 if (!NT_STATUS_IS_OK(status)) {
1518 return NT_STATUS_OK;
1522 * tar_to_process - return true if @t is ready to be processed
1524 * @t is ready if it properly parsed command line arguments.
1526 bool tar_to_process(struct tar *t)
1529 DBG_WARNING("Invalid tar context\n");
1532 return t->to_process;
1536 * skip_useless_char_in_path - skip leading slashes/dots
1538 * Skip leading slashes, backslashes and dot-slashes.
1540 static const char* skip_useless_char_in_path(const char *p)
1543 if (*p == '/' || *p == '\\') {
1546 else if (p[0] == '.' && (p[1] == '/' || p[1] == '\\')) {
1556 * is_subpath - check if the path @sub is a subpath of @full.
1557 * @sub: path to test
1558 * @full: container path
1559 * @_subpath_match: set true if @sub is a subpath of @full, otherwise false
1561 * String comparaison is case-insensitive.
1563 static NTSTATUS is_subpath(const char *sub, const char *full,
1564 bool *_subpath_match)
1566 NTSTATUS status = NT_STATUS_OK;
1569 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
1570 if (tmp_ctx == NULL) {
1571 status = NT_STATUS_NO_MEMORY;
1575 f = strlower_talloc(tmp_ctx, full);
1577 status = NT_STATUS_NO_MEMORY;
1580 string_replace(f, '\\', '/');
1581 s = strlower_talloc(tmp_ctx, sub);
1583 status = NT_STATUS_NO_MEMORY;
1586 string_replace(s, '\\', '/');
1588 /* find the point where sub and full diverge */
1589 while ((*f != '\0') && (*s != '\0') && (*f == *s)) {
1595 if ((*f == '\0') && (*s == '\0')) {
1596 *_subpath_match = true; /* sub and full match */
1600 if ((*f == '\0') && (len > 0) && (*(f - 1) == '/')) {
1601 /* sub diverges from full at path separator */
1602 *_subpath_match = true;
1606 if ((*s == '\0') && (strcmp(f, "/") == 0)) {
1607 /* full diverges from sub with trailing slash only */
1608 *_subpath_match = true;
1612 if ((*s == '/') && (*f == '\0')) {
1613 /* sub diverges from full with extra path component */
1614 *_subpath_match = true;
1617 *_subpath_match = false;
1620 talloc_free(tmp_ctx);
1627 * make_remote_path - recursively make remote dirs
1628 * @full_path: full hierarchy to create
1630 * Create @full_path and each parent directories as needed.
1632 static int make_remote_path(const char *full_path)
1634 extern struct cli_state *cli;
1638 char *last_backslash;
1643 TALLOC_CTX *ctx = talloc_new(NULL);
1648 subpath = talloc_strdup(ctx, full_path);
1649 if (subpath == NULL) {
1653 path = talloc_strdup(ctx, full_path);
1658 len = talloc_get_size(path) - 1;
1660 last_backslash = strrchr_m(path, '\\');
1661 if (last_backslash == NULL) {
1665 *last_backslash = 0;
1668 p = strtok_r(path, "\\", &state);
1671 strlcat(subpath, p, len);
1672 status = cli_chkpath(cli, subpath);
1673 if (!NT_STATUS_IS_OK(status)) {
1674 status = cli_mkdir(cli, subpath);
1675 if (!NT_STATUS_IS_OK(status)) {
1676 d_printf("Can't mkdir %s: %s\n", subpath, nt_errstr(status));
1680 DBG(3, ("mkdir %s\n", subpath));
1683 strlcat(subpath, "\\", len);
1684 p = strtok_r(NULL, "/\\", &state);
1694 * tar_reset_mem_context - reset talloc context associated with @t
1696 * At the start of the program the context is NULL so a new one is
1697 * allocated. On the following runs (interactive session only), simply
1698 * free the children.
1700 static TALLOC_CTX *tar_reset_mem_context(struct tar *t)
1702 tar_free_mem_context(t);
1703 t->talloc_ctx = talloc_new(NULL);
1704 return t->talloc_ctx;
1708 * tar_free_mem_context - free talloc context associated with @t
1710 static void tar_free_mem_context(struct tar *t)
1712 if (t->talloc_ctx) {
1713 talloc_free(t->talloc_ctx);
1714 t->talloc_ctx = NULL;
1715 t->path_list_size = 0;
1716 t->path_list = NULL;
1721 #define XSET(v) [v] = #v
1722 #define XTABLE(v, t) DBG(2, ("DUMP:%-20.20s = %s\n", #v, t[v]))
1723 #define XBOOL(v) DBG(2, ("DUMP:%-20.20s = %d\n", #v, v ? 1 : 0))
1724 #define XSTR(v) DBG(2, ("DUMP:%-20.20s = %s\n", #v, v ? v : "NULL"))
1725 #define XINT(v) DBG(2, ("DUMP:%-20.20s = %d\n", #v, v))
1726 #define XUINT64(v) DBG(2, ("DUMP:%-20.20s = %" PRIu64 "\n", #v, v))
1729 * tar_dump - dump tar structure on stdout
1731 static void tar_dump(struct tar *t)
1734 const char* op[] = {
1735 XSET(TAR_NO_OPERATION),
1740 const char* sel[] = {
1741 XSET(TAR_NO_SELECTION),
1746 XBOOL(t->to_process);
1747 XTABLE(t->mode.operation, op);
1748 XTABLE(t->mode.selection, sel);
1749 XINT(t->mode.blocksize);
1750 XBOOL(t->mode.hidden);
1751 XBOOL(t->mode.system);
1752 XBOOL(t->mode.incremental);
1753 XBOOL(t->mode.reset);
1755 XBOOL(t->mode.verbose);
1756 XUINT64(t->total_size);
1758 XINT(t->path_list_size);
1760 for (i = 0; t->path_list && t->path_list[i]; i++) {
1761 DBG(2, ("DUMP: t->path_list[%2d] = %s\n", i, t->path_list[i]));
1764 DBG(2, ("DUMP:t->path_list @ %p (%d elem)\n", t->path_list, i));
1773 * max_token - return upper limit for the number of token in @str
1775 * The result is not exact, the actual number of token might be less
1776 * than what is returned.
1778 static int max_token(const char *str)
1788 while (s[0] != '\0') {
1789 if (isspace((int)s[0])) {
1801 * fix_unix_path - convert @path to a DOS path
1802 * @path: path to convert
1803 * @removeprefix: if true, remove leading ./ or /.
1805 static char *fix_unix_path(char *path, bool do_remove_prefix)
1807 char *from = path, *to = path;
1809 if (path == NULL || path[0] == '\0') {
1817 if (do_remove_prefix) {
1819 if (path[0] == '/' || path[0] == '\\') {
1824 if (path[1] != '\0' && path[0] == '.' && (path[1] == '/' || path[1] == '\\')) {
1829 /* replace / with \ */
1830 while (from[0] != '\0') {
1831 if (from[0] == '/') {
1846 * path_base_name - return @path basename
1848 * If @path doesn't contain any directory separator return NULL.
1850 static NTSTATUS path_base_name(TALLOC_CTX *ctx, const char *path, char **_base)
1856 for (i = 0; path[i]; i++) {
1857 if (path[i] == '\\' || path[i] == '/') {
1863 base = talloc_strdup(ctx, path);
1865 return NT_STATUS_NO_MEMORY;
1872 return NT_STATUS_OK;
1877 #define NOT_IMPLEMENTED DEBUG(0, ("tar mode not compiled. build used --without-libarchive\n"))
1885 int cmd_tarmode(void)
1897 int tar_process(struct tar* tar)
1903 int tar_parse_args(struct tar *tar, const char *flag, const char **val, int valsize)
1909 bool tar_to_process(struct tar *tar)
1914 struct tar *tar_get_ctx()