smbd: Save a few lines in smbXsrv_client_global_init()
[Samba.git] / source3 / client / clitar.c
blob7e54b7bc2e8d5ac85cfd1afeb1048ab9cf5b483b
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"
74 #include "lib/util/util_file.h"
76 #ifdef HAVE_LIBARCHIVE
78 #include <archive.h>
79 #include <archive_entry.h>
81 /* prepend module name and line number to debug messages */
82 #define DBG(a, b) (DEBUG(a, ("tar:%-4d ", __LINE__)), DEBUG(a, b))
84 /* preprocessor magic to stringify __LINE__ (int) */
85 #define STR1(x) #x
86 #define STR2(x) STR1(x)
88 /**
89 * Number of byte in a block unit.
91 #define TAR_BLOCK_UNIT 512
93 /**
94 * Default tar block size in TAR_BLOCK_UNIT.
96 #define TAR_DEFAULT_BLOCK_SIZE 20
98 /**
99 * Maximum value for the blocksize field
101 #define TAR_MAX_BLOCK_SIZE 0xffff
104 * Size of the buffer used when downloading a file
106 #define TAR_CLI_READ_SIZE 0xff00
108 #define TAR_DO_LIST_ATTR (FILE_ATTRIBUTE_DIRECTORY \
109 | FILE_ATTRIBUTE_SYSTEM \
110 | FILE_ATTRIBUTE_HIDDEN)
113 enum tar_operation {
114 TAR_NO_OPERATION,
115 TAR_CREATE, /* c flag */
116 TAR_EXTRACT, /* x flag */
119 enum tar_selection {
120 TAR_NO_SELECTION,
121 TAR_INCLUDE, /* I and F flag, default */
122 TAR_EXCLUDE, /* X flag */
125 struct tar {
126 TALLOC_CTX *talloc_ctx;
128 /* in state that needs/can be processed? */
129 bool to_process;
131 /* flags */
132 struct tar_mode {
133 enum tar_operation operation; /* create, extract */
134 enum tar_selection selection; /* include, exclude */
135 int blocksize; /* size in TAR_BLOCK_UNIT of a tar file block */
136 bool hidden; /* backup hidden file? */
137 bool system; /* backup system file? */
138 bool incremental; /* backup _only_ archived file? */
139 bool reset; /* unset archive bit? */
140 bool dry; /* don't write tar file? */
141 bool regex; /* XXX: never actually using regex... */
142 bool verbose; /* XXX: ignored */
143 } mode;
145 /* nb of bytes received */
146 uint64_t total_size;
148 /* path to tar archive name */
149 char *tar_path;
151 /* list of path to include or exclude */
152 char **path_list;
153 int path_list_size;
155 /* archive handle */
156 struct archive *archive;
158 /* counters */
159 uint64_t numdir;
160 uint64_t numfile;
164 * Global context imported in client.c when needed.
166 * Default options.
168 struct tar tar_ctx = {
169 .mode.selection = TAR_INCLUDE,
170 .mode.blocksize = TAR_DEFAULT_BLOCK_SIZE,
171 .mode.hidden = true,
172 .mode.system = true,
173 .mode.incremental = false,
174 .mode.reset = false,
175 .mode.dry = false,
176 .mode.regex = false,
177 .mode.verbose = false,
180 /* tar, local function */
181 static int tar_create(struct tar* t);
182 static int tar_create_from_list(struct tar *t);
183 static int tar_extract(struct tar *t);
184 static int tar_read_inclusion_file(struct tar *t, const char* filename);
185 static int tar_send_file(struct tar *t, struct archive_entry *entry);
186 static int tar_set_blocksize(struct tar *t, int size);
187 static int tar_set_newer_than(struct tar *t, const char *filename);
188 static NTSTATUS tar_add_selection_path(struct tar *t, const char *path);
189 static void tar_dump(struct tar *t);
190 static NTSTATUS tar_extract_skip_path(struct tar *t,
191 struct archive_entry *entry,
192 bool *_skip);
193 static TALLOC_CTX *tar_reset_mem_context(struct tar *t);
194 static void tar_free_mem_context(struct tar *t);
195 static NTSTATUS tar_create_skip_path(struct tar *t,
196 const char *fullpath,
197 const struct file_info *finfo,
198 bool *_skip);
200 static NTSTATUS tar_path_in_list(struct tar *t, const char *path,
201 bool reverse, bool *_is_in_list);
203 static int tar_get_file(struct tar *t,
204 const char *full_dos_path,
205 struct file_info *finfo);
207 static NTSTATUS get_file_callback(struct cli_state *cli,
208 struct file_info *finfo,
209 const char *dir);
211 /* utilities */
212 static char *fix_unix_path(char *path, bool removeprefix);
213 static NTSTATUS path_base_name(TALLOC_CTX *ctx, const char *path, char **_base);
214 static const char* skip_useless_char_in_path(const char *p);
215 static int make_remote_path(const char *full_path);
216 static int max_token (const char *str);
217 static NTSTATUS is_subpath(const char *sub, const char *full,
218 bool *_subpath_match);
220 * tar_get_ctx - retrieve global tar context handle
222 struct tar *tar_get_ctx(void)
224 return &tar_ctx;
228 * cmd_block - interactive command to change tar blocksize
230 * Read a size from the client command line and update the current
231 * blocksize.
233 int cmd_block(void)
235 /* XXX: from client.c */
236 const extern char *cmd_ptr;
237 char *buf;
238 int err = 0;
239 bool ok;
240 TALLOC_CTX *ctx = talloc_new(NULL);
241 if (ctx == NULL) {
242 return 1;
245 ok = next_token_talloc(ctx, &cmd_ptr, &buf, NULL);
246 if (!ok) {
247 DBG(0, ("blocksize <n>\n"));
248 err = 1;
249 goto out;
252 ok = tar_set_blocksize(&tar_ctx, atoi(buf));
253 if (ok) {
254 DBG(0, ("invalid blocksize\n"));
255 err = 1;
256 goto out;
259 DBG(2, ("blocksize is now %d\n", tar_ctx.mode.blocksize));
261 out:
262 talloc_free(ctx);
263 return err;
267 * cmd_tarmode - interactive command to change tar behaviour
269 * Read one or more modes from the client command line and update the
270 * current tar mode.
272 int cmd_tarmode(void)
274 const extern char *cmd_ptr;
275 char *buf;
276 TALLOC_CTX *ctx;
278 struct {
279 const char *cmd;
280 bool *p;
281 bool value;
282 } table[] = {
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);
296 if (ctx == NULL) {
297 return 1;
300 while (next_token_talloc(ctx, &cmd_ptr, &buf, NULL)) {
301 size_t i;
302 for (i = 0; i < ARRAY_SIZE(table); i++) {
303 if (strequal(table[i].cmd, buf)) {
304 *table[i].p = table[i].value;
305 break;
309 if (i == ARRAY_SIZE(table))
310 d_printf("tarmode: unrecognised option %s\n", buf);
313 d_printf("tarmode is now %s, %s, %s, %s, %s\n",
314 tar_ctx.mode.incremental ? "incremental" : "full",
315 tar_ctx.mode.system ? "system" : "nosystem",
316 tar_ctx.mode.hidden ? "hidden" : "nohidden",
317 tar_ctx.mode.reset ? "reset" : "noreset",
318 tar_ctx.mode.verbose ? "verbose" : "noverbose");
320 talloc_free(ctx);
321 return 0;
325 * cmd_tar - interactive command to start a tar backup/restoration
327 * Check presence of argument, parse them and handle the request.
329 int cmd_tar(void)
331 const extern char *cmd_ptr;
332 const char *flag;
333 const char **val;
334 char *buf;
335 int maxtok = max_token(cmd_ptr);
336 int i = 0;
337 int err = 0;
338 bool ok;
339 int rc;
340 TALLOC_CTX *ctx = talloc_new(NULL);
341 if (ctx == NULL) {
342 return 1;
345 ok = next_token_talloc(ctx, &cmd_ptr, &buf, NULL);
346 if (!ok) {
347 d_printf("tar <c|x>[IXFbgvanN] [options] <tar file> [path list]\n");
348 err = 1;
349 goto out;
352 flag = buf;
353 val = talloc_array(ctx, const char *, maxtok);
354 if (val == NULL) {
355 err = 1;
356 goto out;
359 while (next_token_talloc(ctx, &cmd_ptr, &buf, NULL)) {
360 val[i++] = buf;
363 rc = tar_parse_args(&tar_ctx, flag, val, i);
364 if (rc != 0) {
365 d_printf("parse_args failed\n");
366 err = 1;
367 goto out;
370 rc = tar_process(&tar_ctx);
371 if (rc != 0) {
372 d_printf("tar_process failed\n");
373 err = 1;
374 goto out;
377 out:
378 talloc_free(ctx);
379 return err;
384 * tar_parse_args - parse and set tar command line arguments
385 * @flag: string pointing to tar options
386 * @val: number of tar arguments
387 * @valsize: table of arguments after the flags (number of element in val)
389 * tar arguments work in a weird way. For each flag f that takes a
390 * value v, the user is supposed to type:
392 * on the CLI:
393 * -Tf1f2f3 v1 v2 v3 TARFILE PATHS...
395 * in the interactive session:
396 * tar f1f2f3 v1 v2 v3 TARFILE PATHS...
398 * @flag has only flags (eg. "f1f2f3") and @val has the arguments
399 * (values) following them (eg. ["v1", "v2", "v3", "TARFILE", "PATH1",
400 * "PATH2"]).
402 * There are only 2 flags that take an arg: b and N. The other flags
403 * just change the semantic of PATH or TARFILE.
405 * PATH can be a list of included/excluded paths, the path to a file
406 * containing a list of included/excluded paths to use (F flag). If no
407 * PATH is provided, the whole share is used (/).
409 int tar_parse_args(struct tar* t,
410 const char *flag,
411 const char **val,
412 int valsize)
414 TALLOC_CTX *ctx;
415 bool do_read_list = false;
416 /* index of next value to use */
417 int ival = 0;
418 int rc;
420 if (t == NULL) {
421 DBG_WARNING("Invalid tar context\n");
422 return 1;
425 ctx = tar_reset_mem_context(t);
426 if (ctx == NULL) {
427 return 1;
430 * Reset back some options - could be from interactive version
431 * all other modes are left as they are
433 t->mode.operation = TAR_NO_OPERATION;
434 t->mode.selection = TAR_NO_SELECTION;
435 t->mode.dry = false;
436 t->to_process = false;
437 t->total_size = 0;
439 while (flag[0] != '\0') {
440 switch(flag[0]) {
441 /* operation */
442 case 'c':
443 if (t->mode.operation != TAR_NO_OPERATION) {
444 d_printf("Tar must be followed by only one of c or x.\n");
445 return 1;
447 t->mode.operation = TAR_CREATE;
448 break;
449 case 'x':
450 if (t->mode.operation != TAR_NO_OPERATION) {
451 d_printf("Tar must be followed by only one of c or x.\n");
452 return 1;
454 t->mode.operation = TAR_EXTRACT;
455 break;
457 /* selection */
458 case 'I':
459 if (t->mode.selection != TAR_NO_SELECTION) {
460 d_printf("Only one of I,X,F must be specified\n");
461 return 1;
463 t->mode.selection = TAR_INCLUDE;
464 break;
465 case 'X':
466 if (t->mode.selection != TAR_NO_SELECTION) {
467 d_printf("Only one of I,X,F must be specified\n");
468 return 1;
470 t->mode.selection = TAR_EXCLUDE;
471 break;
472 case 'F':
473 if (t->mode.selection != TAR_NO_SELECTION) {
474 d_printf("Only one of I,X,F must be specified\n");
475 return 1;
477 t->mode.selection = TAR_INCLUDE;
478 do_read_list = true;
479 break;
481 /* blocksize */
482 case 'b':
483 if (ival >= valsize) {
484 d_printf("Option b must be followed by a blocksize\n");
485 return 1;
488 if (tar_set_blocksize(t, atoi(val[ival]))) {
489 d_printf("Option b must be followed by a valid blocksize\n");
490 return 1;
493 ival++;
494 break;
496 /* incremental mode */
497 case 'g':
498 t->mode.incremental = true;
499 break;
501 /* newer than */
502 case 'N':
503 if (ival >= valsize) {
504 d_printf("Option N must be followed by valid file name\n");
505 return 1;
508 if (tar_set_newer_than(t, val[ival])) {
509 d_printf("Error setting newer-than time\n");
510 return 1;
513 ival++;
514 break;
516 /* reset mode */
517 case 'a':
518 t->mode.reset = true;
519 break;
521 /* verbose */
522 case 'v':
523 t->mode.verbose = true;
524 break;
526 /* regex match */
527 case 'r':
528 t->mode.regex = true;
529 break;
531 /* dry run mode */
532 case 'n':
533 if (t->mode.operation != TAR_CREATE) {
534 d_printf("n is only meaningful when creating a tar-file\n");
535 return 1;
538 t->mode.dry = true;
539 d_printf("dry_run set\n");
540 break;
542 default:
543 d_printf("Unknown tar option\n");
544 return 1;
547 flag++;
550 /* no selection given? default selection is include */
551 if (t->mode.selection == TAR_NO_SELECTION) {
552 t->mode.selection = TAR_INCLUDE;
555 if (valsize - ival < 1) {
556 d_printf("No tar file given.\n");
557 return 1;
560 /* handle TARFILE */
561 t->tar_path = talloc_strdup(ctx, val[ival]);
562 if (t->tar_path == NULL) {
563 return 1;
565 ival++;
568 * Make sure that dbf points to stderr if we are using stdout for
569 * tar output
571 if (t->mode.operation == TAR_CREATE && strequal(t->tar_path, "-")) {
572 setup_logging("smbclient", DEBUG_STDERR);
575 /* handle PATHs... */
577 /* flag F -> read file list */
578 if (do_read_list) {
579 if (valsize - ival != 1) {
580 d_printf("Option F must be followed by exactly one filename.\n");
581 return 1;
584 rc = tar_read_inclusion_file(t, val[ival]);
585 if (rc != 0) {
586 return 1;
588 ival++;
589 /* otherwise store all the PATHs on the command line */
590 } else {
591 int i;
592 for (i = ival; i < valsize; i++) {
593 NTSTATUS status;
594 status = tar_add_selection_path(t, val[i]);
595 if (!NT_STATUS_IS_OK(status)) {
596 return 1;
601 t->to_process = true;
602 tar_dump(t);
603 return 0;
607 * tar_process - start processing archive
609 * The talloc context of the fields is freed at the end of the call.
611 int tar_process(struct tar *t)
613 int rc = 0;
615 if (t == NULL) {
616 DBG_WARNING("Invalid tar context\n");
617 return 1;
620 switch(t->mode.operation) {
621 case TAR_EXTRACT:
622 rc = tar_extract(t);
623 break;
624 case TAR_CREATE:
625 rc = tar_create(t);
626 break;
627 default:
628 DBG_WARNING("Invalid tar state\n");
629 rc = 1;
632 t->to_process = false;
633 tar_free_mem_context(t);
634 DBG(5, ("tar_process done, err = %d\n", rc));
635 return rc;
639 * tar_create - create archive and fetch files
641 static int tar_create(struct tar* t)
643 int r;
644 int err = 0;
645 NTSTATUS status;
646 const char *mask;
647 struct timespec tp_start, tp_end;
648 TALLOC_CTX *ctx = talloc_new(NULL);
649 if (ctx == NULL) {
650 return 1;
653 clock_gettime_mono(&tp_start);
655 t->numfile = 0;
656 t->numdir = 0;
657 t->archive = archive_write_new();
659 if (!t->mode.dry) {
660 const int bsize = t->mode.blocksize * TAR_BLOCK_UNIT;
661 r = archive_write_set_bytes_per_block(t->archive, bsize);
662 if (r != ARCHIVE_OK) {
663 d_printf("Can't use a block size of %d bytes", bsize);
664 err = 1;
665 goto out;
669 * Use PAX restricted format which is not the most
670 * conservative choice but has useful extensions and is widely
671 * supported
673 r = archive_write_set_format_pax_restricted(t->archive);
674 if (r != ARCHIVE_OK) {
675 d_printf("Can't use pax restricted format: %s\n",
676 archive_error_string(t->archive));
677 err = 1;
678 goto out;
681 if (strequal(t->tar_path, "-")) {
682 r = archive_write_open_fd(t->archive, STDOUT_FILENO);
683 } else {
684 r = archive_write_open_filename(t->archive, t->tar_path);
687 if (r != ARCHIVE_OK) {
688 d_printf("Can't open %s: %s\n", t->tar_path,
689 archive_error_string(t->archive));
690 err = 1;
691 goto out_close;
696 * In inclusion mode, iterate on the inclusion list
698 if (t->mode.selection == TAR_INCLUDE && t->path_list_size > 0) {
699 if (tar_create_from_list(t)) {
700 err = 1;
701 goto out_close;
703 } else {
704 mask = talloc_asprintf(ctx, "%s\\*", client_get_cur_dir());
705 if (mask == NULL) {
706 err = 1;
707 goto out_close;
709 mask = client_clean_name(ctx, mask);
710 if (mask == NULL) {
711 err = 1;
712 goto out_close;
714 DBG(5, ("tar_process do_list with mask: %s\n", mask));
715 status = do_list(mask, TAR_DO_LIST_ATTR, get_file_callback, true, true);
716 if (!NT_STATUS_IS_OK(status)) {
717 DBG(0, ("do_list fail %s\n", nt_errstr(status)));
718 err = 1;
719 goto out_close;
723 clock_gettime_mono(&tp_end);
724 d_printf("tar: dumped %"PRIu64" files and %"PRIu64" directories\n",
725 t->numfile, t->numdir);
726 d_printf("Total bytes written: %"PRIu64" (%.1f MiB/s)\n",
727 t->total_size,
728 t->total_size/timespec_elapsed2(&tp_start, &tp_end)/1024/1024);
730 out_close:
732 if (!t->mode.dry) {
733 r = archive_write_close(t->archive);
734 if (r != ARCHIVE_OK) {
735 d_printf("Fatal: %s\n", archive_error_string(t->archive));
736 err = 1;
737 goto out;
740 out:
741 #ifdef HAVE_ARCHIVE_READ_FREE
742 archive_write_free(t->archive);
743 #else
744 archive_write_finish(t->archive);
745 #endif
746 talloc_free(ctx);
747 return err;
751 * tar_create_from_list - fetch from path list in include mode
753 static int tar_create_from_list(struct tar *t)
755 int err = 0;
756 NTSTATUS status;
757 char *base;
758 const char *path, *mask, *start_dir;
759 int i;
760 TALLOC_CTX *ctx = talloc_new(NULL);
761 if (ctx == NULL) {
762 return 1;
765 start_dir = talloc_strdup(ctx, client_get_cur_dir());
766 if (start_dir == NULL) {
767 err = 1;
768 goto out;
771 for (i = 0; i < t->path_list_size; i++) {
772 path = t->path_list[i];
773 base = NULL;
774 status = path_base_name(ctx, path, &base);
775 if (!NT_STATUS_IS_OK(status)) {
776 err = 1;
777 goto out;
779 mask = talloc_asprintf(ctx, "%s\\%s",
780 client_get_cur_dir(), path);
781 if (mask == NULL) {
782 err = 1;
783 goto out;
785 mask = client_clean_name(ctx, mask);
786 if (mask == NULL) {
787 err = 1;
788 goto out;
791 DBG(5, ("incl. path='%s', base='%s', mask='%s'\n",
792 path, base ? base : "NULL", mask));
794 if (base != NULL) {
795 base = talloc_asprintf(ctx, "%s%s\\",
796 client_get_cur_dir(), base);
797 if (base == NULL) {
798 err = 1;
799 goto out;
801 base = client_clean_name(ctx, base);
802 if (base == NULL) {
803 err = 1;
804 goto out;
807 DBG(5, ("cd '%s' before do_list\n", base));
808 client_set_cur_dir(base);
810 status = do_list(mask, TAR_DO_LIST_ATTR, get_file_callback, true, true);
811 if (base != NULL) {
812 client_set_cur_dir(start_dir);
814 if (!NT_STATUS_IS_OK(status)) {
815 DBG(0, ("do_list failed on %s (%s)\n", path, nt_errstr(status)));
816 err = 1;
817 goto out;
821 out:
822 talloc_free(ctx);
823 return err;
827 * get_file_callback - do_list callback
829 * Callback for client.c do_list(). Called for each file found on the
830 * share matching do_list mask. Recursively call do_list() with itself
831 * as callback when the current file is a directory.
833 static NTSTATUS get_file_callback(struct cli_state *cli,
834 struct file_info *finfo,
835 const char *dir)
837 NTSTATUS status = NT_STATUS_OK;
838 char *remote_name;
839 char *old_dir = NULL;
840 char *new_dir = NULL;
841 const char *initial_dir = dir;
842 bool skip = false;
843 bool isdir;
844 int rc;
845 TALLOC_CTX *ctx = talloc_new(NULL);
846 if (ctx == NULL) {
847 return NT_STATUS_NO_MEMORY;
850 remote_name = talloc_asprintf(ctx, "%s\\%s", initial_dir, finfo->name);
851 if (remote_name == NULL) {
852 status = NT_STATUS_NO_MEMORY;
853 goto out;
855 remote_name = client_clean_name(ctx, remote_name);
856 if (remote_name == NULL) {
857 status = NT_STATUS_NO_MEMORY;
858 goto out;
861 if (strequal(finfo->name, "..") || strequal(finfo->name, ".")) {
862 goto out;
865 isdir = finfo->attr & FILE_ATTRIBUTE_DIRECTORY;
866 if (isdir) {
867 old_dir = talloc_strdup(ctx, initial_dir);
868 new_dir = talloc_asprintf(ctx, "%s\\", remote_name);
869 if ((old_dir == NULL) || (new_dir == NULL)) {
870 status = NT_STATUS_NO_MEMORY;
871 goto out;
875 status = tar_create_skip_path(&tar_ctx,
876 isdir ? new_dir : remote_name,
877 finfo, &skip);
878 if (!NT_STATUS_IS_OK(status)) {
879 goto out;
882 if (skip) {
883 DBG(5, ("--- %s\n", remote_name));
884 status = NT_STATUS_OK;
885 goto out;
888 rc = tar_get_file(&tar_ctx, remote_name, finfo);
889 if (rc != 0) {
890 status = NT_STATUS_UNSUCCESSFUL;
891 goto out;
894 out:
895 talloc_free(ctx);
896 return status;
900 * tar_get_file - fetch a remote file to the local archive
901 * @full_dos_path: path to the file to fetch
902 * @finfo: attributes of the file to fetch
904 static int tar_get_file(struct tar *t,
905 const char *full_dos_path,
906 struct file_info *finfo)
908 extern struct cli_state *cli;
909 NTSTATUS status;
910 struct archive_entry *entry;
911 char *full_unix_path;
912 char buf[TAR_CLI_READ_SIZE];
913 size_t len;
914 uint64_t off = 0;
915 uint16_t remote_fd = (uint16_t)-1;
916 int err = 0, r;
917 const bool isdir = finfo->attr & FILE_ATTRIBUTE_DIRECTORY;
918 TALLOC_CTX *ctx = talloc_new(NULL);
920 if (ctx == NULL) {
921 return 1;
924 DBG(5, ("+++ %s\n", full_dos_path));
926 t->total_size += finfo->size;
928 if (t->mode.dry) {
929 goto out;
932 if (t->mode.reset) {
933 /* ignore return value: server might not store DOS attributes */
934 set_remote_attr(full_dos_path, FILE_ATTRIBUTE_ARCHIVE, ATTR_UNSET);
937 full_unix_path = talloc_asprintf(ctx, ".%s", full_dos_path);
938 if (full_unix_path == NULL) {
939 err = 1;
940 goto out;
942 string_replace(full_unix_path, '\\', '/');
943 entry = archive_entry_new();
944 archive_entry_copy_pathname(entry, full_unix_path);
945 archive_entry_set_filetype(entry, isdir ? AE_IFDIR : AE_IFREG);
946 archive_entry_set_atime(entry,
947 finfo->atime_ts.tv_sec,
948 finfo->atime_ts.tv_nsec);
949 archive_entry_set_mtime(entry,
950 finfo->mtime_ts.tv_sec,
951 finfo->mtime_ts.tv_nsec);
952 archive_entry_set_ctime(entry,
953 finfo->ctime_ts.tv_sec,
954 finfo->ctime_ts.tv_nsec);
955 archive_entry_set_perm(entry, isdir ? 0755 : 0644);
957 * check if we can safely cast unsigned file size to libarchive
958 * signed size. Very unlikely problem (>9 exabyte file)
960 if (finfo->size > INT64_MAX) {
961 d_printf("Remote file %s too big\n", full_dos_path);
962 goto out_entry;
965 archive_entry_set_size(entry, (int64_t)finfo->size);
967 if (isdir) {
968 /* It's a directory just write a header */
969 r = archive_write_header(t->archive, entry);
970 if (r != ARCHIVE_OK) {
971 d_printf("Fatal: %s\n", archive_error_string(t->archive));
972 err = 1;
974 if (t->mode.verbose) {
975 d_printf("a %s\\\n", full_dos_path);
977 DBG(5, ("get_file skip dir %s\n", full_dos_path));
978 goto out_entry;
981 status = cli_open(cli, full_dos_path, O_RDONLY, DENY_NONE, &remote_fd);
982 if (!NT_STATUS_IS_OK(status)) {
983 d_printf("%s opening remote file %s\n",
984 nt_errstr(status), full_dos_path);
985 goto out_entry;
988 /* don't make tar file entry until after the file is open */
989 r = archive_write_header(t->archive, entry);
990 if (r != ARCHIVE_OK) {
991 d_printf("Fatal: %s\n", archive_error_string(t->archive));
992 err = 1;
993 goto out_entry;
996 if (t->mode.verbose) {
997 d_printf("a %s\n", full_dos_path);
1000 do {
1001 status = cli_read(cli, remote_fd, buf, off, sizeof(buf), &len);
1002 if (!NT_STATUS_IS_OK(status)) {
1003 d_printf("Error reading file %s : %s\n",
1004 full_dos_path, nt_errstr(status));
1005 err = 1;
1006 goto out_close;
1009 off += len;
1011 r = archive_write_data(t->archive, buf, len);
1012 if (r < 0) {
1013 d_printf("Fatal: %s\n", archive_error_string(t->archive));
1014 err = 1;
1015 goto out_close;
1018 } while (off < finfo->size);
1019 t->numfile++;
1021 out_close:
1022 cli_close(cli, remote_fd);
1024 out_entry:
1025 archive_entry_free(entry);
1027 out:
1028 talloc_free(ctx);
1029 return err;
1033 * tar_extract - open archive and send files.
1035 static int tar_extract(struct tar *t)
1037 int err = 0;
1038 int r;
1039 struct archive_entry *entry;
1040 const size_t bsize = t->mode.blocksize * TAR_BLOCK_UNIT;
1041 int rc;
1043 t->archive = archive_read_new();
1044 archive_read_support_format_all(t->archive);
1045 #ifdef HAVE_ARCHIVE_READ_SUPPORT_FILTER_ALL
1046 archive_read_support_filter_all(t->archive);
1047 #endif
1049 if (strequal(t->tar_path, "-")) {
1050 r = archive_read_open_fd(t->archive, STDIN_FILENO, bsize);
1051 } else {
1052 r = archive_read_open_filename(t->archive, t->tar_path, bsize);
1055 if (r != ARCHIVE_OK) {
1056 d_printf("Can't open %s : %s\n", t->tar_path,
1057 archive_error_string(t->archive));
1058 err = 1;
1059 goto out;
1062 for (;;) {
1063 NTSTATUS status;
1064 bool skip = false;
1065 r = archive_read_next_header(t->archive, &entry);
1066 if (r == ARCHIVE_EOF) {
1067 break;
1069 if (r == ARCHIVE_WARN) {
1070 d_printf("Warning: %s\n", archive_error_string(t->archive));
1072 if (r == ARCHIVE_FATAL) {
1073 d_printf("Fatal: %s\n", archive_error_string(t->archive));
1074 err = 1;
1075 goto out;
1078 status = tar_extract_skip_path(t, entry, &skip);
1079 if (!NT_STATUS_IS_OK(status)) {
1080 err = 1;
1081 goto out;
1083 if (skip) {
1084 DBG(5, ("--- %s\n", archive_entry_pathname(entry)));
1085 continue;
1087 DBG(5, ("+++ %s\n", archive_entry_pathname(entry)));
1089 if (t->mode.verbose) {
1090 d_printf("x %s\n", archive_entry_pathname(entry));
1093 rc = tar_send_file(t, entry);
1094 if (rc != 0) {
1095 err = 1;
1096 goto out;
1100 out:
1101 #ifdef HAVE_ARCHIVE_READ_FREE
1102 r = archive_read_free(t->archive);
1103 #else
1104 r = archive_read_finish(t->archive);
1105 #endif
1106 if (r != ARCHIVE_OK) {
1107 d_printf("Can't close %s : %s\n", t->tar_path,
1108 archive_error_string(t->archive));
1109 err = 1;
1111 return err;
1115 * tar_send_file - send @entry to the remote server
1116 * @entry: current archive entry
1118 * Handle the creation of the parent directories and transfer the
1119 * entry to a new remote file.
1121 static int tar_send_file(struct tar *t, struct archive_entry *entry)
1123 extern struct cli_state *cli;
1124 char *dos_path;
1125 char *full_path;
1126 NTSTATUS status;
1127 uint16_t remote_fd = (uint16_t) -1;
1128 int err = 0;
1129 int flags = O_RDWR | O_CREAT | O_TRUNC;
1130 mode_t filetype = archive_entry_filetype(entry);
1131 mode_t mode = archive_entry_mode(entry);
1132 time_t mtime = archive_entry_mtime(entry);
1133 int rc;
1134 TALLOC_CTX *ctx = talloc_new(NULL);
1135 if (ctx == NULL) {
1136 return 1;
1139 dos_path = talloc_strdup(ctx, archive_entry_pathname(entry));
1140 if (dos_path == NULL) {
1141 err = 1;
1142 goto out;
1144 fix_unix_path(dos_path, true);
1146 full_path = talloc_strdup(ctx, client_get_cur_dir());
1147 if (full_path == NULL) {
1148 err = 1;
1149 goto out;
1151 full_path = talloc_strdup_append(full_path, dos_path);
1152 if (full_path == NULL) {
1153 err = 1;
1154 goto out;
1156 full_path = client_clean_name(ctx, full_path);
1157 if (full_path == NULL) {
1158 err = 1;
1159 goto out;
1162 if (filetype != AE_IFREG && filetype != AE_IFDIR) {
1163 d_printf("Skipping non-dir & non-regular file %s\n", full_path);
1164 goto out;
1167 rc = make_remote_path(full_path);
1168 if (rc != 0) {
1169 err = 1;
1170 goto out;
1173 if (filetype == AE_IFDIR) {
1174 goto out;
1177 status = cli_open(cli, full_path, flags, DENY_NONE, &remote_fd);
1178 if (!NT_STATUS_IS_OK(status)) {
1179 d_printf("Error opening remote file %s: %s\n",
1180 full_path, nt_errstr(status));
1181 err = 1;
1182 goto out;
1185 for (;;) {
1186 const void *buf;
1187 size_t len;
1188 off_t off;
1189 int r;
1191 r = archive_read_data_block(t->archive, &buf, &len, &off);
1192 if (r == ARCHIVE_EOF) {
1193 break;
1195 if (r == ARCHIVE_WARN) {
1196 d_printf("Warning: %s\n", archive_error_string(t->archive));
1198 if (r == ARCHIVE_FATAL) {
1199 d_printf("Fatal: %s\n", archive_error_string(t->archive));
1200 err = 1;
1201 goto close_out;
1204 status = cli_writeall(cli, remote_fd, 0, buf, off, len, NULL);
1205 if (!NT_STATUS_IS_OK(status)) {
1206 d_printf("Error writing remote file %s: %s\n",
1207 full_path, nt_errstr(status));
1208 err = 1;
1209 goto close_out;
1213 close_out:
1214 status = cli_close(cli, remote_fd);
1215 if (!NT_STATUS_IS_OK(status)) {
1216 d_printf("Error closing remote file %s: %s\n",
1217 full_path, nt_errstr(status));
1218 err = 1;
1221 status = cli_setatr(cli, full_path, mode, mtime);
1222 if (!NT_STATUS_IS_OK(status)) {
1223 d_printf("Error setting attributes on remote file %s: %s\n",
1224 full_path, nt_errstr(status));
1225 err = 1;
1228 out:
1229 talloc_free(ctx);
1230 return err;
1234 * tar_add_selection_path - add a path to the path list
1235 * @path: path to add
1237 static NTSTATUS tar_add_selection_path(struct tar *t, const char *path)
1239 const char **list;
1240 TALLOC_CTX *ctx = t->talloc_ctx;
1241 if (!t->path_list) {
1242 t->path_list = str_list_make_empty(ctx);
1243 if (t->path_list == NULL) {
1244 return NT_STATUS_NO_MEMORY;
1246 t->path_list_size = 0;
1249 /* cast to silence gcc const-qual warning */
1250 list = str_list_add((void *)t->path_list, path);
1251 if (list == NULL) {
1252 return NT_STATUS_NO_MEMORY;
1254 t->path_list = discard_const_p(char *, list);
1255 t->path_list_size++;
1256 fix_unix_path(t->path_list[t->path_list_size - 1], true);
1258 return NT_STATUS_OK;
1262 * tar_set_blocksize - set block size in TAR_BLOCK_UNIT
1264 static int tar_set_blocksize(struct tar *t, int size)
1266 if (size <= 0 || size > TAR_MAX_BLOCK_SIZE) {
1267 return 1;
1270 t->mode.blocksize = size;
1272 return 0;
1276 * tar_set_newer_than - set date threshold of saved files
1277 * @filename: local path to a file
1279 * Only files newer than the modification time of @filename will be
1280 * saved.
1282 * Note: this function set the global variable newer_than from
1283 * client.c. Thus the time is not a field of the tar structure. See
1284 * cmd_newer() to change its value from an interactive session.
1286 static int tar_set_newer_than(struct tar *t, const char *filename)
1288 extern time_t newer_than;
1289 SMB_STRUCT_STAT stbuf;
1290 int rc;
1292 rc = sys_stat(filename, &stbuf, false);
1293 if (rc != 0) {
1294 d_printf("Error setting newer-than time\n");
1295 return 1;
1298 newer_than = convert_timespec_to_time_t(stbuf.st_ex_mtime);
1299 DBG(1, ("Getting files newer than %s", time_to_asc(newer_than)));
1300 return 0;
1304 * tar_read_inclusion_file - set path list from file
1305 * @filename: path to the list file
1307 * Read and add each line of @filename to the path list.
1309 static int tar_read_inclusion_file(struct tar *t, const char* filename)
1311 char *line;
1312 int err = 0;
1313 int fd = -1;
1314 TALLOC_CTX *ctx = talloc_new(NULL);
1315 if (ctx == NULL) {
1316 return 1;
1319 fd = open(filename, O_RDONLY);
1320 if (fd < 0) {
1321 d_printf("Can't open inclusion file '%s': %s\n", filename, strerror(errno));
1322 err = 1;
1323 goto out;
1326 for (line = afdgets(fd, ctx, 0);
1327 line != NULL;
1328 line = afdgets(fd, ctx, 0)) {
1329 NTSTATUS status;
1330 status = tar_add_selection_path(t, line);
1331 if (!NT_STATUS_IS_OK(status)) {
1332 err = 1;
1333 goto out;
1337 out:
1338 if (fd != -1) {
1339 close(fd);
1341 talloc_free(ctx);
1342 return err;
1346 * tar_path_in_list - check whether @path is in the path list
1347 * @path: path to find
1348 * @reverse: when true also try to find path list element in @path
1349 * @_is_in_list: set if @path is in the path list
1351 * Look at each path of the path list and set @_is_in_list if @path is a
1352 * subpath of one of them.
1354 * If you want /path to be in the path list (path/a/, path/b/) set
1355 * @reverse to true to try to match the other way around.
1357 static NTSTATUS tar_path_in_list(struct tar *t, const char *path,
1358 bool reverse, bool *_is_in_list)
1360 int i;
1361 const char *p;
1362 const char *pattern;
1364 if (path == NULL || path[0] == '\0') {
1365 *_is_in_list = false;
1366 return NT_STATUS_OK;
1369 p = skip_useless_char_in_path(path);
1371 for (i = 0; i < t->path_list_size; i++) {
1372 bool is_in_list;
1373 NTSTATUS status;
1375 pattern = skip_useless_char_in_path(t->path_list[i]);
1376 status = is_subpath(p, pattern, &is_in_list);
1377 if (!NT_STATUS_IS_OK(status)) {
1378 return status;
1380 if (reverse && !is_in_list) {
1381 status = is_subpath(pattern, p, &is_in_list);
1382 if (!NT_STATUS_IS_OK(status)) {
1383 return status;
1386 if (is_in_list) {
1387 *_is_in_list = true;
1388 return NT_STATUS_OK;
1392 *_is_in_list = false;
1393 return NT_STATUS_OK;
1397 * tar_extract_skip_path - check if @entry should be skipped
1398 * @entry: current tar entry
1399 * @_skip: set true if path should be skipped, otherwise false
1401 * Skip predicate for tar extraction (archive to server) only.
1403 static NTSTATUS tar_extract_skip_path(struct tar *t,
1404 struct archive_entry *entry,
1405 bool *_skip)
1407 const char *fullpath = archive_entry_pathname(entry);
1408 bool in = true;
1410 if (t->path_list_size <= 0) {
1411 *_skip = false;
1412 return NT_STATUS_OK;
1415 if (t->mode.regex) {
1416 in = mask_match_list(fullpath, t->path_list, t->path_list_size, true);
1417 } else {
1418 NTSTATUS status = tar_path_in_list(t, fullpath, false, &in);
1419 if (!NT_STATUS_IS_OK(status)) {
1420 return status;
1424 if (t->mode.selection == TAR_EXCLUDE) {
1425 *_skip = in;
1426 } else {
1427 *_skip = !in;
1430 return NT_STATUS_OK;
1434 * tar_create_skip_path - check if @fullpath should be skipped
1435 * @fullpath: full remote path of the current file
1436 * @finfo: remote file attributes
1437 * @_skip: returned skip not
1439 * Skip predicate for tar creation (server to archive) only.
1441 static NTSTATUS tar_create_skip_path(struct tar *t,
1442 const char *fullpath,
1443 const struct file_info *finfo,
1444 bool *_skip)
1446 /* syntaxic sugar */
1447 const mode_t mode = finfo->attr;
1448 const bool isdir = mode & FILE_ATTRIBUTE_DIRECTORY;
1449 const bool exclude = t->mode.selection == TAR_EXCLUDE;
1450 bool in = true;
1452 if (!isdir) {
1454 /* 1. if we don't want X and we have X, skip */
1455 if (!t->mode.system && (mode & FILE_ATTRIBUTE_SYSTEM)) {
1456 *_skip = true;
1457 return NT_STATUS_OK;
1460 if (!t->mode.hidden && (mode & FILE_ATTRIBUTE_HIDDEN)) {
1461 *_skip = true;
1462 return NT_STATUS_OK;
1465 /* 2. if we only want archive and it's not, skip */
1467 if (t->mode.incremental && !(mode & FILE_ATTRIBUTE_ARCHIVE)) {
1468 *_skip = true;
1469 return NT_STATUS_OK;
1473 /* 3. is it in the selection list? */
1476 * tar_create_from_list() use the include list as a starting
1477 * point, no need to check
1479 if (!exclude) {
1480 *_skip = false;
1481 return NT_STATUS_OK;
1484 /* we are now in exclude mode */
1486 /* no matter the selection, no list => include everything */
1487 if (t->path_list_size <= 0) {
1488 *_skip = false;
1489 return NT_STATUS_OK;
1492 if (t->mode.regex) {
1493 in = mask_match_list(fullpath, t->path_list, t->path_list_size, true);
1494 } else {
1495 bool reverse = isdir && !exclude;
1496 NTSTATUS status = tar_path_in_list(t, fullpath, reverse, &in);
1497 if (!NT_STATUS_IS_OK(status)) {
1498 return status;
1501 *_skip = in;
1503 return NT_STATUS_OK;
1507 * tar_to_process - return true if @t is ready to be processed
1509 * @t is ready if it properly parsed command line arguments.
1511 bool tar_to_process(struct tar *t)
1513 if (t == NULL) {
1514 DBG_WARNING("Invalid tar context\n");
1515 return false;
1517 return t->to_process;
1521 * skip_useless_char_in_path - skip leading slashes/dots
1523 * Skip leading slashes, backslashes and dot-slashes.
1525 static const char* skip_useless_char_in_path(const char *p)
1527 while (p) {
1528 if (*p == '/' || *p == '\\') {
1529 p++;
1531 else if (p[0] == '.' && (p[1] == '/' || p[1] == '\\')) {
1532 p += 2;
1534 else
1535 return p;
1537 return p;
1541 * is_subpath - check if the path @sub is a subpath of @full.
1542 * @sub: path to test
1543 * @full: container path
1544 * @_subpath_match: set true if @sub is a subpath of @full, otherwise false
1546 * String comparison is case-insensitive.
1548 static NTSTATUS is_subpath(const char *sub, const char *full,
1549 bool *_subpath_match)
1551 NTSTATUS status = NT_STATUS_OK;
1552 int len = 0;
1553 char *f, *s;
1554 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
1555 if (tmp_ctx == NULL) {
1556 status = NT_STATUS_NO_MEMORY;
1557 goto out;
1560 f = strlower_talloc(tmp_ctx, full);
1561 if (f == NULL) {
1562 status = NT_STATUS_NO_MEMORY;
1563 goto out_ctx_free;
1565 string_replace(f, '\\', '/');
1566 s = strlower_talloc(tmp_ctx, sub);
1567 if (s == NULL) {
1568 status = NT_STATUS_NO_MEMORY;
1569 goto out_ctx_free;
1571 string_replace(s, '\\', '/');
1573 /* find the point where sub and full diverge */
1574 while ((*f != '\0') && (*s != '\0') && (*f == *s)) {
1575 f++;
1576 s++;
1577 len++;
1580 if ((*f == '\0') && (*s == '\0')) {
1581 *_subpath_match = true; /* sub and full match */
1582 goto out_ctx_free;
1585 if ((*f == '\0') && (len > 0) && (*(f - 1) == '/')) {
1586 /* sub diverges from full at path separator */
1587 *_subpath_match = true;
1588 goto out_ctx_free;
1591 if ((*s == '\0') && (strcmp(f, "/") == 0)) {
1592 /* full diverges from sub with trailing slash only */
1593 *_subpath_match = true;
1594 goto out_ctx_free;
1597 if ((*s == '/') && (*f == '\0')) {
1598 /* sub diverges from full with extra path component */
1599 *_subpath_match = true;
1600 goto out_ctx_free;
1602 *_subpath_match = false;
1604 out_ctx_free:
1605 talloc_free(tmp_ctx);
1606 out:
1607 return status;
1612 * make_remote_path - recursively make remote dirs
1613 * @full_path: full hierarchy to create
1615 * Create @full_path and each parent directories as needed.
1617 static int make_remote_path(const char *full_path)
1619 extern struct cli_state *cli;
1620 char *path;
1621 char *subpath;
1622 char *state;
1623 char *last_backslash;
1624 char *p;
1625 int len;
1626 NTSTATUS status;
1627 int err = 0;
1628 TALLOC_CTX *ctx = talloc_new(NULL);
1629 if (ctx == NULL) {
1630 return 1;
1633 subpath = talloc_strdup(ctx, full_path);
1634 if (subpath == NULL) {
1635 err = 1;
1636 goto out;
1638 path = talloc_strdup(ctx, full_path);
1639 if (path == NULL) {
1640 err = 1;
1641 goto out;
1643 len = talloc_get_size(path) - 1;
1645 last_backslash = strrchr_m(path, '\\');
1646 if (last_backslash == NULL) {
1647 goto out;
1650 *last_backslash = 0;
1652 subpath[0] = 0;
1653 p = strtok_r(path, "\\", &state);
1655 while (p != NULL) {
1656 strlcat(subpath, p, len);
1657 status = cli_chkpath(cli, subpath);
1658 if (!NT_STATUS_IS_OK(status)) {
1659 status = cli_mkdir(cli, subpath);
1660 if (!NT_STATUS_IS_OK(status)) {
1661 d_printf("Can't mkdir %s: %s\n", subpath, nt_errstr(status));
1662 err = 1;
1663 goto out;
1665 DBG(3, ("mkdir %s\n", subpath));
1668 strlcat(subpath, "\\", len);
1669 p = strtok_r(NULL, "/\\", &state);
1673 out:
1674 talloc_free(ctx);
1675 return err;
1679 * tar_reset_mem_context - reset talloc context associated with @t
1681 * At the start of the program the context is NULL so a new one is
1682 * allocated. On the following runs (interactive session only), simply
1683 * free the children.
1685 static TALLOC_CTX *tar_reset_mem_context(struct tar *t)
1687 tar_free_mem_context(t);
1688 t->talloc_ctx = talloc_new(NULL);
1689 return t->talloc_ctx;
1693 * tar_free_mem_context - free talloc context associated with @t
1695 static void tar_free_mem_context(struct tar *t)
1697 if (t->talloc_ctx) {
1698 talloc_free(t->talloc_ctx);
1699 t->talloc_ctx = NULL;
1700 t->path_list_size = 0;
1701 t->path_list = NULL;
1702 t->tar_path = NULL;
1706 #define XSET(v) [v] = #v
1707 #define XTABLE(v, t) DBG(2, ("DUMP:%-20.20s = %s\n", #v, t[v]))
1708 #define XBOOL(v) DBG(2, ("DUMP:%-20.20s = %d\n", #v, v ? 1 : 0))
1709 #define XSTR(v) DBG(2, ("DUMP:%-20.20s = %s\n", #v, v ? v : "NULL"))
1710 #define XINT(v) DBG(2, ("DUMP:%-20.20s = %d\n", #v, v))
1711 #define XUINT64(v) DBG(2, ("DUMP:%-20.20s = %" PRIu64 "\n", #v, v))
1714 * tar_dump - dump tar structure on stdout
1716 static void tar_dump(struct tar *t)
1718 int i;
1719 const char* op[] = {
1720 XSET(TAR_NO_OPERATION),
1721 XSET(TAR_CREATE),
1722 XSET(TAR_EXTRACT),
1725 const char* sel[] = {
1726 XSET(TAR_NO_SELECTION),
1727 XSET(TAR_INCLUDE),
1728 XSET(TAR_EXCLUDE),
1731 XBOOL(t->to_process);
1732 XTABLE(t->mode.operation, op);
1733 XTABLE(t->mode.selection, sel);
1734 XINT(t->mode.blocksize);
1735 XBOOL(t->mode.hidden);
1736 XBOOL(t->mode.system);
1737 XBOOL(t->mode.incremental);
1738 XBOOL(t->mode.reset);
1739 XBOOL(t->mode.dry);
1740 XBOOL(t->mode.verbose);
1741 XUINT64(t->total_size);
1742 XSTR(t->tar_path);
1743 XINT(t->path_list_size);
1745 for (i = 0; t->path_list && t->path_list[i]; i++) {
1746 DBG(2, ("DUMP: t->path_list[%2d] = %s\n", i, t->path_list[i]));
1749 DBG(2, ("DUMP:t->path_list @ %p (%d elem)\n", t->path_list, i));
1751 #undef XSET
1752 #undef XTABLE
1753 #undef XBOOL
1754 #undef XSTR
1755 #undef XINT
1758 * max_token - return upper limit for the number of token in @str
1760 * The result is not exact, the actual number of token might be less
1761 * than what is returned.
1763 static int max_token(const char *str)
1765 const char *s;
1766 int nb = 0;
1768 if (str == NULL) {
1769 return 0;
1772 s = str;
1773 while (s[0] != '\0') {
1774 if (isspace((int)s[0])) {
1775 nb++;
1777 s++;
1780 nb++;
1782 return nb;
1786 * fix_unix_path - convert @path to a DOS path
1787 * @path: path to convert
1788 * @removeprefix: if true, remove leading ./ or /.
1790 static char *fix_unix_path(char *path, bool do_remove_prefix)
1792 char *from = path, *to = path;
1794 if (path == NULL || path[0] == '\0') {
1795 return path;
1798 /* remove prefix:
1799 * ./path => path
1800 * /path => path
1802 if (do_remove_prefix) {
1803 /* /path */
1804 if (path[0] == '/' || path[0] == '\\') {
1805 from += 1;
1808 /* ./path */
1809 if (path[1] != '\0' && path[0] == '.' && (path[1] == '/' || path[1] == '\\')) {
1810 from += 2;
1814 /* replace / with \ */
1815 while (from[0] != '\0') {
1816 if (from[0] == '/') {
1817 to[0] = '\\';
1818 } else {
1819 to[0] = from[0];
1822 from++;
1823 to++;
1825 to[0] = '\0';
1827 return path;
1831 * path_base_name - return @path basename
1833 * If @path doesn't contain any directory separator return NULL.
1835 static NTSTATUS path_base_name(TALLOC_CTX *ctx, const char *path, char **_base)
1837 char *base = NULL;
1838 int last = -1;
1839 int i;
1841 for (i = 0; path[i]; i++) {
1842 if (path[i] == '\\' || path[i] == '/') {
1843 last = i;
1847 if (last >= 0) {
1848 base = talloc_strdup(ctx, path);
1849 if (base == NULL) {
1850 return NT_STATUS_NO_MEMORY;
1853 base[last] = 0;
1856 *_base = base;
1857 return NT_STATUS_OK;
1860 #else
1862 #define NOT_IMPLEMENTED DEBUG(0, ("tar mode not compiled. build used --without-libarchive\n"))
1864 int cmd_block(void)
1866 NOT_IMPLEMENTED;
1867 return 1;
1870 int cmd_tarmode(void)
1872 NOT_IMPLEMENTED;
1873 return 1;
1876 int cmd_tar(void)
1878 NOT_IMPLEMENTED;
1879 return 1;
1882 int tar_process(struct tar* tar)
1884 NOT_IMPLEMENTED;
1885 return 1;
1888 int tar_parse_args(struct tar *tar, const char *flag, const char **val, int valsize)
1890 NOT_IMPLEMENTED;
1891 return 1;
1894 bool tar_to_process(struct tar *tar)
1896 return false;
1899 struct tar *tar_get_ctx()
1901 return NULL;
1904 #endif