File: rasmain.c

package info (click to toggle)
ras 1.03-2.1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny
  • size: 264 kB
  • ctags: 113
  • sloc: ansic: 1,085; sh: 481; makefile: 68
file content (453 lines) | stat: -rw-r--r-- 12,549 bytes parent folder | download | duplicates (3)
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
/*
    ras - Redundant Archive System
    Copyright (C) 1999  Nick Cleaton

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA

    Nick Cleaton <nick@cleaton.net>
*/

#include <stdio.h>
#include <stdlib.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <ctype.h>
#include <errno.h>
#include <string.h>
#include <stddef.h>

#include "common.h"
#include "utils.h"
#include "field.h"
#include "applysum.h"
#include "options.h"
#include "segfname.h"

/****************************************************************************/

static int choosesum_index;

/****************************************************************************/

void restore_missing_segments(int argc, char** argv);
void generate_sums(int argc, char **argv);

int main(int argc, char **argv)
{ 
   int arg_offset;

   arg_offset = get_options(argc, argv);
   choosesum_index = opt_offset;

   init_unlink_list();
   init_field();

   if( opt_create_sums )
      generate_sums(argc-arg_offset, argv+arg_offset);
   else
      restore_missing_segments(argc-arg_offset, argv+arg_offset);

   free_field();
   return 0;
}

/****************************************************************************
**
**  Restoring any missing segfiles  
*/

int open_sumfile(FILE **f, int *xval, char *filename);
int already_got_xval(struct inblkstream *infiles, size_t ifcount, int xval);

void restore_missing_segments(int argc, char** argv)
{
   int i, first_file_opened, arg, ifcount, ofcount;
   struct outblkstream *outfiles;
   struct inblkstream *infiles;
   FILE *current_file;
   int current_xval;


   if( get_segment_count() == -1 )
   {  /*
	 segcount not specified as a command line option, so we must open the 
	 first sumfile specified on the command line to find out how many
	 segments there are in the archive.
       */  
      if( opt_no_header )
      {  fprintf(stderr,PROGNAME
          ": use the -n option to specify segcount when using the -H option\n");
	 exit_because_of_error(INVALID_INPUT);
      }
      if( argc < 1 )
      {  
	 fprintf(stderr,PROGNAME ": no sumfiles specified\n");
	 exit_because_of_error(INVALID_INPUT);
      }
      open_sumfile( &current_file, &current_xval, argv[0] );
      first_file_opened = 1;
   }
   else
      first_file_opened = 0;


   /*
      Now we know how many segments there are, we have an upper bound on the 
      amount of memory needed for inblkstreams and outblkstreams...
    */  
   infiles = Malloc( get_segment_count() * sizeof(*infiles) );
   outfiles = Malloc( get_segment_count() * sizeof(*outfiles) );


   /*
      Go through the segment files, existing ones become inputs and missing
      ones become outputs.
    */  
   ofcount = 0;
   ifcount = 0;
   for( i=0 ; i<get_segment_count() ; i++ )
   {  
      infiles[ifcount].f = fopen( get_segment_filename(i), "rb" );
      if( infiles[ifcount].f )
      {  /* an existing segment */
         infiles[ifcount].name = get_segment_filename(i);
         infiles[ifcount].x_val = i;
	 infiles[ifcount].is_sum = 0;
         ifcount++;
      }
      else
      {  /* a missing segment */
         outfiles[ofcount].name = get_segment_filename(i);
         outfiles[ofcount].f = open_output_file( get_segment_filename(i) );
	 outfiles[ofcount].x_val = i;
	 outfiles[ofcount].is_sum = 0;
	 ofcount++;
	 if( opt_verbose )
	 {  printf(PROGNAME ": segfile %s missing\n", get_segment_filename(i));
	 }
      }
   }


   if( ofcount == 0 )
   {  if( !opt_silent )
      {  printf(PROGNAME ": no segment files missing, nothing to do\n");
      }
      free(infiles);
      free(outfiles);
      return;
   }


   /*
      Now the sumfiles (all inputs)
    */
   arg = 0; 
   while( (arg < argc) && (ifcount < get_segment_count()) )
   {  
      if( arg>0 || !first_file_opened )
         open_sumfile( &current_file, &current_xval, argv[arg] );
      
      if( already_got_xval(infiles, ifcount, current_xval) )
      {  fclose(current_file);
         if( opt_verbose )
	 {  printf(PROGNAME ": dropping redundant sumfile %s\n", argv[arg]);
	 }
      }
      else
      {  infiles[ifcount].name = argv[arg];
	 infiles[ifcount].f = current_file;
	 infiles[ifcount].is_sum = 1;
	 infiles[ifcount].x_val = current_xval;
	 ifcount++;
      }

      arg++;
   }

   if( ifcount < get_segment_count() )
   {  fprintf(stderr,PROGNAME ": too few distinct sumfiles\n");
      exit_because_of_error(NOT_ENOUGH);
   }
   else if( !opt_silent )
   {  printf(PROGNAME ": rebuilding %i of %i segments using %i sums\n",
                                          ofcount, get_segment_count(), argc);
   }


   /*
      Regenerate the missing files
    */
   process_data(ifcount, infiles, ofcount, outfiles, opt_blocksize, 
                                                             !opt_no_footer);


   free(infiles);
   free(outfiles);
}

/*****************************************************************************/

int get_sumID_from_filename(char *filename);

int open_sumfile(FILE **f, int *xval, char *filename)
{
   int segcount, sumID;
   unsigned char headbuf[5];
   
   *f = fopen(filename, "rb");
   if( !(*f) )
   {  fprintf(stderr,PROGNAME ": unable to open sumfile %s for read: %s\n", 
                                                 filename, strerror(errno));
      exit_because_of_error(EXTERNAL_FAILURE);
   }

   if( !opt_no_header )
   {  
      if( fread(headbuf, 1, 5, *f) != 5 )
      {  if( ferror(*f) )
	 {  fprintf(stderr,PROGNAME ": read error on sumfile %s: %s\n",
						    filename, strerror(errno));
	    exit_because_of_error(EXTERNAL_FAILURE);
	 }
	 else
	 {  fprintf(stderr,PROGNAME
	                        ": unexpected EOF in sumfile %s\n", filename);
	    exit_because_of_error(INVALID_INPUT);
	 }
      }

      if(   headbuf[0] != 'S' || headbuf[1] != 'U'
	 || (headbuf[2] != 'M' && headbuf[2] != '8')     )
      {  fprintf(stderr,PROGNAME ": %s isn't a sumfile\n", filename);
	 exit_because_of_error(INVALID_INPUT);
      }

      if( headbuf[2] == 'M' )
      {  fprintf(stderr,
	    PROGNAME ": This is ras version " PROGVERSION 
	             ". Sumfile %s was generated by ras 0.90,\n"
	    PROGNAME
	          ": I can't use it. Version 0.90 is still available, "
		  "mail nick@cleaton.net\n"
	    PROGNAME ": if you have trouble getting hold of it.\n", filename
	  );
	 exit_because_of_error(INVALID_INPUT);
      }

      segcount = (int) (headbuf[3]);
      register_segment_count( segcount, "sumfile header" );

      sumID = (int) (headbuf[4]);
   }
   else
      sumID = get_sumID_from_filename(filename);

   if( sumID<0 || sumID > (255-get_segment_count()) )
   {  fprintf(stderr,PROGNAME ": invalid ID of %i in sumfile %s\n",
                                                            sumID, filename);
      exit_because_of_error(INVALID_INPUT);
   }
   *xval = sumID + get_segment_count();

   return get_segment_count();
}
 
/*****************************************************************************/

int already_got_xval(struct inblkstream *infiles, size_t ifcount, int xval)
{
   int i;

   for( i=0 ; i<ifcount ; i++ )
   {  if( infiles[i].x_val == xval )
         return 1;
   }

   return 0;
}

/*****************************************************************************/

int get_sumID_from_filename(char *filename)
{
   char *p;

   p = filename;
   while( !isdigit(*p) )
   {  if( !*p )
      {  fprintf(stderr,PROGNAME ": unable to infer sumfile ID from "
                                 "filename %s\n", filename);
         exit_because_of_error(INVALID_INPUT);
      }
      p++;
   }

   return atoi(p);
}



/****************************************************************************
**
**  Generating sumfiles from segfiles
*/

void init_sum_outfile(char *spec, struct outblkstream *of);

void generate_sums(int argc, char **argv)
{
   int segs, i;
   struct inblkstream infiles[MAX_SEGMENT_COUNT];
   struct outblkstream *outfiles;

   if( argc < 1 )
   {  fprintf(stderr, PROGNAME ": too few sumfiles on command line\n");
      exit_because_of_error(INVALID_INPUT);
   }

   outfiles = Malloc( argc * sizeof(*outfiles) );

   /*
    Open all the input files and initialise the inblkstreams for process_data.
    If the number of segments has not been specified in an option, we must
    must work it out by seeing how many of the segment files exist.
    */

   if( get_segment_count() == -1 )
      segs = MAX_SEGMENT_COUNT;
   else
      segs = get_segment_count();
      
   for( i=0 ; i<segs ; i++ )
   {  
      infiles[i].f = fopen( get_segment_filename(i), "rb" );
      if( infiles[i].f )
      {  infiles[i].name = get_segment_filename(i);
         infiles[i].is_sum = 0;
	 infiles[i].x_val = i;
      }
      else
      {  if( get_segment_count() == -1 )
         {  register_segment_count(i, "number of seg files found");
	    break;
	 }
	 else
	 {  fprintf(stderr,PROGNAME ": can't open seg file %s for read: %s\n",
	                            get_segment_filename(i), strerror(errno));
	    exit_because_of_error(INVALID_INPUT);
	 }
      }
   }

   if( get_segment_count() == -1 )
   {  /* We fell out the bottom of the for loop */
      register_segment_count(MAX_SEGMENT_COUNT, "number of segfiles found");
   }
    
   /*
    Now we know how many segments there are we can decide what sums to
    generate and write the headers to the output sumfiles.
   */
   for( i=0 ; i<argc ; i++ )
      init_sum_outfile( argv[i], outfiles + i );
      

   if( !opt_silent )
   {  printf(PROGNAME ": generating %i sumfiles of %i segments\n", argc,
                                                      get_segment_count());
   }
      
   /*
    And finally ....
    */
   process_data(get_segment_count(), infiles, argc, outfiles,
                                            opt_blocksize, !opt_no_footer);

   free( outfiles );
}

/**************************************************************************/

void init_sum_outfile(char *spec, struct outblkstream *of)
{
   char *filename, *s, *heapstr;
   uchar head[5];
   int coloncount, sumID;

   heapstr = Strdup(spec);  /* wish to modify it in situ */
   s = heapstr;

   /*
      Determine the output filename and the sum to be generated from
      the command line argument
    */  
   coloncount = countchar(s, ':'); 
   switch ( coloncount )
   {  case 0: /* filename only specified */
         filename = s;
	 sumID = choosesum_index++;
	 break;
      case 1: /* filename + x value specified */
         filename = popfirstword( &s, ':' );
	 sumID = atoi(s);
	 choosesum_index++;
         break;
      default: 
         fprintf(stderr,PROGNAME ": invalid sumspec %s\n", spec);
	 exit_because_of_error(INVALID_INPUT);
	 filename = NULL;  /* to silence a compiler warning */
	 sumID = 0;
   }

   if( sumID<0 || sumID > (255-get_segment_count()) )
   {  fprintf(stderr,PROGNAME ": invalid ID of %i for sumfile %s\n",
                                                            sumID, filename);
      exit_because_of_error(INVALID_INPUT);
   }


   /*
      initialise the outblkstream
    */   
   of->is_sum = 1;
   of->f = open_output_file( nofree(Strdup(filename)) );
   of->name = nofree(Strdup(filename));
   of->x_val = sumID + get_segment_count();
	       
   if( !opt_no_header )	       
   {  /*
       Write the header to the sumfile
       */
      head[0] = 'S'; head[1] = 'U'; head[2] = '8';
      head[3] = fit_int_in_uchar( get_segment_count() );
      head[4] = fit_int_in_uchar( sumID );
      if( fwrite(head, 1, 5, of->f) != 5 )
      {  perror(PROGNAME ": writing header to sum file");
	 exit_because_of_error(EXTERNAL_FAILURE);
      }
   }

   if( opt_verbose )
   {  printf(PROGNAME ": writing sumfile %s, using ID %i\n", filename,
                                                                  sumID);
   }

   free( heapstr );
}

/**************************************************************************/