]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/ua_tree.c
7bc68576cf807a66ce5694eec18fe7891c6d1515
[bacula/bacula] / bacula / src / dird / ua_tree.c
1 /*
2  *
3  *   Bacula Director -- User Agent Database File tree for Restore
4  *      command. This file interacts with the user implementing the
5  *      UA tree commands.
6  *
7  *     Kern Sibbald, July MMII
8  *
9  *   Version $Id$
10  */
11
12 /*
13    Copyright (C) 2002-2004 Kern Sibbald and John Walker
14
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.
19
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.
24
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,
28    MA 02111-1307, USA.
29
30  */
31
32 #include "bacula.h"
33 #include "dird.h"
34 #include <fnmatch.h>
35 #include "findlib/find.h"
36
37
38 /* Forward referenced commands */
39
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);
55
56
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")},    
76              };
77 #define comsize (sizeof(commands)/sizeof(struct cmdstruct))
78
79
80 /*
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", ...
84  */
85 bool user_select_files_from_tree(TREE_CTX *tree)
86 {
87    char cwd[2000];
88    bool stat;
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 */
92
93    bsendmsg(tree->ua, _( 
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"));
97    /*
98     * Enter interactive command handler allowing selection
99     *  of individual files.
100     */
101    tree->node = (TREE_NODE *)tree->root;
102    tree_getpath(tree->node, cwd, sizeof(cwd));
103    bsendmsg(tree->ua, _("cwd is: %s\n"), cwd);
104    for ( ;; ) {       
105       int found, len, i;
106       if (!get_cmd(ua, "$ ")) {
107          break;
108       }
109       parse_ua_args(ua);
110       if (ua->argc == 0) {
111          break;
112       }
113
114       len = strlen(ua->argk[0]);
115       found = 0;
116       stat = false;
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 */
120             found = 1;
121             break;
122          }
123       if (!found) {
124          bsendmsg(tree->ua, _("Illegal command. Enter \"done\" to exit.\n"));
125          continue;
126       }
127       if (!stat) {
128          break;
129       }
130    }
131    ua->UA_sock = NULL;                /* don't release restore socket */
132    stat = !ua->quit;
133    ua->quit = false;
134    free_ua_context(ua);               /* get rid of temp UA context */
135    return stat;
136 }
137
138
139 /*
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)
145  *
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
149  */
150 int insert_tree_handler(void *ctx, int num_fields, char **row)
151 {
152    struct stat statp;
153    TREE_CTX *tree = (TREE_CTX *)ctx;
154    char fname[5000];
155    TREE_NODE *node, *new_node;
156    int type;
157    bool hard_link;
158
159    strip_trailing_junk(row[1]);
160    if (*row[1] == 0) {                /* no filename => directory */
161       if (*row[0] != '/') {           /* Must be Win32 directory */
162          type = TN_DIR_NLS;
163       } else {
164          type = TN_DIR;
165       }
166    } else {
167       type = TN_FILE;
168    }
169    if (tree->avail_node) {
170       node = tree->avail_node;        /* if prev node avail use it */
171    } else {
172       node = new_tree_node(tree->root, type);  /* get new node */
173       tree->avail_node = node;
174    }
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 */
183    } else {
184       tree->avail_node = NULL;        /* added node to tree */
185    }
186    new_node->FileIndex = atoi(row[2]);
187    new_node->JobId = (JobId_t)str_to_int64(row[3]);
188    new_node->type = type;
189    new_node->extract = true;          /* extract all by default */
190    new_node->hard_link = hard_link;
191    new_node->soft_link = S_ISLNK(statp.st_mode) != 0;
192    if (type == TN_DIR || type == TN_DIR_NLS) {
193       new_node->extract_dir = true;   /* if dir, extract it */
194    }
195    tree->cnt++;
196    return 0;
197 }
198
199
200 /*
201  * Set extract to value passed. We recursively walk
202  *  down the tree setting all children if the 
203  *  node is a directory.
204  */
205 static int set_extract(UAContext *ua, TREE_NODE *node, TREE_CTX *tree, bool extract)
206 {
207    TREE_NODE *n;
208    FILE_DBR fdbr;
209    struct stat statp;
210    int count = 0;
211
212    node->extract = extract;
213    if (node->type == TN_DIR || node->type == TN_DIR_NLS) {
214       node->extract_dir = extract;    /* set/clear dir too */
215    }
216    if (node->type != TN_NEWDIR) {
217       count++;
218    }
219    /* For a non-file (i.e. directory), we see all the children */
220    if (node->type != TN_FILE || (node->soft_link && node->child)) {
221       /* Recursive set children within directory */
222       for (n=node->child; n; n=n->sibling) {
223          count += set_extract(ua, n, tree, extract);
224       }
225       /*
226        * Walk up tree marking any unextracted parent to be
227        * extracted.
228        */
229       if (extract) {
230          while (node->parent && !node->parent->extract_dir) {
231             node = node->parent;
232             node->extract_dir = true;
233          }
234       }
235    } else if (extract) {
236       char cwd[2000];
237       /*
238        * Ordinary file, we get the full path, look up the
239        * attributes, decode them, and if we are hard linked to
240        * a file that was saved, we must load that file too.
241        */
242       tree_getpath(node, cwd, sizeof(cwd));
243       fdbr.FileId = 0;
244       fdbr.JobId = node->JobId;
245       if (node->hard_link && db_get_file_attributes_record(ua->jcr, ua->db, cwd, NULL, &fdbr)) {
246          int32_t LinkFI;
247          decode_stat(fdbr.LStat, &statp, &LinkFI); /* decode stat pkt */
248          /*
249           * If we point to a hard linked file, traverse the tree to
250           * find that file, and mark it to be restored as well. It
251           * must have the Link we just obtained and the same JobId.
252           */
253          if (LinkFI) {
254             for (n=first_tree_node(tree->root); n; n=next_tree_node(n)) {
255                if (n->FileIndex == LinkFI && n->JobId == node->JobId) {
256                   n->extract = true;
257                   if (n->type == TN_DIR || n->type == TN_DIR_NLS) {
258                      n->extract_dir = true;
259                   }
260                   break;
261                }
262             }
263          }
264       }
265    }
266    return count;
267 }
268
269 /*
270  * Recursively mark the current directory to be restored as 
271  *  well as all directories and files below it.
272  */
273 static int markcmd(UAContext *ua, TREE_CTX *tree)
274 {
275    TREE_NODE *node;
276    int count = 0;
277
278    if (ua->argc < 2 || !tree->node->child) {
279       bsendmsg(ua, _("No files marked.\n"));
280       return 1;
281    }
282    for (int i=1; i < ua->argc; i++) {
283       for (node = tree->node->child; node; node=node->sibling) {
284          if (fnmatch(ua->argk[i], node->fname, 0) == 0) {
285             count += set_extract(ua, node, tree, true);
286          }
287       }
288    }
289    if (count == 0) {
290       bsendmsg(ua, _("No files marked.\n"));
291    } else {
292       bsendmsg(ua, _("%d file%s marked.\n"), count, count==0?"":"s");
293    }
294    return 1;
295 }
296
297 static int markdircmd(UAContext *ua, TREE_CTX *tree)
298 {
299    TREE_NODE *node;
300    int count = 0;
301
302    if (ua->argc < 2 || !tree->node->child) {
303       bsendmsg(ua, _("No files marked.\n"));
304       return 1;
305    }
306    for (int i=1; i < ua->argc; i++) {
307       for (node = tree->node->child; node; node=node->sibling) {
308          if (fnmatch(ua->argk[i], node->fname, 0) == 0) {
309             if (node->type == TN_DIR || node->type == TN_DIR_NLS) {
310                node->extract_dir = true;
311                count++;
312             }
313          }
314       }
315    }
316    if (count == 0) {
317       bsendmsg(ua, _("No directories marked.\n"));
318    } else {
319       bsendmsg(ua, _("%d director%s marked.\n"), count, count==1?"y":"ies");
320    }
321    return 1;
322 }
323
324
325 static int countcmd(UAContext *ua, TREE_CTX *tree)
326 {
327    int total, num_extract;
328
329    total = num_extract = 0;
330    for (TREE_NODE *node=first_tree_node(tree->root); node; node=next_tree_node(node)) {
331       if (node->type != TN_NEWDIR) {
332          total++;
333          if (node->extract || node->extract_dir) {
334             num_extract++;
335          }
336       }
337    }
338    bsendmsg(ua, "%d total files/dirs. %d marked to be restored.\n", total, num_extract);
339    return 1;
340 }
341
342 static int findcmd(UAContext *ua, TREE_CTX *tree)
343 {
344    char cwd[2000];
345
346    if (ua->argc == 1) {
347       bsendmsg(ua, _("No file specification given.\n"));
348       return 0;
349    }
350    
351    for (int i=1; i < ua->argc; i++) {
352       for (TREE_NODE *node=first_tree_node(tree->root); node; node=next_tree_node(node)) {
353          if (fnmatch(ua->argk[i], node->fname, 0) == 0) {
354             char *tag;
355             tree_getpath(node, cwd, sizeof(cwd));
356             if (node->extract) {
357                tag = "*";
358             } else if (node->extract_dir) {
359                tag = "+";
360             } else {
361                tag = "";
362             }
363             bsendmsg(ua, "%s%s\n", tag, cwd);
364          }
365       }
366    }
367    return 1;
368 }
369
370
371
372 static int lscmd(UAContext *ua, TREE_CTX *tree)
373 {
374    TREE_NODE *node;
375
376    if (!tree->node->child) {     
377       return 1;
378    }
379    for (node = tree->node->child; node; node=node->sibling) {
380       if (ua->argc == 1 || fnmatch(ua->argk[1], node->fname, 0) == 0) {
381          char *tag;
382          if (node->extract) {
383             tag = "*";
384          } else if (node->extract_dir) {
385             tag = "+";
386          } else {
387             tag = "";
388          }
389          bsendmsg(ua, "%s%s%s\n", tag, node->fname, node->child?"/":"");
390       }
391    }
392    return 1;
393 }
394
395 /*
396  * Ls command that lists only the marked files
397  */
398 static int lsmarkcmd(UAContext *ua, TREE_CTX *tree)
399 {
400    TREE_NODE *node;
401
402    if (!tree->node->child) {     
403       return 1;
404    }
405    for (node = tree->node->child; node; node=node->sibling) {
406       if ((ua->argc == 1 || fnmatch(ua->argk[1], node->fname, 0) == 0) &&
407           (node->extract || node->extract_dir)) {
408          char *tag;
409          if (node->extract) {
410             tag = "*";
411          } else if (node->extract_dir) {
412             tag = "+";
413          } else {
414             tag = "";
415          }
416          bsendmsg(ua, "%s%s%s\n", tag, node->fname, node->child?"/":"");
417       }
418    }
419    return 1;
420 }
421
422
423 extern char *getuser(uid_t uid);
424 extern char *getgroup(gid_t gid);
425
426 /*
427  * This is actually the long form used for "dir"
428  */
429 static void ls_output(char *buf, char *fname, char *tag, struct stat *statp)
430 {
431    char *p, *f;
432    char ec1[30];
433    int n;
434
435    p = encode_mode(statp->st_mode, buf);
436    n = sprintf(p, "  %2d ", (uint32_t)statp->st_nlink);
437    p += n;
438    n = sprintf(p, "%-8.8s %-8.8s", getuser(statp->st_uid), getgroup(statp->st_gid));
439    p += n;
440    n = sprintf(p, "%8.8s  ", edit_uint64(statp->st_size, ec1));
441    p += n;
442    p = encode_time(statp->st_ctime, p);
443    *p++ = ' ';
444    *p++ = *tag;
445    for (f=fname; *f; ) {
446       *p++ = *f++;
447    }
448    *p = 0;
449 }
450
451
452 /*
453  * Like ls command, but give more detail on each file
454  */
455 static int dircmd(UAContext *ua, TREE_CTX *tree)
456 {
457    TREE_NODE *node;
458    FILE_DBR fdbr;
459    struct stat statp;
460    char buf[1100];
461    char cwd[1100], *pcwd;
462
463    if (!tree->node->child) {     
464       return 1;
465    }
466    for (node = tree->node->child; node; node=node->sibling) {
467       char *tag;
468       if (ua->argc == 1 || fnmatch(ua->argk[1], node->fname, 0) == 0) {
469          if (node->extract) {
470             tag = "*";
471          } else if (node->extract_dir) {
472             tag = "+";
473          } else {
474             tag = " ";
475          }
476          tree_getpath(node, cwd, sizeof(cwd));
477          fdbr.FileId = 0;
478          fdbr.JobId = node->JobId;
479          /*
480           * Strip / from soft links to directories.
481           *   This is because soft links to files have a trailing slash
482           *   when returned from tree_getpath, but db_get_file_attr...
483           *   treats soft links as files, so they do not have a trailing
484           *   slash like directory names.
485           */
486          if (node->type == TN_FILE && node->child) {
487             bstrncpy(buf, cwd, sizeof(buf));
488             pcwd = buf;
489             int len = strlen(buf);
490             if (len > 1) {
491                buf[len-1] = 0;        /* strip trailing / */
492             }
493          } else {
494             pcwd = cwd;
495          }
496          if (db_get_file_attributes_record(ua->jcr, ua->db, pcwd, NULL, &fdbr)) {
497             int32_t LinkFI;
498             decode_stat(fdbr.LStat, &statp, &LinkFI); /* decode stat pkt */
499          } else {
500             /* Something went wrong getting attributes -- print name */
501             memset(&statp, 0, sizeof(statp));
502          }
503          ls_output(buf, cwd, tag, &statp);
504          bsendmsg(ua, "%s\n", buf);
505       }
506    }
507    return 1;
508 }
509
510
511 static int estimatecmd(UAContext *ua, TREE_CTX *tree)
512 {
513    int total, num_extract;
514    uint64_t total_bytes = 0;
515    FILE_DBR fdbr;
516    struct stat statp;
517    char cwd[1100];
518    char ec1[50];
519
520    total = num_extract = 0;
521    for (TREE_NODE *node=first_tree_node(tree->root); node; node=next_tree_node(node)) {
522       if (node->type != TN_NEWDIR) {
523          total++;
524          /* If regular file, get size */
525          if (node->extract && node->type == TN_FILE) {
526             num_extract++;
527             tree_getpath(node, cwd, sizeof(cwd));
528             fdbr.FileId = 0;
529             fdbr.JobId = node->JobId;
530             if (db_get_file_attributes_record(ua->jcr, ua->db, cwd, NULL, &fdbr)) {
531                int32_t LinkFI;
532                decode_stat(fdbr.LStat, &statp, &LinkFI); /* decode stat pkt */
533                if (S_ISREG(statp.st_mode) && statp.st_size > 0) {
534                   total_bytes += statp.st_size;
535                }
536             }
537          /* Directory, count only */
538          } else if (node->extract || node->extract_dir) {
539             num_extract++;
540          }
541       }
542    }
543    bsendmsg(ua, "%d total files; %d marked to be restored; %s bytes.\n", 
544             total, num_extract, edit_uint64_with_commas(total_bytes, ec1));
545    return 1;
546 }
547
548
549
550 static int helpcmd(UAContext *ua, TREE_CTX *tree) 
551 {
552    unsigned int i;
553
554 /* usage(); */
555    bsendmsg(ua, _("  Command    Description\n  =======    ===========\n"));
556    for (i=0; i<comsize; i++) {
557       bsendmsg(ua, _("  %-10s %s\n"), _(commands[i].key), _(commands[i].help));
558    }
559    bsendmsg(ua, "\n");
560    return 1;
561 }
562
563 /*
564  * Change directories.  Note, if the user specifies x: and it fails,
565  *   we assume it is a Win32 absolute cd rather than relative and
566  *   try a second time with /x: ...  Win32 kludge.
567  */
568 static int cdcmd(UAContext *ua, TREE_CTX *tree) 
569 {
570    TREE_NODE *node;
571    char cwd[2000];
572
573    if (ua->argc != 2) {
574       return 1;
575    }
576    node = tree_cwd(ua->argk[1], tree->root, tree->node);
577    if (!node) {
578       /* Try once more if Win32 drive -- make absolute */
579       if (ua->argk[1][1] == ':') {  /* win32 drive */
580          bstrncpy(cwd, "/", sizeof(cwd));
581          bstrncat(cwd, ua->argk[1], sizeof(cwd));
582          node = tree_cwd(cwd, tree->root, tree->node);
583       }
584       if (!node) {
585          bsendmsg(ua, _("Invalid path given.\n"));
586       } else {
587          tree->node = node;
588       }
589    } else {
590       tree->node = node;
591    }
592    tree_getpath(tree->node, cwd, sizeof(cwd));
593    bsendmsg(ua, _("cwd is: %s\n"), cwd);
594    return 1;
595 }
596
597 static int pwdcmd(UAContext *ua, TREE_CTX *tree) 
598 {
599    char cwd[2000];
600    tree_getpath(tree->node, cwd, sizeof(cwd));
601    bsendmsg(ua, _("cwd is: %s\n"), cwd);
602    return 1;
603 }
604
605
606 static int unmarkcmd(UAContext *ua, TREE_CTX *tree)
607 {
608    TREE_NODE *node;
609    int count = 0;
610
611    if (ua->argc < 2 || !tree->node->child) {     
612       bsendmsg(ua, _("No files unmarked.\n"));
613       return 1;
614    }
615    for (int i=1; i < ua->argc; i++) {
616       for (node = tree->node->child; node; node=node->sibling) {
617          if (fnmatch(ua->argk[i], node->fname, 0) == 0) {
618             count += set_extract(ua, node, tree, false);
619          }
620       }
621    }
622    if (count == 0) {
623       bsendmsg(ua, _("No files unmarked.\n"));
624    } else {
625       bsendmsg(ua, _("%d file%s unmarked.\n"), count, count==0?"":"s");
626    }
627    return 1;
628 }
629
630 static int unmarkdircmd(UAContext *ua, TREE_CTX *tree)
631 {
632    TREE_NODE *node;
633    int count = 0;
634
635    if (ua->argc < 2 || !tree->node->child) {
636       bsendmsg(ua, _("No directories unmarked.\n"));
637       return 1;
638    }
639
640    for (int i=1; i < ua->argc; i++) {
641       for (node = tree->node->child; node; node=node->sibling) {
642          if (fnmatch(ua->argk[i], node->fname, 0) == 0) {
643             if (node->type == TN_DIR || node->type == TN_DIR_NLS) {
644                node->extract_dir = false;
645                count++;
646             }
647          }
648       }
649    }
650
651    if (count == 0) {
652       bsendmsg(ua, _("No directories unmarked.\n"));
653    } else {
654       bsendmsg(ua, _("%d director%s unmarked.\n"), count, count==1?"y":"ies");
655    }
656    return 1;
657 }
658
659
660 static int donecmd(UAContext *ua, TREE_CTX *tree) 
661 {
662    return 0;
663 }
664
665 static int quitcmd(UAContext *ua, TREE_CTX *tree) 
666 {
667    ua->quit = true;
668    return 0;
669 }