3 * Bacula Director -- User Agent Database File tree for Restore
4 * command. This file interacts with the user implementing the
7 * Kern Sibbald, July MMII
13 Copyright (C) 2002-2004 Kern Sibbald and John Walker
15 This program is free software; you can redistribute it and/or
16 modify it under the terms of the GNU General Public License as
17 published by the Free Software Foundation; either version 2 of
18 the License, or (at your option) any later version.
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 General Public License for more details.
25 You should have received a copy of the GNU General Public
26 License along with this program; if not, write to the Free
27 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
35 #include "findlib/find.h"
38 /* Forward referenced commands */
40 static int markcmd(UAContext *ua, TREE_CTX *tree);
41 static int markdircmd(UAContext *ua, TREE_CTX *tree);
42 static int countcmd(UAContext *ua, TREE_CTX *tree);
43 static int findcmd(UAContext *ua, TREE_CTX *tree);
44 static int lscmd(UAContext *ua, TREE_CTX *tree);
45 static int lsmarkcmd(UAContext *ua, TREE_CTX *tree);
46 static int dircmd(UAContext *ua, TREE_CTX *tree);
47 static int estimatecmd(UAContext *ua, TREE_CTX *tree);
48 static int helpcmd(UAContext *ua, TREE_CTX *tree);
49 static int cdcmd(UAContext *ua, TREE_CTX *tree);
50 static int pwdcmd(UAContext *ua, TREE_CTX *tree);
51 static int unmarkcmd(UAContext *ua, TREE_CTX *tree);
52 static int unmarkdircmd(UAContext *ua, TREE_CTX *tree);
53 static int quitcmd(UAContext *ua, TREE_CTX *tree);
54 static int donecmd(UAContext *ua, TREE_CTX *tree);
57 struct cmdstruct { char *key; int (*func)(UAContext *ua, TREE_CTX *tree); char *help; };
58 static struct cmdstruct commands[] = {
59 { N_("cd"), cdcmd, _("change current directory")},
60 { N_("count"), countcmd, _("count marked files in and below the cd")},
61 { N_("dir"), dircmd, _("list current directory")},
62 { N_("done"), donecmd, _("leave file selection mode")},
63 { N_("estimate"), estimatecmd, _("estimate restore size")},
64 { N_("exit"), donecmd, _("exit = done")},
65 { N_("find"), findcmd, _("find files -- wildcards allowed")},
66 { N_("help"), helpcmd, _("print help")},
67 { N_("ls"), lscmd, _("list current directory -- wildcards allowed")},
68 { N_("lsmark"), lsmarkcmd, _("list the marked files in and below the cd")},
69 { N_("mark"), markcmd, _("mark file to be restored")},
70 { N_("markdir"), markdircmd, _("mark directory entry to be restored -- nonrecursive")},
71 { N_("pwd"), pwdcmd, _("print current working directory")},
72 { N_("unmark"), unmarkcmd, _("unmark file to be restored")},
73 { N_("unmarkdir"), unmarkdircmd, _("unmark directory -- no recursion")},
74 { N_("quit"), quitcmd, _("quit")},
75 { N_("?"), helpcmd, _("print help")},
77 #define comsize (sizeof(commands)/sizeof(struct cmdstruct))
81 * Enter a prompt mode where the user can select/deselect
82 * files to be restored. This is sort of like a mini-shell
83 * that allows "cd", "pwd", "add", "rm", ...
85 bool user_select_files_from_tree(TREE_CTX *tree)
89 /* Get a new context so we don't destroy restore command args */
90 UAContext *ua = new_ua_context(tree->ua->jcr);
91 ua->UA_sock = tree->ua->UA_sock; /* patch in UA socket */
94 "\nYou are now entering file selection mode where you add and\n"
95 "remove files to be restored. All files are initially added.\n"
96 "Enter \"done\" to leave this mode.\n\n"));
98 * Enter interactive command handler allowing selection
99 * of individual files.
101 tree->node = (TREE_NODE *)tree->root;
102 tree_getpath(tree->node, cwd, sizeof(cwd));
103 bsendmsg(tree->ua, _("cwd is: %s\n"), cwd);
106 if (!get_cmd(ua, "$ ")) {
114 len = strlen(ua->argk[0]);
117 for (i=0; i<(int)comsize; i++) /* search for command */
118 if (strncasecmp(ua->argk[0], _(commands[i].key), len) == 0) {
119 stat = (*commands[i].func)(ua, tree); /* go execute command */
124 bsendmsg(tree->ua, _("Illegal command. Enter \"done\" to exit.\n"));
131 ua->UA_sock = NULL; /* don't release restore socket */
134 free_ua_context(ua); /* get rid of temp UA context */
140 * This callback routine is responsible for inserting the
141 * items it gets into the directory tree. For each JobId selected
142 * this routine is called once for each file. We do not allow
143 * duplicate filenames, but instead keep the info from the most
144 * recent file entered (i.e. the JobIds are assumed to be sorted)
146 * See uar_sel_files in sql_cmds.c for query that calls us.
147 * row[0]=Path, row[1]=Filename, row[2]=FileIndex
148 * row[3]=JobId row[4]=LStat
150 int insert_tree_handler(void *ctx, int num_fields, char **row)
153 TREE_CTX *tree = (TREE_CTX *)ctx;
155 TREE_NODE *node, *new_node;
159 strip_trailing_junk(row[1]);
160 if (*row[1] == 0) { /* no filename => directory */
161 if (*row[0] != '/') { /* Must be Win32 directory */
169 if (tree->avail_node) {
170 node = tree->avail_node; /* if prev node avail use it */
172 node = new_tree_node(tree->root, type); /* get new node */
173 tree->avail_node = node;
175 hard_link = (decode_LinkFI(row[4], &statp) != 0);
176 bsnprintf(fname, sizeof(fname), "%s%s%s", row[0], row[1], "");
177 // S_ISLNK(statp.st_mode)?" ->":"");
178 Dmsg3(200, "FI=%d type=%d fname=%s\n", node->FileIndex, type, fname);
179 new_node = insert_tree_node(fname, node, tree->root, NULL);
180 /* Note, if node already exists, save new one for next time */
181 if (new_node != node) {
182 tree->avail_node = node; /* node already exists */
184 tree->avail_node = NULL; /* added node to tree */
187 * If the user has backed up a hard linked file twice, the
188 * second copy will be a pointer (i.e. hard link will be set),
189 * so we do not overwrite the original file with a pointer file.
191 if (!new_node->hard_link || hard_link) {
192 new_node->hard_link = hard_link;
193 new_node->FileIndex = atoi(row[2]);
194 new_node->JobId = (JobId_t)str_to_int64(row[3]);
195 new_node->type = type;
196 new_node->extract = true; /* extract all by default */
197 new_node->soft_link = S_ISLNK(statp.st_mode) != 0;
198 if (type == TN_DIR || type == TN_DIR_NLS) {
199 new_node->extract_dir = true; /* if dir, extract it */
208 * Set extract to value passed. We recursively walk
209 * down the tree setting all children if the
210 * node is a directory.
212 static int set_extract(UAContext *ua, TREE_NODE *node, TREE_CTX *tree, bool extract)
219 node->extract = extract;
220 if (node->type == TN_DIR || node->type == TN_DIR_NLS) {
221 node->extract_dir = extract; /* set/clear dir too */
223 if (node->type != TN_NEWDIR) {
226 /* For a non-file (i.e. directory), we see all the children */
227 if (node->type != TN_FILE || (node->soft_link && node->child)) {
228 /* Recursive set children within directory */
229 for (n=node->child; n; n=n->sibling) {
230 count += set_extract(ua, n, tree, extract);
233 * Walk up tree marking any unextracted parent to be
237 while (node->parent && !node->parent->extract_dir) {
239 node->extract_dir = true;
242 } else if (extract) {
245 * Ordinary file, we get the full path, look up the
246 * attributes, decode them, and if we are hard linked to
247 * a file that was saved, we must load that file too.
249 tree_getpath(node, cwd, sizeof(cwd));
251 fdbr.JobId = node->JobId;
252 if (node->hard_link && db_get_file_attributes_record(ua->jcr, ua->db, cwd, NULL, &fdbr)) {
254 decode_stat(fdbr.LStat, &statp, &LinkFI); /* decode stat pkt */
256 * If we point to a hard linked file, traverse the tree to
257 * find that file, and mark it to be restored as well. It
258 * must have the Link we just obtained and the same JobId.
261 for (n=first_tree_node(tree->root); n; n=next_tree_node(n)) {
262 if (n->FileIndex == LinkFI && n->JobId == node->JobId) {
264 if (n->type == TN_DIR || n->type == TN_DIR_NLS) {
265 n->extract_dir = true;
277 * Recursively mark the current directory to be restored as
278 * well as all directories and files below it.
280 static int markcmd(UAContext *ua, TREE_CTX *tree)
285 if (ua->argc < 2 || !tree->node->child) {
286 bsendmsg(ua, _("No files marked.\n"));
289 for (int i=1; i < ua->argc; i++) {
290 for (node = tree->node->child; node; node=node->sibling) {
291 if (fnmatch(ua->argk[i], node->fname, 0) == 0) {
292 count += set_extract(ua, node, tree, true);
297 bsendmsg(ua, _("No files marked.\n"));
299 bsendmsg(ua, _("%d file%s marked.\n"), count, count==0?"":"s");
304 static int markdircmd(UAContext *ua, TREE_CTX *tree)
309 if (ua->argc < 2 || !tree->node->child) {
310 bsendmsg(ua, _("No files marked.\n"));
313 for (int i=1; i < ua->argc; i++) {
314 for (node = tree->node->child; node; node=node->sibling) {
315 if (fnmatch(ua->argk[i], node->fname, 0) == 0) {
316 if (node->type == TN_DIR || node->type == TN_DIR_NLS) {
317 node->extract_dir = true;
324 bsendmsg(ua, _("No directories marked.\n"));
326 bsendmsg(ua, _("%d director%s marked.\n"), count, count==1?"y":"ies");
332 static int countcmd(UAContext *ua, TREE_CTX *tree)
334 int total, num_extract;
336 total = num_extract = 0;
337 for (TREE_NODE *node=first_tree_node(tree->root); node; node=next_tree_node(node)) {
338 if (node->type != TN_NEWDIR) {
340 if (node->extract || node->extract_dir) {
345 bsendmsg(ua, "%d total files/dirs. %d marked to be restored.\n", total, num_extract);
349 static int findcmd(UAContext *ua, TREE_CTX *tree)
354 bsendmsg(ua, _("No file specification given.\n"));
358 for (int i=1; i < ua->argc; i++) {
359 for (TREE_NODE *node=first_tree_node(tree->root); node; node=next_tree_node(node)) {
360 if (fnmatch(ua->argk[i], node->fname, 0) == 0) {
362 tree_getpath(node, cwd, sizeof(cwd));
365 } else if (node->extract_dir) {
370 bsendmsg(ua, "%s%s\n", tag, cwd);
379 static int lscmd(UAContext *ua, TREE_CTX *tree)
383 if (!tree->node->child) {
386 for (node = tree->node->child; node; node=node->sibling) {
387 if (ua->argc == 1 || fnmatch(ua->argk[1], node->fname, 0) == 0) {
391 } else if (node->extract_dir) {
396 bsendmsg(ua, "%s%s%s\n", tag, node->fname, node->child?"/":"");
403 * Ls command that lists only the marked files
405 static void rlsmark(UAContext *ua, TREE_NODE *node)
410 for (node = node->child; node; node=node->sibling) {
411 if ((ua->argc == 1 || fnmatch(ua->argk[1], node->fname, 0) == 0) &&
412 (node->extract || node->extract_dir)) {
416 } else if (node->extract_dir) {
421 bsendmsg(ua, "%s%s%s\n", tag, node->fname, node->child?"/":"");
429 static int lsmarkcmd(UAContext *ua, TREE_CTX *tree)
431 rlsmark(ua, tree->node);
437 extern char *getuser(uid_t uid);
438 extern char *getgroup(gid_t gid);
441 * This is actually the long form used for "dir"
443 static void ls_output(char *buf, char *fname, char *tag, struct stat *statp)
449 p = encode_mode(statp->st_mode, buf);
450 n = sprintf(p, " %2d ", (uint32_t)statp->st_nlink);
452 n = sprintf(p, "%-8.8s %-8.8s", getuser(statp->st_uid), getgroup(statp->st_gid));
454 n = sprintf(p, "%8.8s ", edit_uint64(statp->st_size, ec1));
456 p = encode_time(statp->st_ctime, p);
459 for (f=fname; *f; ) {
467 * Like ls command, but give more detail on each file
469 static int dircmd(UAContext *ua, TREE_CTX *tree)
475 char cwd[1100], *pcwd;
477 if (!tree->node->child) {
480 for (node = tree->node->child; node; node=node->sibling) {
482 if (ua->argc == 1 || fnmatch(ua->argk[1], node->fname, 0) == 0) {
485 } else if (node->extract_dir) {
490 tree_getpath(node, cwd, sizeof(cwd));
492 fdbr.JobId = node->JobId;
494 * Strip / from soft links to directories.
495 * This is because soft links to files have a trailing slash
496 * when returned from tree_getpath, but db_get_file_attr...
497 * treats soft links as files, so they do not have a trailing
498 * slash like directory names.
500 if (node->type == TN_FILE && node->child) {
501 bstrncpy(buf, cwd, sizeof(buf));
503 int len = strlen(buf);
505 buf[len-1] = 0; /* strip trailing / */
510 if (db_get_file_attributes_record(ua->jcr, ua->db, pcwd, NULL, &fdbr)) {
512 decode_stat(fdbr.LStat, &statp, &LinkFI); /* decode stat pkt */
514 /* Something went wrong getting attributes -- print name */
515 memset(&statp, 0, sizeof(statp));
517 ls_output(buf, cwd, tag, &statp);
518 bsendmsg(ua, "%s\n", buf);
525 static int estimatecmd(UAContext *ua, TREE_CTX *tree)
527 int total, num_extract;
528 uint64_t total_bytes = 0;
534 total = num_extract = 0;
535 for (TREE_NODE *node=first_tree_node(tree->root); node; node=next_tree_node(node)) {
536 if (node->type != TN_NEWDIR) {
538 /* If regular file, get size */
539 if (node->extract && node->type == TN_FILE) {
541 tree_getpath(node, cwd, sizeof(cwd));
543 fdbr.JobId = node->JobId;
544 if (db_get_file_attributes_record(ua->jcr, ua->db, cwd, NULL, &fdbr)) {
546 decode_stat(fdbr.LStat, &statp, &LinkFI); /* decode stat pkt */
547 if (S_ISREG(statp.st_mode) && statp.st_size > 0) {
548 total_bytes += statp.st_size;
551 /* Directory, count only */
552 } else if (node->extract || node->extract_dir) {
557 bsendmsg(ua, "%d total files; %d marked to be restored; %s bytes.\n",
558 total, num_extract, edit_uint64_with_commas(total_bytes, ec1));
564 static int helpcmd(UAContext *ua, TREE_CTX *tree)
569 bsendmsg(ua, _(" Command Description\n ======= ===========\n"));
570 for (i=0; i<comsize; i++) {
571 bsendmsg(ua, _(" %-10s %s\n"), _(commands[i].key), _(commands[i].help));
578 * Change directories. Note, if the user specifies x: and it fails,
579 * we assume it is a Win32 absolute cd rather than relative and
580 * try a second time with /x: ... Win32 kludge.
582 static int cdcmd(UAContext *ua, TREE_CTX *tree)
590 node = tree_cwd(ua->argk[1], tree->root, tree->node);
592 /* Try once more if Win32 drive -- make absolute */
593 if (ua->argk[1][1] == ':') { /* win32 drive */
594 bstrncpy(cwd, "/", sizeof(cwd));
595 bstrncat(cwd, ua->argk[1], sizeof(cwd));
596 node = tree_cwd(cwd, tree->root, tree->node);
599 bsendmsg(ua, _("Invalid path given.\n"));
606 tree_getpath(tree->node, cwd, sizeof(cwd));
607 bsendmsg(ua, _("cwd is: %s\n"), cwd);
611 static int pwdcmd(UAContext *ua, TREE_CTX *tree)
614 tree_getpath(tree->node, cwd, sizeof(cwd));
615 bsendmsg(ua, _("cwd is: %s\n"), cwd);
620 static int unmarkcmd(UAContext *ua, TREE_CTX *tree)
625 if (ua->argc < 2 || !tree->node->child) {
626 bsendmsg(ua, _("No files unmarked.\n"));
629 for (int i=1; i < ua->argc; i++) {
630 for (node = tree->node->child; node; node=node->sibling) {
631 if (fnmatch(ua->argk[i], node->fname, 0) == 0) {
632 count += set_extract(ua, node, tree, false);
637 bsendmsg(ua, _("No files unmarked.\n"));
639 bsendmsg(ua, _("%d file%s unmarked.\n"), count, count==0?"":"s");
644 static int unmarkdircmd(UAContext *ua, TREE_CTX *tree)
649 if (ua->argc < 2 || !tree->node->child) {
650 bsendmsg(ua, _("No directories unmarked.\n"));
654 for (int i=1; i < ua->argc; i++) {
655 for (node = tree->node->child; node; node=node->sibling) {
656 if (fnmatch(ua->argk[i], node->fname, 0) == 0) {
657 if (node->type == TN_DIR || node->type == TN_DIR_NLS) {
658 node->extract_dir = false;
666 bsendmsg(ua, _("No directories unmarked.\n"));
668 bsendmsg(ua, _("%d director%s unmarked.\n"), count, count==1?"y":"ies");
674 static int donecmd(UAContext *ua, TREE_CTX *tree)
679 static int quitcmd(UAContext *ua, TREE_CTX *tree)