forked from PacificBiosciences/DAZZ_DB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fasta2DB.c
732 lines (633 loc) · 24.8 KB
/
fasta2DB.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
/************************************************************************************\
* *
* Copyright (c) 2014, Dr. Eugene W. Myers (EWM). All rights reserved. *
* *
* Redistribution and use in source and binary forms, with or without modification, *
* are permitted provided that the following conditions are met: *
* *
* · Redistributions of source code must retain the above copyright notice, this *
* list of conditions and the following disclaimer. *
* *
* · Redistributions in binary form must reproduce the above copyright notice, this *
* list of conditions and the following disclaimer in the documentation and/or *
* other materials provided with the distribution. *
* *
* · The name of EWM may not be used to endorse or promote products derived from *
* this software without specific prior written permission. *
* *
* THIS SOFTWARE IS PROVIDED BY EWM ”AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, *
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND *
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL EWM BE LIABLE *
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES *
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS *
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY *
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN *
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
* *
* For any issues regarding this software and its use, contact EWM at: *
* *
* Eugene W. Myers Jr. *
* Bautzner Str. 122e *
* 01099 Dresden *
* GERMANY *
* Email: [email protected] *
* *
\************************************************************************************/
/*******************************************************************************************
*
* Add .fasta files to a DB:
* Adds the given fasta files in the given order to <path>.db. If the db does not exist
* then it is created. All .fasta files added to a given data base must have the same
* header format and follow Pacbio's convention. A file cannot be added twice and this
* is enforced. The command either builds or appends to the .<path>.idx and .<path>.bps
* files, where the index file (.idx) contains information about each read and their offsets
* in the base-pair file (.bps) that holds the sequences where each base is compessed
* into 2-bits. The two files are hidden by virtue of their names beginning with a '.'.
* <path>.db is effectively a stub file with given name that contains an ASCII listing
* of the files added to the DB and possibly the block partitioning for the DB if DBsplit
* has been called upon it.
*
* Author: Gene Myers
* Date : May 2013
* Modify: DB upgrade: now *add to* or create a DB depending on whether it exists, read
* multiple .fasta files (no longer a stdin pipe).
* Date : April 2014
*
********************************************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <sys/stat.h>
#include <unistd.h>
#include "DB.h"
#include "open_compressed.h"
#ifdef HIDE_FILES
#define PATHSEP "/."
#else
#define PATHSEP "/"
#endif
static char *Usage = "[-v] <path:string> ( -f<file> | <input:fasta> ... )";
static char number[128] =
{ 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 1, 0, 0, 0, 2,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 3, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 1, 0, 0, 0, 2,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 3, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
};
typedef struct
{ int argc;
char **argv;
FILE *input;
int count;
char *name;
} File_Iterator;
File_Iterator *init_file_iterator(int argc, char **argv, FILE *input, int first)
{ File_Iterator *it;
it = Malloc(sizeof(File_Iterator),"Allocating file iterator");
it->argc = argc;
it->argv = argv;
it->input = input;
if (input == NULL)
it->count = first;
else
{ it->count = 1;
rewind(input);
}
return (it);
}
int next_file(File_Iterator *it)
{ static char nbuffer[MAX_NAME+8];
if (it->input == NULL)
{ if (it->count >= it->argc)
return (0);
it->name = it->argv[it->count++];
}
else
{ char *eol;
if (fgets(nbuffer,MAX_NAME+8,it->input) == NULL)
{ if (feof(it->input))
return (0);
SYSTEM_ERROR;
}
if ((eol = index(nbuffer,'\n')) == NULL)
{ fprintf(stderr,"%s: Line %d in file list is longer than %d chars!\n",
Prog_Name,it->count,MAX_NAME+7);
it->name = NULL;
}
*eol = '\0';
it->count += 1;
it->name = nbuffer;
}
return (1);
}
int main(int argc, char *argv[])
{ FILE *istub, *ostub;
char *dbname;
char *root, *pwd;
FILE *bases, *indx;
int64 boff, ioff;
int ifiles, ofiles;
char **flist;
HITS_DB db;
int ureads;
int64 offset;
FILE *IFILE;
int VERBOSE;
// Usage: [-v] <path:string> ( -f<file> | <input:fasta> ... )
{ int i, j, k;
int flags[128];
ARG_INIT("fasta2DB")
IFILE = NULL;
j = 1;
for (i = 1; i < argc; i++)
if (argv[i][0] == '-')
switch (argv[i][1])
{ default:
ARG_FLAGS("v")
break;
case 'f':
IFILE = fopen(argv[i]+2,"r");
if (IFILE == NULL)
{ fprintf(stderr,"%s: Cannot open file of inputs '%s'\n",Prog_Name,argv[i]+2);
exit (1);
}
break;
}
else
argv[j++] = argv[i];
argc = j;
VERBOSE = flags['v'];
if ((IFILE == NULL && argc <= 2) || (IFILE != NULL && argc != 2))
{ fprintf(stderr,"Usage: %s %s\n",Prog_Name,Usage);
exit (1);
}
}
// Try to open DB file, if present then adding to DB, otherwise creating new DB. Set up
// variables as follows:
// dbname = full name of db = <pwd>/<root>.db
// istub = open db file (if adding) or NULL (if creating)
// ostub = new image of db file (will overwrite old image at end)
// bases = .bps file positioned for appending
// indx = .idx file positioned for appending
// ureads = # of reads currently in db
// offset = offset in .bps at which to place next sequence
// ioff = offset in .idx file to truncate to if command fails
// boff = offset in .bps file to truncate to if command fails
// ifiles = # of .fasta files to add
// ofiles = # of .fasta files already in db
// flist = [0..ifiles+ofiles] list of file names (root only) added to db so far
{ int i;
root = Root(argv[1],".db");
pwd = PathTo(argv[1]);
dbname = Strdup(Catenate(pwd,"/",root,".db"),"Allocating db name");
if (dbname == NULL)
exit (1);
if (IFILE == NULL)
ifiles = argc-2;
else
{ File_Iterator *ng;
ifiles = 0;
ng = init_file_iterator(argc,argv,IFILE,2);
while (next_file(ng))
ifiles += 1;
free(ng);
}
istub = fopen(dbname,"r");
if (istub == NULL)
{ ofiles = 0;
bases = Fopen(Catenate(pwd,PATHSEP,root,".bps"),"w+");
indx = Fopen(Catenate(pwd,PATHSEP,root,".idx"),"w+");
if (bases == NULL || indx == NULL)
exit (1);
fwrite(&db,sizeof(HITS_DB),1,indx);
ureads = 0;
offset = 0;
boff = 0;
ioff = 0;
}
else
{ if (fscanf(istub,DB_NFILE,&ofiles) != 1)
SYSTEM_ERROR
bases = Fopen(Catenate(pwd,PATHSEP,root,".bps"),"r+");
indx = Fopen(Catenate(pwd,PATHSEP,root,".idx"),"r+");
if (bases == NULL || indx == NULL)
exit (1);
if (fread(&db,sizeof(HITS_DB),1,indx) != 1)
SYSTEM_ERROR
fseeko(bases,0,SEEK_END);
fseeko(indx, 0,SEEK_END);
ureads = db.ureads;
offset = ftello(bases);
boff = offset;
ioff = ftello(indx);
}
flist = (char **) Malloc(sizeof(char *)*(ofiles+ifiles),"Allocating file list");
ostub = Fopen(Catenate(pwd,"/",root,".dbx"),"w+");
if (ostub == NULL || flist == NULL)
exit (1);
fprintf(ostub,DB_NFILE,ofiles+ifiles);
for (i = 0; i < ofiles; i++)
{ int last;
char prolog[MAX_NAME], fname[MAX_NAME];
if (fscanf(istub,DB_FDATA,&last,fname,prolog) != 3)
SYSTEM_ERROR
if ((flist[i] = Strdup(fname,"Adding to file list")) == NULL)
goto error;
fprintf(ostub,DB_FDATA,last,fname,prolog);
}
}
{ int maxlen;
int64 totlen, count[4];
int pmax, rmax;
HITS_READ *prec;
char *read;
int c;
File_Iterator *ng;
open_compressed_init();
// Buffer for reads all in the same well
pmax = 100;
prec = (HITS_READ *) Malloc(sizeof(HITS_READ)*pmax,"Allocating record buffer");
if (prec == NULL)
goto error;
// Buffer for accumulating .fasta sequence over multiple lines
rmax = MAX_NAME + 60000;
read = (char *) Malloc(rmax+1,"Allocating line buffer");
if (read == NULL)
goto error;
totlen = 0; // total # of bases in new .fasta files
maxlen = 0; // longest read in new .fasta files
for (c = 0; c < 4; c++) // count of acgt in new .fasta files
count[c] = 0;
// For each new .fasta file do:
ng = init_file_iterator(argc,argv,IFILE,2);
while (next_file(ng))
{ int input;
char *core, *prolog;
char *filename, *intermediate_filename;
int nline, rlen, pcnt;
int pwell;
ssize_t input_read;
const char *suffix;
if (ng->name == NULL) goto error;
// Open it: <path>/<core>.fasta, check that core is not too long,
// and checking that it is not already in flist.
filename = ng->name;
if (find_suffix((const char **)&filename,&suffix) == -1) {
fprintf(stderr,"%s: Could not find file: %s\n",Prog_Name,argv[c]);
goto error;
}
intermediate_filename = suffix != NULL ? Root(filename,suffix) : filename;
core = Root(intermediate_filename,".fasta");
if (intermediate_filename != filename) {
free(intermediate_filename);
}
if (strlen(core) >= MAX_NAME)
{ fprintf(stderr,"%s: File name over %d chars: '%.200s'\n",
Prog_Name,MAX_NAME,core);
goto error;
}
if ((input = open_compressed(filename)) == -1) {
fprintf(stderr, "%s: Could not open: %s\n", Prog_Name, filename);
goto error;
}
{ int j;
for (j = 0; j < ofiles; j++)
if (strcmp(core,flist[j]) == 0)
{ fprintf(stderr,"%s: File %s is already in database %s.db\n",
Prog_Name,filename,Root(argv[1],".db"));
goto error;
}
}
// Get the header of the first line. If the file is empty skip.
pcnt = 0;
rlen = 0;
nline = 1;
input_read = pfgets(input,read,MAX_NAME);
if (input_read == -1)
{ fprintf(stderr,"Skipping '%s', file is empty!\n",filename);
close_compressed(input);
free(core);
continue;
}
// Add the file name to flist
if (VERBOSE)
{ fprintf(stderr,"Adding '%s' ...\n",core);
fflush(stderr);
}
flist[ofiles++] = core;
// Check that the first line has PACBIO format, and record prolog in 'prolog'.
if (read[strlen(read)-1] != '\n')
{ fprintf(stderr,"File %s, Line 1: Fasta line is too long (> %d chars)\n",
filename,MAX_NAME-2);
goto error;
}
// > is fasta format, @ is fastq format
if (read[0] != '>' && read[0] != '@')
{ fprintf(stderr,"File %s, Line 1: First header in fasta file is missing\n",filename);
goto error;
}
const char header_delim = read[0]; // read header marker for file
{ char *find;
int well, beg, end, qv;
find = index(read+1,'/');
if (find != NULL && sscanf(find+1,"%d/%d_%d RQ=0.%d\n",&well,&beg,&end,&qv) >= 3)
{ *find = '\0';
prolog = Strdup(read+1,"Extracting prolog");
*find = '/';
if (prolog == NULL) goto error;
}
else
{ fprintf(stderr,"File %s, Line %d: Pacbio header line format error\n",
filename,nline);
goto error;
}
}
// Read in all the sequences until end-of-file
{ int i, x;
pwell = -1;
while (input_read > 0)
{ int beg, end, clen;
int well, qv;
char *find;
find = index(read+(rlen+1),'/');
if (find == NULL)
{ fprintf(stderr,"File %s, Line %d: Pacbio header line format error\n",
filename,nline);
goto error;
}
*find = '\0';
if (strcmp(read+(rlen+1),prolog) != 0)
{ fprintf(stderr,"File %s, Line %d: Pacbio header line name inconsisten\n",
filename,nline);
goto error;
}
*find = '/';
x = sscanf(find+1,"%d/%d_%d RQ=0.%d\n",&well,&beg,&end,&qv);
if (x < 3)
{ fprintf(stderr,"File %s, Line %d: Pacbio header line format error\n",
filename,nline);
goto error;
}
else if (x == 3)
qv = 0;
// up buffer size if needed
if (rmax < end - beg + MAX_NAME) {
rmax = 1.2 * (end - beg) + MAX_NAME;
read = (char *)realloc(read,rmax+1);
if (read == NULL) {
fprintf(stderr,"File %s, Line %d:",filename,nline);
fprintf(stderr," Out of memory (Allocating line buffer)\n");
goto error;
}
}
rlen = 0;
while (1)
{ input_read = pfgets(input,read+rlen,rmax-rlen);
if (input_read == -1) { // eof
break;
}
x = rlen + input_read;
const int has_trailing_newline = read[x - 1] == '\n';
// lack of newline could be eof, or could be end of buffer
if (has_trailing_newline) {
--x; // backup so newline will get overwritten
++nline;
}
// check to see if we've reached the end of the read
if (read[rlen] == header_delim || (read[rlen] == '+' && header_delim == '@')) {
// make sure header line is complete
if (!has_trailing_newline) {
fprintf(stderr,"File %s, Line %d:",filename,nline);
fprintf(stderr," Fasta line not terminated, possible file corruption\n");
goto error;
}
if (read[rlen] == '+') {
char buf[MAX_NAME];
// skip fastq quality
while ((input_read = pfgets(input,buf,MAX_NAME)) != -1 && buf[input_read - 1] != '\n') { }
if (input_read == -1) {
fprintf(stderr,"File %s, Line %d:",filename,nline);
fprintf(stderr," Fasta line not terminated, possible file corruption\n");
goto error;
}
++nline;
// now read in header line
input_read = pfgets(input, buf, MAX_NAME);
if (input_read != -1) { // not eof, copy in header
if (buf[0] != header_delim || buf[input_read - 1] != '\n') {
fprintf(stderr, "File %s, Line %d: Incorrect read header: %s\n", filename, nline, buf);
goto error;
}
++nline;
memcpy(read + rlen, buf, input_read + 1);
}
}
break;
}
rlen = x;
if (rlen + MAX_NAME > rmax)
{ rmax = ((int) (1.2 * rmax)) + 1000 + MAX_NAME;
read = (char *) realloc(read,rmax+1);
if (read == NULL)
{ fprintf(stderr,"File %s, Line %d:",filename,nline);
fprintf(stderr," Out of memory (Allocating line buffer)\n");
goto error;
}
}
}
read[rlen] = '\0';
for (i = 0; i < rlen; i++)
{ x = number[(int) read[i]];
count[x] += 1;
read[i] = (char) x;
}
ureads += 1;
totlen += rlen;
if (rlen > maxlen)
maxlen = rlen;
prec[pcnt].origin = well;
prec[pcnt].fpulse = beg;
prec[pcnt].rlen = rlen;
prec[pcnt].boff = offset;
prec[pcnt].coff = -1;
prec[pcnt].flags = qv;
Compress_Read(rlen,read);
clen = COMPRESSED_LEN(rlen);
fwrite(read,1,clen,bases);
offset += clen;
if (pwell == well)
{ prec[pcnt].flags |= DB_CSS;
pcnt += 1;
if (pcnt >= pmax)
{ pmax = ((int) (pcnt*1.2)) + 100;
prec = (HITS_READ *) realloc(prec,sizeof(HITS_READ)*pmax);
if (prec == NULL)
{ fprintf(stderr,"File %s, Line %d: Out of memory",filename,nline);
fprintf(stderr," (Allocating read records)\n");
goto error;
}
}
}
else if (pcnt == 0)
pcnt += 1;
else
{ x = 0;
for (i = 1; i < pcnt; i++)
if (prec[i].rlen > prec[x].rlen)
x = i;
prec[x].flags |= DB_BEST;
fwrite(prec,sizeof(HITS_READ),pcnt,indx);
prec[0] = prec[pcnt];
pcnt = 1;
}
pwell = well;
}
// Complete processing of .fasta file: flush last well group, write file line
// in db image, free prolog, and close file
x = 0;
for (i = 1; i < pcnt; i++)
if (prec[i].rlen > prec[x].rlen)
x = i;
prec[x].flags |= DB_BEST;
fwrite(prec,sizeof(HITS_READ),pcnt,indx);
fprintf(ostub,DB_FDATA,ureads,core,prolog);
}
free(prolog);
close_compressed(input);
if (filename != ng->name) {
free(filename);
}
}
// Finished loading all sequences: update relevant fields in db record
db.ureads = ureads;
if (istub == NULL)
{ for (c = 0; c < 4; c++)
db.freq[c] = (float) ((1.*count[c])/totlen);
db.totlen = totlen;
db.maxlen = maxlen;
db.cutoff = -1;
}
else
{ for (c = 0; c < 4; c++)
db.freq[c] = (float) ((db.freq[c]*db.totlen + (1.*count[c]))/(db.totlen + totlen));
db.totlen += totlen;
if (maxlen > db.maxlen)
db.maxlen = maxlen;
}
open_compressed_finish();
}
// If db has been previously partitioned then calculate additional partition points and
// write to new db file image
if (db.cutoff >= 0)
{ int64 totlen, dbpos, size;
int nblock, ireads, tfirst, rlen;
int ufirst, cutoff, allflag;
HITS_READ record;
int i;
if (VERBOSE)
{ fprintf(stderr,"Updating block partition ...\n");
fflush(stderr);
}
// Read the block portion of the existing db image getting the indices of the first
// read in the last block of the exisiting db as well as the partition parameters.
// Copy the old image block information to the new block information (except for
// the indices of the last partial block)
if (fscanf(istub,DB_NBLOCK,&nblock) != 1)
SYSTEM_ERROR
dbpos = ftello(ostub);
fprintf(ostub,DB_NBLOCK,0);
if (fscanf(istub,DB_PARAMS,&size,&cutoff,&allflag) != 3)
SYSTEM_ERROR
fprintf(ostub,DB_PARAMS,size,cutoff,allflag);
if (allflag)
allflag = 0;
else
allflag = DB_BEST;
size *= 1000000ll;
nblock -= 1;
for (i = 0; i <= nblock; i++)
{ if (fscanf(istub,DB_BDATA,&ufirst,&tfirst) != 2)
SYSTEM_ERROR
fprintf(ostub,DB_BDATA,ufirst,tfirst);
}
// Seek the first record of the last block of the existing db in .idx, and then
// compute and record partition indices for the rest of the db from this point
// forward.
fseeko(indx,sizeof(HITS_DB)+sizeof(HITS_READ)*ufirst,SEEK_SET);
totlen = 0;
ireads = 0;
for (i = ufirst; i < ureads; i++)
{ if (fread(&record,sizeof(HITS_READ),1,indx) != 1)
SYSTEM_ERROR
rlen = record.rlen;
if (rlen >= cutoff && (record.flags & DB_BEST) >= allflag)
{ ireads += 1;
tfirst += 1;
totlen += rlen;
if (totlen >= size)
{ fprintf(ostub," %9d %9d\n",i+1,tfirst);
totlen = 0;
ireads = 0;
nblock += 1;
}
}
}
if (ireads > 0)
{ fprintf(ostub,DB_BDATA,ureads,tfirst);
nblock += 1;
}
db.treads = tfirst;
fseeko(ostub,dbpos,SEEK_SET);
fprintf(ostub,DB_NBLOCK,nblock); // Rewind and record the new number of blocks
}
else
db.treads = ureads;
rewind(indx);
fwrite(&db,sizeof(HITS_DB),1,indx); // Write the finalized db record into .idx
rewind(ostub); // Rewrite the number of files actually added
fprintf(ostub,DB_NFILE,ofiles);
if (istub != NULL)
fclose(istub);
fclose(ostub);
fclose(indx);
fclose(bases);
rename(Catenate(pwd,"/",root,".dbx"),dbname); // New image replaces old image
exit (0);
// Error exit: Either truncate or remove the .idx and .bps files as appropriate.
// Remove the new image file <pwd>/<root>.dbx
error:
if (ioff != 0)
{ fseeko(indx,0,SEEK_SET);
if (ftruncate(fileno(indx),ioff) < 0)
SYSTEM_ERROR
}
if (boff != 0)
{ fseeko(bases,0,SEEK_SET);
if (ftruncate(fileno(bases),boff) < 0)
SYSTEM_ERROR
}
fclose(indx);
fclose(bases);
if (ioff == 0)
unlink(Catenate(pwd,PATHSEP,root,".idx"));
if (boff == 0)
unlink(Catenate(pwd,PATHSEP,root,".bps"));
if (istub != NULL)
fclose(istub);
fclose(ostub);
unlink(Catenate(pwd,"/",root,".dbx"));
exit (1);
}