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 #include "system/filesys.h"
22 #include "client/client_proto.h"
23 #include "client/clitar_proto.h"
24 #include "libsmb/libsmb.h"
27 #define LEN(x) (sizeof(x)/sizeof((x)[0]))
28 #define TAR_MAX_BLOCK_SIZE 65535
30 * XXX: used in client.c, we have to export it for now.
31 * corresponds to the transfer operation. Can be '\0', 'c' or 'x'
37 TAR_CREATE, /* c flag */
38 TAR_EXTRACT, /* x flag */
43 TAR_INCLUDE, /* I flag, default */
44 TAR_INCLUDE_LIST, /* F flag */
45 TAR_EXCLUDE, /* X flag */
56 enum tar_operation operation; /* create, extract */
57 enum tar_selection selection; /* inc, inc from file, exclude */
58 int blocksize; /* size in bytes of a block in the tar file */
59 bool hidden; /* backup hidden file? */
60 bool system; /* backup system file? */
61 bool incremental; /* backup _only_ archived file? */
62 bool reset; /* unset archive bit? */
63 bool dry; /* don't write tar file? */
64 bool regex; /* XXX: never actually using regex... */
68 /* path to tar archive name */
71 /* file descriptor of tar file */
74 /* list of path to include or exclude */
78 struct archive *archive;
81 struct tar tar_ctx = {
82 .mode.selection = TAR_INCLUDE,
86 .mode.incremental = False,
90 static int tar_set_blocksize(struct tar *t, int size)
92 if (size <= 0 || size > TAR_MAX_BLOCK_SIZE) {
96 t->mode.blocksize = size;
101 static bool tar_set_newer_than(struct tar *t, char *filename)
103 extern time_t newer_than;
104 SMB_STRUCT_STAT stbuf;
106 if (sys_stat(filename, &stbuf, false) != 0) {
107 DEBUG(0, ("Error setting newer-than time\n"));
111 newer_than = convert_timespec_to_time_t(stbuf.st_ex_mtime);
112 DEBUG(1, ("Getting files newer than %s\n", time_to_asc(newer_than)));
116 static bool tar_read_inclusion_file (struct tar *t, const char* filename)
120 TALLOC_CTX *ctx = talloc_tos();
121 int fd = open(filename, O_RDONLY);
124 DEBUG(0, ("Can't open inclusion file '%s': %s\n", filename, strerror(errno)));
128 list = str_list_make_empty(ctx);
130 while ((line = afdgets(fd, ctx, 0))) {
131 list = str_list_add(list, line);
140 * cmd_block - interactive command to change tar blocksize
142 * Read a size from the client command line and update the current
147 /* XXX: from client.c */
148 const extern char *cmd_ptr;
150 TALLOC_CTX *ctx = talloc_tos();
152 if (!next_token_talloc(ctx, &cmd_ptr, &buf, NULL)) {
153 DEBUG(0, ("blocksize <n>\n"));
157 if(!tar_set_blocksize(&tar_ctx, atoi(buf))) {
158 DEBUG(0, ("invalid blocksize\n"));
161 DEBUG(2, ("blocksize is now %d\n", tar_ctx.mode.blocksize));
167 * cmd_tarmode - interactive command to change tar behaviour
169 * Read one or more modes from the client command line and update the
172 int cmd_tarmode(void)
174 const extern char *cmd_ptr;
177 TALLOC_CTX *ctx = talloc_tos();
184 {"full", &tar_ctx.mode.incremental, False},
185 {"inc", &tar_ctx.mode.incremental, True },
186 {"reset", &tar_ctx.mode.reset, True },
187 {"noreset", &tar_ctx.mode.reset, False},
188 {"system", &tar_ctx.mode.system, True },
189 {"nosystem", &tar_ctx.mode.system, False},
190 {"hidden", &tar_ctx.mode.hidden, True },
191 {"nohidden", &tar_ctx.mode.hidden, False},
192 {"verbose", &tar_ctx.mode.verbose, True },
193 {"noquiet", &tar_ctx.mode.verbose, True },
194 {"quiet", &tar_ctx.mode.verbose, False},
195 {"noverbose", &tar_ctx.mode.verbose, False},
198 while (next_token_talloc(ctx, &cmd_ptr, &buf, NULL)) {
199 for (i = 0; i < LEN(table); i++) {
200 if (strequal(table[i].cmd, buf)) {
201 *table[i].p = table[i].value;
207 DEBUG(0, ("tarmode: unrecognised option %s\n", buf));
212 DEBUG(0, ("tarmode is now %s, %s, %s, %s, %s\n",
213 tar_ctx.mode.incremental ? "incremental" : "full",
214 tar_ctx.mode.system ? "system" : "nosystem",
215 tar_ctx.mode.hidden ? "hidden" : "nohidden",
216 tar_ctx.mode.reset ? "reset" : "noreset",
217 tar_ctx.mode.verbose ? "verbose" : "quiet"));
222 * set_remote_attr - set DOS attributes of a remote file
223 * @filename: path to the file name
224 * @new_attr: attribute bit mask to use
225 * @mode: one of ATTR_SET or ATTR_UNSET
227 * Update the file attributes with the one provided.
229 static void set_remote_attr(char *filename, uint16 new_attr, int mode)
231 extern struct cli_state *cli;
235 if (!NT_STATUS_IS_OK(cli_getatr(cli, filename, &old_attr, NULL, NULL))) {
236 /* XXX: debug message */
240 if (mode == ATTR_SET) {
241 new_attr |= old_attr;
243 new_attr = old_attr & ~new_attr;
246 status = cli_setatr(cli, filename, new_attr, 0);
247 if (!NT_STATUS_IS_OK(status)) {
248 DEBUG(1, ("setatr failed: %s\n", nt_errstr(status)));
253 * cmd_setmode - interactive command to set DOS attributes
255 * Read a filename and mode from the client command line and update
256 * the file DOS attributes.
258 int cmd_setmode(void)
260 const extern char *cmd_ptr;
263 uint16 attr[2] = {0};
265 TALLOC_CTX *ctx = talloc_tos();
268 if (!next_token_talloc(ctx, &cmd_ptr, &buf, NULL)) {
269 DEBUG(0, ("setmode <filename> <[+|-]rsha>\n"));
273 fname = talloc_asprintf(ctx,
275 client_get_cur_dir(),
281 while (next_token_talloc(ctx, &cmd_ptr, &buf, NULL)) {
293 attr[mode] |= FILE_ATTRIBUTE_READONLY;
296 attr[mode] |= FILE_ATTRIBUTE_HIDDEN;
299 attr[mode] |= FILE_ATTRIBUTE_SYSTEM;
302 attr[mode] |= FILE_ATTRIBUTE_ARCHIVE;
305 DEBUG(0, ("setmode <filename> <perm=[+|-]rsha>\n"));
311 if (attr[ATTR_SET] == 0 && attr[ATTR_UNSET] == 0) {
312 DEBUG(0, ("setmode <filename> <[+|-]rsha>\n"));
316 DEBUG(2, ("\nperm set %d %d\n", attr[ATTR_SET], attr[ATTR_UNSET]));
317 set_remote_attr(fname, attr[ATTR_SET], ATTR_SET);
318 set_remote_attr(fname, attr[ATTR_UNSET], ATTR_UNSET);
324 * cmd_tar - interactive command to start a tar backup/restoration
326 * Check presence of argument, parse them and handle the request.
333 /****************************************************************************
334 Command line (option) version
335 ***************************************************************************/
337 int process_tar(void)
343 * tar_parse_args - parse and set tar command line arguments
344 * @flag: string pointing to tar options
345 * @val: number of tar arguments
346 * @valsize: table of arguments after the flags (number of element in val)
348 * tar arguments work in a weird way. For each flag f that takes a
349 * value v, the user is supposed to type:
352 * -Tf1f2f3 v1 v2 v3 TARFILE PATHS...
354 * in the interactive session:
355 * tar f1f2f3 v1 v2 v3 TARFILE PATHS...
357 * opt has only flags (eg. "f1f2f3") and val has the arguments
358 * (values) following them (eg. ["v1", "v2", "v3", "TARFILE", "PATH1",
361 * There are only 2 flags that take an arg: b and N. The other flags
362 * just change the semantic of PATH or TARFILE.
364 * PATH can be a list of included/excluded paths, the path to a file
365 * containing a list of included/excluded paths to use (F flag). If no
366 * PATH is provided, the whole share is used (/).
368 int tar_parse_args(struct tar* t, const char *flag, const char **val, int valsize)
370 TALLOC_CTX *ctx = talloc_tos();
372 /* index of next value to use */
376 * Reset back some options - could be from interactive version
377 * all other modes are left as they are
379 t->mode.operation = TAR_NO_OPERATION;
380 t->mode.selection = TAR_NO_SELECTION;
387 if (t->mode.operation != TAR_NO_OPERATION) {
388 printf("Tar must be followed by only one of c or x.\n");
391 t->mode.operation = TAR_CREATE;
394 if (t->mode.operation != TAR_NO_OPERATION) {
395 printf("Tar must be followed by only one of c or x.\n");
398 t->mode.operation = TAR_CREATE;
403 if (t->mode.selection != TAR_NO_SELECTION) {
404 DEBUG(0,("Only one of I,X,F must be specified\n"));
407 t->mode.selection = TAR_INCLUDE;
410 if (t->mode.selection != TAR_NO_SELECTION) {
411 DEBUG(0,("Only one of I,X,F must be specified\n"));
414 t->mode.selection = TAR_EXCLUDE;
417 if (t->mode.selection != TAR_NO_SELECTION) {
418 DEBUG(0,("Only one of I,X,F must be specified\n"));
421 t->mode.selection = TAR_INCLUDE_LIST;
426 if (ival >= valsize) {
427 DEBUG(0, ("Option b must be followed by a blocksize\n"));
431 if (!tar_set_blocksize(t, atoi(val[ival]))) {
432 DEBUG(0, ("Option b must be followed by a valid blocksize\n"));
439 /* incremental mode */
441 t->mode.incremental = True;
446 if (ival >= valsize) {
447 DEBUG(0, ("Option N must be followed by valid file name\n"));
451 if (!tar_set_newer_than(t, val[ival])) {
452 DEBUG(0,("Error setting newer-than time\n"));
461 t->mode.reset = True;
466 t->mode.verbose = True;
471 t->mode.regex = True;
476 if (t->mode.operation != TAR_CREATE) {
477 DEBUG(0, ("n is only meaningful when creating a tar-file\n"));
482 DEBUG(0, ("dry_run set\n"));
486 DEBUG(0,("Unknown tar option\n"));
491 /* no operation given? default operation is include */
492 if (t->mode.operation == TAR_NO_OPERATION) {
493 t->mode.operation = TAR_INCLUDE;
496 if (valsize - ival < 1) {
497 DEBUG(0, ("No tar file given.\n"));
502 t->tar_path = talloc_strdup(ctx, val[ival]);
505 /* handle PATHs... */
506 tar_ctx.path_list = str_list_make_empty(ctx);
508 /* flag F -> read file list */
509 if (t->mode.selection == TAR_INCLUDE_LIST) {
510 if (valsize - ival != 1) {
511 DEBUG(0,("Option F must be followed by exactly one filename.\n"));
515 if(!tar_read_inclusion_file(t, val[ival])) {
521 /* otherwise store all the PATHs on the command line */
524 for (i = ival; i < valsize; i++) {
525 t->path_list = str_list_add(t->path_list, val[i]);