summaryrefslogtreecommitdiff
path: root/cvs_files.c
blob: e812deace8d90abb252a8acb89bbd7bde765463d (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
/**********************************************************
 * cvs_files.c
 *
 * Copyright 2004, Stefan Siegl <ssiegl@gmx.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 cvsfs4hurd distribution.
 *
 * download arbitrary revisions from cvs host and cache them locally
 */

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

#define PACKAGE "cvsfs"
#define VERSION "0.1"

#include <string.h>
#include <stdlib.h>
#include <malloc.h>

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

/* 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)
{
  FILE *send, *recv;
  char *content = NULL;
  unsigned int content_len = 0;
  unsigned int content_alloc = 0;
  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.
		   *
		   * 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(cvs_connect(&send, &recv))
    return EIO;

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

  /* now write out the path to our file, recurse up to the root-node and
   * start writing out each directory's name
   */
  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_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 a diff, having
   * a "M" char in front of each line, or an E, error couple.
   */
  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);
	}

      /* chop the linefeed off the end */
      *ptr = 0;

      if(! strncmp(buf, "ok", 2))
	{
	  cvs_connection_release(send, recv);

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

	  content = realloc(content, content_len + 1);

	  if(! content)
	    return ENOMEM;
	    
	  content[content_len] = 0; /* zero terminate */
	  rev->contents = content;
	  
	  return 0; /* jippie, looks perfectly, he? */
	}

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

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

      switch(buf[0])
	{
	case 'E': /* we expect a patch, padded by M's, this is not what
		   * we want to have, keep on complaining 
		   */
	  cvs_treat_error(recv, buf);
	  cvs_connection_release(send, recv);
	  free(content);
	  return EIO;

	case 'M':
	  got_something = 1;

	  if(buf[2] != '+')
	    continue; /* skip patch's header, we don't need it ... */

	  while(content_len + buflen - 4 > content_alloc) 
	    {
	      content_alloc = content_alloc ? content_alloc << 2 : 4096;
	      content = realloc(content, content_alloc);
	      
	      if(! content)
		return ENOMEM; /* doesn't look too good for us :(   */
	    }

	  memcpy(content + content_len, buf + 4, buflen - 5);
	  content_len += buflen - 5;

	  content[content_len ++] = 10; /* linefeed */
	  break;
	  
	default:
	  cvs_treat_error(recv, buf);
	  cvs_connection_release(send, recv);
	  free(content);
	  return EIO;
	}
    }

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