X-Git-Url: https://git.gag.com/?a=blobdiff_plain;f=common-src%2Ftapelist.c;h=803ea6803fa61adc7ee82180521d68439dc243a7;hb=afaa71b3866b46b082b6c895772e15b36d8865fe;hp=8d39cab6b5bbdd36c9b18c94b88bd2e3388a9c4d;hpb=eefb15c5c04acb3c75f0c704ea664feb1bbae75c;p=debian%2Famanda diff --git a/common-src/tapelist.c b/common-src/tapelist.c index 8d39cab..803ea68 100644 --- a/common-src/tapelist.c +++ b/common-src/tapelist.c @@ -24,7 +24,7 @@ * file named AUTHORS, in the root directory of this distribution. */ /* - * $Id: tapelist.c,v 1.3 2006/01/15 21:00:59 martinea Exp $ + * $Id: tapelist.c,v 1.8 2006/06/12 15:34:48 martinea Exp $ * * Support code for amidxtaped and amindexd. */ @@ -35,59 +35,106 @@ /* * Count the number of entries in this tapelist */ -int num_entries (tapelist) -tapelist_t *tapelist; +int +num_entries( + tapelist_t * tapelist) { tapelist_t *cur_tape; int count = 0; - for(cur_tape = tapelist ; cur_tape ; cur_tape = cur_tape->next) count++; + for(cur_tape = tapelist ; cur_tape ; cur_tape = cur_tape->next) + count++; + dbprintf("num_entries(tapelist=%p)=%d\n", tapelist, count); return(count); } +void +dump_tapelist( + tapelist_t *tapelist) +{ + tapelist_t *cur_tape; + int count = 0; + int file; + + dbprintf("dump_tapelist(%p):\n", tapelist); + for(cur_tape = tapelist ; cur_tape != NULL ; cur_tape = cur_tape->next) { + dbprintf(" %p->next = %p\n", cur_tape, cur_tape->next); + dbprintf(" %p->label = %s\n", cur_tape, cur_tape->label); + dbprintf(" %p->isafile = %d\n", cur_tape, cur_tape->isafile); + dbprintf(" %p->numfiles = %d\n", cur_tape, cur_tape->numfiles); + for (file=0; file < cur_tape->numfiles; file++) { + dbprintf(" %p->files[%d] = %lld, %p->partnum[%d] = %lld\n", + cur_tape, file, (long long)cur_tape->files[file], + cur_tape, file, (long long)cur_tape->partnum[file]); + } + count++; + } + dbprintf(" %p count = %d\n", tapelist, count); +} + /* * Add a tape entry with the given label to the given tapelist, creating a new * tapelist if handed a NULL one. Squashes duplicates. */ -tapelist_t *append_to_tapelist (tapelist, label, file, isafile) -tapelist_t *tapelist; -char *label; -int file, isafile; +tapelist_t * +append_to_tapelist( + tapelist_t *tapelist, + char * label, + off_t file, + int partnum, + int isafile) { tapelist_t *new_tape, *cur_tape; int c; + dbprintf("append_to_tapelist(tapelist=%p, label='%s', file=%lld, partnum=%d, isafile=%d)\n", + tapelist, label, (long long)file, partnum, isafile); + /* see if we have this tape already, and if so just add to its file list */ - for(cur_tape = tapelist; cur_tape; cur_tape = cur_tape->next){ - if(!strcmp(label, cur_tape->label)){ + for(cur_tape = tapelist; cur_tape; cur_tape = cur_tape->next) { + if(strcmp(label, cur_tape->label) == 0) { int d_idx = 0; - int *newfiles; - if(file >= 0){ - newfiles = alloc(sizeof(int) * (cur_tape->numfiles + 1)); - for(c = 0; c < cur_tape->numfiles ; c++){ - if(cur_tape->files[c] > file && c == d_idx){ + off_t *newfiles; + int *newpartnum; + + if(file >= (off_t)0) { + newfiles = alloc(SIZEOF(*newfiles) * + (cur_tape->numfiles + 1)); + newpartnum = alloc(SIZEOF(*newpartnum) * + (cur_tape->numfiles + 1)); + for(c = 0; c < cur_tape->numfiles ; c++) { + if(cur_tape->files[c] > file && c == d_idx) { newfiles[d_idx] = file; + newpartnum[d_idx] = partnum; d_idx++; } newfiles[d_idx] = cur_tape->files[c]; + newpartnum[d_idx] = cur_tape->partnum[c]; d_idx++; } - if(c == d_idx) newfiles[d_idx] = file; + if(c == d_idx) { + newfiles[d_idx] = file; + newpartnum[d_idx] = partnum; + } cur_tape->numfiles++; amfree(cur_tape->files); + amfree(cur_tape->partnum); cur_tape->files = newfiles; + cur_tape->partnum = newpartnum; } return(tapelist); } } - new_tape = alloc(sizeof(tapelist_t)); - memset(new_tape, 0, sizeof(tapelist_t)); + new_tape = alloc(SIZEOF(tapelist_t)); + memset(new_tape, 0, SIZEOF(tapelist_t)); new_tape->label = stralloc(label); - if(file >= 0){ - new_tape->files = alloc(sizeof(int)); + if(file >= (off_t)0){ + new_tape->files = alloc(SIZEOF(*(new_tape->files))); new_tape->files[0] = file; + new_tape->partnum = alloc(SIZEOF(*(new_tape->partnum))); + new_tape->partnum[0] = partnum; new_tape->numfiles = 1; new_tape->isafile = isafile; } @@ -95,21 +142,23 @@ int file, isafile; /* first instance of anything, start our tapelist with it */ if(!tapelist){ tapelist = new_tape; - return(tapelist); + } else { + /* new tape, tack it onto the end of the list */ + cur_tape = tapelist; + while (cur_tape->next != NULL) + cur_tape = cur_tape->next; + cur_tape->next = new_tape; } - /* new tape, tack it onto the end of the list */ - for(cur_tape = tapelist; cur_tape->next; cur_tape = cur_tape->next); - cur_tape->next = new_tape; - return(tapelist); } /* * Backslash-escape all of the commas (and backslashes) in a label string. */ -char *escape_label (label) -char *label; +char * +escape_label( + char * label) { char *cooked_str, *temp_str; int s_idx = 0, d_idx = 0; @@ -139,8 +188,9 @@ char *label; /* * Strip out any escape characters (backslashes) */ -char *unescape_label(label) -char *label; +char * +unescape_label( + char * label) { char *cooked_str, *temp_str; int s_idx = 0, d_idx = 0, prev_esc = 0; @@ -171,9 +221,10 @@ char *label; /* * Convert a tapelist into a parseable string of tape labels and file numbers. */ -char *marshal_tapelist (tapelist, do_escape) -tapelist_t *tapelist; -int do_escape; +char * +marshal_tapelist( + tapelist_t *tapelist, + int do_escape) { tapelist_t *cur_tape; char *str = NULL; @@ -188,13 +239,18 @@ int do_escape; for(c = 0; c < cur_tape->numfiles ; c++){ char num_str[NUM_STR_SIZE]; - snprintf(num_str, sizeof(num_str), "%d", cur_tape->files[c]); - if(!files_str) files_str = stralloc(num_str); - else files_str = vstralloc(files_str, ",", num_str, NULL); + g_snprintf(num_str, SIZEOF(num_str), "%lld", + (long long)cur_tape->files[c]); + if (!files_str) + files_str = stralloc(num_str); + else + vstrextend(&files_str, ",", num_str, NULL); } - if(!str) str = vstralloc(esc_label, ":", files_str, NULL); - else str = vstralloc(str, ";", esc_label, ":", files_str, NULL); + if (!str) + str = vstralloc(esc_label, ":", files_str, NULL); + else + vstrextend(&str, ";", esc_label, ":", files_str, NULL); amfree(esc_label); amfree(files_str); @@ -207,11 +263,13 @@ int do_escape; * Convert a previously str-ified and escaped list of tapes back into a * tapelist structure. */ -tapelist_t *unmarshal_tapelist_str (tapelist_str) -char *tapelist_str; +tapelist_t * +unmarshal_tapelist_str( + char * tapelist_str) { char *temp_label, *temp_filenum; - int l_idx, n_idx, input_length; + int l_idx, n_idx; + size_t input_length; tapelist_t *tapelist = NULL; if(!tapelist_str) return(NULL); @@ -226,32 +284,38 @@ char *tapelist_str; memset(temp_label, '\0', input_length+1); l_idx = 0; while(*tapelist_str != ':' && *tapelist_str != '\0'){ - if(*tapelist_str == '\\') *tapelist_str++; /* skip escapes */ + if(*tapelist_str == '\\') + tapelist_str++; /* skip escapes */ temp_label[l_idx] = *tapelist_str; - if(*tapelist_str == '\0') break; /* bad format, should kvetch */ - *tapelist_str++; + if(*tapelist_str == '\0') + break; /* bad format, should kvetch */ + tapelist_str++; l_idx++; } - if(*tapelist_str != '\0') *tapelist_str++; - tapelist = append_to_tapelist(tapelist, temp_label, -1, 0); + if(*tapelist_str != '\0') + tapelist_str++; + tapelist = append_to_tapelist(tapelist, temp_label, (off_t)-1, -1, 0); /* now read the list of file numbers */ while(*tapelist_str != ';' && *tapelist_str != '\0'){ - int filenum = -1; + off_t filenum; + memset(temp_filenum, '\0', input_length+1); n_idx = 0; while(*tapelist_str != ';' && *tapelist_str != ',' && *tapelist_str != '\0'){ temp_filenum[n_idx] = *tapelist_str; - *tapelist_str++; + tapelist_str++; n_idx++; } - filenum = atoi(temp_filenum); + filenum = OFF_T_ATOI(temp_filenum); - tapelist = append_to_tapelist(tapelist, temp_label, filenum, 0); - if(*tapelist_str != '\0' && *tapelist_str != ';') *tapelist_str++; + tapelist = append_to_tapelist(tapelist, temp_label, filenum, -1, 0); + if(*tapelist_str != '\0' && *tapelist_str != ';') + tapelist_str++; } - if(*tapelist_str != '\0') *tapelist_str++; + if(*tapelist_str != '\0') + tapelist_str++; } while(*tapelist_str != '\0'); @@ -264,18 +328,19 @@ char *tapelist_str; /* * Free up a list of tapes */ -void free_tapelist (tapelist) -tapelist_t *tapelist; +void +free_tapelist( + tapelist_t * tapelist) { tapelist_t *cur_tape; tapelist_t *prev = NULL; for(cur_tape = tapelist ; cur_tape ; cur_tape = cur_tape->next){ - if(cur_tape->label) amfree(cur_tape->label); - if(cur_tape->files) amfree(cur_tape->files); - if(prev) amfree(prev); + amfree(cur_tape->label); + amfree(cur_tape->files); + amfree(cur_tape->partnum); + amfree(prev); prev = cur_tape; } - if(prev) amfree(prev); - + amfree(prev); }