vfs_ceph_new: Remove unused symbol for ceph_readdir
[Samba.git] / source3 / client / clitar.c
blobe034a4cb2619f3a91cd5fc29f1e9d42a35e54720
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 char *timestr = timestring(talloc_tos(), mtime);
1224 d_printf("Error setting attributes (mode: %3o, mtime: %s) on "
1225 "remote file %s: %s\n",
1226 mode & 0777,
1227 timestr,
1228 full_path,
1229 nt_errstr(status));
1230 TALLOC_FREE(timestr);
1231 err = 1;
1234 out:
1235 talloc_free(ctx);
1236 return err;
1240 * tar_add_selection_path - add a path to the path list
1241 * @path: path to add
1243 static NTSTATUS tar_add_selection_path(struct tar *t, const char *path)
1245 const char **list;
1246 TALLOC_CTX *ctx = t->talloc_ctx;
1247 if (!t->path_list) {
1248 t->path_list = str_list_make_empty(ctx);
1249 if (t->path_list == NULL) {
1250 return NT_STATUS_NO_MEMORY;
1252 t->path_list_size = 0;
1255 /* cast to silence gcc const-qual warning */
1256 list = str_list_add((void *)t->path_list, path);
1257 if (list == NULL) {
1258 return NT_STATUS_NO_MEMORY;
1260 t->path_list = discard_const_p(char *, list);
1261 t->path_list_size++;
1262 fix_unix_path(t->path_list[t->path_list_size - 1], true);
1264 return NT_STATUS_OK;
1268 * tar_set_blocksize - set block size in TAR_BLOCK_UNIT
1270 static int tar_set_blocksize(struct tar *t, int size)
1272 if (size <= 0 || size > TAR_MAX_BLOCK_SIZE) {
1273 return 1;
1276 t->mode.blocksize = size;
1278 return 0;
1282 * tar_set_newer_than - set date threshold of saved files
1283 * @filename: local path to a file
1285 * Only files newer than the modification time of @filename will be
1286 * saved.
1288 * Note: this function set the global variable newer_than from
1289 * client.c. Thus the time is not a field of the tar structure. See
1290 * cmd_newer() to change its value from an interactive session.
1292 static int tar_set_newer_than(struct tar *t, const char *filename)
1294 extern time_t newer_than;
1295 SMB_STRUCT_STAT stbuf;
1296 int rc;
1298 rc = sys_stat(filename, &stbuf, false);
1299 if (rc != 0) {
1300 d_printf("Error setting newer-than time\n");
1301 return 1;
1304 newer_than = convert_timespec_to_time_t(stbuf.st_ex_mtime);
1305 DBG(1, ("Getting files newer than %s", time_to_asc(newer_than)));
1306 return 0;
1310 * tar_read_inclusion_file - set path list from file
1311 * @filename: path to the list file
1313 * Read and add each line of @filename to the path list.
1315 static int tar_read_inclusion_file(struct tar *t, const char* filename)
1317 char *line;
1318 int err = 0;
1319 int fd = -1;
1320 TALLOC_CTX *ctx = talloc_new(NULL);
1321 if (ctx == NULL) {
1322 return 1;
1325 fd = open(filename, O_RDONLY);
1326 if (fd < 0) {
1327 d_printf("Can't open inclusion file '%s': %s\n", filename, strerror(errno));
1328 err = 1;
1329 goto out;
1332 for (line = afdgets(fd, ctx, 0);
1333 line != NULL;
1334 line = afdgets(fd, ctx, 0)) {
1335 NTSTATUS status;
1336 status = tar_add_selection_path(t, line);
1337 if (!NT_STATUS_IS_OK(status)) {
1338 err = 1;
1339 goto out;
1343 out:
1344 if (fd != -1) {
1345 close(fd);
1347 talloc_free(ctx);
1348 return err;
1352 * tar_path_in_list - check whether @path is in the path list
1353 * @path: path to find
1354 * @reverse: when true also try to find path list element in @path
1355 * @_is_in_list: set if @path is in the path list
1357 * Look at each path of the path list and set @_is_in_list if @path is a
1358 * subpath of one of them.
1360 * If you want /path to be in the path list (path/a/, path/b/) set
1361 * @reverse to true to try to match the other way around.
1363 static NTSTATUS tar_path_in_list(struct tar *t, const char *path,
1364 bool reverse, bool *_is_in_list)
1366 int i;
1367 const char *p;
1368 const char *pattern;
1370 if (path == NULL || path[0] == '\0') {
1371 *_is_in_list = false;
1372 return NT_STATUS_OK;
1375 p = skip_useless_char_in_path(path);
1377 for (i = 0; i < t->path_list_size; i++) {
1378 bool is_in_list;
1379 NTSTATUS status;
1381 pattern = skip_useless_char_in_path(t->path_list[i]);
1382 status = is_subpath(p, pattern, &is_in_list);
1383 if (!NT_STATUS_IS_OK(status)) {
1384 return status;
1386 if (reverse && !is_in_list) {
1387 status = is_subpath(pattern, p, &is_in_list);
1388 if (!NT_STATUS_IS_OK(status)) {
1389 return status;
1392 if (is_in_list) {
1393 *_is_in_list = true;
1394 return NT_STATUS_OK;
1398 *_is_in_list = false;
1399 return NT_STATUS_OK;
1403 * tar_extract_skip_path - check if @entry should be skipped
1404 * @entry: current tar entry
1405 * @_skip: set true if path should be skipped, otherwise false
1407 * Skip predicate for tar extraction (archive to server) only.
1409 static NTSTATUS tar_extract_skip_path(struct tar *t,
1410 struct archive_entry *entry,
1411 bool *_skip)
1413 const char *fullpath = archive_entry_pathname(entry);
1414 bool in = true;
1416 if (t->path_list_size <= 0) {
1417 *_skip = false;
1418 return NT_STATUS_OK;
1421 if (t->mode.regex) {
1422 in = mask_match_list(fullpath, t->path_list, t->path_list_size, true);
1423 } else {
1424 NTSTATUS status = tar_path_in_list(t, fullpath, false, &in);
1425 if (!NT_STATUS_IS_OK(status)) {
1426 return status;
1430 if (t->mode.selection == TAR_EXCLUDE) {
1431 *_skip = in;
1432 } else {
1433 *_skip = !in;
1436 return NT_STATUS_OK;
1440 * tar_create_skip_path - check if @fullpath should be skipped
1441 * @fullpath: full remote path of the current file
1442 * @finfo: remote file attributes
1443 * @_skip: returned skip not
1445 * Skip predicate for tar creation (server to archive) only.
1447 static NTSTATUS tar_create_skip_path(struct tar *t,
1448 const char *fullpath,
1449 const struct file_info *finfo,
1450 bool *_skip)
1452 /* syntaxic sugar */
1453 const mode_t mode = finfo->attr;
1454 const bool isdir = mode & FILE_ATTRIBUTE_DIRECTORY;
1455 const bool exclude = t->mode.selection == TAR_EXCLUDE;
1456 bool in = true;
1458 if (!isdir) {
1460 /* 1. if we don't want X and we have X, skip */
1461 if (!t->mode.system && (mode & FILE_ATTRIBUTE_SYSTEM)) {
1462 *_skip = true;
1463 return NT_STATUS_OK;
1466 if (!t->mode.hidden && (mode & FILE_ATTRIBUTE_HIDDEN)) {
1467 *_skip = true;
1468 return NT_STATUS_OK;
1471 /* 2. if we only want archive and it's not, skip */
1473 if (t->mode.incremental && !(mode & FILE_ATTRIBUTE_ARCHIVE)) {
1474 *_skip = true;
1475 return NT_STATUS_OK;
1479 /* 3. is it in the selection list? */
1482 * tar_create_from_list() use the include list as a starting
1483 * point, no need to check
1485 if (!exclude) {
1486 *_skip = false;
1487 return NT_STATUS_OK;
1490 /* we are now in exclude mode */
1492 /* no matter the selection, no list => include everything */
1493 if (t->path_list_size <= 0) {
1494 *_skip = false;
1495 return NT_STATUS_OK;
1498 if (t->mode.regex) {
1499 in = mask_match_list(fullpath, t->path_list, t->path_list_size, true);
1500 } else {
1501 bool reverse = isdir && !exclude;
1502 NTSTATUS status = tar_path_in_list(t, fullpath, reverse, &in);
1503 if (!NT_STATUS_IS_OK(status)) {
1504 return status;
1507 *_skip = in;
1509 return NT_STATUS_OK;
1513 * tar_to_process - return true if @t is ready to be processed
1515 * @t is ready if it properly parsed command line arguments.
1517 bool tar_to_process(struct tar *t)
1519 if (t == NULL) {
1520 DBG_WARNING("Invalid tar context\n");
1521 return false;
1523 return t->to_process;
1527 * skip_useless_char_in_path - skip leading slashes/dots
1529 * Skip leading slashes, backslashes and dot-slashes.
1531 static const char* skip_useless_char_in_path(const char *p)
1533 while (p) {
1534 if (*p == '/' || *p == '\\') {
1535 p++;
1537 else if (p[0] == '.' && (p[1] == '/' || p[1] == '\\')) {
1538 p += 2;
1540 else
1541 return p;
1543 return p;
1547 * is_subpath - check if the path @sub is a subpath of @full.
1548 * @sub: path to test
1549 * @full: container path
1550 * @_subpath_match: set true if @sub is a subpath of @full, otherwise false
1552 * String comparison is case-insensitive.
1554 static NTSTATUS is_subpath(const char *sub, const char *full,
1555 bool *_subpath_match)
1557 NTSTATUS status = NT_STATUS_OK;
1558 int len = 0;
1559 char *f, *s;
1560 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
1561 if (tmp_ctx == NULL) {
1562 status = NT_STATUS_NO_MEMORY;
1563 goto out;
1566 f = strlower_talloc(tmp_ctx, full);
1567 if (f == NULL) {
1568 status = NT_STATUS_NO_MEMORY;
1569 goto out_ctx_free;
1571 string_replace(f, '\\', '/');
1572 s = strlower_talloc(tmp_ctx, sub);
1573 if (s == NULL) {
1574 status = NT_STATUS_NO_MEMORY;
1575 goto out_ctx_free;
1577 string_replace(s, '\\', '/');
1579 /* find the point where sub and full diverge */
1580 while ((*f != '\0') && (*s != '\0') && (*f == *s)) {
1581 f++;
1582 s++;
1583 len++;
1586 if ((*f == '\0') && (*s == '\0')) {
1587 *_subpath_match = true; /* sub and full match */
1588 goto out_ctx_free;
1591 if ((*f == '\0') && (len > 0) && (*(f - 1) == '/')) {
1592 /* sub diverges from full at path separator */
1593 *_subpath_match = true;
1594 goto out_ctx_free;
1597 if ((*s == '\0') && (strcmp(f, "/") == 0)) {
1598 /* full diverges from sub with trailing slash only */
1599 *_subpath_match = true;
1600 goto out_ctx_free;
1603 if ((*s == '/') && (*f == '\0')) {
1604 /* sub diverges from full with extra path component */
1605 *_subpath_match = true;
1606 goto out_ctx_free;
1608 *_subpath_match = false;
1610 out_ctx_free:
1611 talloc_free(tmp_ctx);
1612 out:
1613 return status;
1618 * make_remote_path - recursively make remote dirs
1619 * @full_path: full hierarchy to create
1621 * Create @full_path and each parent directories as needed.
1623 static int make_remote_path(const char *full_path)
1625 extern struct cli_state *cli;
1626 char *path;
1627 char *subpath;
1628 char *state;
1629 char *last_backslash;
1630 char *p;
1631 int len;
1632 NTSTATUS status;
1633 int err = 0;
1634 TALLOC_CTX *ctx = talloc_new(NULL);
1635 if (ctx == NULL) {
1636 return 1;
1639 subpath = talloc_strdup(ctx, full_path);
1640 if (subpath == NULL) {
1641 err = 1;
1642 goto out;
1644 path = talloc_strdup(ctx, full_path);
1645 if (path == NULL) {
1646 err = 1;
1647 goto out;
1649 len = talloc_get_size(path) - 1;
1651 last_backslash = strrchr_m(path, '\\');
1652 if (last_backslash == NULL) {
1653 goto out;
1656 *last_backslash = 0;
1658 subpath[0] = 0;
1659 p = strtok_r(path, "\\", &state);
1661 while (p != NULL) {
1662 strlcat(subpath, p, len);
1663 status = cli_chkpath(cli, subpath);
1664 if (!NT_STATUS_IS_OK(status)) {
1665 status = cli_mkdir(cli, subpath);
1666 if (!NT_STATUS_IS_OK(status)) {
1667 d_printf("Can't mkdir %s: %s\n", subpath, nt_errstr(status));
1668 err = 1;
1669 goto out;
1671 DBG(3, ("mkdir %s\n", subpath));
1674 strlcat(subpath, "\\", len);
1675 p = strtok_r(NULL, "/\\", &state);
1679 out:
1680 talloc_free(ctx);
1681 return err;
1685 * tar_reset_mem_context - reset talloc context associated with @t
1687 * At the start of the program the context is NULL so a new one is
1688 * allocated. On the following runs (interactive session only), simply
1689 * free the children.
1691 static TALLOC_CTX *tar_reset_mem_context(struct tar *t)
1693 tar_free_mem_context(t);
1694 t->talloc_ctx = talloc_new(NULL);
1695 return t->talloc_ctx;
1699 * tar_free_mem_context - free talloc context associated with @t
1701 static void tar_free_mem_context(struct tar *t)
1703 if (t->talloc_ctx) {
1704 talloc_free(t->talloc_ctx);
1705 t->talloc_ctx = NULL;
1706 t->path_list_size = 0;
1707 t->path_list = NULL;
1708 t->tar_path = NULL;
1712 #define XSET(v) [v] = #v
1713 #define XTABLE(v, t) DBG(2, ("DUMP:%-20.20s = %s\n", #v, t[v]))
1714 #define XBOOL(v) DBG(2, ("DUMP:%-20.20s = %d\n", #v, v ? 1 : 0))
1715 #define XSTR(v) DBG(2, ("DUMP:%-20.20s = %s\n", #v, v ? v : "NULL"))
1716 #define XINT(v) DBG(2, ("DUMP:%-20.20s = %d\n", #v, v))
1717 #define XUINT64(v) DBG(2, ("DUMP:%-20.20s = %" PRIu64 "\n", #v, v))
1720 * tar_dump - dump tar structure on stdout
1722 static void tar_dump(struct tar *t)
1724 int i;
1725 const char* op[] = {
1726 XSET(TAR_NO_OPERATION),
1727 XSET(TAR_CREATE),
1728 XSET(TAR_EXTRACT),
1731 const char* sel[] = {
1732 XSET(TAR_NO_SELECTION),
1733 XSET(TAR_INCLUDE),
1734 XSET(TAR_EXCLUDE),
1737 XBOOL(t->to_process);
1738 XTABLE(t->mode.operation, op);
1739 XTABLE(t->mode.selection, sel);
1740 XINT(t->mode.blocksize);
1741 XBOOL(t->mode.hidden);
1742 XBOOL(t->mode.system);
1743 XBOOL(t->mode.incremental);
1744 XBOOL(t->mode.reset);
1745 XBOOL(t->mode.dry);
1746 XBOOL(t->mode.verbose);
1747 XUINT64(t->total_size);
1748 XSTR(t->tar_path);
1749 XINT(t->path_list_size);
1751 for (i = 0; t->path_list && t->path_list[i]; i++) {
1752 DBG(2, ("DUMP: t->path_list[%2d] = %s\n", i, t->path_list[i]));
1755 DBG(2, ("DUMP:t->path_list @ %p (%d elem)\n", t->path_list, i));
1757 #undef XSET
1758 #undef XTABLE
1759 #undef XBOOL
1760 #undef XSTR
1761 #undef XINT
1764 * max_token - return upper limit for the number of token in @str
1766 * The result is not exact, the actual number of token might be less
1767 * than what is returned.
1769 static int max_token(const char *str)
1771 const char *s;
1772 int nb = 0;
1774 if (str == NULL) {
1775 return 0;
1778 s = str;
1779 while (s[0] != '\0') {
1780 if (isspace((int)s[0])) {
1781 nb++;
1783 s++;
1786 nb++;
1788 return nb;
1792 * fix_unix_path - convert @path to a DOS path
1793 * @path: path to convert
1794 * @removeprefix: if true, remove leading ./ or /.
1796 static char *fix_unix_path(char *path, bool do_remove_prefix)
1798 char *from = path, *to = path;
1800 if (path == NULL || path[0] == '\0') {
1801 return path;
1804 /* remove prefix:
1805 * ./path => path
1806 * /path => path
1808 if (do_remove_prefix) {
1809 /* /path */
1810 if (path[0] == '/' || path[0] == '\\') {
1811 from += 1;
1814 /* ./path */
1815 if (path[1] != '\0' && path[0] == '.' && (path[1] == '/' || path[1] == '\\')) {
1816 from += 2;
1820 /* replace / with \ */
1821 while (from[0] != '\0') {
1822 if (from[0] == '/') {
1823 to[0] = '\\';
1824 } else {
1825 to[0] = from[0];
1828 from++;
1829 to++;
1831 to[0] = '\0';
1833 return path;
1837 * path_base_name - return @path basename
1839 * If @path doesn't contain any directory separator return NULL.
1841 static NTSTATUS path_base_name(TALLOC_CTX *ctx, const char *path, char **_base)
1843 char *base = NULL;
1844 int last = -1;
1845 int i;
1847 for (i = 0; path[i]; i++) {
1848 if (path[i] == '\\' || path[i] == '/') {
1849 last = i;
1853 if (last >= 0) {
1854 base = talloc_strdup(ctx, path);
1855 if (base == NULL) {
1856 return NT_STATUS_NO_MEMORY;
1859 base[last] = 0;
1862 *_base = base;
1863 return NT_STATUS_OK;
1866 #else
1868 #define NOT_IMPLEMENTED DEBUG(0, ("tar mode not compiled. build used --without-libarchive\n"))
1870 int cmd_block(void)
1872 NOT_IMPLEMENTED;
1873 return 1;
1876 int cmd_tarmode(void)
1878 NOT_IMPLEMENTED;
1879 return 1;
1882 int cmd_tar(void)
1884 NOT_IMPLEMENTED;
1885 return 1;
1888 int tar_process(struct tar* tar)
1890 NOT_IMPLEMENTED;
1891 return 1;
1894 int tar_parse_args(struct tar *tar, const char *flag, const char **val, int valsize)
1896 NOT_IMPLEMENTED;
1897 return 1;
1900 bool tar_to_process(struct tar *tar)
1902 return false;
1905 struct tar *tar_get_ctx()
1907 return NULL;
1910 #endif