-
Notifications
You must be signed in to change notification settings - Fork 0
/
tinyfsTest.c
161 lines (137 loc) · 4.29 KB
/
tinyfsTest.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
/* TinyFS demo file
* * Foaad Khosmood, Cal Poly / modified Winter 2014
* */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "libTinyFS.h"
#include "tinyFS.h"
#include "TinyFS_errno.h"
/* simple helper function to fill Buffer with as many inPhrase strings as possible before reaching size */
int
fillBufferWithPhrase (char *inPhrase, char *Buffer, int size)
{
int index = 0, i;
if (!inPhrase || !Buffer || size <= 0 || size < strlen (inPhrase))
return -1;
while (index < size)
{
for (i = 0; inPhrase[i] != '\0' && (i + index < size); i++)
Buffer[i + index] = inPhrase[i];
index += i;
}
Buffer[size - 1] = '\0'; /* explicit null termination */
return 0;
}
/* This program will create 2 files (of sizes 200 and 1000) to be read from or stored in the TinyFS file system. */
int
main ()
{
char readBuffer;
char *afileContent, *bfileContent; /* buffers to store file content */
int afileSize = 200; /* sizes in bytes */
int bfileSize = 1000;
char phrase1[] = "hello world from (a) file ";
char phrase2[] = "(b) file content ";
fileDescriptor aFD, bFD;
int i, returnValue;
/* try to mount the disk */
if (tfs_mount(DEFAULT_DISK_NAME) < 0) /* if mount fails */
{
tfs_mkfs(DEFAULT_DISK_NAME, DEFAULT_DISK_SIZE); /* then make a new disk */
if (tfs_mount(DEFAULT_DISK_NAME) < 0) /* if we still can't open it... */
{
perror ("failed to open disk"); /* then just exit */
return;
}
}else
printf("mounted\n");
afileContent = (char *) malloc (afileSize * sizeof (char));
if (fillBufferWithPhrase (phrase1, afileContent, afileSize) < 0)
{
perror ("failed");
return;
}
bfileContent = (char *) malloc (bfileSize * sizeof (char));
if (fillBufferWithPhrase (phrase2, bfileContent, bfileSize) < 0)
{
perror ("failed");
return;
}
/* print content of files for debugging */
printf
("(a) File content: %s\n(b) File content: %s\nReady to store in TinyFS\n",
afileContent, bfileContent);
/* read or write files to TinyFS */
printf("start\n");
aFD = tfs_openFile ("afile");
if (aFD < 0)
{
perror ("tfs_openFile failed on afile");
}
/* now, was there already a file named "afile" that had some content? If we can read from it, yes!
* * If we can't read from it, it presumably means the file was empty.
* * If the size is 0 (all new files are sized 0) then any "readByte()" should fail, so
* * it's a new file and empty */
if (tfs_readByte (aFD, &readBuffer) < 0)
{
/* if readByte() fails, there was no afile, so we write to it */
if (tfs_writeFile (aFD, afileContent, afileSize) < 0)
{
perror ("tfs_writeFile failed");
}
else
printf ("Successfully written to afile\n");
}
else
{ int temp;
/* if yes, then just read and print the rest of afile that was already there */
printf ("\n*** reading afile from TinyFS: \n%c", readBuffer); /* print the first byte already read */
/* now print the rest of it, byte by byte */
while (temp = tfs_readByte (aFD, &readBuffer) >= 0){ /* go until readByte fails */
printf ("%c", readBuffer);
}
//printf("failed on: %d\n",temp);
/* close file */
if (tfs_closeFile (aFD) < 0)
perror ("tfs_closeFile failed");
/* now try to delete the file. It should fail because aFD is no longer valid */
if (tfs_deleteFile (aFD) < 0)
{
aFD = tfs_openFile ("afile"); /* so we open it again */
if (tfs_deleteFile (aFD) < 0)
perror ("tfs_deleteFile failed");
}
else
perror ("tfs_closeFile should have failed");
}
/* now bfile tests */
bFD = tfs_openFile ("bfile");
if (bFD < 0)
{
perror ("tfs_openFile failed on bfile");
}else
if (tfs_readByte (bFD, &readBuffer) < 0)
{
if (tfs_writeFile (bFD, bfileContent, bfileSize) < 0)
{
perror ("tfs_writeFile failed");
}
else
printf ("Successfully written to bfile\n");
}
else
{
printf ("\n*** reading bfile from TinyFS: \n%c", readBuffer);
while (tfs_readByte (bFD, &readBuffer) >= 0)
printf ("%c", readBuffer);
tfs_deleteFile (bFD);
}
/* Free both content buffers */
free (bfileContent);
free (afileContent);
if (tfs_unmount () < 0)
perror ("tfs_unmount failed");
printf ("\nend of demo\n\n");
return 0;
}