-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshell.c
190 lines (151 loc) · 4.27 KB
/
shell.c
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
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include "filesys.h"
pthread_t tid[2];
void genTestBytes(MyFILE* stream, int blocks) {
//insert a text of size 4kb
char* str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int counter = 0;
for (int i = 0; i < (blocks*BLOCKSIZE); i++) {
if (counter >25) {
counter = 0;
}
myfputc(str[counter], stream);
counter++;
}
myfputc(EOF_, stream);
}
void readTestBytes(MyFILE* stream) {
FILE *f = fopen("testfileC3_C1_copy.txt", "w");
printf("File contents start:\n");
while(1) {
int b = myfgetc(stream);
if (b == EOF_) {
break;
}
printf("%c", b);
fputc(b, f);
}
printf("\nFile contents end:\n");
fclose(f);
}
void printDir(char** list) {
while (*list != NULL) {
printf("%s\n", *list);
list++;
}
}
void writeFile(char* path, int blocks) {
//writing the test bytes
MyFILE* file = myfopen(path, "w");
if (!file) {
printf("Could not open file\n");
return;
}
//printf("starting block no: %d\n", file->blockno);
genTestBytes(file, blocks);
myfclose(file);
}
void readFile() {
//reading the test bytes
MyFILE* file = myfopen("testfile.txt", "r");
if (!file) {
printf("Could not open file\n");
return;
}
printf("starting block no: %d\n", file->blockno);
readTestBytes(file);
}
void printList(char** list) {
if (list == NULL) {
return;
}
while (*list != NULL) {
printf("--%s\n", *list);
list++;
}
}
void* threadCallback(void *arg) {
pthread_mutex_t* lock = getLockVar();
pthread_mutex_lock(lock); //Lock the mutex inside the virtual file system
//Manipulations on the file system are now thread safe!
writeFile("/firstdir/seconddir/testfile1.txt", 4);
printList(mylistdir("/firstdir/seconddir"));
myremove("/firstdir/seconddir/testfile1.txt");
pthread_mutex_unlock(lock); //relaese the virtual file system lock
return NULL;
}
void initThreads() {
int i = 0;
int err;
pthread_mutex_t* lock = getLockVar();
if (pthread_mutex_init(lock, NULL) != 0) {
printf("Mutex init failed\n");
return;
}
printf("LOCK VAR %p\n", lock);
while (i < 2) {
err = pthread_create(&(tid[i]), NULL, &threadCallback, NULL);
if (err != 0) {
printf("Could not create thread: %s", err);
}
i++;
}
pthread_join(tid[0], NULL);
pthread_join(tid[1], NULL);
pthread_mutex_destroy(lock);
}
void tests() {
format();
printf("1:----------------\n");
mymkdir("/firstdir/seconddir");
writeFile("/firstdir/seconddir/testfile1.txt", 4);
printList(mylistdir("/firstdir/seconddir"));
printf("2:----------------\n");
mychdir("/firstdir/seconddir");
printList(mylistdir("."));
printf("3:----------------\n");
writeFile("testfile2.txt", 1);
mymkdir("thirddir");
writeFile("thirddir/testfile3.txt", 1);
printList(mylistdir("thirddir"));
writeVirtualDisk("virtualdiskA5_A1_a");
printf("4:----------------\n");
myremove("testfile1.txt");
myremove("testfile2.txt");
printList(mylistdir("."));
writeVirtualDisk("virtualdiskA5_A1_b");
printf("5:----------------\n");
mychdir("thirddir");
myremove("testfile3.txt");
printList(mylistdir("."));
writeVirtualDisk("virtualdiskA5_A1_c");
printf("6:----------------\n");
mychdir("/firstdir/seconddir");
myrmdir("thirddir");
mychdir("/firstdir");
myrmdir("seconddir");
mychdir("/");
myrmdir("firstdir");
printList(mylistdir("/"));
writeVirtualDisk("virtualdiskA5_A1_d");
}
int main() {
format();
mymkdir("/firstdir/seconddir");
//init threads for A1
initThreads();
//Tests for A5-A4
tests();
//Tests for A3-A1
writeFile("/firstdir/seconddir/testfile1.txt", 4);
printList(mylistdir("/firstdir/seconddir"));
direntry_t* file = locate("/", "testfile1.txt");
printf("file: %s\n", file->name);
mymkdir("/firstdir/seconddir/testfile2.txt");
mycpy("/firstdir/seconddir/testfile1.txt", "/firstdir/seconddir/testfile2.txt");
myremove("/firstdir/seconddir/testfile2.txt");
mymove("/firstdir/seconddir/testfile1.txt", "/firstdir/seconddir/testfile1.txt");
return 0;
}