2 * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3 * Copyright (c) 1991-1998, 2000 University of Maryland at College Park
6 * Permission to use, copy, modify, distribute, and sell this software and its
7 * documentation for any purpose is hereby granted without fee, provided that
8 * the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of U.M. not be used in advertising or
11 * publicity pertaining to distribution of the software without specific,
12 * written prior permission. U.M. makes no representations about the
13 * suitability of this software for any purpose. It is provided "as is"
14 * without express or implied warranty.
16 * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
18 * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
20 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 * Authors: the Amanda Development Team. Its members are listed in a
24 * file named AUTHORS, in the root directory of this distribution.
27 * $Id: set_commands.c,v 1.3 2006/07/05 13:14:58 martinea Exp $
29 * implements the "set" commands in amrecover
34 #include "amrecover.h"
37 extern unsigned short samba_extract_method;
38 #endif /* SAMBA_CLIENT */
40 /* sets a date, mapping given date into standard form if needed */
49 cmd = stralloc2("DATE ", date);
50 if (converse(cmd) == -1)
53 /* if a host/disk/directory is set, then check if that directory
54 is still valid at the new date, and if not set directory to
56 if (disk_path != NULL) {
57 cmd = newstralloc2(cmd, "OISD ", disk_path);
58 if (exchange(cmd) == -1)
62 suck_dir_list_from_server();
66 printf("No index records for cwd on new date\n");
67 printf("Setting cwd to mount point\n");
68 disk_path = newstralloc(disk_path, "/"); /* fake it */
85 char *uqhost = unquote_string(host);
87 if (is_extract_list_nonempty())
89 printf("Must clear extract list before changing host\n");
93 cmd = stralloc2("HOST ", uqhost);
94 if (converse(cmd) == -1)
103 * Try converting the given host to a fully qualified name
104 * and then try each of the aliases.
106 if ((hp = gethostbyname(uqhost)) != NULL) {
108 printf("Trying host %s ...\n", host);
109 cmd = newstralloc2(cmd, "HOST ", host);
110 if (converse(cmd) == -1)
118 for (hostp = hp->h_aliases; (host = *hostp) != NULL; hostp++)
120 printf("Trying host %s ...\n", host);
121 cmd = newstralloc2(cmd, "HOST ", host);
122 if (converse(cmd) == -1)
135 dump_hostname = newstralloc(dump_hostname, host);
151 cmd = stralloc("LISTHOST");
152 if (converse(cmd) == -1)
166 if (is_extract_list_nonempty())
168 printf("Must clear extract list before changing disk\n");
172 /* if mount point specified, check it is valid */
174 uqmtpt = unquote_string(mtpt);
176 printf("Mount point \"%s\" invalid - must start with /\n", uqmtpt);
183 uqdsk = unquote_string(dsk);
184 cmd = stralloc2("DISK ", uqdsk);
185 if (converse(cmd) == -1)
192 disk_name = newstralloc(disk_name, uqdsk);
195 /* mount point not specified */
198 /* disk specified by mount point, hence use it */
199 mount_point = newstralloc(mount_point, uqdsk);
203 /* device name given, use '/' because nothing better */
204 mount_point = newstralloc(mount_point, "/");
209 /* mount point specified */
210 mount_point = newstralloc(mount_point, uqmtpt);
213 /* set the working directory to the mount point */
214 /* there is the possibility that there are no index records for the
215 disk for the given date, hence setting the directory to the
216 mount point will fail. Preempt this by checking first so we can write
217 a more informative message. */
218 if (exchange("OISD /") == -1)
222 disk_path = newstralloc(disk_path, "/");
223 suck_dir_list_from_server(); /* get list of directory contents */
227 printf("No index records for disk for specified date\n");
228 printf("If date correct, notify system administrator\n");
229 disk_path = newstralloc(disk_path, "/"); /* fake it */
244 uqamdevice = unquote_string(amdevice);
245 cmd = stralloc2("LISTDISK ", uqamdevice);
247 if (converse(cmd) == -1)
252 cmd = stralloc("LISTDISK");
253 if (converse(cmd) == -1)
263 char *uqdir = unquote_string(dir);
264 if (chdir(uqdir) == -1) {
279 char *path_on_disk = NULL;
281 if (disk_name == NULL) {
282 printf("Must select disk before changing directory\n");
286 uqglob = unquote_string(glob);
287 regex = glob_to_regex(uqglob);
288 dbprintf(("cd_glob (%s) -> %s\n", uqglob, regex));
289 if ((s = validate_regexp(regex)) != NULL) {
290 printf("\"%s\" is not a valid shell wildcard pattern: ", glob);
296 * glob_to_regex() anchors the beginning of the pattern with ^,
297 * but we will be tacking it onto the end of the current directory
298 * in add_file, so strip that off. Also, it anchors the end with
299 * $, but we need to match a trailing /, add it if it is not there
301 regex_path = stralloc(regex + 1);
303 if(regex_path[strlen(regex_path) - 2] != '/' ) {
304 regex_path[strlen(regex_path) - 1] = '\0';
305 strappend(regex_path, "/$");
308 /* convert path (assumed in cwd) to one on disk */
309 if (strcmp(disk_path, "/") == 0)
310 path_on_disk = stralloc2("/", regex_path);
312 char *clean_disk_path = clean_regex(disk_path);
313 path_on_disk = vstralloc(clean_disk_path, "/", regex_path, NULL);
314 amfree(clean_disk_path);
317 cd_dir(path_on_disk, uqglob);
320 amfree(path_on_disk);
331 char *path_on_disk = NULL;
333 if (disk_name == NULL) {
334 printf("Must select disk before changing directory\n");
338 uqregex = unquote_string(regex);
339 if ((s = validate_regexp(uqregex)) != NULL) {
340 printf("\"%s\" is not a valid regular expression: ", uqregex);
346 /* convert path (assumed in cwd) to one on disk */
347 if (strcmp(disk_path, "/") == 0)
348 path_on_disk = stralloc2("/", regex);
350 char *clean_disk_path = clean_regex(disk_path);
351 path_on_disk = vstralloc(clean_disk_path, "/", regex, NULL);
352 amfree(clean_disk_path);
355 cd_dir(path_on_disk, uqregex);
357 amfree(path_on_disk);
366 char *path_on_disk_slash = NULL;
374 path_on_disk_slash = stralloc2(path_on_disk, "/");
378 for (ditem=get_dir_list(); ditem!=NULL && nb_found <= 1;
379 ditem=get_next_dir_item(ditem))
381 if (match(path_on_disk, ditem->path)
382 || match(path_on_disk_slash, ditem->path))
384 i = strlen(ditem->path);
385 if((i > 0 && ditem->path[i-1] == '/')
386 || (i > 1 && ditem->path[i-2] == '/' && ditem->path[i-1] == '.'))
387 { /* It is a directory */
390 dir = newstralloc(dir,ditem->path);
391 if(dir[strlen(dir)-1] == '/')
392 dir[strlen(dir)-1] = '\0'; /* remove last / */
393 /* remove everything before the last / */
394 dir1 = rindex(dir,'/');
397 dir2 = stralloc(dir1);
404 amfree(path_on_disk_slash);
407 set_directory(default_dir);
409 else if(nb_found==1) {
413 printf("Too many directory\n");
423 char *new_dir = NULL;
427 /* do nothing if "." */
428 if(strcmp(dir,".")==0) {
429 show_directory(); /* say where we are */
434 if (disk_name == NULL) {
435 printf("Must select disk before setting directory\n");
440 ldir = stralloc(dir);
441 clean_pathname(ldir);
443 /* convert directory into absolute path relative to disk mount point */
446 /* absolute path specified, must start with mount point */
447 if (strcmp(mount_point, "/") == 0)
449 new_dir = stralloc(ldir);
453 if (strncmp(mount_point, ldir, strlen(mount_point)) != 0)
455 printf("Invalid directory - Can't cd outside mount point \"%s\"\n",
461 new_dir = stralloc(ldir+strlen(mount_point));
462 if (strlen(new_dir) == 0) {
463 new_dir = newstralloc(new_dir, "/");
464 /* i.e. ldir == mount_point */
470 new_dir = stralloc(disk_path);
472 /* strip any leading ..s */
473 while (strncmp(dp, "../", 3) == 0)
475 de = strrchr(new_dir, '/'); /* always at least 1 */
488 if (strcmp(dp, "..") == 0) {
489 if (strcmp(new_dir, "/") == 0) {
491 printf("Invalid directory - Can't cd outside mount point \"%s\"\n",
500 de = strrchr(new_dir, '/'); /* always at least 1 */
512 if (strcmp(new_dir, "/") != 0) {
513 strappend(new_dir, "/");
515 strappend(new_dir, ldir);
520 cmd = stralloc2("OISD ", new_dir);
521 if (exchange(cmd) == -1) {
529 disk_path = newstralloc(disk_path, new_dir);
530 suck_dir_list_from_server(); /* get list of directory contents */
531 show_directory(); /* say where we moved to */
535 printf("Invalid directory - %s\n", dir);
545 /* prints the current working directory */
549 if (mount_point == NULL || disk_path == NULL)
550 printf("Must select disk first\n");
551 else if (strcmp(mount_point, "/") == 0)
552 printf("%s\n", disk_path);
553 else if (strcmp(disk_path, "/") == 0)
554 printf("%s\n", mount_point);
556 printf("%s%s\n", mount_point, disk_path);
560 /* set the tape server and device */
565 char *uqtape = unquote_string(tape);
566 char *tapedev = strchr(uqtape, ':');
570 if (tapedev != uqtape) {
571 if((strchr(tapedev+1, ':') == NULL) &&
572 (strncmp(uqtape, "null:", 5) == 0 ||
573 strncmp(uqtape, "rait:", 5) == 0 ||
574 strncmp(uqtape, "file:", 5) == 0 ||
575 strncmp(uqtape, "tape:", 5) == 0)) {
580 tape_server_name = newstralloc(tape_server_name, uqtape);
583 } else { /* reset server_name if start with : */
584 amfree(tape_server_name);
592 if (strcmp(tapedev, "default") == 0)
593 amfree(tape_device_name);
595 tape_device_name = newstralloc(tape_device_name, tapedev);
598 if (tape_device_name)
599 printf ("Using tape \"%s\"", tape_device_name);
601 printf ("Using default tape");
603 if (tape_server_name)
604 printf (" from server %s.\n", tape_server_name);
606 printf (".\nTape server unspecified, assumed to be %s.\n",
615 if (mode == SAMBA_SMBCLIENT) {
616 printf ("SAMBA dumps will be extracted using smbclient\n");
617 samba_extract_method = SAMBA_SMBCLIENT;
619 if (mode == SAMBA_TAR) {
620 printf ("SAMBA dumps will be extracted as TAR dumps\n");
621 samba_extract_method = SAMBA_TAR;
625 (void)mode; /* Quiet unused parameter warning */
626 #endif /* SAMBA_CLIENT */
633 printf ("SAMBA dumps are extracted ");
635 if (samba_extract_method == SAMBA_TAR) {
636 printf (" as TAR dumps\n");
638 printf ("using smbclient\n");
640 #endif /* SAMBA_CLIENT */