summaryrefslogtreecommitdiff
path: root/cvs_files.c
blob: 0ec167fd0b64904c95c2e2efb857b2e71efd4d56 (plain)
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
/**********************************************************
 * cvs_files.c
 *
 * Copyright (C) 2004, 2005 by Stefan Siegl <stesie@brokenpipe.de>, Germany
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Publice License,
 * version 2 or any later. The license is contained in the COPYING
 * file that comes with the cvsfs distribution.
 *
 * download arbitrary revisions from cvs host and cache them locally
 */

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <string.h>
#include <stdlib.h>
#include <malloc.h>
#include <time.h>
#include <sys/stat.h>
#include <fcntl.h>

#ifdef HAVE_LIBZ
#  include <zlib.h>
#endif

#include "cvsfs.h"
#include "cvs_files.h"
#include "cvs_connect.h"

/* cvs_files_print_path_recursively
 *
 * recurse from dir upwards to the root node and write out the directories'
 * names to cvs_handle as falling back.
 */
static void
cvs_files_print_path_recursively(FILE *cvs_handle, struct netnode *dir)
{
  if(dir->parent)
    cvs_files_print_path_recursively(cvs_handle, dir->parent);
  fprintf(cvs_handle, "%s/", dir->name);
}


/* cvs_files_cvsattr_to_mode_t
 *
 * convert cvs mode string to posix style mode_t
 */
static mode_t
cvs_files_cvsattr_to_mode_t(const char *ptr)
{
  const mode_t modes[9] =
    { S_IRUSR, S_IWUSR, S_IXUSR,
      S_IRGRP, S_IWGRP, S_IXGRP,
      S_IROTH, S_IWOTH, S_IXOTH
    };
  const mode_t *stage = modes;
  mode_t perm = 0;

  for(;;ptr ++)
    switch(*ptr)
      {
      case 'u': stage = &modes[0]; break;
      case 'g': stage = &modes[3]; break;
      case 'o': stage = &modes[6]; break;
      case '=': break;
      case ',': break;
      case 'r': perm |= stage[0]; break;
      case 'w': perm |= stage[1]; break;
      case 'x': perm |= stage[2]; break;

      default:
	return perm;
      }
}	  


/* gzip flag byte */
#define ASCII_FLAG   0x01 /* bit 0 set: file probably ascii text */
#define HEAD_CRC     0x02 /* bit 1 set: header CRC present */
#define EXTRA_FIELD  0x04 /* bit 2 set: extra field present */
#define ORIG_NAME    0x08 /* bit 3 set: original file name present */
#define COMMENT      0x10 /* bit 4 set: file comment present */
#define RESERVED     0xE0 /* bits 5..7: reserved */

/* cvs_files_gzip_check_header
 *
 * check the gzip header for validity and move the pointers as necessary.
 * RETURN: 0 on success, 1 if not enough bytes we available or a system error
 * code on trouble
 */
#ifdef HAVE_LIBZ
static int
cvs_files_gzip_check_header(char **data, uInt *len)
{
  static const char gzip_magic[2] = {0x1f, 0x8b};
  uInt pos = 10;
  int method;
  int flags;

  if(*len < 10) 
    return 1; /* not enough bytes for the header */

  if((*data)[0] != gzip_magic[0] || (*data)[1] != gzip_magic[1])
    return EIO; /* not a gzip magic */

  method = (*data)[2];
  flags = (*data)[3];
  if(method != Z_DEFLATED || (flags & RESERVED))
    return EIO; /* not deflated or reserved flags set, what about it?? */

  /* bytes 4..9 are time, xflags and os type. ignore 'em ... */

  if(flags & EXTRA_FIELD)
    {
      int field_len;

      if(*len < pos + 2) return 1; /* not enough data */

      field_len = (*data)[pos ++];
      field_len = (*data)[pos ++];

      if(*len < pos + field_len) return 1; /* not enough data
					    * TODO if extra_field is larger
					    * than our gzip in buffer, we
					    * got a problem ...
					    */
      pos =+ field_len;
    }
  
  if(flags & ORIG_NAME)
    do
      /* skip the original file name ... */
      if(*len < pos + 1) return 1; /* not enough data */
    while((*data)[pos ++]);

  if(flags & COMMENT)
    do
      /* skip the comment ... */
      if(*len < pos + 1) return 1; /* not enough data */
    while((*data)[pos ++]);

  if(flags & HEAD_CRC)
    {
      /* skip two bytes, i.e. header's crc */
      if(*len < pos + 2) return 1; /* not available */
      pos += 2;
    }

  /* okay, we got it, therefore adjust *len and *data accordingly */
  *len -= pos;
  *data += pos;
  return 0;
}
#endif /* HAVE_LIBZ */



/* cvs_files_gzip_inflate
 *
 * gzip inflate given number of bytes from file-handle into content
 * return 0 on success.
 */
#if HAVE_LIBZ
static error_t
cvs_files_gzip_inflate(FILE *recv, size_t bytes, char **content, size_t *len)
{
  char input[4096]; /* input buffer */
  size_t read;
  z_stream z;
  int got_header = 0;
  
  /* initialize zlib decompression */
  z.next_in = ((Bytef *)input);
  z.avail_in = 0;
  z.next_out = NULL;
  z.zalloc = Z_NULL;
  z.zfree = Z_NULL;
  z.opaque = Z_NULL;

  switch(inflateInit2(&z, -MAX_WBITS))
    {
    case Z_OK:
      break; /* it worked, perfect. */

    case Z_MEM_ERROR:
      return ENOMEM;

    case Z_VERSION_ERROR:
      fprintf(stderr, PACKAGE ": incompatible zlib installed.\n");
      return EIEIO;

    default:
      return EIO;
    }

  /* read 'bytes' bytes from reader handle, but not more than we can store
   * to input buffer ...
   */
  while(bytes && (read = sizeof(input) -
		  ((char*)z.next_in - input) - z.avail_in)
	&& (read = fread(z.next_in + z.avail_in, 1,
			 read < bytes ? read : bytes, recv)))
    {
      bytes -= read;
      z.avail_in += read;

      /* check whether there is a correct gzip header */
      if(! got_header)
	switch(cvs_files_gzip_check_header((char **)&z.next_in, &z.avail_in))
	  {
	  case 0: /* success */
	    got_header = 1;
	    break;

	  case 1: /* not enough data */
	    continue;

	  default:
	    inflateEnd(&z);

	    if(z.next_out)
	      free(z.next_out - z.total_out);

	    return EIO;
	  }	  

      /* enlare output buffer, if necessary */
      if(! z.next_out || z.avail_out < (z.avail_in << 2))
	{
	  void *ptr;
	  size_t alloc = z.next_out ? z.total_out << 1 : 16384;

	  ptr = realloc(z.next_out - z.total_out, alloc);
	  if(! ptr)
	    {
	      inflateEnd(&z);
	      return ENOMEM;
	    }

	  z.next_out = ptr + z.total_out;
	  z.avail_out = alloc - z.total_out;
	}

      /* inflate data now */
      switch(inflate(&z, Z_NO_FLUSH))
	{
	case Z_OK:
	case Z_STREAM_END:
	case Z_BUF_ERROR: /* this is not fatal, just not enough
			   * memory in output buffer
			   */
	  break;

	case Z_NEED_DICT:
	case Z_DATA_ERROR:
	case Z_STREAM_ERROR:
	  inflateEnd(&z);

	  if(z.next_out)
	    free(z.next_out - z.total_out);

	  return EIO;
		  
	case Z_MEM_ERROR:
	default:
	  inflateEnd(&z);

	  if(z.next_out)
	    free(z.next_out - z.total_out);

	  return ENOMEM;
	}

      /* discard inflated bits from the input buffer ... */
      memmove(input, z.next_in, sizeof(input) - ((char *)z.next_in - input));
      z.next_in = ((Bytef *)input);
    }

  if(bytes)
    {
      /* unable to read all data ... */
      inflateEnd(&z);

      if(z.next_out)
	free(z.next_out - z.total_out);

      return EIO;
    }

  while(z.avail_in)
    {
      /* data left in input buffer, call inflate to care for that */
      void *ptr;
      size_t alloc = z.total_out + (z.avail_in << 2);

      ptr = realloc(z.next_out - z.total_out, alloc);
      if(! ptr)
	{
	  inflateEnd(&z);
	  return ENOMEM;
	}

      z.next_out = ptr + z.total_out;
      z.avail_out = alloc - z.total_out;

      switch(inflate(&z, Z_FINISH))
	{
	case Z_STREAM_END:
	  z.avail_in = 0; /* okay, we're done, forget the trailing bits */

	case Z_OK:
	case Z_BUF_ERROR: /* this is not fatal, just not enough
			   * memory in output buffer
			   */
	  break;
	  
	case Z_NEED_DICT:
	case Z_DATA_ERROR:
	case Z_STREAM_ERROR:
	  inflateEnd(&z);

	  if(z.next_out)
	    free(z.next_out - z.total_out);

	  return EIO;
		  
	case Z_MEM_ERROR:
	default:
	  inflateEnd(&z);

	  if(z.next_out)
	    free(z.next_out - z.total_out);

	  return ENOMEM;
	}
    }

  inflateEnd(&z);

  /* okay, processed data should be at z.next_out - z.total_out */
  free(*content);
  *content = realloc(z.next_out - z.total_out, z.total_out);
  *len = z.total_out;

  return 0;
}
#endif /* HAVE_LIBZ */



/* cvs_files_cache
 *
 * Download the revision (as specified by rev) of the specified file. 
 */
error_t
cvs_files_cache(struct netnode *file, struct revision *rev)
{
  FUNC_PROLOGUE_FMT("cvs_files_cache", "file=%s, rev=%s", file->name, rev->id);
  FILE *send, *recv;
  char buf[1024]; /* 1k should be enough for most cvs repositories, if
		   * cvsfs tell's you to increase this value, please do so.
		   *
		   * TODO in the far future we may have a fgets-thingy, that
		   * allocates it's memory dynamically, which occurs to be
		   * a goodthing(TM) ...
		   */

  if(rev != file->revision)
    {
      /* we're not retrieving the head revision, thus it is likely that
       * the HEAD revision was sent over the line in this session. However
       * cvs server tends to transmit only one 'Mod-time' per file and 
       * session.
       *
       * therefore get a fresh connection ...
       */
      if(cvs_connect_fresh(&send, &recv))
	FUNC_RETURN(EIO);
    }
  else if(cvs_connect(&send, &recv))
    FUNC_RETURN(EIO);

  /* write out request header */
  fprintf(send,
	  "Argument -l\n" /* local dir only */
	  "Argument -N\n" /* don't shorten module names */
	  "Argument -P\n" /* no empty directories */
	  "Argument -d\nArgument .\n" /* checkout to local dir */
	  "Argument -r\nArgument %s\n"
	  "Argument --\n"
	  "Argument ",
	  rev->id);

  /* write out pathname from rootnode on ... */
  cvs_files_print_path_recursively(send, file->parent);

  /* last but not least write out the filename */
  fprintf(send, "%s\n", file->name);

  /* okay, send checkout command ... */
  fprintf(send,
	  "Directory .\n%s\n" /* cvsroot */
	  "co\n", config.cvs_root);

  /* example response:
   * *** SERVER ***
   * Mod-time 8 Sep 2004 17:05:43 -0000
   * Updated ./wsdebug/
   * /home/cvs/wsdebug/debug.c
   * /debug.c/1.1///T1.1
   * u=rw,g=rw,o=rw
   * 6285
   * [content]
   */

  while(fgets(buf, sizeof(buf), recv))
    {
      char *ptr;
      int buflen = strlen(buf);

      ptr = buf + buflen;
      ptr --;

      if(*ptr != 10)
	{
	  fprintf(stderr, PACKAGE "cvs_files_cache's parse buffer is "
		  "too small, stopping for the moment.\n");
	  exit(10);
	}

      if(buf[0] == '/')
	continue; /* just file name stuff, as we request only one file
		   * we don't have to care for that ...
		   */

      if(! strncmp(buf, "Mod-time ", 9))
	{
	  struct tm tm;
	  time_t t = 0;

	  memset(&tm, 0, sizeof(tm));
	  if(strptime(&buf[9], "%d %b %Y %T ", &tm))
	    t = mktime(&tm);

	  rev->time = t;
	  continue;
	}

      if(! strncmp(buf, "Updated ", 8))
	continue; /* pathname of parent directory, don't give a fuck ... */

      if(buf[0] == 'M')
	continue; /* probably something like 'M U <filename>' */

      if(buf[0] == 'u' && buf[1] == '=')
	rev->perm = cvs_files_cvsattr_to_mode_t(buf);

#if HAVE_LIBZ
      else if(buf[0] == 'z')
	{
	  /* okay, we'll see a gzipped data stream 
	   * the number tells us the number of bytes we will retrieve,
	   * not the size of the inflated file!
	   */
	  size_t bytes = atoi(buf + 1);
	  error_t err;
	  
	  if(! bytes)
	    {
	      cvs_connection_kill(send, recv);
	      FUNC_RETURN(EIO); /* this should not happen, empty file?? */
	    }

	  if((err = cvs_files_gzip_inflate(recv, bytes,
					   &rev->contents, &rev->length)))
	    {
	      cvs_connection_kill(send, recv);
	      FUNC_RETURN(err);
	    }
	}
#endif /* HAVE_LIBZ */

      else if(buf[0] >= '0' && buf[0] <= '9')
	{
	  /* okay, this tells the length of our file.
	   * the file is transmitted without compression applied
	   */
	  size_t read;
	  size_t length = atoi(buf);
	  size_t bytes = length;
	  char *content = malloc(bytes);
	  char *ptr = content;

	  if(! content)
	    {
	      perror(PACKAGE);
	      cvs_connection_kill(send, recv);
	      FUNC_RETURN(ENOMEM);
	    }

	  while(bytes && (read = fread(ptr, 1, bytes, recv)))
	    {
	      bytes -= read;
	      ptr += read;
	    }

	  if(bytes)
	    {
	      /* unable to read all data ... */
	      fprintf(stderr, "unable to read whole file %s\n", file->name);
	      free(content);
	      cvs_connection_kill(send, recv);
	      FUNC_RETURN(EIO);
	    }

	  free(rev->contents);
	  rev->length = length;
	  rev->contents = content;
	}
      else if(! strncmp(buf, "ok", 2))
	{
	  cvs_connection_release(send, recv);
	  return 0; /* seems like everything went well ... */
	}
      else if(buf[0] == 'E')
	{
	  cvs_treat_error(recv, buf);
	  cvs_connection_release(send, recv);
	  return EIO;
	}
      else if(! strncmp(buf, "error", 5))
	{
	  cvs_connection_release(send, recv);
	  return EIO;
	}
      else
	break; /* fuck, what the hell is going on here?? get outta here! */
    }

  /* well, got EOF, that shouldn't ever happen ... */
  cvs_connection_kill(send, recv);
  FUNC_EPILOGUE(EIO);
}



/* cvs_files_hit
 *
 * ask cvs server whether there is a particular revision (as specified by rev)
 * available. return 0 if yes, ENOENT if not. EIO on communication error.
 */
error_t
cvs_files_hit(struct netnode *file, struct revision *rev)
{
  FILE *send, *recv;
  unsigned short int got_something = 0;

  char buf[4096]; /* 4k should be enough for most cvs repositories, if
		   * cvsfs tell's you to increase this value, please do so.
		   */

  if(cvs_connect(&send, &recv))
    return EIO;

  /* write out request header */
  fprintf(send,
	  "UseUnchanged\n"
	  "Argument -s\n"
	  "Argument -r\nArgument 0\n"
	  "Argument -r\nArgument %s\n"
	  "Argument ",
	  rev->id);

  /* write out pathname from rootnode on ... */
  cvs_files_print_path_recursively(send, file->parent);

  /* last but not least write out the filename */
  fprintf(send, "%s\n", file->name);

  /* we need an rdiff ... */
  fprintf(send, "rdiff\n");

  /* okay, now read the server's response, which either is something
   * "M" char padded or an E, error couple.
   */
  while(fgets(buf, sizeof(buf), recv))
    {
      if(! strncmp(buf, "ok", 2))
	{
	  cvs_connection_release(send, recv);

	  if(! got_something)
	    return ENOENT; /* no content, sorry. */

	  return 0; /* jippie, looks perfectly, he? */
	}

      if(! strncmp(buf, "error", 5))
	{
	  cvs_connection_release(send, recv);
	  return EIO;
	}

      if(buf[1] != ' ') 
	{
	  cvs_treat_error(recv, buf);
	  cvs_connection_release(send, recv);
	  return EIO; /* hm, doesn't look got for us ... */
	}

      switch(buf[0])
	{
	case 'E':
	  /* cvs_treat_error(recv, buf);
	   * cvs_connection_release(send, recv);
	   * return EIO;
	   *
	   * don't call cvs_treat_error since it's probably a
	   * "no such tag %s" message ...
	   */
	  break;

	case 'M':
	  got_something = 1;
	  break;
	  
	default:
	  cvs_treat_error(recv, buf);
	  cvs_connection_release(send, recv);
	  return EIO;
	}
    }

  /* well, got EOF, that shouldn't ever happen ... */
  cvs_connection_kill(send, recv);
  return EIO;
}