-
Notifications
You must be signed in to change notification settings - Fork 0
/
task3.cpp
599 lines (532 loc) · 22.2 KB
/
task3.cpp
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
#ifndef __PROGTEST__
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cstdint>
#include <cassert>
#include <functional>
using namespace std;
/*
Figure out the logic in writing and reading files and write it
Test
Do the rest
*/
/* Filesystem size: min 8MiB, max 1GiB
* Filename length: min 1B, max 28B
* Sector size: 512B
* Max open files: 8 at a time
* At most one filesystem mounted at a time.
* Max file size: < 1GiB
* Max files in the filesystem: 128
*/
#define FILENAME_LEN_MAX 28
#define DIR_ENTRIES_MAX 128
#define OPEN_FILES_MAX 8
#define SECTOR_SIZE 512
#define DEVICE_SIZE_MAX ( 1024 * 1024 * 1024 )
#define DEVICE_SIZE_MIN ( 8 * 1024 * 1024 )
struct TFile
{
char m_FileName[FILENAME_LEN_MAX + 1];
size_t m_FileSize;
};
struct TBlkDev
{
size_t m_Sectors;
function<size_t(size_t, void *, size_t )> m_Read;
function<size_t(size_t, const void *, size_t )> m_Write;
};
#endif /* __PROGTEST__ */
//-------------------------------------------------------------------------------------------------
size_t min ( size_t first,
size_t second )
{
if ( first < second )
return first;
return second;
}
//=================================================================================================
struct TFileEnt
{
char m_FileName[FILENAME_LEN_MAX + 1];
uint m_FirstSector;
uint m_FileSize;
};
/*
I'll use this as an object in an array. the index in that array is the FileDescriptor.
*/
struct TFileOpen
{
bool m_Mode;
uint32_t m_Position;
uint32_t m_IdxFiles;
};
struct THeader
{
uint32_t m_DataStartSector;
uint32_t m_FirstFreeSector;
uint32_t m_LastFreeSector;
uint8_t m_PlaceHolder[SECTOR_SIZE - ( sizeof(uint32_t) * 3)];
};
//=================================================================================================
class CFileSystem
{
public:
static bool CreateFs ( const TBlkDev & dev );
static CFileSystem * Mount ( const TBlkDev & dev );
bool Umount ( void );
size_t FileSize ( const char * fileName );
int OpenFile ( const char * fileName,
bool writeMode );
bool CloseFile ( int fd );
size_t ReadFile ( int fd,
void * data,
size_t len );
size_t WriteFile ( int fd,
const void * data,
size_t len );
bool DeleteFile ( const char * fileName );
bool FindFirst ( TFile & file );
bool FindNext ( TFile & file );
~CFileSystem ( void );
//-----------------------------------------------------------------------------------------------
private:
static const uint32_t FS_INVALID = UINT32_MAX;
CFileSystem ( uint32_t sectors,
const TBlkDev & dev );
CFileSystem ( const TBlkDev & dev );
bool serialize ( const void * data,
size_t size,
uint32_t sectorNum );
bool saveMetaData ( void );
bool loadMetaData ( void );
uint32_t findByName ( const char * fileName );
uint32_t findFreeSector ( void );
uint32_t makeFreeSectors ( uint32_t toFree );
uint32_t lastSector ( uint32_t firstSector );
void truncate ( uint32_t fileIdx );
uint32_t createNewFile ( const char * fileName );
uint32_t openFile ( bool writeMode,
uint32_t fileIdx );
size_t writeSector ( uint32_t sector,
uint32_t sectorOffset,
const void * add,
size_t addLen );
size_t readSector ( uint32_t sector,
uint32_t sectorOffset,
void * mem,
size_t len );
bool deserialize ( void * data,
size_t size,
uint32_t sectorNum );
//---------------------------------------------------------------------------------------------
TBlkDev m_BlkDev;
uint32_t m_DataStartSector;
uint32_t m_FirstFreeSector;
uint32_t m_LastFreeSector;
TFileEnt m_Files[DIR_ENTRIES_MAX];
uint32_t * m_FAT;
TFileOpen m_OpenFiles[OPEN_FILES_MAX];
uint32_t m_Find = 0; // for the whole find first, find next mechanism
};
//-------------------------------------------------------------------------------------------------
CFileSystem::CFileSystem ( uint32_t sectors,
const TBlkDev & dev )
: m_BlkDev ( dev ),
m_DataStartSector ( (( DIR_ENTRIES_MAX * sizeof ( TFileEnt ) ) + SECTOR_SIZE -1)/ SECTOR_SIZE
+ (( sectors * sizeof ( uint32_t ) + SECTOR_SIZE -1 )/ SECTOR_SIZE )
+ 1 ), // FRAGILE
m_FirstFreeSector ( m_DataStartSector ),
m_LastFreeSector ( sectors - 1 ),
m_FAT ( new uint32_t [sectors] )
{
memset ( m_Files, 0, sizeof ( m_Files ) );
for ( size_t i = 0; i < sectors; i++ )
{
if ( i < m_DataStartSector || i == sectors - 1 )
m_FAT[i] = FS_INVALID;
else
m_FAT[i] = i + 1;
}
for ( size_t i = 0; i < OPEN_FILES_MAX; i++ )
{
m_OpenFiles[i] . m_IdxFiles = FS_INVALID;
}
}
//-------------------------------------------------------------------------------------------------
bool CFileSystem::saveMetaData ( void )
{
THeader hdr;
hdr . m_DataStartSector = m_DataStartSector;
hdr . m_FirstFreeSector = m_FirstFreeSector;
hdr . m_LastFreeSector = m_LastFreeSector;
memset ( hdr . m_PlaceHolder, 0, sizeof ( hdr . m_PlaceHolder ) );
if ( serialize ( ( void * ) &hdr, sizeof ( THeader ), 0 ) != true )
return false;
if ( serialize ( ( void * ) m_Files, sizeof ( m_Files ), 1 )!= true )
return false;
if ( serialize ( ( void * ) m_FAT,
sizeof ( *m_FAT ) * m_BlkDev . m_Sectors,
( sizeof ( m_Files ) + SECTOR_SIZE - 1 )/ SECTOR_SIZE + 1 )
!= true )
return false;
return true;
}
//-------------------------------------------------------------------------------------------------
bool CFileSystem::serialize ( const void * data,
size_t size,
uint32_t sectorNum )
{
size_t wholeSectors = size / SECTOR_SIZE, leftOver = size % SECTOR_SIZE;
uint8_t mem[SECTOR_SIZE];
if ( wholeSectors != 0 && m_BlkDev . m_Write ( sectorNum, data, wholeSectors ) != wholeSectors )
return false;
if ( leftOver )
{
sectorNum += wholeSectors;
memset ( mem, 0, sizeof ( mem ) );
memcpy ( mem, (const uint8_t*)data + wholeSectors * SECTOR_SIZE, leftOver );
if ( m_BlkDev . m_Write ( sectorNum, mem , 1 ) != 1 )
return false;
}
return true;
}
//-------------------------------------------------------------------------------------------------
CFileSystem::CFileSystem ( const TBlkDev & dev )
: m_BlkDev ( dev )
{
for ( size_t i = 0; i < OPEN_FILES_MAX; i++ )
m_OpenFiles[i] . m_IdxFiles = FS_INVALID;
}
//-------------------------------------------------------------------------------------------------
bool CFileSystem::loadMetaData ( void )
{
THeader hdr;
m_BlkDev . m_Read ( 0, &hdr , 1 );
m_DataStartSector = hdr . m_DataStartSector;
m_FirstFreeSector = hdr . m_FirstFreeSector;
m_LastFreeSector = hdr . m_LastFreeSector;
if ( ! deserialize ( m_Files, sizeof (m_Files), 1 ) )
return false;
m_FAT = new uint32_t[ m_BlkDev . m_Sectors ];
if ( ! deserialize ( m_FAT, sizeof (*m_FAT) * m_BlkDev . m_Sectors, ( sizeof ( m_Files ) + SECTOR_SIZE - 1 ) / SECTOR_SIZE + 1 ) )
return false;
return true;
}
//-------------------------------------------------------------------------------------------------
bool CFileSystem::deserialize ( void * data,
size_t size,
uint32_t sectorNum )
{
size_t wholeSectors = size / SECTOR_SIZE, leftOver = size % SECTOR_SIZE;
uint8_t mem[SECTOR_SIZE];
if (wholeSectors != 0 && m_BlkDev . m_Read ( sectorNum, data, wholeSectors ) != wholeSectors )
return false;
if ( leftOver )
{
if ( m_BlkDev . m_Read ( sectorNum + wholeSectors, mem, 1 ) != 1 )
return false;
memcpy ( (uint8_t *) data + wholeSectors * SECTOR_SIZE, mem, leftOver );
}
return true;
}
//-------------------------------------------------------------------------------------------------
CFileSystem::~CFileSystem ( void )
{
delete [] m_FAT;
}
//-------------------------------------------------------------------------------------------------
bool CFileSystem::CreateFs ( const TBlkDev & dev )
{
CFileSystem hi ( dev . m_Sectors, dev );
return hi . saveMetaData ();
}
//-------------------------------------------------------------------------------------------------
CFileSystem * CFileSystem::Mount ( const TBlkDev & dev )
{
CFileSystem * fs = new CFileSystem ( dev );
fs -> loadMetaData ();
return fs;
}
//-------------------------------------------------------------------------------------------------
bool CFileSystem::Umount ( void )
{
return saveMetaData ();
}
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
size_t CFileSystem::FileSize ( const char * fileName )
{
uint32_t idx = findByName ( fileName );
if ( idx == FS_INVALID )
return SIZE_MAX;
return m_Files[idx] . m_FileSize;
}
//-------------------------------------------------------------------------------------------------
int CFileSystem::OpenFile ( const char * fileName,
bool writeMode )
{
size_t idx = findByName ( fileName );
uint32_t fd;
if ( writeMode == true )
{
if ( idx != FS_INVALID )
{
truncate ( idx );
}
else
{
idx = createNewFile ( fileName );
if ( idx == FS_INVALID )
return -1;
}
fd = openFile ( true, idx );
if ( fd == FS_INVALID )
return -1;
}
else // ReadMode
{
if ( idx != FS_INVALID )
{
fd = openFile ( false, idx ); // if opening file for read mode is somehow different deal with that!
if ( fd == FS_INVALID )
return -1;
}
else
return -1;
}
return fd;
}
//-------------------------------------------------------------------------------------------------
void CFileSystem::truncate ( uint32_t fileIdx )
{
// Gives the allocated sectors into free front, then it sets some meta data.
makeFreeSectors ( m_Files[fileIdx] . m_FirstSector );
m_Files[fileIdx] . m_FileSize = 0;
m_Files[fileIdx] . m_FirstSector = FS_INVALID;
}
//-------------------------------------------------------------------------------------------------
uint32_t CFileSystem::createNewFile ( const char * fileName )
{
for ( uint32_t i = 0; i < DIR_ENTRIES_MAX; ++i )
{
if ( m_Files[i] . m_FirstSector == 0 )
{
m_Files[i] . m_FirstSector = FS_INVALID;
m_Files[i] . m_FileSize = 0;
memset ( m_Files[i] . m_FileName, 0, FILENAME_LEN_MAX + 1 );
strncpy ( m_Files[i] . m_FileName, fileName, FILENAME_LEN_MAX );
return i;
}
}
return FS_INVALID;
}
//-------------------------------------------------------------------------------------------------
uint32_t CFileSystem::openFile ( bool writeMode,
uint32_t fileIdx )
{
for ( uint32_t i = 0; i < OPEN_FILES_MAX; ++i )
{
if ( m_OpenFiles[i] . m_IdxFiles == FS_INVALID )
{
m_OpenFiles[i] . m_IdxFiles = fileIdx;
m_OpenFiles[i] . m_Position = 0;
m_OpenFiles[i] . m_Mode = writeMode;
return i;
}
}
return FS_INVALID;
}
//-------------------------------------------------------------------------------------------------
bool CFileSystem::CloseFile ( int fd )
{
if ( fd < 0 || fd >= OPEN_FILES_MAX )
return false;
m_OpenFiles[fd] . m_IdxFiles = FS_INVALID;
m_OpenFiles[fd] . m_Position = 0;
return true;
}
//-------------------------------------------------------------------------------------------------
size_t CFileSystem::ReadFile ( int fd,
void * data,
size_t len )
{
if ( fd < 0 || fd >= OPEN_FILES_MAX )
return 0;
if ( len == 0 )
return 0;
TFileOpen & of = m_OpenFiles[fd];
TFileEnt & fe = m_Files[of . m_IdxFiles];
size_t sectorNr = of . m_Position / SECTOR_SIZE;
size_t sectorOffset = of . m_Position % SECTOR_SIZE;
uint32_t sector = fe . m_FirstSector;
for ( size_t i = 0; i < sectorNr && sector != FS_INVALID; sector = m_FAT[sector], i ++ ) { }
if ( sector == FS_INVALID ) return 0;
size_t res = 0;
while ( res != len && of . m_Position < fe . m_FileSize )
{
if ( sector == FS_INVALID )
break;
size_t rd = len - res;
if ( rd > SECTOR_SIZE - sectorOffset )
rd = SECTOR_SIZE - sectorOffset;
if ( of . m_Position + rd > fe . m_FileSize )
rd = fe . m_FileSize - of . m_Position;
readSector ( sector, sectorOffset, (uint8_t*)data + res, rd );
res += rd;
of . m_Position += rd;
sector = m_FAT[sector];
sectorOffset = 0;
}
return res;
}
//-------------------------------------------------------------------------------------------------
size_t CFileSystem::readSector ( uint32_t sector,
uint32_t sectorOffset,
void * mem,
size_t len )
{
uint8_t buffer[SECTOR_SIZE];
size_t read = min ( SECTOR_SIZE - sectorOffset, len );
m_BlkDev . m_Read ( sector, buffer, 1 );
memcpy ( mem, buffer + sectorOffset, read );
return read;
}
//-------------------------------------------------------------------------------------------------
size_t CFileSystem::WriteFile ( int fd,
const void * data,
size_t len )
{
if ( fd < 0 || fd >= OPEN_FILES_MAX )
return 0;
TFileOpen & of = m_OpenFiles[fd];
TFileEnt & fe = m_Files[of . m_IdxFiles];
uint32_t sectorToWrite = lastSector ( fe . m_FirstSector );
size_t res = 0;
while ( res != len )
{
if ( of . m_Position % SECTOR_SIZE == 0 )
{
sectorToWrite = findFreeSector();
if ( sectorToWrite == FS_INVALID )
break;
if ( of . m_Position == 0 )
fe . m_FirstSector = sectorToWrite;
else
m_FAT[lastSector(fe . m_FirstSector)] = sectorToWrite;
}
size_t wr = writeSector ( sectorToWrite,
of . m_Position % SECTOR_SIZE,
(uint8_t *) data + res,
len - res );
of . m_Position += wr;
res += wr;
}
fe . m_FileSize += res;
return res;
}
//-------------------------------------------------------------------------------------------------
size_t CFileSystem::writeSector ( uint32_t sector,
uint32_t sectorOffset,
const void * add,
size_t addLen )
{
uint8_t buffer[SECTOR_SIZE];
size_t written = min ( SECTOR_SIZE - sectorOffset, addLen );
m_BlkDev . m_Read ( sector, buffer, 1 );
memcpy ( buffer + sectorOffset, add, written );
m_BlkDev . m_Write ( sector, buffer, 1 );
return written;
}
//-------------------------------------------------------------------------------------------------
bool CFileSystem::DeleteFile ( const char * fileName )
{
// Theoretically I should deal with what happens when the deleted file is open, but I am not sure
// what should happen, so I leave it be.
// mm says: I think it should be possible to delete an open file and then any access to such
// a file (read/write) should report failure
uint32_t idx = findByName ( fileName );
if ( idx == FS_INVALID )
return false;
makeFreeSectors ( m_Files [idx] . m_FirstSector );
m_Files [idx] . m_FirstSector = 0;
m_Files [idx] . m_FileSize = 0;
return true;
}
//-------------------------------------------------------------------------------------------------
bool CFileSystem::FindFirst ( TFile & file )
{
for ( m_Find = 0; m_Find < DIR_ENTRIES_MAX; ++m_Find )
{
if ( m_Files[m_Find] . m_FirstSector != 0 )
{
strcpy (file . m_FileName, m_Files[m_Find] . m_FileName );
file . m_FileSize = m_Files[m_Find] . m_FileSize;
++m_Find;
return true;
}
}
return false;
}
//-------------------------------------------------------------------------------------------------
bool CFileSystem::FindNext ( TFile & file )
{
for ( ; m_Find < DIR_ENTRIES_MAX; ++m_Find )
{
if ( m_Files[m_Find]. m_FirstSector != 0 )
{
strcpy (file . m_FileName, m_Files[m_Find] . m_FileName ) ;
file . m_FileSize = m_Files[m_Find] . m_FileSize;
++m_Find;
return true;
}
}
return false;
}
//-------------------------------------------------------------------------------------------------
uint32_t CFileSystem::findByName ( const char * fileName )
{
for ( uint32_t i = 0; i < DIR_ENTRIES_MAX; i++ )
if ( strncmp ( fileName, m_Files[i] . m_FileName, FILENAME_LEN_MAX ) == 0 )
return i;
return FS_INVALID;
}
//-------------------------------------------------------------------------------------------------
uint32_t CFileSystem::findFreeSector ( void )
{
if ( m_FirstFreeSector == FS_INVALID )
return FS_INVALID;
uint32_t free = m_FirstFreeSector;
m_FirstFreeSector = m_FAT[free];
m_FAT[free] = FS_INVALID;
return free;
}
//-------------------------------------------------------------------------------------------------
uint32_t CFileSystem::makeFreeSectors ( uint32_t toFree )
{
if ( toFree == FS_INVALID )
return FS_INVALID;
if ( m_FirstFreeSector == FS_INVALID )
{
m_FirstFreeSector = toFree;
m_LastFreeSector = lastSector ( toFree );
return m_FirstFreeSector;
}
uint32_t last = lastSector ( toFree );
m_FAT[last] = m_FirstFreeSector;
m_FirstFreeSector = toFree;
return m_FirstFreeSector;
}
//-------------------------------------------------------------------------------------------------
uint32_t CFileSystem::lastSector ( uint32_t firstSector )
{
if ( firstSector == FS_INVALID )
return FS_INVALID;
while ( m_FAT[firstSector] != FS_INVALID )
firstSector = m_FAT[firstSector];
return firstSector;
}
//=================================================================================================
#ifndef __PROGTEST__
#include "simple_test.inc"
#endif /* __PROGTEST__ */