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()
{
// 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)
{
char * p = strrchr(browserList[browser.selIndex].filename, '.');
@ -467,7 +459,7 @@ int WiiFileLoader()
if(!MakeFilePath(filepath, FILE_ROM))
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
{
@ -501,8 +493,6 @@ int BrowserLoadFile()
if(!FindDevice(browser.dir, &device))
return 0;
GetFileSize(browser.selIndex);
// check that this is a valid ROM
if(!IsValidROM())
goto done;

View File

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

View File

@ -14,11 +14,8 @@
#ifndef _FILEOP_H_
#define _FILEOP_H_
#include <gccore.h>
#include <stdio.h>
#include <string.h>
#include <ogcsys.h>
#include <fat.h>
#include <unistd.h>
#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(char * filepath, bool silent);
void CreateAppPath(char * origpath);
bool GetFileSize(int i);
void FindAndSelectLastLoadedFile();
int ParseDirectory(bool waitParse = false, bool filter = true);
bool CreateDirectory(char * path);
void AllocSaveBuffer();
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 LoadSzFile(char * filepath, unsigned char * rbuffer);
size_t SaveFile(char * buffer, char *filepath, size_t datasize, bool silent);

View File

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

View File

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