static FRESULT dir_find ( DIR *dj, /* Pointer to the directory object linked to the file name */ BYTE *dir /* 32-byte working buffer */ ) { FRESULT res; BYTE c; res = dir_rewind(dj); /* Rewind directory object */ if (res != FR_OK) return res; do { res = disk_readp(dir, dj->sect, (dj->index % 16) * 32, 32) /* Read an entry */ ? FR_DISK_ERR : FR_OK; if (res != FR_OK) break; c = dir[DIR_Name]; /* First character */ if (c == 0) { res = FR_NO_FILE; break; } /* Reached to end of table */ if (!(dir[DIR_Attr] & AM_VOL) && !mem_cmp(dir, dj->fn, 11)) /* Is it a valid entry? */ break; res = dir_next(dj); /* Next entry */ } while (res == FR_OK); return res; }
static FRESULT follow_path ( /* FR_OK(0): successful, !=0: error code */ DIR *dj, /* Directory object to return last directory and found object */ BYTE *dir, /* 32-byte working buffer */ const char *path /* Full-path string to find a file or directory */ ) { FRESULT res; while (*path == ' ') path++; /* Strip leading spaces */ if (*path == '/') path++; /* Strip heading separator if exist */ dj->sclust = 0; /* Set start directory (always root dir) */ if ((BYTE)*path < ' ') { /* Null path means the root directory */ res = dir_rewind(dj); dir[0] = 0; } else { /* Follow path */ for (;;) { res = create_name(dj, &path); /* Get a segment */ if (res != FR_OK) break; res = dir_find(dj, dir); /* Find it */ if (res != FR_OK) break; /* Could not find the object */ if (dj->fn[11]) break; /* Last segment match. Function completed. */ if (!(dir[DIR_Attr] & AM_DIR)) { /* Cannot follow path because it is a file */ res = FR_NO_FILE; break; } dj->sclust = get_clust(dir); /* Follow next */ } } return res; }
FRESULT pf_opendir ( DIR *dj, /* Pointer to directory object to create */ const char *path /* Pointer to the directory path */ ) { FRESULT res; BYTE sp[12], dir[32]; FATFS *fs = FatFs; if (!fs) { /* Check file system */ res = FR_NOT_ENABLED; } else { dj->fn = sp; res = follow_path(dj, dir, path); /* Follow the path to the directory */ if (res == FR_OK) { /* Follow completed */ if (dir[0]) { /* It is not the root dir */ if (dir[DIR_Attr] & AM_DIR) /* The object is a directory */ dj->sclust = get_clust(dir); else /* The object is not a directory */ res = FR_NO_FILE; } if (res == FR_OK) res = dir_rewind(dj); /* Rewind dir */ } } return res; }
FRESULT pf_readdir ( DIR *dj, /* Pointer to the open directory object */ FILINFO *fno /* Pointer to file information to return */ ) { FRESULT res; BYTE sp[12], dir[32]; FATFS *fs = FatFs; if (!fs) { /* Check file system */ res = FR_NOT_ENABLED; } else { dj->fn = sp; if (!fno) { res = dir_rewind(dj); } else { res = dir_read(dj, dir); /* Get current directory item */ if (res == FR_NO_FILE) res = FR_OK; if (res == FR_OK) { /* A valid entry is found */ get_fileinfo(dj, dir, fno); /* Get the object information */ res = dir_next(dj); /* Increment read index for next */ if (res == FR_NO_FILE) res = FR_OK; } } } return res; }
static FRESULT dir_find ( DIR *dj, /* Pointer to the directory object linked to the file name */ BYTE *dir /* 32-byte working buffer */ ) { FRESULT res; BYTE c; res = dir_rewind(dj); /* Rewind directory object */ if (res != FR_OK) return res; do { if ( (dj->index & 7) == 0 ) { // Need to read data res = disk_readp( Abuff.DirBuffer, dj->sect, (WORD)((dj->index & 8) * 32), 256) /* Read half a sector */ ? FR_DISK_ERR : FR_OK; } else { res = FR_OK ; } if (res != FR_OK) break; memcpy( dir, &Abuff.DirBuffer[((dj->index % 8) * 32)], 32 ) ; c = dir[DIR_Name]; /* First character */ if (c == 0) { res = FR_NO_FILE; break; } /* Reached to end of table */ if (!(dir[DIR_Attr] & AM_VOL) && !mem_cmp(dir, dj->fn, 11)) /* Is it a valid entry? */ break; res = dir_next(dj); /* Next entry */ } while (res == FR_OK); return res; }
FRESULT pf_readdir ( DIR *dj, /* Pointer to the open directory object */ FILINFO *fno /* Pointer to file information to return */ ) { FRESULT res; BYTE sp[12], dir[32]; FATFS *fs = FatFs; if (!fs) { /* Check file system */ res = FR_NOT_ENABLED; } else { dj->fn = sp; if (!fno) { res = dir_rewind(dj); } else { res = dir_read(dj, dir, &fno->lfname[0]); if (res == FR_NO_FILE) res = FR_OK; if (res == FR_OK) { /* A valid entry is found */ get_fileinfo(dj, dir, fno); /* Get the object information */ res = dir_next2(dj); /* Increment index for next */ if (res == FR_NO_FILE) res = FR_OK; } } } if (fno->lfname[0] == '\0') strcpy(&fno->lfname[0],&fno->fname[0]); //printf("%s %s(%d)\n",&fno->fname[0], &fno->lfname[0],strlen(&fno->lfname[0])); return res; }
static FRESULT follow_path ( /* FR_OK(0): successful, !=0: error code */ DIR *dj, /* Directory object to return last directory and found object */ const char *path /* Full-path string to find a file or directory */ ) { FRESULT res; u8 *dir; u8 t[15]; // memcpypgm2ram(t, path, 15); while (*path == ' ') path++; /* Skip leading spaces */ if (*path == '/') path++; /* Strip heading separator */ dj->sclust = 0; /* Set start directory (always root dir) */ // memcpypgm2ram(t, path, 15); if ((u8)*path <= ' ') { /* Null path means the root directory */ res = dir_rewind(dj); FatFs->buf[0] = 0; } else { /* Follow path */ for (;;) { res = create_name(dj, &path); /* Get a segment */ if (res != FR_OK) break; res = dir_find(dj); if (res != FR_OK) { /* Could not find the object */ if (res == FR_NO_FILE && !*(dj->fn+11)) res = FR_NO_PATH; break; } if (*(dj->fn+11)) break; /* Last segment match. Function completed. */ dir = FatFs->buf; /* There is next segment. Follow the sub directory */ if (!(dir[DIR_Attr] & AM_DIR)) { /* Cannot follow because it is a file */ res = FR_NO_PATH; break; } dj->sclust = #if _FS_FAT32 ((u32)LD_WORD(dir+DIR_FstClusHI) << 16) | #endif LD_WORD(dir+DIR_FstClusLO); } } return res; }
size_t FileSystem::dir_size(fs_dir_t dir) { off_t off = dir_tell(dir); size_t size = 0; struct dirent *ent = new struct dirent; dir_rewind(dir); while (true) { int res = dir_read(dir, ent); if (res <= 0) { break; } size += 1; } dir_seek(dir, off); delete ent; return size; }
FRESULT pf_opendir ( DIR *dj, /* Pointer to directory object to create */ const char *path /* Pointer to the directory path */ ) { FRESULT res; BYTE sp[12], dir[32]; FATFS *fs = FatFs; if (!fs) { /* Check file system */ res = FR_NOT_ENABLED; } else { fs->buf = dir; dj->fn = sp; res = follow_path(dj, path); /* Follow the path to the directory */ if (res == FR_OK) { /* Follow completed */ if (dir[0]) { /* It is not the root dir */ if (dir[DIR_Attr] & AM_DIR) { /* The object is a directory */ dj->sclust = #if _FS_FAT32 ((DWORD)LD_WORD(dir+DIR_FstClusHI) << 16) | #endif LD_WORD(dir+DIR_FstClusLO); } else { /* The object is not a directory */ res = FR_NO_PATH; } } if (res == FR_OK) { res = dir_rewind(dj); /* Rewind dir */ } } if (res == FR_NO_FILE) res = FR_NO_PATH; } return res; }