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; }
void print_tree(struct fat_vol_handle *vol, struct fat_file_handle *dir, int nest) { struct fat_file_handle subdir; struct dirent ent; while(!fat_readdir(dir, &ent)) { if((strcmp(ent.d_name, ".") == 0) || (strcmp(ent.d_name, "..") == 0)) continue; for(int i = 0; i < nest; i++) printf("\t"); printf("%s\n", ent.d_name); if(ent.fat_attr == FAT_ATTR_DIRECTORY) { fat_chdir(vol, ent.d_name); assert(fat_open(vol, ".", 0, &subdir) == 0); print_tree(vol, &subdir, nest + 1); fat_chdir(vol, ".."); } } }
void print_tree(struct fat_vol_handle *vol, struct fat_file_handle *dir, const char *path) { struct dirent ent; char tmppath[1024]; struct fat_file_handle subdir; while(!fat_readdir(dir, &ent)) { if((strcmp(ent.d_name, ".") == 0) || (strcmp(ent.d_name, "..") == 0)) continue; sprintf(tmppath, "%s/%s", path, ent.d_name); puts(tmppath); if(ent.fat_attr == FAT_ATTR_DIRECTORY) { fat_chdir(vol, ent.d_name); assert(fat_open(vol, ".", 0, &subdir) == 0); print_tree(vol, &subdir, tmppath); fat_chdir(vol, ".."); } } }
bool _FAT_Init(void) { // Try mounting sd/mmc bool sdOK = false; if( _io_ds2_mmcf.fn_startup() == 0) //NO ERROR sdOK = _FAT_partition_freeMount( PI_DEFAULT, &_io_ds2_mmcf, 8 ); else return false; if(sdOK == true) fat_chdir ("fat:/"); return sdOK; }
/** * Open a file * * \param name File name * \param mode Mode to open file * * - _O_BINARY Raw mode. * - _O_TEXT End of line translation. * - _O_EXCL Open only if it does not exist. * - _O_RDONLY Read only. any of the write modes will (potentially) modify the file or directory entry * - _O_CREAT Create file if it does not exist. * - _O_APPEND Always write at the end. * - _O_RDWR Read and write. * - _O_WRONLY Write only. * - _O_TRUNC Truncate file if it exists. This is currently not supported * * \return File handle */ int fat_open(const char *name, int mode) { fatffdata_t *ff; fhandle_t *fd = 0; char *p; int handle_index; TRACE_FAT("fat_open %s\n",name); if (!name || !*name) return -1; if (strchr(name,'/')) // if the name has a path component fat_chdir((char*)name); // follow the path p = (char*) name; while ( strchr(p,'/') != 0 ) // if the name has an initial path component p = strchr(p,'/') + 1; // step past it // alloc for findfile struct ff = malloc(sizeof(fatffdata_t)); if (ff == NULL) { TRACE_FAT("cannot alloc fatffdata_t in fat_open\n"); return -1; // fail } // alloc for file spec fd = malloc(sizeof(fhandle_t)); // get a struct if (fd == NULL) { free(ff); TRACE_FAT("cannot alloc fhandle_t in fat_open\n"); return -1; // fail } for (handle_index=0;handle_index<MAX_FILES;handle_index++) { if (fd_handles[handle_index] == NULL) break; } if (handle_index == MAX_FILES) { free(ff); free(fd); TRACE_FAT("no free handle slots in fat_open\n"); return -1; // fail } memset(fd,0,sizeof(fhandle_t)); // clear all fields fd->mode = mode; // save mode // expecting existing file // try find it if ( find_first(p, ff) == 0) { // found existing entry TRACE_FAT("found existing file\n"); // NASTY !!!! TODO // fd->diroffset = ( ((char*)&ff->ff_de) - ((char*)sector_buffer)) / sizeof(fatdirentry_t); // fd->dirsector = sector_in_buffer; // setup pointers fd->firstcluster = ((uint32_t)ff->ff_de.deHighClust << 16) | (ff->ff_de.deStartCluster); // starting cluster fd->cluster = fd->firstcluster; // current cluster fd->foffset = 0; // offset in file fd->highwater = ff->ff_de.deFileSize; // max position in file goto open_ok; } else { // file not found, this is always a fault as we do not support creating files free(ff); free(fd); TRACE_FAT("file not found: %s\n", name); return -1; // fail } open_ok: free(ff); fd_handles[handle_index] = fd; TRACE_FAT("fat_open returns %d\n",handle_index); return handle_index; }