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
33 #include "amrecover.h"
36 extern unsigned short samba_extract_method;
37 #endif /* SAMBA_CLIENT */
39 /* sets a date, mapping given date into standard form if needed */
48 cmd = stralloc2("DATE ", date);
49 if (converse(cmd) == -1)
52 /* if a host/disk/directory is set, then check if that directory
53 is still valid at the new date, and if not set directory to
55 if (disk_path != NULL) {
56 cmd = newstralloc2(cmd, "OISD ", disk_path);
57 if (exchange(cmd) == -1)
61 suck_dir_list_from_server();
65 printf("No index records for cwd on new date\n");
66 printf("Setting cwd to mount point\n");
67 disk_path = newstralloc(disk_path, "/"); /* fake it */
85 if (is_extract_list_nonempty())
87 printf("Must clear extract list before changing host\n");
91 cmd = stralloc2("HOST ", host);
92 if (converse(cmd) == -1)
101 * Try converting the given host to a fully qualified name
102 * and then try each of the aliases.
104 if ((hp = gethostbyname(host)) != NULL) {
106 printf("Trying host %s ...\n", host);
107 cmd = newstralloc2(cmd, "HOST ", host);
108 if (converse(cmd) == -1)
116 for (hostp = hp->h_aliases; (host = *hostp) != NULL; hostp++)
118 printf("Trying host %s ...\n", host);
119 cmd = newstralloc2(cmd, "HOST ", host);
120 if (converse(cmd) == -1)
133 dump_hostname = newstralloc(dump_hostname, host);
148 cmd = stralloc("LISTHOST");
149 if (converse(cmd) == -1)
161 if (is_extract_list_nonempty())
163 printf("Must clear extract list before changing disk\n");
167 /* if mount point specified, check it is valid */
168 if ((mtpt != NULL) && (*mtpt != '/'))
170 printf("Mount point \"%s\" invalid - must start with /\n", mtpt);
175 cmd = stralloc2("DISK ", dsk);
176 if (converse(cmd) == -1)
183 disk_name = newstralloc(disk_name, dsk);
186 /* mount point not specified */
189 /* disk specified by mount point, hence use it */
190 mount_point = newstralloc(mount_point, dsk);
194 /* device name given, use '/' because nothing better */
195 mount_point = newstralloc(mount_point, "/");
200 /* mount point specified */
201 mount_point = newstralloc(mount_point, mtpt);
204 /* set the working directory to the mount point */
205 /* there is the possibility that there are no index records for the
206 disk for the given date, hence setting the directory to the
207 mount point will fail. Preempt this by checking first so we can write
208 a more informative message. */
209 if (exchange("OISD /") == -1)
213 disk_path = newstralloc(disk_path, "/");
214 suck_dir_list_from_server(); /* get list of directory contents */
218 printf("No index records for disk for specified date\n");
219 printf("If date correct, notify system administrator\n");
220 disk_path = newstralloc(disk_path, "/"); /* fake it */
232 cmd = stralloc2("LISTDISK ", amdevice);
233 if (converse(cmd) == -1)
238 cmd = stralloc("LISTDISK");
239 if (converse(cmd) == -1)
253 char *path_on_disk = NULL;
255 if (disk_name == NULL) {
256 printf("Must select disk before changing directory\n");
260 regex = glob_to_regex(glob);
261 dbprintf(("cd_glob (%s) -> %s\n", glob, regex));
262 if ((s = validate_regexp(regex)) != NULL) {
263 printf("\"%s\" is not a valid shell wildcard pattern: ", glob);
269 * glob_to_regex() anchors the beginning of the pattern with ^,
270 * but we will be tacking it onto the end of the current directory
271 * in add_file, so strip that off. Also, it anchors the end with
272 * $, but we need to match a trailing /, add it if it is not there
274 regex_path = stralloc(regex + 1);
276 if(regex_path[strlen(regex_path) - 2] != '/' ) {
277 regex_path[strlen(regex_path) - 1] = '\0';
278 strappend(regex_path, "/$");
281 /* convert path (assumed in cwd) to one on disk */
282 if (strcmp(disk_path, "/") == 0)
283 path_on_disk = stralloc2("/", regex_path);
285 char *clean_disk_path = clean_regex(disk_path);
286 path_on_disk = vstralloc(clean_disk_path, "/", regex_path, NULL);
287 amfree(clean_disk_path);
290 cd_dir(path_on_disk, glob);
293 amfree(path_on_disk);
302 char *path_on_disk = NULL;
304 if (disk_name == NULL) {
305 printf("Must select disk before changing directory\n");
309 if ((s = validate_regexp(regex)) != NULL) {
310 printf("\"%s\" is not a valid regular expression: ", regex);
315 /* convert path (assumed in cwd) to one on disk */
316 if (strcmp(disk_path, "/") == 0)
317 path_on_disk = stralloc2("/", regex);
319 char *clean_disk_path = clean_regex(disk_path);
320 path_on_disk = vstralloc(clean_disk_path, "/", regex, NULL);
321 amfree(clean_disk_path);
324 cd_dir(path_on_disk, regex);
326 amfree(path_on_disk);
334 char *path_on_disk_slash = NULL;
342 path_on_disk_slash = stralloc2(path_on_disk, "/");
346 for (ditem=get_dir_list(); ditem!=NULL && nb_found <= 1;
347 ditem=get_next_dir_item(ditem))
349 if (match(path_on_disk, ditem->path)
350 || match(path_on_disk_slash, ditem->path))
352 i = strlen(ditem->path);
353 if((i > 0 && ditem->path[i-1] == '/')
354 || (i > 1 && ditem->path[i-2] == '/' && ditem->path[i-1] == '.'))
355 { /* It is a directory */
358 dir = newstralloc(dir,ditem->path);
359 if(dir[strlen(dir)-1] == '/')
360 dir[strlen(dir)-1] = '\0'; /* remove last / */
361 /* remove everything before the last / */
362 dir1 = rindex(dir,'/');
365 dir2 = stralloc(dir1);
372 amfree(path_on_disk_slash);
375 set_directory(default_dir);
377 else if(nb_found==1) {
381 printf("Too many directory\n");
391 char *new_dir = NULL;
395 /* do nothing if "." */
396 if(strcmp(dir,".")==0) {
397 show_directory(); /* say where we are */
402 if (disk_name == NULL) {
403 printf("Must select disk before setting directory\n");
408 ldir = stralloc(dir);
409 clean_pathname(ldir);
411 /* convert directory into absolute path relative to disk mount point */
414 /* absolute path specified, must start with mount point */
415 if (strcmp(mount_point, "/") == 0)
417 new_dir = stralloc(ldir);
421 if (strncmp(mount_point, ldir, strlen(mount_point)) != 0)
423 printf("Invalid directory - Can't cd outside mount point \"%s\"\n",
429 new_dir = stralloc(ldir+strlen(mount_point));
430 if (strlen(new_dir) == 0) {
431 new_dir = newstralloc(new_dir, "/");
432 /* i.e. ldir == mount_point */
438 new_dir = stralloc(disk_path);
440 /* strip any leading ..s */
441 while (strncmp(dp, "../", 3) == 0)
443 de = strrchr(new_dir, '/'); /* always at least 1 */
456 if (strcmp(dp, "..") == 0) {
457 if (strcmp(new_dir, "/") == 0) {
459 printf("Invalid directory - Can't cd outside mount point \"%s\"\n",
468 de = strrchr(new_dir, '/'); /* always at least 1 */
480 if (strcmp(new_dir, "/") != 0) {
481 strappend(new_dir, "/");
483 strappend(new_dir, ldir);
488 cmd = stralloc2("OISD ", new_dir);
489 if (exchange(cmd) == -1) {
497 disk_path = newstralloc(disk_path, new_dir);
498 suck_dir_list_from_server(); /* get list of directory contents */
499 show_directory(); /* say where we moved to */
503 printf("Invalid directory - %s\n", dir);
513 /* prints the current working directory */
517 if (mount_point == NULL || disk_path == NULL)
518 printf("Must select disk first\n");
519 else if (strcmp(mount_point, "/") == 0)
520 printf("%s\n", disk_path);
521 else if (strcmp(disk_path, "/") == 0)
522 printf("%s\n", mount_point);
524 printf("%s%s\n", mount_point, disk_path);
528 /* set the tape server and device */
533 char *tapedev = strchr(tape, ':');
537 if (tapedev != tape) {
538 if((strchr(tapedev+1, ':') == NULL) &&
539 (strncmp(tape, "null:", 5) == 0 ||
540 strncmp(tape, "rait:", 5) == 0 ||
541 strncmp(tape, "file:", 5) == 0 ||
542 strncmp(tape, "tape:", 5) == 0)) {
547 tape_server_name = newstralloc(tape_server_name, tape);
550 } else { /* reset server_name if start with : */
551 amfree(tape_server_name);
559 if (strcmp(tapedev, "default") == 0)
560 amfree(tape_device_name);
562 tape_device_name = newstralloc(tape_device_name, tapedev);
565 if (tape_device_name)
566 printf ("Using tape \"%s\"", tape_device_name);
568 printf ("Using default tape");
570 if (tape_server_name)
571 printf (" from server %s.\n", tape_server_name);
573 printf (".\nTape server unspecified, assumed to be %s.\n",
582 if (mode == SAMBA_SMBCLIENT) {
583 printf ("SAMBA dumps will be extracted using smbclient\n");
584 samba_extract_method = SAMBA_SMBCLIENT;
586 if (mode == SAMBA_TAR) {
587 printf ("SAMBA dumps will be extracted as TAR dumps\n");
588 samba_extract_method = SAMBA_TAR;
592 (void)mode; /* Quiet unused parameter warning */
593 #endif /* SAMBA_CLIENT */
600 printf ("SAMBA dumps are extracted ");
602 if (samba_extract_method == SAMBA_TAR) {
603 printf (" as TAR dumps\n");
605 printf ("using smbclient\n");
607 #endif /* SAMBA_CLIENT */