2 Unix SMB/CIFS implementation.
3 Tar backup command extension
4 Copyright (C) Aurélien Aptel 2013
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 * # General overview of the tar extension
23 * All tar_xxx() functions work on a `struct tar` which store most of
24 * the context of the backup process.
26 * The current tar context can be accessed via the global variable
27 * `tar_ctx`. It's publicly exported as an opaque handle via
30 * A tar context is first configured through tar_parse_args() which
31 * can be called from either the CLI (in client.c) or the interactive
32 * session (via the cmd_tar() callback).
34 * Once the configuration is done (successfully), the context is ready
35 * for processing and tar_to_process() returns true.
37 * The next step is to call tar_process() which dispatch the
38 * processing to either tar_create() or tar_extract(), depending on
43 * tar_create() creates an archive using the libarchive API then
45 * - iterates on the requested paths if the context is in inclusion
46 * mode with tar_create_from_list()
48 * - or iterates on the whole share (starting from the current dir) if
49 * in exclusion mode or if no specific path were requested
51 * The do_list() function from client.c is used to list recursively
52 * the share. In particular it takes a DOS path mask (eg. \mydir\*)
53 * and a callback function which will be called with each file name
54 * and attributes. The tar callback function is get_file_callback().
56 * The callback function checks whether the file should be skipped
57 * according the the configuration via tar_create_skip_path(). If it's
58 * not skipped it's downloaded and written to the archive in
61 * ## Archive extraction
63 * tar_extract() opens the archive and iterates on each file in
64 * it. For each file tar_extract_skip_path() checks whether it should
65 * be skipped according to the config. If it's not skipped it's
66 * uploaded on the server in tar_send_file().
70 #include "system/filesys.h"
71 #include "client/client_proto.h"
72 #include "client/clitar_proto.h"
73 #include "libsmb/libsmb.h"
74 #include "lib/util/util_file.h"
76 #ifdef HAVE_LIBARCHIVE
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) */
86 #define STR2(x) STR1(x)
89 * Number of byte in a block unit.
91 #define TAR_BLOCK_UNIT 512
94 * Default tar block size in TAR_BLOCK_UNIT.
96 #define TAR_DEFAULT_BLOCK_SIZE 20
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)
115 TAR_CREATE
, /* c flag */
116 TAR_EXTRACT
, /* x flag */
121 TAR_INCLUDE
, /* I and F flag, default */
122 TAR_EXCLUDE
, /* X flag */
126 TALLOC_CTX
*talloc_ctx
;
128 /* in state that needs/can be processed? */
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 */
145 /* nb of bytes received */
148 /* path to tar archive name */
151 /* list of path to include or exclude */
156 struct archive
*archive
;
164 * Global context imported in client.c when needed.
168 struct tar tar_ctx
= {
169 .mode
.selection
= TAR_INCLUDE
,
170 .mode
.blocksize
= TAR_DEFAULT_BLOCK_SIZE
,
173 .mode
.incremental
= 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
,
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
,
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
,
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)
228 * cmd_block - interactive command to change tar blocksize
230 * Read a size from the client command line and update the current
235 /* XXX: from client.c */
236 const extern char *cmd_ptr
;
240 TALLOC_CTX
*ctx
= talloc_new(NULL
);
245 ok
= next_token_talloc(ctx
, &cmd_ptr
, &buf
, NULL
);
247 DBG(0, ("blocksize <n>\n"));
252 ok
= tar_set_blocksize(&tar_ctx
, atoi(buf
));
254 DBG(0, ("invalid blocksize\n"));
259 DBG(2, ("blocksize is now %d\n", tar_ctx
.mode
.blocksize
));
267 * cmd_tarmode - interactive command to change tar behaviour
269 * Read one or more modes from the client command line and update the
272 int cmd_tarmode(void)
274 const extern char *cmd_ptr
;
283 {"full", &tar_ctx
.mode
.incremental
, false},
284 {"inc", &tar_ctx
.mode
.incremental
, true },
285 {"reset", &tar_ctx
.mode
.reset
, true },
286 {"noreset", &tar_ctx
.mode
.reset
, false},
287 {"system", &tar_ctx
.mode
.system
, true },
288 {"nosystem", &tar_ctx
.mode
.system
, false},
289 {"hidden", &tar_ctx
.mode
.hidden
, true },
290 {"nohidden", &tar_ctx
.mode
.hidden
, false},
291 {"verbose", &tar_ctx
.mode
.verbose
, false},
292 {"noverbose", &tar_ctx
.mode
.verbose
, true },
295 ctx
= talloc_new(NULL
);
300 while (next_token_talloc(ctx
, &cmd_ptr
, &buf
, NULL
)) {
302 for (i
= 0; i
< ARRAY_SIZE(table
); i
++) {
303 if (strequal(table
[i
].cmd
, buf
)) {
304 *table
[i
].p
= table
[i
].value
;
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");
325 * cmd_tar - interactive command to start a tar backup/restoration
327 * Check presence of argument, parse them and handle the request.
331 const extern char *cmd_ptr
;
335 int maxtok
= max_token(cmd_ptr
);
340 TALLOC_CTX
*ctx
= talloc_new(NULL
);
345 ok
= next_token_talloc(ctx
, &cmd_ptr
, &buf
, NULL
);
347 d_printf("tar <c|x>[IXFbgvanN] [options] <tar file> [path list]\n");
353 val
= talloc_array(ctx
, const char *, maxtok
);
359 while (next_token_talloc(ctx
, &cmd_ptr
, &buf
, NULL
)) {
363 rc
= tar_parse_args(&tar_ctx
, flag
, val
, i
);
365 d_printf("parse_args failed\n");
370 rc
= tar_process(&tar_ctx
);
372 d_printf("tar_process failed\n");
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:
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",
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
,
415 bool do_read_list
= false;
416 /* index of next value to use */
421 DBG_WARNING("Invalid tar context\n");
425 ctx
= tar_reset_mem_context(t
);
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
;
436 t
->to_process
= false;
439 while (flag
[0] != '\0') {
443 if (t
->mode
.operation
!= TAR_NO_OPERATION
) {
444 d_printf("Tar must be followed by only one of c or x.\n");
447 t
->mode
.operation
= TAR_CREATE
;
450 if (t
->mode
.operation
!= TAR_NO_OPERATION
) {
451 d_printf("Tar must be followed by only one of c or x.\n");
454 t
->mode
.operation
= TAR_EXTRACT
;
459 if (t
->mode
.selection
!= TAR_NO_SELECTION
) {
460 d_printf("Only one of I,X,F must be specified\n");
463 t
->mode
.selection
= TAR_INCLUDE
;
466 if (t
->mode
.selection
!= TAR_NO_SELECTION
) {
467 d_printf("Only one of I,X,F must be specified\n");
470 t
->mode
.selection
= TAR_EXCLUDE
;
473 if (t
->mode
.selection
!= TAR_NO_SELECTION
) {
474 d_printf("Only one of I,X,F must be specified\n");
477 t
->mode
.selection
= TAR_INCLUDE
;
483 if (ival
>= valsize
) {
484 d_printf("Option b must be followed by a blocksize\n");
488 if (tar_set_blocksize(t
, atoi(val
[ival
]))) {
489 d_printf("Option b must be followed by a valid blocksize\n");
496 /* incremental mode */
498 t
->mode
.incremental
= true;
503 if (ival
>= valsize
) {
504 d_printf("Option N must be followed by valid file name\n");
508 if (tar_set_newer_than(t
, val
[ival
])) {
509 d_printf("Error setting newer-than time\n");
518 t
->mode
.reset
= true;
523 t
->mode
.verbose
= true;
528 t
->mode
.regex
= true;
533 if (t
->mode
.operation
!= TAR_CREATE
) {
534 d_printf("n is only meaningful when creating a tar-file\n");
539 d_printf("dry_run set\n");
543 d_printf("Unknown tar option\n");
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");
561 t
->tar_path
= talloc_strdup(ctx
, val
[ival
]);
562 if (t
->tar_path
== NULL
) {
568 * Make sure that dbf points to stderr if we are using stdout for
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 */
579 if (valsize
- ival
!= 1) {
580 d_printf("Option F must be followed by exactly one filename.\n");
584 rc
= tar_read_inclusion_file(t
, val
[ival
]);
589 /* otherwise store all the PATHs on the command line */
592 for (i
= ival
; i
< valsize
; i
++) {
594 status
= tar_add_selection_path(t
, val
[i
]);
595 if (!NT_STATUS_IS_OK(status
)) {
601 t
->to_process
= true;
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
)
616 DBG_WARNING("Invalid tar context\n");
620 switch(t
->mode
.operation
) {
628 DBG_WARNING("Invalid tar state\n");
632 t
->to_process
= false;
633 tar_free_mem_context(t
);
634 DBG(5, ("tar_process done, err = %d\n", rc
));
639 * tar_create - create archive and fetch files
641 static int tar_create(struct tar
* t
)
647 struct timespec tp_start
, tp_end
;
648 TALLOC_CTX
*ctx
= talloc_new(NULL
);
653 clock_gettime_mono(&tp_start
);
657 t
->archive
= archive_write_new();
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
);
669 * Use PAX restricted format which is not the most
670 * conservative choice but has useful extensions and is widely
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
));
681 if (strequal(t
->tar_path
, "-")) {
682 r
= archive_write_open_fd(t
->archive
, STDOUT_FILENO
);
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
));
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
)) {
704 mask
= talloc_asprintf(ctx
, "%s\\*", client_get_cur_dir());
709 mask
= client_clean_name(ctx
, mask
);
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
)));
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",
728 t
->total_size
/timespec_elapsed2(&tp_start
, &tp_end
)/1024/1024);
733 r
= archive_write_close(t
->archive
);
734 if (r
!= ARCHIVE_OK
) {
735 d_printf("Fatal: %s\n", archive_error_string(t
->archive
));
741 #ifdef HAVE_ARCHIVE_READ_FREE
742 archive_write_free(t
->archive
);
744 archive_write_finish(t
->archive
);
751 * tar_create_from_list - fetch from path list in include mode
753 static int tar_create_from_list(struct tar
*t
)
758 const char *path
, *mask
, *start_dir
;
760 TALLOC_CTX
*ctx
= talloc_new(NULL
);
765 start_dir
= talloc_strdup(ctx
, client_get_cur_dir());
766 if (start_dir
== NULL
) {
771 for (i
= 0; i
< t
->path_list_size
; i
++) {
772 path
= t
->path_list
[i
];
774 status
= path_base_name(ctx
, path
, &base
);
775 if (!NT_STATUS_IS_OK(status
)) {
779 mask
= talloc_asprintf(ctx
, "%s\\%s",
780 client_get_cur_dir(), path
);
785 mask
= client_clean_name(ctx
, mask
);
791 DBG(5, ("incl. path='%s', base='%s', mask='%s'\n",
792 path
, base
? base
: "NULL", mask
));
795 base
= talloc_asprintf(ctx
, "%s%s\\",
796 client_get_cur_dir(), base
);
801 base
= client_clean_name(ctx
, base
);
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);
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
)));
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
,
837 NTSTATUS status
= NT_STATUS_OK
;
839 char *old_dir
= NULL
;
840 char *new_dir
= NULL
;
841 const char *initial_dir
= dir
;
845 TALLOC_CTX
*ctx
= talloc_new(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
;
855 remote_name
= client_clean_name(ctx
, remote_name
);
856 if (remote_name
== NULL
) {
857 status
= NT_STATUS_NO_MEMORY
;
861 if (strequal(finfo
->name
, "..") || strequal(finfo
->name
, ".")) {
865 isdir
= finfo
->attr
& FILE_ATTRIBUTE_DIRECTORY
;
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
;
875 status
= tar_create_skip_path(&tar_ctx
,
876 isdir
? new_dir
: remote_name
,
878 if (!NT_STATUS_IS_OK(status
)) {
883 DBG(5, ("--- %s\n", remote_name
));
884 status
= NT_STATUS_OK
;
888 rc
= tar_get_file(&tar_ctx
, remote_name
, finfo
);
890 status
= NT_STATUS_UNSUCCESSFUL
;
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
;
910 struct archive_entry
*entry
;
911 char *full_unix_path
;
912 char buf
[TAR_CLI_READ_SIZE
];
915 uint16_t remote_fd
= (uint16_t)-1;
917 const bool isdir
= finfo
->attr
& FILE_ATTRIBUTE_DIRECTORY
;
918 TALLOC_CTX
*ctx
= talloc_new(NULL
);
924 DBG(5, ("+++ %s\n", full_dos_path
));
926 t
->total_size
+= finfo
->size
;
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
) {
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
);
965 archive_entry_set_size(entry
, (int64_t)finfo
->size
);
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
));
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
));
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
);
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
));
996 if (t
->mode
.verbose
) {
997 d_printf("a %s\n", full_dos_path
);
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
));
1011 r
= archive_write_data(t
->archive
, buf
, len
);
1013 d_printf("Fatal: %s\n", archive_error_string(t
->archive
));
1018 } while (off
< finfo
->size
);
1022 cli_close(cli
, remote_fd
);
1025 archive_entry_free(entry
);
1033 * tar_extract - open archive and send files.
1035 static int tar_extract(struct tar
*t
)
1039 struct archive_entry
*entry
;
1040 const size_t bsize
= t
->mode
.blocksize
* TAR_BLOCK_UNIT
;
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
);
1049 if (strequal(t
->tar_path
, "-")) {
1050 r
= archive_read_open_fd(t
->archive
, STDIN_FILENO
, bsize
);
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
));
1065 r
= archive_read_next_header(t
->archive
, &entry
);
1066 if (r
== ARCHIVE_EOF
) {
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
));
1078 status
= tar_extract_skip_path(t
, entry
, &skip
);
1079 if (!NT_STATUS_IS_OK(status
)) {
1084 DBG(5, ("--- %s\n", archive_entry_pathname(entry
)));
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
);
1101 #ifdef HAVE_ARCHIVE_READ_FREE
1102 r
= archive_read_free(t
->archive
);
1104 r
= archive_read_finish(t
->archive
);
1106 if (r
!= ARCHIVE_OK
) {
1107 d_printf("Can't close %s : %s\n", t
->tar_path
,
1108 archive_error_string(t
->archive
));
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
;
1127 uint16_t remote_fd
= (uint16_t) -1;
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
);
1134 TALLOC_CTX
*ctx
= talloc_new(NULL
);
1139 dos_path
= talloc_strdup(ctx
, archive_entry_pathname(entry
));
1140 if (dos_path
== NULL
) {
1144 fix_unix_path(dos_path
, true);
1146 full_path
= talloc_strdup(ctx
, client_get_cur_dir());
1147 if (full_path
== NULL
) {
1151 full_path
= talloc_strdup_append(full_path
, dos_path
);
1152 if (full_path
== NULL
) {
1156 full_path
= client_clean_name(ctx
, full_path
);
1157 if (full_path
== NULL
) {
1162 if (filetype
!= AE_IFREG
&& filetype
!= AE_IFDIR
) {
1163 d_printf("Skipping non-dir & non-regular file %s\n", full_path
);
1167 rc
= make_remote_path(full_path
);
1173 if (filetype
== AE_IFDIR
) {
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
));
1191 r
= archive_read_data_block(t
->archive
, &buf
, &len
, &off
);
1192 if (r
== ARCHIVE_EOF
) {
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
));
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
));
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
));
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",
1230 TALLOC_FREE(timestr
);
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
)
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
);
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
) {
1276 t
->mode
.blocksize
= size
;
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
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
;
1298 rc
= sys_stat(filename
, &stbuf
, false);
1300 d_printf("Error setting newer-than time\n");
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
)));
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
)
1320 TALLOC_CTX
*ctx
= talloc_new(NULL
);
1325 fd
= open(filename
, O_RDONLY
);
1327 d_printf("Can't open inclusion file '%s': %s\n", filename
, strerror(errno
));
1332 for (line
= afdgets(fd
, ctx
, 0);
1334 line
= afdgets(fd
, ctx
, 0)) {
1336 status
= tar_add_selection_path(t
, line
);
1337 if (!NT_STATUS_IS_OK(status
)) {
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
)
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
++) {
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
)) {
1386 if (reverse
&& !is_in_list
) {
1387 status
= is_subpath(pattern
, p
, &is_in_list
);
1388 if (!NT_STATUS_IS_OK(status
)) {
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
,
1413 const char *fullpath
= archive_entry_pathname(entry
);
1416 if (t
->path_list_size
<= 0) {
1418 return NT_STATUS_OK
;
1421 if (t
->mode
.regex
) {
1422 in
= mask_match_list(fullpath
, t
->path_list
, t
->path_list_size
, true);
1424 NTSTATUS status
= tar_path_in_list(t
, fullpath
, false, &in
);
1425 if (!NT_STATUS_IS_OK(status
)) {
1430 if (t
->mode
.selection
== TAR_EXCLUDE
) {
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
,
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
;
1460 /* 1. if we don't want X and we have X, skip */
1461 if (!t
->mode
.system
&& (mode
& FILE_ATTRIBUTE_SYSTEM
)) {
1463 return NT_STATUS_OK
;
1466 if (!t
->mode
.hidden
&& (mode
& FILE_ATTRIBUTE_HIDDEN
)) {
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
)) {
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
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) {
1495 return NT_STATUS_OK
;
1498 if (t
->mode
.regex
) {
1499 in
= mask_match_list(fullpath
, t
->path_list
, t
->path_list_size
, true);
1501 bool reverse
= isdir
&& !exclude
;
1502 NTSTATUS status
= tar_path_in_list(t
, fullpath
, reverse
, &in
);
1503 if (!NT_STATUS_IS_OK(status
)) {
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
)
1520 DBG_WARNING("Invalid tar context\n");
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
)
1534 if (*p
== '/' || *p
== '\\') {
1537 else if (p
[0] == '.' && (p
[1] == '/' || p
[1] == '\\')) {
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
;
1560 TALLOC_CTX
*tmp_ctx
= talloc_new(NULL
);
1561 if (tmp_ctx
== NULL
) {
1562 status
= NT_STATUS_NO_MEMORY
;
1566 f
= strlower_talloc(tmp_ctx
, full
);
1568 status
= NT_STATUS_NO_MEMORY
;
1571 string_replace(f
, '\\', '/');
1572 s
= strlower_talloc(tmp_ctx
, sub
);
1574 status
= NT_STATUS_NO_MEMORY
;
1577 string_replace(s
, '\\', '/');
1579 /* find the point where sub and full diverge */
1580 while ((*f
!= '\0') && (*s
!= '\0') && (*f
== *s
)) {
1586 if ((*f
== '\0') && (*s
== '\0')) {
1587 *_subpath_match
= true; /* sub and full match */
1591 if ((*f
== '\0') && (len
> 0) && (*(f
- 1) == '/')) {
1592 /* sub diverges from full at path separator */
1593 *_subpath_match
= true;
1597 if ((*s
== '\0') && (strcmp(f
, "/") == 0)) {
1598 /* full diverges from sub with trailing slash only */
1599 *_subpath_match
= true;
1603 if ((*s
== '/') && (*f
== '\0')) {
1604 /* sub diverges from full with extra path component */
1605 *_subpath_match
= true;
1608 *_subpath_match
= false;
1611 talloc_free(tmp_ctx
);
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
;
1629 char *last_backslash
;
1634 TALLOC_CTX
*ctx
= talloc_new(NULL
);
1639 subpath
= talloc_strdup(ctx
, full_path
);
1640 if (subpath
== NULL
) {
1644 path
= talloc_strdup(ctx
, full_path
);
1649 len
= talloc_get_size(path
) - 1;
1651 last_backslash
= strrchr_m(path
, '\\');
1652 if (last_backslash
== NULL
) {
1656 *last_backslash
= 0;
1659 p
= strtok_r(path
, "\\", &state
);
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
));
1671 DBG(3, ("mkdir %s\n", subpath
));
1674 strlcat(subpath
, "\\", len
);
1675 p
= strtok_r(NULL
, "/\\", &state
);
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
;
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
)
1725 const char* op
[] = {
1726 XSET(TAR_NO_OPERATION
),
1731 const char* sel
[] = {
1732 XSET(TAR_NO_SELECTION
),
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
);
1746 XBOOL(t
->mode
.verbose
);
1747 XUINT64(t
->total_size
);
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
));
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
)
1779 while (s
[0] != '\0') {
1780 if (isspace((int)s
[0])) {
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') {
1808 if (do_remove_prefix
) {
1810 if (path
[0] == '/' || path
[0] == '\\') {
1815 if (path
[1] != '\0' && path
[0] == '.' && (path
[1] == '/' || path
[1] == '\\')) {
1820 /* replace / with \ */
1821 while (from
[0] != '\0') {
1822 if (from
[0] == '/') {
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
)
1847 for (i
= 0; path
[i
]; i
++) {
1848 if (path
[i
] == '\\' || path
[i
] == '/') {
1854 base
= talloc_strdup(ctx
, path
);
1856 return NT_STATUS_NO_MEMORY
;
1863 return NT_STATUS_OK
;
1868 #define NOT_IMPLEMENTED DEBUG(0, ("tar mode not compiled. build used --without-libarchive\n"))
1876 int cmd_tarmode(void)
1888 int tar_process(struct tar
* tar
)
1894 int tar_parse_args(struct tar
*tar
, const char *flag
, const char **val
, int valsize
)
1900 bool tar_to_process(struct tar
*tar
)
1905 struct tar
*tar_get_ctx()