PageRenderTime 46ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/amanda/tags/amanda260p2/recover-src/set_commands.c

#
C | 721 lines | 551 code | 73 blank | 97 comment | 164 complexity | 96d81eb5bc0dc24e46b269c2385739d6 MD5 | raw file
  1. /*
  2. * Amanda, The Advanced Maryland Automatic Network Disk Archiver
  3. * Copyright (c) 1991-1998, 2000 University of Maryland at College Park
  4. * All Rights Reserved.
  5. *
  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.
  15. *
  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.
  22. *
  23. * Authors: the Amanda Development Team. Its members are listed in a
  24. * file named AUTHORS, in the root directory of this distribution.
  25. */
  26. /*
  27. * $Id: set_commands.c,v 1.26 2006/07/05 13:14:58 martinea Exp $
  28. *
  29. * implements the "set" commands in amrecover
  30. */
  31. #include "amanda.h"
  32. #include "util.h"
  33. #include "amrecover.h"
  34. #ifdef SAMBA_CLIENT
  35. extern unsigned short samba_extract_method;
  36. #endif /* SAMBA_CLIENT */
  37. /* sets a date, mapping given date into standard form if needed */
  38. int
  39. set_date(
  40. char * date)
  41. {
  42. char *cmd = NULL;
  43. char *qdisk_path;
  44. clear_dir_list();
  45. cmd = stralloc2("DATE ", date);
  46. if (converse(cmd) == -1)
  47. exit(1);
  48. /* if a host/disk/directory is set, then check if that directory
  49. is still valid at the new date, and if not set directory to
  50. mount_point */
  51. if (disk_path != NULL) {
  52. qdisk_path = quote_string(disk_path);
  53. cmd = newstralloc2(cmd, "OISD ", qdisk_path);
  54. amfree(qdisk_path);
  55. if (exchange(cmd) == -1)
  56. exit(1);
  57. if (server_happy())
  58. {
  59. suck_dir_list_from_server();
  60. }
  61. else
  62. {
  63. g_printf(_("No index records for cwd on new date\n"));
  64. g_printf(_("Setting cwd to mount point\n"));
  65. disk_path = newstralloc(disk_path, "/"); /* fake it */
  66. clear_dir_list();
  67. }
  68. }
  69. amfree(cmd);
  70. return 0;
  71. }
  72. void
  73. set_host(
  74. const char *host)
  75. {
  76. char *cmd = NULL;
  77. struct hostent *hp = NULL;
  78. char **hostp;
  79. int found_host = 0;
  80. char *uqhost = unquote_string(host);
  81. if (is_extract_list_nonempty())
  82. {
  83. g_printf(_("Must clear extract list before changing host\n"));
  84. return;
  85. }
  86. /*
  87. * The idea here is to try as many permutations of the hostname
  88. * as we can imagine. The server will reject anything it doesn't
  89. * recognize.
  90. */
  91. cmd = stralloc2("HOST ", uqhost);
  92. if (converse(cmd) == -1)
  93. exit(1);
  94. if (server_happy())
  95. found_host = 1;
  96. /*
  97. * Try converting the given host to a fully qualified, canonical
  98. * name.
  99. */
  100. if (!found_host) {
  101. if ((hp = gethostbyname(uqhost)) != NULL) {
  102. host = hp->h_name;
  103. g_printf(_("Trying host %s ...\n"), host);
  104. cmd = newstralloc2(cmd, "HOST ", host);
  105. if (converse(cmd) == -1)
  106. exit(1);
  107. if(server_happy())
  108. found_host = 1;
  109. }
  110. }
  111. /*
  112. * Since we have them, try any CNAMEs that were traversed from uqhost
  113. * to the canonical name (this assumes gethostbyname was called above)
  114. */
  115. if (!found_host) {
  116. if (hp) {
  117. for (hostp = hp->h_aliases; (host = *hostp) != NULL; hostp++)
  118. {
  119. g_printf(_("Trying host %s ...\n"), host);
  120. cmd = newstralloc2(cmd, "HOST ", host);
  121. if (converse(cmd) == -1)
  122. exit(1);
  123. if(server_happy())
  124. {
  125. found_host = 1;
  126. break;
  127. }
  128. }
  129. }
  130. }
  131. /* Try looking up the canonical name of the host */
  132. if (!found_host) {
  133. char *canonname;
  134. int result;
  135. result = resolve_hostname(uqhost, 0, NULL, &canonname);
  136. if (result == 0 && canonname) {
  137. host = canonname;
  138. g_printf(_("Trying host %s ...\n"), host);
  139. cmd = newstralloc2(cmd, "HOST ", host);
  140. if (converse(cmd) == -1)
  141. exit(1);
  142. if(server_happy())
  143. found_host = 1;
  144. }
  145. }
  146. if(found_host) {
  147. dump_hostname = newstralloc(dump_hostname, host);
  148. amfree(disk_name);
  149. amfree(mount_point);
  150. amfree(disk_path);
  151. clear_dir_list();
  152. }
  153. amfree(cmd);
  154. amfree(uqhost);
  155. }
  156. void
  157. list_host(void)
  158. {
  159. char *cmd = NULL;
  160. cmd = stralloc("LISTHOST");
  161. if (converse(cmd) == -1)
  162. exit(1);
  163. amfree(cmd);
  164. }
  165. void
  166. set_disk(
  167. char * dsk,
  168. char * mtpt)
  169. {
  170. char *cmd = NULL;
  171. char *qdsk;
  172. char *uqdsk;
  173. char *uqmtpt = NULL;
  174. if (is_extract_list_nonempty())
  175. {
  176. g_printf(_("Must clear extract list before changing disk\n"));
  177. return;
  178. }
  179. /* if mount point specified, check it is valid */
  180. if (mtpt != NULL) {
  181. uqmtpt = unquote_string(mtpt);
  182. if (*mtpt != '/') {
  183. g_printf(_("Mount point \"%s\" invalid - must start with /\n"), uqmtpt);
  184. amfree(uqmtpt);
  185. return;
  186. }
  187. }
  188. clear_dir_list();
  189. uqdsk = unquote_string(dsk);
  190. qdsk = quote_string(uqdsk);
  191. cmd = stralloc2("DISK ", qdsk);
  192. amfree(qdsk);
  193. if (converse(cmd) == -1)
  194. exit(1);
  195. amfree(cmd);
  196. if (!server_happy())
  197. return;
  198. disk_name = newstralloc(disk_name, uqdsk);
  199. if (mtpt == NULL)
  200. {
  201. /* mount point not specified */
  202. if (*uqdsk == '/')
  203. {
  204. /* disk specified by mount point, hence use it */
  205. mount_point = newstralloc(mount_point, uqdsk);
  206. }
  207. else
  208. {
  209. /* device name given, use '/' because nothing better */
  210. mount_point = newstralloc(mount_point, "/");
  211. }
  212. }
  213. else
  214. {
  215. /* mount point specified */
  216. mount_point = newstralloc(mount_point, uqmtpt);
  217. }
  218. /* set the working directory to the mount point */
  219. /* there is the possibility that there are no index records for the
  220. disk for the given date, hence setting the directory to the
  221. mount point will fail. Preempt this by checking first so we can write
  222. a more informative message. */
  223. if (exchange("OISD /") == -1)
  224. exit(1);
  225. if (server_happy())
  226. {
  227. disk_path = newstralloc(disk_path, "/");
  228. suck_dir_list_from_server(); /* get list of directory contents */
  229. }
  230. else
  231. {
  232. g_printf(_("No index records for disk for specified date\n"));
  233. g_printf(_("If date correct, notify system administrator\n"));
  234. disk_path = newstralloc(disk_path, "/"); /* fake it */
  235. clear_dir_list();
  236. }
  237. amfree(uqmtpt);
  238. amfree(uqdsk);
  239. }
  240. void
  241. list_disk(
  242. char * amdevice)
  243. {
  244. char *cmd = NULL;
  245. char *qamdevice, *uqamdevice;
  246. if(amdevice) {
  247. uqamdevice = unquote_string(amdevice);
  248. qamdevice = quote_string(uqamdevice);
  249. cmd = stralloc2("LISTDISK ", qamdevice);
  250. amfree(uqamdevice);
  251. amfree(qamdevice);
  252. if (converse(cmd) == -1)
  253. exit(1);
  254. amfree(cmd);
  255. }
  256. else {
  257. cmd = stralloc("LISTDISK");
  258. if (converse(cmd) == -1)
  259. exit(1);
  260. amfree(cmd);
  261. }
  262. }
  263. void
  264. local_cd(
  265. char *dir)
  266. {
  267. char *uqdir = unquote_string(dir);
  268. if (chdir(uqdir) == -1) {
  269. perror(uqdir);
  270. }
  271. amfree(uqdir);
  272. }
  273. void
  274. cd_glob(
  275. char * glob)
  276. {
  277. char *regex;
  278. char *regex_path;
  279. char *s;
  280. char *uqglob;
  281. char *path_on_disk = NULL;
  282. if (disk_name == NULL) {
  283. g_printf(_("Must select disk before changing directory\n"));
  284. return;
  285. }
  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. g_printf(_("\"%s\" is not a valid shell wildcard pattern: "), glob);
  291. puts(s);
  292. amfree(regex);
  293. return;
  294. }
  295. /*
  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
  300. */
  301. regex_path = stralloc(regex + 1);
  302. amfree(regex);
  303. if(regex_path[strlen(regex_path) - 2] != '/' ) {
  304. regex_path[strlen(regex_path) - 1] = '\0';
  305. strappend(regex_path, "/$");
  306. }
  307. /* convert path (assumed in cwd) to one on disk */
  308. if (strcmp(disk_path, "/") == 0)
  309. path_on_disk = stralloc2("/", regex_path);
  310. else {
  311. char *clean_disk_path = clean_regex(disk_path);
  312. path_on_disk = vstralloc(clean_disk_path, "/", regex_path, NULL);
  313. amfree(clean_disk_path);
  314. }
  315. cd_dir(path_on_disk, uqglob);
  316. amfree(regex_path);
  317. amfree(path_on_disk);
  318. amfree(uqglob);
  319. }
  320. void
  321. cd_regex(
  322. char * regex)
  323. {
  324. char *s;
  325. char *uq_orig_regex;
  326. char *uqregex;
  327. int len_uqregex;
  328. char *path_on_disk = NULL;
  329. if (disk_name == NULL) {
  330. g_printf(_("Must select disk before changing directory\n"));
  331. return;
  332. }
  333. uq_orig_regex = unquote_string(regex);
  334. uqregex = stralloc(uq_orig_regex);
  335. /* Add a terminating '/' if it is not there, maybe before a '$' */
  336. len_uqregex = strlen(uqregex);
  337. if (uqregex[len_uqregex-1] == '$') {
  338. if (uqregex[len_uqregex-2] != '/') {
  339. uqregex[len_uqregex-1] = '\0';
  340. strappend(uqregex, "/$");
  341. }
  342. } else if (uqregex[len_uqregex-1] != '/') {
  343. //uqregex[len_uqregex-1] = '\0';
  344. strappend(uqregex, "/");
  345. }
  346. if ((s = validate_regexp(uqregex)) != NULL) {
  347. g_printf(_("\"%s\" is not a valid regular expression: "), uq_orig_regex);
  348. amfree(uqregex);
  349. puts(s);
  350. return;
  351. }
  352. /* convert path (assumed in cwd) to one on disk */
  353. if (strcmp(disk_path, "/") == 0)
  354. path_on_disk = stralloc2("/", uqregex);
  355. else {
  356. char *clean_disk_path = clean_regex(disk_path);
  357. path_on_disk = vstralloc(clean_disk_path, "/", regex, NULL);
  358. amfree(clean_disk_path);
  359. }
  360. cd_dir(path_on_disk, uq_orig_regex);
  361. amfree(path_on_disk);
  362. amfree(uqregex);
  363. amfree(uq_orig_regex);
  364. }
  365. void
  366. cd_dir(
  367. char * path_on_disk,
  368. char * default_dir)
  369. {
  370. char *dir = NULL;
  371. char *s;
  372. int nb_found;
  373. size_t i;
  374. DIR_ITEM *ditem;
  375. if ((s = validate_regexp(path_on_disk)) != NULL) {
  376. set_directory(default_dir);
  377. return;
  378. }
  379. nb_found = 0;
  380. for (ditem=get_dir_list(); ditem!=NULL && nb_found <= 1;
  381. ditem=get_next_dir_item(ditem))
  382. {
  383. if (match(path_on_disk, ditem->path))
  384. {
  385. i = strlen(ditem->path);
  386. if((i > 0 && ditem->path[i-1] == '/')
  387. || (i > 1 && ditem->path[i-2] == '/' && ditem->path[i-1] == '.'))
  388. { /* It is a directory */
  389. char *dir1, *dir2;
  390. nb_found++;
  391. dir = newstralloc(dir,ditem->path);
  392. if(dir[strlen(dir)-1] == '/')
  393. dir[strlen(dir)-1] = '\0'; /* remove last / */
  394. /* remove everything before the last / */
  395. dir1 = rindex(dir,'/');
  396. if (dir1) {
  397. dir1++;
  398. dir2 = stralloc(dir1);
  399. amfree(dir);
  400. dir = dir2;
  401. }
  402. }
  403. }
  404. }
  405. if(nb_found==0) {
  406. set_directory(default_dir);
  407. }
  408. else if(nb_found==1) {
  409. set_directory(dir);
  410. }
  411. else {
  412. g_printf(_("Too many directories\n"));
  413. }
  414. amfree(dir);
  415. }
  416. void
  417. set_directory(
  418. char * dir)
  419. {
  420. char *cmd = NULL;
  421. char *new_dir = NULL;
  422. char *qnew_dir;
  423. char *dp, *de;
  424. char *ldir = NULL;
  425. /* do nothing if "." */
  426. if(strcmp(dir,".")==0) {
  427. show_directory(); /* say where we are */
  428. return;
  429. /*NOTREACHED*/
  430. }
  431. if (disk_name == NULL) {
  432. g_printf(_("Must select disk before setting directory\n"));
  433. return;
  434. /*NOTREACHED*/
  435. }
  436. ldir = stralloc(dir);
  437. clean_pathname(ldir);
  438. /* convert directory into absolute path relative to disk mount point */
  439. if (ldir[0] == '/')
  440. {
  441. /* absolute path specified, must start with mount point */
  442. if (strcmp(mount_point, "/") == 0)
  443. {
  444. new_dir = stralloc(ldir);
  445. }
  446. else
  447. {
  448. if (strncmp(mount_point, ldir, strlen(mount_point)) != 0)
  449. {
  450. g_printf(_("Invalid directory - Can't cd outside mount point \"%s\"\n"),
  451. mount_point);
  452. amfree(ldir);
  453. return;
  454. /*NOTREACHED*/
  455. }
  456. new_dir = stralloc(ldir+strlen(mount_point));
  457. if (strlen(new_dir) == 0) {
  458. new_dir = newstralloc(new_dir, "/");
  459. /* i.e. ldir == mount_point */
  460. }
  461. }
  462. }
  463. else
  464. {
  465. new_dir = stralloc(disk_path);
  466. dp = ldir;
  467. /* strip any leading ..s */
  468. while (strncmp(dp, "../", 3) == 0)
  469. {
  470. de = strrchr(new_dir, '/'); /* always at least 1 */
  471. if (de == new_dir)
  472. {
  473. /* at top of disk */
  474. *(de + 1) = '\0';
  475. dp = dp + 3;
  476. }
  477. else
  478. {
  479. *de = '\0';
  480. dp = dp + 3;
  481. }
  482. }
  483. if (strcmp(dp, "..") == 0) {
  484. if (strcmp(new_dir, "/") == 0) {
  485. /* at top of disk */
  486. g_printf(_("Invalid directory - Can't cd outside mount point \"%s\"\n"),
  487. mount_point);
  488. /*@ignore@*/
  489. amfree(new_dir);
  490. /*@end@*/
  491. amfree(ldir);
  492. return;
  493. /*NOTREACHED*/
  494. }
  495. de = strrchr(new_dir, '/'); /* always at least 1 */
  496. if (de == new_dir)
  497. {
  498. /* at top of disk */
  499. *(de+1) = '\0';
  500. }
  501. else
  502. {
  503. *de = '\0';
  504. }
  505. } else {
  506. /*@ignore@*/
  507. if (strcmp(new_dir, "/") != 0) {
  508. strappend(new_dir, "/");
  509. }
  510. strappend(new_dir, ldir);
  511. /*@end@*/
  512. }
  513. }
  514. qnew_dir = quote_string(new_dir);
  515. cmd = stralloc2("OISD ", qnew_dir);
  516. amfree(qnew_dir);
  517. if (exchange(cmd) == -1) {
  518. exit(1);
  519. /*NOTREACHED*/
  520. }
  521. amfree(cmd);
  522. if (server_happy())
  523. {
  524. disk_path = newstralloc(disk_path, new_dir);
  525. suck_dir_list_from_server(); /* get list of directory contents */
  526. show_directory(); /* say where we moved to */
  527. }
  528. else
  529. {
  530. g_printf(_("Invalid directory - %s\n"), dir);
  531. }
  532. /*@ignore@*/
  533. amfree(new_dir);
  534. amfree(ldir);
  535. /*@end@*/
  536. }
  537. /* prints the current working directory */
  538. void
  539. show_directory(void)
  540. {
  541. if (mount_point == NULL || disk_path == NULL)
  542. g_printf(_("Must select disk first\n"));
  543. else if (strcmp(mount_point, "/") == 0)
  544. g_printf("%s\n", disk_path);
  545. else if (strcmp(disk_path, "/") == 0)
  546. g_printf("%s\n", mount_point);
  547. else
  548. g_printf("%s%s\n", mount_point, disk_path);
  549. }
  550. /* set the tape server and device (deprecated version) */
  551. void
  552. set_tape(
  553. char * tape)
  554. {
  555. char *uqtape = unquote_string(tape);
  556. char *tapedev = strchr(uqtape, ':');
  557. char *host = NULL;
  558. g_printf(_("NOTE: 'settape' is deprecated; use setdevice instead.\n"));
  559. if (tapedev)
  560. {
  561. /* This command is deprecated because this parsing is going to fall
  562. * behind the list of available device names at some point, or to shadow
  563. * an interesting hostname (wouldn't 'tape' be a good name for a
  564. * tape server?) */
  565. if (tapedev != uqtape) {
  566. if((strchr(tapedev+1, ':') == NULL) &&
  567. (strncmp_const(uqtape, "null:") == 0 ||
  568. strncmp_const(uqtape, "rait:") == 0 ||
  569. strncmp_const(uqtape, "file:") == 0 ||
  570. strncmp_const(uqtape, "s3:") == 0 ||
  571. strncmp_const(uqtape, "tape:") == 0)) {
  572. tapedev = uqtape;
  573. }
  574. else {
  575. *tapedev = '\0';
  576. host = stralloc(uqtape);
  577. ++tapedev;
  578. }
  579. } else {
  580. ++tapedev;
  581. }
  582. } else
  583. tapedev = uqtape;
  584. if (tapedev[0])
  585. {
  586. if (strcmp(tapedev, "default") == 0)
  587. tapedev = NULL;
  588. }
  589. /* call out to the new version */
  590. set_device(host, tapedev);
  591. amfree(host);
  592. amfree(uqtape);
  593. }
  594. /* set the tape server and device, for real */
  595. void
  596. set_device(
  597. char * host,
  598. char * device)
  599. {
  600. if (host)
  601. tape_server_name = newstralloc(tape_server_name, host);
  602. else
  603. amfree(tape_server_name);
  604. if (device)
  605. tape_device_name = newstralloc(tape_device_name, device);
  606. else
  607. amfree(tape_device_name);
  608. /* print the current status */
  609. if (tape_device_name)
  610. g_printf (_("Using tape \"%s\""), tape_device_name);
  611. else
  612. g_printf (_("Using default tape"));
  613. if (tape_server_name)
  614. g_printf (_(" from server %s.\n"), tape_server_name);
  615. else
  616. g_printf (_(".\nTape server unspecified, assumed to be %s.\n"),
  617. server_name);
  618. }
  619. void
  620. set_mode(
  621. int mode)
  622. {
  623. #ifdef SAMBA_CLIENT
  624. if (mode == SAMBA_SMBCLIENT) {
  625. g_printf (_("SAMBA dumps will be extracted using smbclient\n"));
  626. samba_extract_method = SAMBA_SMBCLIENT;
  627. } else {
  628. if (mode == SAMBA_TAR) {
  629. g_printf (_("SAMBA dumps will be extracted as TAR dumps\n"));
  630. samba_extract_method = SAMBA_TAR;
  631. }
  632. }
  633. #else
  634. (void)mode; /* Quiet unused parameter warning */
  635. #endif /* SAMBA_CLIENT */
  636. }
  637. void
  638. show_mode(void)
  639. {
  640. #ifdef SAMBA_CLIENT
  641. g_printf (_("SAMBA dumps are extracted "));
  642. if (samba_extract_method == SAMBA_TAR) {
  643. g_printf (_(" as TAR dumps\n"));
  644. } else {
  645. g_printf (_("using smbclient\n"));
  646. }
  647. #endif /* SAMBA_CLIENT */
  648. }