]> 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 (int i=1; i < ua->argc; i++) {
238       for (node = tree->node->child; node; node=node->sibling) {
239          if (fnmatch(ua->argk[i], node->fname, 0) == 0) {
240             count += set_extract(ua, node, tree, true);
241          }
242       }
243    }
244    if (count == 0) {
245       bsendmsg(ua, _("No files marked.\n"));
246    } else {
247       bsendmsg(ua, _("%d file%s marked.\n"), count, count==0?"":"s");
248    }
249    return 1;
250 }
251
252 static int countcmd(UAContext *ua, TREE_CTX *tree)
253 {
254    int total, num_extract;
255
256    total = num_extract = 0;
257    for (TREE_NODE *node=first_tree_node(tree->root); node; node=next_tree_node(node)) {
258       if (node->type != TN_NEWDIR) {
259          total++;
260          if (node->extract) {
261             num_extract++;
262          }
263       }
264    }
265    bsendmsg(ua, "%d total files. %d marked to be restored.\n", total, num_extract);
266    return 1;
267 }
268
269 static int findcmd(UAContext *ua, TREE_CTX *tree)
270 {
271    char cwd[2000];
272
273    if (ua->argc == 1) {
274       bsendmsg(ua, _("No file specification given.\n"));
275       return 0;
276    }
277    
278    for (int i=1; i < ua->argc; i++) {
279       for (TREE_NODE *node=first_tree_node(tree->root); node; node=next_tree_node(node)) {
280          if (fnmatch(ua->argk[i], node->fname, 0) == 0) {
281             tree_getpath(node, cwd, sizeof(cwd));
282             bsendmsg(ua, "%s%s\n", node->extract?"*":"", cwd);
283          }
284       }
285    }
286    return 1;
287 }
288
289
290
291 static int lscmd(UAContext *ua, TREE_CTX *tree)
292 {
293    TREE_NODE *node;
294
295    if (!tree->node->child) {     
296       return 1;
297    }
298    for (node = tree->node->child; node; node=node->sibling) {
299       if (ua->argc == 1 || fnmatch(ua->argk[1], node->fname, 0) == 0) {
300          bsendmsg(ua, "%s%s%s\n", node->extract?"*":"", node->fname,
301             (node->type==TN_DIR||node->type==TN_NEWDIR)?"/":"");
302       }
303    }
304    return 1;
305 }
306
307 /*
308  * Ls command that lists only the marked files
309  */
310 static int lsmark(UAContext *ua, TREE_CTX *tree)
311 {
312    TREE_NODE *node;
313
314    if (!tree->node->child) {     
315       return 1;
316    }
317    for (node = tree->node->child; node; node=node->sibling) {
318       if (ua->argc == 1 || fnmatch(ua->argk[1], node->fname, 0) == 0 &&
319           node->extract) {
320          bsendmsg(ua, "%s%s%s\n", node->extract?"*":"", node->fname,
321             (node->type==TN_DIR||node->type==TN_NEWDIR)?"/":"");
322       }
323    }
324    return 1;
325 }
326
327
328 extern char *getuser(uid_t uid);
329 extern char *getgroup(gid_t gid);
330
331 /*
332  * This is actually the long form used for "dir"
333  */
334 static void ls_output(char *buf, char *fname, bool extract, struct stat *statp)
335 {
336    char *p, *f;
337    char ec1[30];
338    int n;
339
340    p = encode_mode(statp->st_mode, buf);
341    n = sprintf(p, "  %2d ", (uint32_t)statp->st_nlink);
342    p += n;
343    n = sprintf(p, "%-8.8s %-8.8s", getuser(statp->st_uid), getgroup(statp->st_gid));
344    p += n;
345    n = sprintf(p, "%8.8s  ", edit_uint64(statp->st_size, ec1));
346    p += n;
347    p = encode_time(statp->st_ctime, p);
348    *p++ = ' ';
349    if (extract) {
350       *p++ = '*';
351    } else {
352       *p++ = ' ';
353    }
354    for (f=fname; *f; )
355       *p++ = *f++;
356    *p = 0;
357 }
358
359
360 /*
361  * Like ls command, but give more detail on each file
362  */
363 static int dircmd(UAContext *ua, TREE_CTX *tree)
364 {
365    TREE_NODE *node;
366    FILE_DBR fdbr;
367    struct stat statp;
368    char buf[1000];
369    char cwd[1100];
370
371    if (!tree->node->child) {     
372       return 1;
373    }
374    for (node = tree->node->child; node; node=node->sibling) {
375       if (ua->argc == 1 || fnmatch(ua->argk[1], node->fname, 0) == 0) {
376          tree_getpath(node, cwd, sizeof(cwd));
377          fdbr.FileId = 0;
378          fdbr.JobId = node->JobId;
379          if (db_get_file_attributes_record(ua->jcr, ua->db, cwd, NULL, &fdbr)) {
380             int32_t LinkFI;
381             decode_stat(fdbr.LStat, &statp, &LinkFI); /* decode stat pkt */
382             ls_output(buf, cwd, node->extract, &statp);
383             bsendmsg(ua, "%s\n", buf);
384          } else {
385             /* Something went wrong getting attributes -- print name */
386             bsendmsg(ua, "%s%s%s\n", node->extract?"*":"", node->fname,
387                (node->type==TN_DIR||node->type==TN_NEWDIR)?"/":"");
388          }
389       }
390    }
391    return 1;
392 }
393
394
395 static int estimatecmd(UAContext *ua, TREE_CTX *tree)
396 {
397    int total, num_extract;
398    uint64_t total_bytes = 0;
399    FILE_DBR fdbr;
400    struct stat statp;
401    char cwd[1100];
402    char ec1[50];
403
404    total = num_extract = 0;
405    for (TREE_NODE *node=first_tree_node(tree->root); node; node=next_tree_node(node)) {
406       if (node->type != TN_NEWDIR) {
407          total++;
408          /* If regular file, get size */
409          if (node->extract && node->type == TN_FILE) {
410             num_extract++;
411             tree_getpath(node, cwd, sizeof(cwd));
412             fdbr.FileId = 0;
413             fdbr.JobId = node->JobId;
414             if (db_get_file_attributes_record(ua->jcr, ua->db, cwd, NULL, &fdbr)) {
415                int32_t LinkFI;
416                decode_stat(fdbr.LStat, &statp, &LinkFI); /* decode stat pkt */
417                if (S_ISREG(statp.st_mode) && statp.st_size > 0) {
418                   total_bytes += statp.st_size;
419                }
420             }
421          /* Directory, count only */
422          } else if (node->extract) {
423             num_extract++;
424          }
425       }
426    }
427    bsendmsg(ua, "%d total files; %d marked to be restored; %s bytes.\n", 
428             total, num_extract, edit_uint64_with_commas(total_bytes, ec1));
429    return 1;
430 }
431
432
433
434 static int helpcmd(UAContext *ua, TREE_CTX *tree) 
435 {
436    unsigned int i;
437
438 /* usage(); */
439    bsendmsg(ua, _("  Command    Description\n  =======    ===========\n"));
440    for (i=0; i<comsize; i++) {
441       bsendmsg(ua, _("  %-10s %s\n"), _(commands[i].key), _(commands[i].help));
442    }
443    bsendmsg(ua, "\n");
444    return 1;
445 }
446
447 /*
448  * Change directories.  Note, if the user specifies x: and it fails,
449  *   we assume it is a Win32 absolute cd rather than relative and
450  *   try a second time with /x: ...  Win32 kludge.
451  */
452 static int cdcmd(UAContext *ua, TREE_CTX *tree) 
453 {
454    TREE_NODE *node;
455    char cwd[2000];
456
457    if (ua->argc != 2) {
458       return 1;
459    }
460    node = tree_cwd(ua->argk[1], tree->root, tree->node);
461    if (!node) {
462       /* Try once more if Win32 drive -- make absolute */
463       if (ua->argk[1][1] == ':') {  /* win32 drive */
464          bstrncpy(cwd, "/", sizeof(cwd));
465          bstrncat(cwd, ua->argk[1], sizeof(cwd));
466          node = tree_cwd(cwd, tree->root, tree->node);
467       }
468       if (!node) {
469          bsendmsg(ua, _("Invalid path given.\n"));
470       } else {
471          tree->node = node;
472       }
473    } else {
474       tree->node = node;
475    }
476    tree_getpath(tree->node, cwd, sizeof(cwd));
477    bsendmsg(ua, _("cwd is: %s\n"), cwd);
478    return 1;
479 }
480
481 static int pwdcmd(UAContext *ua, TREE_CTX *tree) 
482 {
483    char cwd[2000];
484    tree_getpath(tree->node, cwd, sizeof(cwd));
485    bsendmsg(ua, _("cwd is: %s\n"), cwd);
486    return 1;
487 }
488
489
490 static int unmarkcmd(UAContext *ua, TREE_CTX *tree)
491 {
492    TREE_NODE *node;
493    int count = 0;
494
495    if (ua->argc < 2 || !tree->node->child) {     
496       bsendmsg(ua, _("No files unmarked.\n"));
497       return 1;
498    }
499    for (int i=1; i < ua->argc; i++) {
500       for (node = tree->node->child; node; node=node->sibling) {
501          if (fnmatch(ua->argk[i], node->fname, 0) == 0) {
502             count += set_extract(ua, node, tree, false);
503          }
504       }
505    }
506    if (count == 0) {
507       bsendmsg(ua, _("No files unmarked.\n"));
508    } else {
509       bsendmsg(ua, _("%d file%s unmarked.\n"), count, count==0?"":"s");
510    }
511    return 1;
512 }
513
514 static int quitcmd(UAContext *ua, TREE_CTX *tree) 
515 {
516    return 0;
517 }