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"
75 #ifdef HAVE_LIBARCHIVE
78 #include <archive_entry.h>
80 /* prepend module name and line number to debug messages */
81 #define DBG(a, b) (DEBUG(a, ("tar:%-4d ", __LINE__)), DEBUG(a, b))
83 /* preprocessor magic to stringify __LINE__ (int) */
85 #define STR2(x) STR1(x)
88 * Number of byte in a block unit.
90 #define TAR_BLOCK_UNIT 512
93 * Default tar block size in TAR_BLOCK_UNIT.
95 #define TAR_DEFAULT_BLOCK_SIZE 20
98 * Maximum value for the blocksize field
100 #define TAR_MAX_BLOCK_SIZE 0xffff
103 * Size of the buffer used when downloading a file
105 #define TAR_CLI_READ_SIZE 0xff00
107 #define TAR_DO_LIST_ATTR (FILE_ATTRIBUTE_DIRECTORY \
108 | FILE_ATTRIBUTE_SYSTEM \
109 | FILE_ATTRIBUTE_HIDDEN)
114 TAR_CREATE
, /* c flag */
115 TAR_EXTRACT
, /* x flag */
120 TAR_INCLUDE
, /* I and F flag, default */
121 TAR_EXCLUDE
, /* X flag */
125 TALLOC_CTX
*talloc_ctx
;
127 /* in state that needs/can be processed? */
132 enum tar_operation operation
; /* create, extract */
133 enum tar_selection selection
; /* include, exclude */
134 int blocksize
; /* size in TAR_BLOCK_UNIT of a tar file block */
135 bool hidden
; /* backup hidden file? */
136 bool system
; /* backup system file? */
137 bool incremental
; /* backup _only_ archived file? */
138 bool reset
; /* unset archive bit? */
139 bool dry
; /* don't write tar file? */
140 bool regex
; /* XXX: never actually using regex... */
141 bool verbose
; /* XXX: ignored */
144 /* nb of bytes received */
147 /* path to tar archive name */
150 /* list of path to include or exclude */
155 struct archive
*archive
;
163 * Global context imported in client.c when needed.
167 struct tar tar_ctx
= {
168 .mode
.selection
= TAR_INCLUDE
,
169 .mode
.blocksize
= TAR_DEFAULT_BLOCK_SIZE
,
172 .mode
.incremental
= false,
176 .mode
.verbose
= false,
179 /* tar, local function */
180 static int tar_create(struct tar
* t
);
181 static int tar_create_from_list(struct tar
*t
);
182 static int tar_extract(struct tar
*t
);
183 static int tar_read_inclusion_file(struct tar
*t
, const char* filename
);
184 static int tar_send_file(struct tar
*t
, struct archive_entry
*entry
);
185 static int tar_set_blocksize(struct tar
*t
, int size
);
186 static int tar_set_newer_than(struct tar
*t
, const char *filename
);
187 static NTSTATUS
tar_add_selection_path(struct tar
*t
, const char *path
);
188 static void tar_dump(struct tar
*t
);
189 static NTSTATUS
tar_extract_skip_path(struct tar
*t
,
190 struct archive_entry
*entry
,
192 static TALLOC_CTX
*tar_reset_mem_context(struct tar
*t
);
193 static void tar_free_mem_context(struct tar
*t
);
194 static NTSTATUS
tar_create_skip_path(struct tar
*t
,
195 const char *fullpath
,
196 const struct file_info
*finfo
,
199 static NTSTATUS
tar_path_in_list(struct tar
*t
, const char *path
,
200 bool reverse
, bool *_is_in_list
);
202 static int tar_get_file(struct tar
*t
,
203 const char *full_dos_path
,
204 struct file_info
*finfo
);
206 static NTSTATUS
get_file_callback(struct cli_state
*cli
,
207 struct file_info
*finfo
,
211 static char *fix_unix_path(char *path
, bool removeprefix
);
212 static NTSTATUS
path_base_name(TALLOC_CTX
*ctx
, const char *path
, char **_base
);
213 static const char* skip_useless_char_in_path(const char *p
);
214 static int make_remote_path(const char *full_path
);
215 static int max_token (const char *str
);
216 static NTSTATUS
is_subpath(const char *sub
, const char *full
,
217 bool *_subpath_match
);
219 * tar_get_ctx - retrieve global tar context handle
221 struct tar
*tar_get_ctx(void)
227 * cmd_block - interactive command to change tar blocksize
229 * Read a size from the client command line and update the current
234 /* XXX: from client.c */
235 const extern char *cmd_ptr
;
239 TALLOC_CTX
*ctx
= talloc_new(NULL
);
244 ok
= next_token_talloc(ctx
, &cmd_ptr
, &buf
, NULL
);
246 DBG(0, ("blocksize <n>\n"));
251 ok
= tar_set_blocksize(&tar_ctx
, atoi(buf
));
253 DBG(0, ("invalid blocksize\n"));
258 DBG(2, ("blocksize is now %d\n", tar_ctx
.mode
.blocksize
));
266 * cmd_tarmode - interactive command to change tar behaviour
268 * Read one or more modes from the client command line and update the
271 int cmd_tarmode(void)
273 const extern char *cmd_ptr
;
282 {"full", &tar_ctx
.mode
.incremental
, false},
283 {"inc", &tar_ctx
.mode
.incremental
, true },
284 {"reset", &tar_ctx
.mode
.reset
, true },
285 {"noreset", &tar_ctx
.mode
.reset
, false},
286 {"system", &tar_ctx
.mode
.system
, true },
287 {"nosystem", &tar_ctx
.mode
.system
, false},
288 {"hidden", &tar_ctx
.mode
.hidden
, true },
289 {"nohidden", &tar_ctx
.mode
.hidden
, false},
290 {"verbose", &tar_ctx
.mode
.verbose
, false},
291 {"noverbose", &tar_ctx
.mode
.verbose
, true },
294 ctx
= talloc_new(NULL
);
299 while (next_token_talloc(ctx
, &cmd_ptr
, &buf
, NULL
)) {
301 for (i
= 0; i
< ARRAY_SIZE(table
); i
++) {
302 if (strequal(table
[i
].cmd
, buf
)) {
303 *table
[i
].p
= table
[i
].value
;
308 if (i
== ARRAY_SIZE(table
))
309 d_printf("tarmode: unrecognised option %s\n", buf
);
312 d_printf("tarmode is now %s, %s, %s, %s, %s\n",
313 tar_ctx
.mode
.incremental
? "incremental" : "full",
314 tar_ctx
.mode
.system
? "system" : "nosystem",
315 tar_ctx
.mode
.hidden
? "hidden" : "nohidden",
316 tar_ctx
.mode
.reset
? "reset" : "noreset",
317 tar_ctx
.mode
.verbose
? "verbose" : "noverbose");
324 * cmd_tar - interactive command to start a tar backup/restoration
326 * Check presence of argument, parse them and handle the request.
330 const extern char *cmd_ptr
;
334 int maxtok
= max_token(cmd_ptr
);
339 TALLOC_CTX
*ctx
= talloc_new(NULL
);
344 ok
= next_token_talloc(ctx
, &cmd_ptr
, &buf
, NULL
);
346 d_printf("tar <c|x>[IXFbgvanN] [options] <tar file> [path list]\n");
352 val
= talloc_array(ctx
, const char *, maxtok
);
358 while (next_token_talloc(ctx
, &cmd_ptr
, &buf
, NULL
)) {
362 rc
= tar_parse_args(&tar_ctx
, flag
, val
, i
);
364 d_printf("parse_args failed\n");
369 rc
= tar_process(&tar_ctx
);
371 d_printf("tar_process failed\n");
383 * tar_parse_args - parse and set tar command line arguments
384 * @flag: string pointing to tar options
385 * @val: number of tar arguments
386 * @valsize: table of arguments after the flags (number of element in val)
388 * tar arguments work in a weird way. For each flag f that takes a
389 * value v, the user is supposed to type:
392 * -Tf1f2f3 v1 v2 v3 TARFILE PATHS...
394 * in the interactive session:
395 * tar f1f2f3 v1 v2 v3 TARFILE PATHS...
397 * @flag has only flags (eg. "f1f2f3") and @val has the arguments
398 * (values) following them (eg. ["v1", "v2", "v3", "TARFILE", "PATH1",
401 * There are only 2 flags that take an arg: b and N. The other flags
402 * just change the semantic of PATH or TARFILE.
404 * PATH can be a list of included/excluded paths, the path to a file
405 * containing a list of included/excluded paths to use (F flag). If no
406 * PATH is provided, the whole share is used (/).
408 int tar_parse_args(struct tar
* t
,
414 bool do_read_list
= false;
415 /* index of next value to use */
420 DBG_WARNING("Invalid tar context\n");
424 ctx
= tar_reset_mem_context(t
);
429 * Reset back some options - could be from interactive version
430 * all other modes are left as they are
432 t
->mode
.operation
= TAR_NO_OPERATION
;
433 t
->mode
.selection
= TAR_NO_SELECTION
;
435 t
->to_process
= false;
438 while (flag
[0] != '\0') {
442 if (t
->mode
.operation
!= TAR_NO_OPERATION
) {
443 d_printf("Tar must be followed by only one of c or x.\n");
446 t
->mode
.operation
= TAR_CREATE
;
449 if (t
->mode
.operation
!= TAR_NO_OPERATION
) {
450 d_printf("Tar must be followed by only one of c or x.\n");
453 t
->mode
.operation
= TAR_EXTRACT
;
458 if (t
->mode
.selection
!= TAR_NO_SELECTION
) {
459 d_printf("Only one of I,X,F must be specified\n");
462 t
->mode
.selection
= TAR_INCLUDE
;
465 if (t
->mode
.selection
!= TAR_NO_SELECTION
) {
466 d_printf("Only one of I,X,F must be specified\n");
469 t
->mode
.selection
= TAR_EXCLUDE
;
472 if (t
->mode
.selection
!= TAR_NO_SELECTION
) {
473 d_printf("Only one of I,X,F must be specified\n");
476 t
->mode
.selection
= TAR_INCLUDE
;
482 if (ival
>= valsize
) {
483 d_printf("Option b must be followed by a blocksize\n");
487 if (tar_set_blocksize(t
, atoi(val
[ival
]))) {
488 d_printf("Option b must be followed by a valid blocksize\n");
495 /* incremental mode */
497 t
->mode
.incremental
= true;
502 if (ival
>= valsize
) {
503 d_printf("Option N must be followed by valid file name\n");
507 if (tar_set_newer_than(t
, val
[ival
])) {
508 d_printf("Error setting newer-than time\n");
517 t
->mode
.reset
= true;
522 t
->mode
.verbose
= true;
527 t
->mode
.regex
= true;
532 if (t
->mode
.operation
!= TAR_CREATE
) {
533 d_printf("n is only meaningful when creating a tar-file\n");
538 d_printf("dry_run set\n");
542 d_printf("Unknown tar option\n");
549 /* no selection given? default selection is include */
550 if (t
->mode
.selection
== TAR_NO_SELECTION
) {
551 t
->mode
.selection
= TAR_INCLUDE
;
554 if (valsize
- ival
< 1) {
555 d_printf("No tar file given.\n");
560 t
->tar_path
= talloc_strdup(ctx
, val
[ival
]);
561 if (t
->tar_path
== NULL
) {
567 * Make sure that dbf points to stderr if we are using stdout for
570 if (t
->mode
.operation
== TAR_CREATE
&& strequal(t
->tar_path
, "-")) {
571 setup_logging("smbclient", DEBUG_STDERR
);
574 /* handle PATHs... */
576 /* flag F -> read file list */
578 if (valsize
- ival
!= 1) {
579 d_printf("Option F must be followed by exactly one filename.\n");
583 rc
= tar_read_inclusion_file(t
, val
[ival
]);
588 /* otherwise store all the PATHs on the command line */
591 for (i
= ival
; i
< valsize
; i
++) {
593 status
= tar_add_selection_path(t
, val
[i
]);
594 if (!NT_STATUS_IS_OK(status
)) {
600 t
->to_process
= true;
606 * tar_process - start processing archive
608 * The talloc context of the fields is freed at the end of the call.
610 int tar_process(struct tar
*t
)
615 DBG_WARNING("Invalid tar context\n");
619 switch(t
->mode
.operation
) {
627 DBG_WARNING("Invalid tar state\n");
631 t
->to_process
= false;
632 tar_free_mem_context(t
);
633 DBG(5, ("tar_process done, err = %d\n", rc
));
638 * tar_create - create archive and fetch files
640 static int tar_create(struct tar
* t
)
646 struct timespec tp_start
, tp_end
;
647 TALLOC_CTX
*ctx
= talloc_new(NULL
);
652 clock_gettime_mono(&tp_start
);
656 t
->archive
= archive_write_new();
659 const int bsize
= t
->mode
.blocksize
* TAR_BLOCK_UNIT
;
660 r
= archive_write_set_bytes_per_block(t
->archive
, bsize
);
661 if (r
!= ARCHIVE_OK
) {
662 d_printf("Can't use a block size of %d bytes", bsize
);
668 * Use PAX restricted format which is not the most
669 * conservative choice but has useful extensions and is widely
672 r
= archive_write_set_format_pax_restricted(t
->archive
);
673 if (r
!= ARCHIVE_OK
) {
674 d_printf("Can't use pax restricted format: %s\n",
675 archive_error_string(t
->archive
));
680 if (strequal(t
->tar_path
, "-")) {
681 r
= archive_write_open_fd(t
->archive
, STDOUT_FILENO
);
683 r
= archive_write_open_filename(t
->archive
, t
->tar_path
);
686 if (r
!= ARCHIVE_OK
) {
687 d_printf("Can't open %s: %s\n", t
->tar_path
,
688 archive_error_string(t
->archive
));
695 * In inclusion mode, iterate on the inclusion list
697 if (t
->mode
.selection
== TAR_INCLUDE
&& t
->path_list_size
> 0) {
698 if (tar_create_from_list(t
)) {
703 mask
= talloc_asprintf(ctx
, "%s\\*", client_get_cur_dir());
708 mask
= client_clean_name(ctx
, mask
);
713 DBG(5, ("tar_process do_list with mask: %s\n", mask
));
714 status
= do_list(mask
, TAR_DO_LIST_ATTR
, get_file_callback
, true, true);
715 if (!NT_STATUS_IS_OK(status
)) {
716 DBG(0, ("do_list fail %s\n", nt_errstr(status
)));
722 clock_gettime_mono(&tp_end
);
723 d_printf("tar: dumped %"PRIu64
" files and %"PRIu64
" directories\n",
724 t
->numfile
, t
->numdir
);
725 d_printf("Total bytes written: %"PRIu64
" (%.1f MiB/s)\n",
727 t
->total_size
/timespec_elapsed2(&tp_start
, &tp_end
)/1024/1024);
732 r
= archive_write_close(t
->archive
);
733 if (r
!= ARCHIVE_OK
) {
734 d_printf("Fatal: %s\n", archive_error_string(t
->archive
));
740 #ifdef HAVE_ARCHIVE_READ_FREE
741 archive_write_free(t
->archive
);
743 archive_write_finish(t
->archive
);
750 * tar_create_from_list - fetch from path list in include mode
752 static int tar_create_from_list(struct tar
*t
)
757 const char *path
, *mask
, *start_dir
;
759 TALLOC_CTX
*ctx
= talloc_new(NULL
);
764 start_dir
= talloc_strdup(ctx
, client_get_cur_dir());
765 if (start_dir
== NULL
) {
770 for (i
= 0; i
< t
->path_list_size
; i
++) {
771 path
= t
->path_list
[i
];
773 status
= path_base_name(ctx
, path
, &base
);
774 if (!NT_STATUS_IS_OK(status
)) {
778 mask
= talloc_asprintf(ctx
, "%s\\%s",
779 client_get_cur_dir(), path
);
784 mask
= client_clean_name(ctx
, mask
);
790 DBG(5, ("incl. path='%s', base='%s', mask='%s'\n",
791 path
, base
? base
: "NULL", mask
));
794 base
= talloc_asprintf(ctx
, "%s%s\\",
795 client_get_cur_dir(), base
);
800 base
= client_clean_name(ctx
, base
);
806 DBG(5, ("cd '%s' before do_list\n", base
));
807 client_set_cur_dir(base
);
809 status
= do_list(mask
, TAR_DO_LIST_ATTR
, get_file_callback
, true, true);
811 client_set_cur_dir(start_dir
);
813 if (!NT_STATUS_IS_OK(status
)) {
814 DBG(0, ("do_list failed on %s (%s)\n", path
, nt_errstr(status
)));
826 * get_file_callback - do_list callback
828 * Callback for client.c do_list(). Called for each file found on the
829 * share matching do_list mask. Recursively call do_list() with itself
830 * as callback when the current file is a directory.
832 static NTSTATUS
get_file_callback(struct cli_state
*cli
,
833 struct file_info
*finfo
,
836 NTSTATUS status
= NT_STATUS_OK
;
838 char *old_dir
= NULL
;
839 char *new_dir
= NULL
;
840 const char *initial_dir
= dir
;
844 TALLOC_CTX
*ctx
= talloc_new(NULL
);
846 return NT_STATUS_NO_MEMORY
;
849 remote_name
= talloc_asprintf(ctx
, "%s\\%s", initial_dir
, finfo
->name
);
850 if (remote_name
== NULL
) {
851 status
= NT_STATUS_NO_MEMORY
;
854 remote_name
= client_clean_name(ctx
, remote_name
);
855 if (remote_name
== NULL
) {
856 status
= NT_STATUS_NO_MEMORY
;
860 if (strequal(finfo
->name
, "..") || strequal(finfo
->name
, ".")) {
864 isdir
= finfo
->attr
& FILE_ATTRIBUTE_DIRECTORY
;
866 old_dir
= talloc_strdup(ctx
, initial_dir
);
867 new_dir
= talloc_asprintf(ctx
, "%s\\", remote_name
);
868 if ((old_dir
== NULL
) || (new_dir
== NULL
)) {
869 status
= NT_STATUS_NO_MEMORY
;
874 status
= tar_create_skip_path(&tar_ctx
,
875 isdir
? new_dir
: remote_name
,
877 if (!NT_STATUS_IS_OK(status
)) {
882 DBG(5, ("--- %s\n", remote_name
));
883 status
= NT_STATUS_OK
;
887 rc
= tar_get_file(&tar_ctx
, remote_name
, finfo
);
889 status
= NT_STATUS_UNSUCCESSFUL
;
899 * tar_get_file - fetch a remote file to the local archive
900 * @full_dos_path: path to the file to fetch
901 * @finfo: attributes of the file to fetch
903 static int tar_get_file(struct tar
*t
,
904 const char *full_dos_path
,
905 struct file_info
*finfo
)
907 extern struct cli_state
*cli
;
909 struct archive_entry
*entry
;
910 char *full_unix_path
;
911 char buf
[TAR_CLI_READ_SIZE
];
914 uint16_t remote_fd
= (uint16_t)-1;
916 const bool isdir
= finfo
->attr
& FILE_ATTRIBUTE_DIRECTORY
;
917 TALLOC_CTX
*ctx
= talloc_new(NULL
);
923 DBG(5, ("+++ %s\n", full_dos_path
));
925 t
->total_size
+= finfo
->size
;
932 /* ignore return value: server might not store DOS attributes */
933 set_remote_attr(full_dos_path
, FILE_ATTRIBUTE_ARCHIVE
, ATTR_UNSET
);
936 full_unix_path
= talloc_asprintf(ctx
, ".%s", full_dos_path
);
937 if (full_unix_path
== NULL
) {
941 string_replace(full_unix_path
, '\\', '/');
942 entry
= archive_entry_new();
943 archive_entry_copy_pathname(entry
, full_unix_path
);
944 archive_entry_set_filetype(entry
, isdir
? AE_IFDIR
: AE_IFREG
);
945 archive_entry_set_atime(entry
,
946 finfo
->atime_ts
.tv_sec
,
947 finfo
->atime_ts
.tv_nsec
);
948 archive_entry_set_mtime(entry
,
949 finfo
->mtime_ts
.tv_sec
,
950 finfo
->mtime_ts
.tv_nsec
);
951 archive_entry_set_ctime(entry
,
952 finfo
->ctime_ts
.tv_sec
,
953 finfo
->ctime_ts
.tv_nsec
);
954 archive_entry_set_perm(entry
, isdir
? 0755 : 0644);
956 * check if we can safely cast unsigned file size to libarchive
957 * signed size. Very unlikely problem (>9 exabyte file)
959 if (finfo
->size
> INT64_MAX
) {
960 d_printf("Remote file %s too big\n", full_dos_path
);
964 archive_entry_set_size(entry
, (int64_t)finfo
->size
);
967 /* It's a directory just write a header */
968 r
= archive_write_header(t
->archive
, entry
);
969 if (r
!= ARCHIVE_OK
) {
970 d_printf("Fatal: %s\n", archive_error_string(t
->archive
));
973 if (t
->mode
.verbose
) {
974 d_printf("a %s\\\n", full_dos_path
);
976 DBG(5, ("get_file skip dir %s\n", full_dos_path
));
980 status
= cli_open(cli
, full_dos_path
, O_RDONLY
, DENY_NONE
, &remote_fd
);
981 if (!NT_STATUS_IS_OK(status
)) {
982 d_printf("%s opening remote file %s\n",
983 nt_errstr(status
), full_dos_path
);
987 /* don't make tar file entry until after the file is open */
988 r
= archive_write_header(t
->archive
, entry
);
989 if (r
!= ARCHIVE_OK
) {
990 d_printf("Fatal: %s\n", archive_error_string(t
->archive
));
995 if (t
->mode
.verbose
) {
996 d_printf("a %s\n", full_dos_path
);
1000 status
= cli_read(cli
, remote_fd
, buf
, off
, sizeof(buf
), &len
);
1001 if (!NT_STATUS_IS_OK(status
)) {
1002 d_printf("Error reading file %s : %s\n",
1003 full_dos_path
, nt_errstr(status
));
1010 r
= archive_write_data(t
->archive
, buf
, len
);
1012 d_printf("Fatal: %s\n", archive_error_string(t
->archive
));
1017 } while (off
< finfo
->size
);
1021 cli_close(cli
, remote_fd
);
1024 archive_entry_free(entry
);
1032 * tar_extract - open archive and send files.
1034 static int tar_extract(struct tar
*t
)
1038 struct archive_entry
*entry
;
1039 const size_t bsize
= t
->mode
.blocksize
* TAR_BLOCK_UNIT
;
1042 t
->archive
= archive_read_new();
1043 archive_read_support_format_all(t
->archive
);
1044 #ifdef HAVE_ARCHIVE_READ_SUPPORT_FILTER_ALL
1045 archive_read_support_filter_all(t
->archive
);
1048 if (strequal(t
->tar_path
, "-")) {
1049 r
= archive_read_open_fd(t
->archive
, STDIN_FILENO
, bsize
);
1051 r
= archive_read_open_filename(t
->archive
, t
->tar_path
, bsize
);
1054 if (r
!= ARCHIVE_OK
) {
1055 d_printf("Can't open %s : %s\n", t
->tar_path
,
1056 archive_error_string(t
->archive
));
1064 r
= archive_read_next_header(t
->archive
, &entry
);
1065 if (r
== ARCHIVE_EOF
) {
1068 if (r
== ARCHIVE_WARN
) {
1069 d_printf("Warning: %s\n", archive_error_string(t
->archive
));
1071 if (r
== ARCHIVE_FATAL
) {
1072 d_printf("Fatal: %s\n", archive_error_string(t
->archive
));
1077 status
= tar_extract_skip_path(t
, entry
, &skip
);
1078 if (!NT_STATUS_IS_OK(status
)) {
1083 DBG(5, ("--- %s\n", archive_entry_pathname(entry
)));
1086 DBG(5, ("+++ %s\n", archive_entry_pathname(entry
)));
1088 if (t
->mode
.verbose
) {
1089 d_printf("x %s\n", archive_entry_pathname(entry
));
1092 rc
= tar_send_file(t
, entry
);
1100 #ifdef HAVE_ARCHIVE_READ_FREE
1101 r
= archive_read_free(t
->archive
);
1103 r
= archive_read_finish(t
->archive
);
1105 if (r
!= ARCHIVE_OK
) {
1106 d_printf("Can't close %s : %s\n", t
->tar_path
,
1107 archive_error_string(t
->archive
));
1114 * tar_send_file - send @entry to the remote server
1115 * @entry: current archive entry
1117 * Handle the creation of the parent directories and transfer the
1118 * entry to a new remote file.
1120 static int tar_send_file(struct tar
*t
, struct archive_entry
*entry
)
1122 extern struct cli_state
*cli
;
1126 uint16_t remote_fd
= (uint16_t) -1;
1128 int flags
= O_RDWR
| O_CREAT
| O_TRUNC
;
1129 mode_t filetype
= archive_entry_filetype(entry
);
1130 mode_t mode
= archive_entry_mode(entry
);
1131 time_t mtime
= archive_entry_mtime(entry
);
1133 TALLOC_CTX
*ctx
= talloc_new(NULL
);
1138 dos_path
= talloc_strdup(ctx
, archive_entry_pathname(entry
));
1139 if (dos_path
== NULL
) {
1143 fix_unix_path(dos_path
, true);
1145 full_path
= talloc_strdup(ctx
, client_get_cur_dir());
1146 if (full_path
== NULL
) {
1150 full_path
= talloc_strdup_append(full_path
, dos_path
);
1151 if (full_path
== NULL
) {
1155 full_path
= client_clean_name(ctx
, full_path
);
1156 if (full_path
== NULL
) {
1161 if (filetype
!= AE_IFREG
&& filetype
!= AE_IFDIR
) {
1162 d_printf("Skipping non-dir & non-regular file %s\n", full_path
);
1166 rc
= make_remote_path(full_path
);
1172 if (filetype
== AE_IFDIR
) {
1176 status
= cli_open(cli
, full_path
, flags
, DENY_NONE
, &remote_fd
);
1177 if (!NT_STATUS_IS_OK(status
)) {
1178 d_printf("Error opening remote file %s: %s\n",
1179 full_path
, nt_errstr(status
));
1190 r
= archive_read_data_block(t
->archive
, &buf
, &len
, &off
);
1191 if (r
== ARCHIVE_EOF
) {
1194 if (r
== ARCHIVE_WARN
) {
1195 d_printf("Warning: %s\n", archive_error_string(t
->archive
));
1197 if (r
== ARCHIVE_FATAL
) {
1198 d_printf("Fatal: %s\n", archive_error_string(t
->archive
));
1203 status
= cli_writeall(cli
, remote_fd
, 0, buf
, off
, len
, NULL
);
1204 if (!NT_STATUS_IS_OK(status
)) {
1205 d_printf("Error writing remote file %s: %s\n",
1206 full_path
, nt_errstr(status
));
1213 status
= cli_close(cli
, remote_fd
);
1214 if (!NT_STATUS_IS_OK(status
)) {
1215 d_printf("Error closing remote file %s: %s\n",
1216 full_path
, nt_errstr(status
));
1220 status
= cli_setatr(cli
, full_path
, mode
, mtime
);
1221 if (!NT_STATUS_IS_OK(status
)) {
1222 d_printf("Error setting attributes on remote file %s: %s\n",
1223 full_path
, nt_errstr(status
));
1233 * tar_add_selection_path - add a path to the path list
1234 * @path: path to add
1236 static NTSTATUS
tar_add_selection_path(struct tar
*t
, const char *path
)
1239 TALLOC_CTX
*ctx
= t
->talloc_ctx
;
1240 if (!t
->path_list
) {
1241 t
->path_list
= str_list_make_empty(ctx
);
1242 if (t
->path_list
== NULL
) {
1243 return NT_STATUS_NO_MEMORY
;
1245 t
->path_list_size
= 0;
1248 /* cast to silence gcc const-qual warning */
1249 list
= str_list_add((void *)t
->path_list
, path
);
1251 return NT_STATUS_NO_MEMORY
;
1253 t
->path_list
= discard_const_p(char *, list
);
1254 t
->path_list_size
++;
1255 fix_unix_path(t
->path_list
[t
->path_list_size
- 1], true);
1257 return NT_STATUS_OK
;
1261 * tar_set_blocksize - set block size in TAR_BLOCK_UNIT
1263 static int tar_set_blocksize(struct tar
*t
, int size
)
1265 if (size
<= 0 || size
> TAR_MAX_BLOCK_SIZE
) {
1269 t
->mode
.blocksize
= size
;
1275 * tar_set_newer_than - set date threshold of saved files
1276 * @filename: local path to a file
1278 * Only files newer than the modification time of @filename will be
1281 * Note: this function set the global variable newer_than from
1282 * client.c. Thus the time is not a field of the tar structure. See
1283 * cmd_newer() to change its value from an interactive session.
1285 static int tar_set_newer_than(struct tar
*t
, const char *filename
)
1287 extern time_t newer_than
;
1288 SMB_STRUCT_STAT stbuf
;
1291 rc
= sys_stat(filename
, &stbuf
, false);
1293 d_printf("Error setting newer-than time\n");
1297 newer_than
= convert_timespec_to_time_t(stbuf
.st_ex_mtime
);
1298 DBG(1, ("Getting files newer than %s\n", time_to_asc(newer_than
)));
1303 * tar_read_inclusion_file - set path list from file
1304 * @filename: path to the list file
1306 * Read and add each line of @filename to the path list.
1308 static int tar_read_inclusion_file(struct tar
*t
, const char* filename
)
1313 TALLOC_CTX
*ctx
= talloc_new(NULL
);
1318 fd
= open(filename
, O_RDONLY
);
1320 d_printf("Can't open inclusion file '%s': %s\n", filename
, strerror(errno
));
1325 for (line
= afdgets(fd
, ctx
, 0);
1327 line
= afdgets(fd
, ctx
, 0)) {
1329 status
= tar_add_selection_path(t
, line
);
1330 if (!NT_STATUS_IS_OK(status
)) {
1345 * tar_path_in_list - check whether @path is in the path list
1346 * @path: path to find
1347 * @reverse: when true also try to find path list element in @path
1348 * @_is_in_list: set if @path is in the path list
1350 * Look at each path of the path list and set @_is_in_list if @path is a
1351 * subpath of one of them.
1353 * If you want /path to be in the path list (path/a/, path/b/) set
1354 * @reverse to true to try to match the other way around.
1356 static NTSTATUS
tar_path_in_list(struct tar
*t
, const char *path
,
1357 bool reverse
, bool *_is_in_list
)
1361 const char *pattern
;
1363 if (path
== NULL
|| path
[0] == '\0') {
1364 *_is_in_list
= false;
1365 return NT_STATUS_OK
;
1368 p
= skip_useless_char_in_path(path
);
1370 for (i
= 0; i
< t
->path_list_size
; i
++) {
1374 pattern
= skip_useless_char_in_path(t
->path_list
[i
]);
1375 status
= is_subpath(p
, pattern
, &is_in_list
);
1376 if (!NT_STATUS_IS_OK(status
)) {
1379 if (reverse
&& !is_in_list
) {
1380 status
= is_subpath(pattern
, p
, &is_in_list
);
1381 if (!NT_STATUS_IS_OK(status
)) {
1386 *_is_in_list
= true;
1387 return NT_STATUS_OK
;
1391 *_is_in_list
= false;
1392 return NT_STATUS_OK
;
1396 * tar_extract_skip_path - check if @entry should be skipped
1397 * @entry: current tar entry
1398 * @_skip: set true if path should be skipped, otherwise false
1400 * Skip predicate for tar extraction (archive to server) only.
1402 static NTSTATUS
tar_extract_skip_path(struct tar
*t
,
1403 struct archive_entry
*entry
,
1406 const char *fullpath
= archive_entry_pathname(entry
);
1409 if (t
->path_list_size
<= 0) {
1411 return NT_STATUS_OK
;
1414 if (t
->mode
.regex
) {
1415 in
= mask_match_list(fullpath
, t
->path_list
, t
->path_list_size
, true);
1417 NTSTATUS status
= tar_path_in_list(t
, fullpath
, false, &in
);
1418 if (!NT_STATUS_IS_OK(status
)) {
1423 if (t
->mode
.selection
== TAR_EXCLUDE
) {
1429 return NT_STATUS_OK
;
1433 * tar_create_skip_path - check if @fullpath shoud be skipped
1434 * @fullpath: full remote path of the current file
1435 * @finfo: remote file attributes
1436 * @_skip: returned skip not
1438 * Skip predicate for tar creation (server to archive) only.
1440 static NTSTATUS
tar_create_skip_path(struct tar
*t
,
1441 const char *fullpath
,
1442 const struct file_info
*finfo
,
1445 /* syntaxic sugar */
1446 const mode_t mode
= finfo
->attr
;
1447 const bool isdir
= mode
& FILE_ATTRIBUTE_DIRECTORY
;
1448 const bool exclude
= t
->mode
.selection
== TAR_EXCLUDE
;
1453 /* 1. if we don't want X and we have X, skip */
1454 if (!t
->mode
.system
&& (mode
& FILE_ATTRIBUTE_SYSTEM
)) {
1456 return NT_STATUS_OK
;
1459 if (!t
->mode
.hidden
&& (mode
& FILE_ATTRIBUTE_HIDDEN
)) {
1461 return NT_STATUS_OK
;
1464 /* 2. if we only want archive and it's not, skip */
1466 if (t
->mode
.incremental
&& !(mode
& FILE_ATTRIBUTE_ARCHIVE
)) {
1468 return NT_STATUS_OK
;
1472 /* 3. is it in the selection list? */
1475 * tar_create_from_list() use the include list as a starting
1476 * point, no need to check
1480 return NT_STATUS_OK
;
1483 /* we are now in exclude mode */
1485 /* no matter the selection, no list => include everything */
1486 if (t
->path_list_size
<= 0) {
1488 return NT_STATUS_OK
;
1491 if (t
->mode
.regex
) {
1492 in
= mask_match_list(fullpath
, t
->path_list
, t
->path_list_size
, true);
1494 bool reverse
= isdir
&& !exclude
;
1495 NTSTATUS status
= tar_path_in_list(t
, fullpath
, reverse
, &in
);
1496 if (!NT_STATUS_IS_OK(status
)) {
1502 return NT_STATUS_OK
;
1506 * tar_to_process - return true if @t is ready to be processed
1508 * @t is ready if it properly parsed command line arguments.
1510 bool tar_to_process(struct tar
*t
)
1513 DBG_WARNING("Invalid tar context\n");
1516 return t
->to_process
;
1520 * skip_useless_char_in_path - skip leading slashes/dots
1522 * Skip leading slashes, backslashes and dot-slashes.
1524 static const char* skip_useless_char_in_path(const char *p
)
1527 if (*p
== '/' || *p
== '\\') {
1530 else if (p
[0] == '.' && (p
[1] == '/' || p
[1] == '\\')) {
1540 * is_subpath - check if the path @sub is a subpath of @full.
1541 * @sub: path to test
1542 * @full: container path
1543 * @_subpath_match: set true if @sub is a subpath of @full, otherwise false
1545 * String comparaison is case-insensitive.
1547 static NTSTATUS
is_subpath(const char *sub
, const char *full
,
1548 bool *_subpath_match
)
1550 NTSTATUS status
= NT_STATUS_OK
;
1553 TALLOC_CTX
*tmp_ctx
= talloc_new(NULL
);
1554 if (tmp_ctx
== NULL
) {
1555 status
= NT_STATUS_NO_MEMORY
;
1559 f
= strlower_talloc(tmp_ctx
, full
);
1561 status
= NT_STATUS_NO_MEMORY
;
1564 string_replace(f
, '\\', '/');
1565 s
= strlower_talloc(tmp_ctx
, sub
);
1567 status
= NT_STATUS_NO_MEMORY
;
1570 string_replace(s
, '\\', '/');
1572 /* find the point where sub and full diverge */
1573 while ((*f
!= '\0') && (*s
!= '\0') && (*f
== *s
)) {
1579 if ((*f
== '\0') && (*s
== '\0')) {
1580 *_subpath_match
= true; /* sub and full match */
1584 if ((*f
== '\0') && (len
> 0) && (*(f
- 1) == '/')) {
1585 /* sub diverges from full at path separator */
1586 *_subpath_match
= true;
1590 if ((*s
== '\0') && (strcmp(f
, "/") == 0)) {
1591 /* full diverges from sub with trailing slash only */
1592 *_subpath_match
= true;
1596 if ((*s
== '/') && (*f
== '\0')) {
1597 /* sub diverges from full with extra path component */
1598 *_subpath_match
= true;
1601 *_subpath_match
= false;
1604 talloc_free(tmp_ctx
);
1611 * make_remote_path - recursively make remote dirs
1612 * @full_path: full hierarchy to create
1614 * Create @full_path and each parent directories as needed.
1616 static int make_remote_path(const char *full_path
)
1618 extern struct cli_state
*cli
;
1622 char *last_backslash
;
1627 TALLOC_CTX
*ctx
= talloc_new(NULL
);
1632 subpath
= talloc_strdup(ctx
, full_path
);
1633 if (subpath
== NULL
) {
1637 path
= talloc_strdup(ctx
, full_path
);
1642 len
= talloc_get_size(path
) - 1;
1644 last_backslash
= strrchr_m(path
, '\\');
1645 if (last_backslash
== NULL
) {
1649 *last_backslash
= 0;
1652 p
= strtok_r(path
, "\\", &state
);
1655 strlcat(subpath
, p
, len
);
1656 status
= cli_chkpath(cli
, subpath
);
1657 if (!NT_STATUS_IS_OK(status
)) {
1658 status
= cli_mkdir(cli
, subpath
);
1659 if (!NT_STATUS_IS_OK(status
)) {
1660 d_printf("Can't mkdir %s: %s\n", subpath
, nt_errstr(status
));
1664 DBG(3, ("mkdir %s\n", subpath
));
1667 strlcat(subpath
, "\\", len
);
1668 p
= strtok_r(NULL
, "/\\", &state
);
1678 * tar_reset_mem_context - reset talloc context associated with @t
1680 * At the start of the program the context is NULL so a new one is
1681 * allocated. On the following runs (interactive session only), simply
1682 * free the children.
1684 static TALLOC_CTX
*tar_reset_mem_context(struct tar
*t
)
1686 tar_free_mem_context(t
);
1687 t
->talloc_ctx
= talloc_new(NULL
);
1688 return t
->talloc_ctx
;
1692 * tar_free_mem_context - free talloc context associated with @t
1694 static void tar_free_mem_context(struct tar
*t
)
1696 if (t
->talloc_ctx
) {
1697 talloc_free(t
->talloc_ctx
);
1698 t
->talloc_ctx
= NULL
;
1699 t
->path_list_size
= 0;
1700 t
->path_list
= NULL
;
1705 #define XSET(v) [v] = #v
1706 #define XTABLE(v, t) DBG(2, ("DUMP:%-20.20s = %s\n", #v, t[v]))
1707 #define XBOOL(v) DBG(2, ("DUMP:%-20.20s = %d\n", #v, v ? 1 : 0))
1708 #define XSTR(v) DBG(2, ("DUMP:%-20.20s = %s\n", #v, v ? v : "NULL"))
1709 #define XINT(v) DBG(2, ("DUMP:%-20.20s = %d\n", #v, v))
1710 #define XUINT64(v) DBG(2, ("DUMP:%-20.20s = %" PRIu64 "\n", #v, v))
1713 * tar_dump - dump tar structure on stdout
1715 static void tar_dump(struct tar
*t
)
1718 const char* op
[] = {
1719 XSET(TAR_NO_OPERATION
),
1724 const char* sel
[] = {
1725 XSET(TAR_NO_SELECTION
),
1730 XBOOL(t
->to_process
);
1731 XTABLE(t
->mode
.operation
, op
);
1732 XTABLE(t
->mode
.selection
, sel
);
1733 XINT(t
->mode
.blocksize
);
1734 XBOOL(t
->mode
.hidden
);
1735 XBOOL(t
->mode
.system
);
1736 XBOOL(t
->mode
.incremental
);
1737 XBOOL(t
->mode
.reset
);
1739 XBOOL(t
->mode
.verbose
);
1740 XUINT64(t
->total_size
);
1742 XINT(t
->path_list_size
);
1744 for (i
= 0; t
->path_list
&& t
->path_list
[i
]; i
++) {
1745 DBG(2, ("DUMP: t->path_list[%2d] = %s\n", i
, t
->path_list
[i
]));
1748 DBG(2, ("DUMP:t->path_list @ %p (%d elem)\n", t
->path_list
, i
));
1757 * max_token - return upper limit for the number of token in @str
1759 * The result is not exact, the actual number of token might be less
1760 * than what is returned.
1762 static int max_token(const char *str
)
1772 while (s
[0] != '\0') {
1773 if (isspace((int)s
[0])) {
1785 * fix_unix_path - convert @path to a DOS path
1786 * @path: path to convert
1787 * @removeprefix: if true, remove leading ./ or /.
1789 static char *fix_unix_path(char *path
, bool do_remove_prefix
)
1791 char *from
= path
, *to
= path
;
1793 if (path
== NULL
|| path
[0] == '\0') {
1801 if (do_remove_prefix
) {
1803 if (path
[0] == '/' || path
[0] == '\\') {
1808 if (path
[1] != '\0' && path
[0] == '.' && (path
[1] == '/' || path
[1] == '\\')) {
1813 /* replace / with \ */
1814 while (from
[0] != '\0') {
1815 if (from
[0] == '/') {
1830 * path_base_name - return @path basename
1832 * If @path doesn't contain any directory separator return NULL.
1834 static NTSTATUS
path_base_name(TALLOC_CTX
*ctx
, const char *path
, char **_base
)
1840 for (i
= 0; path
[i
]; i
++) {
1841 if (path
[i
] == '\\' || path
[i
] == '/') {
1847 base
= talloc_strdup(ctx
, path
);
1849 return NT_STATUS_NO_MEMORY
;
1856 return NT_STATUS_OK
;
1861 #define NOT_IMPLEMENTED DEBUG(0, ("tar mode not compiled. build used --without-libarchive\n"))
1869 int cmd_tarmode(void)
1881 int tar_process(struct tar
* tar
)
1887 int tar_parse_args(struct tar
*tar
, const char *flag
, const char **val
, int valsize
)
1893 bool tar_to_process(struct tar
*tar
)
1898 struct tar
*tar_get_ctx()