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.23 2004/02/11 13:15:18 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 */
47 cmd = stralloc2("DATE ", date);
48 if (converse(cmd) == -1)
51 /* if a host/disk/directory is set, then check if that directory
52 is still valid at the new date, and if not set directory to
54 if (disk_path != NULL) {
55 cmd = newstralloc2(cmd, "OISD ", disk_path);
56 if (exchange(cmd) == -1)
60 suck_dir_list_from_server();
64 printf("No index records for cwd on new date\n");
65 printf("Setting cwd to mount point\n");
66 disk_path = newstralloc(disk_path, "/"); /* fake it */
84 if (is_extract_list_nonempty())
86 printf("Must clear extract list before changing host\n");
90 cmd = stralloc2("HOST ", host);
91 if (converse(cmd) == -1)
100 * Try converting the given host to a fully qualified name
101 * and then try each of the aliases.
103 if ((hp = gethostbyname(host)) != NULL) {
105 printf("Trying host %s ...\n", host);
106 cmd = newstralloc2(cmd, "HOST ", host);
107 if (converse(cmd) == -1)
115 for (hostp = hp->h_aliases; (host = *hostp) != NULL; hostp++)
117 printf("Trying host %s ...\n", host);
118 cmd = newstralloc2(cmd, "HOST ", host);
119 if (converse(cmd) == -1)
132 dump_hostname = newstralloc(dump_hostname, host);
142 void set_disk(dsk, mtpt)
148 if (is_extract_list_nonempty())
150 printf("Must clear extract list before changing disk\n");
154 /* if mount point specified, check it is valid */
155 if ((mtpt != NULL) && (*mtpt != '/'))
157 printf("Mount point \"%s\" invalid - must start with /\n", mtpt);
162 cmd = stralloc2("DISK ", dsk);
163 if (converse(cmd) == -1)
170 disk_name = newstralloc(disk_name, dsk);
173 /* mount point not specified */
176 /* disk specified by mount point, hence use it */
177 mount_point = newstralloc(mount_point, dsk);
181 /* device name given, use '/' because nothing better */
182 mount_point = newstralloc(mount_point, "/");
187 /* mount point specified */
188 mount_point = newstralloc(mount_point, mtpt);
191 /* set the working directory to the mount point */
192 /* there is the possibility that there are no index records for the
193 disk for the given date, hence setting the directory to the
194 mount point will fail. Preempt this by checking first so we can write
195 a more informative message. */
196 if (exchange("OISD /") == -1)
200 disk_path = newstralloc(disk_path, "/");
201 suck_dir_list_from_server(); /* get list of directory contents */
205 printf("No index records for disk for specified date\n");
206 printf("If date correct, notify system administrator\n");
207 disk_path = newstralloc(disk_path, "/"); /* fake it */
212 void list_disk(amdevice)
218 cmd = stralloc2("LISTDISK ", amdevice);
219 if (converse(cmd) == -1)
224 cmd = stralloc("LISTDISK");
225 if (converse(cmd) == -1)
238 char *path_on_disk = NULL;
240 if (disk_name == NULL) {
241 printf("Must select disk before changing directory\n");
245 regex = glob_to_regex(glob);
246 dbprintf(("cd_glob (%s) -> %s\n", glob, regex));
247 if ((s = validate_regexp(regex)) != NULL) {
248 printf("\"%s\" is not a valid shell wildcard pattern: ", glob);
254 * glob_to_regex() anchors the beginning of the pattern with ^,
255 * but we will be tacking it onto the end of the current directory
256 * in add_file, so strip that off. Also, it anchors the end with
257 * $, but we need to match a trailing /, add it if it is not there
259 regex_path = stralloc(regex + 1);
261 if(regex_path[strlen(regex_path) - 2] != '/' ) {
262 regex_path[strlen(regex_path) - 1] = '\0';
263 strappend(regex_path, "/$");
266 /* convert path (assumed in cwd) to one on disk */
267 if (strcmp(disk_path, "/") == 0)
268 path_on_disk = stralloc2("/", regex_path);
270 char *clean_disk_path = clean_regex(disk_path);
271 path_on_disk = vstralloc(clean_disk_path, "/", regex_path, NULL);
272 amfree(clean_disk_path);
275 cd_dir(path_on_disk, glob);
278 amfree(path_on_disk);
286 char *path_on_disk = NULL;
288 if (disk_name == NULL) {
289 printf("Must select disk before changing directory\n");
293 if ((s = validate_regexp(regex)) != NULL) {
294 printf("\"%s\" is not a valid regular expression: ", regex);
299 /* convert path (assumed in cwd) to one on disk */
300 if (strcmp(disk_path, "/") == 0)
301 path_on_disk = stralloc2("/", regex);
303 char *clean_disk_path = clean_regex(disk_path);
304 path_on_disk = vstralloc(clean_disk_path, "/", regex, NULL);
305 amfree(clean_disk_path);
308 cd_dir(path_on_disk, regex);
310 amfree(path_on_disk);
313 void cd_dir(path_on_disk, default_dir)
317 char *path_on_disk_slash = NULL;
325 path_on_disk_slash = stralloc2(path_on_disk, "/");
329 for (ditem=get_dir_list(); ditem!=NULL && nb_found <= 1;
330 ditem=get_next_dir_item(ditem))
332 if (match(path_on_disk, ditem->path)
333 || match(path_on_disk_slash, ditem->path))
335 i = strlen(ditem->path);
336 if((i > 0 && ditem->path[i-1] == '/')
337 || (i > 1 && ditem->path[i-2] == '/' && ditem->path[i-1] == '.'))
338 { /* It is a directory */
341 dir = newstralloc(dir,ditem->path);
342 if(dir[strlen(dir)-1] == '/')
343 dir[strlen(dir)-1] = '\0'; /* remove last / */
344 /* remove everything before the last / */
345 dir1 = rindex(dir,'/');
347 dir2 = stralloc(dir1);
353 amfree(path_on_disk_slash);
356 set_directory(default_dir);
358 else if(nb_found==1) {
362 printf("Too many directory\n");
367 void set_directory(dir)
371 char *new_dir = NULL;
375 /* do nothing if "." */
376 if(strcmp(dir,".")==0) {
377 show_directory(); /* say where we are */
381 if (disk_name == NULL) {
382 printf("Must select disk before setting directory\n");
386 ldir = stralloc(dir);
387 clean_pathname(ldir);
389 /* convert directory into absolute path relative to disk mount point */
392 /* absolute path specified, must start with mount point */
393 if (strcmp(mount_point, "/") == 0)
395 new_dir = stralloc(ldir);
399 if (strncmp(mount_point, ldir, strlen(mount_point)) != 0)
401 printf("Invalid directory - Can't cd outside mount point \"%s\"\n",
406 new_dir = stralloc(ldir+strlen(mount_point));
407 if (strlen(new_dir) == 0) {
408 new_dir = newstralloc(new_dir, "/");
409 /* i.e. ldir == mount_point */
415 new_dir = stralloc(disk_path);
417 /* strip any leading ..s */
418 while (strncmp(dp, "../", 3) == 0)
420 de = strrchr(new_dir, '/'); /* always at least 1 */
433 if (strcmp(dp, "..") == 0) {
434 if (strcmp(new_dir, "/") == 0) {
436 printf("Invalid directory - Can't cd outside mount point \"%s\"\n",
442 de = strrchr(new_dir, '/'); /* always at least 1 */
453 if (strcmp(new_dir, "/") != 0) {
454 strappend(new_dir, "/");
456 strappend(new_dir, ldir);
460 cmd = stralloc2("OISD ", new_dir);
461 if (exchange(cmd) == -1)
466 disk_path = newstralloc(disk_path, new_dir);
467 suck_dir_list_from_server(); /* get list of directory contents */
468 show_directory(); /* say where we moved to */
472 printf("Invalid directory - %s\n", dir);
480 /* prints the current working directory */
481 void show_directory P((void))
483 if (mount_point == NULL || disk_path == NULL)
484 printf("Must select disk first\n");
485 else if (strcmp(mount_point, "/") == 0)
486 printf("%s\n", disk_path);
487 else if (strcmp(disk_path, "/") == 0)
488 printf("%s\n", mount_point);
490 printf("%s%s\n", mount_point, disk_path);
494 /* set the tape server and device */
498 char *tapedev = strchr(tape, ':');
502 if (tapedev != tape) {
503 if((strchr(tapedev+1, ':') == NULL) &&
504 (strncmp(tape, "null:", 5) == 0 ||
505 strncmp(tape, "rait:", 5) == 0 ||
506 strncmp(tape, "file:", 5) == 0 ||
507 strncmp(tape, "tape:", 5) == 0)) {
512 tape_server_name = newstralloc(tape_server_name, tape);
515 } else { /* reset server_name if start with : */
516 amfree(tape_server_name);
524 if (strcmp(tapedev, "default") == 0)
525 amfree(tape_device_name);
527 tape_device_name = newstralloc(tape_device_name, tapedev);
530 if (tape_device_name)
531 printf ("Using tape \"%s\"", tape_device_name);
533 printf ("Using default tape");
535 if (tape_server_name)
536 printf (" from server %s.\n", tape_server_name);
538 printf (".\nTape server unspecified, assumed to be %s.\n",
546 if (mode == SAMBA_SMBCLIENT) {
547 printf ("SAMBA dumps will be extracted using smbclient\n");
548 samba_extract_method = SAMBA_SMBCLIENT;
550 if (mode == SAMBA_TAR) {
551 printf ("SAMBA dumps will be extracted as TAR dumps\n");
552 samba_extract_method = SAMBA_TAR;
555 #endif /* SAMBA_CLIENT */
558 void show_mode (void)
561 printf ("SAMBA dumps are extracted ");
563 if (samba_extract_method == SAMBA_TAR) {
564 printf (" as TAR dumps\n");
566 printf ("using smbclient\n");
568 #endif /* SAMBA_CLIENT */