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

Subversion Repositories mlite

[/] [mlite/] [trunk/] [kernel/] [filesys.c] - Blame information for rev 302

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

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

powered by: WebSVN 2.1.0

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