Changes On Branch 36d0805e0eae0bdf

Changes In Branch pure-c Through [36d0805e0e] Excluding Merge-Ins

This is equivalent to a diff from 992994189a to 36d0805e0e

2020-03-25
00:24
Integrate pure C version check-in: eebfe1f40f user: rkeene tags: trunk
2019-12-06
14:17
Updated ignores Closed-Leaf check-in: a01594ff30 user: rkeene tags: pure-c
2019-12-03
17:59
Made Tcl and C version of "xvfs-create" more similar by re-ordering output check-in: 36d0805e0e user: rkeene tags: pure-c
2019-12-02
03:09
Updated to use string for minirivet parsing check-in: 2d9592486f user: rkeene tags: pure-c
2019-12-01
00:16
Started work on xvfs-create-c, a pure C version of the builder with no dependencies check-in: ae8437b96b user: rkeene tags: pure-c
2019-11-14
23:39
Revert [ac58551e55], needs more work check-in: 992994189a user: rkeene tags: trunk
23:14
Updated to use Tcl-computed mechanism for building shared objects check-in: ac58551e55 user: rkeene tags: trunk

Modified .fossil-settings/ignore-glob from [5dadc8b9dc] to [5116205bcb].

15
16
17
18
19
20
21


22
23
24
25
26
27
28
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30







+
+







example-client.gcda
example-client.gcno
xvfs.gcda
xvfs.gcno
xvfs-create-standalone.new
xvfs-create-standalone
xvfs-test-coverage
xvfs-create-c
xvfs-create-c.o
__test__.tcl
sdks
xvfs_random.so
xvfs_synthetic.so
profile-bare
profile-gperf
oprofile_data

Modified Makefile from [fa4437d0b1] to [0296cd0ad4].



1

2
3
4
5
6
7
8
9
10
11
12
13
14







15
16
17
18
19
20
21
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
+
+
-
+










-
-
-
+
+
+
+
+
+
+







TCLSH_NATIVE  := tclsh
TCL_CONFIG_SH_DIR := $(shell echo 'puts [tcl::pkgconfig get libdir,runtime]' | $(TCLSH_NATIVE))
TCL_CONFIG_SH := /usr/lib64/tclConfig.sh
TCL_CONFIG_SH := $(TCL_CONFIG_SH_DIR)/tclConfig.sh
XVFS_ROOT_MOUNTPOINT := //xvfs:/
CPPFLAGS      := -DXVFS_ROOT_MOUNTPOINT='"$(XVFS_ROOT_MOUNTPOINT)"' -I. -DUSE_TCL_STUBS=1 -DXVFS_DEBUG $(shell . "${TCL_CONFIG_SH}" && echo "$${TCL_INCLUDE_SPEC}") $(XVFS_ADD_CPPFLAGS)
CFLAGS        := -fPIC -g3 -ggdb3 -Wall $(XVFS_ADD_CFLAGS)
LDFLAGS       := $(XVFS_ADD_LDFLAGS)
LIBS          := $(shell . "${TCL_CONFIG_SH}" && echo "$${TCL_STUB_LIB_SPEC}")
TCLSH         := tclsh
LIB_SUFFIX    := $(shell . "${TCL_CONFIG_SH}"; echo "$${TCL_SHLIB_SUFFIX:-.so}")

all: example-standalone$(LIB_SUFFIX) example-client$(LIB_SUFFIX) example-flexible$(LIB_SUFFIX) xvfs$(LIB_SUFFIX)

example.c: $(shell find example -type f) $(shell find lib -type f) lib/xvfs/xvfs.c.rvt xvfs-create Makefile
	./xvfs-create --directory example --name example > example.c.new
	mv example.c.new example.c
example.c: $(shell find example -type f) $(shell find lib -type f) lib/xvfs/xvfs.c.rvt xvfs-create-c xvfs-create Makefile
	rm -f example.c.new.1 example.c.new.2
	./xvfs-create-c --directory example --name example > example.c.new.1
	./xvfs-create --directory example --name example > example.c.new.2
	bash -c "diff -u <(grep -v '^ *$$' example.c.new.1) <(grep -v '^ *$$' example.c.new.2)" || :
	rm -f example.c.new.2
	mv example.c.new.1 example.c

example-standalone.o: example.c xvfs-core.h xvfs-core.c Makefile
	$(CC) $(CPPFLAGS) -DXVFS_MODE_STANDALONE $(CFLAGS) -o example-standalone.o -c example.c

example-standalone$(LIB_SUFFIX): example-standalone.o Makefile
	$(CC) $(CFLAGS) $(LDFLAGS) -shared -o example-standalone$(LIB_SUFFIX) example-standalone.o $(LIBS)

41
42
43
44
45
46
47






48
49
50
51
52
53
54
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66







+
+
+
+
+
+







# like lib/minirivet, xvfs-core.c, etc) version of "xvfs-create"
xvfs-create-standalone: $(shell find lib -type f) xvfs-create xvfs-core.c xvfs-core.h lib/xvfs/xvfs.c.rvt Makefile
	rm -f xvfs-create-standalone.new xvfs-create-standalone
	./xvfs-create --dump-tcl --remove-debug > xvfs-create-standalone.new
	chmod +x xvfs-create-standalone.new
	mv xvfs-create-standalone.new xvfs-create-standalone

xvfs-create-c: xvfs-create-c.o
	$(CC) $(CFLAGS) $(LDFLAGS) -o xvfs-create-c xvfs-create-c.o $(LIBS)

xvfs-create-c.o: xvfs-create-c.c
	$(CC) $(CPPFLAGS) $(CFLAGS) -o xvfs-create-c.o -c xvfs-create-c.c

xvfs_random$(LIB_SUFFIX): $(shell find example -type f) $(shell find lib -type f) lib/xvfs/xvfs.c.rvt xvfs-create-random Makefile
	./xvfs-create-random | $(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -DXVFS_MODE_FLEXIBLE -x c - -shared -o xvfs_random$(LIB_SUFFIX) $(LIBS)

xvfs_synthetic$(LIB_SUFFIX): $(shell find lib -type f) lib/xvfs/xvfs.c.rvt xvfs-create-synthetic Makefile
	./xvfs-create-synthetic | $(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -DXVFS_MODE_FLEXIBLE -x c - -shared -o xvfs_synthetic$(LIB_SUFFIX) $(LIBS)

do-benchmark:
95
96
97
98
99
100
101

102

103
104
105
106
107
108
109
107
108
109
110
111
112
113
114

115
116
117
118
119
120
121
122







+
-
+







	./profile-gperf
	gprof ./profile-gperf
	valgrind --tool=callgrind --callgrind-out-file=callgrind.out ./profile-bare 10 2
	callgrind_annotate callgrind.out

clean:
	rm -f xvfs-create-standalone.new xvfs-create-standalone
	rm -f xvfs-create-c.o xvfs-create-c
	rm -f example.c example.c.new
	rm -f example.c example.c.new example.c.new.1 example.c.new.2
	rm -f example-standalone$(LIB_SUFFIX) example-standalone.o
	rm -f example-client.o example-client$(LIB_SUFFIX)
	rm -f example-flexible.o example-flexible$(LIB_SUFFIX)
	rm -f xvfs.o xvfs$(LIB_SUFFIX)
	rm -f example-standalone.gcda example-standalone.gcno
	rm -f example-client.gcda example-client.gcno
	rm -f example-flexible.gcda example-flexible.gcno

Modified lib/xvfs/xvfs.c.rvt from [ced159acac] to [307533bbcd].

1







2
3

4
5
6
7
8
9
10
1
2
3
4
5
6
7
8
9

10
11
12
13
14
15
16
17

+
+
+
+
+
+
+

-
+







<?
	# Care must be taken when editing this file as
	# it may be processed by either "xvfs-create"
	# which uses a full Tcl and Rivet parser
	# or by "xvfs-create-c" which uses a much
	# simpler one that only knows about printable
	# sections and ignores all Tcl sections

	if {[info exists ::xvfs::xvfsCoreH]} {
?><?= $::xvfs::xvfsCoreH ?><?
		::minirivet::_emitOutput $::xvfs::xvfsCoreH
	} else {
?>#include <xvfs-core.h><? } ?>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <tcl.h>

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
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







-
-
+
+
-


-
-
-
-
-
-
-
-
-
-
+
+
+
-
-
+








-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
-







	} data;
};
#endif

<?
	package require xvfs

	xvfs::main $::xvfs::argv

	set ::xvfs::fileInfoStruct [xvfs::main $::xvfs::argv]
?><?= $::xvfs::fileInfoStruct ?>
?>
static long xvfs_<?= $::xvfs::fsName ?>_nameToIndex(const char *path) {
<?
		if {[llength $::xvfs::outputFiles] < 3} {
			set hashMode perfectHashFunction
		} else {
			set hashMode hashTable
		}

		if {$hashMode eq "hashTable"} {
			set hashTable [::xvfs::generateHashTable pathIndex path pathLen XVFS_NAME_LOOKUP_ERROR $::xvfs::outputFiles prefix "\t" hashTableSize 30 validate "strcmp(path, xvfs_${::xvfs::fsName}_data\[pathIndex\].name) == 0" onValidated "return(pathIndex);"]
			set hashTableHeader [dict get $hashTable header]
			puts $hashTableHeader
	set hashTable [::xvfs::generateHashTable pathIndex path pathLen XVFS_NAME_LOOKUP_ERROR $::xvfs::outputFiles prefix "\t" hashTableSize 30 validate "strcmp(path, xvfs_${::xvfs::fsName}_data\[pathIndex\].name) == 0" onValidated "return(pathIndex);"]
	set hashTableHeader [dict get $hashTable header]
?><?= $hashTableHeader ?>
		}
?>	long pathIndex;
	long pathIndex;
	ssize_t pathLen;

	if (path == NULL) {
		return(XVFS_NAME_LOOKUP_ERROR);
	}

	pathLen = strlen(path);

<?
		if {$hashMode eq "perfectHashFunction"} {
?>
	pathIndex = <?= [::xvfs::generatePerfectHashFunctionCall "path" pathLen XVFS_NAME_LOOKUP_ERROR $::xvfs::outputFiles] ?>;
	if (pathIndex < 0 || pathIndex >= <?= [llength $::xvfs::outputFiles] ?>) {
		pathIndex = XVFS_NAME_LOOKUP_ERROR;
	}

	if (pathIndex != XVFS_NAME_LOOKUP_ERROR) {
		if (strcmp(path, xvfs_<?= $::xvfs::fsName ?>_data[pathIndex].name) == 0) {
			return(pathIndex);
		}
	}
<?
		} else {
			puts [dict get $hashTable body]
		}
<?= [dict get $hashTable body] ?>

?>
	return(XVFS_NAME_LOOKUP_ERROR);
}

static const char **xvfs_<?= $::xvfs::fsName ?>_getChildren(const char *path, Tcl_WideInt *count) {
	const struct xvfs_file_data *fileInfo;
	long inode;

214
215
216
217
218
219
220
221
222


223
224
225
226
227
228
229
196
197
198
199
200
201
202


203
204
205
206
207
208
209
210
211







-
-
+
+







	inode = xvfs_<?= $::xvfs::fsName ?>_nameToIndex(path);
	if (inode == XVFS_NAME_LOOKUP_ERROR) {
		return(XVFS_RV_ERR_ENOENT);
	}
	
	fileInfo = &xvfs_<?= $::xvfs::fsName ?>_data[inode];
	
	statBuf->st_dev   = <?= [zlib adler32 $::xvfs::fsName] ?>;
	statBuf->st_rdev  = <?= [zlib adler32 $::xvfs::fsName] ?>;
	statBuf->st_dev   = <?= [zlib adler32 $::xvfs::fsName 0] ?>;
	statBuf->st_rdev  = <?= [zlib adler32 $::xvfs::fsName 0] ?>;
	statBuf->st_ino   = inode;
	statBuf->st_uid   = 0;
	statBuf->st_gid   = 0;
	statBuf->st_atime = 0;
	statBuf->st_ctime = 0;
	statBuf->st_mtime = 0;
#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE

Modified lib/xvfs/xvfs.tcl from [842e8c1c67] to [832d8bb370].

1
2
3
4
5
6
7
8
9
10
11
12
13

14
15
16
17
18
19
20
21
1
2
3
4
5
6
7
8
9




10

11
12
13
14
15
16
17









-
-
-
-
+
-







#! /usr/bin/env tclsh

namespace eval ::xvfs {}
namespace eval ::xvfs::callback {}

set ::xvfs::_xvfsDir [file dirname [info script]]

# Functions
proc ::xvfs::_emitLine {line} {
	if {[info command ::minirivet::_emitOutput] ne ""} {
		::minirivet::_emitOutput "${line}\n"
	} else {
		puts $line
	lappend ::xvfs::_emitLine $line
	}
}

proc ::xvfs::printHelp {channel {errors ""}} {
	if {[llength $errors] != 0} {
		foreach error $errors {
			puts $channel "error: $error"
		}
127
128
129
130
131
132
133
134
135
136
137

138
139
140

141
142

143
144
145
146
147
148
149
123
124
125
126
127
128
129

130
131

132
133
134

135
136
137
138
139
140
141
142
143
144
145







-


-
+


-
+


+







			return -code error "Unable to process $inputFile, unknown type: $fileInfo(type)"
		}
	}

	::xvfs::_emitLine "\t\{"
	::xvfs::_emitLine "\t\t.name = \"[sanitizeCString $outputFile]\","
	::xvfs::_emitLine "\t\t.type = $type,"
	::xvfs::_emitLine "\t\t.size = $size,"
	switch -exact -- $fileInfo(type) {
		"file" {
			::xvfs::_emitLine "\t\t.data.fileContents = (const unsigned char *) $data"
			::xvfs::_emitLine "\t\t.data.fileContents = (const unsigned char *) $data,"
		}
		"directory" {
			::xvfs::_emitLine "\t\t.data.dirChildren  = $children"
			::xvfs::_emitLine "\t\t.data.dirChildren  = $children,"
		}
	}
	::xvfs::_emitLine "\t\t.size = $size"
	::xvfs::_emitLine "\t\},"
}

proc ::xvfs::processDirectory {fsName directory {subDirectory ""}} {
	set subDirectories [list]
	set outputFiles [list]
	set workingDirectory [file join $directory $subDirectory]
278
279
280
281
282
283
284



285
286
287
288
289
290
291
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290







+
+
+







	}

	## 3. Start processing directory and producing initial output
	set ::xvfs::outputFiles [processDirectory $fsName $rootDirectory]

	set ::xvfs::fsName $fsName
	set ::xvfs::rootDirectory $rootDirectory

	# Return the output
	return [join $::xvfs::_emitLine "\n"]
}

proc ::xvfs::run {args} {
	uplevel #0 { package require minirivet }

	set ::xvfs::argv $args
	::minirivet::parse [file join $::xvfs::_xvfsDir xvfs.c.rvt]

Added xvfs-create-c.c version [8abf7d803b].








































































































































































































































































































































































































































































































































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
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
#include <sys/stat.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <stdio.h>
#include <ctype.h>
#include <fcntl.h>

struct options {
	char *name;
	char *directory;
};

struct xvfs_state {
	char **children;
	unsigned long child_count;
	unsigned long child_len;
	int bucket_count;
	int max_index;
};

enum xvfs_minirivet_mode {
	XVFS_MINIRIVET_MODE_COPY,
	XVFS_MINIRIVET_MODE_TCL,
	XVFS_MINIRIVET_MODE_TCL_PRINT
};

/*
 * adler32() function from zlib 1.1.4 and under the same license
 */
static unsigned long adler32(unsigned long adler, const unsigned char *buf, unsigned int len) {
	const int len_max = 5552;
	const unsigned long base = 65521;
	unsigned long s1 = adler & 0xffff;
	unsigned long s2 = (adler >> 16) & 0xffff;
	int k, i;

	if (buf == NULL) {
		return(1UL);
	}

	while (len > 0) {
		k = len < len_max ? len : len_max;
		len -= k;

		while (k >= 16) {
			for (i = 0; i < 16; i++) {
				s2 += buf[i];
			}
			s1 = buf[15];

			buf += 16;
			k   -= 16;
		}

		if (k != 0) {
			do {
				s1 += *buf++;
				s2 += s1;
			} while (--k);
		}

		s1 %= base;
		s2 %= base;
	}

	return((s2 << 16) | s1);
}

/*
 * Handle XVFS Rivet template file substitution
 */
static void parse_xvfs_minirivet_file(FILE *outfp, const char * const external_file_name, const char * const internal_file_name) {
	FILE *fp;
	unsigned long file_size;
	unsigned char buf[10];
	size_t item_count;
	int idx;

	fp = fopen(external_file_name, "rb");
	if (!fp) {
		return;
	}

	fprintf(outfp, "\t{\n");
	fprintf(outfp, "\t\t.name = \"%s\",\n", internal_file_name);
	fprintf(outfp, "\t\t.type = XVFS_FILE_TYPE_REG,\n");
	fprintf(outfp, "\t\t.data.fileContents = (const unsigned char *) \"");

	file_size = 0;
	while (1) {
		item_count = fread(&buf, 1, sizeof(buf), fp);
		if (item_count <= 0) {
			break;
		}

		if (file_size != 0) {
			fprintf(outfp, "\n\t\t\t\"");
		}

		for (idx = 0; idx < item_count; idx++) {
			fprintf(outfp, "\\x%02x", (int) buf[idx]);
		}
		fprintf(outfp, "\"");

		file_size += item_count;
	}

	fclose(fp);

	fprintf(outfp, ",\n");
	fprintf(outfp, "\t\t.size = %lu\n", file_size);
	fprintf(outfp, "\t},\n");
}

static void parse_xvfs_minirivet_directory(FILE *outfp, struct xvfs_state *xvfs_state, const char * const directory, const char * const prefix) {
	const unsigned int max_path_len = 8192, max_children = 65536;
	unsigned long child_idx, child_count;
	DIR *dp;
	struct dirent *file_info;
	struct stat file_stat;
	char *full_path_buf;
	char *rel_path_buf;
	char **children;
	int stat_ret;
	int snprintf_ret;

	dp = opendir(directory);
	if (!dp) {
		return;
	}

	full_path_buf = malloc(max_path_len);
	rel_path_buf = malloc(max_path_len);
	children = malloc(sizeof(*children) * max_children);

	child_idx = 0;
	while (1) {
		file_info = readdir(dp);
		if (!file_info) {
			break;
		}

		if (strcmp(file_info->d_name, ".") == 0) {
			continue;
		}

		if (strcmp(file_info->d_name, "..") == 0) {
			continue;
		}

		snprintf_ret = snprintf(full_path_buf, max_path_len, "%s/%s", directory, file_info->d_name);
		if (snprintf_ret >= max_path_len) {
			continue;
		}

		snprintf_ret = snprintf(rel_path_buf, max_path_len, "%s%s%s",
			prefix,
			strcmp(prefix, "") == 0 ? "" : "/",
			file_info->d_name
		);
		if (snprintf_ret >= max_path_len) {
			continue;
		}

		stat_ret = stat(full_path_buf, &file_stat);
		if (stat_ret != 0) {
			continue;
		}

		children[child_idx] = strdup(file_info->d_name);
		child_idx++;

		if (S_ISDIR(file_stat.st_mode)) {
			parse_xvfs_minirivet_directory(outfp, xvfs_state, full_path_buf, rel_path_buf);
		} else {
			parse_xvfs_minirivet_file(outfp, full_path_buf, rel_path_buf);

			xvfs_state->children[xvfs_state->child_count] = strdup(rel_path_buf);
			xvfs_state->child_count++;
		}
	}
	free(full_path_buf);
	free(rel_path_buf);

	child_count = child_idx;

	fprintf(outfp, "\t{\n");
	fprintf(outfp, "\t\t.name = \"%s\",\n", prefix);
	fprintf(outfp, "\t\t.type = XVFS_FILE_TYPE_DIR,\n");
	fprintf(outfp, "\t\t.data.dirChildren  = (const char *[]) {");
	for (child_idx = 0; child_idx < child_count; child_idx++) {
		if (child_idx != 0) {
			fprintf(outfp, ", ");
		}

		fprintf(outfp, "\"%s\"", children[child_idx]);

		free(children[child_idx]);
	}
	fprintf(outfp, "},\n");
	fprintf(outfp, "\t\t.size = %lu\n", child_count);

	free(children);

	fprintf(outfp, "\t},\n");

	xvfs_state->children[xvfs_state->child_count] = strdup(prefix);
	xvfs_state->child_count++;

	closedir(dp);

	return;
}

static void parse_xvfs_minirivet_hashtable_header(FILE *outfp, struct xvfs_state *xvfs_state) {
	const int max_bucket_count = 30;
	int bucket_count;
	int idx1, idx2;
	int check_hash;
	int first_entry;

	if (xvfs_state->child_count > max_bucket_count) {
		bucket_count = max_bucket_count;
	} else {
		bucket_count = xvfs_state->child_count;
	}
	xvfs_state->bucket_count = bucket_count;
	xvfs_state->max_index = xvfs_state->child_count;

	fprintf(outfp, "\tlong pathIndex_idx;\n");
	fprintf(outfp, "\tint pathIndex_hash;\n");

	/*
	 * XXX:TODO: Make this not O(n^2)
	 */
	for (idx1 = 0; idx1 < bucket_count; idx1++) {
		fprintf(outfp, "\tstatic const long pathIndex_hashTable_%i[] = {\n", idx1);
		fprintf(outfp, "\t\t");
		first_entry = 1;

		for (idx2 = 0; idx2 < xvfs_state->child_count; idx2++) {
			check_hash = adler32(0, (unsigned char *) xvfs_state->children[idx2], strlen(xvfs_state->children[idx2])) % bucket_count;
			if (check_hash != idx1) {
				continue;
			}

			if (!first_entry) {
				fprintf(outfp, ", ");
			}
			first_entry = 0;

			if (check_hash == idx1) {
				fprintf(outfp, "%i", idx2);
			}
		}

		if (!first_entry) {
			fprintf(outfp, ", ");
		}
		fprintf(outfp, "XVFS_NAME_LOOKUP_ERROR");

		fprintf(outfp, "\n");

		fprintf(outfp, "\t};\n");
	}

	for (idx2 = 0; idx2 < xvfs_state->child_count; idx2++) {
		free(xvfs_state->children[idx2]);
	}
	free(xvfs_state->children);

	fprintf(outfp, "\tstatic const long * const pathIndex_hashTable[%i] = {\n", bucket_count);
	for (idx1 = 0; idx1 < bucket_count; idx1++) {
		fprintf(outfp, "\t\tpathIndex_hashTable_%i,\n", idx1);
	}
	fprintf(outfp, "\t};\n");
	return;
}

static void parse_xvfs_minirivet_hashtable_body(FILE *outfp, struct xvfs_state *xvfs_state) {
	fprintf(outfp, "\tpathIndex_hash = Tcl_ZlibAdler32(0, (unsigned char *) path, pathLen) %% %i;\n", xvfs_state->bucket_count);
	fprintf(outfp, "\tfor (pathIndex_idx = 0; pathIndex_idx < %i; pathIndex_idx++) {\n", xvfs_state->max_index);
	fprintf(outfp, "\t\tpathIndex = pathIndex_hashTable[pathIndex_hash][pathIndex_idx];\n");
	fprintf(outfp, "\t\tif (pathIndex == XVFS_NAME_LOOKUP_ERROR) {\n");
	fprintf(outfp, "\t\t\tbreak;\n");
	fprintf(outfp, "\t\t}\n");
	fprintf(outfp, "\n");
	fprintf(outfp, "\t\tif (strcmp(path, xvfs_example_data[pathIndex].name) == 0) {\n");
	fprintf(outfp, "\t\t\treturn(pathIndex);\n");
	fprintf(outfp, "\t\t}\n");
	fprintf(outfp, "\t}\n");
	return;
}

static void parse_xvfs_minirivet_handle_tcl_print(FILE *outfp, const struct options * const options, struct xvfs_state *xvfs_state, char *command) {
	char *buffer_p, *buffer_e;

	buffer_p = command;
	while (*buffer_p && isspace(*buffer_p)) {
		buffer_p++;
	}

	buffer_e = buffer_p + strlen(buffer_p) - 1;
	while (buffer_e >= buffer_p && isspace(*buffer_e)) {
		*buffer_e = '\0';
		buffer_e--;
	}

	if (strcmp(buffer_p, "$::xvfs::fsName") == 0) {
		fprintf(outfp, "%s", options->name);
	} else if (strcmp(buffer_p, "$::xvfs::fileInfoStruct") == 0) {
		fprintf(outfp, "static const struct xvfs_file_data xvfs_");
		fprintf(outfp, "%s", options->name);
		fprintf(outfp, "_data[] = {\n");
		parse_xvfs_minirivet_directory(outfp, xvfs_state, options->directory, "");
		fprintf(outfp, "};\n");
	} else if (strcmp(buffer_p, "[zlib adler32 $::xvfs::fsName 0]") == 0) {
		fprintf(outfp, "%lu", adler32(0, (unsigned char *) options->name, strlen(options->name)));
	} else if (strcmp(buffer_p, "$hashTableHeader") == 0) {
		parse_xvfs_minirivet_hashtable_header(outfp, xvfs_state);
	} else if (strcmp(buffer_p, "[dict get $hashTable body]") == 0) {
		parse_xvfs_minirivet_hashtable_body(outfp, xvfs_state);
	} else {
		fprintf(outfp, "@INVALID@%s@INVALID@", buffer_p);
	}

	return;
}

static int parse_xvfs_minirivet(FILE *outfp, const char * const template, const struct options * const options) {
	struct xvfs_state xvfs_state;
	int ch, ch_buf;
	int template_idx = 0;
	char tcl_buffer[8192], *tcl_buffer_p;
	enum xvfs_minirivet_mode mode;

	xvfs_state.child_count = 0;
	xvfs_state.child_len   = 65536;
	xvfs_state.children    = malloc(sizeof(*xvfs_state.children) * xvfs_state.child_len);

#define parse_xvfs_minirivet_getbyte(var) var = template[template_idx]; template_idx++; if (var == 0) { break; }

	mode = XVFS_MINIRIVET_MODE_COPY;
	tcl_buffer_p = NULL;
	while (1) {
		parse_xvfs_minirivet_getbyte(ch);

		switch (mode) {
			case XVFS_MINIRIVET_MODE_COPY:
				if (ch == '<') {
					parse_xvfs_minirivet_getbyte(ch_buf);
					if (ch_buf != '?') {
						fputc('<', outfp);
						ch = ch_buf;

						break;
					}

					tcl_buffer_p = tcl_buffer;
					parse_xvfs_minirivet_getbyte(ch_buf);
					if (ch_buf == '=') {
						mode = XVFS_MINIRIVET_MODE_TCL_PRINT;
					} else {
						mode = XVFS_MINIRIVET_MODE_TCL;
						*tcl_buffer_p = ch_buf;
						tcl_buffer_p++;
					}
					*tcl_buffer_p = '\0';
					continue;
				}
				break;
			case XVFS_MINIRIVET_MODE_TCL:
			case XVFS_MINIRIVET_MODE_TCL_PRINT:
				if (ch == '?') {
					parse_xvfs_minirivet_getbyte(ch_buf);
					if (ch_buf != '>') {
						*tcl_buffer_p = ch;
						tcl_buffer_p++;

						ch = ch_buf;
						
						break;
					}

					*tcl_buffer_p = '\0';

					if (mode == XVFS_MINIRIVET_MODE_TCL_PRINT) {
						parse_xvfs_minirivet_handle_tcl_print(outfp, options, &xvfs_state, tcl_buffer);
					}

					mode = XVFS_MINIRIVET_MODE_COPY;
					continue;
				}
				break;
		}

		switch (mode) {
			case XVFS_MINIRIVET_MODE_COPY:
				fputc(ch, outfp);
				break;
			case XVFS_MINIRIVET_MODE_TCL:
			case XVFS_MINIRIVET_MODE_TCL_PRINT:
				*tcl_buffer_p = ch;
				tcl_buffer_p++;
				break;
		}
	}

#undef parse_xvfs_minirivet_getbyte

	return(1);
}

static int xvfs_create(FILE *outfp, const struct options * const options) {
	const int template_len = 65536;
	const char * const template_file = "lib/xvfs/xvfs.c.rvt";
	FILE *fp;
	char *template, *template_p;
	int template_remain;
	size_t fread_ret;
	int retval;

	fp = fopen(template_file, "r");
	if (!fp) {
		return(0);
	}

	template = malloc(template_len);
	template_remain = template_len;
	if (!template) {
		fclose(fp);

		return(0);
	}

	template_p = template;
	while (1) {
		fread_ret = fread(template_p, 1, template_remain, fp);
		if (fread_ret <= 0) {
			break;
		}

		template_p += fread_ret;
		template_remain -= fread_ret;
	}
	if (template_remain > 0) {
		*template_p = '\0';
	}

	fclose(fp);

	retval = parse_xvfs_minirivet(outfp, template, options);

	free(template);

	return(retval);
}

/*
 * Parse command line options
 */
static int parse_options(int argc, char **argv, struct options *options) {
	char *arg;
	char **option;
	int idx;
	int retval;

	for (idx = 0; idx < argc; idx++) {
		arg = argv[idx];

		if (strcmp(arg, "--directory") == 0) {
			option = &options->directory;
		} else if (strcmp(arg, "--name") == 0) {
			option = &options->name;
		} else {
			fprintf(stderr, "Invalid argument %s\n", arg);

			return(0);
		}

		idx++;
		arg = argv[idx];
		*option = arg;
	}

	retval = 1;
	if (!options->directory) {		
		fprintf(stderr, "error: --directory must be specified\n");
		retval = 0;
	}

	if (!options->name) {
		fprintf(stderr, "error: --name must be specified\n");
		retval = 0;
	}

	return(retval);
}

int main(int argc, char **argv) {
	struct options options = {0};
	int parse_options_ret, xvfs_create_ret;

	argc--;
	argv++;

	parse_options_ret = parse_options(argc, argv, &options);
	if (!parse_options_ret) {
		return(1);
	}

	xvfs_create_ret = xvfs_create(stdout, &options);
	if (!xvfs_create_ret) {
		return(1);
	}

	return(0);
}

Modified xvfs-create-synthetic from [48aa2ff0d9] to [d0aa4cbe5c].

17
18
19
20
21
22
23








24
25
26
27
28
29
30
31



32
33
34
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







+
+
+
+
+
+
+
+







-
+
+
+



	fileContents "abc"
}
dict set ::myOwnVFS "xvfs-create-synthetic" {
	type file
}

proc ::xvfs::callback::addOutputFiles {fsName} {
	for {set i 0} {$i < 100000} {incr i} {
		set filename [expr rand()]
		::xvfs::processFile $fsName "" $filename [dict create type file fileContents [expr rand()]]
		lappend retval $filename
	}

	dict set ::myOwnVFS "" children [list foo xvfs-create-synthetic {*}$retval]

	dict for {outputName fileContentsDict} $::myOwnVFS {
		set inputFile $outputName
		if {[dict exists $fileContentsDict inputFile]} {
			set inputFile [dict get $fileContentsDict inputFile]
		}
		::xvfs::processFile $fsName $inputFile $outputName $fileContentsDict
	}
	return [dict keys $::myOwnVFS]
	lappend retval {*}[dict keys $::myOwnVFS]

	return $retval
}

::xvfs::run --directory [pwd] --name synthetic

Modified xvfs-test-phf from [97db11b922] to [c605598e4e].

14
15
16
17
18
19
20
21

22
23
24
25
14
15
16
17
18
19
20

21
22
23
24
25







-
+




	lappend list $i
}

for {set idx 0} {$idx < [llength $list]} {incr idx} {
	set subList [lrange $list 0 $idx]
	puts "$idx ($subList):"
	puts [time {
		puts [::xvfs::generatePerfectHashFunctionCall pathName strlen(pathName) -1 $subList triesAtHashSize 1]
		puts [::xvfs::generatePerfectHashFunctionCall pathName strlen(pathName) -1 $subList useCacheFirst true]
	} 1]
	puts ""
}