OpenCores
URL https://opencores.org/ocsvn/plasma/plasma/trunk

Subversion Repositories plasma

[/] [plasma/] [trunk/] [kernel/] [filesys.c] - Blame information for rev 415

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 398 rhoads
/*--------------------------------------------------------------------
2
 * TITLE: Plasma File System
3
 * AUTHOR: Steve Rhoads (rhoadss@yahoo.com)
4
 * DATE CREATED: 4/26/07
5
 * FILENAME: filesys.c
6
 * PROJECT: Plasma CPU core
7
 * COPYRIGHT: Software placed into the public domain by the author.
8
 *    Software 'as is' without warranty.  Author liable for nothing.
9
 * DESCRIPTION:
10
 *    Plasma File System.  Supports RAM, flash, and disk file systems.
11
 *    Possible call tree:
12
 *      OS_fclose()
13
 *        FileFindRecursive()      //find the existing file
14
 *          FileOpen()             //open root file system
15
 *          FileFind()             //find the next level of directory
16
 *            OS_fread()           //read the directory file
17
 *              BlockRead()        //read blocks of directory
18
 *                MediaBlockRead() //low level read
19
 *          FileOpen()             //open next directory
20
 *        OS_fwrite()              //write file entry into directory
21
 *        BlockRead()              //flush changes to directory
22
 *--------------------------------------------------------------------*/
23
#include "rtos.h"
24
 
25 415 rhoads
#define FLASH_SIZE        (1024*1024*16)
26
#define FLASH_SECTOR_SIZE (1024*128)
27 398 rhoads
#define FLASH_BLOCK_SIZE  512
28
#define FLASH_LN2_SIZE    9                  //2^FLASH_LN2_SIZE == FLASH_BLOCK_SIZE
29
#define FLASH_OFFSET      FLASH_SECTOR_SIZE  //offset to start of flash file system
30 415 rhoads
#define FLASH_BLOCKS      FLASH_SIZE/FLASH_BLOCK_SIZE
31
#define FLASH_START       (FLASH_OFFSET+FLASH_BLOCKS/8*2)/FLASH_BLOCK_SIZE
32 398 rhoads
 
33
#define BLOCK_SIZE        512
34
#define FILE_NAME_SIZE    40
35
#define FULL_NAME_SIZE    128
36
#define BLOCK_MALLOC      0x0
37
#define BLOCK_EOF         0xffffffff
38
 
39
typedef enum {
40
   FILE_MEDIA_RAM,
41
   FILE_MEDIA_FLASH,
42
   FILE_MEDIA_DISK
43
} OS_MediaType_e;
44
 
45
typedef struct OS_FileEntry_s {
46
   char name[FILE_NAME_SIZE];
47
   uint32 blockIndex;       //first block of file
48
   uint32 modifiedTime;
49
   uint32 length;
50
   uint8 isDirectory;
51
   uint8 attributes;
52
   uint8 valid;
53
   uint8 mediaType;
54
   uint16 blockSize;        //Normally BLOCK_SIZE
55
   uint8 pad1, pad2;
56
} OS_FileEntry_t;
57
 
58
typedef struct OS_Block_s {
59
   uint32 next;
60
   uint8 data[BLOCK_SIZE - sizeof(uint32)];
61
} OS_Block_t;
62
 
63
struct OS_FILE_s {
64
   OS_FileEntry_t fileEntry;  //written to directory upon OS_fclose()
65
   uint8 fileModified;
66
   uint8 blockModified;
67
   uint8 pad1, pad2;
68
   uint32 blockIndex;         //index of block
69
   uint32 blockOffset;        //byte offset into block
70
   uint32 fileOffset;         //byte offset into file
71
   char fullname[FULL_NAME_SIZE]; //includes full path
72
   OS_Block_t *block;
73
   OS_Block_t blockLocal;     //local copy for flash or disk file system
74
};
75
 
76
static OS_FileEntry_t rootFileEntry;
77
static OS_Mutex_t *mutexFilesys;
78
 
79
// Public prototypes
80
#ifndef _FILESYS_
81
typedef struct OS_FILE_s OS_FILE;
82
#endif
83
OS_FILE *OS_fopen(char *name, char *mode);
84
void OS_fclose(OS_FILE *file);
85
int OS_fread(void *buffer, int size, int count, OS_FILE *file);
86
int OS_fwrite(void *buffer, int size, int count, OS_FILE *file);
87
int OS_fseek(OS_FILE *file, int offset, int mode);
88
int OS_fmkdir(char *name);
89
int OS_fdir(OS_FILE *dir, char name[64]);
90
void OS_fdelete(char *name);
91
 
92
 
93
/***************** Media Functions Start ***********************/
94 415 rhoads
#ifndef EXCLUDE_FLASH
95 398 rhoads
static unsigned char FlashBlockEmpty[FLASH_BLOCKS/8];
96
static unsigned char FlashBlockUsed[FLASH_BLOCKS/8];
97
static int FlashBlock;
98
 
99
//Free unused flash blocks
100
static int MediaBlockCleanup(void)
101
{
102
   int i, sector, block, count=0;
103
   unsigned char *buf;
104
 
105
   printf("FlashCleanup\n");
106
   buf = (unsigned char*)malloc(FLASH_SECTOR_SIZE);
107
   if(buf == NULL)
108
      return 0;
109 415 rhoads
   FlashLock();
110 398 rhoads
   for(sector = FLASH_OFFSET / FLASH_SECTOR_SIZE; sector < FLASH_SIZE / FLASH_SECTOR_SIZE; ++sector)
111
   {
112 415 rhoads
      printf(".");
113 398 rhoads
      FlashRead((uint16*)buf, FLASH_SECTOR_SIZE*sector, FLASH_SECTOR_SIZE);
114
      if(sector == FLASH_OFFSET / FLASH_SECTOR_SIZE)
115
      {
116 415 rhoads
         for(i = FLASH_START/8; i < FLASH_BLOCKS/8; ++i)
117 398 rhoads
            FlashBlockEmpty[i] |= ~FlashBlockUsed[i];
118 415 rhoads
         memset(FlashBlockEmpty, 0, FLASH_START/8);
119 398 rhoads
         memcpy(buf, FlashBlockEmpty, sizeof(FlashBlockEmpty));
120
         memset(FlashBlockUsed, 0xff, sizeof(FlashBlockUsed));
121
         memset(buf+sizeof(FlashBlockEmpty), 0xff, sizeof(FlashBlockUsed));
122
      }
123
      //Erase empty blocks
124
      for(block = 0; block < FLASH_SECTOR_SIZE / FLASH_BLOCK_SIZE; ++block)
125
      {
126
         i = sector * FLASH_SECTOR_SIZE / FLASH_BLOCK_SIZE + block;
127 415 rhoads
         if(FlashBlockEmpty[i >> 3] & (1 << (i & 7)))
128 398 rhoads
         {
129
            memset(buf + FLASH_BLOCK_SIZE*block, 0xff, FLASH_BLOCK_SIZE);
130
            ++count;
131
         }
132
      }
133
      FlashErase(FLASH_SECTOR_SIZE * sector);
134
      FlashWrite((uint16*)buf, FLASH_SECTOR_SIZE * sector, FLASH_SECTOR_SIZE);
135
   }
136 415 rhoads
   FlashBlock = FLASH_START;
137
   FlashUnlock();
138 398 rhoads
   free(buf);
139
   return count;
140
}
141
 
142
 
143
int MediaBlockInit(void)
144
{
145
   FlashRead((uint16*)FlashBlockEmpty, FLASH_OFFSET, sizeof(FlashBlockEmpty));
146
   FlashRead((uint16*)FlashBlockUsed, FLASH_OFFSET+sizeof(FlashBlockEmpty),
147
             sizeof(FlashBlockUsed));
148
   FlashBlock = FLASH_START;
149 415 rhoads
   memset(FlashBlockEmpty, 0, FLASH_START/8);  //space for FlashBlockEmpty/Used
150 398 rhoads
   return FlashBlockEmpty[FlashBlock >> 3] & (1 << (FlashBlock & 7));
151
}
152 415 rhoads
#endif  //!EXCLUDE_FLASH
153 398 rhoads
 
154
 
155
static uint32 MediaBlockMalloc(OS_FILE *file)
156
{
157
   int i, j;
158
   (void)i; (void)j;
159
 
160
   if(file->fileEntry.mediaType == FILE_MEDIA_RAM)
161
      return (uint32)malloc(file->fileEntry.blockSize);
162 415 rhoads
#ifndef EXCLUDE_FLASH
163 398 rhoads
   //Find empty flash block
164
   for(i = FlashBlock; i < FLASH_BLOCKS; ++i)
165
   {
166
      if(FlashBlockEmpty[i >> 3] & (1 << (i & 7)))
167
      {
168
         FlashBlock = i + 1;
169
         FlashBlockEmpty[i >> 3] &= ~(1 << (i & 7));
170
         j = i >> 3;
171
         j &= ~1;
172
         FlashWrite((uint16*)(FlashBlockEmpty + j), FLASH_OFFSET + j, 2);
173
         return i;
174
      }
175
   }
176
 
177
   i = MediaBlockCleanup();
178
   if(i == 0)
179
      return 0;
180
   FlashBlock = FLASH_START;
181
   return MediaBlockMalloc(file);
182
#else
183
   return 0;
184
#endif
185
}
186
 
187
 
188
static void MediaBlockFree(OS_FILE *file, uint32 blockIndex)
189
{
190
   if(file->fileEntry.mediaType == FILE_MEDIA_RAM)
191
      free((void*)blockIndex);
192 415 rhoads
#ifndef EXCLUDE_FLASH
193 398 rhoads
   else
194
   {
195
      int i=blockIndex, j;
196
      FlashBlockUsed[i >> 3] &= ~(1 << (i & 7));
197
      j = i >> 3;
198
      j &= ~1;
199
      FlashWrite((uint16*)(FlashBlockUsed + j), FLASH_OFFSET + sizeof(FlashBlockEmpty) + j, 2);
200
   }
201
#endif
202
}
203
 
204
 
205
static void MediaBlockRead(OS_FILE *file, uint32 blockIndex)
206
{
207
   if(file->fileEntry.mediaType == FILE_MEDIA_RAM)
208
      file->block = (OS_Block_t*)blockIndex;
209 415 rhoads
#ifndef EXCLUDE_FLASH
210 398 rhoads
   else
211
   {
212
      file->block = &file->blockLocal;
213
      FlashRead((uint16*)file->block, blockIndex << FLASH_LN2_SIZE, FLASH_BLOCK_SIZE);
214
   }
215
#endif
216
}
217
 
218
 
219
static void MediaBlockWrite(OS_FILE *file, uint32 blockIndex)
220
{
221
   (void)file;
222
   (void)blockIndex;
223 415 rhoads
#ifndef EXCLUDE_FLASH
224 398 rhoads
   if(file->fileEntry.mediaType != FILE_MEDIA_RAM)
225
      FlashWrite((uint16*)file->block, blockIndex << FLASH_LN2_SIZE, FLASH_BLOCK_SIZE);
226
#endif
227
}
228
 
229
/***************** Media Functions End *************************/
230
 
231
// Get the next block and write the old block if it was modified
232
static void BlockRead(OS_FILE *file, uint32 blockIndex)
233
{
234
   uint32 blockIndexSave = blockIndex;
235
 
236
   OS_MutexPend(mutexFilesys);
237
   if(blockIndex == BLOCK_MALLOC)
238
   {
239
      // Get a new block
240
      blockIndex = MediaBlockMalloc(file);
241
      if(blockIndex == 0)
242
         blockIndex = BLOCK_EOF;
243
      if(file->block)
244
      {
245
         // Set next pointer in previous block
246
         file->block->next = blockIndex;
247
         file->blockModified = 1;
248
      }
249
   }
250
   if(file->block && file->blockModified)
251
   {
252
      // Write block back to flash or disk
253
      MediaBlockWrite(file, file->blockIndex);
254
      file->blockModified = 0;
255
   }
256
   if(blockIndex == BLOCK_EOF)
257
   {
258
      OS_MutexPost(mutexFilesys);
259
      return;
260
   }
261
   file->blockIndex = blockIndex;
262
   file->blockOffset = 0;
263
   MediaBlockRead(file, blockIndex);
264
   if(blockIndexSave == BLOCK_MALLOC)
265
   {
266
      memset(file->block, 0xff, file->fileEntry.blockSize);
267
      file->blockModified = 1;
268
   }
269
   OS_MutexPost(mutexFilesys);
270
}
271
 
272
 
273
int OS_fread(void *buffer, int size, int count, OS_FILE *file)
274
{
275
   int items, bytes;
276
   uint8 *buf = (uint8*)buffer;
277
 
278
   for(items = 0; items < count; ++items)
279
   {
280
      for(bytes = 0; bytes < size; ++bytes)
281
      {
282
         if(file->fileOffset >= file->fileEntry.length &&
283
            file->fileEntry.isDirectory == 0)
284
            return items;
285
         if(file->blockOffset >= file->fileEntry.blockSize - sizeof(uint32))
286
         {
287
            if(file->block->next == BLOCK_EOF)
288
               return items;
289
            BlockRead(file, file->block->next);
290
         }
291
         *buf++ = file->block->data[file->blockOffset++];
292
         ++file->fileOffset;
293
      }
294
   }
295
   return items;
296
}
297
 
298
 
299
int OS_fwrite(void *buffer, int size, int count, OS_FILE *file)
300
{
301
   int items, bytes;
302
   uint8 *buf = (uint8*)buffer;
303
 
304
   OS_MutexPend(mutexFilesys);
305
   file->blockModified = 1;
306
   for(items = 0; items < count; ++items)
307
   {
308
      for(bytes = 0; bytes < size; ++bytes)
309
      {
310
         if(file->blockOffset >= file->fileEntry.blockSize - sizeof(uint32))
311
         {
312
            if(file->block->next == BLOCK_EOF)
313
               file->block->next = BLOCK_MALLOC;
314
            BlockRead(file, file->block->next);
315
            if(file->blockIndex == BLOCK_EOF)
316
            {
317
               count = 0;
318
               --items;
319
               break;
320
            }
321
            file->blockModified = 1;
322
         }
323
         file->block->data[file->blockOffset++] = *buf++;
324
         ++file->fileOffset;
325
      }
326
   }
327
   file->blockModified = 1;
328
   file->fileModified = 1;
329
   if(file->fileOffset > file->fileEntry.length)
330
      file->fileEntry.length = file->fileOffset;
331
   OS_MutexPost(mutexFilesys);
332
   return items;
333
}
334
 
335
 
336
int OS_fseek(OS_FILE *file, int offset, int mode)
337
{
338
   if(mode == 1)      //SEEK_CUR
339
      offset += file->fileOffset;
340
   else if(mode == 2) //SEEK_END
341
      offset += file->fileEntry.length;
342
   file->fileOffset = offset;
343
   BlockRead(file, file->fileEntry.blockIndex);
344
   while(offset > (int)file->fileEntry.blockSize - (int)sizeof(uint32))
345
   {
346
      BlockRead(file, file->block->next);
347
      offset -= file->fileEntry.blockSize - (int)sizeof(uint32);
348
   }
349
   file->blockOffset = offset;
350
   return 0;
351
}
352
 
353
 
354
static int FileOpen(OS_FILE *file, char *name, OS_FileEntry_t *fileEntry)
355
{
356
   memset(file, 0, sizeof(OS_FILE));
357
   if(fileEntry == NULL)
358
   {
359
      // Open root file
360
      memcpy(&file->fileEntry, &rootFileEntry, sizeof(OS_FileEntry_t));
361
   }
362
   else if(fileEntry->valid == 1)
363
   {
364
      // Open existing file
365
      memcpy(&file->fileEntry, fileEntry, sizeof(OS_FileEntry_t));
366
   }
367
   else
368
   {
369
      // Initialize new file
370
      file->fileModified = 1;
371
      file->blockModified = 1;
372
      memset(&file->fileEntry, 0, sizeof(OS_FileEntry_t));
373
      file->fileEntry.isDirectory = 0;
374
      file->fileEntry.length = 0;
375
      strncpy(file->fileEntry.name, name, FILE_NAME_SIZE-1);
376
      file->fileEntry.blockIndex = 0;
377
      file->fileEntry.valid = 1;
378
      file->fileEntry.blockSize = fileEntry->blockSize;
379
      file->fileEntry.mediaType = fileEntry->mediaType;
380
   }
381
   BlockRead(file, file->fileEntry.blockIndex);    //Get first block
382
   file->fileEntry.blockIndex = file->blockIndex;
383
   file->fileOffset = 0;
384
   if(file->blockIndex == BLOCK_EOF)
385
      return -1;
386
   return 0;
387
}
388
 
389
 
390
static int FileFind(OS_FILE *directory, char *name, OS_FileEntry_t *fileEntry)
391
{
392
   int count, rc = -1;
393
   uint32 blockIndex, blockOffset;
394
   uint32 blockIndexEmpty=BLOCK_EOF, blockOffsetEmpty=0;
395
 
396
   // Loop through files in directory
397
   for(;;)
398
   {
399
      blockIndex = directory->blockIndex;
400
      blockOffset = directory->blockOffset;
401
      count = OS_fread(fileEntry, sizeof(OS_FileEntry_t), 1, directory);
402
      if(count == 0 || fileEntry->blockIndex == BLOCK_EOF)
403
         break;
404
      if(fileEntry->valid == 1 && strcmp(fileEntry->name, name) == 0)
405
      {
406
         rc = 0;  //Found the file in the directory
407
         break;
408
      }
409
      if(fileEntry->valid != 1 && blockIndexEmpty == BLOCK_EOF)
410
      {
411
         blockIndexEmpty = blockIndex;
412
         blockOffsetEmpty = blockOffset;
413
      }
414
   }
415
   if(rc == 0 || directory->fileEntry.mediaType == FILE_MEDIA_FLASH ||
416
      blockIndexEmpty == BLOCK_EOF)
417
   {
418
      // Backup to start of fileEntry or last entry in directory
419
      if(directory->blockIndex != blockIndex)
420
         BlockRead(directory, blockIndex);
421
      directory->blockOffset = blockOffset;
422
   }
423
   else
424
   {
425
      // Backup to empty slot
426
      if(directory->blockIndex != blockIndexEmpty)
427
         BlockRead(directory, blockIndexEmpty);
428
      directory->blockOffset = blockOffsetEmpty;
429
   }
430
   return rc;
431
}
432
 
433
 
434
static int FileFindRecursive(OS_FILE *directory, char *name,
435
                             OS_FileEntry_t *fileEntry, char *filename)
436
{
437
   int rc, length;
438
 
439
   rc = FileOpen(directory, NULL, NULL);            //Open root directory
440
   for(;;)
441
   {
442
      if(name[0] == '/')
443
         ++name;
444
      for(length = 0; length < FILE_NAME_SIZE; ++length)
445
      {
446
         if(name[length] == 0 || name[length] == '/')
447
            break;
448
         filename[length] = name[length];
449
      }
450
      filename[length] = 0;
451
      rc = FileFind(directory, filename, fileEntry);  //Find file
452
      if(rc)
453
      {
454
         // File not found
455
         fileEntry->mediaType = directory->fileEntry.mediaType;
456
         fileEntry->blockSize = directory->fileEntry.blockSize;
457
         fileEntry->valid = 0;
458
         if(strstr(name, "/") == NULL)
459
            return rc;
460
         else
461
            return -2;  //can't find parent directory
462
      }
463
      name += length;
464
      if(name[0])
465
         rc = FileOpen(directory, filename, fileEntry);  //Open subdir
466
      else
467
         break;
468
   }
469
   return rc;
470
}
471
 
472
 
473
OS_FILE *OS_fopen(char *name, char *mode)
474
{
475
   OS_FILE *file;
476
   OS_FileEntry_t fileEntry;
477
   OS_FILE dir;
478
   char filename[FILE_NAME_SIZE];  //Name without directories
479
   int rc;
480
 
481
   //printf("OS_fopen(%s)\n", name);
482
   if(rootFileEntry.blockIndex == 0)
483
   {
484
      // Mount file system
485
      mutexFilesys = OS_MutexCreate("filesys");
486
      memset(&dir, 0, sizeof(OS_FILE));
487
      dir.fileEntry.blockSize = BLOCK_SIZE;
488
      //dir.fileEntry.mediaType = FILE_MEDIA_FLASH;  //Test flash
489
      BlockRead(&dir, BLOCK_MALLOC);
490
      strcpy(rootFileEntry.name, "/");
491
      rootFileEntry.mediaType = dir.fileEntry.mediaType;
492
      rootFileEntry.blockIndex = dir.blockIndex;
493
      rootFileEntry.blockSize = dir.fileEntry.blockSize;
494
      rootFileEntry.isDirectory = 1;
495
      BlockRead(&dir, BLOCK_EOF);    //Flush data
496 415 rhoads
#ifndef EXCLUDE_FLASH
497 398 rhoads
      file = OS_fopen("flash", "w+");
498
      if(file == NULL)
499
         return NULL;
500
      file->fileEntry.isDirectory = 1;
501
      file->fileEntry.mediaType = FILE_MEDIA_FLASH;
502
      file->fileEntry.blockSize = FLASH_BLOCK_SIZE;
503
      file->block = NULL;
504
      rc = MediaBlockInit();
505
      if(rc == 1)
506
         BlockRead(file, BLOCK_MALLOC);
507
      else
508
         BlockRead(file, FLASH_START);
509
      file->fileEntry.blockIndex = file->blockIndex;
510
      OS_fclose(file);
511
#endif
512
   }
513
 
514
   file = (OS_FILE*)malloc(sizeof(OS_FILE));
515
   if(file == NULL)
516
      return NULL;
517
   OS_MutexPend(mutexFilesys);
518
   if(name[0] == 0 || strcmp(name, "/") == 0)
519
   {
520
      FileOpen(file, NULL, NULL);
521
      OS_MutexPost(mutexFilesys);
522
      return file;
523
   }
524
   if(mode[0] == 'w')
525
   {
526
      //Don't over write a directory
527
      fileEntry.isDirectory = 0;
528
      rc = FileFindRecursive(&dir, name, &fileEntry, filename);
529
      if(rc == 0)
530
      {
531
         if(fileEntry.isDirectory)
532
         {
533
            free(file);
534
            return NULL;
535
         }
536
         OS_fdelete(name);
537
      }
538
   }
539
   rc = FileFindRecursive(&dir, name, &fileEntry, filename);
540
   if(rc == -2 || (rc && mode[0] == 'r'))
541
   {
542
      free(file);
543
      OS_MutexPost(mutexFilesys);
544
      return NULL;
545
   }
546
   if(rc)
547
      fileEntry.valid = 0;
548
   rc = FileOpen(file, filename, &fileEntry);  //Open file
549
   file->fullname[0] = 0;
550 360 rhoads
   strncat(file->fullname, name, FULL_NAME_SIZE);
551 398 rhoads
   OS_MutexPost(mutexFilesys);
552 360 rhoads
   if(mode[0] == 'a')
553
      OS_fseek(file, 0, 2);  //goto end of file
554 398 rhoads
   return file;
555
}
556
 
557
 
558
void OS_fclose(OS_FILE *file)
559
{
560
   OS_FileEntry_t fileEntry;
561
   OS_FILE dir;
562
   char filename[FILE_NAME_SIZE];
563
   int rc;
564
 
565
   if(file->fileModified)
566
   {
567
      // Write file->fileEntry into parent directory
568
      OS_MutexPend(mutexFilesys);
569
      BlockRead(file, BLOCK_EOF);
570
      rc = FileFindRecursive(&dir, file->fullname, &fileEntry, filename);
571
      if(file->fileEntry.mediaType == FILE_MEDIA_FLASH && rc == 0)
572
      {
573
         // Invalidate old entry and add new entry at the end
574
         fileEntry.valid = 0;
575
         OS_fwrite(&fileEntry, sizeof(OS_FileEntry_t), 1, &dir);
576
         FileFind(&dir, "endoffile", &fileEntry);
577
      }
578
      OS_fwrite(&file->fileEntry, sizeof(OS_FileEntry_t), 1, &dir);
579
      BlockRead(&dir, BLOCK_EOF);  //flush data
580
      OS_MutexPost(mutexFilesys);
581
   }
582
   free(file);
583
}
584
 
585
 
586
int OS_fmkdir(char *name)
587
{
588
   OS_FILE *file;
589
   file = OS_fopen(name, "w+");
590
   if(file == NULL)
591
      return -1;
592
   file->fileEntry.isDirectory = 1;
593
   OS_fclose(file);
594
   return 0;
595
}
596
 
597
 
598
void OS_fdelete(char *name)
599
{
600
   OS_FILE dir, file;
601
   OS_FileEntry_t fileEntry;
602
   int rc;
603
   uint32 blockIndex;
604
   char filename[FILE_NAME_SIZE];  //Name without directories
605
 
606
   OS_MutexPend(mutexFilesys);
607
   rc = FileFindRecursive(&dir, name, &fileEntry, filename);
608
   if(rc == 0)
609
   {
610
      FileOpen(&file, NULL, &fileEntry);
611
      for(blockIndex = file.blockIndex; file.block->next != BLOCK_EOF; blockIndex = file.blockIndex)
612
      {
613
         BlockRead(&file, file.block->next);
614
         MediaBlockFree(&file, blockIndex);
615
      }
616
      MediaBlockFree(&file, blockIndex);
617
      fileEntry.valid = 0;
618
      OS_fwrite((char*)&fileEntry, sizeof(OS_FileEntry_t), 1, &dir);
619
      BlockRead(&dir, BLOCK_EOF);
620
   }
621
   OS_MutexPost(mutexFilesys);
622
}
623
 
624
 
625
int OS_flength(char *entry)
626
{
627
   OS_FileEntry_t *entry2=(OS_FileEntry_t*)entry;
628
   return entry2->length;
629
}
630
 
631
 
632
int OS_fdir(OS_FILE *dir, char name[64])
633
{
634
   OS_FileEntry_t *fileEntry = (OS_FileEntry_t*)name;
635
   int count;
636
   for(;;)
637
   {
638
      count = OS_fread(fileEntry, sizeof(OS_FileEntry_t), 1, dir);
639
      if(count == 0 || fileEntry->blockIndex == BLOCK_EOF)
640
         return -1;
641
      if(fileEntry->valid == 1)
642
         break;
643
   }
644
   return 0;
645
}
646
 
647
/*************************************************/
648
#define TEST_FILES
649
#ifdef TEST_FILES
650
int DirRecursive(char *name)
651
{
652
   OS_FileEntry_t fileEntry;
653
   OS_FILE *dir;
654
   char fullname[FULL_NAME_SIZE];
655
   int rc;
656
 
657
   dir = OS_fopen(name, "r");
658
   for(;;)
659
   {
660
      rc = OS_fdir(dir, (char*)&fileEntry);
661
      if(rc)
662
         break;
663
      printf("%s %d\n", fileEntry.name, fileEntry.length);
664
      if(fileEntry.isDirectory)
665
      {
666
         if(strcmp(name, "/") == 0)
667
            sprintf(fullname, "/%s", fileEntry.name);
668
         else
669
            sprintf(fullname, "%s/%s", name, fileEntry.name);
670
         DirRecursive(fullname);
671
      }
672
   }
673
   OS_fclose(dir);
674
   return 0;
675
}
676
 
677
int OS_ftest(void)
678
{
679
   OS_FILE *file;
680
   char *buf;
681
   int count;
682
   int i, j;
683
 
684
   buf = (char*)malloc(5000);
685
   memset(buf, 0, 5000);
686
   for(count = 0; count < 4000; ++count)
687
      buf[count] = (char)('A' + (count % 26));
688
   OS_fmkdir("dir");
689
   OS_fmkdir("/dir/subdir");
690
   file = OS_fopen("/dir/subdir/test.txt", "w");
691
   count = OS_fwrite(buf, 1, 4000, file);
692
   OS_fclose(file);
693
   memset(buf, 0, 5000);
694
   file = OS_fopen("/dir/subdir/test.txt", "r");
695
   count = OS_fread(buf, 1, 5000, file);
696
   OS_fclose(file);
697
   printf("(%s)\n", buf);
698
 
699
   DirRecursive("/");
700
 
701
   for(i = 0; i < 5; ++i)
702
   {
703
      sprintf(buf, "/dir%d", i);
704
      OS_fmkdir(buf);
705
      for(j = 0; j < 5; ++j)
706
      {
707
         sprintf(buf, "/dir%d/file%d%d", i, i, j);
708
         file = OS_fopen(buf, "w");
709
         sprintf(buf, "i=%d j=%d", i, j);
710
         OS_fwrite(buf, 1, 8, file);
711
         OS_fclose(file);
712
      }
713
   }
714
 
715
   OS_fdelete("/dir1/file12");
716
   DirRecursive("/");
717
   file = OS_fopen("/baddir/myfile.txt", "w");
718
   if(file)
719
      printf("ERROR!\n");
720
 
721
   for(i = 0; i < 5; ++i)
722
   {
723
      for(j = 0; j < 5; ++j)
724
      {
725
         sprintf(buf, "/dir%d/file%d%d", i, i, j);
726
         file = OS_fopen(buf, "r");
727
         if(file)
728
         {
729
            count = OS_fread(buf, 1, 500, file);
730
            printf("i=%d j=%d count=%d (%s)\n", i, j, count, buf);
731
            OS_fclose(file);
732
         }
733
      }
734
   }
735
 
736
   OS_fdelete("/dir/subdir/test.txt");
737
   OS_fdelete("/dir/subdir");
738
   OS_fdelete("/dir");
739
   for(i = 0; i < 5; ++i)
740
   {
741
      for(j = 0; j < 5; ++j)
742
      {
743
         sprintf(buf, "/dir%d/file%d%d", i, i, j);
744
         OS_fdelete(buf);
745
      }
746
      sprintf(buf, "/dir%d", i);
747
      OS_fdelete(buf);
748
   }
749
 
750
   DirRecursive("/");
751
 
752
   free(buf);
753
   return 0;
754
}
755
#endif  //TEST_FILES

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.