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.26.2.1 2006/12/22 15:10:27 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 */
50 cmd = stralloc2("DATE ", date);
51 if (converse(cmd) == -1)
54 /* if a host/disk/directory is set, then check if that directory
55 is still valid at the new date, and if not set directory to
57 if (disk_path != NULL) {
58 qdisk_path = quote_string(disk_path);
59 cmd = newstralloc2(cmd, "OISD ", qdisk_path);
61 if (exchange(cmd) == -1)
65 suck_dir_list_from_server();
69 printf("No index records for cwd on new date\n");
70 printf("Setting cwd to mount point\n");
71 disk_path = newstralloc(disk_path, "/"); /* fake it */
89 if (is_extract_list_nonempty())
91 printf("Must clear extract list before changing host\n");
95 cmd = stralloc2("HOST ", host);
96 if (converse(cmd) == -1)
105 * Try converting the given host to a fully qualified name
106 * and then try each of the aliases.
108 if ((hp = gethostbyname(host)) != NULL) {
110 printf("Trying host %s ...\n", host);
111 cmd = newstralloc2(cmd, "HOST ", host);
112 if (converse(cmd) == -1)
120 for (hostp = hp->h_aliases; (host = *hostp) != NULL; hostp++)
122 printf("Trying host %s ...\n", host);
123 cmd = newstralloc2(cmd, "HOST ", host);
124 if (converse(cmd) == -1)
137 dump_hostname = newstralloc(dump_hostname, host);
151 cmd = stralloc("LISTHOST");
152 if (converse(cmd) == -1)
165 if (is_extract_list_nonempty())
167 printf("Must clear extract list before changing disk\n");
171 /* if mount point specified, check it is valid */
172 if ((mtpt != NULL) && (*mtpt != '/'))
174 printf("Mount point \"%s\" invalid - must start with /\n", mtpt);
179 qdsk = quote_string(dsk);
180 cmd = stralloc2("DISK ", qdsk);
182 if (converse(cmd) == -1)
189 disk_name = newstralloc(disk_name, dsk);
192 /* mount point not specified */
195 /* disk specified by mount point, hence use it */
196 mount_point = newstralloc(mount_point, dsk);
200 /* device name given, use '/' because nothing better */
201 mount_point = newstralloc(mount_point, "/");
206 /* mount point specified */
207 mount_point = newstralloc(mount_point, mtpt);
210 /* set the working directory to the mount point */
211 /* there is the possibility that there are no index records for the
212 disk for the given date, hence setting the directory to the
213 mount point will fail. Preempt this by checking first so we can write
214 a more informative message. */
215 if (exchange("OISD /") == -1)
219 disk_path = newstralloc(disk_path, "/");
220 suck_dir_list_from_server(); /* get list of directory contents */
224 printf("No index records for disk for specified date\n");
225 printf("If date correct, notify system administrator\n");
226 disk_path = newstralloc(disk_path, "/"); /* fake it */
239 qamdevice = quote_string(amdevice);
240 cmd = stralloc2("LISTDISK ", qamdevice);
242 if (converse(cmd) == -1)
247 cmd = stralloc("LISTDISK");
248 if (converse(cmd) == -1)
262 char *path_on_disk = NULL;
264 if (disk_name == NULL) {
265 printf("Must select disk before changing directory\n");
269 regex = glob_to_regex(glob);
270 dbprintf(("cd_glob (%s) -> %s\n", glob, regex));
271 if ((s = validate_regexp(regex)) != NULL) {
272 printf("\"%s\" is not a valid shell wildcard pattern: ", glob);
278 * glob_to_regex() anchors the beginning of the pattern with ^,
279 * but we will be tacking it onto the end of the current directory
280 * in add_file, so strip that off. Also, it anchors the end with
281 * $, but we need to match a trailing /, add it if it is not there
283 regex_path = stralloc(regex + 1);
285 if(regex_path[strlen(regex_path) - 2] != '/' ) {
286 regex_path[strlen(regex_path) - 1] = '\0';
287 strappend(regex_path, "/$");
290 /* convert path (assumed in cwd) to one on disk */
291 if (strcmp(disk_path, "/") == 0)
292 path_on_disk = stralloc2("/", regex_path);
294 char *clean_disk_path = clean_regex(disk_path);
295 path_on_disk = vstralloc(clean_disk_path, "/", regex_path, NULL);
296 amfree(clean_disk_path);
299 cd_dir(path_on_disk, glob);
302 amfree(path_on_disk);
311 char *path_on_disk = NULL;
313 if (disk_name == NULL) {
314 printf("Must select disk before changing directory\n");
318 if ((s = validate_regexp(regex)) != NULL) {
319 printf("\"%s\" is not a valid regular expression: ", regex);
324 /* convert path (assumed in cwd) to one on disk */
325 if (strcmp(disk_path, "/") == 0)
326 path_on_disk = stralloc2("/", regex);
328 char *clean_disk_path = clean_regex(disk_path);
329 path_on_disk = vstralloc(clean_disk_path, "/", regex, NULL);
330 amfree(clean_disk_path);
333 cd_dir(path_on_disk, regex);
335 amfree(path_on_disk);
343 char *path_on_disk_slash = NULL;
351 path_on_disk_slash = stralloc2(path_on_disk, "/");
355 for (ditem=get_dir_list(); ditem!=NULL && nb_found <= 1;
356 ditem=get_next_dir_item(ditem))
358 if (match(path_on_disk, ditem->path)
359 || match(path_on_disk_slash, ditem->path))
361 i = strlen(ditem->path);
362 if((i > 0 && ditem->path[i-1] == '/')
363 || (i > 1 && ditem->path[i-2] == '/' && ditem->path[i-1] == '.'))
364 { /* It is a directory */
367 dir = newstralloc(dir,ditem->path);
368 if(dir[strlen(dir)-1] == '/')
369 dir[strlen(dir)-1] = '\0'; /* remove last / */
370 /* remove everything before the last / */
371 dir1 = rindex(dir,'/');
374 dir2 = stralloc(dir1);
381 amfree(path_on_disk_slash);
384 set_directory(default_dir);
386 else if(nb_found==1) {
390 printf("Too many directory\n");
400 char *new_dir = NULL;
405 /* do nothing if "." */
406 if(strcmp(dir,".")==0) {
407 show_directory(); /* say where we are */
412 if (disk_name == NULL) {
413 printf("Must select disk before setting directory\n");
418 ldir = stralloc(dir);
419 clean_pathname(ldir);
421 /* convert directory into absolute path relative to disk mount point */
424 /* absolute path specified, must start with mount point */
425 if (strcmp(mount_point, "/") == 0)
427 new_dir = stralloc(ldir);
431 if (strncmp(mount_point, ldir, strlen(mount_point)) != 0)
433 printf("Invalid directory - Can't cd outside mount point \"%s\"\n",
439 new_dir = stralloc(ldir+strlen(mount_point));
440 if (strlen(new_dir) == 0) {
441 new_dir = newstralloc(new_dir, "/");
442 /* i.e. ldir == mount_point */
448 new_dir = stralloc(disk_path);
450 /* strip any leading ..s */
451 while (strncmp(dp, "../", 3) == 0)
453 de = strrchr(new_dir, '/'); /* always at least 1 */
466 if (strcmp(dp, "..") == 0) {
467 if (strcmp(new_dir, "/") == 0) {
469 printf("Invalid directory - Can't cd outside mount point \"%s\"\n",
478 de = strrchr(new_dir, '/'); /* always at least 1 */
490 if (strcmp(new_dir, "/") != 0) {
491 strappend(new_dir, "/");
493 strappend(new_dir, ldir);
498 qnew_dir = quote_string(new_dir);
499 cmd = stralloc2("OISD ", qnew_dir);
501 if (exchange(cmd) == -1) {
509 disk_path = newstralloc(disk_path, new_dir);
510 suck_dir_list_from_server(); /* get list of directory contents */
511 show_directory(); /* say where we moved to */
515 printf("Invalid directory - %s\n", dir);
525 /* prints the current working directory */
529 if (mount_point == NULL || disk_path == NULL)
530 printf("Must select disk first\n");
531 else if (strcmp(mount_point, "/") == 0)
532 printf("%s\n", disk_path);
533 else if (strcmp(disk_path, "/") == 0)
534 printf("%s\n", mount_point);
536 printf("%s%s\n", mount_point, disk_path);
540 /* set the tape server and device */
545 char *tapedev = strchr(tape, ':');
549 if (tapedev != tape) {
550 if((strchr(tapedev+1, ':') == NULL) &&
551 (strncmp(tape, "null:", 5) == 0 ||
552 strncmp(tape, "rait:", 5) == 0 ||
553 strncmp(tape, "file:", 5) == 0 ||
554 strncmp(tape, "tape:", 5) == 0)) {
559 tape_server_name = newstralloc(tape_server_name, tape);
562 } else { /* reset server_name if start with : */
563 amfree(tape_server_name);
571 if (strcmp(tapedev, "default") == 0)
572 amfree(tape_device_name);
574 tape_device_name = newstralloc(tape_device_name, tapedev);
577 if (tape_device_name)
578 printf ("Using tape \"%s\"", tape_device_name);
580 printf ("Using default tape");
582 if (tape_server_name)
583 printf (" from server %s.\n", tape_server_name);
585 printf (".\nTape server unspecified, assumed to be %s.\n",
594 if (mode == SAMBA_SMBCLIENT) {
595 printf ("SAMBA dumps will be extracted using smbclient\n");
596 samba_extract_method = SAMBA_SMBCLIENT;
598 if (mode == SAMBA_TAR) {
599 printf ("SAMBA dumps will be extracted as TAR dumps\n");
600 samba_extract_method = SAMBA_TAR;
604 (void)mode; /* Quiet unused parameter warning */
605 #endif /* SAMBA_CLIENT */
612 printf ("SAMBA dumps are extracted ");
614 if (samba_extract_method == SAMBA_TAR) {
615 printf (" as TAR dumps\n");
617 printf ("using smbclient\n");
619 #endif /* SAMBA_CLIENT */