int main(int argc, char *argv[]) { struct block_device *bldev; FatVol vol; FatFile file; char *rootpath = argc > 2 ? argv[2] : "/"; bldev = block_device_file_new(argc > 1 ? argv[1] : "fat32.img", "r+"); assert(bldev != NULL); assert(fat_vol_init(bldev, &vol) == 0); fprintf(stderr, "Fat type is FAT%d\n", vol.type); fat_mkdir(&vol, "Directory1"); fat_mkdir(&vol, "Directory2"); fat_mkdir(&vol, "Directory3"); assert(fat_chdir(&vol, "Directory1") == 0); fat_mkdir(&vol, "Directory1"); fat_mkdir(&vol, "Directory2"); fat_mkdir(&vol, "Directory3"); if(fat_create(&vol, "Message file with a long name.txt", O_WRONLY, &file) == 0) { for(int i = 0; i < 100; i++) { char message[80]; sprintf(message, "Here is a message %d\n", i); assert(fat_write(&file, message, strlen(message)) == (int)strlen(message)); } } assert(fat_chdir(&vol, "..") == 0); assert(fat_open(&vol, ".", O_RDONLY, &file) == 0); print_tree(&vol, &file, rootpath[0] == '/' ? rootpath + 1 : rootpath); block_device_file_destroy(bldev); }
int main(void) { struct mmc_port spi2; struct block_mbr_partition part; struct fat_vol_handle vol; struct fat_file_handle file; stm32_setup(); mmc_init(SPI2, GPIOA, GPIO3, &spi2); mbr_partition_init(&part, (struct block_device *)&spi2, 0); assert(fat_vol_init((struct block_device *)&part, &vol) == 0); printf("Fat type is FAT%d\n", vol.type); time_counter = 0; char dirname[20]; char filename[20]; char buffer[2000]; for(int i = 0; i < 100; i++) { sprintf(dirname, "Dir%d", i); fat_mkdir(&vol, dirname); assert(fat_chdir(&vol, dirname) == 0); for(int j = 0; j < 100; j++) { sprintf(filename, "File%d", j); assert(fat_create(&vol, filename, O_WRONLY, &file) == 0); assert(fat_write(&file, buffer, sizeof(buffer)) == sizeof(buffer)); } assert(fat_chdir(&vol, "..") == 0); } asm("bkpt"); assert(fat_open(&vol, ".", 0, &file) == 0); print_tree(&vol, &file, 0); while (1) { } return 0; }
int __fopen (const char *fname, int openmode) { /* Low level file open function. */ U32 i,fid; int handle; IOB *fcb; START_LOCK (int); /* Find unused _iob[] structure. */ if ((handle = fs_find_iob ()) == EOF) { /* Cannot find any unused _iob[] structure */ RETURN (-1); } fcb = &_iob[handle]; fcb->drive = fs_get_drive (fname); if (fcb->drive != DRV_NONE) { /* Skip drive letter 'X:' */ fname += 2; } else { fcb->drive = _DEF_DRIVE; } if (openmode & OPEN_PLUS) { /* File mode "rw" is currently not supported. */ goto err; } fcb->flags = (openmode & (OPEN_W | OPEN_A)) ? _IOWRT : _IOREAD; if (openmode & OPEN_A) { fcb->flags |= _IOAPPEND; } if (fcb->drive == DRV_MCARD) { /* Open a file on Flash Memory Card. */ fid = (fat_find_file (fname, fcb) == __TRUE) ? 0 : 1; } else { /* Open a file on Embedded Flash/RAM device. */ if (fs_set_params (fcb) == __FALSE) { goto err; } fcb->_fidx = 0; fid = fs_Find_File (fname, fcb); } if (fid == 0) { /* File with a given 'fname' has been found */ for (i = 0; i < _NFILE; i++) { if (i == handle) { /* Skip own file handle. */ continue; } if (!(_iob[i].flags & (_IOREAD|_IOWRT))) { /* File closed. */ continue; } if (_iob[i].drive != fcb->drive) { /* File opened on different drives. */ continue; } if (_iob[i].fileID != fcb->fileID) { /* Different file IDs. */ continue; } if ((_iob[i].drive == DRV_MCARD) && (_iob[i]._currDatClus != fcb->_currDatClus)) { /* Different starting clusters for SD card. */ continue; } /* This file is opened. */ if ((_iob[i].flags & _IOWRT) || (fcb->flags & _IOWRT)) { /* Only multiple fopen for read is allowed. */ goto err; } } if (fcb->flags & _IOAPPEND) { /* Append mode, done here, block appended by _setfpos(). */ fcb->fsize = __getfsize (fcb, __TRUE); RETURN (handle); } if (fcb->drive == DRV_MCARD) { /* Open a file on Flash Memory Card. */ if (fcb->flags & _IOWRT) { fat_delete (fname, fcb); goto mcard; } } else { /* Open a file on embedded Flash/RAM Device. */ if (fcb->flags & _IOWRT) { _fdelete (fcb); goto fdev; } /* Open also 0-size file for reading. */ fcb->_ftop = fcb->_fbot; } RETURN (handle); } /* File not found */ if (fcb->flags & _IOREAD) { err: fcb->flags = 0; RETURN (-1); } if (fcb->drive == DRV_MCARD) { mcard:if (fat_create (fname, fcb) == __FALSE) { goto err; } } else { /* The max. 'fid' found in previous 'fs_Find_File' is here. */ fcb->fileID = fs_get_freeID (fid, fcb); fdev: if (_fcreate (fname, fcb) != 0) { goto err; } } RETURN (handle); END_LOCK; }
fat_dir_t* df_new_file(){ return fat_create(df_mk_name()); }