WHATSNEW: Add release notes for Samba 4.15.0.
[Samba.git] / source3 / client / clitar.c
blobd62b53d2b17055246e1951cf35c52a9c8b8cf335
1 /*
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/>.
20 /**
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
28 * tar_get_ctx().
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
39 * the context.
41 * ## Archive creation
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
59 * tar_get_file().
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().
69 #include "includes.h"
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
77 #include <archive.h>
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) */
84 #define STR1(x) #x
85 #define STR2(x) STR1(x)
87 /**
88 * Number of byte in a block unit.
90 #define TAR_BLOCK_UNIT 512
92 /**
93 * Default tar block size in TAR_BLOCK_UNIT.
95 #define TAR_DEFAULT_BLOCK_SIZE 20
97 /**
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)
112 enum tar_operation {
113 TAR_NO_OPERATION,
114 TAR_CREATE, /* c flag */
115 TAR_EXTRACT, /* x flag */
118 enum tar_selection {
119 TAR_NO_SELECTION,
120 TAR_INCLUDE, /* I and F flag, default */
121 TAR_EXCLUDE, /* X flag */
124 struct tar {
125 TALLOC_CTX *talloc_ctx;
127 /* in state that needs/can be processed? */
128 bool to_process;
130 /* flags */
131 struct tar_mode {
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 */
142 } mode;
144 /* nb of bytes received */
145 uint64_t total_size;
147 /* path to tar archive name */
148 char *tar_path;
150 /* list of path to include or exclude */
151 char **path_list;
152 int path_list_size;
154 /* archive handle */
155 struct archive *archive;
157 /* counters */
158 uint64_t numdir;
159 uint64_t numfile;
163 * Global context imported in client.c when needed.
165 * Default options.
167 struct tar tar_ctx = {
168 .mode.selection = TAR_INCLUDE,
169 .mode.blocksize = TAR_DEFAULT_BLOCK_SIZE,
170 .mode.hidden = true,
171 .mode.system = true,
172 .mode.incremental = false,
173 .mode.reset = false,
174 .mode.dry = false,
175 .mode.regex = 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,
191 bool *_skip);
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,
197 bool *_skip);
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,
208 const char *dir);
210 /* utilities */
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()
223 return &tar_ctx;
227 * cmd_block - interactive command to change tar blocksize
229 * Read a size from the client command line and update the current
230 * blocksize.
232 int cmd_block(void)
234 /* XXX: from client.c */
235 const extern char *cmd_ptr;
236 char *buf;
237 int err = 0;
238 bool ok;
239 TALLOC_CTX *ctx = talloc_new(NULL);
240 if (ctx == NULL) {
241 return 1;
244 ok = next_token_talloc(ctx, &cmd_ptr, &buf, NULL);
245 if (!ok) {
246 DBG(0, ("blocksize <n>\n"));
247 err = 1;
248 goto out;
251 ok = tar_set_blocksize(&tar_ctx, atoi(buf));
252 if (ok) {
253 DBG(0, ("invalid blocksize\n"));
254 err = 1;
255 goto out;
258 DBG(2, ("blocksize is now %d\n", tar_ctx.mode.blocksize));
260 out:
261 talloc_free(ctx);
262 return err;
266 * cmd_tarmode - interactive command to change tar behaviour
268 * Read one or more modes from the client command line and update the
269 * current tar mode.
271 int cmd_tarmode(void)
273 const extern char *cmd_ptr;
274 char *buf;
275 TALLOC_CTX *ctx;
277 struct {
278 const char *cmd;
279 bool *p;
280 bool value;
281 } table[] = {
282 {"full", &tar_ctx.mode.incremental, false},
283 {"inc", &tar_ctx.mode.incremental, true },
284 {"reset", &tar_ctx.mode.reset, true },
285 {"noreset", &tar_ctx.mode.reset, false},
286 {"system", &tar_ctx.mode.system, true },
287 {"nosystem", &tar_ctx.mode.system, false},
288 {"hidden", &tar_ctx.mode.hidden, true },
289 {"nohidden", &tar_ctx.mode.hidden, false},
290 {"verbose", &tar_ctx.mode.verbose, false},
291 {"noverbose", &tar_ctx.mode.verbose, true },
294 ctx = talloc_new(NULL);
295 if (ctx == NULL) {
296 return 1;
299 while (next_token_talloc(ctx, &cmd_ptr, &buf, NULL)) {
300 size_t i;
301 for (i = 0; i < ARRAY_SIZE(table); i++) {
302 if (strequal(table[i].cmd, buf)) {
303 *table[i].p = table[i].value;
304 break;
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");
319 talloc_free(ctx);
320 return 0;
324 * cmd_tar - interactive command to start a tar backup/restoration
326 * Check presence of argument, parse them and handle the request.
328 int cmd_tar(void)
330 const extern char *cmd_ptr;
331 const char *flag;
332 const char **val;
333 char *buf;
334 int maxtok = max_token(cmd_ptr);
335 int i = 0;
336 int err = 0;
337 bool ok;
338 int rc;
339 TALLOC_CTX *ctx = talloc_new(NULL);
340 if (ctx == NULL) {
341 return 1;
344 ok = next_token_talloc(ctx, &cmd_ptr, &buf, NULL);
345 if (!ok) {
346 d_printf("tar <c|x>[IXFbgvanN] [options] <tar file> [path list]\n");
347 err = 1;
348 goto out;
351 flag = buf;
352 val = talloc_array(ctx, const char *, maxtok);
353 if (val == NULL) {
354 err = 1;
355 goto out;
358 while (next_token_talloc(ctx, &cmd_ptr, &buf, NULL)) {
359 val[i++] = buf;
362 rc = tar_parse_args(&tar_ctx, flag, val, i);
363 if (rc != 0) {
364 d_printf("parse_args failed\n");
365 err = 1;
366 goto out;
369 rc = tar_process(&tar_ctx);
370 if (rc != 0) {
371 d_printf("tar_process failed\n");
372 err = 1;
373 goto out;
376 out:
377 talloc_free(ctx);
378 return err;
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:
391 * on the CLI:
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",
399 * "PATH2"]).
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,
409 const char *flag,
410 const char **val,
411 int valsize)
413 TALLOC_CTX *ctx;
414 bool do_read_list = false;
415 /* index of next value to use */
416 int ival = 0;
417 int rc;
419 if (t == NULL) {
420 DBG_WARNING("Invalid tar context\n");
421 return 1;
424 ctx = tar_reset_mem_context(t);
425 if (ctx == NULL) {
426 return 1;
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;
434 t->mode.dry = false;
435 t->to_process = false;
436 t->total_size = 0;
438 while (flag[0] != '\0') {
439 switch(flag[0]) {
440 /* operation */
441 case 'c':
442 if (t->mode.operation != TAR_NO_OPERATION) {
443 d_printf("Tar must be followed by only one of c or x.\n");
444 return 1;
446 t->mode.operation = TAR_CREATE;
447 break;
448 case 'x':
449 if (t->mode.operation != TAR_NO_OPERATION) {
450 d_printf("Tar must be followed by only one of c or x.\n");
451 return 1;
453 t->mode.operation = TAR_EXTRACT;
454 break;
456 /* selection */
457 case 'I':
458 if (t->mode.selection != TAR_NO_SELECTION) {
459 d_printf("Only one of I,X,F must be specified\n");
460 return 1;
462 t->mode.selection = TAR_INCLUDE;
463 break;
464 case 'X':
465 if (t->mode.selection != TAR_NO_SELECTION) {
466 d_printf("Only one of I,X,F must be specified\n");
467 return 1;
469 t->mode.selection = TAR_EXCLUDE;
470 break;
471 case 'F':
472 if (t->mode.selection != TAR_NO_SELECTION) {
473 d_printf("Only one of I,X,F must be specified\n");
474 return 1;
476 t->mode.selection = TAR_INCLUDE;
477 do_read_list = true;
478 break;
480 /* blocksize */
481 case 'b':
482 if (ival >= valsize) {
483 d_printf("Option b must be followed by a blocksize\n");
484 return 1;
487 if (tar_set_blocksize(t, atoi(val[ival]))) {
488 d_printf("Option b must be followed by a valid blocksize\n");
489 return 1;
492 ival++;
493 break;
495 /* incremental mode */
496 case 'g':
497 t->mode.incremental = true;
498 break;
500 /* newer than */
501 case 'N':
502 if (ival >= valsize) {
503 d_printf("Option N must be followed by valid file name\n");
504 return 1;
507 if (tar_set_newer_than(t, val[ival])) {
508 d_printf("Error setting newer-than time\n");
509 return 1;
512 ival++;
513 break;
515 /* reset mode */
516 case 'a':
517 t->mode.reset = true;
518 break;
520 /* verbose */
521 case 'v':
522 t->mode.verbose = true;
523 break;
525 /* regex match */
526 case 'r':
527 t->mode.regex = true;
528 break;
530 /* dry run mode */
531 case 'n':
532 if (t->mode.operation != TAR_CREATE) {
533 d_printf("n is only meaningful when creating a tar-file\n");
534 return 1;
537 t->mode.dry = true;
538 d_printf("dry_run set\n");
539 break;
541 default:
542 d_printf("Unknown tar option\n");
543 return 1;
546 flag++;
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");
556 return 1;
559 /* handle TARFILE */
560 t->tar_path = talloc_strdup(ctx, val[ival]);
561 if (t->tar_path == NULL) {
562 return 1;
564 ival++;
567 * Make sure that dbf points to stderr if we are using stdout for
568 * tar output
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 */
577 if (do_read_list) {
578 if (valsize - ival != 1) {
579 d_printf("Option F must be followed by exactly one filename.\n");
580 return 1;
583 rc = tar_read_inclusion_file(t, val[ival]);
584 if (rc != 0) {
585 return 1;
587 ival++;
588 /* otherwise store all the PATHs on the command line */
589 } else {
590 int i;
591 for (i = ival; i < valsize; i++) {
592 NTSTATUS status;
593 status = tar_add_selection_path(t, val[i]);
594 if (!NT_STATUS_IS_OK(status)) {
595 return 1;
600 t->to_process = true;
601 tar_dump(t);
602 return 0;
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)
612 int rc = 0;
614 if (t == NULL) {
615 DBG_WARNING("Invalid tar context\n");
616 return 1;
619 switch(t->mode.operation) {
620 case TAR_EXTRACT:
621 rc = tar_extract(t);
622 break;
623 case TAR_CREATE:
624 rc = tar_create(t);
625 break;
626 default:
627 DBG_WARNING("Invalid tar state\n");
628 rc = 1;
631 t->to_process = false;
632 tar_free_mem_context(t);
633 DBG(5, ("tar_process done, err = %d\n", rc));
634 return rc;
638 * tar_create - create archive and fetch files
640 static int tar_create(struct tar* t)
642 int r;
643 int err = 0;
644 NTSTATUS status;
645 const char *mask;
646 struct timespec tp_start, tp_end;
647 TALLOC_CTX *ctx = talloc_new(NULL);
648 if (ctx == NULL) {
649 return 1;
652 clock_gettime_mono(&tp_start);
654 t->numfile = 0;
655 t->numdir = 0;
656 t->archive = archive_write_new();
658 if (!t->mode.dry) {
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);
663 err = 1;
664 goto out;
668 * Use PAX restricted format which is not the most
669 * conservative choice but has useful extensions and is widely
670 * supported
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));
676 err = 1;
677 goto out;
680 if (strequal(t->tar_path, "-")) {
681 r = archive_write_open_fd(t->archive, STDOUT_FILENO);
682 } else {
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));
689 err = 1;
690 goto out_close;
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)) {
699 err = 1;
700 goto out_close;
702 } else {
703 mask = talloc_asprintf(ctx, "%s\\*", client_get_cur_dir());
704 if (mask == NULL) {
705 err = 1;
706 goto out_close;
708 mask = client_clean_name(ctx, mask);
709 if (mask == NULL) {
710 err = 1;
711 goto out_close;
713 DBG(5, ("tar_process do_list with mask: %s\n", mask));
714 status = do_list(mask, TAR_DO_LIST_ATTR, get_file_callback, true, true);
715 if (!NT_STATUS_IS_OK(status)) {
716 DBG(0, ("do_list fail %s\n", nt_errstr(status)));
717 err = 1;
718 goto out_close;
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",
726 t->total_size,
727 t->total_size/timespec_elapsed2(&tp_start, &tp_end)/1024/1024);
729 out_close:
731 if (!t->mode.dry) {
732 r = archive_write_close(t->archive);
733 if (r != ARCHIVE_OK) {
734 d_printf("Fatal: %s\n", archive_error_string(t->archive));
735 err = 1;
736 goto out;
739 out:
740 #ifdef HAVE_ARCHIVE_READ_FREE
741 archive_write_free(t->archive);
742 #else
743 archive_write_finish(t->archive);
744 #endif
745 talloc_free(ctx);
746 return err;
750 * tar_create_from_list - fetch from path list in include mode
752 static int tar_create_from_list(struct tar *t)
754 int err = 0;
755 NTSTATUS status;
756 char *base;
757 const char *path, *mask, *start_dir;
758 int i;
759 TALLOC_CTX *ctx = talloc_new(NULL);
760 if (ctx == NULL) {
761 return 1;
764 start_dir = talloc_strdup(ctx, client_get_cur_dir());
765 if (start_dir == NULL) {
766 err = 1;
767 goto out;
770 for (i = 0; i < t->path_list_size; i++) {
771 path = t->path_list[i];
772 base = NULL;
773 status = path_base_name(ctx, path, &base);
774 if (!NT_STATUS_IS_OK(status)) {
775 err = 1;
776 goto out;
778 mask = talloc_asprintf(ctx, "%s\\%s",
779 client_get_cur_dir(), path);
780 if (mask == NULL) {
781 err = 1;
782 goto out;
784 mask = client_clean_name(ctx, mask);
785 if (mask == NULL) {
786 err = 1;
787 goto out;
790 DBG(5, ("incl. path='%s', base='%s', mask='%s'\n",
791 path, base ? base : "NULL", mask));
793 if (base != NULL) {
794 base = talloc_asprintf(ctx, "%s%s\\",
795 client_get_cur_dir(), base);
796 if (base == NULL) {
797 err = 1;
798 goto out;
800 base = client_clean_name(ctx, base);
801 if (base == NULL) {
802 err = 1;
803 goto out;
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, true, true);
810 if (base != NULL) {
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)));
815 err = 1;
816 goto out;
820 out:
821 talloc_free(ctx);
822 return err;
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,
834 const char *dir)
836 NTSTATUS status = NT_STATUS_OK;
837 char *remote_name;
838 char *old_dir = NULL;
839 char *new_dir = NULL;
840 const char *initial_dir = dir;
841 bool skip = false;
842 bool isdir;
843 int rc;
844 TALLOC_CTX *ctx = talloc_new(NULL);
845 if (ctx == 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;
852 goto out;
854 remote_name = client_clean_name(ctx, remote_name);
855 if (remote_name == NULL) {
856 status = NT_STATUS_NO_MEMORY;
857 goto out;
860 if (strequal(finfo->name, "..") || strequal(finfo->name, ".")) {
861 goto out;
864 isdir = finfo->attr & FILE_ATTRIBUTE_DIRECTORY;
865 if (isdir) {
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;
870 goto out;
874 status = tar_create_skip_path(&tar_ctx,
875 isdir ? new_dir : remote_name,
876 finfo, &skip);
877 if (!NT_STATUS_IS_OK(status)) {
878 goto out;
881 if (skip) {
882 DBG(5, ("--- %s\n", remote_name));
883 status = NT_STATUS_OK;
884 goto out;
887 rc = tar_get_file(&tar_ctx, remote_name, finfo);
888 if (rc != 0) {
889 status = NT_STATUS_UNSUCCESSFUL;
890 goto out;
893 out:
894 talloc_free(ctx);
895 return status;
899 * tar_get_file - fetch a remote file to the local archive
900 * @full_dos_path: path to the file to fetch
901 * @finfo: attributes of the file to fetch
903 static int tar_get_file(struct tar *t,
904 const char *full_dos_path,
905 struct file_info *finfo)
907 extern struct cli_state *cli;
908 NTSTATUS status;
909 struct archive_entry *entry;
910 char *full_unix_path;
911 char buf[TAR_CLI_READ_SIZE];
912 size_t len;
913 uint64_t off = 0;
914 uint16_t remote_fd = (uint16_t)-1;
915 int err = 0, r;
916 const bool isdir = finfo->attr & FILE_ATTRIBUTE_DIRECTORY;
917 TALLOC_CTX *ctx = talloc_new(NULL);
919 if (ctx == NULL) {
920 return 1;
923 DBG(5, ("+++ %s\n", full_dos_path));
925 t->total_size += finfo->size;
927 if (t->mode.dry) {
928 goto out;
931 if (t->mode.reset) {
932 /* ignore return value: server might not store DOS attributes */
933 set_remote_attr(full_dos_path, FILE_ATTRIBUTE_ARCHIVE, ATTR_UNSET);
936 full_unix_path = talloc_asprintf(ctx, ".%s", full_dos_path);
937 if (full_unix_path == NULL) {
938 err = 1;
939 goto out;
941 string_replace(full_unix_path, '\\', '/');
942 entry = archive_entry_new();
943 archive_entry_copy_pathname(entry, full_unix_path);
944 archive_entry_set_filetype(entry, isdir ? AE_IFDIR : AE_IFREG);
945 archive_entry_set_atime(entry,
946 finfo->atime_ts.tv_sec,
947 finfo->atime_ts.tv_nsec);
948 archive_entry_set_mtime(entry,
949 finfo->mtime_ts.tv_sec,
950 finfo->mtime_ts.tv_nsec);
951 archive_entry_set_ctime(entry,
952 finfo->ctime_ts.tv_sec,
953 finfo->ctime_ts.tv_nsec);
954 archive_entry_set_perm(entry, isdir ? 0755 : 0644);
956 * check if we can safely cast unsigned file size to libarchive
957 * signed size. Very unlikely problem (>9 exabyte file)
959 if (finfo->size > INT64_MAX) {
960 d_printf("Remote file %s too big\n", full_dos_path);
961 goto out_entry;
964 archive_entry_set_size(entry, (int64_t)finfo->size);
966 if (isdir) {
967 /* It's a directory just write a header */
968 r = archive_write_header(t->archive, entry);
969 if (r != ARCHIVE_OK) {
970 d_printf("Fatal: %s\n", archive_error_string(t->archive));
971 err = 1;
973 if (t->mode.verbose) {
974 d_printf("a %s\\\n", full_dos_path);
976 DBG(5, ("get_file skip dir %s\n", full_dos_path));
977 goto out_entry;
980 status = cli_open(cli, full_dos_path, O_RDONLY, DENY_NONE, &remote_fd);
981 if (!NT_STATUS_IS_OK(status)) {
982 d_printf("%s opening remote file %s\n",
983 nt_errstr(status), full_dos_path);
984 goto out_entry;
987 /* don't make tar file entry until after the file is open */
988 r = archive_write_header(t->archive, entry);
989 if (r != ARCHIVE_OK) {
990 d_printf("Fatal: %s\n", archive_error_string(t->archive));
991 err = 1;
992 goto out_entry;
995 if (t->mode.verbose) {
996 d_printf("a %s\n", full_dos_path);
999 do {
1000 status = cli_read(cli, remote_fd, buf, off, sizeof(buf), &len);
1001 if (!NT_STATUS_IS_OK(status)) {
1002 d_printf("Error reading file %s : %s\n",
1003 full_dos_path, nt_errstr(status));
1004 err = 1;
1005 goto out_close;
1008 off += len;
1010 r = archive_write_data(t->archive, buf, len);
1011 if (r < 0) {
1012 d_printf("Fatal: %s\n", archive_error_string(t->archive));
1013 err = 1;
1014 goto out_close;
1017 } while (off < finfo->size);
1018 t->numfile++;
1020 out_close:
1021 cli_close(cli, remote_fd);
1023 out_entry:
1024 archive_entry_free(entry);
1026 out:
1027 talloc_free(ctx);
1028 return err;
1032 * tar_extract - open archive and send files.
1034 static int tar_extract(struct tar *t)
1036 int err = 0;
1037 int r;
1038 struct archive_entry *entry;
1039 const size_t bsize = t->mode.blocksize * TAR_BLOCK_UNIT;
1040 int rc;
1042 t->archive = archive_read_new();
1043 archive_read_support_format_all(t->archive);
1044 #ifdef HAVE_ARCHIVE_READ_SUPPORT_FILTER_ALL
1045 archive_read_support_filter_all(t->archive);
1046 #endif
1048 if (strequal(t->tar_path, "-")) {
1049 r = archive_read_open_fd(t->archive, STDIN_FILENO, bsize);
1050 } else {
1051 r = archive_read_open_filename(t->archive, t->tar_path, bsize);
1054 if (r != ARCHIVE_OK) {
1055 d_printf("Can't open %s : %s\n", t->tar_path,
1056 archive_error_string(t->archive));
1057 err = 1;
1058 goto out;
1061 for (;;) {
1062 NTSTATUS status;
1063 bool skip = false;
1064 r = archive_read_next_header(t->archive, &entry);
1065 if (r == ARCHIVE_EOF) {
1066 break;
1068 if (r == ARCHIVE_WARN) {
1069 d_printf("Warning: %s\n", archive_error_string(t->archive));
1071 if (r == ARCHIVE_FATAL) {
1072 d_printf("Fatal: %s\n", archive_error_string(t->archive));
1073 err = 1;
1074 goto out;
1077 status = tar_extract_skip_path(t, entry, &skip);
1078 if (!NT_STATUS_IS_OK(status)) {
1079 err = 1;
1080 goto out;
1082 if (skip) {
1083 DBG(5, ("--- %s\n", archive_entry_pathname(entry)));
1084 continue;
1086 DBG(5, ("+++ %s\n", archive_entry_pathname(entry)));
1088 if (t->mode.verbose) {
1089 d_printf("x %s\n", archive_entry_pathname(entry));
1092 rc = tar_send_file(t, entry);
1093 if (rc != 0) {
1094 err = 1;
1095 goto out;
1099 out:
1100 #ifdef HAVE_ARCHIVE_READ_FREE
1101 r = archive_read_free(t->archive);
1102 #else
1103 r = archive_read_finish(t->archive);
1104 #endif
1105 if (r != ARCHIVE_OK) {
1106 d_printf("Can't close %s : %s\n", t->tar_path,
1107 archive_error_string(t->archive));
1108 err = 1;
1110 return err;
1114 * tar_send_file - send @entry to the remote server
1115 * @entry: current archive entry
1117 * Handle the creation of the parent directories and transfer the
1118 * entry to a new remote file.
1120 static int tar_send_file(struct tar *t, struct archive_entry *entry)
1122 extern struct cli_state *cli;
1123 char *dos_path;
1124 char *full_path;
1125 NTSTATUS status;
1126 uint16_t remote_fd = (uint16_t) -1;
1127 int err = 0;
1128 int flags = O_RDWR | O_CREAT | O_TRUNC;
1129 mode_t filetype = archive_entry_filetype(entry);
1130 mode_t mode = archive_entry_mode(entry);
1131 time_t mtime = archive_entry_mtime(entry);
1132 int rc;
1133 TALLOC_CTX *ctx = talloc_new(NULL);
1134 if (ctx == NULL) {
1135 return 1;
1138 dos_path = talloc_strdup(ctx, archive_entry_pathname(entry));
1139 if (dos_path == NULL) {
1140 err = 1;
1141 goto out;
1143 fix_unix_path(dos_path, true);
1145 full_path = talloc_strdup(ctx, client_get_cur_dir());
1146 if (full_path == NULL) {
1147 err = 1;
1148 goto out;
1150 full_path = talloc_strdup_append(full_path, dos_path);
1151 if (full_path == NULL) {
1152 err = 1;
1153 goto out;
1155 full_path = client_clean_name(ctx, full_path);
1156 if (full_path == NULL) {
1157 err = 1;
1158 goto out;
1161 if (filetype != AE_IFREG && filetype != AE_IFDIR) {
1162 d_printf("Skipping non-dir & non-regular file %s\n", full_path);
1163 goto out;
1166 rc = make_remote_path(full_path);
1167 if (rc != 0) {
1168 err = 1;
1169 goto out;
1172 if (filetype == AE_IFDIR) {
1173 goto out;
1176 status = cli_open(cli, full_path, flags, DENY_NONE, &remote_fd);
1177 if (!NT_STATUS_IS_OK(status)) {
1178 d_printf("Error opening remote file %s: %s\n",
1179 full_path, nt_errstr(status));
1180 err = 1;
1181 goto out;
1184 for (;;) {
1185 const void *buf;
1186 size_t len;
1187 off_t off;
1188 int r;
1190 r = archive_read_data_block(t->archive, &buf, &len, &off);
1191 if (r == ARCHIVE_EOF) {
1192 break;
1194 if (r == ARCHIVE_WARN) {
1195 d_printf("Warning: %s\n", archive_error_string(t->archive));
1197 if (r == ARCHIVE_FATAL) {
1198 d_printf("Fatal: %s\n", archive_error_string(t->archive));
1199 err = 1;
1200 goto close_out;
1203 status = cli_writeall(cli, remote_fd, 0, buf, off, len, NULL);
1204 if (!NT_STATUS_IS_OK(status)) {
1205 d_printf("Error writing remote file %s: %s\n",
1206 full_path, nt_errstr(status));
1207 err = 1;
1208 goto close_out;
1212 close_out:
1213 status = cli_close(cli, remote_fd);
1214 if (!NT_STATUS_IS_OK(status)) {
1215 d_printf("Error closing remote file %s: %s\n",
1216 full_path, nt_errstr(status));
1217 err = 1;
1220 status = cli_setatr(cli, full_path, mode, mtime);
1221 if (!NT_STATUS_IS_OK(status)) {
1222 d_printf("Error setting attributes on remote file %s: %s\n",
1223 full_path, nt_errstr(status));
1224 err = 1;
1227 out:
1228 talloc_free(ctx);
1229 return err;
1233 * tar_add_selection_path - add a path to the path list
1234 * @path: path to add
1236 static NTSTATUS tar_add_selection_path(struct tar *t, const char *path)
1238 const char **list;
1239 TALLOC_CTX *ctx = t->talloc_ctx;
1240 if (!t->path_list) {
1241 t->path_list = str_list_make_empty(ctx);
1242 if (t->path_list == NULL) {
1243 return NT_STATUS_NO_MEMORY;
1245 t->path_list_size = 0;
1248 /* cast to silence gcc const-qual warning */
1249 list = str_list_add((void *)t->path_list, path);
1250 if (list == NULL) {
1251 return NT_STATUS_NO_MEMORY;
1253 t->path_list = discard_const_p(char *, list);
1254 t->path_list_size++;
1255 fix_unix_path(t->path_list[t->path_list_size - 1], true);
1257 return NT_STATUS_OK;
1261 * tar_set_blocksize - set block size in TAR_BLOCK_UNIT
1263 static int tar_set_blocksize(struct tar *t, int size)
1265 if (size <= 0 || size > TAR_MAX_BLOCK_SIZE) {
1266 return 1;
1269 t->mode.blocksize = size;
1271 return 0;
1275 * tar_set_newer_than - set date threshold of saved files
1276 * @filename: local path to a file
1278 * Only files newer than the modification time of @filename will be
1279 * saved.
1281 * Note: this function set the global variable newer_than from
1282 * client.c. Thus the time is not a field of the tar structure. See
1283 * cmd_newer() to change its value from an interactive session.
1285 static int tar_set_newer_than(struct tar *t, const char *filename)
1287 extern time_t newer_than;
1288 SMB_STRUCT_STAT stbuf;
1289 int rc;
1291 rc = sys_stat(filename, &stbuf, false);
1292 if (rc != 0) {
1293 d_printf("Error setting newer-than time\n");
1294 return 1;
1297 newer_than = convert_timespec_to_time_t(stbuf.st_ex_mtime);
1298 DBG(1, ("Getting files newer than %s\n", time_to_asc(newer_than)));
1299 return 0;
1303 * tar_read_inclusion_file - set path list from file
1304 * @filename: path to the list file
1306 * Read and add each line of @filename to the path list.
1308 static int tar_read_inclusion_file(struct tar *t, const char* filename)
1310 char *line;
1311 int err = 0;
1312 int fd = -1;
1313 TALLOC_CTX *ctx = talloc_new(NULL);
1314 if (ctx == NULL) {
1315 return 1;
1318 fd = open(filename, O_RDONLY);
1319 if (fd < 0) {
1320 d_printf("Can't open inclusion file '%s': %s\n", filename, strerror(errno));
1321 err = 1;
1322 goto out;
1325 for (line = afdgets(fd, ctx, 0);
1326 line != NULL;
1327 line = afdgets(fd, ctx, 0)) {
1328 NTSTATUS status;
1329 status = tar_add_selection_path(t, line);
1330 if (!NT_STATUS_IS_OK(status)) {
1331 err = 1;
1332 goto out;
1336 out:
1337 if (fd != -1) {
1338 close(fd);
1340 talloc_free(ctx);
1341 return err;
1345 * tar_path_in_list - check whether @path is in the path list
1346 * @path: path to find
1347 * @reverse: when true also try to find path list element in @path
1348 * @_is_in_list: set if @path is in the path list
1350 * Look at each path of the path list and set @_is_in_list if @path is a
1351 * subpath of one of them.
1353 * If you want /path to be in the path list (path/a/, path/b/) set
1354 * @reverse to true to try to match the other way around.
1356 static NTSTATUS tar_path_in_list(struct tar *t, const char *path,
1357 bool reverse, bool *_is_in_list)
1359 int i;
1360 const char *p;
1361 const char *pattern;
1363 if (path == NULL || path[0] == '\0') {
1364 *_is_in_list = false;
1365 return NT_STATUS_OK;
1368 p = skip_useless_char_in_path(path);
1370 for (i = 0; i < t->path_list_size; i++) {
1371 bool is_in_list;
1372 NTSTATUS status;
1374 pattern = skip_useless_char_in_path(t->path_list[i]);
1375 status = is_subpath(p, pattern, &is_in_list);
1376 if (!NT_STATUS_IS_OK(status)) {
1377 return status;
1379 if (reverse && !is_in_list) {
1380 status = is_subpath(pattern, p, &is_in_list);
1381 if (!NT_STATUS_IS_OK(status)) {
1382 return status;
1385 if (is_in_list) {
1386 *_is_in_list = true;
1387 return NT_STATUS_OK;
1391 *_is_in_list = false;
1392 return NT_STATUS_OK;
1396 * tar_extract_skip_path - check if @entry should be skipped
1397 * @entry: current tar entry
1398 * @_skip: set true if path should be skipped, otherwise false
1400 * Skip predicate for tar extraction (archive to server) only.
1402 static NTSTATUS tar_extract_skip_path(struct tar *t,
1403 struct archive_entry *entry,
1404 bool *_skip)
1406 const char *fullpath = archive_entry_pathname(entry);
1407 bool in = true;
1409 if (t->path_list_size <= 0) {
1410 *_skip = false;
1411 return NT_STATUS_OK;
1414 if (t->mode.regex) {
1415 in = mask_match_list(fullpath, t->path_list, t->path_list_size, true);
1416 } else {
1417 NTSTATUS status = tar_path_in_list(t, fullpath, false, &in);
1418 if (!NT_STATUS_IS_OK(status)) {
1419 return status;
1423 if (t->mode.selection == TAR_EXCLUDE) {
1424 *_skip = in;
1425 } else {
1426 *_skip = !in;
1429 return NT_STATUS_OK;
1433 * tar_create_skip_path - check if @fullpath shoud be skipped
1434 * @fullpath: full remote path of the current file
1435 * @finfo: remote file attributes
1436 * @_skip: returned skip not
1438 * Skip predicate for tar creation (server to archive) only.
1440 static NTSTATUS tar_create_skip_path(struct tar *t,
1441 const char *fullpath,
1442 const struct file_info *finfo,
1443 bool *_skip)
1445 /* syntaxic sugar */
1446 const mode_t mode = finfo->attr;
1447 const bool isdir = mode & FILE_ATTRIBUTE_DIRECTORY;
1448 const bool exclude = t->mode.selection == TAR_EXCLUDE;
1449 bool in = true;
1451 if (!isdir) {
1453 /* 1. if we don't want X and we have X, skip */
1454 if (!t->mode.system && (mode & FILE_ATTRIBUTE_SYSTEM)) {
1455 *_skip = true;
1456 return NT_STATUS_OK;
1459 if (!t->mode.hidden && (mode & FILE_ATTRIBUTE_HIDDEN)) {
1460 *_skip = true;
1461 return NT_STATUS_OK;
1464 /* 2. if we only want archive and it's not, skip */
1466 if (t->mode.incremental && !(mode & FILE_ATTRIBUTE_ARCHIVE)) {
1467 *_skip = true;
1468 return NT_STATUS_OK;
1472 /* 3. is it in the selection list? */
1475 * tar_create_from_list() use the include list as a starting
1476 * point, no need to check
1478 if (!exclude) {
1479 *_skip = false;
1480 return NT_STATUS_OK;
1483 /* we are now in exclude mode */
1485 /* no matter the selection, no list => include everything */
1486 if (t->path_list_size <= 0) {
1487 *_skip = false;
1488 return NT_STATUS_OK;
1491 if (t->mode.regex) {
1492 in = mask_match_list(fullpath, t->path_list, t->path_list_size, true);
1493 } else {
1494 bool reverse = isdir && !exclude;
1495 NTSTATUS status = tar_path_in_list(t, fullpath, reverse, &in);
1496 if (!NT_STATUS_IS_OK(status)) {
1497 return status;
1500 *_skip = in;
1502 return NT_STATUS_OK;
1506 * tar_to_process - return true if @t is ready to be processed
1508 * @t is ready if it properly parsed command line arguments.
1510 bool tar_to_process(struct tar *t)
1512 if (t == NULL) {
1513 DBG_WARNING("Invalid tar context\n");
1514 return false;
1516 return t->to_process;
1520 * skip_useless_char_in_path - skip leading slashes/dots
1522 * Skip leading slashes, backslashes and dot-slashes.
1524 static const char* skip_useless_char_in_path(const char *p)
1526 while (p) {
1527 if (*p == '/' || *p == '\\') {
1528 p++;
1530 else if (p[0] == '.' && (p[1] == '/' || p[1] == '\\')) {
1531 p += 2;
1533 else
1534 return p;
1536 return p;
1540 * is_subpath - check if the path @sub is a subpath of @full.
1541 * @sub: path to test
1542 * @full: container path
1543 * @_subpath_match: set true if @sub is a subpath of @full, otherwise false
1545 * String comparaison is case-insensitive.
1547 static NTSTATUS is_subpath(const char *sub, const char *full,
1548 bool *_subpath_match)
1550 NTSTATUS status = NT_STATUS_OK;
1551 int len = 0;
1552 char *f, *s;
1553 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
1554 if (tmp_ctx == NULL) {
1555 status = NT_STATUS_NO_MEMORY;
1556 goto out;
1559 f = strlower_talloc(tmp_ctx, full);
1560 if (f == NULL) {
1561 status = NT_STATUS_NO_MEMORY;
1562 goto out_ctx_free;
1564 string_replace(f, '\\', '/');
1565 s = strlower_talloc(tmp_ctx, sub);
1566 if (s == NULL) {
1567 status = NT_STATUS_NO_MEMORY;
1568 goto out_ctx_free;
1570 string_replace(s, '\\', '/');
1572 /* find the point where sub and full diverge */
1573 while ((*f != '\0') && (*s != '\0') && (*f == *s)) {
1574 f++;
1575 s++;
1576 len++;
1579 if ((*f == '\0') && (*s == '\0')) {
1580 *_subpath_match = true; /* sub and full match */
1581 goto out_ctx_free;
1584 if ((*f == '\0') && (len > 0) && (*(f - 1) == '/')) {
1585 /* sub diverges from full at path separator */
1586 *_subpath_match = true;
1587 goto out_ctx_free;
1590 if ((*s == '\0') && (strcmp(f, "/") == 0)) {
1591 /* full diverges from sub with trailing slash only */
1592 *_subpath_match = true;
1593 goto out_ctx_free;
1596 if ((*s == '/') && (*f == '\0')) {
1597 /* sub diverges from full with extra path component */
1598 *_subpath_match = true;
1599 goto out_ctx_free;
1601 *_subpath_match = false;
1603 out_ctx_free:
1604 talloc_free(tmp_ctx);
1605 out:
1606 return status;
1611 * make_remote_path - recursively make remote dirs
1612 * @full_path: full hierarchy to create
1614 * Create @full_path and each parent directories as needed.
1616 static int make_remote_path(const char *full_path)
1618 extern struct cli_state *cli;
1619 char *path;
1620 char *subpath;
1621 char *state;
1622 char *last_backslash;
1623 char *p;
1624 int len;
1625 NTSTATUS status;
1626 int err = 0;
1627 TALLOC_CTX *ctx = talloc_new(NULL);
1628 if (ctx == NULL) {
1629 return 1;
1632 subpath = talloc_strdup(ctx, full_path);
1633 if (subpath == NULL) {
1634 err = 1;
1635 goto out;
1637 path = talloc_strdup(ctx, full_path);
1638 if (path == NULL) {
1639 err = 1;
1640 goto out;
1642 len = talloc_get_size(path) - 1;
1644 last_backslash = strrchr_m(path, '\\');
1645 if (last_backslash == NULL) {
1646 goto out;
1649 *last_backslash = 0;
1651 subpath[0] = 0;
1652 p = strtok_r(path, "\\", &state);
1654 while (p != NULL) {
1655 strlcat(subpath, p, len);
1656 status = cli_chkpath(cli, subpath);
1657 if (!NT_STATUS_IS_OK(status)) {
1658 status = cli_mkdir(cli, subpath);
1659 if (!NT_STATUS_IS_OK(status)) {
1660 d_printf("Can't mkdir %s: %s\n", subpath, nt_errstr(status));
1661 err = 1;
1662 goto out;
1664 DBG(3, ("mkdir %s\n", subpath));
1667 strlcat(subpath, "\\", len);
1668 p = strtok_r(NULL, "/\\", &state);
1672 out:
1673 talloc_free(ctx);
1674 return err;
1678 * tar_reset_mem_context - reset talloc context associated with @t
1680 * At the start of the program the context is NULL so a new one is
1681 * allocated. On the following runs (interactive session only), simply
1682 * free the children.
1684 static TALLOC_CTX *tar_reset_mem_context(struct tar *t)
1686 tar_free_mem_context(t);
1687 t->talloc_ctx = talloc_new(NULL);
1688 return t->talloc_ctx;
1692 * tar_free_mem_context - free talloc context associated with @t
1694 static void tar_free_mem_context(struct tar *t)
1696 if (t->talloc_ctx) {
1697 talloc_free(t->talloc_ctx);
1698 t->talloc_ctx = NULL;
1699 t->path_list_size = 0;
1700 t->path_list = NULL;
1701 t->tar_path = NULL;
1705 #define XSET(v) [v] = #v
1706 #define XTABLE(v, t) DBG(2, ("DUMP:%-20.20s = %s\n", #v, t[v]))
1707 #define XBOOL(v) DBG(2, ("DUMP:%-20.20s = %d\n", #v, v ? 1 : 0))
1708 #define XSTR(v) DBG(2, ("DUMP:%-20.20s = %s\n", #v, v ? v : "NULL"))
1709 #define XINT(v) DBG(2, ("DUMP:%-20.20s = %d\n", #v, v))
1710 #define XUINT64(v) DBG(2, ("DUMP:%-20.20s = %" PRIu64 "\n", #v, v))
1713 * tar_dump - dump tar structure on stdout
1715 static void tar_dump(struct tar *t)
1717 int i;
1718 const char* op[] = {
1719 XSET(TAR_NO_OPERATION),
1720 XSET(TAR_CREATE),
1721 XSET(TAR_EXTRACT),
1724 const char* sel[] = {
1725 XSET(TAR_NO_SELECTION),
1726 XSET(TAR_INCLUDE),
1727 XSET(TAR_EXCLUDE),
1730 XBOOL(t->to_process);
1731 XTABLE(t->mode.operation, op);
1732 XTABLE(t->mode.selection, sel);
1733 XINT(t->mode.blocksize);
1734 XBOOL(t->mode.hidden);
1735 XBOOL(t->mode.system);
1736 XBOOL(t->mode.incremental);
1737 XBOOL(t->mode.reset);
1738 XBOOL(t->mode.dry);
1739 XBOOL(t->mode.verbose);
1740 XUINT64(t->total_size);
1741 XSTR(t->tar_path);
1742 XINT(t->path_list_size);
1744 for (i = 0; t->path_list && t->path_list[i]; i++) {
1745 DBG(2, ("DUMP: t->path_list[%2d] = %s\n", i, t->path_list[i]));
1748 DBG(2, ("DUMP:t->path_list @ %p (%d elem)\n", t->path_list, i));
1750 #undef XSET
1751 #undef XTABLE
1752 #undef XBOOL
1753 #undef XSTR
1754 #undef XINT
1757 * max_token - return upper limit for the number of token in @str
1759 * The result is not exact, the actual number of token might be less
1760 * than what is returned.
1762 static int max_token(const char *str)
1764 const char *s;
1765 int nb = 0;
1767 if (str == NULL) {
1768 return 0;
1771 s = str;
1772 while (s[0] != '\0') {
1773 if (isspace((int)s[0])) {
1774 nb++;
1776 s++;
1779 nb++;
1781 return nb;
1785 * fix_unix_path - convert @path to a DOS path
1786 * @path: path to convert
1787 * @removeprefix: if true, remove leading ./ or /.
1789 static char *fix_unix_path(char *path, bool do_remove_prefix)
1791 char *from = path, *to = path;
1793 if (path == NULL || path[0] == '\0') {
1794 return path;
1797 /* remove prefix:
1798 * ./path => path
1799 * /path => path
1801 if (do_remove_prefix) {
1802 /* /path */
1803 if (path[0] == '/' || path[0] == '\\') {
1804 from += 1;
1807 /* ./path */
1808 if (path[1] != '\0' && path[0] == '.' && (path[1] == '/' || path[1] == '\\')) {
1809 from += 2;
1813 /* replace / with \ */
1814 while (from[0] != '\0') {
1815 if (from[0] == '/') {
1816 to[0] = '\\';
1817 } else {
1818 to[0] = from[0];
1821 from++;
1822 to++;
1824 to[0] = '\0';
1826 return path;
1830 * path_base_name - return @path basename
1832 * If @path doesn't contain any directory separator return NULL.
1834 static NTSTATUS path_base_name(TALLOC_CTX *ctx, const char *path, char **_base)
1836 char *base = NULL;
1837 int last = -1;
1838 int i;
1840 for (i = 0; path[i]; i++) {
1841 if (path[i] == '\\' || path[i] == '/') {
1842 last = i;
1846 if (last >= 0) {
1847 base = talloc_strdup(ctx, path);
1848 if (base == NULL) {
1849 return NT_STATUS_NO_MEMORY;
1852 base[last] = 0;
1855 *_base = base;
1856 return NT_STATUS_OK;
1859 #else
1861 #define NOT_IMPLEMENTED DEBUG(0, ("tar mode not compiled. build used --without-libarchive\n"))
1863 int cmd_block(void)
1865 NOT_IMPLEMENTED;
1866 return 1;
1869 int cmd_tarmode(void)
1871 NOT_IMPLEMENTED;
1872 return 1;
1875 int cmd_tar(void)
1877 NOT_IMPLEMENTED;
1878 return 1;
1881 int tar_process(struct tar* tar)
1883 NOT_IMPLEMENTED;
1884 return 1;
1887 int tar_parse_args(struct tar *tar, const char *flag, const char **val, int valsize)
1889 NOT_IMPLEMENTED;
1890 return 1;
1893 bool tar_to_process(struct tar *tar)
1895 return false;
1898 struct tar *tar_get_ctx()
1900 return NULL;
1903 #endif