-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathivm64-fsgen
executable file
·4243 lines (3888 loc) · 132 KB
/
ivm64-fsgen
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
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
# Preservation Virtual Machine Project
#
# This static filesystem generator allows to recreate a folderless
# read/write static filesystem for testing the ivm64 ecosystem.
#
# By invoking the script with the list of files to include in the filesystem,
# it will print to stdout a C code with the file contents and some primitives
# (open, read, lseek) to access the files:
#
# ivmfs-gen.sh file1.c file2.c ... > ivmfs.c
#
# Note: if the file name includes paths to directories they are
# considered part of the name (i.e. if you use "ivm64-fsgen /path/to/file"
# you need to open it as 'open("/path/to/file"...)'
#
# Note that files or directories are added individually to the filesystem and,
# therefore, to add a folder recursively you may wish to do:
#
# ivm64-fsgen $(find folder_name) ... > ivmfs.c
#
# Then you can link a program with the generated C file, so that the primitives
# included in it will replace those of newlib, therefore enabling to access the
# files in a stardard way:
#
# ivm64-gcc main.c ivmfs.c # Always compile ivmfs.c before libraries
#
# where main.c can be like this:
#
# main(){
# FILE *f = fopen("file1.txt", "r");
# int n = fread(buff, 1, 5, f);
# ...
# }
#
# By default, file data contents are generated by initializing a static array.
# However, data can be generated directly in ivm64 assembly object format which
# does not need to be compiled (only linked).
# To this end, invoke this script with "-o out.o" as first two arguments:
#
# ivm64-fsgen -o out.o $(find folder_name) # assembly object 'out.o' is generated
# # with the filesystem in it
#
# In case of very large filesystem (>4GB), it is recommended to generate data in
# assembly object format, and link it directly with the rest of the project:
# # Directly, generate the assembly object with the files of interest:
# ivm64-fsgen -o ivmfs-large.o $(find folder_name_with_very_large_files)
# # Optionally, generate an empty filesystem (in C, contaning fs primitives):
# ivm64-fsgen > ivmfs-empty.c
# # Link ivmfs-large.o with the rest of the project (optional with an empty fs)
# # Do not forget to place the object with the FS in the first place
# ivm64-gcc ivmfs-large.o ivmfs-empty.c main.c ....
#
# Debugging options:
#
# ivm64-gcc -DIVMFS_DEBUG ivmfs.c ....
# -> print information for each file operation
#
# ivm64-gcc -DIVMFS_DUMPFILES ivmfs.c ....
# -> dump a list of files when the program exits
#
# ivm64-gcc -DIVMFS_DUMPFILECONTENTS ivmfs.c ....
# -> dump the contents of all files when the program exits
#
# Authors:
# Eladio Gutierrez Carrasco
# Sergio Romero Montiel
# Oscar Plata Gonzalez
# * University of Malaga
#
# Date: Ago 2020 - Apr 2024
#cmdline="`basename $0` $@"
cmdname="`basename $0`"
print_header(){
cat << EEOOPP
/*
* iVM/iDA Preservation Virtual Machine Projects
*
* To be linked with the rest of the C files before libraries
*/
EEOOPP
}
init() {
export IVMFS_ROOT="/work"
export IVMFS_OUT_OBJ=
export GLOBAL_FILETABLE=__IVM64_directory0__
export GLOBAL_NFILES=__IVM64_nfiles0__
# Initial directories
ROOT="/"
TMPDIR="/tmp"
DEVDIR="/dev"
IVMFSROOT=$IVMFS_ROOT
# This file emulates standar input
STDIN_EMU_NAME="stdin"
STDIN_FILENAME="$IVMFSROOT""/""$STDIN_EMU_NAME"
# Name assigned to temporal files
UNNAMED_NAME="***unnamed***"
# Standard IO devices
STDIN_NAME="stdin"
STDOUT_NAME="stdout"
STDERR_NAME="stderr"
STDIN_DEVICE="$DEVDIR""/""$STDIN_NAME"
STDOUT_DEVICE="$DEVDIR""/""$STDOUT_NAME"
STDERR_DEVICE="$DEVDIR""/""$STDERR_NAME"
# Other useful char devices
NULL_NAME="null"
ZERO_NAME="zero"
YES_NAME="yes"
NULL_DEVICE="$DEVDIR""/""$NULL_NAME"
ZERO_DEVICE="$DEVDIR""/""$ZERO_NAME"
YES_DEVICE="$DEVDIR""/""$YES_NAME"
# Special IO char devices
BYTES_NAME="bytes"
SAMPLE_NAME="sample"
FRAMEOUT_NAME="frameout"
SAMPLE_DEVICE="$DEVDIR""/""$SAMPLE_NAME"
BYTES_DEVICE="$DEVDIR""/""$BYTES_NAME"
FRAMEOUT_DEVICE="$DEVDIR""/""$FRAMEOUT_NAME"
# Special IO block devices
FRAMEIN_NAME="framein"
PIXELIN_NAME="pixelin"
PIXELOUT_NAME="pixelout"
PIXELOUT_NAME4="pixelout4"
PIXELOUT_NAME2="pixelout2"
PIXELOUT_NAME1="pixelout1"
FRAMEIN_DEVICE="$DEVDIR""/""$FRAMEIN_NAME"
PIXELIN_DEVICE="$DEVDIR""/""$PIXELIN_NAME"
PIXELOUT_DEVICE="$DEVDIR""/""$PIXELOUT_NAME"
PIXELOUT_DEVICE4="$DEVDIR""/""$PIXELOUT_NAME4"
PIXELOUT_DEVICE2="$DEVDIR""/""$PIXELOUT_NAME2"
PIXELOUT_DEVICE1="$DEVDIR""/""$PIXELOUT_NAME1"
}
print_usage(){
cat << UUSSEE
Usage:
$cmdname file1 file2 .... > ivmfs.c
$cmdname -o out.obj file1 file2 ...
UUSSEE
}
parse_args(){
# If called as "ivm64-fgsen -o out.o ...."
# generate assembly object 'out.o'
export IVMFS_OUT_OBJ=
if test "$1" == "-o"; then
if ! test -z "$2"; then
IVMFS_OUT_OBJ="$2"
else
1>&2 print_usage
exit 1
fi
fi
}
printing_asm() {
# Return true if printing assembly object
# instead of C
! test -z "$IVMFS_OUT_OBJ" && return 0
return 1
}
print_preamble() {
cat << EEOOPP
#ifdef __ivm64__
#ifdef __cplusplus
extern "C" {
#endif
#include <limits.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
#include <stdarg.h>
#include <string.h>
#include <libgen.h>
#include <utime.h>
#include <termios.h>
#include <ioctl.h>
// DIRECTORIES
#define ROOT "$ROOT"
#define TMPDIR "$TMPDIR"
#define DEVDIR "$DEVDIR"
// The default initial current directory
#define IVMFSROOT "$IVMFSROOT"
// This file will emulate STDIN
#define STDIN_FILENAME "$STDIN_FILENAME"
// Name assigned to temporal files
#define UNNAMED_NAME "$UNNAMED_NAME"
// standard input/output
#define STDIN_FILENO 0
#define STDOUT_FILENO 1
#define STDERR_FILENO 2
#define FIRST_FILENO 3
// Max number of symbolic link hops to be taken before returning error ELOOP
#define MAX_LOOP 30
#ifndef TERMINAL_COLORS
#define TERMINAL_COLORS 0
#endif
// Set this to 1 to make read() emulate some tty features
// (echo, cannonical mode)
#ifndef IVM_TERMIOS
#define IVM_TERMIOS 1
#endif
// To be used by termios related stuff
#define TTYLINESIZE (16*1024)
////////////////////////////////////////////////////////////////////////////////
// strdup is not a standard C function; it may fail if compiler
// is asked to be strict C compliant (e.g., -std=c++14)
char* strdup(const char* s);
// second way to deal with strdup
//~ #define strdup(buf) strcpy((char*)malloc(strlen(buf)+1), buf)
//~ #undef strdup
////////////////////////////////////////////////////////////////////////////////
// Some extra macros and flags. See newlib/libc/include/sys/_default_fcntl.h
#define __set_errno(val) (*filesystem->errno_p = (val))
#define __get_errno() (*filesystem->errno_p)
#ifndef O_TMPFILE
#define O_TMPFILE 0x800000
#endif
#ifndef O_PATH
#define O_PATH 0x2000000
#endif
#define AT_EMPTY_PATH 16
// When the file grows a multiple of this is allocated
#define BLKSIZE 64
// Allocate these extra blocks when writting
#define EXTRABLKS 8
// MAX/MIN only one evaluation
#define MIN(a,b) ({ __typeof__(a) _a=(a);\\
__typeof__(b) _b=(b);\\
(_a < _b)?_a:_b; })
#define MAX(a,b) ({ __typeof__(a) _a=(a);\\
__typeof__(b) _b=(b);\\
(_a > _b)?_a:_b;})
////////////////////////////////////////////////////////////////////////////////
// DEBUGGING STUFF
////////////////////////////////////////////////////////////////////////////////
// error messages without stderr
extern int printk(const char *format, ...);
#ifdef IVMFS_DUMPFILECONTENTS
#define IVMFS_DUMPFILES
#endif
#ifdef IVMFS_DEBUG_VERBOSE
#define IVMFS_DEBUG
#endif
#ifdef IVMFS_DEBUG
int __ivm64_debug_tab__ = 0;
#define DEBUG_TAB_WIDTH 2
#define DEBUG_TAB_INC do{__ivm64_debug_tab__+=DEBUG_TAB_WIDTH;}while(0)
#define DEBUG_TAB_DEC do{if (__ivm64_debug_tab__>=DEBUG_TAB_WIDTH) __ivm64_debug_tab__-=DEBUG_TAB_WIDTH;}while(0)
#define DEBUG_PRINT_TAB(n) do{ for(int i = 0; i < n; i++) printk(" "); }while(0)
#define DEBUG_ENTRY_CODE(CODE) do{DEBUG_TAB_INC; CODE;}while(0)
#define DEBUG_EXIT_CODE(CODE) do{CODE; DEBUG_TAB_DEC;}while(0)
#define DEBUG_PRINT(...) do{DEBUG_PRINT_TAB(__ivm64_debug_tab__); printk("[%s at %d] ",__func__, __LINE__); printk(__VA_ARGS__);}while(0)
#ifdef IVMFS_DEBUG_VERBOSE
#define DEBUG_ENTRY DEBUG_ENTRY_CODE(DEBUG_PRINT_TAB(__ivm64_debug_tab__); printk("[%s] ENTRY\n",__func__);)
#define DEBUG_EXIT DEBUG_EXIT_CODE(DEBUG_PRINT_TAB(__ivm64_debug_tab__); printk("[%s] EXIT\n",__func__);)
#define DEBUG_PRINT_VERBOSE DEBUG_PRINT
#else
#define DEBUG_ENTRY DEBUG_ENTRY_CODE()
#define DEBUG_EXIT DEBUG_EXIT_CODE()
#define DEBUG_PRINT_VERBOSE(...)
#endif
#define DEBUG_COND_PRINT(c,...) do{ if (c) { DEBUG_PRINT(__VA_ARGS__); } }while(0)
// debugging, warn on strdup
#define strdup(buf)\\
({\\
size_t len = strnlen(buf, PATH_MAX);\\
if (len > PATH_MAX) printk("[%s at %d] DUPLICATING STRING %ld\n",\\
__func__, __LINE__, len);\\
strcpy((char*)malloc(len+1), buf);\\
})
#else
#define DEBUG_PRINT_VERBOSE(...)
#define DEBUG_COND_PRINT(c,...)
#define DEBUG_PRINT(...)
#define DEBUG_ENTRY
#define DEBUG_EXIT
#endif
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// filesystem data structures (filetable)
////////////////////////////////////////////////////////////////////////////////
typedef struct {
unsigned long size;
unsigned long allocated;
long links; // number of hardlinks to this inode (used as pointer, see push/pop inode)
char *data;
mode_t mode; // 0754 -> rwxr-xr--
unsigned int inodeFixed;// If 1, do not free this struct
unsigned int dataFixed; // If 1, do not free data
} inode_t;
typedef struct { // entry in directory
char* pathname; // the fullpath of the file
inode_t *inode; // pointer to the inode of the file
long links; // number of file descriptors with this entry in use
} dentry_t;
typedef struct {
unsigned long nfiles; // Files initially in the filesystem
unsigned long allocated;
dentry_t *directory; // The current filesystem table
long last_dentry_idx;
inode_t *inode_free_list;
} filesystem_data_t;
typedef struct { // Open file description structure
long idx; // Entry in Filetable/Directory (dentry_t)
unsigned long pos; // position -> read, write, lseek,...
unsigned int flags; // flags passed to open: access mode, append,...
unsigned int links; // number of file descriptors using this description (dup, dup2)
inode_t *inode; // redundant, also accessed via directory[idx]
} file_t;
typedef struct {
unsigned long size; // size of openfile table
file_t **fd; // Open files table
} filesystem_file_t;
typedef struct { // Current working directory
char *soft; // The cwd in the user view (softlinks dirs)
char *real; // The cwd in the real path space
} filesystem_cwd_t;
typedef struct { // Spawn programs use these pointers to work in host program space
void (*_cleanupdir)(DIR *dirp);
void (*_seekdir)(DIR *dirp, long loc);
int (*chdir)(const char *path);
int (*close)(int fd);
int (*dup)(int oldfd);
int (*dup2)(int oldfd, int newfd);
int (*faccessat)(int dirfd, const char *pathname, int mode, int flags);
int (*fchdir)(int fd);
int (*fchmod)(int fd, mode_t mode);
int (*fchmodat)(int dirfd, const char *pathname, mode_t mode, int flags);
int (*fcntl)(int fd, int cmd, va_list arg);
int (*fdatasync)(int fd);
int (*fstat)(int fd, struct stat *st);
int (*fstatat)(int dirfd, const char *pathname, struct stat *statbuf, int flags);
int (*fsync)(int fd);
int (*ftruncate)(int fd, off_t length);
char* (*getcwd)(char *buf, size_t size);
long (*getdents)(int fd, struct dirent *dirp, size_t count);
int (*ioctl)(int fd, unsigned long request, va_list arg);
int (*isatty)(int fd);
int (*link)(const char *oldpath, const char *newpath);
int (*linkat)(int olddirfd, const char *oldpath, int newdirfd, const char *newpath, int flags);
off_t (*lseek)(int file, off_t offset, int whence);
int (*lstat)(const char *pathname, struct stat *st);
int (*openat)(int dirfd, const char *pathname, int flags, ...);
ssize_t (*read)(int fd, void *buf, size_t count);
struct dirent* (*readdir)(DIR *dirp);
ssize_t (*readlinkat)(int dirfd, const char *pathname, char *buf, size_t bufsiz);
char* (*realpath)(const char *pathname, char *canon_name);
int (*renameat)(int olddirfd, const char *oldpath, int newdirfd, const char *newpath);
int (*rmdir)(const char *pathname);
int (*stat)(const char *file, struct stat *st);
int (*symlinkat)(const char *target, int newdirdf, const char *linkpath);
long (*telldir)(DIR *dirp);
int (*truncate)(const char *path, off_t length);
mode_t (*umask)(mode_t mask);
int (*unlinkat)(int dirfd, const char *pathname, int flags);
int (*utime)(const char *filename, const struct utimbuf *times);
int (*utimes)(const char *filename, const struct timeval times[2]);
ssize_t (*write)(int fd, const void *buf, size_t count);
} filesystem_oper_t;
typedef struct { // Spawn programs use this structure to work in host program space
filesystem_oper_t oper; // all filesystem operations
filesystem_data_t data; // file table
filesystem_file_t fd; // open files
filesystem_cwd_t cwd; // Current work directory
struct termios tty_termios;
mode_t umask;
int * errno_p;
} filesystem_t;
EEOOPP
}
print_files() {
if ! which hexdump >& /dev/null; then
>&2 echo "hexdump not found; it is required by this script"
exit 1
fi
# Functions to generate C code for static array initialization with the file content
# Dump 8-byte words (hexdump not supporting 16/8, so concatenating parts with sed)
hdf() { hexdump -v -e '16/4 "0x%08x, " "\n"' < "$1" | sed -E 's/, 0x +//g' | sed -E 's/0x([0-9a-f]+)\s*,\s*0x([0-9a-f]+)/0x\2\1/g'; }
# The same, but with some unrolling for higher performance
hdf8n() { hexdump -v -e '16/4 "%08x " "\n"' < "$1" |
gawk '{
if (length($2 )) {printf "0x%s%s, ", $2, $1;} else {printf "0x00000000%s, ", $1}
if (length($4 )) {printf "0x%s%s, ", $4, $3;} else {printf "0x00000000%s, ", $3}
if (length($6 )) {printf "0x%s%s, ", $6, $5;} else {printf "0x00000000%s, ", $5}
if (length($8 )) {printf "0x%s%s, ", $8, $7;} else {printf "0x00000000%s, ", $7}
if (length($10)) {printf "0x%s%s, ", $10, $9;} else {printf "0x00000000%s, ", $9}
if (length($12)) {printf "0x%s%s, ", $12, $11;} else {printf "0x00000000%s, ", $11}
if (length($14)) {printf "0x%s%s, ", $14, $13;} else {printf "0x00000000%s, ", $13}
if (length($16)) {printf "0x%s%s,", $16, $15;} else {printf "0x00000000%s,", $15}
print ""
}' ;
}
hdf() { hdf8n "$@" ; }
# Functions to generate file contents as embedded ivm64 asm
hdf8n_list() { hexdump -v -e '16/4 "%08x " "\n"' < "$1" |
gawk '{
if (length($2 )) {printf "0x%s%s ", $2, $1;} else {printf "0x00000000%s ", $1}
if (length($4 )) {printf "0x%s%s ", $4, $3;} else {printf "0x00000000%s ", $3}
if (length($6 )) {printf "0x%s%s ", $6, $5;} else {printf "0x00000000%s ", $5}
if (length($8 )) {printf "0x%s%s ", $8, $7;} else {printf "0x00000000%s ", $7}
if (length($10)) {printf "0x%s%s ", $10, $9;} else {printf "0x00000000%s ", $9}
if (length($12)) {printf "0x%s%s ", $12, $11;} else {printf "0x00000000%s ", $11}
if (length($14)) {printf "0x%s%s ", $14, $13;} else {printf "0x00000000%s ", $13}
if (length($16)) {printf "0x%s%s ", $16, $15;} else {printf "0x00000000%s ", $15}
print ""
}' ;
}
hdf8n_asm() { hdf8n_list "$@" | gawk '{ print "data8 [" $0 "]"}' ; }
hdf_asm() { hdf8n_asm "$@" ; }
# Function to print a string in asm
asm_str(){ echo -n "$1" | hexdump -v -e '1/1 "%d" " "' | gawk '{ print "data1 [" $0 0 "]"}' ; }
# Function to print the string to be used as filename in a table file entyr
# Usage:
# asm_string label string
asm_string(){
# $1=label, $2=string
# Note that eventually this string must be print with "echo -e"
# to parse \n as newlines
echo -n "$1:\n#\t.string \"${2}\"\n\t$(asm_str "$2")"
}
# Associative arrays for file table entries
declare -A inode_entry
declare -A file_entry
declare -A dir_entry
# An associative array with the files already processed
declare -A seenfile
# An associative array for extern clauses with the label named associated
# to file data
declare -A asmfilenamestr
nf=0
nd=0
nt=0
# from gcc-12.2.0-ivm64/ivm64/include/sys/stat.h
_IFDIR=0040000 #/* directory */
_IFCHR=0020000 #/* character special */
_IFBLK=0060000 #/* block special */
_IFREG=0100000 #/* regular */
_IFLNK=0120000 #/* symbolic link */
# Function to set up inode_entry[], dir_entry[] (and asmfilenamestr[] if needed) for a DIRECTORY
# Usage:
# filetable_dir_entry directory_name c_mode
# $1 $2
filetable_dir_entry(){
seenfile[$1]=1
if printing_asm; then
inodelabel=".LIVMFS_INODE${nt}.${ASM_DATA_OBJID}"
filenamelabel=".LIVMFS_FN${nt}.${ASM_DATA_OBJID}"
mode=$(( _IFDIR | $2 ))
inode_entry[$nt]="$inodelabel:\n\tdata8 [0 0 1 0]\n\tdata4 [$mode 1 1 0]"
asmfilenamestr[$nt]=$(asm_string $filenamelabel $1)
dir_entry[$nd]="data8 [$filenamelabel $inodelabel 0]\n"
else
inode_entry[$nt]="static inode_t inode${nt} = (inode_t){ 0, 0, 1, NULL, (S_IFDIR | $2), 1, 1 };"
dir_entry[$nd]="{(char*)\"$1\", &inode${nt}, 0}"
fi
let nd=nd+1
let nt=nt+1
}
# Function to set up inode_entry[], dir_entry[] (and asmfilenamestr[] if needed) for a FILE
# Usage:
# filetable_file_entry file_name file
# $1 $2
filetable_file_entry(){
seenfile[$1]=1
size=$(stat -c %s "$2")
mode=$(stat -c %#a "$2") # mode in octal
if printing_asm; then
filedatalabel=".LIVMFS_FC${nt}.${ASM_DATA_OBJID}"
inodelabel=".LIVMFS_INODE${nt}.${ASM_DATA_OBJID}"
filenamelabel=".LIVMFS_FN${nt}.${ASM_DATA_OBJID}"
echo -e "$filedatalabel:"
hdf_asm "$2"
mode=$(( $mode | _IFREG ))
inode_entry[$nt]="$inodelabel:\n\tdata8 [$size $size 1 $filedatalabel]\n\tdata4 [$mode 1 1 0]"
asmfilenamestr[$nt]=$(asm_string $filenamelabel $1)
file_entry[$nf]="data8 [$filenamelabel $inodelabel 0]\n"
else
ptrname=content_of_file_${nt}
echo "static uint64_t $ptrname[]={"
hdf "$2"
echo "};"
inode_entry[$nt]="static inode_t inode${nt} = (inode_t){ $size, $size, 1, (char*)$ptrname, (S_IFREG | $mode), 1, 1 };"
file_entry[$nf]="{(char*)\"$1\", &inode${nt}, 0}"
fi
let nf=nf+1
let nt=nt+1
}
# Function to set up inode_entry[], dir_entry[] (and asmfilenamestr[] if needed) for a DEVICE
# Usage:
# filetable_dev_entry file_var_name type mode c_var_string
# $1 $2 $3 $4
filetable_dev_entry(){
seenfile[$1]=1
if printing_asm; then
filedatalabel=".LIVMFS_FC${nt}.${ASM_DATA_OBJID}"
inodelabel=".LIVMFS_INODE${nt}.${ASM_DATA_OBJID}"
filenamelabel=".LIVMFS_FN${nt}.${ASM_DATA_OBJID}"
mode=$(( ${!2} | $3 ))
echo -e $(asm_string $filedatalabel "${!4}")
inode_entry[$nt]="$inodelabel:\n\tdata8 [0 0 1 $filedatalabel]\n\tdata4 [$mode 1 1 0]"
asmfilenamestr[$nt]=$(asm_string $filenamelabel ${!1})
file_entry[$nf]="data8 [$filenamelabel $inodelabel 0]\n"
else
# Insert C macros for the device name/path
echo "#define $4 \"${!4}\""
echo "#define $1 \"${!1}\""
mode="(S$2 | $3)"
inode_entry[$nt]="static inode_t inode${nt} = (inode_t){ 0, 0, 1, $4, $mode, 1, 1 };"
file_entry[$nf]="{(char*)$1, &inode${nt}, 0}"
fi
let nf=nf+1
let nt=nt+1
}
# Function to set up inode_entry[], dir_entry[] (and asmfilenamestr[] if needed) for a DEVICE
# Usage:
# filetable_lnk_entry file_name file
# $1 $2
filetable_lnk_entry(){
seenfile[$1]=1
size=$(( $(stat -c %s "$2") + 1))
symlink=$(readlink "$2")
if printing_asm; then
filedatalabel=".LIVMFS_FC${nt}.${ASM_DATA_OBJID}"
echo -e $(asm_string $filedatalabel $symlink)
inodelabel=".LIVMFS_INODE${nt}.${ASM_DATA_OBJID}"
filenamelabel=".LIVMFS_FN${nt}.${ASM_DATA_OBJID}"
mode=$(( _IFLNK | 0777 )) # mode 0777 for symlinks
inode_entry[$nt]="$inodelabel:\n\tdata8 [$size $size 1 $filedatalabel]\n\tdata4 [$mode 1 1 0]"
asmfilenamestr[$nt]=$(asm_string $filenamelabel $1)
file_entry[$nf]="data8 [$filenamelabel $inodelabel 0]\n"
else
inode_entry[$nt]="static inode_t inode${nt} = (inode_t){ $size, $size, 1, \"$symlink\", (S_IFLNK | 0777), 1, 1 };"
file_entry[$nf]="{(char*)\"$1\", &inode${nt}, 0}"
fi
let nf=nf+1
let nt=nt+1
}
if printing_asm; then
export ASM_DATA_OBJID=oivmfs$RANDOM
cat << EEOOFF
##:ivm64:obj-id: $ASM_DATA_OBJID
# This is a GCC-generated ivm64 assembly object file
EEOOFF
fi
# Add always directories: "/", "/tmp", "/dev" & "/work"
filetable_dir_entry $ROOT 0777
filetable_dir_entry $TMPDIR 0777
filetable_dir_entry $DEVDIR 0777
filetable_dir_entry $IVMFSROOT 0777
#
# Char devices stdin, stdout, stderr, null, zero,...
filetable_dev_entry STDIN_DEVICE _IFCHR 0444 STDIN_NAME
filetable_dev_entry STDOUT_DEVICE _IFCHR 0222 STDOUT_NAME
filetable_dev_entry STDERR_DEVICE _IFCHR 0222 STDERR_NAME
filetable_dev_entry NULL_DEVICE _IFCHR 0666 NULL_NAME
filetable_dev_entry ZERO_DEVICE _IFCHR 0444 ZERO_NAME
filetable_dev_entry YES_DEVICE _IFCHR 0444 YES_NAME
filetable_dev_entry BYTES_DEVICE _IFCHR 0222 BYTES_NAME
filetable_dev_entry SAMPLE_DEVICE _IFCHR 0222 SAMPLE_NAME
filetable_dev_entry FRAMEOUT_DEVICE _IFCHR 0222 FRAMEOUT_NAME
filetable_dev_entry FRAMEIN_DEVICE _IFBLK 0444 FRAMEIN_NAME
filetable_dev_entry PIXELIN_DEVICE _IFBLK 0444 PIXELIN_NAME
filetable_dev_entry PIXELOUT_DEVICE _IFBLK 0222 PIXELOUT_NAME
filetable_dev_entry PIXELOUT_DEVICE4 _IFBLK 0222 PIXELOUT_NAME4
filetable_dev_entry PIXELOUT_DEVICE2 _IFBLK 0222 PIXELOUT_NAME2
filetable_dev_entry PIXELOUT_DEVICE1 _IFBLK 0222 PIXELOUT_NAME1
#
while test $# -gt 0
do
# Get relative/absolute path
if [[ "$1" =~ ^/.* ]] ; then
#Absolute name
ivmfsroot=
filename=$(realpath -m "$1")
else
basename=$(basename $1)
dirname=$(dirname $1)
rpath="$(realpath -m --relative-to=. "$dirname")"
if [[ "$rpath" =~ ^\.\. ]] ; then
#Filename not relative to ., absolutize it
ivmfsroot=$(realpath -m $rpath)
elif [[ "$rpath" =~ ^\. ]] ; then
#Filename relative to .
ivmfsroot=$IVMFSROOT
else
#Filename relative to .
ivmfsroot=$IVMFSROOT"/"$rpath
fi
filename="$ivmfsroot""/""$basename"
fi
if ! test -z ${seenfile["$filename"]}; then
# Do no process the same file twice
shift
continue
fi
if ! test -z "$1" && test -h "$1"
# include symbolic links
then
size=$(stat -c %s "$1") # the size of a symbolic link does not include the nul terminator
filetable_lnk_entry $filename $1
elif ! test -z "$1" && test -f "$1"
# include existing regular files whose name is not an empty string
then
filetable_file_entry $filename $1
elif ! test -z "$1" && test -d "$1"
# include directories
then
filetable_dir_entry $filename 0777
else
filetable_dir_entry $filename 0777
fi
seenfile["$filename"]=1
# Traverse all dirs in the filename patand append them to the argument list,
# to include also these dirs in the file system
dirname="$(dirname "$filename")"
dirlist=
while test -z ${seenfile["$dirname"]} && [[ "$dirname" != "." ]] && [[ "$dirname" != "/" ]]
do
dirlist="$dirname $dirlist"
dirname="$(dirname "$dirname")"
done
if ! test -z "${dirlist}"
then
set -- "$@" $dirlist
fi
shift
done
# For each file, generate a statement initializing the inode struct of type inode_t:
# // (size, allocated, attr, links, inodeFix, dataFix, data)
# { 5, 5, S_IFREG | 0640, 1, 1, 1, (char*)content_of_fileXX }
# { 0, 0, S_IFDIR | 0777, 1, 1, 1, NULL }
# { 5, 5, S_IFLNK | 0777, 1, 1, 1, "datao" }
# { 0, 0, S_IFCHR | 0444, 1, 1, 1, "stdin" }
# { 0, 0, S_IFBLK | 0666, 1, 1, 1, "frame" }
# Print inodes
for i in ${!inode_entry[@]}
#~ for i in `seq 0 $(( $nt - 1 ))`
do
echo -e ${inode_entry[$i]}
done
# Number of entries (files+directories)
n=$(($nf + $nd))
# Print all initialization statements into one only array:
# static dentry_t directory0[NFILES] =
# {
# {"/", &inode0, 0},
# {"/work", &inode1, 0},
# ...
# {"file8.txt", &inode8, 0},
# {"file9.txt", &inode9, 0},
# ...
# };
# static dentry_t *directory = directory0;
# Fix a number of max files
MAXFILES=$(($n))
if printing_asm; then
# Print the asm string for each existing file name
for f in ${!asmfilenamestr[@]}
#~ for ((f=0; f<$nt; f++))
do
echo -e ${asmfilenamestr[$f]}
done
echo
# When printing data in asm format, the filetable pointer is the
# only external (exported) label
echo "## Initial filesystem table"
echo "EXPORT ${GLOBAL_NFILES}"
echo -e "${GLOBAL_NFILES}:\n data8[$MAXFILES]"
echo "EXPORT $GLOBAL_FILETABLE"
echo "$GLOBAL_FILETABLE:"
else
# Starting table when printing C
echo ''
echo ''
cat << EEOOFF
#ifndef NAME_MAX
#define NAME_MAX 256
#endif
#ifndef PATH_MAX
#define PATH_MAX 4096
#endif
#define MAX_FILES $MAXFILES
// Initial filesystem table"
volatile uint64_t ${GLOBAL_NFILES} = MAX_FILES;
dentry_t $GLOBAL_FILETABLE[MAX_FILES] = {
EEOOFF
fi
# First, print directories
#~ for d in ${!dir_entry[@]}
for ((d=0; d<$nd; d++))
do
echo -e -n ' ' ${dir_entry[$d]}
! printing_asm && echo ','
done
# Next, print regular files
for f in ${!file_entry[@]}
#~ for ((f=0; f<$nf; f++))
do
echo -e -n ' ' ${file_entry[$f]}
! printing_asm && echo ','
done
#echo ' {NULL, NULL, 0}'
if ! printing_asm ; then
# Closing table when printing C
echo '};'
echo ''
fi
}
print_structure() {
cat << EEOOFF
static void _cleanupdir0(DIR *dirp);
static void _seekdir0(DIR *dirp, long loc);
static int chdir0(const char *path);
static int close0(int fd);
static struct dirent* readdir0(DIR *dirp);
static int dup0(int oldfd);
static int dup20(int oldfd, int newfd);
static int faccessat0(int dirfd, const char *pathname, int mode, int flags);
static int fchdir0(int fd);
static int fchmod0(int fd, mode_t mode);
static int fchmodat0(int dirfd, const char *pathname, mode_t mode, int flags);
static int fcntl0(int fd, int cmd, va_list arg);
static int fdatasync0(int fd);
static int fstat0(int fd, struct stat *st);
static int fstatat0(int dirfd, const char *pathname, struct stat *statbuf, int flags);
static int fsync0(int fd);
static int ftruncate0(int fd, off_t length);
static char* getcwd0(char *buf, size_t size);
static long getdents0(int fd, struct dirent *dirp, size_t count);
static int ioctl0(int fd, unsigned long request, va_list arg);
static int isatty0(int fd);
static int linkat0(int olddirfd, const char *oldpath, int newdirfd, const char *newpath, int flags);
static off_t lseek0(int file, off_t offset, int whence);
static int openat0(int dirfd, const char *pathname, int flags, ...);
static ssize_t read0(int fd, void *vbuf, size_t len);
static ssize_t readlinkat0(int dirfd, const char *pathname, char *buf, size_t bufsiz);
static char* realpath0(const char *pathpath, char *resolved_path);
static int renameat0(int olddirfd, const char *oldpath, int newdirfd, const char *newpath);
static int symlinkat0(const char *target, int newdirfd, const char *linkpath);
static long telldir0(DIR *dirp);
static int truncate0(const char *path, off_t length);
static mode_t umask0(mode_t mask);
static int unlinkat0(int dirfd, const char *pathname, int flags);
static int utime0(const char *filename, const struct utimbuf *times);
static int utimes0(const char *filename, const struct timeval times[2]);
static ssize_t write0(int fd, const void *vptr, size_t nbytes);
static int errno0;
static filesystem_t filesystem0 = {
oper: {
_cleanupdir: _cleanupdir0,
_seekdir: _seekdir0,
chdir: chdir0,
close: close0,
dup: dup0,
dup2: dup20,
faccessat: faccessat0,
fchdir: fchdir0,
fchmod: fchmod0,
fchmodat: fchmodat0,
fcntl: fcntl0,
fdatasync: fdatasync0,
fstat: fstat0,
fstatat: fstatat0,
fsync: fsync0,
ftruncate: ftruncate0,
getcwd: getcwd0,
getdents: getdents0,
ioctl: ioctl0,
isatty: isatty0,
linkat: linkat0,
lseek: lseek0,
openat: openat0,
read: read0,
readdir: readdir0,
readlinkat: readlinkat0,
realpath: realpath0,
renameat: renameat0,
symlinkat: symlinkat0,
telldir: telldir0,
truncate: truncate0,
umask: umask0,
unlinkat: unlinkat0,
utime: utime0,
utimes: utimes0,
write: write0,
},
data: {
nfiles: 0,
allocated: 0,
directory: ${GLOBAL_FILETABLE},
last_dentry_idx: -1,
inode_free_list: NULL,
},
fd: {
size: 0,
fd: NULL,
},
cwd: {
soft: NULL,
real: NULL,
},
tty_termios: {},
umask: 0,
errno_p: &errno0,
};
static filesystem_t *filesystem = &filesystem0;
static long openfile_get_directory_entry(file_t *p) {return (p->idx - 1);}
static unsigned long openfile_get_position(file_t *p) {return p->pos;}
static unsigned int openfile_get_flags(file_t *p) {return p->flags;}
static inode_t* openfile_get_inode(file_t *p) {return p->inode;}
static void openfile_set_directory_entry(file_t *p, long idx) {p->idx = idx + 1;}
static void openfile_set_position(file_t *p, unsigned long pos) {p->pos = pos;}
static void openfile_set_links(file_t *p, unsigned int links) {p->links = links;}
static void openfile_set_flags(file_t *p, unsigned int flags) {p->flags = flags;}
static void openfile_set_inode(file_t *p, inode_t *inode) {p->inode = inode;}
static void openfile_link(file_t *p) {p->links++;}
static void openfile_unlink(file_t *p) {if (!--p->links) free(p);}
static file_t *new_openfile(long idx, inode_t *inode_p, int flags)
{
DEBUG_ENTRY;
file_t *new_entry = (file_t *)malloc(sizeof(file_t));
if (!new_entry) {
__set_errno(ENOSPC);
DEBUG_EXIT;
return NULL;
}
openfile_set_directory_entry(new_entry, idx);
openfile_set_position(new_entry, 0);
openfile_set_flags(new_entry, flags);
openfile_set_links(new_entry, 0);
openfile_set_inode(new_entry, inode_p);
DEBUG_EXIT;
return new_entry;
}
#define OPENFILE_GET_INODE(file)\\
({\\
inode_t *inode_p = openfile_get_inode(file);\\
if (inode_p == NULL) {\\
DEBUG_PRINT("failed (inode is nul)\n");\\
DEBUG_EXIT;\\
__set_errno(EIO);\\
return -1;\\
}\\
inode_p;\\
})
static char *inode_get_data(inode_t *ino) {return ino->data;}
static void inode_set_data(inode_t *ino, char *data) {ino->data = data;}
static ssize_t inode_get_size(inode_t *ino) {return ino->size;}
static void inode_set_size(inode_t *ino, ssize_t size) {ino->size = size;}
static ssize_t inode_get_allocated(inode_t *ino) {return ino->allocated;}
static void inode_set_allocated(inode_t *ino, ssize_t allocated){ino->allocated = allocated;}
static mode_t inode_get_mode(inode_t *ino) {return ino->mode;}
static void inode_set_mode(inode_t *ino, mode_t mode) {ino->mode = mode; }
static long inode_get_links(inode_t *ino) {return ino->links;}
static void inode_set_links(inode_t *ino, long links) {ino->links = links;}
static inode_t* inode_dup(inode_t *ino) {ino->links++; return ino;}
int inode_get_filetype(inode_t *ino)
{
mode_t mode = inode_get_mode(ino);
return S_ISLNK(mode)? DT_LNK:
S_ISDIR(mode)? DT_DIR:
S_ISCHR(mode)? DT_CHR:
S_ISBLK(mode)? DT_BLK:
S_ISREG(mode)? DT_REG: DT_UNKNOWN;
}
static void push_inode(inode_t *ino)
{
if (ino) {
inode_set_links(ino, (long)filesystem->data.inode_free_list);
filesystem->data.inode_free_list = ino;
}
}
static inode_t* pop_inode()
{
inode_t *last = filesystem->data.inode_free_list;
if (last) {
filesystem->data.inode_free_list = (inode_t*)inode_get_links(last);
}
return last;
}
static inode_t* new_inode(mode_t mode)