]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/ua_tree.c
This commit was manufactured by cvs2svn to create tag
[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-2003 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 countcmd(UAContext *ua, TREE_CTX *tree);
42 static int findcmd(UAContext *ua, TREE_CTX *tree);
43 static int lscmd(UAContext *ua, TREE_CTX *tree);
44 static int lsmark(UAContext *ua, TREE_CTX *tree);
45 static int dircmd(UAContext *ua, TREE_CTX *tree);
46 static int estimatecmd(UAContext *ua, TREE_CTX *tree);
47 static int helpcmd(UAContext *ua, TREE_CTX *tree);
48 static int cdcmd(UAContext *ua, TREE_CTX *tree);
49 static int pwdcmd(UAContext *ua, TREE_CTX *tree);
50 static int unmarkcmd(UAContext *ua, TREE_CTX *tree);
51 static int quitcmd(UAContext *ua, TREE_CTX *tree);
52
53
54 struct cmdstruct { char *key; int (*func)(UAContext *ua, TREE_CTX *tree); char *help; }; 
55 static struct cmdstruct commands[] = {
56  { N_("cd"),         cdcmd,        _("change current directory")},
57  { N_("count"),      countcmd,     _("count marked files")},
58  { N_("dir"),        dircmd,       _("list current directory")},    
59  { N_("done"),       quitcmd,      _("leave file selection mode")},
60  { N_("estimate"),   estimatecmd,  _("estimate restore size")},
61  { N_("exit"),       quitcmd,      _("exit = done")},
62  { N_("find"),       findcmd,      _("find files")},
63  { N_("help"),       helpcmd,      _("print help")},
64  { N_("lsmark"),     lsmark,       _("list the marked files")},    
65  { N_("ls"),         lscmd,        _("list current directory")},    
66  { N_("mark"),       markcmd,      _("mark file to be restored")},
67  { N_("pwd"),        pwdcmd,       _("print current working directory")},
68  { N_("unmark"),     unmarkcmd,    _("unmark file to be restored")},
69  { N_("?"),          helpcmd,      _("print help")},    
70              };
71 #define comsize (sizeof(commands)/sizeof(struct cmdstruct))
72
73
74 /*
75  * Enter a prompt mode where the user can select/deselect
76  *  files to be restored. This is sort of like a mini-shell
77  *  that allows "cd", "pwd", "add", "rm", ...
78  */
79 void user_select_files_from_tree(TREE_CTX *tree)
80 {
81    char cwd[2000];
82    /* Get a new context so we don't destroy restore command args */
83    UAContext *ua = new_ua_context(tree->ua->jcr);
84    ua->UA_sock = tree->ua->UA_sock;   /* patch in UA socket */
85
86    bsendmsg(tree->ua, _( 
87       "\nYou are now entering file selection mode where you add and\n"
88       "remove files to be restored. All files are initially added.\n"
89       "Enter \"done\" to leave this mode.\n\n"));
90    /*
91     * Enter interactive command handler allowing selection
92     *  of individual files.
93     */
94    tree->node = (TREE_NODE *)tree->root;
95    tree_getpath(tree->node, cwd, sizeof(cwd));
96    bsendmsg(tree->ua, _("cwd is: %s\n"), cwd);
97    for ( ;; ) {       
98       int found, len, stat, i;
99       if (!get_cmd(ua, "$ ")) {
100          break;
101       }
102       parse_ua_args(ua);
103       if (ua->argc == 0) {
104          break;
105       }
106
107       len = strlen(ua->argk[0]);
108       found = 0;
109       stat = 0;
110       for (i=0; i<(int)comsize; i++)       /* search for command */
111          if (strncasecmp(ua->argk[0],  _(commands[i].key), len) == 0) {
112             stat = (*commands[i].func)(ua, tree);   /* go execute command */
113             found = 1;
114             break;
115          }
116       if (!found) {
117          bsendmsg(tree->ua, _("Illegal command. Enter \"done\" to exit.\n"));
118          continue;
119       }
120       if (!stat) {
121          break;
122       }
123    }
124    ua->UA_sock = NULL;                /* don't release restore socket */
125    free_ua_context(ua);               /* get rid of temp UA context */
126 }
127
128
129 /*
130  * This callback routine is responsible for inserting the
131  *  items it gets into the directory tree. For each JobId selected
132  *  this routine is called once for each file. We do not allow
133  *  duplicate filenames, but instead keep the info from the most
134  *  recent file entered (i.e. the JobIds are assumed to be sorted)
135  */
136 int insert_tree_handler(void *ctx, int num_fields, char **row)
137 {
138    TREE_CTX *tree = (TREE_CTX *)ctx;
139    char fname[5000];
140    TREE_NODE *node, *new_node;
141    int type;
142
143    strip_trailing_junk(row[1]);
144    if (*row[1] == 0) {                /* no filename => directory */
145       if (*row[0] != '/') {           /* Must be Win32 directory */
146          type = TN_DIR_NLS;
147       } else {
148          type = TN_DIR;
149       }
150    } else {
151       type = TN_FILE;
152    }
153    bsnprintf(fname, sizeof(fname), "%s%s", row[0], row[1]);
154    if (tree->avail_node) {
155       node = tree->avail_node;
156    } else {
157       node = new_tree_node(tree->root, type);
158       tree->avail_node = node;
159    }
160    Dmsg3(200, "FI=%d type=%d fname=%s\n", node->FileIndex, type, fname);
161    new_node = insert_tree_node(fname, node, tree->root, NULL);
162    /* Note, if node already exists, save new one for next time */
163    if (new_node != node) {
164       tree->avail_node = node;
165    } else {
166       tree->avail_node = NULL;
167    }
168    new_node->FileIndex = atoi(row[2]);
169    new_node->JobId = atoi(row[3]);
170    new_node->type = type;
171    new_node->extract = true;          /* extract all by default */
172    tree->cnt++;
173    return 0;
174 }
175
176
177 /*
178  * Set extract to value passed. We recursively walk
179  *  down the tree setting all children if the 
180  *  node is a directory.
181  */
182 static int set_extract(UAContext *ua, TREE_NODE *node, TREE_CTX *tree, bool extract)
183 {
184    TREE_NODE *n;
185    FILE_DBR fdbr;
186    struct stat statp;
187    int count = 0;
188
189    node->extract = extract;
190    if (node->type != TN_NEWDIR) {
191       count++;
192    }
193    /* For a non-file (i.e. directory), we see all the children */
194    if (node->type != TN_FILE) {
195       for (n=node->child; n; n=n->sibling) {
196          count += set_extract(ua, n, tree, extract);
197       }
198    } else if (extract) {
199       char cwd[2000];
200       /* Ordinary file, we get the full path, look up the
201        * attributes, decode them, and if we are hard linked to
202        * a file that was saved, we must load that file too.
203        */
204       tree_getpath(node, cwd, sizeof(cwd));
205       fdbr.FileId = 0;
206       fdbr.JobId = node->JobId;
207       if (db_get_file_attributes_record(ua->jcr, ua->db, cwd, NULL, &fdbr)) {
208          int32_t LinkFI;
209          decode_stat(fdbr.LStat, &statp, &LinkFI); /* decode stat pkt */
210          /*
211           * If we point to a hard linked file, traverse the tree to
212           * find that file, and mark it to be restored as well. It
213           * must have the Link we just obtained and the same JobId.
214           */
215          if (LinkFI) {
216             for (n=first_tree_node(tree->root); n; n=next_tree_node(n)) {
217                if (n->FileIndex == LinkFI && n->JobId == node->JobId) {
218                   n->extract = true;
219                   break;
220                }
221             }
222          }
223       }
224    }
225    return count;
226 }
227
228 static int markcmd(UAContext *ua, TREE_CTX *tree)
229 {
230    TREE_NODE *node;
231    int count = 0;
232
233    if (ua->argc < 2 || !tree->node->child) {
234       bsendmsg(ua, _("No files marked.\n"));
235       return 1;
236    }
237    for (node = tree->node->child; node; node=node->sibling) {
238       if (fnmatch(ua->argk[1], node->fname, 0) == 0) {
239          count += set_extract(ua, node, tree, true);
240       }
241    }
242    if (count == 0) {
243       bsendmsg(ua, _("No files marked.\n"));
244    } else {
245       bsendmsg(ua, _("%d file%s marked.\n"), count, count==0?"":"s");
246    }
247    return 1;
248 }
249
250 static int countcmd(UAContext *ua, TREE_CTX *tree)
251 {
252    int total, num_extract;
253
254    total = num_extract = 0;
255    for (TREE_NODE *node=first_tree_node(tree->root); node; node=next_tree_node(node)) {
256       if (node->type != TN_NEWDIR) {
257          total++;
258          if (node->extract) {
259             num_extract++;
260          }
261       }
262    }
263    bsendmsg(ua, "%d total files. %d marked to be restored.\n", total, num_extract);
264    return 1;
265 }
266
267 static int findcmd(UAContext *ua, TREE_CTX *tree)
268 {
269    char cwd[2000];
270
271    if (ua->argc == 1) {
272       bsendmsg(ua, _("No file specification given.\n"));
273       return 0;
274    }
275    
276    for (int i=1; i < ua->argc; i++) {
277       for (TREE_NODE *node=first_tree_node(tree->root); node; node=next_tree_node(node)) {
278          if (fnmatch(ua->argk[i], node->fname, 0) == 0) {
279             tree_getpath(node, cwd, sizeof(cwd));
280             bsendmsg(ua, "%s%s\n", node->extract?"*":"", cwd);
281          }
282       }
283    }
284    return 1;
285 }
286
287
288
289 static int lscmd(UAContext *ua, TREE_CTX *tree)
290 {
291    TREE_NODE *node;
292
293    if (!tree->node->child) {     
294       return 1;
295    }
296    for (node = tree->node->child; node; node=node->sibling) {
297       if (ua->argc == 1 || fnmatch(ua->argk[1], node->fname, 0) == 0) {
298          bsendmsg(ua, "%s%s%s\n", node->extract?"*":"", node->fname,
299             (node->type==TN_DIR||node->type==TN_NEWDIR)?"/":"");
300       }
301    }
302    return 1;
303 }
304
305 /*
306  * Ls command that lists only the marked files
307  */
308 static int lsmark(UAContext *ua, TREE_CTX *tree)
309 {
310    TREE_NODE *node;
311
312    if (!tree->node->child) {     
313       return 1;
314    }
315    for (node = tree->node->child; node; node=node->sibling) {
316       if (ua->argc == 1 || fnmatch(ua->argk[1], node->fname, 0) == 0 &&
317           node->extract) {
318          bsendmsg(ua, "%s%s%s\n", node->extract?"*":"", node->fname,
319             (node->type==TN_DIR||node->type==TN_NEWDIR)?"/":"");
320       }
321    }
322    return 1;
323 }
324
325
326 extern char *getuser(uid_t uid);
327 extern char *getgroup(gid_t gid);
328
329 /*
330  * This is actually the long form used for "dir"
331  */
332 static void ls_output(char *buf, char *fname, bool extract, struct stat *statp)
333 {
334    char *p, *f;
335    char ec1[30];
336    int n;
337
338    p = encode_mode(statp->st_mode, buf);
339    n = sprintf(p, "  %2d ", (uint32_t)statp->st_nlink);
340    p += n;
341    n = sprintf(p, "%-8.8s %-8.8s", getuser(statp->st_uid), getgroup(statp->st_gid));
342    p += n;
343    n = sprintf(p, "%8.8s  ", edit_uint64(statp->st_size, ec1));
344    p += n;
345    p = encode_time(statp->st_ctime, p);
346    *p++ = ' ';
347    if (extract) {
348       *p++ = '*';
349    } else {
350       *p++ = ' ';
351    }
352    for (f=fname; *f; )
353       *p++ = *f++;
354    *p = 0;
355 }
356
357
358 /*
359  * Like ls command, but give more detail on each file
360  */
361 static int dircmd(UAContext *ua, TREE_CTX *tree)
362 {
363    TREE_NODE *node;
364    FILE_DBR fdbr;
365    struct stat statp;
366    char buf[1000];
367    char cwd[1100];
368
369    if (!tree->node->child) {     
370       return 1;
371    }
372    for (node = tree->node->child; node; node=node->sibling) {
373       if (ua->argc == 1 || fnmatch(ua->argk[1], node->fname, 0) == 0) {
374          tree_getpath(node, cwd, sizeof(cwd));
375          fdbr.FileId = 0;
376          fdbr.JobId = node->JobId;
377          if (db_get_file_attributes_record(ua->jcr, ua->db, cwd, NULL, &fdbr)) {
378             int32_t LinkFI;
379             decode_stat(fdbr.LStat, &statp, &LinkFI); /* decode stat pkt */
380             ls_output(buf, cwd, node->extract, &statp);
381             bsendmsg(ua, "%s\n", buf);
382          } else {
383             /* Something went wrong getting attributes -- print name */
384             bsendmsg(ua, "%s%s%s\n", node->extract?"*":"", node->fname,
385                (node->type==TN_DIR||node->type==TN_NEWDIR)?"/":"");
386          }
387       }
388    }
389    return 1;
390 }
391
392
393 static int estimatecmd(UAContext *ua, TREE_CTX *tree)
394 {
395    int total, num_extract;
396    uint64_t total_bytes = 0;
397    FILE_DBR fdbr;
398    struct stat statp;
399    char cwd[1100];
400    char ec1[50];
401
402    total = num_extract = 0;
403    for (TREE_NODE *node=first_tree_node(tree->root); node; node=next_tree_node(node)) {
404       if (node->type != TN_NEWDIR) {
405          total++;
406          /* If regular file, get size */
407          if (node->extract && node->type == TN_FILE) {
408             num_extract++;
409             tree_getpath(node, cwd, sizeof(cwd));
410             fdbr.FileId = 0;
411             fdbr.JobId = node->JobId;
412             if (db_get_file_attributes_record(ua->jcr, ua->db, cwd, NULL, &fdbr)) {
413                int32_t LinkFI;
414                decode_stat(fdbr.LStat, &statp, &LinkFI); /* decode stat pkt */
415                if (S_ISREG(statp.st_mode) && statp.st_size > 0) {
416                   total_bytes += statp.st_size;
417                }
418             }
419          /* Directory, count only */
420          } else if (node->extract) {
421             num_extract++;
422          }
423       }
424    }
425    bsendmsg(ua, "%d total files; %d marked to be restored; %s bytes.\n", 
426             total, num_extract, edit_uint64_with_commas(total_bytes, ec1));
427    return 1;
428 }
429
430
431
432 static int helpcmd(UAContext *ua, TREE_CTX *tree) 
433 {
434    unsigned int i;
435
436 /* usage(); */
437    bsendmsg(ua, _("  Command    Description\n  =======    ===========\n"));
438    for (i=0; i<comsize; i++) {
439       bsendmsg(ua, _("  %-10s %s\n"), _(commands[i].key), _(commands[i].help));
440    }
441    bsendmsg(ua, "\n");
442    return 1;
443 }
444
445 /*
446  * Change directories.  Note, if the user specifies x: and it fails,
447  *   we assume it is a Win32 absolute cd rather than relative and
448  *   try a second time with /x: ...  Win32 kludge.
449  */
450 static int cdcmd(UAContext *ua, TREE_CTX *tree) 
451 {
452    TREE_NODE *node;
453    char cwd[2000];
454
455    if (ua->argc != 2) {
456       return 1;
457    }
458    node = tree_cwd(ua->argk[1], tree->root, tree->node);
459    if (!node) {
460       /* Try once more if Win32 drive -- make absolute */
461       if (ua->argk[1][1] == ':') {  /* win32 drive */
462          bstrncpy(cwd, "/", sizeof(cwd));
463          bstrncat(cwd, ua->argk[1], sizeof(cwd));
464          node = tree_cwd(cwd, tree->root, tree->node);
465       }
466       if (!node) {
467          bsendmsg(ua, _("Invalid path given.\n"));
468       } else {
469          tree->node = node;
470       }
471    } else {
472       tree->node = node;
473    }
474    tree_getpath(tree->node, cwd, sizeof(cwd));
475    bsendmsg(ua, _("cwd is: %s\n"), cwd);
476    return 1;
477 }
478
479 static int pwdcmd(UAContext *ua, TREE_CTX *tree) 
480 {
481    char cwd[2000];
482    tree_getpath(tree->node, cwd, sizeof(cwd));
483    bsendmsg(ua, _("cwd is: %s\n"), cwd);
484    return 1;
485 }
486
487
488 static int unmarkcmd(UAContext *ua, TREE_CTX *tree)
489 {
490    TREE_NODE *node;
491    int count = 0;
492
493    if (ua->argc < 2 || !tree->node->child) {     
494       bsendmsg(ua, _("No files unmarked.\n"));
495       return 1;
496    }
497    for (node = tree->node->child; node; node=node->sibling) {
498       if (fnmatch(ua->argk[1], node->fname, 0) == 0) {
499          count += set_extract(ua, node, tree, false);
500       }
501    }
502    if (count == 0) {
503       bsendmsg(ua, _("No files unmarked.\n"));
504    } else {
505       bsendmsg(ua, _("%d file%s unmarked.\n"), count, count==0?"":"s");
506    }
507    return 1;
508 }
509
510 static int quitcmd(UAContext *ua, TREE_CTX *tree) 
511 {
512    return 0;
513 }