Changes To XVFS Build Walkthrough

Changes to "XVFS Build Walkthrough" between 2019-09-17 19:56:59 and 2019-09-17 20:08:12

1
2
3
4
5
6
7
8
9





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









+
+
+
+
+







# XVFS Build Walkthrough
## xvfs-create

The script "`xvfs-create`" which is referenced below creates a C source file containing
a local static structure and an `Xvfs_<name>_Init()` symbol.  The C source file contains
all the data from the specified directory, file names, contents, sizes, directory
children.  This C file can be compiled in a few different ways ("Client", "Standalone",
and "Flexible") covered below.

See the final section for what this C source looks like.

Note the generated output will work for any mode (though it's unnecessary for "Server"
mode, because "Server" mode does not supply any actual VFS data).

## "Server"
### Description

The "Server" mode is a single in-process handler for "`//xvfs:/`" and additional clients
can call `Xvfs_Register()` to register their filesystem with that handler.  This has the
advantage of being more efficient since there are not one `Tcl_Filesystem` per extension
which have to all be searched through but instead a single handler which uses a
143
144
145
146
147
148
149




































































































































































































































































































































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







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
    00000000000050c8 T Xvfs_example_Init
                     U fprintf
                     U memcmp
                     U memcpy
                     U stderr
                     U strlen

## Generated Output

    $ make example.c
    ./xvfs-create --directory example --name example > example.c.new
    mv example.c.new example.c
    
    $ ls -l example.c
    -rw-r--r-- 1 rkeene users 47539 Sep 17 14:58 example.c

    $ cat example.c
    #include <xvfs-core.h>
    #include <sys/stat.h>
    #include <unistd.h>
    #include <string.h>
    #include <tcl.h>

    #define XVFS_NAME_LOOKUP_ERROR (-1)
    #define XVFS_FILE_BLOCKSIZE 1024

    /*
     * XXX:TODO: Determine this automatically rather than
     *           by heuristics
     */
    #define HAVE_STRUCT_STAT_ST_BLKSIZE 1
    #define HAVE_STRUCT_STAT_ST_BLOCKS  1
    #ifdef WIN32
    #  undef HAVE_STRUCT_STAT_ST_BLKSIZE
    #  undef HAVE_STRUCT_STAT_ST_BLOCKS
    #endif

    #define MIN(a, b) (((a) < (b)) ? (a) : (b))

    typedef enum {
        XVFS_FILE_TYPE_REG,
        XVFS_FILE_TYPE_DIR
    } xvfs_file_type_t;

    typedef Tcl_WideInt xvfs_size_t;

    struct xvfs_file_data {
        const char          *name;
        xvfs_file_type_t    type;
        xvfs_size_t         size;
        union {
                const unsigned char *fileContents;
                const char          **dirChildren;
        } data;
    };

    static struct xvfs_file_data xvfs_example_data[] = {
        {
                .name = "main.tcl",
                .type = XVFS_FILE_TYPE_REG,
                .size = 8730,
                .data.fileContents = (const unsigned char *) "\x23\x21\x20\x2f\x75\x73\x72\x2f\x62\x69"...
        },
        {
                .name = "foo",
                .type = XVFS_FILE_TYPE_REG,
                .size = 12,
                .data.fileContents = (const unsigned char *) "\x46\x6f\x6f\x20\x42\x61\x72\x20\x42\x61"...
        },
        {
                .name = "lib/hello/hello.tcl",
                .type = XVFS_FILE_TYPE_REG,
                .size = 77,
                .data.fileContents = (const unsigned char *) "\x6e\x61\x6d\x65\x73\x70\x61\x63\x65\x20"...
        },
        {
                .name = "lib/hello/pkgIndex.tcl",
                .type = XVFS_FILE_TYPE_REG,
                .size = 66,
                .data.fileContents = (const unsigned char *) "\x70\x61\x63\x6b\x61\x67\x65\x20\x69\x66"...
        },
        {
                .name = "lib/hello",
                .type = XVFS_FILE_TYPE_DIR,
                .size = 2,
                .data.dirChildren  = (const char *[]) {"hello.tcl", "pkgIndex.tcl"}
        },
        {
                .name = "lib",
                .type = XVFS_FILE_TYPE_DIR,
                .size = 1,
                .data.dirChildren  = (const char *[]) {"hello"}
        },
        {
                .name = "",
                .type = XVFS_FILE_TYPE_DIR,
                .size = 3,
                .data.dirChildren  = (const char *[]) {"main.tcl", "lib", "foo"}
        },
    };

    static long xvfs_example_nameToIndex(const char *path) {
        unsigned int pathHash;
        size_t pathLen;

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

        pathLen = strlen(path);
        pathHash = Tcl_ZlibAdler32(0, (const unsigned char *) path, pathLen);
        switch (pathHash) {
                case 0:
                        if (pathLen == 0 && memcmp(path, xvfs_example_data[6].name, pathLen) == 0) {
                                return(6);
                        }
                        break;
                case 41419063:
                        if (pathLen == 3 && memcmp(path, xvfs_example_data[5].name, pathLen) == 0) {
                                return(5);
                        }
                        break;
                case 41877828:
                        if (pathLen == 3 && memcmp(path, xvfs_example_data[1].name, pathLen) == 0) {
                                return(1);
                        }
                        break;
                case 233898774:
                        if (pathLen == 8 && memcmp(path, xvfs_example_data[0].name, pathLen) == 0) {
                                return(0);
                        }
                        break;
                case 285410170:
                        if (pathLen == 9 && memcmp(path, xvfs_example_data[4].name, pathLen) == 0) {
                                return(4);
                        }
                        break;
                case 1197082414:
                        if (pathLen == 19 && memcmp(path, xvfs_example_data[2].name, pathLen) == 0) {
                                return(2);
                        }
                        break;
                case 1596983380:
                        if (pathLen == 22 && memcmp(path, xvfs_example_data[3].name, pathLen) == 0) {
                                return(3);
                        }
                        break;

        }

        return(XVFS_NAME_LOOKUP_ERROR);
    }

    static const char **xvfs_example_getChildren(const char *path, Tcl_WideInt *count) {
        struct xvfs_file_data *fileInfo;
        long inode;

        /*
         * Validate input parameters
         */
        if (count == NULL) {
                return(NULL);
        }

        /*
         * Get the inode from the lookup function
         */
        inode = xvfs_example_nameToIndex(path);
        if (inode == XVFS_NAME_LOOKUP_ERROR) {
                *count = XVFS_RV_ERR_ENOENT;
                return(NULL);
        }

        fileInfo = &xvfs_example_data[inode];

        /*
         * Ensure this is a directory
         */
        if (fileInfo->type != XVFS_FILE_TYPE_DIR) {
                *count = XVFS_RV_ERR_ENOTDIR;
                return(NULL);
        }

        *count = fileInfo->size;
        return(fileInfo->data.dirChildren);
    }

    static const unsigned char *xvfs_example_getData(const char *path, Tcl_WideInt start, Tcl_WideInt *length) {
        struct xvfs_file_data *fileInfo;
        Tcl_WideInt resultLength;
        long inode;

        /*
         * Validate input parameters
         */
        if (length == NULL) {
                return(NULL);
        }

        if (start < 0) {
                *length = XVFS_RV_ERR_EINVAL;
                return(NULL);
        }

        if (*length < 0) {
                *length = XVFS_RV_ERR_EINVAL;
                return(NULL);
        }

        /*
         * Get the inode from the lookup function
         */
        inode = xvfs_example_nameToIndex(path);
        if (inode == XVFS_NAME_LOOKUP_ERROR) {
                *length = XVFS_RV_ERR_ENOENT;
                return(NULL);
        }

        fileInfo = &xvfs_example_data[inode];

        /*
         * Ensure this is a file that can be read
         */
        if (fileInfo->type != XVFS_FILE_TYPE_REG) {
                *length = XVFS_RV_ERR_EISDIR;
                return(NULL);
        }

        /*
         * Validate the length
         */
        if (start > fileInfo->size) {
                *length = XVFS_RV_ERR_EFAULT;
                return(NULL);
        }

        if (*length == 0) {
                resultLength = fileInfo->size - start;
        } else {
                resultLength = MIN(fileInfo->size - start, *length);
        }
        *length = resultLength;

        /*
         * Return the data
         */
        return(fileInfo->data.fileContents + start);
    }

    static int xvfs_example_getStat(const char *path, Tcl_StatBuf *statBuf) {
        struct xvfs_file_data *fileInfo;
        long inode;

        /*
         * Validate input parameters
         */
        if (!statBuf) {
                return(XVFS_RV_ERR_EINVAL);
        }

        /*
         * Get the inode from the lookup function
         */
        inode = xvfs_example_nameToIndex(path);
        if (inode == XVFS_NAME_LOOKUP_ERROR) {
                return(XVFS_RV_ERR_ENOENT);
        }

        fileInfo = &xvfs_example_data[inode];

        statBuf->st_dev   = 0;
        statBuf->st_rdev  = 0;
        statBuf->st_ino   = inode;
        statBuf->st_uid   = -1;
        statBuf->st_gid   = -1;
        statBuf->st_atime = 0;
        statBuf->st_ctime = 0;
        statBuf->st_mtime = 0;
    #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
        statBuf->st_blksize = XVFS_FILE_BLOCKSIZE;
    #endif

        if (fileInfo->type == XVFS_FILE_TYPE_REG) {
                statBuf->st_mode   = 0100444;
                statBuf->st_nlink  = 1;
                statBuf->st_size   = fileInfo->size;
    #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
                statBuf->st_blocks = (fileInfo->size + statBuf->st_blksize - 1) / statBuf->st_blksize;
    #endif
        } else if (fileInfo->type == XVFS_FILE_TYPE_DIR) {
                statBuf->st_mode   = 040555;
                statBuf->st_nlink  = fileInfo->size;
                statBuf->st_size   = fileInfo->size;
    #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
                statBuf->st_blocks = 1;
    #endif
        }

        return(0);
    }

    static struct Xvfs_FSInfo xvfs_example_fsInfo = {
        .protocolVersion = XVFS_PROTOCOL_VERSION,
        .name            = "example",
        .getChildrenProc = xvfs_example_getChildren,
        .getDataProc     = xvfs_example_getData,
        .getStatProc     = xvfs_example_getStat
    };

    int Xvfs_example_Init(Tcl_Interp *interp) {
        int register_ret;

    #ifdef USE_TCL_STUBS
        const char *tclInitStubs_ret;
        /* Initialize Stubs */
        tclInitStubs_ret = Tcl_InitStubs(interp, TCL_PATCH_LEVEL, 0);
        if (!tclInitStubs_ret) {
                return(TCL_ERROR);
        }
    #endif

        register_ret = Xvfs_Register(interp, &xvfs_example_fsInfo);
        if (register_ret != TCL_OK) {
                return(register_ret);
        }

        return(TCL_OK);
    }
    #undef XVFS_NAME_LOOKUP_ERROR
    #undef XVFS_FILE_BLOCKSIZE
    $