PageRenderTime 68ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 1ms

/amanda/tags/3_2_1/server-src/find.c

#
C | 1307 lines | 1084 code | 135 blank | 88 comment | 332 complexity | 6bd56d0711570653a8e290207ca5c2e4 MD5 | raw file
  1. /*
  2. * Amanda, The Advanced Maryland Automatic Network Disk Archiver
  3. * Copyright (c) 1991-1998 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. * Author: James da Silva, Systems Design and Analysis Group
  24. * Computer Science Department
  25. * University of Maryland at College Park
  26. */
  27. /*
  28. * $Id: find.c,v 1.33 2006/07/06 13:13:15 martinea Exp $
  29. *
  30. * controlling process for the Amanda backup system
  31. */
  32. #include "amanda.h"
  33. #include "match.h"
  34. #include "conffile.h"
  35. #include "tapefile.h"
  36. #include "logfile.h"
  37. #include "holding.h"
  38. #include "find.h"
  39. #include <regex.h>
  40. #include "cmdline.h"
  41. int find_match(char *host, char *disk);
  42. static char *find_nicedate(char *datestamp);
  43. static int len_find_nicedate(char *datestamp);
  44. static int find_compare(const void *, const void *);
  45. static int parse_taper_datestamp_log(char *logline, char **datestamp, char **level);
  46. static gboolean logfile_has_tape(char * label, char * datestamp,
  47. char * logfile);
  48. static char *find_sort_order = NULL;
  49. find_result_t * find_dump(disklist_t* diskqp) {
  50. char *conf_logdir, *logfile = NULL;
  51. int tape, tape1, maxtape, logs;
  52. unsigned seq;
  53. tape_t *tp, *tp1;
  54. find_result_t *output_find = NULL;
  55. gboolean *tape_seen = NULL;
  56. conf_logdir = config_dir_relative(getconf_str(CNF_LOGDIR));
  57. maxtape = lookup_nb_tape();
  58. tape_seen = g_new0(gboolean, maxtape+1);
  59. for(tape = 1; tape <= maxtape; tape++) {
  60. if (tape_seen[tape] == 1)
  61. continue;
  62. tp = lookup_tapepos(tape);
  63. if(tp == NULL) continue;
  64. /* find all tape with the same datestamp */
  65. for (tape1 = tape; tape1 <= maxtape; tape1++) {
  66. tp1 = lookup_tapepos(tape1);
  67. if (tp1 == NULL) continue;
  68. if (strcmp(tp->datestamp, tp1->datestamp) != 0)
  69. continue;
  70. tape_seen[tape1] = 1;
  71. }
  72. /* search log files */
  73. logs = 0;
  74. /* new-style log.<date>.<seq> */
  75. for(seq = 0; 1; seq++) {
  76. char seq_str[NUM_STR_SIZE];
  77. g_snprintf(seq_str, SIZEOF(seq_str), "%u", seq);
  78. logfile = newvstralloc(logfile,
  79. conf_logdir, "/log.", tp->datestamp, ".", seq_str, NULL);
  80. if(access(logfile, R_OK) != 0) break;
  81. if (search_logfile(&output_find, NULL, tp->datestamp,
  82. logfile, diskqp)) {
  83. logs ++;
  84. }
  85. }
  86. /* search old-style amflush log, if any */
  87. logfile = newvstralloc(logfile, conf_logdir, "/log.",
  88. tp->datestamp, ".amflush", NULL);
  89. if(access(logfile,R_OK) == 0) {
  90. if (search_logfile(&output_find, NULL, tp->datestamp,
  91. logfile, diskqp)) {
  92. logs ++;
  93. }
  94. }
  95. /* search old-style main log, if any */
  96. logfile = newvstralloc(logfile, conf_logdir, "/log.", tp->datestamp,
  97. NULL);
  98. if(access(logfile,R_OK) == 0) {
  99. if (search_logfile(&output_find, NULL, tp->datestamp,
  100. logfile, diskqp)) {
  101. logs ++;
  102. }
  103. }
  104. }
  105. g_free(tape_seen);
  106. amfree(logfile);
  107. amfree(conf_logdir);
  108. search_holding_disk(&output_find, diskqp);
  109. return(output_find);
  110. }
  111. char **
  112. find_log(void)
  113. {
  114. char *conf_logdir, *logfile = NULL;
  115. char *pathlogfile = NULL;
  116. int tape, maxtape, logs;
  117. unsigned seq;
  118. tape_t *tp;
  119. char **output_find_log = NULL;
  120. char **current_log;
  121. conf_logdir = config_dir_relative(getconf_str(CNF_LOGDIR));
  122. maxtape = lookup_nb_tape();
  123. output_find_log = alloc((maxtape*5+10) * SIZEOF(char *));
  124. current_log = output_find_log;
  125. for(tape = 1; tape <= maxtape; tape++) {
  126. tp = lookup_tapepos(tape);
  127. if(tp == NULL) continue;
  128. /* search log files */
  129. logs = 0;
  130. /* new-style log.<date>.<seq> */
  131. for(seq = 0; 1; seq++) {
  132. char seq_str[NUM_STR_SIZE];
  133. g_snprintf(seq_str, SIZEOF(seq_str), "%u", seq);
  134. logfile = newvstralloc(logfile, "log.", tp->datestamp, ".", seq_str, NULL);
  135. pathlogfile = newvstralloc(pathlogfile, conf_logdir, "/", logfile, NULL);
  136. if (access(pathlogfile, R_OK) != 0) break;
  137. if (logfile_has_tape(tp->label, tp->datestamp, pathlogfile)) {
  138. if (current_log == output_find_log || strcmp(*(current_log-1), logfile)) {
  139. *current_log = stralloc(logfile);
  140. current_log++;
  141. }
  142. logs++;
  143. break;
  144. }
  145. }
  146. /* search old-style amflush log, if any */
  147. logfile = newvstralloc(logfile, "log.", tp->datestamp, ".amflush", NULL);
  148. pathlogfile = newvstralloc(pathlogfile, conf_logdir, "/", logfile, NULL);
  149. if (access(pathlogfile, R_OK) == 0) {
  150. if (logfile_has_tape(tp->label, tp->datestamp, pathlogfile)) {
  151. if (current_log == output_find_log || strcmp(*(current_log-1), logfile)) {
  152. *current_log = stralloc(logfile);
  153. current_log++;
  154. }
  155. logs++;
  156. }
  157. }
  158. /* search old-style main log, if any */
  159. logfile = newvstralloc(logfile, "log.", tp->datestamp, NULL);
  160. pathlogfile = newvstralloc(pathlogfile, conf_logdir, "/", logfile, NULL);
  161. if (access(pathlogfile, R_OK) == 0) {
  162. if (logfile_has_tape(tp->label, tp->datestamp, pathlogfile)) {
  163. if (current_log == output_find_log || strcmp(*(current_log-1), logfile)) {
  164. *current_log = stralloc(logfile);
  165. current_log++;
  166. }
  167. logs++;
  168. }
  169. }
  170. if(logs == 0 && strcmp(tp->datestamp,"0") != 0)
  171. g_fprintf(stderr, _("Warning: no log files found for tape %s written %s\n"),
  172. tp->label, find_nicedate(tp->datestamp));
  173. }
  174. amfree(logfile);
  175. amfree(pathlogfile);
  176. amfree(conf_logdir);
  177. *current_log = NULL;
  178. return(output_find_log);
  179. }
  180. void
  181. search_holding_disk(
  182. find_result_t **output_find,
  183. disklist_t * dynamic_disklist)
  184. {
  185. GSList *holding_file_list;
  186. GSList *e;
  187. char *holding_file;
  188. disk_t *dp;
  189. char *orig_name;
  190. holding_file_list = holding_get_files(NULL, 1);
  191. for(e = holding_file_list; e != NULL; e = e->next) {
  192. dumpfile_t file;
  193. holding_file = (char *)e->data;
  194. if (!holding_file_get_dumpfile(holding_file, &file))
  195. continue;
  196. if (file.dumplevel < 0 || file.dumplevel >= DUMP_LEVELS) {
  197. dumpfile_free_data(&file);
  198. continue;
  199. }
  200. dp = NULL;
  201. orig_name = g_strdup(file.name);
  202. for(;;) {
  203. char *s;
  204. if((dp = lookup_disk(file.name, file.disk)))
  205. break;
  206. if((s = strrchr(file.name,'.')) == NULL)
  207. break;
  208. *s = '\0';
  209. }
  210. strcpy(file.name, orig_name); /* restore munged string */
  211. g_free(orig_name);
  212. if ( dp == NULL ) {
  213. if (dynamic_disklist == NULL) {
  214. dumpfile_free_data(&file);
  215. continue;
  216. }
  217. dp = add_disk(dynamic_disklist, file.name, file.disk);
  218. enqueue_disk(dynamic_disklist, dp);
  219. }
  220. if(find_match(file.name,file.disk)) {
  221. find_result_t *new_output_find = g_new0(find_result_t, 1);
  222. new_output_find->next=*output_find;
  223. new_output_find->timestamp = stralloc(file.datestamp);
  224. new_output_find->write_timestamp = stralloc("00000000000000");
  225. new_output_find->hostname = stralloc(file.name);
  226. new_output_find->diskname = stralloc(file.disk);
  227. new_output_find->level=file.dumplevel;
  228. new_output_find->label=stralloc(holding_file);
  229. new_output_find->partnum = -1;
  230. new_output_find->totalparts = -1;
  231. new_output_find->filenum=0;
  232. if (file.is_partial) {
  233. new_output_find->status=stralloc("PARTIAL");
  234. new_output_find->dump_status=stralloc("PARTIAL");
  235. } else {
  236. new_output_find->status=stralloc("OK");
  237. new_output_find->dump_status=stralloc("OK");
  238. }
  239. new_output_find->message=stralloc("");
  240. new_output_find->kb = holding_file_size(holding_file, 1);
  241. new_output_find->orig_kb = file.orig_size;
  242. *output_find=new_output_find;
  243. }
  244. dumpfile_free_data(&file);
  245. }
  246. slist_free_full(holding_file_list, g_free);
  247. }
  248. static int
  249. find_compare(
  250. const void *i1,
  251. const void *j1)
  252. {
  253. int compare=0;
  254. find_result_t *i, *j;
  255. size_t nb_compare=strlen(find_sort_order);
  256. size_t k;
  257. for(k=0;k<nb_compare;k++) {
  258. char sort_key = find_sort_order[k];
  259. if (isupper((int)sort_key)) {
  260. /* swap */
  261. sort_key = tolower(sort_key);
  262. j = *(find_result_t **)i1;
  263. i = *(find_result_t **)j1;
  264. } else {
  265. i = *(find_result_t **)i1;
  266. j = *(find_result_t **)j1;
  267. }
  268. switch (sort_key) {
  269. case 'h' : compare=strcmp(i->hostname,j->hostname);
  270. break;
  271. case 'k' : compare=strcmp(i->diskname,j->diskname);
  272. break;
  273. case 'd' : compare=strcmp(i->timestamp,j->timestamp);
  274. break;
  275. case 'l' : compare=j->level - i->level;
  276. break;
  277. case 'f' : compare=(i->filenum == j->filenum) ? 0 :
  278. ((i->filenum < j->filenum) ? -1 : 1);
  279. break;
  280. case 'b' : compare=compare_possibly_null_strings(i->label,
  281. j->label);
  282. break;
  283. case 'w': compare=strcmp(i->write_timestamp, j->write_timestamp);
  284. break;
  285. case 'p' :
  286. compare=i->partnum - j->partnum;
  287. break;
  288. }
  289. if(compare != 0)
  290. return compare;
  291. }
  292. return 0;
  293. }
  294. void
  295. sort_find_result(
  296. char *sort_order,
  297. find_result_t **output_find)
  298. {
  299. find_result_t *output_find_result;
  300. find_result_t **array_find_result = NULL;
  301. size_t nb_result=0;
  302. size_t no_result;
  303. find_sort_order = sort_order;
  304. /* qsort core dump if nothing to sort */
  305. if(*output_find==NULL)
  306. return;
  307. /* How many result */
  308. for(output_find_result=*output_find;
  309. output_find_result;
  310. output_find_result=output_find_result->next) {
  311. nb_result++;
  312. }
  313. /* put the list in an array */
  314. array_find_result=alloc(nb_result * SIZEOF(find_result_t *));
  315. for(output_find_result=*output_find,no_result=0;
  316. output_find_result;
  317. output_find_result=output_find_result->next,no_result++) {
  318. array_find_result[no_result]=output_find_result;
  319. }
  320. /* sort the array */
  321. qsort(array_find_result,nb_result,SIZEOF(find_result_t *),
  322. find_compare);
  323. /* put the sorted result in the list */
  324. for(no_result=0;
  325. no_result<nb_result-1; no_result++) {
  326. array_find_result[no_result]->next = array_find_result[no_result+1];
  327. }
  328. array_find_result[nb_result-1]->next=NULL;
  329. *output_find=array_find_result[0];
  330. amfree(array_find_result);
  331. }
  332. void
  333. print_find_result(
  334. find_result_t *output_find)
  335. {
  336. find_result_t *output_find_result;
  337. int max_len_datestamp = 4;
  338. int max_len_hostname = 4;
  339. int max_len_diskname = 4;
  340. int max_len_level = 2;
  341. int max_len_label =12;
  342. int max_len_filenum = 4;
  343. int max_len_part = 4;
  344. int max_len_status = 6;
  345. size_t len;
  346. for(output_find_result=output_find;
  347. output_find_result;
  348. output_find_result=output_find_result->next) {
  349. char *s;
  350. len=len_find_nicedate(output_find_result->timestamp);
  351. if((int)len > max_len_datestamp)
  352. max_len_datestamp=(int)len;
  353. len=strlen(output_find_result->hostname);
  354. if((int)len > max_len_hostname)
  355. max_len_hostname = (int)len;
  356. len = len_quote_string(output_find_result->diskname);
  357. if((int)len > max_len_diskname)
  358. max_len_diskname = (int)len;
  359. if (output_find_result->label != NULL) {
  360. len = len_quote_string(output_find_result->label);
  361. if((int)len > max_len_label)
  362. max_len_label = (int)len;
  363. }
  364. len=strlen(output_find_result->status) + 1 + strlen(output_find_result->dump_status);
  365. if((int)len > max_len_status)
  366. max_len_status = (int)len;
  367. s = g_strdup_printf("%d/%d", output_find_result->partnum,
  368. output_find_result->totalparts);
  369. len=strlen(s);
  370. if((int)len > max_len_part)
  371. max_len_part = (int)len;
  372. amfree(s);
  373. }
  374. /*
  375. * Since status is the rightmost field, we zap the maximum length
  376. * because it is not needed. The code is left in place in case
  377. * another column is added later.
  378. */
  379. max_len_status = 1;
  380. if(output_find==NULL) {
  381. g_printf(_("\nNo dump to list\n"));
  382. }
  383. else {
  384. g_printf(_("\ndate%*s host%*s disk%*s lv%*s tape or file%*s file%*s part%*s status\n"),
  385. max_len_datestamp-4,"",
  386. max_len_hostname-4 ,"",
  387. max_len_diskname-4 ,"",
  388. max_len_level-2 ,"",
  389. max_len_label-12 ,"",
  390. max_len_filenum-4 ,"",
  391. max_len_part-4 ,"");
  392. for(output_find_result=output_find;
  393. output_find_result;
  394. output_find_result=output_find_result->next) {
  395. char *qdiskname;
  396. char * formatted_label;
  397. char *s;
  398. char *status;
  399. qdiskname = quote_string(output_find_result->diskname);
  400. if (output_find_result->label == NULL)
  401. formatted_label = stralloc("");
  402. else
  403. formatted_label = quote_string(output_find_result->label);
  404. if (strcmp(output_find_result->status, "OK") != 0 ||
  405. strcmp(output_find_result->dump_status, "OK") != 0) {
  406. status = vstralloc(output_find_result->status, " ",
  407. output_find_result->dump_status, NULL);
  408. } else {
  409. status = stralloc(output_find_result->status);
  410. }
  411. /*@ignore@*/
  412. /* sec and kb are omitted here, for compatibility with the existing
  413. * output from 'amadmin' */
  414. s = g_strdup_printf("%d/%d", output_find_result->partnum,
  415. output_find_result->totalparts);
  416. g_printf("%-*s %-*s %-*s %*d %-*s %*lld %*s %s %s\n",
  417. max_len_datestamp,
  418. find_nicedate(output_find_result->timestamp),
  419. max_len_hostname, output_find_result->hostname,
  420. max_len_diskname, qdiskname,
  421. max_len_level, output_find_result->level,
  422. max_len_label, formatted_label,
  423. max_len_filenum, (long long)output_find_result->filenum,
  424. max_len_part, s,
  425. status,
  426. output_find_result->message
  427. );
  428. amfree(status);
  429. amfree(s);
  430. /*@end@*/
  431. amfree(qdiskname);
  432. amfree(formatted_label);
  433. }
  434. }
  435. }
  436. void
  437. free_find_result(
  438. find_result_t **output_find)
  439. {
  440. find_result_t *output_find_result, *prev;
  441. prev=NULL;
  442. for(output_find_result=*output_find;
  443. output_find_result;
  444. output_find_result=output_find_result->next) {
  445. amfree(prev);
  446. amfree(output_find_result->timestamp);
  447. amfree(output_find_result->write_timestamp);
  448. amfree(output_find_result->hostname);
  449. amfree(output_find_result->diskname);
  450. amfree(output_find_result->label);
  451. amfree(output_find_result->status);
  452. amfree(output_find_result->dump_status);
  453. amfree(output_find_result->message);
  454. prev = output_find_result;
  455. }
  456. amfree(prev);
  457. *output_find = NULL;
  458. }
  459. int
  460. find_match(
  461. char *host,
  462. char *disk)
  463. {
  464. disk_t *dp = lookup_disk(host,disk);
  465. return (dp && dp->todo);
  466. }
  467. static char *
  468. find_nicedate(
  469. char *datestamp)
  470. {
  471. static char nice[20];
  472. int year, month, day;
  473. int hours, minutes, seconds;
  474. char date[9], atime[7];
  475. int numdate, numtime;
  476. strncpy(date, datestamp, 8);
  477. date[8] = '\0';
  478. numdate = atoi(date);
  479. year = numdate / 10000;
  480. month = (numdate / 100) % 100;
  481. day = numdate % 100;
  482. if(strlen(datestamp) <= 8) {
  483. g_snprintf(nice, SIZEOF(nice), "%4d-%02d-%02d",
  484. year, month, day);
  485. }
  486. else {
  487. strncpy(atime, &(datestamp[8]), 6);
  488. atime[6] = '\0';
  489. numtime = atoi(atime);
  490. hours = numtime / 10000;
  491. minutes = (numtime / 100) % 100;
  492. seconds = numtime % 100;
  493. g_snprintf(nice, SIZEOF(nice), "%4d-%02d-%02d %02d:%02d:%02d",
  494. year, month, day, hours, minutes, seconds);
  495. }
  496. return nice;
  497. }
  498. static int
  499. len_find_nicedate(
  500. char *datestamp)
  501. {
  502. if(strlen(datestamp) <= 8) {
  503. return 10;
  504. } else {
  505. return 19;
  506. }
  507. }
  508. static int
  509. parse_taper_datestamp_log(
  510. char *logline,
  511. char **datestamp,
  512. char **label)
  513. {
  514. char *s;
  515. int ch;
  516. s = logline;
  517. ch = *s++;
  518. skip_whitespace(s, ch);
  519. if(ch == '\0') {
  520. return 0;
  521. }
  522. if(strncmp_const_skip(s - 1, "datestamp", s, ch) != 0) {
  523. return 0;
  524. }
  525. skip_whitespace(s, ch);
  526. if(ch == '\0') {
  527. return 0;
  528. }
  529. *datestamp = s - 1;
  530. skip_non_whitespace(s, ch);
  531. s[-1] = '\0';
  532. skip_whitespace(s, ch);
  533. if(ch == '\0') {
  534. return 0;
  535. }
  536. if(strncmp_const_skip(s - 1, "label", s, ch) != 0) {
  537. return 0;
  538. }
  539. skip_whitespace(s, ch);
  540. if(ch == '\0') {
  541. return 0;
  542. }
  543. *label = s - 1;
  544. skip_non_whitespace(s, ch);
  545. s[-1] = '\0';
  546. return 1;
  547. }
  548. /* Returns TRUE if the given logfile mentions the given tape. */
  549. static gboolean logfile_has_tape(char * label, char * datestamp,
  550. char * logfile) {
  551. FILE * logf;
  552. char * ck_datestamp, *ck_label;
  553. if((logf = fopen(logfile, "r")) == NULL) {
  554. error(_("could not open logfile %s: %s"), logfile, strerror(errno));
  555. /*NOTREACHED*/
  556. }
  557. while(get_logline(logf)) {
  558. if(curlog == L_START && curprog == P_TAPER) {
  559. if(parse_taper_datestamp_log(curstr,
  560. &ck_datestamp, &ck_label) == 0) {
  561. g_printf(_("strange log line \"start taper %s\" curstr='%s'\n"),
  562. logfile, curstr);
  563. } else if(strcmp(ck_datestamp, datestamp) == 0
  564. && strcmp(ck_label, label) == 0) {
  565. afclose(logf);
  566. return TRUE;
  567. }
  568. }
  569. }
  570. afclose(logf);
  571. return FALSE;
  572. }
  573. static gboolean
  574. volume_matches(
  575. const char *label1,
  576. const char *label2,
  577. const char *datestamp)
  578. {
  579. tape_t *tp;
  580. if (!label2)
  581. return TRUE;
  582. if (label1)
  583. return (strcmp(label1, label2) == 0);
  584. /* check in tapelist */
  585. if (!(tp = lookup_tapelabel(label2)))
  586. return FALSE;
  587. if (strcmp(tp->datestamp, datestamp) != 0)
  588. return FALSE;
  589. return TRUE;
  590. }
  591. /* WARNING: Function accesses globals find_diskqp, curlog, curlog, curstr,
  592. * dynamic_disklist */
  593. gboolean
  594. search_logfile(
  595. find_result_t **output_find,
  596. const char *label,
  597. const char *passed_datestamp,
  598. const char *logfile,
  599. disklist_t * dynamic_disklist)
  600. {
  601. FILE *logf;
  602. char *host, *host_undo;
  603. char *disk, *qdisk, *disk_undo;
  604. char *date, *date_undo;
  605. int partnum;
  606. int totalparts;
  607. int maxparts = -1;
  608. char *number;
  609. int fileno;
  610. char *current_label = stralloc("");
  611. char *rest, *rest_undo;
  612. char *ck_label=NULL;
  613. int level = 0;
  614. off_t filenum;
  615. char *ck_datestamp, *datestamp;
  616. char *s;
  617. int ch;
  618. disk_t *dp;
  619. GHashTable* valid_label;
  620. GHashTable* part_by_dle;
  621. find_result_t *part_find;
  622. find_result_t *a_part_find;
  623. gboolean right_label = FALSE;
  624. gboolean found_something = FALSE;
  625. double sec;
  626. off_t kb;
  627. off_t orig_kb;
  628. int taper_part = 0;
  629. g_return_val_if_fail(output_find != NULL, 0);
  630. g_return_val_if_fail(logfile != NULL, 0);
  631. valid_label = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
  632. part_by_dle = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
  633. datestamp = g_strdup(passed_datestamp);
  634. if((logf = fopen(logfile, "r")) == NULL) {
  635. error(_("could not open logfile %s: %s"), logfile, strerror(errno));
  636. /*NOTREACHED*/
  637. }
  638. filenum = (off_t)0;
  639. while(get_logline(logf)) {
  640. if (curlog == L_START && curprog == P_TAPER) {
  641. if(parse_taper_datestamp_log(curstr, &ck_datestamp,
  642. &ck_label) == 0) {
  643. g_printf(_("strange log line in %s \"start taper %s\"\n"),
  644. logfile, curstr);
  645. continue;
  646. }
  647. if (datestamp != NULL) {
  648. if (strcmp(datestamp, ck_datestamp) != 0) {
  649. g_printf(_("Log file %s stamped %s, expecting %s!\n"),
  650. logfile, ck_datestamp, datestamp);
  651. break;
  652. }
  653. }
  654. right_label = volume_matches(label, ck_label, ck_datestamp);
  655. if (right_label && ck_label) {
  656. g_hash_table_insert(valid_label, g_strdup(ck_label),
  657. GINT_TO_POINTER(1));
  658. }
  659. if (label && datestamp && right_label) {
  660. found_something = TRUE;
  661. }
  662. amfree(current_label);
  663. current_label = g_strdup(ck_label);
  664. if (datestamp == NULL) {
  665. datestamp = g_strdup(ck_datestamp);
  666. }
  667. filenum = (off_t)0;
  668. }
  669. if (right_label &&
  670. (curlog == L_SUCCESS ||
  671. curlog == L_CHUNK || curlog == L_PART || curlog == L_PARTPARTIAL) &&
  672. curprog == P_TAPER) {
  673. filenum++;
  674. } else if (right_label && curlog == L_PARTIAL && curprog == P_TAPER &&
  675. taper_part == 0) {
  676. filenum++;
  677. }
  678. partnum = -1;
  679. totalparts = -1;
  680. if (curlog == L_SUCCESS || curlog == L_CHUNKSUCCESS ||
  681. curlog == L_DONE || curlog == L_FAIL ||
  682. curlog == L_CHUNK || curlog == L_PART || curlog == L_PARTIAL ||
  683. curlog == L_PARTPARTIAL ) {
  684. s = curstr;
  685. ch = *s++;
  686. skip_whitespace(s, ch);
  687. if(ch == '\0') {
  688. g_printf(_("strange log line in %s \"%s\"\n"),
  689. logfile, curstr);
  690. continue;
  691. }
  692. if (curlog == L_PART || curlog == L_PARTPARTIAL) {
  693. char * part_label = s - 1;
  694. taper_part++;
  695. skip_non_whitespace(s, ch);
  696. s[-1] = '\0';
  697. if (!g_hash_table_lookup(valid_label, part_label))
  698. continue;
  699. amfree(current_label);
  700. current_label = stralloc(part_label);
  701. skip_whitespace(s, ch);
  702. if(ch == '\0') {
  703. g_printf("strange log line in %s \"%s\"\n",
  704. logfile, curstr);
  705. continue;
  706. }
  707. number = s - 1;
  708. skip_non_whitespace(s, ch);
  709. s[-1] = '\0';
  710. fileno = atoi(number);
  711. filenum = fileno;
  712. if (filenum == 0)
  713. continue;
  714. skip_whitespace(s, ch);
  715. if(ch == '\0') {
  716. g_printf("strange log line in %s \"%s\"\n",
  717. logfile, curstr);
  718. continue;
  719. }
  720. } else {
  721. taper_part = 0;
  722. }
  723. host = s - 1;
  724. skip_non_whitespace(s, ch);
  725. host_undo = s - 1;
  726. *host_undo = '\0';
  727. skip_whitespace(s, ch);
  728. if(ch == '\0') {
  729. g_printf(_("strange log line in %s \"%s\"\n"),
  730. logfile, curstr);
  731. continue;
  732. }
  733. qdisk = s - 1;
  734. skip_quoted_string(s, ch);
  735. disk_undo = s - 1;
  736. *disk_undo = '\0';
  737. disk = unquote_string(qdisk);
  738. skip_whitespace(s, ch);
  739. if(ch == '\0') {
  740. g_printf(_("strange log line in %s \"%s\"\n"),
  741. logfile, curstr);
  742. continue;
  743. }
  744. date = s - 1;
  745. skip_non_whitespace(s, ch);
  746. date_undo = s - 1;
  747. *date_undo = '\0';
  748. if(strlen(date) < 3) { /* old log didn't have datestamp */
  749. level = atoi(date);
  750. date = stralloc(datestamp);
  751. partnum = 1;
  752. totalparts =1;
  753. } else {
  754. if (curprog == P_TAPER &&
  755. (curlog == L_CHUNK || curlog == L_PART ||
  756. curlog == L_PARTPARTIAL || curlog == L_PARTIAL ||
  757. curlog == L_DONE)) {
  758. skip_whitespace(s, ch);
  759. number = s - 1;
  760. skip_non_whitespace(s, ch);
  761. s[-1] = '\0';
  762. sscanf(number, "%d/%d", &partnum, &totalparts);
  763. if (partnum > maxparts)
  764. maxparts = partnum;
  765. if (totalparts > maxparts)
  766. maxparts = totalparts;
  767. }
  768. skip_whitespace(s, ch);
  769. if(ch == '\0' || sscanf(s - 1, "%d", &level) != 1) {
  770. g_printf(_("strange log line in %s \"%s\"\n"),
  771. logfile, curstr);
  772. continue;
  773. }
  774. skip_integer(s, ch);
  775. }
  776. skip_whitespace(s, ch);
  777. if(ch == '\0') {
  778. g_printf(_("strange log line in %s \"%s\"\n"),
  779. logfile, curstr);
  780. continue;
  781. }
  782. rest = s - 1;
  783. skip_non_whitespace(s, ch);
  784. rest_undo = s - 1;
  785. *rest_undo = '\0';
  786. if (strcmp(rest, "[sec") == 0) {
  787. skip_whitespace(s, ch);
  788. if(ch == '\0') {
  789. g_printf(_("strange log line in %s \"%s\"\n"),
  790. logfile, curstr);
  791. continue;
  792. }
  793. sec = atof(s - 1);
  794. skip_non_whitespace(s, ch);
  795. skip_whitespace(s, ch);
  796. rest = s - 1;
  797. skip_non_whitespace(s, ch);
  798. rest_undo = s - 1;
  799. *rest_undo = '\0';
  800. if (strcmp(rest, "kb") != 0) {
  801. g_printf(_("Bstrange log line in %s \"%s\"\n"),
  802. logfile, curstr);
  803. continue;
  804. }
  805. skip_whitespace(s, ch);
  806. if (ch == '\0') {
  807. g_printf(_("strange log line in %s \"%s\"\n"),
  808. logfile, curstr);
  809. continue;
  810. }
  811. kb = atof(s - 1);
  812. skip_non_whitespace(s, ch);
  813. skip_whitespace(s, ch);
  814. rest = s - 1;
  815. skip_non_whitespace(s, ch);
  816. rest_undo = s - 1;
  817. *rest_undo = '\0';
  818. if (strcmp(rest, "kps") != 0) {
  819. g_printf(_("Cstrange log line in %s \"%s\"\n"),
  820. logfile, curstr);
  821. continue;
  822. }
  823. skip_whitespace(s, ch);
  824. if (ch == '\0') {
  825. g_printf(_("strange log line in %s \"%s\"\n"),
  826. logfile, curstr);
  827. continue;
  828. }
  829. /* kps = atof(s - 1); */
  830. skip_non_whitespace(s, ch);
  831. skip_whitespace(s, ch);
  832. rest = s - 1;
  833. skip_non_whitespace(s, ch);
  834. rest_undo = s - 1;
  835. *rest_undo = '\0';
  836. if (strcmp(rest, "orig-kb") != 0) {
  837. orig_kb = 0;
  838. } else {
  839. skip_whitespace(s, ch);
  840. if(ch == '\0') {
  841. g_printf(_("strange log line in %s \"%s\"\n"),
  842. logfile, curstr);
  843. continue;
  844. }
  845. orig_kb = atof(s - 1);
  846. }
  847. } else {
  848. sec = 0;
  849. kb = 0;
  850. orig_kb = 0;
  851. *rest_undo = ' ';
  852. }
  853. if (strncmp(rest, "error", 5) == 0) rest += 6;
  854. if (strncmp(rest, "config", 6) == 0) rest += 7;
  855. dp = lookup_disk(host,disk);
  856. if ( dp == NULL ) {
  857. if (dynamic_disklist == NULL) {
  858. continue;
  859. }
  860. dp = add_disk(dynamic_disklist, host, disk);
  861. enqueue_disk(dynamic_disklist, dp);
  862. }
  863. if (find_match(host, disk)) {
  864. if(curprog == P_TAPER) {
  865. char *key = g_strdup_printf(
  866. "HOST:%s DISK:%s: DATE:%s LEVEL:%d",
  867. host, disk, date, level);
  868. find_result_t *new_output_find = g_new0(find_result_t, 1);
  869. part_find = g_hash_table_lookup(part_by_dle, key);
  870. new_output_find->timestamp = stralloc(date);
  871. new_output_find->write_timestamp = stralloc(datestamp);
  872. new_output_find->hostname=stralloc(host);
  873. new_output_find->diskname=stralloc(disk);
  874. new_output_find->level=level;
  875. new_output_find->partnum = partnum;
  876. new_output_find->totalparts = totalparts;
  877. new_output_find->label=stralloc(current_label);
  878. new_output_find->status=NULL;
  879. new_output_find->dump_status=NULL;
  880. new_output_find->message=stralloc("");
  881. new_output_find->filenum=filenum;
  882. new_output_find->sec=sec;
  883. new_output_find->kb=kb;
  884. new_output_find->orig_kb=orig_kb;
  885. new_output_find->next=NULL;
  886. if (curlog == L_SUCCESS) {
  887. new_output_find->status = stralloc("OK");
  888. new_output_find->dump_status = stralloc("OK");
  889. new_output_find->next = *output_find;
  890. new_output_find->partnum = 1; /* L_SUCCESS is pre-splitting */
  891. *output_find = new_output_find;
  892. found_something = TRUE;
  893. } else if (curlog == L_CHUNKSUCCESS || curlog == L_DONE ||
  894. curlog == L_PARTIAL || curlog == L_FAIL) {
  895. /* result line */
  896. if (curlog == L_PARTIAL || curlog == L_FAIL) {
  897. /* set dump_status of each part */
  898. for (a_part_find = part_find;
  899. a_part_find;
  900. a_part_find = a_part_find->next) {
  901. amfree(a_part_find->dump_status);
  902. if (curlog == L_PARTIAL)
  903. a_part_find->dump_status = stralloc("PARTIAL");
  904. else {
  905. a_part_find->dump_status = stralloc("FAIL");
  906. amfree(a_part_find->message);
  907. a_part_find->message = stralloc(rest);
  908. }
  909. }
  910. } else {
  911. if (maxparts > -1) { /* format with part */
  912. /* must check if all part are there */
  913. int num_part = maxparts;
  914. for (a_part_find = part_find;
  915. a_part_find;
  916. a_part_find = a_part_find->next) {
  917. if (a_part_find->partnum == num_part &&
  918. strcmp(a_part_find->status, "OK") == 0)
  919. num_part--;
  920. }
  921. /* set dump_status of each part */
  922. for (a_part_find = part_find;
  923. a_part_find;
  924. a_part_find = a_part_find->next) {
  925. amfree(a_part_find->dump_status);
  926. if (num_part == 0) {
  927. a_part_find->dump_status =
  928. stralloc("OK");
  929. } else {
  930. a_part_find->dump_status =
  931. stralloc("FAIL");
  932. amfree(a_part_find->message);
  933. a_part_find->message =
  934. stralloc("Missing part");
  935. }
  936. }
  937. }
  938. }
  939. if (curlog == L_DONE) {
  940. for (a_part_find = part_find;
  941. a_part_find;
  942. a_part_find = a_part_find->next) {
  943. if (a_part_find->totalparts == -1) {
  944. a_part_find->totalparts = maxparts;
  945. }
  946. if (a_part_find->orig_kb == 0) {
  947. a_part_find->orig_kb = orig_kb;
  948. }
  949. }
  950. }
  951. if (part_find) { /* find last element */
  952. for (a_part_find = part_find;
  953. a_part_find->next != NULL;
  954. a_part_find=a_part_find->next) {
  955. }
  956. /* merge part_find to *output_find */
  957. a_part_find->next = *output_find;
  958. *output_find = part_find;
  959. part_find = NULL;
  960. maxparts = -1;
  961. found_something = TRUE;
  962. g_hash_table_remove(part_by_dle, key);
  963. }
  964. free_find_result(&new_output_find);
  965. } else { /* part line */
  966. if (curlog == L_PART || curlog == L_CHUNK) {
  967. new_output_find->status=stralloc("OK");
  968. new_output_find->dump_status=stralloc("OK");
  969. } else { /* PARTPARTIAL */
  970. new_output_find->status=stralloc("PARTIAL");
  971. new_output_find->dump_status=stralloc("PARTIAL");
  972. }
  973. /* Add to part_find list */
  974. if (part_find) {
  975. new_output_find->next = part_find;
  976. part_find = new_output_find;
  977. } else {
  978. new_output_find->next = NULL;
  979. part_find = new_output_find;
  980. }
  981. g_hash_table_insert(part_by_dle, g_strdup(key),
  982. part_find);
  983. found_something = TRUE;
  984. }
  985. amfree(key);
  986. }
  987. else if(curlog == L_FAIL) {
  988. /* print other failures too -- this is a hack to ensure that failures which
  989. * did not make it to tape are also listed in the output of 'amadmin x find';
  990. * users that do not want this information (e.g., Amanda::DB::Catalog) should
  991. * filter dumps with a NULL label. */
  992. find_result_t *new_output_find = g_new0(find_result_t, 1);
  993. new_output_find->next = *output_find;
  994. new_output_find->timestamp = stralloc(date);
  995. new_output_find->write_timestamp = g_strdup("00000000000000"); /* dump was not written.. */
  996. new_output_find->hostname=stralloc(host);
  997. new_output_find->diskname=stralloc(disk);
  998. new_output_find->level=level;
  999. new_output_find->label=NULL;
  1000. new_output_find->partnum=partnum;
  1001. new_output_find->totalparts=totalparts;
  1002. new_output_find->filenum=0;
  1003. new_output_find->sec=sec;
  1004. new_output_find->kb=kb;
  1005. new_output_find->kb=orig_kb;
  1006. new_output_find->status=vstralloc(
  1007. "FAILED (",
  1008. program_str[(int)curprog],
  1009. ") ",
  1010. rest,
  1011. NULL);
  1012. new_output_find->dump_status=stralloc("");
  1013. new_output_find->message=stralloc("");
  1014. *output_find=new_output_find;
  1015. found_something = TRUE;
  1016. maxparts = -1;
  1017. }
  1018. }
  1019. amfree(disk);
  1020. }
  1021. }
  1022. g_hash_table_destroy(valid_label);
  1023. afclose(logf);
  1024. amfree(datestamp);
  1025. amfree(current_label);
  1026. return found_something;
  1027. }
  1028. /*
  1029. * Return the set of dumps that match *all* of the given patterns (we consider
  1030. * an empty pattern to match .*, though). If 'ok' is true, will only match
  1031. * dumps with SUCCESS status.
  1032. *
  1033. * Returns a newly allocated list of results, where all strings are also newly
  1034. * allocated. Apparently some part of Amanda leaks under this condition.
  1035. */
  1036. find_result_t *
  1037. dumps_match(
  1038. find_result_t *output_find,
  1039. char *hostname,
  1040. char *diskname,
  1041. char *datestamp,
  1042. char *level,
  1043. int ok)
  1044. {
  1045. find_result_t *cur_result;
  1046. find_result_t *matches = NULL;
  1047. for(cur_result=output_find;
  1048. cur_result;
  1049. cur_result=cur_result->next) {
  1050. char level_str[NUM_STR_SIZE];
  1051. g_snprintf(level_str, SIZEOF(level_str), "%d", cur_result->level);
  1052. if((!hostname || *hostname == '\0' || match_host(hostname, cur_result->hostname)) &&
  1053. (!diskname || *diskname == '\0' || match_disk(diskname, cur_result->diskname)) &&
  1054. (!datestamp || *datestamp== '\0' || match_datestamp(datestamp, cur_result->timestamp)) &&
  1055. (!level || *level== '\0' || match_level(level, level_str)) &&
  1056. (!ok || !strcmp(cur_result->status, "OK")) &&
  1057. (!ok || !strcmp(cur_result->dump_status, "OK"))){
  1058. find_result_t *curmatch = g_new0(find_result_t, 1);
  1059. memcpy(curmatch, cur_result, SIZEOF(find_result_t));
  1060. curmatch->timestamp = stralloc(cur_result->timestamp);
  1061. curmatch->write_timestamp = stralloc(cur_result->write_timestamp);
  1062. curmatch->hostname = stralloc(cur_result->hostname);
  1063. curmatch->diskname = stralloc(cur_result->diskname);
  1064. curmatch->level = cur_result->level;
  1065. curmatch->label = cur_result->label? stralloc(cur_result->label) : NULL;
  1066. curmatch->filenum = cur_result->filenum;
  1067. curmatch->sec = cur_result->sec;
  1068. curmatch->kb = cur_result->kb;
  1069. curmatch->orig_kb = cur_result->orig_kb;
  1070. curmatch->status = stralloc(cur_result->status);
  1071. curmatch->dump_status = stralloc(cur_result->dump_status);
  1072. curmatch->message = stralloc(cur_result->message);
  1073. curmatch->partnum = cur_result->partnum;
  1074. curmatch->totalparts = cur_result->totalparts;
  1075. curmatch->next = matches;
  1076. matches = curmatch;
  1077. }
  1078. }
  1079. return(matches);
  1080. }
  1081. /*
  1082. * Return the set of dumps that match one or more of the given dumpspecs,
  1083. * If 'ok' is true, only dumps with a SUCCESS status will be matched.
  1084. *
  1085. * Returns a newly allocated list of results, where all strings are also newly
  1086. * allocated. Apparently some part of Amanda leaks under this condition.
  1087. */
  1088. find_result_t *
  1089. dumps_match_dumpspecs(
  1090. find_result_t *output_find,
  1091. GSList *dumpspecs,
  1092. int ok)
  1093. {
  1094. find_result_t *cur_result;
  1095. find_result_t *matches = NULL;
  1096. GSList *dumpspec;
  1097. dumpspec_t *ds;
  1098. for(cur_result=output_find;
  1099. cur_result;
  1100. cur_result=cur_result->next) {
  1101. char level_str[NUM_STR_SIZE];
  1102. char *zeropad_ts = NULL;
  1103. char *zeropad_w_ts = NULL;
  1104. g_snprintf(level_str, SIZEOF(level_str), "%d", cur_result->level);
  1105. /* get the timestamp padded to full width */
  1106. if (strlen(cur_result->timestamp) < 14) {
  1107. zeropad_ts = g_new0(char, 15);
  1108. memset(zeropad_ts, '0', 14);
  1109. memcpy(zeropad_ts, cur_result->timestamp, strlen(cur_result->timestamp));
  1110. }
  1111. if (strlen(cur_result->write_timestamp) < 14) {
  1112. zeropad_w_ts = g_new0(char, 15);
  1113. memset(zeropad_w_ts, '0', 14);
  1114. memcpy(zeropad_w_ts, cur_result->write_timestamp, strlen(cur_result->write_timestamp));
  1115. }
  1116. for (dumpspec = dumpspecs; dumpspec; dumpspec = dumpspec->next) {
  1117. ds = (dumpspec_t *)dumpspec->data;
  1118. if((!ds->host || *ds->host == '\0' || match_host(ds->host, cur_result->hostname)) &&
  1119. (!ds->disk || *ds->disk == '\0' || match_disk(ds->disk, cur_result->diskname)) &&
  1120. (!ds->datestamp || *ds->datestamp== '\0'
  1121. || match_datestamp(ds->datestamp, cur_result->timestamp)
  1122. || (zeropad_ts && match_datestamp(ds->datestamp, zeropad_ts))) &&
  1123. (!ds->write_timestamp || *ds->write_timestamp== '\0'
  1124. || match_datestamp(ds->write_timestamp, cur_result->write_timestamp)
  1125. || (zeropad_w_ts && match_datestamp(ds->write_timestamp, zeropad_w_ts))) &&
  1126. (!ds->level || *ds->level== '\0' || match_level(ds->level, level_str)) &&
  1127. (!ok || !strcmp(cur_result->status, "OK")) &&
  1128. (!ok || !strcmp(cur_result->dump_status, "OK"))) {
  1129. find_result_t *curmatch = alloc(SIZEOF(find_result_t));
  1130. memcpy(curmatch, cur_result, SIZEOF(find_result_t));
  1131. curmatch->timestamp = stralloc(cur_result->timestamp);
  1132. curmatch->write_timestamp = stralloc(cur_result->write_timestamp);
  1133. curmatch->hostname = stralloc(cur_result->hostname);
  1134. curmatch->diskname = stralloc(cur_result->diskname);
  1135. curmatch->level = cur_result->level;
  1136. curmatch->label = cur_result->label? stralloc(cur_result->label) : NULL;
  1137. curmatch->filenum = cur_result->filenum;
  1138. curmatch->status = stralloc(cur_result->status);
  1139. curmatch->dump_status = stralloc(cur_result->dump_status);
  1140. curmatch->message = stralloc(cur_result->message);
  1141. curmatch->partnum = cur_result->partnum;
  1142. curmatch->totalparts = cur_result->totalparts;
  1143. curmatch->next = matches;
  1144. matches = curmatch;
  1145. break;
  1146. }
  1147. }
  1148. amfree(zeropad_ts);
  1149. }
  1150. return(matches);
  1151. }
  1152. find_result_t *
  1153. dump_exist(
  1154. find_result_t *output_find,
  1155. char *hostname,
  1156. char *diskname,
  1157. char *datestamp,
  1158. int level)
  1159. {
  1160. find_result_t *output_find_result;
  1161. for(output_find_result=output_find;
  1162. output_find_result;
  1163. output_find_result=output_find_result->next) {
  1164. if( !strcmp(output_find_result->hostname, hostname) &&
  1165. !strcmp(output_find_result->diskname, diskname) &&
  1166. !strcmp(output_find_result->timestamp, datestamp) &&
  1167. output_find_result->level == level) {
  1168. return output_find_result;
  1169. }
  1170. }
  1171. return(NULL);
  1172. }