* 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.
*/
/*
* 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;
}
/* 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;
/*
* 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;
/*
* 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;
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);
* 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);
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');
/*
* 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);
}