-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilesys.h
155 lines (113 loc) · 4.08 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/* 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>
#include <pthread.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 64
#define MAXPATHLENGTH 1024
#define UNUSED -1
#define ENDOFCHAIN 0
#define EOF_ 255 //changed from -1 to 255, due to "Byte" being an unsigned char and -1 just wraps around to 255 anyway
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 used ;
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 voltume block to store all the data to desribe the block 0, along with the mutex lock
typedef struct volumeblock {
pthread_mutex_t lock ; //mutex lock variable
char name [MAXNAME]; //the name of this volume
} volumeblock_t ;
// 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 ;
volumeblock_t vol;
} 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 ;
void format() ;
void writedisk ( const char * filename ) ;
diskblock_t emptyBlock();
void copyFAT();
void writeVirtualDisk(char* name);
MyFILE* myfopen(const char* filename, const char* mode);
void myfputc(int b, MyFILE* stream);
int myfgetc(MyFILE* stream);
void myfclose(MyFILE* stream);
dirblock_t* traverseToDir(char* path, int createWhenNotFound);
int createDirBlock(dirblock_t* parent, int isdir);
int addDirBlockToEntryList(dirblock_t* parent, int pos, int isdir, char* name);
int doesExist(diskblock_t* block, char* name);
int getFreeFATPos();
void mymkdir(char* path);
char** mylistdir(char* path);
void deleteDirBlock(int index);
void deleteDirBlockFromEntryList(dirblock_t* parent, dirblock_t* entry);
void mychdir(char* path);
void myremove(char* path);
void myrmdir(char* path);
void loadRealFile(char* source, char* dest);
void mycpy(char* source, char* dest);
void mymove(char* source, char* dest);
pthread_mutex_t* getLockVar();
//Extra functionality for A1
direntry_t* locate(char* startingPath, char* filename);
direntry_t* searchDir(dirblock_t* start, char* filename);
#endif
/*
#define NUM_TYPES (sizeof types / sizeof types[0])
static* int types[] = {
1,
2,
3,
4 };
*/