PageRenderTime 62ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/amanda/trunk/oldrecover-src/set_commands.c

#
C | 664 lines | 523 code | 63 blank | 78 comment | 130 complexity | 4513ac5c57fb9e2390f921bad77a86c1 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.3 2006/07/05 13:14:58 martinea Exp $
  28. *
  29. * implements the "set" commands in amrecover
  30. */
  31. #include "amanda.h"
  32. #include "match.h"
  33. #include "util.h"
  34. #include "amrecover.h"
  35. #ifdef SAMBA_CLIENT
  36. extern unsigned short samba_extract_method;
  37. #endif /* SAMBA_CLIENT */
  38. /* sets a date, mapping given date into standard form if needed */
  39. int
  40. set_date(
  41. char * date)
  42. {
  43. char *cmd = NULL;
  44. clear_dir_list();
  45. cmd = g_strconcat("DATE ", date, NULL);
  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. g_free(cmd);
  53. cmd = g_strconcat("OISD ", disk_path, NULL);
  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. g_free(disk_path);
  65. disk_path = g_strdup("/"); /* 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;
  78. char **hostp;
  79. int found_host = 0;
  80. char *uqhost;
  81. if (is_extract_list_nonempty())
  82. {
  83. g_printf(_("Must clear extract list before changing host\n"));
  84. return;
  85. }
  86. uqhost = unquote_string(host);
  87. cmd = g_strconcat("HOST ", uqhost, NULL);
  88. if (converse(cmd) == -1)
  89. exit(1);
  90. if (server_happy())
  91. {
  92. found_host = 1;
  93. }
  94. else
  95. {
  96. /*
  97. * Try converting the given host to a fully qualified name
  98. * and then try each of the aliases.
  99. */
  100. if ((hp = gethostbyname(uqhost)) != NULL) {
  101. host = hp->h_name;
  102. g_printf(_("Trying host %s ...\n"), host);
  103. g_free(cmd);
  104. cmd = g_strconcat("HOST ", host, NULL);
  105. if (converse(cmd) == -1)
  106. exit(1);
  107. if(server_happy())
  108. {
  109. found_host = 1;
  110. }
  111. else
  112. {
  113. for (hostp = hp->h_aliases; (host = *hostp) != NULL; hostp++)
  114. {
  115. g_printf(_("Trying host %s ...\n"), host);
  116. g_free(cmd);
  117. cmd = g_strconcat("HOST ", host, NULL);
  118. if (converse(cmd) == -1)
  119. exit(1);
  120. if(server_happy())
  121. {
  122. found_host = 1;
  123. break;
  124. }
  125. }
  126. }
  127. }
  128. }
  129. if(found_host)
  130. {
  131. g_free(dump_hostname);
  132. dump_hostname = g_strdup(host);
  133. amfree(disk_name);
  134. amfree(mount_point);
  135. amfree(disk_path);
  136. clear_dir_list();
  137. }
  138. amfree(cmd);
  139. amfree(uqhost);
  140. }
  141. void
  142. list_host(void)
  143. {
  144. char *cmd = NULL;
  145. cmd = g_strdup("LISTHOST");
  146. if (converse(cmd) == -1)
  147. exit(1);
  148. amfree(cmd);
  149. }
  150. void
  151. set_disk(
  152. char * dsk,
  153. char * mtpt)
  154. {
  155. char *cmd = NULL;
  156. char *uqdsk;
  157. char *uqmtpt = NULL;
  158. if (is_extract_list_nonempty())
  159. {
  160. g_printf(_("Must clear extract list before changing disk\n"));
  161. return;
  162. }
  163. /* if mount point specified, check it is valid */
  164. if (mtpt != NULL) {
  165. uqmtpt = unquote_string(mtpt);
  166. if (*mtpt != '/') {
  167. g_printf(_("Mount point \"%s\" invalid - must start with /\n"), uqmtpt);
  168. amfree(uqmtpt);
  169. return;
  170. }
  171. }
  172. clear_dir_list();
  173. uqdsk = unquote_string(dsk);
  174. cmd = g_strconcat("DISK ", uqdsk, NULL);
  175. if (converse(cmd) == -1)
  176. exit(1);
  177. amfree(cmd);
  178. if (!server_happy()) {
  179. amfree(uqmtpt);
  180. return;
  181. }
  182. g_free(disk_name);
  183. disk_name = g_strdup(uqdsk);
  184. if (mtpt == NULL)
  185. {
  186. /* mount point not specified */
  187. if (*uqdsk == '/')
  188. {
  189. /* disk specified by mount point, hence use it */
  190. g_free(mount_point);
  191. mount_point = g_strdup(uqdsk);
  192. }
  193. else
  194. {
  195. /* device name given, use '/' because nothing better */
  196. g_free(mount_point);
  197. mount_point = g_strdup("/");
  198. }
  199. }
  200. else
  201. {
  202. /* mount point specified */
  203. g_free(mount_point);
  204. mount_point = g_strdup(uqmtpt);
  205. }
  206. /* set the working directory to the mount point */
  207. /* there is the possibility that there are no index records for the
  208. disk for the given date, hence setting the directory to the
  209. mount point will fail. Preempt this by checking first so we can write
  210. a more informative message. */
  211. if (exchange("OISD /") == -1)
  212. exit(1);
  213. if (server_happy())
  214. {
  215. g_free(disk_path);
  216. disk_path = g_strdup("/");
  217. suck_dir_list_from_server(); /* get list of directory contents */
  218. }
  219. else
  220. {
  221. g_printf(_("No index records for disk for specified date\n"));
  222. g_printf(_("If date correct, notify system administrator\n"));
  223. g_free(disk_path);
  224. disk_path = g_strdup("/"); /* fake it */
  225. clear_dir_list();
  226. }
  227. amfree(uqmtpt);
  228. amfree(uqdsk);
  229. }
  230. void
  231. list_disk(
  232. char * amdevice)
  233. {
  234. char *cmd = NULL;
  235. char *uqamdevice;
  236. if(amdevice) {
  237. uqamdevice = unquote_string(amdevice);
  238. cmd = g_strconcat("LISTDISK ", uqamdevice, NULL);
  239. amfree(uqamdevice);
  240. if (converse(cmd) == -1)
  241. exit(1);
  242. amfree(cmd);
  243. }
  244. else {
  245. cmd = g_strdup("LISTDISK");
  246. if (converse(cmd) == -1)
  247. exit(1);
  248. amfree(cmd);
  249. }
  250. }
  251. void
  252. local_cd(
  253. char *dir)
  254. {
  255. char *uqdir = unquote_string(dir);
  256. if (chdir(uqdir) == -1) {
  257. perror(uqdir);
  258. }
  259. amfree(uqdir);
  260. }
  261. void
  262. cd_glob(
  263. char * glob)
  264. {
  265. char *regex;
  266. char *regex_path;
  267. char *s;
  268. char *uqglob;
  269. char *path_on_disk = NULL;
  270. if (disk_name == NULL) {
  271. g_printf(_("Must select disk before changing directory\n"));
  272. return;
  273. }
  274. uqglob = unquote_string(glob);
  275. regex = glob_to_regex(uqglob);
  276. dbprintf(_("cd_glob (%s) -> %s\n"), uqglob, regex);
  277. if ((s = validate_regexp(regex)) != NULL) {
  278. g_printf(_("\"%s\" is not a valid shell wildcard pattern: "), glob);
  279. puts(s);
  280. amfree(regex);
  281. amfree(uqglob);
  282. return;
  283. }
  284. /*
  285. * glob_to_regex() anchors the beginning of the pattern with ^,
  286. * but we will be tacking it onto the end of the current directory
  287. * in add_file, so strip that off. Also, it anchors the end with
  288. * $, but we need to match a trailing /, add it if it is not there
  289. */
  290. regex_path = g_strdup(regex + 1);
  291. amfree(regex);
  292. if(regex_path[strlen(regex_path) - 2] != '/' ) {
  293. regex_path[strlen(regex_path) - 1] = '\0';
  294. strappend(regex_path, "/$");
  295. }
  296. /* convert path (assumed in cwd) to one on disk */
  297. if (g_str_equal(disk_path, "/"))
  298. path_on_disk = g_strconcat("/", regex_path, NULL);
  299. else {
  300. char *clean_disk_path = clean_regex(disk_path, 0);
  301. path_on_disk = g_strjoin(NULL, clean_disk_path, "/", regex_path, NULL);
  302. amfree(clean_disk_path);
  303. }
  304. cd_dir(path_on_disk, uqglob);
  305. amfree(regex_path);
  306. amfree(path_on_disk);
  307. amfree(uqglob);
  308. }
  309. void
  310. cd_regex(
  311. char * regex)
  312. {
  313. char *s;
  314. char *uqregex;
  315. char *path_on_disk = NULL;
  316. if (disk_name == NULL) {
  317. g_printf(_("Must select disk before changing directory\n"));
  318. return;
  319. }
  320. uqregex = unquote_string(regex);
  321. if ((s = validate_regexp(uqregex)) != NULL) {
  322. g_printf(_("\"%s\" is not a valid regular expression: "), uqregex);
  323. amfree(uqregex);
  324. puts(s);
  325. return;
  326. }
  327. /* convert path (assumed in cwd) to one on disk */
  328. if (g_str_equal(disk_path, "/"))
  329. path_on_disk = g_strconcat("/", regex, NULL);
  330. else {
  331. char *clean_disk_path = clean_regex(disk_path, 0);
  332. path_on_disk = g_strjoin(NULL, clean_disk_path, "/", regex, NULL);
  333. amfree(clean_disk_path);
  334. }
  335. cd_dir(path_on_disk, uqregex);
  336. amfree(path_on_disk);
  337. amfree(uqregex);
  338. }
  339. void
  340. cd_dir(
  341. char * path_on_disk,
  342. char * default_dir)
  343. {
  344. char *path_on_disk_slash = NULL;
  345. char *dir = NULL;
  346. int nb_found;
  347. size_t i;
  348. DIR_ITEM *ditem;
  349. path_on_disk_slash = g_strconcat(path_on_disk, "/", NULL);
  350. nb_found = 0;
  351. for (ditem=get_dir_list(); ditem!=NULL && nb_found <= 1;
  352. ditem=get_next_dir_item(ditem))
  353. {
  354. if (match(path_on_disk, ditem->path)
  355. || match(path_on_disk_slash, ditem->path))
  356. {
  357. i = strlen(ditem->path);
  358. if((i > 0 && ditem->path[i-1] == '/')
  359. || (i > 1 && ditem->path[i-2] == '/' && ditem->path[i-1] == '.'))
  360. { /* It is a directory */
  361. char *dir1, *dir2;
  362. nb_found++;
  363. g_free(dir);
  364. dir = g_strdup(ditem->path);
  365. if(dir[strlen(dir)-1] == '/')
  366. dir[strlen(dir)-1] = '\0'; /* remove last / */
  367. /* remove everything before the last / */
  368. dir1 = strrchr(dir,'/');
  369. if (dir1) {
  370. dir1++;
  371. dir2 = g_strdup(dir1);
  372. amfree(dir);
  373. dir = dir2;
  374. }
  375. }
  376. }
  377. }
  378. amfree(path_on_disk_slash);
  379. if(nb_found==0) {
  380. set_directory(default_dir);
  381. }
  382. else if(nb_found==1) {
  383. set_directory(dir);
  384. }
  385. else {
  386. g_printf(_("Too many directory\n"));
  387. }
  388. amfree(dir);
  389. }
  390. void
  391. set_directory(
  392. char * dir)
  393. {
  394. char *cmd = NULL;
  395. char *new_dir = NULL;
  396. char *dp, *de;
  397. char *ldir = NULL;
  398. /* do nothing if "." */
  399. if(g_str_equal(dir, ".")) {
  400. show_directory(); /* say where we are */
  401. return;
  402. /*NOTREACHED*/
  403. }
  404. if (disk_name == NULL) {
  405. g_printf(_("Must select disk before setting directory\n"));
  406. return;
  407. /*NOTREACHED*/
  408. }
  409. ldir = g_strdup(dir);
  410. clean_pathname(ldir);
  411. /* convert directory into absolute path relative to disk mount point */
  412. if (ldir[0] == '/')
  413. {
  414. /* absolute path specified, must start with mount point */
  415. if (g_str_equal(mount_point, "/"))
  416. {
  417. new_dir = g_strdup(ldir);
  418. }
  419. else
  420. {
  421. if (strncmp(mount_point, ldir, strlen(mount_point)) != 0)
  422. {
  423. g_printf(_("Invalid directory - Can't cd outside mount point \"%s\"\n"),
  424. mount_point);
  425. amfree(ldir);
  426. return;
  427. /*NOTREACHED*/
  428. }
  429. new_dir = g_strdup(ldir+strlen(mount_point));
  430. if (strlen(new_dir) == 0) {
  431. g_free(new_dir);
  432. new_dir = g_strdup("/");
  433. /* i.e. ldir == mount_point */
  434. }
  435. }
  436. }
  437. else
  438. {
  439. new_dir = g_strdup(disk_path);
  440. dp = ldir;
  441. /* strip any leading ..s */
  442. while (g_str_has_prefix(dp, "../"))
  443. {
  444. de = strrchr(new_dir, '/'); /* always at least 1 */
  445. if (de == new_dir)
  446. {
  447. /* at top of disk */
  448. *(de + 1) = '\0';
  449. dp = dp + 3;
  450. }
  451. else
  452. {
  453. *de = '\0';
  454. dp = dp + 3;
  455. }
  456. }
  457. if (g_str_equal(dp, "..")) {
  458. if (g_str_equal(new_dir, "/")) {
  459. /* at top of disk */
  460. g_printf(_("Invalid directory - Can't cd outside mount point \"%s\"\n"),
  461. mount_point);
  462. /*@ignore@*/
  463. amfree(new_dir);
  464. /*@end@*/
  465. amfree(ldir);
  466. return;
  467. /*NOTREACHED*/
  468. }
  469. de = strrchr(new_dir, '/'); /* always at least 1 */
  470. if (de == new_dir)
  471. {
  472. /* at top of disk */
  473. *(de+1) = '\0';
  474. }
  475. else
  476. {
  477. *de = '\0';
  478. }
  479. } else {
  480. /*@ignore@*/
  481. if (!g_str_equal(new_dir, "/")) {
  482. strappend(new_dir, "/");
  483. }
  484. strappend(new_dir, ldir);
  485. /*@end@*/
  486. }
  487. }
  488. cmd = g_strconcat("OISD ", new_dir, NULL);
  489. if (exchange(cmd) == -1) {
  490. exit(1);
  491. /*NOTREACHED*/
  492. }
  493. amfree(cmd);
  494. if (server_happy())
  495. {
  496. g_free(disk_path);
  497. disk_path = g_strdup(new_dir);
  498. suck_dir_list_from_server(); /* get list of directory contents */
  499. show_directory(); /* say where we moved to */
  500. }
  501. else
  502. {
  503. g_printf(_("Invalid directory - %s\n"), dir);
  504. }
  505. /*@ignore@*/
  506. amfree(new_dir);
  507. amfree(ldir);
  508. /*@end@*/
  509. }
  510. /* prints the current working directory */
  511. void
  512. show_directory(void)
  513. {
  514. if (mount_point == NULL || disk_path == NULL)
  515. g_printf(_("Must select disk first\n"));
  516. else if (g_str_equal(mount_point, "/"))
  517. g_printf("%s\n", disk_path);
  518. else if (g_str_equal(disk_path, "/"))
  519. g_printf("%s\n", mount_point);
  520. else
  521. g_printf("%s%s\n", mount_point, disk_path);
  522. }
  523. /* set the tape server and device */
  524. void
  525. set_tape(
  526. char * tape)
  527. {
  528. char *uqtape = unquote_string(tape);
  529. char *tapedev = strchr(uqtape, ':');
  530. if (tapedev)
  531. {
  532. if (tapedev != uqtape) {
  533. if((strchr(tapedev+1, ':') == NULL) &&
  534. (g_str_has_prefix(uqtape, "null:") ||
  535. g_str_has_prefix(uqtape, "rait:") ||
  536. g_str_has_prefix(uqtape, "file:") ||
  537. g_str_has_prefix(uqtape, "tape:"))) {
  538. tapedev = uqtape;
  539. }
  540. else {
  541. *tapedev = '\0';
  542. g_free(tape_server_name);
  543. tape_server_name = g_strdup(uqtape);
  544. ++tapedev;
  545. }
  546. } else { /* reset server_name if start with : */
  547. amfree(tape_server_name);
  548. ++tapedev;
  549. }
  550. } else
  551. tapedev = uqtape;
  552. if (tapedev[0])
  553. {
  554. if (g_str_equal(tapedev, "default"))
  555. amfree(tape_device_name);
  556. else {
  557. g_free(tape_device_name);
  558. tape_device_name = g_strdup(tapedev);
  559. }
  560. }
  561. if (tape_device_name)
  562. g_printf (_("Using tape \"%s\""), tape_device_name);
  563. else
  564. g_printf (_("Using default tape"));
  565. if (tape_server_name)
  566. g_printf (_(" from server %s.\n"), tape_server_name);
  567. else
  568. g_printf (_(".\nTape server unspecified, assumed to be %s.\n"),
  569. server_name);
  570. amfree(uqtape);
  571. }
  572. void
  573. set_mode(
  574. int mode)
  575. {
  576. #ifdef SAMBA_CLIENT
  577. if (mode == SAMBA_SMBCLIENT) {
  578. g_printf (_("SAMBA dumps will be extracted using smbclient\n"));
  579. samba_extract_method = SAMBA_SMBCLIENT;
  580. } else {
  581. if (mode == SAMBA_TAR) {
  582. g_printf (_("SAMBA dumps will be extracted as TAR dumps\n"));
  583. samba_extract_method = SAMBA_TAR;
  584. }
  585. }
  586. #else
  587. (void)mode; /* Quiet unused parameter warning */
  588. #endif /* SAMBA_CLIENT */
  589. }
  590. void
  591. show_mode(void)
  592. {
  593. #ifdef SAMBA_CLIENT
  594. g_printf (_("SAMBA dumps are extracted "));
  595. if (samba_extract_method == SAMBA_TAR) {
  596. g_printf (_(" as TAR dumps\n"));
  597. } else {
  598. g_printf (_("using smbclient\n"));
  599. }
  600. #endif /* SAMBA_CLIENT */
  601. }