-
Notifications
You must be signed in to change notification settings - Fork 1
/
filesys.h
140 lines (102 loc) · 3.48 KB
/
filesys.h
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
/* filesys.h
*
* describes FAT structures
* http://www.c-jump.com/CIS24/Slides/FAT/lecture.html#F01_0020_fat
* http://www.tavi.co.uk/phobos/fat.html
*/
#ifndef FILESYS_H
#define FILESYS_H
#include <time.h>
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
#define MAXBLOCKS 1024
#define BLOCKSIZE 1024
#define FATENTRYCOUNT (BLOCKSIZE / sizeof(fatentry_t))
#define DIRENTRYCOUNT ((BLOCKSIZE - (2*sizeof(int)) ) / sizeof(direntry_t))
#define MAXNAME 256
#define MAXPATHLENGTH 1024
#define UNUSED -1
#define ENDOFCHAIN 0
#define EOF -1
typedef unsigned char Byte ;
/* create a type fatentry_t, we set this currently to short (16-bit)
*/
typedef short fatentry_t ;
// a FAT block is a list of 16-bit entries that form a chain of disk addresses
//const int fatentrycount = (blocksize / sizeof(fatentry_t)) ;
typedef fatentry_t fatblock_t [ FATENTRYCOUNT ] ;
/* create a type direntry_t
*/
typedef struct direntry {
int entrylength ; // records length of this entry (can be used with names of variables length)
Byte isdir ;
Byte unused ;
time_t modtime ;
int filelength ;
fatentry_t firstblock ;
char name [MAXNAME] ;
} direntry_t ;
// a directory block is an array of directory entries
//const int direntrycount = (blocksize - (2*sizeof(int)) ) / sizeof(direntry_t) ;
typedef struct dirblock {
int isdir ;
int nextEntry ;
direntry_t entrylist [ DIRENTRYCOUNT ] ; // the first two integer are marker and endpos
} dirblock_t ;
// a data block holds the actual data of a filelength, it is an array of 8-bit (byte) elements
typedef Byte datablock_t [ BLOCKSIZE ] ;
// a diskblock can be either a directory block, a FAT block or actual data
typedef union block {
datablock_t data ;
dirblock_t dir ;
fatblock_t fat ;
} diskblock_t ;
// finally, this is the disk: a list of diskblocks
// the disk is declared as extern, as it is shared in the program
// it has to be defined in the main program filelength
extern diskblock_t virtualDisk [ MAXBLOCKS ] ;
// when a file is opened on this disk, a file handle has to be
// created in the opening program
typedef struct filedescriptor {
char mode[3] ;
fatentry_t blockno ; // block no
int pos ; // byte within a block
diskblock_t buffer ;
} MyFILE ;
/* ======================================= D3_D1 FUNCTIONS */
void format() ;
/* ======================================= C3_C1 FUNCTIONS */
MyFILE * myfopen ( const char *, const char * ) ;
void myfclose ( MyFILE * );
void myfputc ( int , MyFILE * );
int myfgetc ( MyFILE * );
/* ======================================= B3_B1 FUNCTIONS */
void mymkdir(char *);
char **mylistdir(char *);
/* ======================================= A5_A1 FUNCTIONS */
void mychdir(char *);
void myremove(char *);
void myrmdir(char *);
/* ======================================= HELPER FUNCTIONS */
void writedisk ( const char * ) ;
void printBlock ( int );
void copyFAT();
void moveToBlock (diskblock_t *, int);
int nextUnusedBlock();
int nextUnusedDir();
int fileLocation(const char *);
void cleanFAT(int);
diskblock_t freeDirBlock();
int fileEntryIndex(const char *);
void printContents(char **);
int isDirEmpty(dirblock_t);
/* ======================================= EXTRA */
void myfvdsys(const char *, const char *);
void myfsysvd(const char *, const char * );
void myfcopy (char *, char* );
void myfmove (char *, char* );
#endif