avoid using file stat when possible. it's slow.

This commit is contained in:
Daryl Borth 2018-08-28 14:47:59 -06:00
parent 61459262ca
commit e63c43e225
5 changed files with 29 additions and 67 deletions

View File

@ -346,14 +346,6 @@ int FileSortCallback(const void *f1, const void *f2)
***************************************************************************/ ***************************************************************************/
static bool IsValidROM() static bool IsValidROM()
{ {
// file size should be between 32K and 8MB
if(browserList[browser.selIndex].length < (1024*32) ||
browserList[browser.selIndex].length > Memory.MAX_ROM_SIZE)
{
ErrorPrompt("Invalid file size!");
return false;
}
if (strlen(browserList[browser.selIndex].filename) > 4) if (strlen(browserList[browser.selIndex].filename) > 4)
{ {
char * p = strrchr(browserList[browser.selIndex].filename, '.'); char * p = strrchr(browserList[browser.selIndex].filename, '.');
@ -467,7 +459,7 @@ int WiiFileLoader()
if(!MakeFilePath(filepath, FILE_ROM)) if(!MakeFilePath(filepath, FILE_ROM))
return 0; return 0;
size = LoadFile ((char *)Memory.ROM, filepath, browserList[browser.selIndex].length, NOTSILENT); size = LoadFile ((char *)Memory.ROM, filepath, 0, Memory.MAX_ROM_SIZE, NOTSILENT);
} }
else else
{ {
@ -501,8 +493,6 @@ int BrowserLoadFile()
if(!FindDevice(browser.dir, &device)) if(!FindDevice(browser.dir, &device))
return 0; return 0;
GetFileSize(browser.selIndex);
// check that this is a valid ROM // check that this is a valid ROM
if(!IsValidROM()) if(!IsValidROM())
goto done; goto done;

View File

@ -20,6 +20,7 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <zlib.h> #include <zlib.h>
#include <malloc.h> #include <malloc.h>
#include <fat.h>
#include <sdcard/wiisd_io.h> #include <sdcard/wiisd_io.h>
#include <sdcard/gcsd.h> #include <sdcard/gcsd.h>
#include <ogc/usbstorage.h> #include <ogc/usbstorage.h>
@ -475,22 +476,6 @@ static char *GetExt(char *file)
return ext; return ext;
} }
bool GetFileSize(int i)
{
if(browserList[i].length > 0)
return true;
struct stat filestat;
char path[MAXPATHLEN+1];
snprintf(path, MAXPATHLEN, "%s%s", browser.dir, browserList[i].filename);
if(stat(path, &filestat) < 0)
return false;
browserList[i].length = filestat.st_size;
return true;
}
void FindAndSelectLastLoadedFile () void FindAndSelectLastLoadedFile ()
{ {
int indexFound = -1; int indexFound = -1;
@ -642,10 +627,8 @@ ParseDirectory(bool waitParse, bool filter)
return -1; return -1;
if(dir == NULL) if(dir == NULL)
{
retry = ErrorPromptRetry("Error opening directory!"); retry = ErrorPromptRetry("Error opening directory!");
} }
}
// if we can't open the dir, try higher levels // if we can't open the dir, try higher levels
if (dir == NULL) if (dir == NULL)
@ -675,7 +658,6 @@ ParseDirectory(bool waitParse, bool filter)
AddBrowserEntry(); AddBrowserEntry();
sprintf(browserList[0].filename, ".."); sprintf(browserList[0].filename, "..");
sprintf(browserList[0].displayname, "Up One Level"); sprintf(browserList[0].displayname, "Up One Level");
browserList[0].length = 0;
browserList[0].isdir = 1; // flag this as a dir browserList[0].isdir = 1; // flag this as a dir
browserList[0].icon = ICON_FOLDER; browserList[0].icon = ICON_FOLDER;
browser.numEntries++; browser.numEntries++;
@ -777,7 +759,7 @@ LoadSzFile(char * filepath, unsigned char * rbuffer)
* LoadFile * LoadFile
***************************************************************************/ ***************************************************************************/
size_t size_t
LoadFile (char * rbuffer, char *filepath, size_t length, bool silent) LoadFile (char * rbuffer, char *filepath, size_t length, size_t buffersize, bool silent)
{ {
char zipbuffer[2048]; char zipbuffer[2048];
size_t size = 0, offset = 0, readsize = 0; size_t size = 0, offset = 0, readsize = 0;
@ -829,7 +811,7 @@ LoadFile (char * rbuffer, char *filepath, size_t length, bool silent)
if (IsZipFile (zipbuffer)) if (IsZipFile (zipbuffer))
{ {
size = UnZipBuffer ((unsigned char *)rbuffer); // unzip size = UnZipBuffer ((unsigned char *)rbuffer, buffersize); // unzip
} }
else else
{ {
@ -837,6 +819,10 @@ LoadFile (char * rbuffer, char *filepath, size_t length, bool silent)
size = ftello(file); size = ftello(file);
fseeko(file,0,SEEK_SET); fseeko(file,0,SEEK_SET);
if(size > buffersize) {
size = 0;
}
else {
while(!feof(file)) while(!feof(file))
{ {
ShowProgress ("Loading...", offset, size); ShowProgress ("Loading...", offset, size);
@ -851,6 +837,7 @@ LoadFile (char * rbuffer, char *filepath, size_t length, bool silent)
CancelAction(); CancelAction();
} }
} }
}
retry = 0; retry = 0;
fclose (file); fclose (file);
} }
@ -863,19 +850,7 @@ LoadFile (char * rbuffer, char *filepath, size_t length, bool silent)
size_t LoadFile(char * filepath, bool silent) size_t LoadFile(char * filepath, bool silent)
{ {
struct stat filestat; return LoadFile((char *)savebuffer, filepath, 0, SAVEBUFFERSIZE, silent);
if(stat(filepath, &filestat) != 0) {
return 0;
}
int size = filestat.st_size;
if(size >= SAVEBUFFERSIZE) {
return 0;
}
return LoadFile((char *)savebuffer, filepath, 0, silent);
} }
/**************************************************************************** /****************************************************************************
@ -944,7 +919,6 @@ SaveFile (char * buffer, char *filepath, size_t datasize, bool silent)
// go back to checking if devices were inserted/removed // go back to checking if devices were inserted/removed
ResumeDeviceThread(); ResumeDeviceThread();
if(!silent) if(!silent)
CancelAction(); CancelAction();
return written; return written;

View File

@ -14,11 +14,8 @@
#ifndef _FILEOP_H_ #ifndef _FILEOP_H_
#define _FILEOP_H_ #define _FILEOP_H_
#include <gccore.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <ogcsys.h>
#include <fat.h>
#include <unistd.h> #include <unistd.h>
#define SAVEBUFFERSIZE (1024 * 1024 * 2) // leave room for IPS/UPS files and large images #define SAVEBUFFERSIZE (1024 * 1024 * 2) // leave room for IPS/UPS files and large images
@ -34,13 +31,12 @@ char * StripDevice(char * path);
bool ChangeInterface(int device, bool silent); bool ChangeInterface(int device, bool silent);
bool ChangeInterface(char * filepath, bool silent); bool ChangeInterface(char * filepath, bool silent);
void CreateAppPath(char * origpath); void CreateAppPath(char * origpath);
bool GetFileSize(int i);
void FindAndSelectLastLoadedFile(); void FindAndSelectLastLoadedFile();
int ParseDirectory(bool waitParse = false, bool filter = true); int ParseDirectory(bool waitParse = false, bool filter = true);
bool CreateDirectory(char * path); bool CreateDirectory(char * path);
void AllocSaveBuffer(); void AllocSaveBuffer();
void FreeSaveBuffer(); void FreeSaveBuffer();
size_t LoadFile(char * rbuffer, char *filepath, size_t length, bool silent); size_t LoadFile(char * rbuffer, char *filepath, size_t length, size_t buffersize, bool silent);
size_t LoadFile(char * filepath, bool silent); size_t LoadFile(char * filepath, bool silent);
size_t LoadSzFile(char * filepath, unsigned char * rbuffer); size_t LoadSzFile(char * filepath, unsigned char * rbuffer);
size_t SaveFile(char * buffer, char *filepath, size_t datasize, bool silent); size_t SaveFile(char * buffer, char *filepath, size_t datasize, bool silent);

View File

@ -99,7 +99,7 @@ IsZipFile (char *buffer)
******************************************************************************/ ******************************************************************************/
size_t size_t
UnZipBuffer (unsigned char *outbuffer) UnZipBuffer (unsigned char *outbuffer, size_t buffersize)
{ {
PKZIPHEADER pkzip; PKZIPHEADER pkzip;
size_t zipoffset = 0; size_t zipoffset = 0;
@ -124,6 +124,10 @@ UnZipBuffer (unsigned char *outbuffer)
pkzip.uncompressedSize = FLIP32 (pkzip.uncompressedSize); pkzip.uncompressedSize = FLIP32 (pkzip.uncompressedSize);
if(pkzip.uncompressedSize > buffersize) {
return 0;
}
ShowProgress ("Loading...", 0, pkzip.uncompressedSize); ShowProgress ("Loading...", 0, pkzip.uncompressedSize);
/*** Prepare the zip stream ***/ /*** Prepare the zip stream ***/
@ -213,7 +217,7 @@ GetFirstZipFilename ()
return NULL; return NULL;
// read start of ZIP // read start of ZIP
if(LoadFile (tempbuffer, filepath, ZIPCHUNK, NOTSILENT) < 35) if(LoadFile (tempbuffer, filepath, ZIPCHUNK, ZIPCHUNK, NOTSILENT) < 35)
return NULL; return NULL;
tempbuffer[28] = 0; // truncate - filename length is 2 bytes long (bytes 26-27) tempbuffer[28] = 0; // truncate - filename length is 2 bytes long (bytes 26-27)
@ -356,7 +360,6 @@ void SzClose()
SzArDbExFree(&SzDb, SzAllocImp.Free); SzArDbExFree(&SzDb, SzAllocImp.Free);
} }
/**************************************************************************** /****************************************************************************
* SzParse * SzParse
* *
@ -431,7 +434,6 @@ int SzParse(char * filepath)
sprintf(browserList[0].filename, ".."); sprintf(browserList[0].filename, "..");
sprintf(browserList[0].displayname, "Up One Level"); sprintf(browserList[0].displayname, "Up One Level");
browserList[0].isdir = 1; browserList[0].isdir = 1;
browserList[0].length = filelen;
browserList[0].icon = ICON_FOLDER; browserList[0].icon = ICON_FOLDER;
// get contents and parse them into file list structure // get contents and parse them into file list structure

View File

@ -14,7 +14,7 @@
int IsZipFile (char *buffer); int IsZipFile (char *buffer);
char * GetFirstZipFilename(); char * GetFirstZipFilename();
size_t UnZipBuffer (unsigned char *outbuffer); size_t UnZipBuffer (unsigned char *outbuffer, size_t buffersize);
int SzParse(char * filepath); int SzParse(char * filepath);
size_t SzExtractFile(int i, unsigned char *buffer); size_t SzExtractFile(int i, unsigned char *buffer);
void SzClose(); void SzClose();