s3-clitar: Improve readabilty of tar_read_inclusion_file().
[Samba/wip.git] / source3 / client / clitar.c
blobfa16c9b382c5fefd5bdf7ec5eb69d7e76a7e6f03
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 strigify __LINE__ (int) */
84 #define STR1(x) #x
85 #define STR2(x) STR1(x)
87 /* helper macro to die in case of NULL pointer */
88 #define PANIC_IF_NULL(x) \
89 _panic_if_null(x, __FILE__ ":" STR2(__LINE__) " (" #x ") == NULL\n")
91 /* prototype to silent gcc warning */
92 static inline void* _panic_if_null(void *p, const char *expr);
93 static inline void* _panic_if_null(void *p, const char *expr)
95 if (!p) {
96 smb_panic(expr);
98 return p;
102 * Number of byte in a block unit.
104 #define TAR_BLOCK_UNIT 512
107 * Default tar block size in TAR_BLOCK_UNIT.
109 #define TAR_DEFAULT_BLOCK_SIZE 20
112 * Maximum value for the blocksize field
114 #define TAR_MAX_BLOCK_SIZE 0xffff
117 * Size of the buffer used when downloading a file
119 #define TAR_CLI_READ_SIZE 0xff00
121 #define TAR_DO_LIST_ATTR (FILE_ATTRIBUTE_DIRECTORY \
122 | FILE_ATTRIBUTE_SYSTEM \
123 | FILE_ATTRIBUTE_HIDDEN)
126 enum tar_operation {
127 TAR_NO_OPERATION,
128 TAR_CREATE, /* c flag */
129 TAR_EXTRACT, /* x flag */
132 enum tar_selection {
133 TAR_NO_SELECTION,
134 TAR_INCLUDE, /* I and F flag, default */
135 TAR_EXCLUDE, /* X flag */
138 enum {
139 ATTR_UNSET,
140 ATTR_SET,
143 struct tar {
144 TALLOC_CTX *talloc_ctx;
146 /* in state that needs/can be processed? */
147 bool to_process;
149 /* flags */
150 struct tar_mode {
151 enum tar_operation operation; /* create, extract */
152 enum tar_selection selection; /* include, exclude */
153 int blocksize; /* size in TAR_BLOCK_UNIT of a tar file block */
154 bool hidden; /* backup hidden file? */
155 bool system; /* backup system file? */
156 bool incremental; /* backup _only_ archived file? */
157 bool reset; /* unset archive bit? */
158 bool dry; /* don't write tar file? */
159 bool regex; /* XXX: never actually using regex... */
160 bool verbose; /* XXX: ignored */
161 } mode;
163 /* nb of bytes received */
164 uint64_t total_size;
166 /* path to tar archive name */
167 char *tar_path;
169 /* list of path to include or exclude */
170 char **path_list;
171 int path_list_size;
173 /* archive handle */
174 struct archive *archive;
178 * Global context imported in client.c when needed.
180 * Default options.
182 struct tar tar_ctx = {
183 .mode.selection = TAR_INCLUDE,
184 .mode.blocksize = TAR_DEFAULT_BLOCK_SIZE,
185 .mode.hidden = true,
186 .mode.system = true,
187 .mode.incremental = false,
188 .mode.reset = false,
189 .mode.dry = false,
190 .mode.regex = false,
191 .mode.verbose = false,
194 /* tar, local function */
195 static int tar_create(struct tar* t);
196 static int tar_create_from_list(struct tar *t);
197 static int tar_extract(struct tar *t);
198 static int tar_read_inclusion_file (struct tar *t, const char* filename);
199 static int tar_send_file(struct tar *t, struct archive_entry *entry);
200 static int tar_set_blocksize(struct tar *t, int size);
201 static int tar_set_newer_than(struct tar *t, const char *filename);
202 static void tar_add_selection_path(struct tar *t, const char *path);
203 static void tar_dump(struct tar *t);
204 static bool tar_extract_skip_path(struct tar *t, struct archive_entry *entry);
205 static TALLOC_CTX *tar_reset_mem_context(struct tar *t);
206 static void tar_free_mem_context(struct tar *t);
207 static bool tar_create_skip_path(struct tar *t,
208 const char *fullpath,
209 const struct file_info *finfo);
211 static bool tar_path_in_list(struct tar *t,
212 const char *path,
213 bool reverse);
215 static int tar_get_file(struct tar *t,
216 const char *full_dos_path,
217 struct file_info *finfo);
219 static NTSTATUS get_file_callback(struct cli_state *cli,
220 struct file_info *finfo,
221 const char *dir);
223 /* utilities */
224 static char *fix_unix_path (char *path, bool removeprefix);
225 static char *path_base_name (const char *path);
226 static const char* skip_useless_char_in_path(const char *p);
227 static int make_remote_path(const char *full_path);
228 static int max_token (const char *str);
229 static bool is_subpath(const char *sub, const char *full);
230 static int set_remote_attr(const char *filename, uint16 new_attr, int mode);
233 * tar_get_ctx - retrieve global tar context handle
235 struct tar *tar_get_ctx()
237 return &tar_ctx;
241 * cmd_block - interactive command to change tar blocksize
243 * Read a size from the client command line and update the current
244 * blocksize.
246 int cmd_block(void)
248 /* XXX: from client.c */
249 const extern char *cmd_ptr;
250 char *buf;
251 TALLOC_CTX *ctx = PANIC_IF_NULL(talloc_new(NULL));
252 int err = 0;
253 bool ok;
255 ok = next_token_talloc(ctx, &cmd_ptr, &buf, NULL);
256 if (!ok) {
257 DBG(0, ("blocksize <n>\n"));
258 err = 1;
259 goto out;
262 ok = tar_set_blocksize(&tar_ctx, atoi(buf));
263 if (ok) {
264 DBG(0, ("invalid blocksize\n"));
265 err = 1;
266 goto out;
269 DBG(2, ("blocksize is now %d\n", tar_ctx.mode.blocksize));
271 out:
272 talloc_free(ctx);
273 return err;
277 * cmd_tarmode - interactive command to change tar behaviour
279 * Read one or more modes from the client command line and update the
280 * current tar mode.
282 int cmd_tarmode(void)
284 const extern char *cmd_ptr;
285 char *buf;
286 int i;
287 TALLOC_CTX *ctx = PANIC_IF_NULL(talloc_new(NULL));
289 struct {
290 const char *cmd;
291 bool *p;
292 bool value;
293 } table[] = {
294 {"full", &tar_ctx.mode.incremental, false},
295 {"inc", &tar_ctx.mode.incremental, true },
296 {"reset", &tar_ctx.mode.reset, true },
297 {"noreset", &tar_ctx.mode.reset, false},
298 {"system", &tar_ctx.mode.system, true },
299 {"nosystem", &tar_ctx.mode.system, false},
300 {"hidden", &tar_ctx.mode.hidden, true },
301 {"nohidden", &tar_ctx.mode.hidden, false},
302 {"verbose", &tar_ctx.mode.verbose, true },
303 {"noquiet", &tar_ctx.mode.verbose, true },
304 {"quiet", &tar_ctx.mode.verbose, false},
305 {"noverbose", &tar_ctx.mode.verbose, false},
308 while (next_token_talloc(ctx, &cmd_ptr, &buf, NULL)) {
309 for (i = 0; i < ARRAY_SIZE(table); i++) {
310 if (strequal(table[i].cmd, buf)) {
311 *table[i].p = table[i].value;
312 break;
316 if (i == ARRAY_SIZE(table))
317 DBG(0, ("tarmode: unrecognised option %s\n", buf));
320 DBG(0, ("tarmode is now %s, %s, %s, %s, %s\n",
321 tar_ctx.mode.incremental ? "incremental" : "full",
322 tar_ctx.mode.system ? "system" : "nosystem",
323 tar_ctx.mode.hidden ? "hidden" : "nohidden",
324 tar_ctx.mode.reset ? "reset" : "noreset",
325 tar_ctx.mode.verbose ? "verbose" : "quiet"));
327 talloc_free(ctx);
328 return 0;
332 * cmd_tar - interactive command to start a tar backup/restoration
334 * Check presence of argument, parse them and handle the request.
336 int cmd_tar(void)
338 TALLOC_CTX *ctx = PANIC_IF_NULL(talloc_new(NULL));
339 const extern char *cmd_ptr;
340 const char *flag;
341 const char **val;
342 char *buf;
343 int maxtok = max_token(cmd_ptr);
344 int i = 0;
345 int err = 0;
346 bool ok;
347 int rc;
349 ok = next_token_talloc(ctx, &cmd_ptr, &buf, NULL);
350 if (!ok) {
351 DBG(0, ("tar <c|x>[IXFbganN] [options] <tar file> [path list]\n"));
352 err = 1;
353 goto out;
356 flag = buf;
357 val = PANIC_IF_NULL(talloc_array(ctx, const char*, maxtok));
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 DBG(0, ("parse_args failed\n"));
366 err = 1;
367 goto out;
370 rc = tar_process(&tar_ctx);
371 if (rc != 0) {
372 DBG(0, ("tar_process failed\n"));
373 err = 1;
374 goto out;
377 out:
378 talloc_free(ctx);
379 return err;
383 * cmd_setmode - interactive command to set DOS attributes
385 * Read a filename and mode from the client command line and update
386 * the file DOS attributes.
388 int cmd_setmode(void)
390 const extern char *cmd_ptr;
391 char *buf;
392 char *fname = NULL;
393 uint16 attr[2] = {0};
394 int mode = ATTR_SET;
395 TALLOC_CTX *ctx = PANIC_IF_NULL(talloc_new(NULL));
396 int err = 0;
397 bool ok;
400 ok = next_token_talloc(ctx, &cmd_ptr, &buf, NULL);
401 if (!ok) {
402 DBG(0, ("setmode <filename> <[+|-]rsha>\n"));
403 err = 1;
404 goto out;
407 fname = PANIC_IF_NULL(talloc_asprintf(ctx,
408 "%s%s",
409 client_get_cur_dir(),
410 buf));
411 if (fname == NULL) {
412 err = 1;
413 goto out;
416 while (next_token_talloc(ctx, &cmd_ptr, &buf, NULL)) {
417 const char *s = buf;
419 while (*s) {
420 switch (*s++) {
421 case '+':
422 mode = ATTR_SET;
423 break;
424 case '-':
425 mode = ATTR_UNSET;
426 break;
427 case 'r':
428 attr[mode] |= FILE_ATTRIBUTE_READONLY;
429 break;
430 case 'h':
431 attr[mode] |= FILE_ATTRIBUTE_HIDDEN;
432 break;
433 case 's':
434 attr[mode] |= FILE_ATTRIBUTE_SYSTEM;
435 break;
436 case 'a':
437 attr[mode] |= FILE_ATTRIBUTE_ARCHIVE;
438 break;
439 default:
440 DBG(0, ("setmode <filename> <perm=[+|-]rsha>\n"));
441 err = 1;
442 goto out;
447 if (attr[ATTR_SET] == 0 && attr[ATTR_UNSET] == 0) {
448 DBG(0, ("setmode <filename> <[+|-]rsha>\n"));
449 err = 1;
450 goto out;
453 DBG(2, ("perm set %d %d\n", attr[ATTR_SET], attr[ATTR_UNSET]));
455 /* ignore return value: server might not store DOS attributes */
456 set_remote_attr(fname, attr[ATTR_SET], ATTR_SET);
457 set_remote_attr(fname, attr[ATTR_UNSET], ATTR_UNSET);
458 out:
459 talloc_free(ctx);
460 return err;
464 * tar_parse_args - parse and set tar command line arguments
465 * @flag: string pointing to tar options
466 * @val: number of tar arguments
467 * @valsize: table of arguments after the flags (number of element in val)
469 * tar arguments work in a weird way. For each flag f that takes a
470 * value v, the user is supposed to type:
472 * on the CLI:
473 * -Tf1f2f3 v1 v2 v3 TARFILE PATHS...
475 * in the interactive session:
476 * tar f1f2f3 v1 v2 v3 TARFILE PATHS...
478 * @flag has only flags (eg. "f1f2f3") and @val has the arguments
479 * (values) following them (eg. ["v1", "v2", "v3", "TARFILE", "PATH1",
480 * "PATH2"]).
482 * There are only 2 flags that take an arg: b and N. The other flags
483 * just change the semantic of PATH or TARFILE.
485 * PATH can be a list of included/excluded paths, the path to a file
486 * containing a list of included/excluded paths to use (F flag). If no
487 * PATH is provided, the whole share is used (/).
489 int tar_parse_args(struct tar* t, const char *flag,
490 const char **val, int valsize)
492 TALLOC_CTX *ctx;
493 bool do_read_list = false;
494 /* index of next value to use */
495 int ival = 0;
496 int rc;
498 if (t == NULL) {
499 DBG(0, ("Invalid tar context\n"));
500 return 1;
503 ctx = tar_reset_mem_context(t);
505 * Reset back some options - could be from interactive version
506 * all other modes are left as they are
508 t->mode.operation = TAR_NO_OPERATION;
509 t->mode.selection = TAR_NO_SELECTION;
510 t->mode.dry = false;
511 t->to_process = false;
512 t->total_size = 0;
514 while (flag[0] != '\0') {
515 switch(flag[0]) {
516 /* operation */
517 case 'c':
518 if (t->mode.operation != TAR_NO_OPERATION) {
519 printf("Tar must be followed by only one of c or x.\n");
520 return 1;
522 t->mode.operation = TAR_CREATE;
523 break;
524 case 'x':
525 if (t->mode.operation != TAR_NO_OPERATION) {
526 printf("Tar must be followed by only one of c or x.\n");
527 return 1;
529 t->mode.operation = TAR_EXTRACT;
530 break;
532 /* selection */
533 case 'I':
534 if (t->mode.selection != TAR_NO_SELECTION) {
535 DBG(0,("Only one of I,X,F must be specified\n"));
536 return 1;
538 t->mode.selection = TAR_INCLUDE;
539 break;
540 case 'X':
541 if (t->mode.selection != TAR_NO_SELECTION) {
542 DBG(0,("Only one of I,X,F must be specified\n"));
543 return 1;
545 t->mode.selection = TAR_EXCLUDE;
546 break;
547 case 'F':
548 if (t->mode.selection != TAR_NO_SELECTION) {
549 DBG(0,("Only one of I,X,F must be specified\n"));
550 return 1;
552 t->mode.selection = TAR_INCLUDE;
553 do_read_list = true;
554 break;
556 /* blocksize */
557 case 'b':
558 if (ival >= valsize) {
559 DBG(0, ("Option b must be followed by a blocksize\n"));
560 return 1;
563 if (tar_set_blocksize(t, atoi(val[ival]))) {
564 DBG(0, ("Option b must be followed by a valid blocksize\n"));
565 return 1;
568 ival++;
569 break;
571 /* incremental mode */
572 case 'g':
573 t->mode.incremental = true;
574 break;
576 /* newer than */
577 case 'N':
578 if (ival >= valsize) {
579 DBG(0, ("Option N must be followed by valid file name\n"));
580 return 1;
583 if (tar_set_newer_than(t, val[ival])) {
584 DBG(0,("Error setting newer-than time\n"));
585 return 1;
588 ival++;
589 break;
591 /* reset mode */
592 case 'a':
593 t->mode.reset = true;
594 break;
596 /* verbose */
597 case 'q':
598 t->mode.verbose = true;
599 break;
601 /* regex match */
602 case 'r':
603 t->mode.regex = true;
604 break;
606 /* dry run mode */
607 case 'n':
608 if (t->mode.operation != TAR_CREATE) {
609 DBG(0, ("n is only meaningful when creating a tar-file\n"));
610 return 1;
613 t->mode.dry = true;
614 DBG(0, ("dry_run set\n"));
615 break;
617 default:
618 DBG(0,("Unknown tar option\n"));
619 return 1;
622 flag++;
625 /* no selection given? default selection is include */
626 if (t->mode.selection == TAR_NO_SELECTION) {
627 t->mode.selection = TAR_INCLUDE;
630 if (valsize - ival < 1) {
631 DBG(0, ("No tar file given.\n"));
632 return 1;
635 /* handle TARFILE */
636 t->tar_path = PANIC_IF_NULL(talloc_strdup(ctx, val[ival]));
637 ival++;
640 * Make sure that dbf points to stderr if we are using stdout for
641 * tar output
643 if (t->mode.operation == TAR_CREATE && strequal(t->tar_path, "-")) {
644 setup_logging("smbclient", DEBUG_STDERR);
647 /* handle PATHs... */
649 /* flag F -> read file list */
650 if (do_read_list) {
651 if (valsize - ival != 1) {
652 DBG(0,("Option F must be followed by exactly one filename.\n"));
653 return 1;
656 rc = tar_read_inclusion_file(t, val[ival]);
657 if (rc != 0) {
658 return 1;
660 ival++;
663 /* otherwise store all the PATHs on the command line */
664 else {
665 int i;
666 for (i = ival; i < valsize; i++) {
667 tar_add_selection_path(t, val[i]);
671 t->to_process = true;
672 tar_dump(t);
673 return 0;
677 * tar_process - start processing archive
679 * The talloc context of the fields is freed at the end of the call.
681 int tar_process(struct tar *t)
683 int rc = 0;
685 if (t == NULL) {
686 DBG(0, ("Invalid tar context\n"));
687 return 1;
690 switch(t->mode.operation) {
691 case TAR_EXTRACT:
692 rc = tar_extract(t);
693 break;
694 case TAR_CREATE:
695 rc = tar_create(t);
696 break;
697 default:
698 DBG(0, ("Invalid tar state\n"));
699 rc = 1;
702 t->to_process = false;
703 tar_free_mem_context(t);
704 DBG(5, ("tar_process done, err = %d\n", rc));
705 return rc;
709 * tar_create - create archive and fetch files
711 static int tar_create(struct tar* t)
713 TALLOC_CTX *ctx = PANIC_IF_NULL(talloc_new(NULL));
714 int r;
715 int err = 0;
716 NTSTATUS status;
717 const char *mask;
719 t->archive = archive_write_new();
721 if (!t->mode.dry) {
722 const int bsize = t->mode.blocksize * TAR_BLOCK_UNIT;
723 r = archive_write_set_bytes_per_block(t->archive, bsize);
724 if (r != ARCHIVE_OK) {
725 DBG(0, ("Can't use a block size of %d bytes", bsize));
726 err = 1;
727 goto out;
731 * Use PAX restricted format which is not the most
732 * conservative choice but has useful extensions and is widely
733 * supported
735 r = archive_write_set_format_pax_restricted(t->archive);
736 if (r != ARCHIVE_OK) {
737 DBG(0, ("Can't use pax restricted format: %s\n",
738 archive_error_string(t->archive)));
739 err = 1;
740 goto out;
743 if (strequal(t->tar_path, "-")) {
744 r = archive_write_open_fd(t->archive, STDOUT_FILENO);
745 } else {
746 r = archive_write_open_filename(t->archive, t->tar_path);
749 if (r != ARCHIVE_OK) {
750 DBG(0, ("Can't open %s: %s\n", t->tar_path,
751 archive_error_string(t->archive)));
752 err = 1;
753 goto out_close;
758 * In inclusion mode, iterate on the inclusion list
760 if (t->mode.selection == TAR_INCLUDE && t->path_list_size > 0) {
761 if (tar_create_from_list(t)) {
762 err = 1;
763 goto out_close;
765 } else {
766 mask = PANIC_IF_NULL(talloc_asprintf(ctx, "%s\\*",
767 client_get_cur_dir()));
768 DBG(5, ("tar_process do_list with mask: %s\n", mask));
769 status = do_list(mask, TAR_DO_LIST_ATTR, get_file_callback, false, true);
770 if (!NT_STATUS_IS_OK(status)) {
771 DBG(0, ("do_list fail %s\n", nt_errstr(status)));
772 err = 1;
773 goto out_close;
777 out_close:
778 DBG(0, ("Total bytes received: %" PRIu64 "\n", t->total_size));
780 if (!t->mode.dry) {
781 r = archive_write_close(t->archive);
782 if (r != ARCHIVE_OK) {
783 DBG(0, ("Fatal: %s\n", archive_error_string(t->archive)));
784 err = 1;
785 goto out;
788 out:
789 archive_write_free(t->archive);
790 talloc_free(ctx);
791 return err;
795 * tar_create_from_list - fetch from path list in include mode
797 static int tar_create_from_list(struct tar *t)
799 TALLOC_CTX *ctx = PANIC_IF_NULL(talloc_new(NULL));
800 int err = 0;
801 NTSTATUS status;
802 const char *path, *mask, *base, *start_dir;
803 int i;
805 start_dir = talloc_strdup(ctx, client_get_cur_dir());
807 for (i = 0; i < t->path_list_size; i++) {
808 path = t->path_list[i];
809 base = path_base_name(path);
810 mask = PANIC_IF_NULL(talloc_asprintf(ctx, "%s\\%s",
811 client_get_cur_dir(), path));
813 DBG(5, ("incl. path='%s', base='%s', mask='%s'\n",
814 path, base ? base : "NULL", mask));
816 if (base != NULL) {
817 base = talloc_asprintf(ctx, "%s%s\\",
818 client_get_cur_dir(), path_base_name(path));
819 DBG(5, ("cd '%s' before do_list\n", base));
820 client_set_cur_dir(base);
822 status = do_list(mask, TAR_DO_LIST_ATTR, get_file_callback, false, true);
823 if (base != NULL) {
824 client_set_cur_dir(start_dir);
826 if (!NT_STATUS_IS_OK(status)) {
827 DBG(0, ("do_list failed on %s (%s)\n", path, nt_errstr(status)));
828 err = 1;
829 goto out;
833 out:
834 talloc_free(ctx);
835 return err;
839 * get_file_callback - do_list callback
841 * Callback for client.c do_list(). Called for each file found on the
842 * share matching do_list mask. Recursively call do_list() with itself
843 * as callback when the current file is a directory.
845 static NTSTATUS get_file_callback(struct cli_state *cli,
846 struct file_info *finfo,
847 const char *dir)
849 TALLOC_CTX *ctx = PANIC_IF_NULL(talloc_new(NULL));
850 NTSTATUS err = NT_STATUS_OK;
851 char *remote_name;
852 const char *initial_dir = client_get_cur_dir();
853 int rc;
855 remote_name = PANIC_IF_NULL(talloc_asprintf(ctx, "%s%s",
856 initial_dir, finfo->name));
858 if (strequal(finfo->name, "..") || strequal(finfo->name, ".")) {
859 goto out;
862 rc = tar_create_skip_path(&tar_ctx, remote_name, finfo);
863 if (rc != 0) {
864 DBG(5, ("--- %s\n", remote_name));
865 goto out;
868 if (finfo->mode & FILE_ATTRIBUTE_DIRECTORY) {
869 char *old_dir;
870 char *new_dir;
871 char *mask;
873 old_dir = PANIC_IF_NULL(talloc_strdup(ctx, initial_dir));
874 new_dir = PANIC_IF_NULL(talloc_asprintf(ctx, "%s%s\\",
875 initial_dir, finfo->name));
876 mask = PANIC_IF_NULL(talloc_asprintf(ctx, "%s*", new_dir));
878 rc = tar_get_file(&tar_ctx, remote_name, finfo);
879 if (rc != 0) {
880 err = NT_STATUS_UNSUCCESSFUL;
881 goto out;
884 client_set_cur_dir(new_dir);
885 do_list(mask, TAR_DO_LIST_ATTR, get_file_callback, false, true);
886 client_set_cur_dir(old_dir);
887 } else {
888 rc = tar_get_file(&tar_ctx, remote_name, finfo);
889 if (rc != 0) {
890 err = NT_STATUS_UNSUCCESSFUL;
891 goto out;
895 out:
896 talloc_free(ctx);
897 return err;
901 * tar_get_file - fetch a remote file to the local archive
902 * @full_dos_path: path to the file to fetch
903 * @finfo: attributes of the file to fetch
905 static int tar_get_file(struct tar *t, const char *full_dos_path,
906 struct file_info *finfo)
908 extern struct cli_state *cli;
909 TALLOC_CTX *ctx = PANIC_IF_NULL(talloc_new(NULL));
910 NTSTATUS status;
911 struct archive_entry *entry;
912 char *full_unix_path;
913 char buf[TAR_CLI_READ_SIZE];
914 size_t len;
915 uint64_t off = 0;
916 uint16_t remote_fd = (uint16_t)-1;
917 int err = 0, r;
918 const bool isdir = finfo->mode & FILE_ATTRIBUTE_DIRECTORY;
920 DBG(5, ("+++ %s\n", full_dos_path));
922 t->total_size += finfo->size;
924 if (t->mode.dry) {
925 goto out;
928 if (t->mode.reset) {
929 /* ignore return value: server might not store DOS attributes */
930 set_remote_attr(full_dos_path, FILE_ATTRIBUTE_ARCHIVE, ATTR_UNSET);
933 full_unix_path = PANIC_IF_NULL(talloc_asprintf(ctx, ".%s", full_dos_path));
934 string_replace(full_unix_path, '\\', '/');
935 entry = archive_entry_new();
936 archive_entry_copy_pathname(entry, full_unix_path);
937 archive_entry_set_filetype(entry, isdir ? AE_IFDIR : AE_IFREG);
938 archive_entry_set_atime(entry,
939 finfo->atime_ts.tv_sec,
940 finfo->atime_ts.tv_nsec);
941 archive_entry_set_mtime(entry,
942 finfo->mtime_ts.tv_sec,
943 finfo->mtime_ts.tv_nsec);
944 archive_entry_set_ctime(entry,
945 finfo->ctime_ts.tv_sec,
946 finfo->ctime_ts.tv_nsec);
947 archive_entry_set_perm(entry, isdir ? 0755 : 0644);
949 * check if we can safely cast unsigned file size to libarchive
950 * signed size. Very unlikely problem (>9 exabyte file)
952 if (finfo->size > INT64_MAX) {
953 DBG(0, ("Remote file %s too big\n", full_dos_path));
954 goto out_entry;
957 archive_entry_set_size(entry, (int64_t)finfo->size);
959 r = archive_write_header(t->archive, entry);
960 if (r != ARCHIVE_OK) {
961 DBG(0, ("Fatal: %s\n", archive_error_string(t->archive)));
962 err = 1;
963 goto out_entry;
966 if (isdir) {
967 DBG(5, ("get_file skip dir %s\n", full_dos_path));
968 goto out_entry;
971 status = cli_open(cli, full_dos_path, O_RDONLY, DENY_NONE, &remote_fd);
972 if (!NT_STATUS_IS_OK(status)) {
973 DBG(0,("%s opening remote file %s\n",
974 nt_errstr(status), full_dos_path));
975 goto out_entry;
978 do {
979 status = cli_read(cli, remote_fd, buf, off, sizeof(buf), &len);
980 if (!NT_STATUS_IS_OK(status)) {
981 DBG(0,("Error reading file %s : %s\n",
982 full_dos_path, nt_errstr(status)));
983 err = 1;
984 goto out_close;
987 off += len;
989 r = archive_write_data(t->archive, buf, len);
990 if (r < 0) {
991 DBG(0, ("Fatal: %s\n", archive_error_string(t->archive)));
992 err = 1;
993 goto out_close;
996 } while (off < finfo->size);
998 out_close:
999 cli_close(cli, remote_fd);
1001 out_entry:
1002 archive_entry_free(entry);
1004 out:
1005 talloc_free(ctx);
1006 return err;
1010 * tar_extract - open archive and send files.
1012 static int tar_extract(struct tar *t)
1014 int err = 0;
1015 int r;
1016 struct archive_entry *entry;
1017 const size_t bsize = t->mode.blocksize * TAR_BLOCK_UNIT;
1018 int rc;
1020 t->archive = archive_read_new();
1021 archive_read_support_format_all(t->archive);
1022 archive_read_support_filter_all(t->archive);
1024 if (strequal(t->tar_path, "-")) {
1025 r = archive_read_open_fd(t->archive, STDIN_FILENO, bsize);
1026 } else {
1027 r = archive_read_open_filename(t->archive, t->tar_path, bsize);
1030 if (r != ARCHIVE_OK) {
1031 DBG(0, ("Can't open %s : %s\n", t->tar_path,
1032 archive_error_string(t->archive)));
1033 err = 1;
1034 goto out;
1037 for (;;) {
1038 r = archive_read_next_header(t->archive, &entry);
1039 if (r == ARCHIVE_EOF) {
1040 break;
1042 if (r == ARCHIVE_WARN) {
1043 DBG(0, ("Warning: %s\n", archive_error_string(t->archive)));
1045 if (r == ARCHIVE_FATAL) {
1046 DBG(0, ("Fatal: %s\n", archive_error_string(t->archive)));
1047 err = 1;
1048 goto out;
1051 rc = tar_extract_skip_path(t, entry);
1052 if (rc != 0) {
1053 DBG(5, ("--- %s\n", archive_entry_pathname(entry)));
1054 continue;
1057 DBG(5, ("+++ %s\n", archive_entry_pathname(entry)));
1059 rc = tar_send_file(t, entry);
1060 if (rc != 0) {
1061 err = 1;
1062 goto out;
1066 out:
1067 r = archive_read_free(t->archive);
1068 if (r != ARCHIVE_OK) {
1069 DBG(0, ("Can't close %s : %s\n", t->tar_path,
1070 archive_error_string(t->archive)));
1071 err = 1;
1073 return err;
1077 * tar_send_file - send @entry to the remote server
1078 * @entry: current archive entry
1080 * Handle the creation of the parent directories and transfer the
1081 * entry to a new remote file.
1083 static int tar_send_file(struct tar *t, struct archive_entry *entry)
1085 extern struct cli_state *cli;
1086 TALLOC_CTX *ctx = PANIC_IF_NULL(talloc_new(NULL));
1087 char *dos_path;
1088 char *full_path;
1089 NTSTATUS status;
1090 uint16_t remote_fd = (uint16_t) -1;
1091 int err = 0;
1092 int flags = O_RDWR | O_CREAT | O_TRUNC;
1093 mode_t mode = archive_entry_filetype(entry);
1094 int rc;
1096 dos_path = PANIC_IF_NULL(talloc_strdup(ctx, archive_entry_pathname(entry)));
1097 fix_unix_path(dos_path, true);
1099 full_path = PANIC_IF_NULL(talloc_strdup(ctx, client_get_cur_dir()));
1100 full_path = PANIC_IF_NULL(talloc_strdup_append(full_path, dos_path));
1102 if (mode != AE_IFREG && mode != AE_IFDIR) {
1103 DBG(0, ("Skipping non-dir & non-regular file %s\n", full_path));
1104 goto out;
1107 rc = make_remote_path(full_path);
1108 if (rc != 0) {
1109 err = 1;
1110 goto out;
1113 if (mode == AE_IFDIR) {
1114 goto out;
1117 status = cli_open(cli, full_path, flags, DENY_NONE, &remote_fd);
1118 if (!NT_STATUS_IS_OK(status)) {
1119 DBG(0, ("Error opening remote file %s: %s\n",
1120 full_path, nt_errstr(status)));
1121 err = 1;
1122 goto out;
1125 for (;;) {
1126 const void *buf;
1127 size_t len;
1128 off_t off;
1129 int r;
1131 r = archive_read_data_block(t->archive, &buf, &len, &off);
1132 if (r == ARCHIVE_EOF) {
1133 break;
1135 if (r == ARCHIVE_WARN) {
1136 DBG(0, ("Warning: %s\n", archive_error_string(t->archive)));
1138 if (r == ARCHIVE_FATAL) {
1139 DBG(0, ("Fatal: %s\n", archive_error_string(t->archive)));
1140 err = 1;
1141 goto close_out;
1144 status = cli_writeall(cli, remote_fd, 0, buf, off, len, NULL);
1145 if (!NT_STATUS_IS_OK(status)) {
1146 DBG(0, ("Error writing remote file %s: %s\n",
1147 full_path, nt_errstr(status)));
1148 err = 1;
1149 goto close_out;
1153 close_out:
1154 status = cli_close(cli, remote_fd);
1155 if (!NT_STATUS_IS_OK(status)) {
1156 DBG(0, ("Error losing remote file %s: %s\n",
1157 full_path, nt_errstr(status)));
1158 err = 1;
1161 out:
1162 talloc_free(ctx);
1163 return err;
1167 * tar_add_selection_path - add a path to the path list
1168 * @path: path to add
1170 static void tar_add_selection_path(struct tar *t, const char *path)
1172 TALLOC_CTX *ctx = t->talloc_ctx;
1173 if (!t->path_list) {
1174 t->path_list = PANIC_IF_NULL(str_list_make_empty(ctx));
1175 t->path_list_size = 0;
1178 /* cast to silent gcc const-qual warning */
1179 t->path_list = PANIC_IF_NULL(str_list_add((void*)t->path_list,
1180 path));
1181 t->path_list_size++;
1182 fix_unix_path(t->path_list[t->path_list_size - 1], true);
1186 * tar_set_blocksize - set block size in TAR_BLOCK_UNIT
1188 static int tar_set_blocksize(struct tar *t, int size)
1190 if (size <= 0 || size > TAR_MAX_BLOCK_SIZE) {
1191 return 1;
1194 t->mode.blocksize = size;
1196 return 0;
1200 * tar_set_newer_than - set date threshold of saved files
1201 * @filename: local path to a file
1203 * Only files newer than the modification time of @filename will be
1204 * saved.
1206 * Note: this function set the global variable newer_than from
1207 * client.c. Thus the time is not a field of the tar structure. See
1208 * cmd_newer() to change its value from an interactive session.
1210 static int tar_set_newer_than(struct tar *t, const char *filename)
1212 extern time_t newer_than;
1213 SMB_STRUCT_STAT stbuf;
1214 int rc;
1216 rc = sys_stat(filename, &stbuf, false);
1217 if (rc != 0) {
1218 DBG(0, ("Error setting newer-than time\n"));
1219 return 1;
1222 newer_than = convert_timespec_to_time_t(stbuf.st_ex_mtime);
1223 DBG(1, ("Getting files newer than %s\n", time_to_asc(newer_than)));
1224 return 0;
1228 * tar_read_inclusion_file - set path list from file
1229 * @filename: path to the list file
1231 * Read and add each line of @filename to the path list.
1233 static int tar_read_inclusion_file (struct tar *t, const char* filename)
1235 char *line;
1236 TALLOC_CTX *ctx = PANIC_IF_NULL(talloc_new(NULL));
1237 int err = 0;
1238 int fd;
1240 fd = open(filename, O_RDONLY);
1241 if (fd < 0) {
1242 DBG(0, ("Can't open inclusion file '%s': %s\n", filename, strerror(errno)));
1243 err = 1;
1244 goto out;
1247 for (line = afdgets(fd, ctx, 0);
1248 line != NULL;
1249 line = afdgets(fd, ctx, 0)) {
1250 tar_add_selection_path(t, line);
1253 close(fd);
1255 out:
1256 talloc_free(ctx);
1257 return err;
1261 * tar_path_in_list - return true if @path is in the path list
1262 * @path: path to find
1263 * @reverse: when true also try to find path list element in @path
1265 * Look at each path of the path list and return true if @path is a
1266 * subpath of one of them.
1268 * If you want /path to be in the path list (path/a/, path/b/) set
1269 * @reverse to true to try to match the other way around.
1271 static bool tar_path_in_list(struct tar *t, const char *path, bool reverse)
1273 int i;
1274 const char *p = path;
1275 const char *pattern;
1276 bool res;
1278 if (!p || !p[0])
1279 return false;
1281 p = skip_useless_char_in_path(p);
1283 for (i = 0; i < t->path_list_size; i++) {
1284 pattern = skip_useless_char_in_path(t->path_list[i]);
1285 res = is_subpath(p, pattern);
1286 if (reverse) {
1287 res = res || is_subpath(pattern, p);
1289 if (res) {
1290 return true;
1294 return false;
1298 * tar_extract_skip_path - return true if @entry should be skipped
1299 * @entry: current tar entry
1301 * Skip predicate for tar extraction (archive to server) only.
1303 static bool tar_extract_skip_path(struct tar *t,
1304 struct archive_entry *entry)
1306 const bool skip = true;
1307 const char *fullpath = archive_entry_pathname(entry);
1308 bool in = true;
1310 if (t->path_list_size <= 0) {
1311 return !skip;
1314 if (t->mode.regex) {
1315 in = mask_match_list(fullpath, t->path_list, t->path_list_size, true);
1316 } else {
1317 in = tar_path_in_list(t, fullpath, false);
1320 if (t->mode.selection == TAR_EXCLUDE) {
1321 in = !in;
1324 return in ? !skip : skip;
1328 * tar_create_skip_path - return true if @fullpath shoud be skipped
1329 * @fullpath: full remote path of the current file
1330 * @finfo: remote file attributes
1332 * Skip predicate for tar creation (server to archive) only.
1334 static bool tar_create_skip_path(struct tar *t,
1335 const char *fullpath,
1336 const struct file_info *finfo)
1338 /* syntaxic sugar */
1339 const bool skip = true;
1340 const mode_t mode = finfo->mode;
1341 const bool isdir = mode & FILE_ATTRIBUTE_DIRECTORY;
1342 const bool exclude = t->mode.selection == TAR_EXCLUDE;
1343 bool in = true;
1345 if (!isdir) {
1347 /* 1. if we dont want X and we have X, skip */
1348 if (!t->mode.system && (mode & FILE_ATTRIBUTE_SYSTEM)) {
1349 return skip;
1352 if (!t->mode.hidden && (mode & FILE_ATTRIBUTE_HIDDEN)) {
1353 return skip;
1356 /* 2. if we only want archive and it's not, skip */
1358 if (t->mode.incremental && !(mode & FILE_ATTRIBUTE_ARCHIVE)) {
1359 return skip;
1363 /* 3. is it in the selection list? */
1366 * tar_create_from_list() use the include list as a starting
1367 * point, no need to check
1369 if (!exclude) {
1370 return !skip;
1373 /* we are now in exclude mode */
1375 /* no matter the selection, no list => include everything */
1376 if (t->path_list_size <= 0) {
1377 return !skip;
1380 if (t->mode.regex) {
1381 in = mask_match_list(fullpath, t->path_list, t->path_list_size, true);
1382 } else {
1383 in = tar_path_in_list(t, fullpath, isdir && !exclude);
1386 return in ? skip : !skip;
1390 * tar_to_process - return true if @t is ready to be processed
1392 * @t is ready if it properly parsed command line arguments.
1394 bool tar_to_process (struct tar *t)
1396 if (t == NULL) {
1397 DBG(0, ("Invalid tar context\n"));
1398 return false;
1400 return t->to_process;
1404 * skip_useless_char_in_path - skip leading slashes/dots
1406 * Skip leading slashes, backslashes and dot-slashes.
1408 static const char* skip_useless_char_in_path(const char *p)
1410 while (p) {
1411 if (*p == '/' || *p == '\\') {
1412 p++;
1414 else if (p[0] == '.' && (p[1] == '/' || p[1] == '\\')) {
1415 p += 2;
1417 else
1418 return p;
1420 return p;
1424 * is_subpath - return true if the path @sub is a subpath of @full.
1425 * @sub: path to test
1426 * @full: container path
1428 * String comparaison is case-insensitive.
1430 * Return true if @sub = @full
1432 static bool is_subpath(const char *sub, const char *full)
1434 const char *full_copy = full;
1436 while (*full && *sub &&
1437 (*full == *sub || tolower_m(*full) == tolower_m(*sub) ||
1438 (*full == '\\' && *sub=='/') || (*full == '/' && *sub=='\\'))) {
1439 full++; sub++;
1442 /* if full has a trailing slash, it compared equal, so full is an "initial"
1443 string of sub.
1445 if (!*full && full != full_copy && (*(full-1) == '/' || *(full-1) == '\\'))
1446 return true;
1448 /* ignore trailing slash on full */
1449 if (!*sub && (*full == '/' || *full == '\\') && !*(full+1))
1450 return true;
1452 /* check for full is an "initial" string of sub */
1453 if ((*sub == '/' || *sub == '\\') && !*full)
1454 return true;
1456 return *full == *sub;
1460 * set_remote_attr - set DOS attributes of a remote file
1461 * @filename: path to the file name
1462 * @new_attr: attribute bit mask to use
1463 * @mode: one of ATTR_SET or ATTR_UNSET
1465 * Update the file attributes with the one provided.
1467 static int set_remote_attr(const char *filename, uint16 new_attr, int mode)
1469 extern struct cli_state *cli;
1470 uint16 old_attr;
1471 NTSTATUS status;
1473 status = cli_getatr(cli, filename, &old_attr, NULL, NULL);
1474 if (!NT_STATUS_IS_OK(status)) {
1475 DBG(0, ("cli_getatr failed: %s\n", nt_errstr(status)));
1476 return 1;
1479 if (mode == ATTR_SET) {
1480 new_attr |= old_attr;
1481 } else {
1482 new_attr = old_attr & ~new_attr;
1485 status = cli_setatr(cli, filename, new_attr, 0);
1486 if (!NT_STATUS_IS_OK(status)) {
1487 DBG(1, ("cli_setatr failed: %s\n", nt_errstr(status)));
1488 return 1;
1491 return 0;
1496 * make_remote_path - recursively make remote dirs
1497 * @full_path: full hierarchy to create
1499 * Create @full_path and each parent directories as needed.
1501 static int make_remote_path(const char *full_path)
1503 extern struct cli_state *cli;
1504 TALLOC_CTX *ctx = PANIC_IF_NULL(talloc_new(NULL));
1505 char *path;
1506 char *subpath;
1507 char *state;
1508 char *last_backslash;
1509 char *p;
1510 int len;
1511 NTSTATUS status;
1512 int err = 0;
1514 subpath = PANIC_IF_NULL(talloc_strdup(ctx, full_path));
1515 path = PANIC_IF_NULL(talloc_strdup(ctx, full_path));
1516 len = talloc_get_size(path) - 1;
1518 last_backslash = strrchr_m(path, '\\');
1520 if (!last_backslash) {
1521 goto out;
1524 *last_backslash = 0;
1526 subpath[0] = 0;
1527 p = strtok_r(path, "\\", &state);
1529 while (p) {
1530 strlcat(subpath, p, len);
1531 status = cli_chkpath(cli, subpath);
1532 if (!NT_STATUS_IS_OK(status)) {
1533 status = cli_mkdir(cli, subpath);
1534 if (!NT_STATUS_IS_OK(status)) {
1535 DBG(0, ("Can't mkdir %s: %s\n", subpath, nt_errstr(status)));
1536 err = 1;
1537 goto out;
1539 DBG(3, ("mkdir %s\n", subpath));
1542 strlcat(subpath, "\\", len);
1543 p = strtok_r(NULL, "/\\", &state);
1547 out:
1548 talloc_free(ctx);
1549 return err;
1553 * tar_reset_mem_context - reset talloc context associated with @t
1555 * At the start of the program the context is NULL so a new one is
1556 * allocated. On the following runs (interactive session only), simply
1557 * free the children.
1559 static TALLOC_CTX *tar_reset_mem_context(struct tar *t)
1561 tar_free_mem_context(t);
1562 t->talloc_ctx = PANIC_IF_NULL(talloc_new(NULL));
1563 return t->talloc_ctx;
1567 * tar_free_mem_context - free talloc context associated with @t
1569 static void tar_free_mem_context(struct tar *t)
1571 if (t->talloc_ctx) {
1572 talloc_free(t->talloc_ctx);
1573 t->talloc_ctx = NULL;
1574 t->path_list_size = 0;
1575 t->path_list = NULL;
1576 t->tar_path = NULL;
1580 #define XSET(v) [v] = #v
1581 #define XTABLE(v, t) DBG(2, ("DUMP:%-20.20s = %s\n", #v, t[v]))
1582 #define XBOOL(v) DBG(2, ("DUMP:%-20.20s = %d\n", #v, v ? 1 : 0))
1583 #define XSTR(v) DBG(2, ("DUMP:%-20.20s = %s\n", #v, v ? v : "NULL"))
1584 #define XINT(v) DBG(2, ("DUMP:%-20.20s = %d\n", #v, v))
1585 #define XUINT64(v) DBG(2, ("DUMP:%-20.20s = %" PRIu64 "\n", #v, v))
1588 * tar_dump - dump tar structure on stdout
1590 static void tar_dump(struct tar *t)
1592 int i;
1593 const char* op[] = {
1594 XSET(TAR_NO_OPERATION),
1595 XSET(TAR_CREATE),
1596 XSET(TAR_EXTRACT),
1599 const char* sel[] = {
1600 XSET(TAR_NO_SELECTION),
1601 XSET(TAR_INCLUDE),
1602 XSET(TAR_EXCLUDE),
1605 XBOOL(t->to_process);
1606 XTABLE(t->mode.operation, op);
1607 XTABLE(t->mode.selection, sel);
1608 XINT(t->mode.blocksize);
1609 XBOOL(t->mode.hidden);
1610 XBOOL(t->mode.system);
1611 XBOOL(t->mode.incremental);
1612 XBOOL(t->mode.reset);
1613 XBOOL(t->mode.dry);
1614 XBOOL(t->mode.verbose);
1615 XUINT64(t->total_size);
1616 XSTR(t->tar_path);
1617 XINT(t->path_list_size);
1619 for (i = 0; t->path_list && t->path_list[i]; i++) {
1620 DBG(2, ("DUMP: t->path_list[%2d] = %s\n", i, t->path_list[i]));
1623 DBG(2, ("DUMP:t->path_list @ %p (%d elem)\n", t->path_list, i));
1625 #undef XSET
1626 #undef XTABLE
1627 #undef XBOOL
1628 #undef XSTR
1629 #undef XINT
1632 * max_token - return upper limit for the number of token in @str
1634 * The result is not exact, the actual number of token might be less
1635 * than what is returned.
1637 static int max_token (const char *str)
1639 const char *s = str;
1640 int nb = 0;
1642 if (!str) {
1643 return 0;
1646 while (*s) {
1647 if (isspace(*s)) {
1648 nb++;
1650 s++;
1653 nb++;
1655 return nb;
1659 * fix_unix_path - convert @path to a DOS path
1660 * @path: path to convert
1661 * @removeprefix: if true, remove leading ./ or /.
1663 static char *fix_unix_path (char *path, bool removeprefix)
1665 char *from = path, *to = path;
1667 if (!path || !*path)
1668 return path;
1670 /* remove prefix:
1671 * ./path => path
1672 * /path => path
1674 if (removeprefix) {
1675 /* /path */
1676 if (path[0] == '/' || path[0] == '\\') {
1677 from += 1;
1680 /* ./path */
1681 if (path[1] && path[0] == '.' && (path[1] == '/' || path[1] == '\\')) {
1682 from += 2;
1686 /* replace / with \ */
1687 while (*from) {
1688 if (*from == '/') {
1689 *to = '\\';
1690 } else {
1691 *to = *from;
1693 from++; to++;
1695 *to = 0;
1697 return path;
1701 * path_base_name - return @path basename
1703 * If @path doesn't contain any directory separator return NULL.
1705 static char *path_base_name (const char *path)
1707 TALLOC_CTX *ctx = PANIC_IF_NULL(talloc_tos());
1708 char *base = NULL;
1709 int last = -1;
1710 int i;
1712 for (i = 0; path[i]; i++) {
1713 if (path[i] == '\\' || path[i] == '/') {
1714 last = i;
1718 if (last >= 0) {
1719 base = PANIC_IF_NULL(talloc_strdup(ctx, path));
1720 base[last] = 0;
1723 return base;
1726 #else
1728 #define NOT_IMPLEMENTED DEBUG(0, ("tar mode not compiled. build with --with-libarchive\n"))
1730 int cmd_block(void)
1732 NOT_IMPLEMENTED;
1733 return 1;
1736 int cmd_tarmode(void)
1738 NOT_IMPLEMENTED;
1739 return 1;
1742 int cmd_setmode(void)
1744 NOT_IMPLEMENTED;
1745 return 1;
1748 int cmd_tar(void)
1750 NOT_IMPLEMENTED;
1751 return 1;
1754 int tar_process(struct tar* tar)
1756 NOT_IMPLEMENTED;
1757 return 1;
1760 int tar_parse_args(struct tar *tar, const char *flag, const char **val, int valsize)
1762 NOT_IMPLEMENTED;
1763 return 1;
1766 bool tar_to_process(struct tar *tar)
1768 NOT_IMPLEMENTED;
1769 return false;
1772 struct tar *tar_get_ctx()
1774 return NULL;
1777 #endif