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

/amanda/branches/3_1_0_rpm/server-src/find.c

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