-
Notifications
You must be signed in to change notification settings - Fork 0
/
vcfs_fh.c
724 lines (595 loc) · 17.8 KB
/
vcfs_fh.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
/****************************************************************************
* File: vcfs_fh.c
* This file contains functions for manipulating the in-memory filesystem
* structures. We keep a hash table of all pathnames in the filesystem,
* represented by vcfs_fileid's. Each vcfs_fileid contains a pointer to a
* vcfs_ventry, which contains file attributes for regular files and
* a directory listing for directories.
***************************************************************************/
#include <dirent.h>
#include <sys/types.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <sys/errno.h>
#include <rpc/rpc.h>
#include <fcntl.h>
#include <stdio.h>
#include <time.h>
#include "vcfs.h"
#include "cvs_cmds.h"
#include "utils.h"
/* Our hash table of file ids */
vcfs_fileid *file_hash[1024];
/* Our virtual cvs project */
vcfs_ventry *ventry_list;
/* This is the fileid for the root */
vcfs_fileid root_node = {0, 0, {'\0'}, 0, NULL, NULL};
static fh_ut root_handle;
/* Stuff for the vinode bitmap */
#define NUM_VINODES 256
int vinode_bmap[NUM_VINODES];
/* We cache the most recently read file */
/* TODO: Make this a lot better */
static vcfs_read_cachent *read_cache;
/* Debug a filehandle */
void dump_fh(vcfs_fhdata *f)
{
ASSERT(f != NULL, "Dumping a NULL filehandle");
printf("\t*** fh dump ***\n");
printf("\tmagic - 0x%x\n", f->magic);
printf("\tid - %d\n", f->id);
printf("\tkey = %d\n", f->key);
printf("\n");
}
/* Set the first few bits of the bitmap to reserve the first few numbers */
void init_vinode_bmap()
{
/* Skip the first couple inode numbers */
vinode_bmap[0] = 7;
}
/* Get the next unique vinode number */
int alloc_vinode()
{
int i,j;
int temp;
int num = 0;
int new;
for (i = 0; i < NUM_VINODES; i++)
{
temp = vinode_bmap[i];
for (j = 0; j < 32; j++)
{
if (temp & 1)
{
++num;
}
else
{
new = 1 << j;
vinode_bmap[i] ^= new;
return ((32 * i) + j + 1);
}
temp >>= 1;
}
}
/* Shouldn't happen! */
ASSERT(1 < 0, "Ran out of vinodes");
return 0;
}
/* Simple hash function */
int hash(char *s)
{
int i;
int n = 0;
for (i = 0; i < strlen(s); i++) {
n += s[i];
}
if (n % 1024 == 0)
return 7;
else
return n % 1024;
}
/* This function returns a fileid given a vcfs_fhdata */
vcfs_fileid *get_fh(vcfs_fhdata *h)
{
vcfs_fileid *ret;
if (h->magic != MAGICNUM) {
/* Update the root handle */
h->id = 1;
h->key = 1;
root_handle.fh.id = 1;
memcpy((char *)&root_handle, (char *)h, sizeof(root_handle));
return &root_node;
}
/* We didn't want the root */
ret = find_fh("fake string", h->id, h->key);
return ret;
}
/* Look up a filehandle in the cache */
vcfs_fileid *find_fh(char *name, int id, int key)
{
int bucket;
vcfs_fileid *i;
bucket = key;
i = file_hash[bucket];
while (i != NULL)
{
if (i->id == id)
{
return i;
}
i = i->next;
}
return NULL;
}
/* Create a file id */
vcfs_fileid *create_fh(vcfs_path name, int v, vcfs_ventry *vent)
{
vcfs_fileid *f;
f = (vcfs_fileid *)malloc(sizeof(vcfs_fileid));
strcpy(f->name, name);
f->id = vent->id;
f->hash_key = hash(f->name);
f->ventry = vent;
insert_fh(f);
return f;
}
/* Return a fileid for the given pathname */
vcfs_fileid *lookup_fh_name(vcfs_path name)
{
int h;
vcfs_fileid *f;
h = hash(name);
for (f = file_hash[h]; f != NULL; f = f->next)
{
if (strcmp(f->name, name) == 0)
{
return f;
}
}
return NULL;
}
/* TODO - This should return something, it could fail */
void insert_ventry(vcfs_ventry *v)
{
vcfs_path parent;
vcfs_name entry;
vcfs_fileid *f;
vcfs_ventry *p;
ASSERT(v != NULL, "Inserting a NULL ventry");
/* First get a pointer to the parent ventry */
split_path(v->name, &parent, &entry);
f = lookup_fh_name(parent);
if (f == NULL)
{
printf("insert_ventry: Couldn't find parent %s for %s\n", parent,v->name);
return;
}
if (f->ventry == NULL)
{
fprintf(stderr, "insert_ventry: ventry of parent %s is NULL!!!\n",
parent);
return;
}
p = f->ventry;
v->next = p->dirent;
p->dirent = v;
return;
}
/* Create a ventry */
vcfs_ventry *create_ventry(vcfs_path name, int size, ftype type,
unsigned int mode, char *ver, time_t t, char *tag)
{
vcfs_ventry *v;
v = (vcfs_ventry *)malloc(sizeof(vcfs_ventry));
strcpy(v->name, name);
v->id = alloc_vinode();
v->size = size;
v->type = type;
v->mode = mode;
if (ver != NULL)
{
strncpy(v->ver, ver, VCFS_VER_LEN);
}
v->ctime = t;
v->next = NULL;
v->dirent = NULL;
if (tag != NULL)
{
strncpy(v->tag, tag, VCFS_TAG_LEN);
}
else
{
memset(v->tag, 0, VCFS_TAG_LEN);
}
insert_ventry(v);
return v;
}
/* Put a file into the cache */
void insert_fh(vcfs_fileid *f)
{
ASSERT(f != NULL, "Inserting a NULL fileid");
f->next = file_hash[f->hash_key];
file_hash[f->hash_key] = f;
}
/* Lookup a file in the cache */
vcfs_fileid *lookuph(vcfs_fileid *d, char *name, vcfs_fhdata *fh)
{
char path[NFS_MAXPATHLEN];
vcfs_fileid *f;
vcfs_path short_name;
vcfs_ver ver;
int extended = 0;
int size;
ASSERT(name != NULL, "NULL name pointer");
fh->magic = MAGICNUM;
if (cvs_ver_extended(name, &short_name, &ver))
{
extended = 1;
sprintf(path, "%s/%s", d->name, short_name);
}
else if (strlen(d->name) != 0)
{
sprintf(path,"%s/%s", d->name, name);
}
else
{
sprintf(path, "%s", name);
}
f = lookup_fh_name(path);
if (f == NULL)
{
return NULL;
}
if (!extended)
{
fh->id = f->id;
fh->key = hash(f->name);
}
else
{
/* Look for the extended name */
if (!vcfs_validate_version(f->ventry, ver))
{
/* The client asked for a version number that does not exists */
return NULL;
}
sprintf(path, "%s/%s", d->name, name);
size = f->ventry->size;
f = lookup_fh_name(path);
if (f != NULL)
{
fh->id = f->id;
fh->key = hash(f->name);
}
else
{
vcfs_ventry *v;
/* Construct it */
v = create_ventry(path, size, NFREG, 0, ver, (time_t)0, NULL);
f = create_fh(path, 1, v);
fh->id = v->id;
fh->key = hash(path);
}
}
return f;
}
/* Checkout the project from CVS, and build and in-memory representation
* of it. Currently, this will stay around forever. Soon, there will be an option
* to dynamically update directory contents based on activity in the repository.
*/
int vcfs_build_project()
{
int r;
cvs_buff *expand_buff;
cvs_buff *co_buff;
char *beg;
char *end;
vcfs_path temp;
int n;
char *line;
int i;
time_t current_time;
char ver[16];
int count = 0;
printf("Please wait, loading project...\n");
time(¤t_time);
/* Expand the module */
r = cvs_expand_modules(&expand_buff);
/* Grab the top dir of the project from the response */
beg = (char *)memchr(expand_buff->data, ' ', expand_buff->size);
if (beg == NULL)
{
fprintf(stderr, "Cannot understand expand-modules response\n");
return 0;
}
beg++;
end = (char *)memchr(beg, '\012', expand_buff->size);
if (end == NULL || end < beg)
{
fprintf(stderr, "Cannot read name in expand-modules response\n");
return 0;
}
memset(temp, 0, sizeof(temp));
memcpy(temp, beg, (end - beg));
init_vinode_bmap();
/* Prepare the root dir of the project */
ventry_list = (vcfs_ventry *)malloc(sizeof(vcfs_ventry));
ventry_list->id = 3; /* The very top dir is 1 */
ventry_list->size = 2048;
ventry_list->type = NFDIR;
ventry_list->next = NULL;
ventry_list->dirent = NULL;
for (i = 0; i < strlen(beg); i++)
{
if (beg[i] == '/' || beg[i] == '\012')
{
vcfs_path mod_path;
strncpy(mod_path, beg, i);
mod_path[i] = '\0';
if (count == 0)
{
/* This is the root dir */
strcpy(ventry_list->name, mod_path);
create_fh(ventry_list->name, 1, ventry_list);
}
else
{
/* Insert a new directory */
vcfs_ventry *v;
v = create_ventry(mod_path, 2048, NFDIR,
0, NULL, current_time, NULL);
create_fh(mod_path, 1, v);
}
count++;
}
if (beg[i] == '\012')
{
/* All that's left is the 'ok' */
break;
}
}
/* Check out the project */
r = cvs_co(&co_buff, NULL);
if (!r)
{
/* The repository could not be checked out */
return 0;
}
/* Parse the response buffer, build the rest of the ventry tree */
cvs_buff_read_line(co_buff, &line); /* Swallow first line. TBD - really? */
while((n = cvs_buff_read_line(co_buff, &line)) > 0)
{
int i;
vcfs_ventry *v;
vcfs_path path;
int size;
int beg_ver;
int real_size;
vcfs_tag tag_copy;
memset(path, 0, sizeof(path));
memset(tag_copy, 0, sizeof(tag_copy));
if (line[0] == 'E')
{
/* Looks like a directory.
* HACK - Everything from line[23] on is the dir name.
* TODO - Make extra sure this is a dir and not another message.
*/
strncpy(path, line + 23, (strlen(line) - 23));
v = create_ventry(path, 2048, NFDIR, 0, NULL, current_time, NULL);
create_fh(path, 1, v);
fprintf(stderr, "create dir %s\n", path);
}
else if (line[0] == 'M')
{
/* Looks like a file */
/* TODO - Get the mode */
/* HACK - Filename is from line[4] on */
char *tag = NULL;
strncpy(path, line + 4, (strlen(line) - 4));
free(line);
for (i = 0; i < 2; i++)
{
cvs_buff_read_line(co_buff, NULL);
}
/* Get the version */
cvs_buff_read_line(co_buff, &line);
beg_ver = 0;
for (i = 1; i < strlen(line); i++)
{
if (line[i] == '/' && beg_ver == 0)
{
beg_ver = i + 1;
}
else if (line[i] == '/' && beg_ver > 0)
{
strncpy(&ver[0], (line + beg_ver), (i - beg_ver));
ver[i - beg_ver] = '\0';
break;
}
}
/* Get the tag if it exists */
beg = strrchr(line, '/');
if (beg != NULL && (char)*(beg + 1) == 'T')
{
if (beg + 2 < line + strlen(line))
{
tag = beg + 2;
}
strncpy(tag_copy, tag, sizeof(tag_copy));
}
free(line);
cvs_buff_read_line(co_buff, NULL); /* permissions */
cvs_buff_read_line(co_buff, &line);
if (line[0] == 'z')
{
char buff[8192];
/* We are using compression - this is the compressed size */
size = atoi(line + 1);
/* Get the uncompressed size */
real_size = cvs_zlib_inflate_buffer(co_buff, size, 0,
buff, 8192, FALSE);
}
else
{
real_size = size = atoi(line);
}
v = create_ventry(path, real_size, NFREG, 0, ver, current_time, tag_copy);
create_fh(path, 1, v);
fprintf(stderr, " create file %s\n", path);
/* Skip over the file data */
co_buff->cookie += size;
}
free(line);
}
cvs_free_buff(expand_buff);
cvs_free_buff(co_buff);
/* Allocate the read cache */
read_cache = malloc(sizeof(vcfs_read_cachent));
return r;
}
/* Perform a read. The very simplistic "cache" used here simply
* slurps up files in large chunks and stores the current chunk for the most
* recently requested file.
*/
int vcfs_read(char *buff, vcfs_fhdata *fh, int count, int offset)
{
cvs_buff *resp;
vcfs_fileid *f;
int i;
char *line;
int size;
int len;
int cache_end;
int partial_read = 0;
int cached_read = FALSE;
vcfs_path filename;
vcfs_ver ver;
int compressed_offset;
bool compressed = FALSE;
f = get_fh(fh);
ASSERT(f != NULL, "Reading from a bad filehandle");
/* Adjust the name if it is version extended */
if (!cvs_ver_extended(f->name, &filename, &ver))
{
strcpy(filename, f->name);
}
cache_end = read_cache->start + read_cache->size;
/* Are we reading from our cache? */
if (read_cache->id == f->id)
{
cached_read = TRUE;
if (read_cache->size == READ_CACHE_SIZE && offset + count > cache_end)
{
/* We are reading beyond our current cache */
if (offset < cache_end)
{
/* Copy what we have into the buffer, go get the rest */
partial_read = cache_end - offset;
memcpy(buff, read_cache->data + offset, partial_read);
count -= partial_read;
}
}
else
{
/* We have everything we need in cache, return it */
memcpy(buff, read_cache->data + offset - read_cache->start, count);
len = count;
return len;
}
}
/* Populate the cache for the next read to this file */
read_cache->id = f->id;
cvs_get_file(filename, f->ventry->ver, &resp);
for (i = 0; i < 5; i++)
{
cvs_buff_read_line(resp, NULL);
}
cvs_buff_read_line(resp, &line);
if (line[0] == 'z')
{
/* This file is compressed */
compressed = TRUE;
size = atoi(line + 1);
/* Get the uncompressed chunk of the file we need to fill the cache */
if (cached_read)
{
compressed_offset = read_cache->start + read_cache->size;
}
else
{
compressed_offset = 0;
}
size = cvs_zlib_inflate_buffer(resp, size, compressed_offset, read_cache->data,
READ_CACHE_SIZE, TRUE);
}
else
{
size = atoi(line);
}
free(line);
f->ventry->size = size;
if (cached_read)
{
/* The file has already been cached, but we need to read more */
if (size - read_cache->size > READ_CACHE_SIZE)
{
size = READ_CACHE_SIZE;
}
read_cache->start += read_cache->size;
if (!compressed)
{
memcpy(read_cache->data, resp->data + resp->cookie + read_cache->start,
size);
}
read_cache->size = size;
offset -= read_cache->start;
}
else
{
if (size > READ_CACHE_SIZE)
{
size = READ_CACHE_SIZE;
}
if (!compressed)
{
memcpy(read_cache->data, resp->data + resp->cookie, size);
}
read_cache->size = size;
read_cache->start = 0;
}
if (offset + count > read_cache->start + read_cache->size)
{
memcpy(buff + partial_read, (read_cache->data + offset),
read_cache->size - offset);
len = size - offset;
}
else
{
memcpy(buff + partial_read, (read_cache->data + offset), count);
len = count + partial_read;
}
//printf("The data we read is %s\n", buff);
cvs_free_buff(resp);
return len;
}
/* Make sure the given version is actually a version of the specified file */
int vcfs_validate_version(vcfs_ventry *v, char *ver)
{
cvs_buff *resp;
int n;
char *next_ver;
int valid = 0;
cvs_get_log(v->name, &resp);
while(cvs_get_log_info(resp, &next_ver, NULL, NULL, NULL) > 0)
{
if (strcmp(ver, next_ver) == 0)
{
/* It's a valid version */
valid = 1;
}
free(next_ver);
}
cvs_free_buff(resp);
return valid;
}