-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathappl.c
241 lines (191 loc) · 5.96 KB
/
appl.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
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
/****************/
/* Your Name */
/* Date */
/* CS 244B */
/* Spring 2014 */
/****************/
#define DEBUG
#include <stdio.h>
#include <string.h>
#include <client.h>
#include <appl.h>
#include <stdlib.h>
static int
openFile(char *file)
{
int fd = OpenFile(file);
if (fd < 0) printf("OpenFile(%s): failed (%d)\n", file, fd);
return fd;
}
static int
commit(int fd)
{
int result = Commit(fd);
if (result < 0) printf("Commit(%d): failed (%d)\n", fd, result);
return fd;
}
static int
closeFile(int fd)
{
int result = CloseFile(fd);
if (result < 0) printf("CloseFile(%d): failed (%d)\n", fd, result);
return fd;
}
static void appl8() {
// multiple openFiles of the same file. As a consequence,
// this also checks that
// when a file exists in the mount directory, they should openFile it
// and not create a new one.
int fd;
int retVal;
int i;
char commitStrBuf[512];
for( i = 0; i < 512; i++ )
commitStrBuf[i] = '1';
fd = openFile( "file8" );
// write first transaction starting at offset 512
for (i = 0; i < 50; i++)
retVal = WriteBlock( fd, commitStrBuf, 512 + i * 512 , 512 );
retVal = commit( fd );
retVal = closeFile( fd );
for( i = 0; i < 512; i++ )
commitStrBuf[i] = '2';
fd = openFile( "file8" );
// write second transaction starting at offset 0
retVal = WriteBlock( fd, commitStrBuf, 0 , 512 );
retVal = commit( fd );
retVal = closeFile( fd );
for( i = 0; i < 512; i++ )
commitStrBuf[i] = '3';
fd = openFile( "file8" );
// write third transaction starting at offset 50*512
for (i = 0; i < 100; i++)
retVal = WriteBlock( fd, commitStrBuf, 50 * 512 + i * 512 , 512 );
retVal = commit( fd );
retVal = closeFile( fd );
}
/* ------------------------------------------------------------------ */
int
main(int argc, char *argv[]) {
int fd;
int loopCnt;
int byteOffset= 0;
char strData[MaxBlockLength];
char fileName[32] = "writeTest.txt";
char dummyfile[32] = "dummyOpen.txt";
/*****************************/
/* Initialize the system */
/*****************************/
int numServer = 0;
if(argc >= 2)
numServer = atoi(argv[1]);
int packetLoss = 0;
if(argc >= 3)
packetLoss = atoi(argv[2]);
if( InitReplFs( ReplFsPort, packetLoss, numServer ) < 0 ) {
fprintf( stderr, "Error initializing the system\n" );
return( ErrorExit );
}
appl8();
/*****************************/
/* Open the file for writing */
/*****************************/
fd = OpenFile( fileName );
if ( fd < 0 ) {
fprintf( stderr, "Error opening file '%s'\n", fileName );
return( ErrorExit );
}
int fd_dummy = OpenFile(dummyfile);
if ( fd_dummy >= 0 ) {
fprintf( stderr, "Should not open this file '%s'\n", dummyfile );
return ( ErrorExit );
}
/**************************************/
/* Write incrementing numbers to the file */
/**************************************/
srand(time(NULL));
for ( loopCnt=0; loopCnt<140; loopCnt++ ) {
sprintf( strData, "%d\n", loopCnt );
#ifdef DEBUG
// printf( "%d: Writing '%s' to file.\n", loopCnt, strData );
#endif
if ( WriteBlock( fd, strData, byteOffset, strlen( strData ) ) < 0 ) {
printf( "Error writing to file %s [LoopCnt=%d]\n", fileName, loopCnt );
// return( ErrorExit );
}
byteOffset += strlen( strData );
// byteOffset = rand() % (67 * strlen(strData));
}
Commit(fd);
/* if ( Abort( fd ) < 0) {
printf("Could not abort changes '%s'\n", fileName);
}*/
int randOffset = 0;
for ( loopCnt=0; loopCnt<128; loopCnt++ ) {
sprintf( strData, "%d\n", loopCnt + 128 );
if ( WriteBlock( fd, strData, randOffset, strlen( strData ) ) < 0 ) {
printf( "Error writing to file %s [LoopCnt=%d]\n", fileName, loopCnt );
return( ErrorExit );
}
randOffset = rand() % (67 * strlen(strData));
// byteOffset += strlen( strData );
}
if ( Abort( fd ) < 0) {
printf("Could not abort changes '%s'\n", fileName);
}
/**********************************************/
/* Can we commit the writes to the server(s)? */
/**********************************************/
if ( Commit( fd ) < 0 ) {
printf( "Could not commit changes to File '%s'\n", fileName );
return( ErrorExit );
}
byteOffset += 100;
/** write with out explicit commit **/
for ( loopCnt=0; loopCnt<128; loopCnt++ ) {
sprintf( strData, "%d\n", loopCnt + 256 );
if ( WriteBlock( fd, strData, byteOffset, strlen( strData ) ) < 0 ) {
printf( "Error writing to file %s [LoopCnt=%d]\n", fileName, loopCnt );
return( ErrorExit );
}
byteOffset += strlen( strData );
}
/* if ( Abort( fd ) < 0) {
printf("Could not abort changes '%s'\n", fileName);
}*/
/**************************************/
/* Close the writes to the server(s) */
/**************************************/
if ( CloseFile( fd ) < 0 ) {
printf( "Error Closing File '%s'\n", fileName );
return( ErrorExit );
}
printf( "Writes to file '%s' complete.\n", fileName );
if ( WriteBlock( fd, strData, byteOffset, strlen( strData ) ) >= 0 ) {
printf("Error, cannot write while this file is closed '%s'\n", fileName);
return ( ErrorExit );
}
/*********************/
/* Test open file again */
/*********************/
/* fd = OpenFile( fileName );
if ( fd < 0 ) {
fprintf( stderr, "Error opening file '%s'\n", fileName );
return( ErrorExit );
}
for ( loopCnt=0; loopCnt<128; loopCnt++ ) {
sprintf( strData, "%d\n", loopCnt );
if ( WriteBlock( fd, strData, byteOffset, strlen( strData ) ) < 0 ) {
printf( "Error writing to file %s [LoopCnt=%d]\n", fileName, loopCnt );
return( ErrorExit );
}
byteOffset = (rand() % 67) * strlen( strData );
}
if ( CloseFile( fd ) < 0 ) {
printf( "Error Closing File '%s'\n", fileName );
return( ErrorExit );
}
*/
return( NormalExit );
}
/* ------------------------------------------------------------------ */