PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
C | 743 lines | 569 code | 77 blank | 97 comment | 171 complexity | bc425eab0cf67be019bfaffa15ace240 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. #include "amxml.h"
  35. extern unsigned short samba_extract_method;
  36. /* sets a date, mapping given date into standard form if needed */
  37. int
  38. set_date(
  39. char * date)
  40. {
  41. char *cmd = NULL;
  42. char *qdisk_path;
  43. clear_dir_list();
  44. cmd = stralloc2("DATE ", date);
  45. if (converse(cmd) == -1)
  46. exit(1);
  47. /* if a host/disk/directory is set, then check if that directory
  48. is still valid at the new date, and if not set directory to
  49. mount_point */
  50. if (disk_path != NULL) {
  51. qdisk_path = quote_string(disk_path);
  52. cmd = newstralloc2(cmd, "OISD ", qdisk_path);
  53. amfree(qdisk_path);
  54. if (exchange(cmd) == -1)
  55. exit(1);
  56. if (server_happy())
  57. {
  58. suck_dir_list_from_server();
  59. }
  60. else
  61. {
  62. g_printf(_("No index records for cwd on new date\n"));
  63. g_printf(_("Setting cwd to mount point\n"));
  64. disk_path = newstralloc(disk_path, "/"); /* fake it */
  65. clear_dir_list();
  66. }
  67. }
  68. amfree(cmd);
  69. return 0;
  70. }
  71. void
  72. set_host(
  73. const char *host)
  74. {
  75. char *cmd = NULL;
  76. struct hostent *hp = NULL;
  77. char **hostp;
  78. int found_host = 0;
  79. char *uqhost = unquote_string(host);
  80. if (is_extract_list_nonempty())
  81. {
  82. g_printf(_("Must clear extract list before changing host\n"));
  83. amfree(uqhost);
  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. amfree(uqmtpt);
  198. amfree(uqdsk);
  199. return;
  200. }
  201. disk_name = newstralloc(disk_name, uqdsk);
  202. if (mtpt == NULL)
  203. {
  204. /* mount point not specified */
  205. if (*uqdsk == '/')
  206. {
  207. /* disk specified by mount point, hence use it */
  208. mount_point = newstralloc(mount_point, uqdsk);
  209. }
  210. else
  211. {
  212. /* device name given, use '/' because nothing better */
  213. mount_point = newstralloc(mount_point, "/");
  214. }
  215. }
  216. else
  217. {
  218. /* mount point specified */
  219. mount_point = newstralloc(mount_point, uqmtpt);
  220. }
  221. /* set the working directory to the mount point */
  222. /* there is the possibility that there are no index records for the
  223. disk for the given date, hence setting the directory to the
  224. mount point will fail. Preempt this by checking first so we can write
  225. a more informative message. */
  226. if (exchange("OISD /") == -1)
  227. exit(1);
  228. if (server_happy())
  229. {
  230. disk_path = newstralloc(disk_path, "/");
  231. suck_dir_list_from_server(); /* get list of directory contents */
  232. }
  233. else
  234. {
  235. g_printf(_("No index records for disk for specified date\n"));
  236. g_printf(_("If date correct, notify system administrator\n"));
  237. disk_path = newstralloc(disk_path, "/"); /* fake it */
  238. clear_dir_list();
  239. }
  240. amfree(uqmtpt);
  241. amfree(uqdsk);
  242. if (am_has_feature(indexsrv_features, fe_amindexd_DLE)) {
  243. char *dle_str;
  244. char *errmsg = NULL;
  245. cmd = stralloc("DLE");
  246. if (exchange(cmd) == -1)
  247. exit(1);
  248. amfree(cmd);
  249. if (!server_happy())
  250. return;
  251. dle_str = reply_line();
  252. if (BSTRNCMP(dle_str+4, "NODLE") == 0) {
  253. dump_dle = NULL;
  254. } else {
  255. dle_str = unquote_string(dle_str+4);
  256. dump_dle = amxml_parse_node_CHAR(dle_str, &errmsg);
  257. }
  258. }
  259. }
  260. void
  261. list_disk(
  262. char * amdevice)
  263. {
  264. char *cmd = NULL;
  265. char *qamdevice, *uqamdevice;
  266. if(amdevice) {
  267. uqamdevice = unquote_string(amdevice);
  268. qamdevice = quote_string(uqamdevice);
  269. cmd = stralloc2("LISTDISK ", qamdevice);
  270. amfree(uqamdevice);
  271. amfree(qamdevice);
  272. if (converse(cmd) == -1)
  273. exit(1);
  274. amfree(cmd);
  275. }
  276. else {
  277. cmd = stralloc("LISTDISK");
  278. if (converse(cmd) == -1)
  279. exit(1);
  280. amfree(cmd);
  281. }
  282. }
  283. void
  284. local_cd(
  285. char *dir)
  286. {
  287. char *uqdir = unquote_string(dir);
  288. if (chdir(uqdir) == -1) {
  289. perror(uqdir);
  290. }
  291. amfree(uqdir);
  292. }
  293. void
  294. cd_glob(
  295. char * glob)
  296. {
  297. char *regex;
  298. char *regex_path;
  299. char *s;
  300. char *uqglob;
  301. char *path_on_disk = NULL;
  302. if (disk_name == NULL) {
  303. g_printf(_("Must select disk before changing directory\n"));
  304. return;
  305. }
  306. uqglob = unquote_string(glob);
  307. regex = glob_to_regex(uqglob);
  308. dbprintf(_("cd_glob (%s) -> %s\n"), uqglob, regex);
  309. if ((s = validate_regexp(regex)) != NULL) {
  310. g_printf(_("\"%s\" is not a valid shell wildcard pattern: "), glob);
  311. puts(s);
  312. amfree(regex);
  313. amfree(uqglob);
  314. return;
  315. }
  316. /*
  317. * glob_to_regex() anchors the beginning of the pattern with ^,
  318. * but we will be tacking it onto the end of the current directory
  319. * in add_file, so strip that off. Also, it anchors the end with
  320. * $, but we need to match a trailing /, add it if it is not there
  321. */
  322. regex_path = stralloc(regex + 1);
  323. amfree(regex);
  324. if(regex_path[strlen(regex_path) - 2] != '/' ) {
  325. regex_path[strlen(regex_path) - 1] = '\0';
  326. strappend(regex_path, "/$");
  327. }
  328. /* convert path (assumed in cwd) to one on disk */
  329. if (strcmp(disk_path, "/") == 0)
  330. path_on_disk = stralloc2("/", regex_path);
  331. else {
  332. char *clean_disk_path = clean_regex(disk_path);
  333. path_on_disk = vstralloc(clean_disk_path, "/", regex_path, NULL);
  334. amfree(clean_disk_path);
  335. }
  336. cd_dir(path_on_disk, uqglob);
  337. amfree(regex_path);
  338. amfree(path_on_disk);
  339. amfree(uqglob);
  340. }
  341. void
  342. cd_regex(
  343. char * regex)
  344. {
  345. char *s;
  346. char *uq_orig_regex;
  347. char *uqregex;
  348. int len_uqregex;
  349. char *path_on_disk = NULL;
  350. if (disk_name == NULL) {
  351. g_printf(_("Must select disk before changing directory\n"));
  352. return;
  353. }
  354. uq_orig_regex = unquote_string(regex);
  355. uqregex = stralloc(uq_orig_regex);
  356. /* Add a terminating '/' if it is not there, maybe before a '$' */
  357. len_uqregex = strlen(uqregex);
  358. if (uqregex[len_uqregex-1] == '$') {
  359. if (uqregex[len_uqregex-2] != '/') {
  360. uqregex[len_uqregex-1] = '\0';
  361. strappend(uqregex, "/$");
  362. }
  363. } else if (uqregex[len_uqregex-1] != '/') {
  364. //uqregex[len_uqregex-1] = '\0';
  365. strappend(uqregex, "/");
  366. }
  367. if ((s = validate_regexp(uqregex)) != NULL) {
  368. g_printf(_("\"%s\" is not a valid regular expression: "), uq_orig_regex);
  369. amfree(uqregex);
  370. amfree(uq_orig_regex);
  371. puts(s);
  372. return;
  373. }
  374. /* convert path (assumed in cwd) to one on disk */
  375. if (strcmp(disk_path, "/") == 0)
  376. path_on_disk = stralloc2("/", uqregex);
  377. else {
  378. char *clean_disk_path = clean_regex(disk_path);
  379. path_on_disk = vstralloc(clean_disk_path, "/", regex, NULL);
  380. amfree(clean_disk_path);
  381. }
  382. cd_dir(path_on_disk, uq_orig_regex);
  383. amfree(path_on_disk);
  384. amfree(uqregex);
  385. amfree(uq_orig_regex);
  386. }
  387. void
  388. cd_dir(
  389. char * path_on_disk,
  390. char * default_dir)
  391. {
  392. char *dir = NULL;
  393. char *s;
  394. int nb_found;
  395. size_t i;
  396. DIR_ITEM *ditem;
  397. if ((s = validate_regexp(path_on_disk)) != NULL) {
  398. set_directory(default_dir);
  399. return;
  400. }
  401. nb_found = 0;
  402. for (ditem=get_dir_list(); ditem!=NULL && nb_found <= 1;
  403. ditem=get_next_dir_item(ditem))
  404. {
  405. if (match(path_on_disk, ditem->path))
  406. {
  407. i = strlen(ditem->path);
  408. if((i > 0 && ditem->path[i-1] == '/')
  409. || (i > 1 && ditem->path[i-2] == '/' && ditem->path[i-1] == '.'))
  410. { /* It is a directory */
  411. char *dir1, *dir2;
  412. nb_found++;
  413. dir = newstralloc(dir,ditem->path);
  414. if(dir[strlen(dir)-1] == '/')
  415. dir[strlen(dir)-1] = '\0'; /* remove last / */
  416. /* remove everything before the last / */
  417. dir1 = strrchr(dir,'/');
  418. if (dir1) {
  419. dir1++;
  420. dir2 = stralloc(dir1);
  421. amfree(dir);
  422. dir = dir2;
  423. }
  424. }
  425. }
  426. }
  427. if(nb_found==0) {
  428. set_directory(default_dir);
  429. }
  430. else if(nb_found==1) {
  431. set_directory(dir);
  432. }
  433. else {
  434. g_printf(_("Too many directories\n"));
  435. }
  436. amfree(dir);
  437. }
  438. void
  439. set_directory(
  440. char * dir)
  441. {
  442. char *cmd = NULL;
  443. char *new_dir = NULL;
  444. char *qnew_dir;
  445. char *dp, *de;
  446. char *ldir = NULL;
  447. /* do nothing if "." */
  448. if(strcmp(dir,".")==0) {
  449. show_directory(); /* say where we are */
  450. return;
  451. /*NOTREACHED*/
  452. }
  453. if (disk_name == NULL) {
  454. g_printf(_("Must select disk before setting directory\n"));
  455. return;
  456. /*NOTREACHED*/
  457. }
  458. ldir = stralloc(dir);
  459. clean_pathname(ldir);
  460. /* convert directory into absolute path relative to disk mount point */
  461. if (ldir[0] == '/')
  462. {
  463. /* absolute path specified, must start with mount point */
  464. if (strcmp(mount_point, "/") == 0)
  465. {
  466. new_dir = stralloc(ldir);
  467. }
  468. else
  469. {
  470. if (strncmp(mount_point, ldir, strlen(mount_point)) != 0)
  471. {
  472. g_printf(_("Invalid directory - Can't cd outside mount point \"%s\"\n"),
  473. mount_point);
  474. amfree(ldir);
  475. return;
  476. /*NOTREACHED*/
  477. }
  478. new_dir = stralloc(ldir+strlen(mount_point));
  479. if (strlen(new_dir) == 0) {
  480. new_dir = newstralloc(new_dir, "/");
  481. /* i.e. ldir == mount_point */
  482. }
  483. }
  484. }
  485. else
  486. {
  487. new_dir = stralloc(disk_path);
  488. dp = ldir;
  489. /* strip any leading ..s */
  490. while (strncmp(dp, "../", 3) == 0)
  491. {
  492. de = strrchr(new_dir, '/'); /* always at least 1 */
  493. if (de == new_dir)
  494. {
  495. /* at top of disk */
  496. *(de + 1) = '\0';
  497. dp = dp + 3;
  498. }
  499. else
  500. {
  501. *de = '\0';
  502. dp = dp + 3;
  503. }
  504. }
  505. if (strcmp(dp, "..") == 0) {
  506. if (strcmp(new_dir, "/") == 0) {
  507. /* at top of disk */
  508. g_printf(_("Invalid directory - Can't cd outside mount point \"%s\"\n"),
  509. mount_point);
  510. /*@ignore@*/
  511. amfree(new_dir);
  512. /*@end@*/
  513. amfree(ldir);
  514. return;
  515. /*NOTREACHED*/
  516. }
  517. de = strrchr(new_dir, '/'); /* always at least 1 */
  518. if (de == new_dir)
  519. {
  520. /* at top of disk */
  521. *(de+1) = '\0';
  522. }
  523. else
  524. {
  525. *de = '\0';
  526. }
  527. } else {
  528. /*@ignore@*/
  529. if (strcmp(new_dir, "/") != 0) {
  530. strappend(new_dir, "/");
  531. }
  532. strappend(new_dir, ldir);
  533. /*@end@*/
  534. }
  535. }
  536. qnew_dir = quote_string(new_dir);
  537. cmd = stralloc2("OISD ", qnew_dir);
  538. amfree(qnew_dir);
  539. if (exchange(cmd) == -1) {
  540. exit(1);
  541. /*NOTREACHED*/
  542. }
  543. amfree(cmd);
  544. if (server_happy())
  545. {
  546. disk_path = newstralloc(disk_path, new_dir);
  547. suck_dir_list_from_server(); /* get list of directory contents */
  548. show_directory(); /* say where we moved to */
  549. }
  550. else
  551. {
  552. g_printf(_("Invalid directory - %s\n"), dir);
  553. }
  554. /*@ignore@*/
  555. amfree(new_dir);
  556. amfree(ldir);
  557. /*@end@*/
  558. }
  559. /* prints the current working directory */
  560. void
  561. show_directory(void)
  562. {
  563. if (mount_point == NULL || disk_path == NULL)
  564. g_printf(_("Must select disk first\n"));
  565. else if (strcmp(mount_point, "/") == 0)
  566. g_printf("%s\n", disk_path);
  567. else if (strcmp(disk_path, "/") == 0)
  568. g_printf("%s\n", mount_point);
  569. else
  570. g_printf("%s%s\n", mount_point, disk_path);
  571. }
  572. /* set the tape server and device (deprecated version) */
  573. void
  574. set_tape(
  575. char * tape)
  576. {
  577. char *uqtape = unquote_string(tape);
  578. char *tapedev = strchr(uqtape, ':');
  579. char *host = NULL;
  580. g_printf(_("NOTE: 'settape' is deprecated; use setdevice instead.\n"));
  581. if (tapedev)
  582. {
  583. /* This command is deprecated because this parsing is going to fall
  584. * behind the list of available device names at some point, or to shadow
  585. * an interesting hostname (wouldn't 'tape' be a good name for a
  586. * tape server?) */
  587. if (tapedev != uqtape) {
  588. if((strchr(tapedev+1, ':') == NULL) &&
  589. (strncmp_const(uqtape, "null:") == 0 ||
  590. strncmp_const(uqtape, "rait:") == 0 ||
  591. strncmp_const(uqtape, "file:") == 0 ||
  592. strncmp_const(uqtape, "s3:") == 0 ||
  593. strncmp_const(uqtape, "tape:") == 0)) {
  594. tapedev = uqtape;
  595. }
  596. else {
  597. *tapedev = '\0';
  598. host = stralloc(uqtape);
  599. ++tapedev;
  600. }
  601. } else {
  602. ++tapedev;
  603. }
  604. } else
  605. tapedev = uqtape;
  606. if (tapedev[0])
  607. {
  608. if (strcmp(tapedev, "default") == 0)
  609. tapedev = NULL;
  610. }
  611. /* call out to the new version */
  612. set_device(host, tapedev);
  613. amfree(host);
  614. amfree(uqtape);
  615. }
  616. /* set the tape server and device, for real */
  617. void
  618. set_device(
  619. char * host,
  620. char * device)
  621. {
  622. if (host)
  623. tape_server_name = newstralloc(tape_server_name, host);
  624. else
  625. amfree(tape_server_name);
  626. if (device)
  627. tape_device_name = newstralloc(tape_device_name, device);
  628. else
  629. amfree(tape_device_name);
  630. /* print the current status */
  631. if (tape_device_name)
  632. g_printf (_("Using tape \"%s\""), tape_device_name);
  633. else
  634. g_printf (_("Using default tape"));
  635. if (tape_server_name)
  636. g_printf (_(" from server %s.\n"), tape_server_name);
  637. else
  638. g_printf (_(".\nTape server unspecified, assumed to be %s.\n"),
  639. server_name);
  640. }
  641. void
  642. set_mode(
  643. int mode)
  644. {
  645. if (mode == SAMBA_SMBCLIENT) {
  646. g_printf (_("SAMBA dumps will be extracted using smbclient\n"));
  647. samba_extract_method = SAMBA_SMBCLIENT;
  648. } else {
  649. if (mode == SAMBA_TAR) {
  650. g_printf (_("SAMBA dumps will be extracted as TAR dumps\n"));
  651. samba_extract_method = SAMBA_TAR;
  652. }
  653. }
  654. }
  655. void
  656. show_mode(void)
  657. {
  658. #ifdef SAMBA_CLIENT
  659. g_printf (_("SAMBA dumps are extracted "));
  660. if (samba_extract_method == SAMBA_TAR) {
  661. g_printf (_(" as TAR dumps\n"));
  662. } else {
  663. g_printf (_("using smbclient\n"));
  664. }
  665. #endif /* SAMBA_CLIENT */
  666. }