summaryrefslogtreecommitdiff
path: root/cvs_tree.c
blob: ee390c950c7140e007525766f119356fd6f56fa6 (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
/**********************************************************
 * cvs_tree.c
 *
 * Copyright 2004, 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 file/directory tree from cvs
 */

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

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>

#include "cvs_connect.h"
#include "cvs_tree.h"

static struct netnode *cvs_tree_enqueue(struct netnode *, const char *);

/* check whether there already is a netnode for the file with the provided
 * name, create a new one, if not. add revision information for HEAD revision.
 */
static error_t cvs_tree_enqueue_file(struct netnode *cwd, const char *filename,
				     const char *revision);


/* next file number (aka inode) we will assign */
volatile unsigned int next_fileno = 1;


/* netnode *cvs_tree_read
 *
 * read the whole file and directory tree of the module specified in config
 * structure.  The tree is stored in **ptr_to_rootnode, make sure
 * *ptr_to_rootnode is NULL on first call.
 * RETURN: 0 on success
 */
error_t
cvs_tree_read(struct netnode **rootdir)
{
  FUNC_PROLOGUE("cvs_tree_read");
  FILE *send, *recv;
  struct netnode *cwd = (void *) 0xDEADBEEF;
  char *ptr;
  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))
    FUNC_RETURN(EIO);

  fprintf(send, 
	  "UseUnchanged\n"
	  "Argument -s\n" /* we don't want to download the file's contents */
	  "Argument -r\nArgument 0\n"
	  "Argument -r\nArgument HEAD\n"
	  "Argument %s\n"
	  "rdiff\n", config.cvs_module);

  /* cvs now either answers like this:
   * E cvs rdiff: Diffing <directory>
   * M File <file> is new; HEAD revision <revision>
   *
   * the other possibility is as follows:
   * E cvs rdiff: cannot find module <module> - ignored
   * error
   */
  while(fgets(buf, sizeof(buf), recv))
    {
      ptr = buf + strlen(buf);
      ptr --;

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

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

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

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

      if(buf[1] != ' ') 
	{
	  cvs_treat_error(recv, buf);
	  cvs_connection_release(send, recv);
	  FUNC_RETURN(EIO);
	}

      DEBUG("tree-read", "%s\n", buf);
      switch(buf[0])
	{
	case 'E': /* E cvs rdiff: Diffing <directory> */
	  if(! (ptr = strstr(buf, "Diffing ")))
	    {
	      cvs_treat_error(recv, buf);
	      cvs_connection_release(send, recv);
	      FUNC_RETURN(EIO);
	    }

	  ptr += 8;
	  if(! *rootdir) 
	    cwd = *rootdir = cvs_tree_enqueue(NULL, ptr);
	  else 
	    cwd = cvs_tree_enqueue(*rootdir, ptr);

	  if(! cwd)
	    {
	      cvs_connection_kill(send, recv);
	      FUNC_RETURN(ENOMEM);
	    }
	  
	  break;

	case 'M': /* M File <file> is new; HEAD revision <revision> */
	  if(! (ptr = strstr(buf, "File ")))
	    {
	      cvs_treat_error(recv, buf);
	      cvs_connection_release(send, recv);
	      FUNC_RETURN(EIO);
	    }
	  
	  {
	    const char *revision;
	    const char *filename = (ptr += 5);

	    if(! (ptr = strstr(filename, " is new")))
	      {
		cvs_treat_error(recv, buf);
		cvs_connection_release(send, recv);
		FUNC_RETURN(EIO);
	      }
	    *(ptr ++) = 0;

	    revision = ptr;

	    /* strip leading path from filename */
	    while((ptr = strchr(filename, '/')))
	      filename = ptr + 1;

	    if(! (ptr = strstr(revision, "revision ")))
	      {
		cvs_treat_error(recv, NULL);
		cvs_connection_release(send, recv);
		FUNC_RETURN(EIO);
	      }
	  
	    revision = (ptr += 9);

	    if(cvs_tree_enqueue_file(cwd, filename, revision))
	      {
		cvs_connection_kill(send, recv);
		FUNC_RETURN(ENOMEM);
	      }

	    break;
	  }

	default:
	  cvs_treat_error(recv, buf);
	  cvs_connection_release(send, recv);
	  FUNC_RETURN(EIO);
	}
    }

  cvs_connection_kill(send, recv);
  FUNC_EPILOGUE(EIO);
}


/* cvs_tree_enqueue(netnode, path)
 *
 * allocate an empty netnode structure for the directory addressed by
 * the argument 'path' and put it into the netnode structure *dir
 */
static struct netnode *
cvs_tree_enqueue(struct netnode *dir, const char *path)
{
  struct netnode *new, *parent = NULL;
  char *end;

  DEBUG("tree-enqueue", "root=%s, path=%s\n", dir ? dir->name : NULL, path);

  if(! (end = strchr(path, '/')))
    {
      /* request for root directory, else there would be a '/' within
       * path. return existing rootdir (dir), if available.
       */
      if(dir) {
	if(! strcmp(dir->name, path))
	  return dir;

	parent = dir;
	dir = (parent = dir)->child;
      }
    }
  else do
    {
      /* if we are in repository browsing mode (i.e. top level module's name
       * is '.', compare root dir's children names first, since the CVS server
       * writes something like CVSROOT/Emptydir (omitting the leading '.')
       */
      if(! strcmp(dir->name, "."))
	dir = dir->child;

      /* now select this directory from within dir (on the current level) */
      if(dir)
	do
	  if(strncmp(dir->name, path, end - path) == 0
	     /* make sure not to match partials: */
	     && dir->name[end - path] == 0)
	    break; /* hey, this is the directory we're looking for! */
	while((dir = dir->sibling));
      
      if(! dir) 
	{
	  /* this MUST NOT happen, if it occurs anyways, there seems to be
	   * something wrong with our cvs server!
	   */
	  fprintf(stderr, PACKAGE ": unable to find directory '%s'\n", path);
	  return NULL;
	}

      path = end + 1;
      dir = (parent = dir)->child;
    }
  while((end = strchr(path, '/')));

  /* scan parent directory for the entry we're looking for ... */
  if(parent)
    for(new = parent->child; new; new = new->sibling)
      if(! strcmp(new->name, path))
	return new;

  /* okay, create new directory structure right in place ... */
  DEBUG("tree-enqueue", "adding new node: parent=%s, path=%s\n",
	parent ? parent->name : NULL, path);

  new = malloc(sizeof(*new));
  if(! new)
    {
      perror(PACKAGE);
      return NULL; /* pray for cvsfs to survive! */
    }

  new->name = strdup(path);
  new->sibling = dir;
  new->child = NULL;
  new->parent = parent;
  new->revision = NULL; /* mark as a directory */
  new->fileno = next_fileno ++;
  new->node = NULL;

  pthread_rwlock_init(&new->lock, NULL);
  
  if(parent)
    parent->child = new;

  return new;
}


/* cvs_tree_enqueue_file
 *
 * check whether there already is a netnode for the file with the provided
 * name, create a new one, if not. add revision information for HEAD revision.
 */
static error_t
cvs_tree_enqueue_file(struct netnode *cwd,
		      const char *filename, const char *revision)
{
  struct netnode *entry;

  /* cvs_tree_add_rev_struct
   * add a mostly empty revision structure to the specified netnode
   */
  error_t cvs_tree_add_rev_struct(struct netnode *entry, const char *revision)
    {
      struct revision *cached_rev;

      pthread_rwlock_wrlock(&entry->lock);
      cached_rev = entry->revision;

      if(! (entry->revision = malloc(sizeof(*entry->revision))))
	{
	  pthread_rwlock_unlock(&entry->lock);
	  return ENOMEM; /* pray for cvsfs to survive! */
	}

      entry->revision->id = strdup(revision);
      entry->revision->contents = NULL;
      entry->revision->next = cached_rev;

      pthread_rwlock_init(&entry->revision->lock, NULL);
      pthread_rwlock_unlock(&entry->lock);

      return 0;
    }

  /* well, first scan directory tree whether we already have 
   * the file we're looking for ... 
   */
  for(entry = cwd->child; entry; entry = entry->sibling)
    if(! strcmp(entry->name, filename))
      {
	/* okay, we already got a netnode for file 'filename', check whether
	 * revision information is up to date ...
	 */
	pthread_rwlock_rdlock(&entry->lock);
	if(! strcmp(revision, entry->revision->id))
	  {
	    pthread_rwlock_unlock(&entry->lock);
	    return 0; /* head revision id hasn't changed ... */
	  }
	pthread_rwlock_unlock(&entry->lock);

	/* okay, create new revision struct */
	if(cvs_tree_add_rev_struct(entry, revision))
	  return ENOMEM;

	return 0;
      }

  /* okay, don't have this particular file available,
   * put a new netnode together ...
   */
  if(! (entry = malloc(sizeof(*entry))))
    {
      perror(PACKAGE);
      return ENOMEM; /* pray for cvsfs to survive! */
    }

  entry->name = strdup(filename);
  entry->sibling = cwd->child;
  entry->child = NULL;
  entry->parent = cwd;
  entry->fileno = next_fileno ++;
  entry->node = NULL;

  /* create lock entry for our new netnode, as it is not linked
   * to somewhere and this is the only thread to update tree info,
   * we don't have to write lock to access entry->revision!
   */
  pthread_rwlock_init(&entry->lock, NULL);

  entry->revision = NULL;
  if(cvs_tree_add_rev_struct(entry, revision))
    {
      perror(PACKAGE);
      free(entry->name);
      free(entry);
      return ENOMEM;
    }

  /* do this as late as possible, aka only if the full entry structure
   * is valid, since we do not lock the netnode -- however we're in the
   * only thread touching the tree at all
   */
  cwd->child = entry;

  return 0;
}