PageRenderTime 61ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/amanda/tags/3_3_0beta1/oldrecover-src/set_commands.c

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