Improved / fixed DirStruct entry name handling

Fixes #431
This commit is contained in:
d0k3 2018-10-01 23:51:05 +02:00
parent 8f24ccec0c
commit bfc3363012
4 changed files with 17 additions and 18 deletions

View File

@ -2,7 +2,7 @@
void DirEntryCpy(DirEntry* dest, const DirEntry* orig) { void DirEntryCpy(DirEntry* dest, const DirEntry* orig) {
memcpy(dest, orig, sizeof(DirEntry)); memcpy(dest, orig, sizeof(DirEntry));
dest->name = dest->path + (orig->name - orig->path); dest->name = dest->path + dest->p_name;
} }
int compDirEntry(const void* e1, const void* e2) { int compDirEntry(const void* e1, const void* e2) {
@ -20,12 +20,6 @@ void SortDirStruct(DirStruct* contents) {
// fix entry->names after qsort // fix entry->names after qsort
for (int i = 0; i < (int)contents->n_entries; i++) { for (int i = 0; i < (int)contents->n_entries; i++) {
DirEntry* entry = &(contents->entry[i]); DirEntry* entry = &(contents->entry[i]);
if ((entry->type == T_DIR) || (entry->type == T_FILE)) { entry->name = entry->path + entry->p_name;
char* slash = strrchr(entry->path, '/');
entry->name = slash ? slash + 1 : entry->path;
} else {
// somewhat hacky, applies to T_DOTDOT and T_ROOT (see fsdrive.c)
entry->name = entry->path + 4;
}
} }
} }

View File

@ -17,6 +17,7 @@ typedef struct {
u64 size; u64 size;
EntryType type; EntryType type;
u8 marked; u8 marked;
u8 p_name;
} DirEntry; } DirEntry;
typedef struct { typedef struct {

View File

@ -90,15 +90,17 @@ bool GetRootDirContentsWorker(DirStruct* contents) {
for (u32 i = 0; (i < NORM_FS+VIRT_FS) && (n_entries < MAX_DIR_ENTRIES); i++) { for (u32 i = 0; (i < NORM_FS+VIRT_FS) && (n_entries < MAX_DIR_ENTRIES); i++) {
DirEntry* entry = &(contents->entry[n_entries]); DirEntry* entry = &(contents->entry[n_entries]);
if (!DriveType(drvnum[i])) continue; // drive not available if (!DriveType(drvnum[i])) continue; // drive not available
entry->p_name = 4;
entry->name = entry->path + entry->p_name;
memset(entry->path, 0x00, 64); memset(entry->path, 0x00, 64);
snprintf(entry->path + 0, 4, "%s", drvnum[i]); snprintf(entry->path, 4, "%s", drvnum[i]);
if ((*(drvnum[i]) >= '7') && (*(drvnum[i]) <= '9') && !(GetMountState() & IMG_NAND)) // Drive 7...9 handling if ((*(drvnum[i]) >= '7') && (*(drvnum[i]) <= '9') && !(GetMountState() & IMG_NAND)) // Drive 7...9 handling
snprintf(entry->path + 4, 32, "[%s] %s", drvnum[i], snprintf(entry->name, 32, "[%s] %s", drvnum[i],
(*(drvnum[i]) == '7') ? "FAT IMAGE" : (*(drvnum[i]) == '7') ? "FAT IMAGE" :
(*(drvnum[i]) == '8') ? "BONUS DRIVE" : (*(drvnum[i]) == '8') ? "BONUS DRIVE" :
(*(drvnum[i]) == '9') ? "RAMDRIVE" : "UNK"); (*(drvnum[i]) == '9') ? "RAMDRIVE" : "UNK");
else if (*(drvnum[i]) == 'G') // Game drive special handling else if (*(drvnum[i]) == 'G') // Game drive special handling
snprintf(entry->path + 4, 32, "[%s] %s %s", drvnum[i], snprintf(entry->name, 32, "[%s] %s %s", drvnum[i],
(GetMountState() & GAME_CIA ) ? "CIA" : (GetMountState() & GAME_CIA ) ? "CIA" :
(GetMountState() & GAME_NCSD ) ? "NCSD" : (GetMountState() & GAME_NCSD ) ? "NCSD" :
(GetMountState() & GAME_NCCH ) ? "NCCH" : (GetMountState() & GAME_NCCH ) ? "NCCH" :
@ -108,9 +110,8 @@ bool GetRootDirContentsWorker(DirStruct* contents) {
(GetMountState() & SYS_FIRM ) ? "FIRM" : (GetMountState() & SYS_FIRM ) ? "FIRM" :
(GetMountState() & GAME_TAD ) ? "DSIWARE" : "UNK", drvname[i]); (GetMountState() & GAME_TAD ) ? "DSIWARE" : "UNK", drvname[i]);
else if (*(drvnum[i]) == '0') // SD card handling else if (*(drvnum[i]) == '0') // SD card handling
snprintf(entry->path + 4, 32, "[%s] %s (%s)", drvnum[i], drvname[i], sdlabel); snprintf(entry->name, 32, "[%s] %s (%s)", drvnum[i], drvname[i], sdlabel);
else snprintf(entry->path + 4, 32, "[%s] %s", drvnum[i], drvname[i]); else snprintf(entry->name, 32, "[%s] %s", drvnum[i], drvname[i]);
entry->name = entry->path + 4;
entry->size = GetTotalSpace(entry->path); entry->size = GetTotalSpace(entry->path);
entry->type = T_ROOT; entry->type = T_ROOT;
entry->marked = 0; entry->marked = 0;
@ -146,7 +147,8 @@ bool GetDirContentsWorker(DirStruct* contents, char* fpath, int fnsize, const ch
DirEntry* entry = &(contents->entry[contents->n_entries]); DirEntry* entry = &(contents->entry[contents->n_entries]);
strncpy(entry->path, fpath, 256); strncpy(entry->path, fpath, 256);
entry->path[255] = '\0'; entry->path[255] = '\0';
entry->name = entry->path + (fname - fpath); entry->p_name = fname - fpath;
entry->name = entry->path + entry->p_name;
if (fno.fattrib & AM_DIR) { if (fno.fattrib & AM_DIR) {
entry->type = T_DIR; entry->type = T_DIR;
entry->size = 0; entry->size = 0;
@ -179,7 +181,8 @@ void SearchDirContents(DirStruct* contents, const char* path, const char* patter
contents->n_entries = 0; // not required, but so what? contents->n_entries = 0; // not required, but so what?
} else { } else {
// create virtual '..' entry // create virtual '..' entry
contents->entry->name = contents->entry->path + 4; contents->entry->p_name = 4;
contents->entry->name = contents->entry->path + entry->p_name;
strncpy(contents->entry->path, "*?*", 4); strncpy(contents->entry->path, "*?*", 4);
strncpy(contents->entry->name, "..", 4); strncpy(contents->entry->name, "..", 4);
contents->entry->type = T_DOTDOT; contents->entry->type = T_DOTDOT;

View File

@ -14,8 +14,9 @@ void SetDirGoodNames(DirStruct* contents) {
if ((GetGoodName(goodname, entry->path, false) != 0) || if ((GetGoodName(goodname, entry->path, false) != 0) ||
(plen + 1 + strnlen(goodname, 256) + 1 > 256)) (plen + 1 + strnlen(goodname, 256) + 1 > 256))
continue; continue;
strncpy(entry->path + plen + 1, goodname, 256 - 1 - plen - 1); entry->p_name = plen + 1;
entry->name = entry->path + plen + 1; entry->name = entry->path + entry->p_name;
strncpy(entry->name, goodname, 256 - 1 - entry->p_name);
} }
} }