struct dirent *fat_read_directory(struct fs *fs, char **name) { printf("[fat_read_directory]\r\n"); struct dirent *cur_dir = fat_read_dir((struct fat_fs *)fs, (void*)0); while(*name) { // Search the directory entries for one of the requested name int found = 0; while(cur_dir) { if(!strcmp(*name, cur_dir->name)) { if(!cur_dir->is_dir) { errno = ENOTDIR; return (void*)0; } found = 1; cur_dir = fat_read_dir((struct fat_fs *)fs, cur_dir); name++; break; } cur_dir = cur_dir->next; } if(!found) { #ifdef FAT_DEBUG printf("FAT: path part %s not found\r\n", *name); #endif errno = ENOENT; return (void*)0; } } return cur_dir; }
SdErrorCode directoryNextEntry(char* buffer, uint8_t bufsize) { struct fat_dir_entry_struct entry; // This is a bit of a hack. For whatever reason, some filesystems return // files with nulls as the first character of their name. This isn't // necessarily broken in of itself, but a null name is also our way // of signalling we've gone through the directory, so we discard these // entries. We have an upper limit on the number of entries to cycle // through, so we don't potentially lock up here. uint8_t tries = 5; while (tries) { if (fat_read_dir(dd, &entry)) { int i; for (i = 0; (i < bufsize-1) && entry.long_name[i] != 0; i++) { buffer[i] = entry.long_name[i]; } buffer[i] = 0; if (i > 0) { break; } else { tries--; } } else { buffer[0] = 0; break; } } return SD_SUCCESS; }
/* recursively delete entries */ bool SDCardEntry::deleteFirstEntry() { if (!isDirectory()) return false; fat_dir_struct *dd = fat_open_dir(SDCard.fs, &dir_entry); if (dd == NULL) return false; SDCardEntry entry; uint8_t ret; again: ret = fat_read_dir(dd, &entry.dir_entry); if (!strcmp(entry.dir_entry.long_name, ".") || !strcmp(entry.dir_entry.long_name, "..")) { goto again; } fat_close_dir(dd); if (!ret) { return false; } entry.setFromParentEntry(this); return entry.deleteEntry(true); }
//! This function goes to at the parent directory //! //! @return false in case of error, see global value "fs_g_status" for more detail //! @return true otherwise //! //! @verbatim //! After, the filtered file list changes and contains the files and directories of the new directory. //! By default, the file selected in filtered file list is the previous (children) directory. //! @endverbatim //! bool nav_filterlist_gotoparent( void ) { uint32_t u32_cluster_old_dir; if (!fat_check_mount_noopen()) return false; if (0 == fs_g_nav.u32_cluster_sel_dir) { fs_g_status = FS_ERR_IS_ROOT; // There aren't parent return false; } // Select and read information about directory ".." fs_g_nav_fast.u16_entry_pos_sel_file = 1; if ( !fat_read_dir()) return false; fat_get_entry_info(); // Save the children directory cluster u32_cluster_old_dir = fs_g_nav.u32_cluster_sel_dir; // Select the parent directory via information present in the current directory ".." fs_g_nav.u32_cluster_sel_dir = fs_g_nav_entry.u32_cluster; // Select the children directory in new directory (=parent directory) if( false == nav_filterlist_reset()) return false; while( nav_filterlist_next() ) { if (fs_g_nav_entry.u32_cluster == u32_cluster_old_dir) return true; // It is the children directory } fs_g_status = FS_ERR_FS; return false; }
bool findFileInDir(const char* name, struct fat_dir_entry_struct* dir_entry) { while(fat_read_dir(dd, dir_entry)) { if(strcmp(dir_entry->long_name, name) == 0) { fat_reset_dir(dd); return true; } } return false; }
uint8_t find_file_in_dir(struct fat_fs_struct* fs, struct fat_dir_struct* dd, const char* name, struct fat_dir_entry_struct* dir_entry) { while(fat_read_dir(dd, dir_entry)) { if(strcmp(dir_entry->long_name, name) == 0) { fat_reset_dir(dd); return 1; } } return 0; }
uint8_t RepRapSDCard::sd_scan_next(char* buffer, uint8_t bufsize) { struct fat_dir_entry_struct entry; if (fat_read_dir(dd, &entry)) { int i; for (i = 0; (i < bufsize-1) && entry.long_name[i] != 0; i++) { buffer[i] = entry.long_name[i]; } buffer[i] = 0; } else { buffer[0] = 0; } return 0; }
bool FatFsClass::find_file_in_dir(const char *file_name, struct fat_dir_entry_struct *dir_entry) { while(1){ if(!fat_read_dir(_dd, dir_entry)) break; if(strcmp(file_name, dir_entry->long_name) == 0){ fat_reset_dir(_dd); return true; } } return false; }
uint8_t find_file_in_dir(struct fat_fs_struct* fs, struct fat_dir_struct* dd, const char* name, struct fat_dir_entry_struct* dir_entry) { fat_reset_dir(dd); //Make sure to start from the beginning of the directory! while(fat_read_dir(dd, dir_entry)) { if(strcmp(dir_entry->long_name, name) == 0) { //fat_reset_dir(dd); return 1; } } return 0; }
char get_wav_filename(struct fat_dir_struct* cur_dir, char * new_file) { //'directory' is a global variable of type directory_entry_struct //Get the next file from the root directory while(fat_read_dir(cur_dir, &dir_entry)) { //If we found a .wav file, copy the name and exit! if(strcmp(&dir_entry.long_name[strlen(dir_entry.long_name)-4], ".wav")==0) { sprintf(new_file, "%s", dir_entry.long_name); return 1; } } return 0; //If we got to the end of the current directory without finding a wav file, return 0 }
bool SDCardEntry::findFile(const char *name, struct fat_dir_entry_struct *entry) { if (!isDirectory()) return false; fat_dir_struct *dd = fat_open_dir(SDCard.fs, &dir_entry); if (dd == NULL) return false; while (fat_read_dir(dd, entry)) { if (strcmp(entry->long_name, name) == 0) { fat_close_dir(dd); return true; } } fat_close_dir(dd); return false; }
int SDCardEntry::listDirectory(SDCardEntry entries[], int maxCount) { if (!isDirectory()) return -1; int entryCount = 0; fat_dir_struct *dd = fat_open_dir(SDCard.fs, &dir_entry); if (dd == NULL) return -1; while (fat_read_dir(dd, &entries[entryCount].dir_entry) && (entryCount < maxCount)) { if (!strcmp(entries[entryCount].dir_entry.long_name, ".")) continue; entries[entryCount].setFromParentEntry(this); entryCount++; } fat_close_dir(dd); return entryCount; }
int16_t parse_cmd_sd_dir(char *cmd, char *output, uint16_t len) { if (vfs_sd_rootnode == 0) return ECMD_FINAL(snprintf_P(output, len, PSTR("SD/MMC backend not available."))); if (cmd[0] != ECMD_STATE_MAGIC) { fat_reset_dir(vfs_sd_rootnode); cmd[0] = ECMD_STATE_MAGIC; } struct fat_dir_entry_struct dir_entry; if (!fat_read_dir(vfs_sd_rootnode, &dir_entry)) return ECMD_FINAL_OK; return ECMD_AGAIN(snprintf_P(output, len, PSTR("%32s%c %ld"), dir_entry.long_name, dir_entry.attributes & FAT_ATTRIB_DIR ? '/' : ' ', dir_entry.file_size)); }
bool SDCardEntry::isEmpty() { if (!isDirectory()) { return false; } fat_dir_struct *dd = fat_open_dir(SDCard.fs, &dir_entry); if (dd == NULL) return -1; uint8_t cnt = 0; struct fat_dir_entry_struct entry; uint8_t ret; while ((ret = fat_read_dir(dd, &entry))) { if (!strcmp(entry.long_name, ".") || !strcmp(entry.long_name, "..")) { continue; } else { cnt++; break; } } fat_close_dir(dd); return (cnt == 0); }
int fat_load(const char *path, uint8_t *buf, uint32_t buf_size) { const char *c = path; int offset; struct dirent *node = NULL; struct dirent *current_node = NULL; struct dirent *free_node = NULL; struct fs* filesys = fs_get(); if( path == NULL) { return -1; } // find first folder/file // look for '/' or '/' while(1) { if(*c == '/') { c++; } offset = fat_get_next_path(c); char* path_buf = (char*)malloc(sizeof(char) * offset + 1); memcpy(path_buf, c, offset); path_buf[offset] = '\0'; // now path_buf contains the next segment in the path. // search for it! // delete all the other nodes in the linked list node = fat_read_dir( (struct fat_fs*)filesys, current_node); //uart_puts("fat read dir gives:\r\n"); while(node) { // uart_puts(node->name); // uart_puts("\r\n"); if( !strcmp( node->name, path_buf) ) { current_node = node; node = node->next; } else { free_node = node; node = node->next; free(free_node); } } if( current_node == NULL) { return -1; } // current_node now contains the dirent for the next path segment if(current_node->is_dir) { } else { break; // is file } c = c + offset; free(path_buf); } // do something to load the file if( current_node->byte_size > buf_size) { uart_put_uint32_t(current_node->byte_size, 10); return -1; } int ret = fat_read( (struct fat_fs*)filesys, current_node->cluster_no, buf, current_node->byte_size, 0); return ret; }
int main() { /* we will just use ordinary idle mode */ set_sleep_mode(SLEEP_MODE_IDLE); /* setup uart */ uart_init(); while(1) { /* setup sd card slot */ if(!sd_raw_init()) { #if DEBUG uart_puts_p(PSTR("MMC/SD initialization failed\n")); #endif continue; } /* open first partition */ struct partition_struct* partition = partition_open(sd_raw_read, sd_raw_read_interval, #if SD_RAW_WRITE_SUPPORT sd_raw_write, sd_raw_write_interval, #else 0, 0, #endif 0 ); if(!partition) { /* If the partition did not open, assume the storage device * is a "superfloppy", i.e. has no MBR. */ partition = partition_open(sd_raw_read, sd_raw_read_interval, #if SD_RAW_WRITE_SUPPORT sd_raw_write, sd_raw_write_interval, #else 0, 0, #endif -1 ); if(!partition) { #if DEBUG uart_puts_p(PSTR("opening partition failed\n")); #endif continue; } } /* open file system */ struct fat_fs_struct* fs = fat_open(partition); if(!fs) { #if DEBUG uart_puts_p(PSTR("opening filesystem failed\n")); #endif continue; } /* open root directory */ struct fat_dir_entry_struct directory; fat_get_dir_entry_of_path(fs, "/", &directory); struct fat_dir_struct* dd = fat_open_dir(fs, &directory); if(!dd) { #if DEBUG uart_puts_p(PSTR("opening root directory failed\n")); #endif continue; } /* print some card information as a boot message */ print_disk_info(fs); /* provide a simple shell */ char buffer[24]; while(1) { /* print prompt */ uart_putc('>'); uart_putc(' '); /* read command */ char* command = buffer; if(read_line(command, sizeof(buffer)) < 1) continue; /* execute command */ if(strcmp_P(command, PSTR("init")) == 0) { break; } else if(strncmp_P(command, PSTR("cd "), 3) == 0) { command += 3; if(command[0] == '\0') continue; /* change directory */ struct fat_dir_entry_struct subdir_entry; if(find_file_in_dir(fs, dd, command, &subdir_entry)) { struct fat_dir_struct* dd_new = fat_open_dir(fs, &subdir_entry); if(dd_new) { fat_close_dir(dd); dd = dd_new; continue; } } uart_puts_p(PSTR("directory not found: ")); uart_puts(command); uart_putc('\n'); } else if(strcmp_P(command, PSTR("ls")) == 0) { /* print directory listing */ struct fat_dir_entry_struct dir_entry; while(fat_read_dir(dd, &dir_entry)) { uint8_t spaces = sizeof(dir_entry.long_name) - strlen(dir_entry.long_name) + 4; uart_puts(dir_entry.long_name); uart_putc(dir_entry.attributes & FAT_ATTRIB_DIR ? '/' : ' '); while(spaces--) uart_putc(' '); uart_putdw_dec(dir_entry.file_size); uart_putc('\n'); } } else if(strncmp_P(command, PSTR("cat "), 4) == 0) { command += 4; if(command[0] == '\0') continue; /* search file in current directory and open it */ struct fat_file_struct* fd = open_file_in_dir(fs, dd, command); if(!fd) { uart_puts_p(PSTR("error opening ")); uart_puts(command); uart_putc('\n'); continue; } /* print file contents */ uint8_t buffer[8]; uint32_t offset = 0; while(fat_read_file(fd, buffer, sizeof(buffer)) > 0) { uart_putdw_hex(offset); uart_putc(':'); for(uint8_t i = 0; i < 8; ++i) { uart_putc(' '); uart_putc_hex(buffer[i]); } uart_putc('\n'); offset += 8; } fat_close_file(fd); } else if(strcmp_P(command, PSTR("disk")) == 0) { if(!print_disk_info(fs)) uart_puts_p(PSTR("error reading disk info\n")); } #if FAT_WRITE_SUPPORT else if(strncmp_P(command, PSTR("rm "), 3) == 0) { command += 3; if(command[0] == '\0') continue; struct fat_dir_entry_struct file_entry; if(find_file_in_dir(fs, dd, command, &file_entry)) { if(fat_delete_file(fs, &file_entry)) continue; } uart_puts_p(PSTR("error deleting file: ")); uart_puts(command); uart_putc('\n'); } else if(strncmp_P(command, PSTR("touch "), 6) == 0) { command += 6; if(command[0] == '\0') continue; struct fat_dir_entry_struct file_entry; if(!fat_create_file(dd, command, &file_entry)) { uart_puts_p(PSTR("error creating file: ")); uart_puts(command); uart_putc('\n'); } } else if(strncmp_P(command, PSTR("write "), 6) == 0) { command += 6; if(command[0] == '\0') continue; char* offset_value = command; while(*offset_value != ' ' && *offset_value != '\0') ++offset_value; if(*offset_value == ' ') *offset_value++ = '\0'; else continue; /* search file in current directory and open it */ struct fat_file_struct* fd = open_file_in_dir(fs, dd, command); if(!fd) { uart_puts_p(PSTR("error opening ")); uart_puts(command); uart_putc('\n'); continue; } int32_t offset = strtolong(offset_value); if(!fat_seek_file(fd, &offset, FAT_SEEK_SET)) { uart_puts_p(PSTR("error seeking on ")); uart_puts(command); uart_putc('\n'); fat_close_file(fd); continue; } /* read text from the shell and write it to the file */ uint8_t data_len; while(1) { /* give a different prompt */ uart_putc('<'); uart_putc(' '); /* read one line of text */ data_len = read_line(buffer, sizeof(buffer)); if(!data_len) break; /* write text to file */ if(fat_write_file(fd, (uint8_t*) buffer, data_len) != data_len) { uart_puts_p(PSTR("error writing to file\n")); break; } } fat_close_file(fd); } else if(strncmp_P(command, PSTR("mkdir "), 6) == 0) { command += 6; if(command[0] == '\0') continue; struct fat_dir_entry_struct dir_entry; if(!fat_create_dir(dd, command, &dir_entry)) { uart_puts_p(PSTR("error creating directory: ")); uart_puts(command); uart_putc('\n'); } } #endif #if SD_RAW_WRITE_BUFFERING else if(strcmp_P(command, PSTR("sync")) == 0) { if(!sd_raw_sync()) uart_puts_p(PSTR("error syncing disk\n")); } #endif else if(strcmp_P(command, PSTR("sync")) == 0) { // vytvor adresar mereni_teploty // nekonecna smycka - pomoci RTCka kazdou minutu odmer teplotu } else { uart_puts_p(PSTR("unknown command: ")); uart_puts(command); uart_putc('\n'); } } /* close directory */ fat_close_dir(dd); /* close file system */ fat_close(fs); /* close partition */ partition_close(partition); } return 0; }
int syscall_file_readdir(unsigned long *vdirdesc, char *dirent) { return fat_read_dir(vdirdesc, dirent); }
//! This function moves back to the parent directory and selects this directory //! //! @param b_direction direction of navigation (FS_FIND_NEXT or FS_FIND_PREV) //! //! @return The state of the function //! static navauto_mov_explorer_rec_t navauto_mov_explorer_updir(bool b_direction) { bool b_updir_ok = false; // To support directory only mode if (g_navauto_exp_mode == NAVAUTO_MODE_DIRONLY) return NAVAUTO_MOV_EXPLORER_ERROR; // To Support subdirectories mode if (g_navauto_exp_mode == NAVAUTO_MODE_DIRSUB) { // If the selection is on the root level if (g_navauto_u16_dir_level <= g_navauto_u16_dir_level_root) return NAVAUTO_MOV_EXPLORER_ERROR; } #if defined(NAVAUTO_SPEED_OPTIMIZATION) && NAVAUTO_SPEED_OPTIMIZATION == true if (i_navauto_cache >= 0) { if (i_navauto_cache >= NAVAUTO_SPEED_OPTIMIZATION_LEVEL) { if (nav_dir_gotoparent()) { b_updir_ok = true; i_navauto_cache--; } } else { fat_clear_entry_info_and_ptr(); // Look for the "../" directory entry fs_g_nav_fast.u16_entry_pos_sel_file = 1; if (!fat_read_dir()) return NAVAUTO_MOV_EXPLORER_ERROR; fat_get_entry_info(); fs_g_nav.u32_cluster_sel_dir = fs_g_nav_entry.u32_cluster; fs_g_nav.u16_pos_sel_file = navauto_cache[i_navauto_cache].u16_pos_file; fs_g_nav_fast.u16_entry_pos_sel_file = navauto_cache[i_navauto_cache].u16_entry_pos_file; fat_read_dir(); fat_get_entry_info(); i_navauto_cache--; b_updir_ok = true; } } #else if (nav_dir_gotoparent()) b_updir_ok = true; #endif if (b_updir_ok) { // Set folder change bit bitfield_status.folder_change = 1; g_navauto_u16_dir_level--; return NAVAUTO_MOV_EXPLORER_RECURSIVE; } else // If it fails, means the pointer is on the root return NAVAUTO_MOV_EXPLORER_ERROR; }
void IBN_FileAccess (u8 nParamsGet_u8,u8 CMD_u8,u32 Param_u32) { s32 FileID_s32; s32 Ret_s32; // testtt[1000] = 4; if (0 == nParamsGet_u8) { CI_LocalPrintf ("File access\r\n"); CI_LocalPrintf (" 0 = Set lun 0\r\n"); CI_LocalPrintf (" 1 = Mount\r\n"); CI_LocalPrintf (" 2 = Show free space\r\n"); CI_LocalPrintf ("11 = Set BUSY Lun 0\r\n"); CI_LocalPrintf ("12 = Update contend lun X\r\n"); CI_LocalPrintf ("16 = Copy test.txt to test1.txt\n\r"); CI_LocalPrintf ("17 = Format LUN X\n\r"); return; } switch (CMD_u8) { case 0 : fs_g_nav.u8_lun = 0; // On chip RAM break; #if LUN_2 == ENABLE case 1 : if (2 != nParamsGet_u8) { CI_LocalPrintf ("USAGE: fa 1 [lun]\r\n"); break; } b_fsaccess_init (); fs_g_nav.u8_lun = Param_u32; // On chip RAM virtual_test_unit_ready() ; if (TRUE == fat_mount ()) { CI_LocalPrintf ("Mount LUN %d OK\r\n",Param_u32); } else { CI_LocalPrintf ("Mount LUN %d FAIL - %d - %s\r\n",Param_u32,fs_g_status, IBN_FileSystemErrorText(fs_g_status)); } // nav_partition_mount (); break; #endif case 2 : CI_LocalPrintf ("Free mem = %d sectors\r\n",fat_getfreespace()); break; case 3 : FileID_s32 = open ("test.txt",O_CREAT|O_RDWR); CI_LocalPrintf ("Open = %d - %d - %s\r\n",FileID_s32,fs_g_status, IBN_FileSystemErrorText(fs_g_status)); Ret_s32 = write (FileID_s32,"Test\n",6); CI_LocalPrintf ("Write = %d \r\n",Ret_s32); /* if (TRUE == fat_cache_flush ()) { CI_LocalPrintf ("fat_cache_flush OK\r\n"); } else { CI_LocalPrintf ("fat_cache_flush FAIL - %d - %s\r\n",fs_g_status, IBN_FileSystemErrorText(fs_g_status)); } */ Ret_s32 = close (FileID_s32); CI_LocalPrintf ("Close = %d \r\n",Ret_s32); #if LUN_2 == ENABLE virtual_unit_state_e = CTRL_BUSY; // only for ram disk #endif sd_mmc_mci_unit_state_e = CTRL_BUSY; vTaskDelay (500); #if LUN_2 == ENABLE virtual_unit_state_e = CTRL_GOOD; #endif sd_mmc_mci_unit_state_e = CTRL_GOOD; break; case 4 : if (2 != nParamsGet_u8) { CI_LocalPrintf ("USAGE: fa 6 [lun]\r\n"); break; } FAI_Write (Param_u32); break; case 5 : FAI_ShowDirContent (); break; case 6: if (2 != nParamsGet_u8) { CI_LocalPrintf ("USAGE: fa 6 [lun]\r\n"); break; } FAI_mount(Param_u32); break; case 7 : FAI_free_space(Param_u32); break; case 8 : FAI_nb_drive(); break; case 9 : FAI_Dir (0); break; case 10 : if (TRUE == nav_drive_set (Param_u32)) { CI_LocalPrintf ("nav_drive_set OK\r\n"); } else { CI_LocalPrintf ("nav_drive_set FAIL - %d - %s\r\n",fs_g_status, IBN_FileSystemErrorText(fs_g_status)); } break; case 11 : #if LUN_2 == ENABLE CI_LocalPrintf ("Set LUN %d CTRL_BUSY\n\r",Param_u32); //virtual_unit_state_e = CTRL_BUSY; set_virtual_unit_busy (); #endif break; case 12 : CI_LocalPrintf ("Update LUN %d\n\r",Param_u32); FAI_UpdateContend (Param_u32);; break; case 13 : CI_LocalPrintf ("fat_cache_flush\n\r"); fat_cache_flush (); break; case 14 : CI_LocalPrintf ("fat_check_device\n\r"); fat_check_device (); break; case 15 : CI_LocalPrintf ("fat_check_device\n\r"); fat_read_dir (); break; case 16 : CI_LocalPrintf ("Copy test.txt to test1.txt\n\r"); { u8 F1[10]; u8 F2[10]; strcpy ((char*)F1,"test.txt"); strcpy ((char*)F2,"test1.txt"); FAI_CopyFile (F1,F2); } break; case 17 : CI_LocalPrintf ("Format LUN %d\n\r",Param_u32); nav_drive_set(Param_u32); if( !nav_drive_format(FS_FORMAT_FAT) ) { CI_LocalPrintf ("Format LUN %d - ERROR\n\r",Param_u32); } break; } }