Skip to content

Commit

Permalink
- Discard directory entries exceeding PATHNAME_MAX.
Browse files Browse the repository at this point in the history
  • Loading branch information
Extrems committed Feb 16, 2024
1 parent 38e42b5 commit 571d834
Show file tree
Hide file tree
Showing 11 changed files with 68 additions and 54 deletions.
4 changes: 2 additions & 2 deletions cube/swiss/include/files.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ file_handle** getSortedDirEntries();
file_handle* getCurrentDirEntries();
#define getSortedDirEntryCount getCurrentDirEntryCount
int getCurrentDirEntryCount();
void concat_path(char *pathName, const char *dirName, const char *baseName);
void concatf_path(char *pathName, const char *dirName, const char *baseName, ...);
size_t concat_path(char *pathName, const char *dirName, const char *baseName);
size_t concatf_path(char *pathName, const char *dirName, const char *baseName, ...);
void ensure_path(int deviceSlot, char *path, char *oldPath);

#endif
12 changes: 6 additions & 6 deletions cube/swiss/source/devices/fat/deviceHandler-FAT.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,17 +134,17 @@ s32 deviceHandler_FAT_readDir(file_handle* ffile, file_handle** dir, u32 type) {
*dir = reallocarray(*dir, num_entries, sizeof(file_handle));
}
memset(&(*dir)[i], 0, sizeof(file_handle));
concat_path((*dir)[i].name, ffile->name, entry.fname);
(*dir)[i].fileBase = i;
(*dir)[i].size = entry.fsize;
(*dir)[i].fileAttrib = (entry.fattrib & AM_DIR) ? IS_DIR : IS_FILE;
++i;
if(concat_path((*dir)[i].name, ffile->name, entry.fname) < PATHNAME_MAX) {
(*dir)[i].size = entry.fsize;
(*dir)[i].fileAttrib = (entry.fattrib & AM_DIR) ? IS_DIR : IS_FILE;
++i;
}
}
}

f_closedir(dp);
free(dp);
return num_entries;
return i;
}

s64 deviceHandler_FAT_seekFile(file_handle* file, s64 where, u32 type){
Expand Down
14 changes: 7 additions & 7 deletions cube/swiss/source/devices/fsp/deviceHandler-FSP.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,17 @@ s32 deviceHandler_FSP_readDir(file_handle* ffile, file_handle** dir, u32 type) {
*dir = reallocarray(*dir, num_entries, sizeof(file_handle));
}
memset(&(*dir)[i], 0, sizeof(file_handle));
concat_path((*dir)[i].name, ffile->name, entry.name);
(*dir)[i].fileBase = i;
(*dir)[i].size = entry.size;
(*dir)[i].fileAttrib = (entry.type == FSP_RDTYPE_DIR) ? IS_DIR : IS_FILE;
usedSpace += (*dir)[i].size;
++i;
if(concat_path((*dir)[i].name, ffile->name, entry.name) < PATHNAME_MAX) {
(*dir)[i].size = entry.size;
(*dir)[i].fileAttrib = (entry.type == FSP_RDTYPE_DIR) ? IS_DIR : IS_FILE;
usedSpace += (*dir)[i].size;
++i;
}
}
}
initial_FSP_info.totalSpace = usedSpace;
fsp_closedir(dp);
return num_entries;
return i;
}

s64 deviceHandler_FSP_seekFile(file_handle* file, s64 where, u32 type) {
Expand Down
14 changes: 7 additions & 7 deletions cube/swiss/source/devices/ftp/deviceHandler-FTP.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,17 +119,17 @@ s32 deviceHandler_FTP_readDir(file_handle* ffile, file_handle** dir, u32 type){
*dir = reallocarray(*dir, num_entries, sizeof(file_handle));
}
memset(&(*dir)[i], 0, sizeof(file_handle));
concat_path((*dir)[i].name, ffile->name, entry->d_name);
stat((*dir)[i].name, &fstat);
(*dir)[i].fileBase = i;
(*dir)[i].size = fstat.st_size;
(*dir)[i].fileAttrib = S_ISDIR(fstat.st_mode) ? IS_DIR : IS_FILE;
++i;
if(concat_path((*dir)[i].name, ffile->name, entry->d_name) < PATHNAME_MAX) {
stat((*dir)[i].name, &fstat);
(*dir)[i].size = fstat.st_size;
(*dir)[i].fileAttrib = S_ISDIR(fstat.st_mode) ? IS_DIR : IS_FILE;
++i;
}
}
} while(entry || errno == EOVERFLOW);

closedir(dp);
return num_entries;
return i;
}

s64 deviceHandler_FTP_seekFile(file_handle* file, s64 where, u32 type){
Expand Down
4 changes: 2 additions & 2 deletions cube/swiss/source/devices/memcard/deviceHandler-CARD.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ s32 deviceHandler_CARD_readDir(file_handle* ffile, file_handle** dir, u32 type){
}
memset(&(*dir)[i], 0, sizeof(file_handle));
concatf_path((*dir)[i].name, ffile->name, "%.*s", CARD_FILENAMELEN, memcard_dir->filename);
(*dir)[i].fileBase = i;
(*dir)[i].fileBase = memcard_dir->fileno;
(*dir)[i].size = memcard_dir->filelen;
(*dir)[i].fileAttrib = IS_FILE;
memcpy( (*dir)[i].other, memcard_dir, sizeof(card_dir));
Expand All @@ -142,7 +142,7 @@ s32 deviceHandler_CARD_readDir(file_handle* ffile, file_handle** dir, u32 type){

initial_CARD_info[slot].freeSpace = initial_CARD_info[slot].totalSpace - usedSpace;

return num_entries;
return i;
}

// Finds a file based on file->name and populates file->size if found.
Expand Down
2 changes: 1 addition & 1 deletion cube/swiss/source/devices/qoob/deviceHandler-Qoob.c
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ s32 deviceHandler_Qoob_readDir(file_handle* ffile, file_handle** dir, u32 type)
}
initial_Qoob_info.freeSpace = initial_Qoob_info.totalSpace - usedSpace;
DrawDispose(msgBox);
return num_entries;
return i;
}

s64 deviceHandler_Qoob_seekFile(file_handle* file, s64 where, u32 type) {
Expand Down
14 changes: 7 additions & 7 deletions cube/swiss/source/devices/smb/deviceHandler-SMB.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,17 +123,17 @@ s32 deviceHandler_SMB_readDir(file_handle* ffile, file_handle** dir, u32 type){
*dir = reallocarray(*dir, num_entries, sizeof(file_handle));
}
memset(&(*dir)[i], 0, sizeof(file_handle));
concat_path((*dir)[i].name, ffile->name, entry->d_name);
stat((*dir)[i].name, &fstat);
(*dir)[i].fileBase = i;
(*dir)[i].size = fstat.st_size;
(*dir)[i].fileAttrib = S_ISDIR(fstat.st_mode) ? IS_DIR : IS_FILE;
++i;
if(concat_path((*dir)[i].name, ffile->name, entry->d_name) < PATHNAME_MAX) {
stat((*dir)[i].name, &fstat);
(*dir)[i].size = fstat.st_size;
(*dir)[i].fileAttrib = S_ISDIR(fstat.st_mode) ? IS_DIR : IS_FILE;
++i;
}
}
} while(entry || errno == EOVERFLOW);

closedir(dp);
return num_entries;
return i;
}

s64 deviceHandler_SMB_seekFile(file_handle* file, s64 where, u32 type){
Expand Down
17 changes: 7 additions & 10 deletions cube/swiss/source/devices/system/deviceHandler-SYS.c
Original file line number Diff line number Diff line change
Expand Up @@ -400,19 +400,16 @@ s32 deviceHandler_SYS_init(file_handle* file) {
}

s32 deviceHandler_SYS_readDir(file_handle* ffile, file_handle** dir, u32 type) {
int num_entries = 1, i;
int num_entries = NUM_ROMS, i = ROM_VOID;
*dir = calloc(num_entries, sizeof(file_handle));
concat_path((*dir)[0].name, ffile->name, "..");
(*dir)[0].fileAttrib = IS_SPECIAL;
concat_path((*dir)[i].name, ffile->name, "..");
(*dir)[i].fileAttrib = IS_SPECIAL;

for(i = ROM_IPL; i < NUM_ROMS; i++) {
*dir = reallocarray(*dir, num_entries + 1, sizeof(file_handle));
memset(&(*dir)[num_entries], 0, sizeof(file_handle));
concat_path((*dir)[num_entries].name, ffile->name, rom_names[i]);
(*dir)[num_entries].fileBase = i;
(*dir)[num_entries].size = rom_sizes[i];
(*dir)[num_entries].fileAttrib = IS_FILE;
num_entries++;
concat_path((*dir)[i].name, ffile->name, rom_names[i]);
(*dir)[i].fileBase = i;
(*dir)[i].size = rom_sizes[i];
(*dir)[i].fileAttrib = IS_FILE;
}

return num_entries;
Expand Down
14 changes: 7 additions & 7 deletions cube/swiss/source/devices/usbgecko/deviceHandler-usbgecko.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,16 @@ s32 deviceHandler_USBGecko_readDir(file_handle* ffile, file_handle** dir, u32 ty
*dir = reallocarray(*dir, num_entries, sizeof(file_handle));
}
memset(&(*dir)[i], 0, sizeof(file_handle));
strcpy((*dir)[i].name, entry->name);
(*dir)[i].fileBase = i;
(*dir)[i].size = entry->size;
(*dir)[i].fileAttrib = entry->fileAttrib;
usedSpace += (*dir)[i].size;
++i;
if(strlcpy((*dir)[i].name, entry->name, PATHNAME_MAX) < PATHNAME_MAX) {
(*dir)[i].size = entry->size;
(*dir)[i].fileAttrib = entry->fileAttrib;
usedSpace += (*dir)[i].size;
++i;
}
}
initial_USBGecko_info.totalSpace = usedSpace;
DrawDispose(msgBox);
return num_entries;
return i;
}

s64 deviceHandler_USBGecko_seekFile(file_handle* file, s64 where, u32 type){
Expand Down
1 change: 0 additions & 1 deletion cube/swiss/source/devices/wode/deviceHandler-WODE.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ s32 deviceHandler_WODE_readDir(file_handle* ffile, file_handle** dir, u32 type){
*dir = reallocarray(*dir, num_entries + 1, sizeof(file_handle));
memset(&(*dir)[num_entries], 0, sizeof(file_handle));
concatf_path((*dir)[num_entries].name, ffile->name, "%.64s.gcm", &tmp.name[0]);
(*dir)[num_entries].fileBase = num_entries;
(*dir)[num_entries].size = DISC_SIZE;
(*dir)[num_entries].fileAttrib = IS_FILE;
memcpy(&(*dir)[num_entries].other, &tmp, sizeof(ISOInfo_t));
Expand Down
26 changes: 22 additions & 4 deletions cube/swiss/source/files.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ int getCurrentDirEntryCount() {
return curDirEntryCount;
}

void concat_path(char *pathName, const char *dirName, const char *baseName)
size_t concat_path(char *pathName, const char *dirName, const char *baseName)
{
size_t len;

Expand All @@ -113,18 +113,24 @@ void concat_path(char *pathName, const char *dirName, const char *baseName)
else
len = strlcpy(pathName, dirName, PATHNAME_MAX);

if (len >= PATHNAME_MAX)
return len;

if (len) {
if (pathName[len - 1] != '/' && baseName[0] != '/') {
if (len + 1 >= PATHNAME_MAX)
return len + 1 + strlen(baseName);

pathName[len++] = '/';
pathName[len] = '\0';
} else if (pathName[len - 1] == '/' && baseName[0] == '/')
baseName++;
}

strlcat(pathName, baseName, PATHNAME_MAX);
return strlcat(pathName, baseName, PATHNAME_MAX);
}

void concatf_path(char *pathName, const char *dirName, const char *baseName, ...)
size_t concatf_path(char *pathName, const char *dirName, const char *baseName, ...)
{
size_t len;

Expand All @@ -133,8 +139,19 @@ void concatf_path(char *pathName, const char *dirName, const char *baseName, ...
else
len = strlcpy(pathName, dirName, PATHNAME_MAX);

if (len >= PATHNAME_MAX)
return len;

if (len) {
if (pathName[len - 1] != '/' && baseName[0] != '/') {
if (len + 1 >= PATHNAME_MAX) {
va_list args;
va_start(args, baseName);
len += vsnprintf(NULL, 0, baseName, args);
va_end(args);
return len + 1;
}

pathName[len++] = '/';
pathName[len] = '\0';
} else if (pathName[len - 1] == '/' && baseName[0] == '/')
Expand All @@ -143,8 +160,9 @@ void concatf_path(char *pathName, const char *dirName, const char *baseName, ...

va_list args;
va_start(args, baseName);
vsnprintf(pathName + len, PATHNAME_MAX - len, baseName, args);
len += vsnprintf(pathName + len, PATHNAME_MAX - len, baseName, args);
va_end(args);
return len;
}

// Either renames a path to a new one, or creates one.
Expand Down

0 comments on commit 571d834

Please sign in to comment.