fceugx/source/ngc/fileop.cpp

818 lines
18 KiB
C++
Raw Normal View History

2008-09-02 03:57:21 +02:00
/****************************************************************************
2009-07-22 04:05:49 +02:00
* FCE Ultra
2008-09-02 03:57:21 +02:00
* Nintendo Wii/Gamecube Port
*
2009-03-28 18:23:08 +01:00
* Tantric 2008-2009
2008-09-02 03:57:21 +02:00
*
2009-03-28 18:23:08 +01:00
* fileop.cpp
2008-09-02 03:57:21 +02:00
*
* File operations
****************************************************************************/
#include <gccore.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
2008-09-02 03:57:21 +02:00
#include <ogcsys.h>
#include <sys/dir.h>
#include <sys/stat.h>
2008-09-02 03:57:21 +02:00
#include <zlib.h>
#include <sdcard/wiisd_io.h>
#include <sdcard/gcsd.h>
#include <ogc/usbstorage.h>
2008-09-02 03:57:21 +02:00
#include "dvd.h"
2009-07-17 19:27:04 +02:00
#include "fceusupport.h"
#include "fceugx.h"
2008-09-02 03:57:21 +02:00
#include "fileop.h"
#include "memcardop.h"
2008-12-24 08:58:23 +01:00
#include "networkop.h"
2008-09-02 03:57:21 +02:00
#include "gcunzip.h"
2009-03-28 18:23:08 +01:00
#include "menu.h"
#include "filebrowser.h"
2008-09-02 03:57:21 +02:00
#define THREAD_SLEEP 100
unsigned char savebuffer[SAVEBUFFERSIZE] ATTRIBUTE_ALIGN(32);
static mutex_t bufferLock = LWP_MUTEX_NULL;
FILE * file; // file pointer - the only one we should ever use!
bool unmountRequired[9] = { false, false, false, false, false, false, false, false, false };
bool isMounted[9] = { false, false, false, false, false, false, false, false, false };
#ifdef HW_RVL
const DISC_INTERFACE* sd = &__io_wiisd;
const DISC_INTERFACE* usb = &__io_usbstorage;
#else
const DISC_INTERFACE* carda = &__io_gcsda;
const DISC_INTERFACE* cardb = &__io_gcsdb;
#endif
// folder parsing thread
static lwp_t parsethread = LWP_THREAD_NULL;
static DIR_ITER * dirIter = NULL;
static bool parseHalt = true;
bool ParseDirEntries();
// device thread
2009-05-06 19:27:20 +02:00
static lwp_t devicethread = LWP_THREAD_NULL;
static bool deviceHalt = true;
/****************************************************************************
* ResumeDeviceThread
*
* Signals the device thread to start, and resumes the thread.
***************************************************************************/
void
ResumeDeviceThread()
{
deviceHalt = false;
LWP_ResumeThread(devicethread);
}
/****************************************************************************
* HaltGui
*
* Signals the device thread to stop.
***************************************************************************/
void
HaltDeviceThread()
{
deviceHalt = true;
#ifdef HW_RVL
if(inNetworkInit) // don't wait for network to initialize
return;
#endif
2009-05-06 19:27:20 +02:00
// wait for thread to finish
while(!LWP_ThreadIsSuspended(devicethread))
usleep(THREAD_SLEEP);
2009-05-06 19:27:20 +02:00
}
2009-10-02 00:21:25 +02:00
/****************************************************************************
* HaltParseThread
*
* Signals the parse thread to stop.
***************************************************************************/
void
HaltParseThread()
{
parseHalt = true;
while(!LWP_ThreadIsSuspended(parsethread))
usleep(THREAD_SLEEP);
}
/****************************************************************************
* devicecallback
*
* This checks our devices for changes (SD/USB removed) and
* initializes the network in the background
***************************************************************************/
static int devsleep = 1*1000*1000;
static void *
devicecallback (void *arg)
{
while(devsleep > 0)
{
if(deviceHalt)
LWP_SuspendThread(devicethread);
usleep(THREAD_SLEEP);
devsleep -= THREAD_SLEEP;
}
while (1)
{
#ifdef HW_RVL
2009-10-02 00:21:25 +02:00
if(isMounted[DEVICE_SD])
{
if(!sd->isInserted()) // check if the device was removed
{
2009-10-02 00:21:25 +02:00
unmountRequired[DEVICE_SD] = true;
isMounted[DEVICE_SD] = false;
}
}
2009-10-02 00:21:25 +02:00
if(isMounted[DEVICE_USB])
{
if(!usb->isInserted()) // check if the device was removed
{
2009-10-02 00:21:25 +02:00
unmountRequired[DEVICE_USB] = true;
isMounted[DEVICE_USB] = false;
}
}
2009-04-01 07:48:01 +02:00
UpdateCheck();
InitializeNetwork(SILENT);
#else
2009-10-02 00:21:25 +02:00
if(isMounted[DEVICE_SD_SLOTA])
{
if(!carda->isInserted()) // check if the device was removed
{
2009-10-02 00:21:25 +02:00
unmountRequired[DEVICE_SD_SLOTA] = true;
isMounted[DEVICE_SD_SLOTA] = false;
}
}
2009-10-02 00:21:25 +02:00
if(isMounted[DEVICE_SD_SLOTB])
{
if(!cardb->isInserted()) // check if the device was removed
{
2009-10-02 00:21:25 +02:00
unmountRequired[DEVICE_SD_SLOTB] = true;
isMounted[DEVICE_SD_SLOTB] = false;
}
}
#endif
devsleep = 1000*1000; // 1 sec
while(devsleep > 0)
{
if(deviceHalt)
LWP_SuspendThread(devicethread);
usleep(THREAD_SLEEP);
devsleep -= THREAD_SLEEP;
}
}
return NULL;
}
2008-09-02 03:57:21 +02:00
static void *
parsecallback (void *arg)
{
while(1)
{
while(ParseDirEntries())
usleep(THREAD_SLEEP);
LWP_SuspendThread(parsethread);
}
return NULL;
}
2008-10-28 07:50:07 +01:00
/****************************************************************************
* InitDeviceThread
*
* libOGC provides a nice wrapper for LWP access.
* This function sets up a new local queue and attaches the thread to it.
2008-10-28 07:50:07 +01:00
***************************************************************************/
void
InitDeviceThread()
2008-10-28 07:50:07 +01:00
{
2009-03-28 18:23:08 +01:00
LWP_CreateThread (&devicethread, devicecallback, NULL, NULL, 0, 40);
LWP_CreateThread (&parsethread, parsecallback, NULL, NULL, 0, 80);
2008-10-28 07:50:07 +01:00
}
2008-09-02 03:57:21 +02:00
2008-10-28 07:50:07 +01:00
/****************************************************************************
* UnmountAllFAT
* Unmounts all FAT devices
***************************************************************************/
void UnmountAllFAT()
{
#ifdef HW_RVL
2009-06-12 09:48:27 +02:00
fatUnmount("sd:");
fatUnmount("usb:");
#else
2009-06-12 09:48:27 +02:00
fatUnmount("carda:");
fatUnmount("cardb:");
2008-10-28 07:50:07 +01:00
#endif
2008-09-02 03:57:21 +02:00
}
2008-11-12 09:40:09 +01:00
/****************************************************************************
* MountFAT
* Checks if the device needs to be (re)mounted
* If so, unmounts the device
* Attempts to mount the device specified
* Sets libfat to use the device by default
***************************************************************************/
2009-10-02 00:21:25 +02:00
static bool MountFAT(int device, int silent)
2008-11-12 09:40:09 +01:00
{
bool mounted = true; // assume our disc is already mounted
2009-10-02 00:21:25 +02:00
char name[10], name2[10];
const DISC_INTERFACE* disc = NULL;
2008-11-12 09:40:09 +01:00
2009-10-02 00:21:25 +02:00
switch(device)
{
#ifdef HW_RVL
2009-10-02 00:21:25 +02:00
case DEVICE_SD:
sprintf(name, "sd");
2009-10-02 00:21:25 +02:00
sprintf(name2, "sd:");
disc = sd;
break;
2009-10-02 00:21:25 +02:00
case DEVICE_USB:
sprintf(name, "usb");
2009-10-02 00:21:25 +02:00
sprintf(name2, "usb:");
disc = usb;
break;
#else
2009-10-02 00:21:25 +02:00
case DEVICE_SD_SLOTA:
sprintf(name, "carda");
2009-10-02 00:21:25 +02:00
sprintf(name2, "carda:");
disc = carda;
break;
2009-10-02 00:21:25 +02:00
case DEVICE_SD_SLOTB:
sprintf(name, "cardb");
2009-10-02 00:21:25 +02:00
sprintf(name2, "cardb:");
disc = cardb;
break;
#endif
default:
return false; // unknown device
}
2008-11-12 09:40:09 +01:00
2009-10-02 00:21:25 +02:00
if(unmountRequired[device])
2008-11-12 09:40:09 +01:00
{
2009-10-02 00:21:25 +02:00
unmountRequired[device] = false;
fatUnmount(name2);
disc->shutdown();
2009-10-02 00:21:25 +02:00
isMounted[device] = false;
2008-11-12 09:40:09 +01:00
}
2009-10-02 00:21:25 +02:00
if(!isMounted[device])
{
if(!disc->startup())
mounted = false;
else if(!fatMountSimple(name, disc))
mounted = false;
}
2009-10-02 00:21:25 +02:00
if(!mounted && !silent)
{
if(device == DEVICE_SD)
ErrorPrompt("SD card not found!");
else
ErrorPrompt("USB drive not found!");
}
2009-10-02 00:21:25 +02:00
isMounted[device] = mounted;
2008-11-12 09:40:09 +01:00
return mounted;
}
void MountAllFAT()
{
#ifdef HW_RVL
2009-10-02 00:21:25 +02:00
MountFAT(DEVICE_SD, SILENT);
MountFAT(DEVICE_USB, SILENT);
#else
2009-10-02 00:21:25 +02:00
MountFAT(DEVICE_SD_SLOTA, SILENT);
MountFAT(DEVICE_SD_SLOTB, SILENT);
#endif
}
2009-10-02 00:21:25 +02:00
bool FindDevice(char * filepath, int * device)
2008-09-02 03:57:21 +02:00
{
2009-10-02 00:21:25 +02:00
if(!filepath || filepath[0] == 0)
return false;
if(strncmp(filepath, "sd:", 3) == 0)
2008-09-02 03:57:21 +02:00
{
2009-10-02 00:21:25 +02:00
*device = DEVICE_SD;
return true;
2008-09-02 03:57:21 +02:00
}
2009-10-02 00:21:25 +02:00
else if(strncmp(filepath, "usb:", 4) == 0)
2008-09-02 03:57:21 +02:00
{
2009-10-02 00:21:25 +02:00
*device = DEVICE_USB;
return true;
2008-09-02 03:57:21 +02:00
}
2009-10-02 00:21:25 +02:00
else if(strncmp(filepath, "dvd:", 4) == 0)
{
2009-10-02 00:21:25 +02:00
*device = DEVICE_DVD;
return true;
}
2009-10-02 00:21:25 +02:00
else if(strncmp(filepath, "smb:", 4) == 0)
{
2009-10-02 00:21:25 +02:00
*device = DEVICE_SMB;
return true;
}
2009-10-02 00:21:25 +02:00
else if(strncmp(filepath, "carda:", 5) == 0)
2009-03-28 18:23:08 +01:00
{
2009-10-02 00:21:25 +02:00
*device = DEVICE_SD_SLOTA;
return true;
2009-03-28 18:23:08 +01:00
}
2009-10-02 00:21:25 +02:00
else if(strncmp(filepath, "cardb:", 5) == 0)
2009-03-28 18:23:08 +01:00
{
2009-10-02 00:21:25 +02:00
*device = DEVICE_SD_SLOTB;
return true;
2009-03-28 18:23:08 +01:00
}
2009-10-02 00:21:25 +02:00
else if(strncmp(filepath, "mca:", 4) == 0)
{
*device = DEVICE_MC_SLOTA;
return true;
}
else if(strncmp(filepath, "mcb:", 4) == 0)
{
*device = DEVICE_MC_SLOTB;
return true;
}
return false;
}
char * StripDevice(char * path)
{
if(path == NULL)
return NULL;
char * newpath = strchr(path,'/');
if(newpath != NULL)
newpath++;
return newpath;
}
2008-09-02 03:57:21 +02:00
2009-10-02 00:21:25 +02:00
/****************************************************************************
* ChangeInterface
* Attempts to mount/configure the device specified
***************************************************************************/
bool ChangeInterface(int device, bool silent)
{
bool mounted = false;
switch(device)
2009-06-12 09:48:27 +02:00
{
2009-10-02 00:21:25 +02:00
case DEVICE_SD:
case DEVICE_USB:
mounted = MountFAT(device, silent);
break;
case DEVICE_DVD:
mounted = MountDVD(silent);
break;
case DEVICE_SMB:
mounted = ConnectShare(silent);
break;
case DEVICE_MC_SLOTA:
mounted = TestMC(CARD_SLOTA, silent);
break;
case DEVICE_MC_SLOTB:
mounted = TestMC(CARD_SLOTB, silent);
break;
2009-06-12 09:48:27 +02:00
}
2008-10-28 07:50:07 +01:00
return mounted;
2008-09-02 03:57:21 +02:00
}
2009-10-02 00:21:25 +02:00
bool ChangeInterface(char * filepath, bool silent)
{
int device = -1;
if(!FindDevice(filepath, &device))
return false;
return ChangeInterface(device, silent);
}
void CreateAppPath(char * origpath)
{
char * path = strdup(origpath); // make a copy so we don't mess up original
if(!path)
return;
char * loc = strrchr(path,'/');
if (loc != NULL)
*loc = 0; // strip file name
int pos = 0;
// replace fat:/ with sd:/
if(strncmp(path, "fat:/", 5) == 0)
{
pos++;
path[1] = 's';
path[2] = 'd';
}
if(ChangeInterface(&path[pos], SILENT))
strncpy(appPath, &path[pos], MAXPATHLEN);
appPath[MAXPATHLEN-1] = 0;
free(path);
}
bool ParseDirEntries()
{
if(!dirIter)
return false;
char filename[MAXPATHLEN];
struct stat filestat;
int i, res;
for(i=0; i < 20; i++)
{
res = dirnext(dirIter,filename,&filestat);
if(res != 0)
break;
if(strcmp(filename,".") == 0)
{
i--;
continue;
}
2009-10-02 00:21:25 +02:00
if(AddBrowserEntry())
{
2009-10-02 00:21:25 +02:00
strncpy(browserList[browser.numEntries+i].filename, filename, MAXJOLIET);
browserList[browser.numEntries+i].length = filestat.st_size;
browserList[browser.numEntries+i].mtime = filestat.st_mtime;
browserList[browser.numEntries+i].isdir = (filestat.st_mode & _IFDIR) == 0 ? 0 : 1; // flag this as a dir
if(browserList[browser.numEntries+i].isdir)
{
if(strcmp(filename, "..") == 0)
sprintf(browserList[browser.numEntries+i].displayname, "Up One Level");
else
strncpy(browserList[browser.numEntries+i].displayname, browserList[browser.numEntries+i].filename, MAXJOLIET);
browserList[browser.numEntries+i].icon = ICON_FOLDER;
}
else
2009-10-02 00:21:25 +02:00
{
StripExt(browserList[browser.numEntries+i].displayname, browserList[browser.numEntries+i].filename); // hide file extension
}
}
}
// Sort the file list
if(i >= 0)
{
browser.numEntries += i;
qsort(browserList, browser.numEntries, sizeof(BROWSERENTRY), FileSortCallback);
}
if(res != 0 || parseHalt)
{
dirclose(dirIter); // close directory
dirIter = NULL;
return false; // no more entries
}
return true; // more entries
}
2008-09-02 03:57:21 +02:00
/***************************************************************************
* Browse subdirectories
2008-11-12 09:40:09 +01:00
**************************************************************************/
2008-09-02 03:57:21 +02:00
int
2009-10-02 00:21:25 +02:00
ParseDirectory(bool waitParse)
2008-09-02 03:57:21 +02:00
{
char msg[128];
2009-03-28 18:23:08 +01:00
int retry = 1;
bool mounted = false;
2009-10-02 00:21:25 +02:00
ResetBrowser(); // reset browser
2008-09-02 03:57:21 +02:00
// open the directory
while(dirIter == NULL && retry == 1)
2009-03-28 18:23:08 +01:00
{
2009-10-02 00:21:25 +02:00
mounted = ChangeInterface(browser.dir, NOTSILENT);
if(mounted)
dirIter = diropen(browser.dir);
else
return -1;
if(dirIter == NULL)
2009-03-28 18:23:08 +01:00
{
2009-10-02 00:21:25 +02:00
sprintf(msg, "Error opening %s", browser.dir);
2009-03-28 18:23:08 +01:00
retry = ErrorPromptRetry(msg);
}
}
2009-10-02 00:21:25 +02:00
// if we can't open the dir, try higher levels
if (dirIter == NULL)
2008-09-02 03:57:21 +02:00
{
2009-10-02 00:21:25 +02:00
while(!IsDeviceRoot(browser.dir))
2008-09-02 03:57:21 +02:00
{
2009-10-02 00:21:25 +02:00
char * devEnd = strrchr(browser.dir, '/');
2009-10-02 05:14:04 +02:00
if(devEnd == NULL)
break;
2009-10-02 00:21:25 +02:00
devEnd[0] = 0; // strip remaining file listing
dirIter = diropen(browser.dir);
if (dirIter)
break;
2008-09-02 03:57:21 +02:00
}
}
2009-10-02 00:21:25 +02:00
if(dirIter == NULL)
return -1;
if(IsDeviceRoot(browser.dir))
{
browser.numEntries = 1;
sprintf(browserList[0].filename, "..");
sprintf(browserList[0].displayname, "Up One Level");
browserList[0].length = 0;
browserList[0].mtime = 0;
browserList[0].isdir = 1; // flag this as a dir
}
2008-09-02 03:57:21 +02:00
parseHalt = false;
2009-07-16 19:47:24 +02:00
ParseDirEntries(); // index first 20 entries
LWP_ResumeThread(parsethread); // index remaining entries
2009-03-28 18:23:08 +01:00
2009-07-16 19:47:24 +02:00
if(waitParse) // wait for complete parsing
{
ShowAction("Loading...");
while(!LWP_ThreadIsSuspended(parsethread))
usleep(THREAD_SLEEP);
CancelAction();
}
return browser.numEntries;
}
/****************************************************************************
* AllocSaveBuffer ()
* Clear and allocate the savebuffer
***************************************************************************/
void
AllocSaveBuffer ()
{
if(bufferLock == LWP_MUTEX_NULL)
LWP_MutexInit(&bufferLock, false);
if(bufferLock != LWP_MUTEX_NULL)
LWP_MutexLock(bufferLock);
memset (savebuffer, 0, SAVEBUFFERSIZE);
}
/****************************************************************************
* FreeSaveBuffer ()
* Free the savebuffer memory
***************************************************************************/
void
FreeSaveBuffer ()
{
if(bufferLock != LWP_MUTEX_NULL)
LWP_MutexUnlock(bufferLock);
2008-09-02 03:57:21 +02:00
}
/****************************************************************************
* LoadSzFile
2008-11-12 09:40:09 +01:00
* Loads the selected file # from the specified 7z into rbuffer
* Returns file size
2008-10-13 20:15:03 +02:00
***************************************************************************/
u32
LoadSzFile(char * filepath, unsigned char * rbuffer)
2008-09-02 03:57:21 +02:00
{
u32 size = 0;
// stop checking if devices were removed/inserted
// since we're loading a file
2009-05-06 19:27:20 +02:00
HaltDeviceThread();
2009-07-08 22:25:52 +02:00
// halt parsing
parseHalt = true;
file = fopen (filepath, "rb");
if (file > 0)
2008-09-02 03:57:21 +02:00
{
size = SzExtractFile(browserList[browser.selIndex].offset, rbuffer);
fclose (file);
2008-09-02 03:57:21 +02:00
}
else
{
2009-03-28 18:23:08 +01:00
ErrorPrompt("Error opening file");
2008-09-02 03:57:21 +02:00
}
// go back to checking if devices were inserted/removed
2009-05-06 19:27:20 +02:00
ResumeDeviceThread();
return size;
2008-09-02 03:57:21 +02:00
}
2008-10-13 20:15:03 +02:00
/****************************************************************************
* LoadFile
2008-10-13 20:15:03 +02:00
***************************************************************************/
u32
2009-10-02 00:21:25 +02:00
LoadFile (char * rbuffer, char *filepath, u32 length, bool silent)
2008-10-13 20:15:03 +02:00
{
2008-11-12 09:40:09 +01:00
char zipbuffer[2048];
u32 size = 0;
u32 readsize = 0;
2009-03-28 18:23:08 +01:00
int retry = 1;
2009-10-02 00:21:25 +02:00
int device;
2009-10-02 00:21:25 +02:00
if(!FindDevice(filepath, &device))
return 0;
2009-10-02 00:21:25 +02:00
switch(device)
{
2009-10-02 00:21:25 +02:00
case DEVICE_DVD:
return LoadDVDFile (rbuffer, StripDevice(filepath), length, silent);
break;
2009-10-02 00:21:25 +02:00
case DEVICE_MC_SLOTA:
return LoadMCFile (rbuffer, CARD_SLOTA, StripDevice(filepath), silent);
break;
2009-10-02 00:21:25 +02:00
case DEVICE_MC_SLOTB:
return LoadMCFile (rbuffer, CARD_SLOTB, StripDevice(filepath), silent);
break;
}
// stop checking if devices were removed/inserted
// since we're loading a file
2009-05-06 19:27:20 +02:00
HaltDeviceThread();
2009-07-08 22:25:52 +02:00
// halt parsing
parseHalt = true;
2009-03-28 18:23:08 +01:00
// open the file
while(!size && retry == 1)
2008-10-13 20:15:03 +02:00
{
2009-10-02 00:21:25 +02:00
if(ChangeInterface(device, silent))
2008-11-12 09:40:09 +01:00
{
2009-10-02 00:21:25 +02:00
file = fopen (filepath, "rb");
2008-11-12 09:40:09 +01:00
2009-03-28 18:23:08 +01:00
if(file > 0)
2008-11-12 09:40:09 +01:00
{
2009-03-28 18:23:08 +01:00
if(length > 0 && length <= 2048) // do a partial read (eg: to check file header)
2008-11-12 09:40:09 +01:00
{
2009-03-28 18:23:08 +01:00
size = fread (rbuffer, 1, length, file);
2008-11-12 09:40:09 +01:00
}
2009-03-28 18:23:08 +01:00
else // load whole file
2008-11-12 09:40:09 +01:00
{
2009-03-28 18:23:08 +01:00
readsize = fread (zipbuffer, 1, 2048, file);
2009-03-28 18:23:08 +01:00
if(readsize > 0)
{
2009-03-28 18:23:08 +01:00
if (IsZipFile (zipbuffer))
{
2009-10-02 00:21:25 +02:00
size = UnZipBuffer ((unsigned char *)rbuffer, device); // unzip
2009-03-28 18:23:08 +01:00
}
else
{
struct stat fileinfo;
if(fstat(file->_file, &fileinfo) == 0)
{
size = fileinfo.st_size;
2009-03-28 18:23:08 +01:00
memcpy (rbuffer, zipbuffer, readsize); // copy what we already read
2009-03-28 18:23:08 +01:00
u32 offset = readsize;
u32 nextread = 0;
while(offset < size)
{
2009-05-30 08:28:00 +02:00
if(size - offset > 4*1024) nextread = 4*1024;
else nextread = size-offset;
ShowProgress ("Loading...", offset, size);
readsize = fread (rbuffer + offset, 1, nextread, file); // read in next chunk
2009-03-28 18:23:08 +01:00
if(readsize <= 0 || readsize > nextread)
break; // read failure
2009-03-28 18:23:08 +01:00
if(readsize > 0)
offset += readsize;
}
CancelAction();
2009-03-28 18:23:08 +01:00
if(offset != size) // # bytes read doesn't match # expected
size = 0;
}
2009-03-28 18:23:08 +01:00
}
}
2008-11-12 09:40:09 +01:00
}
2009-03-28 18:23:08 +01:00
fclose (file);
}
}
if(!size)
{
if(!silent)
{
2009-10-02 00:21:25 +02:00
unmountRequired[device] = true;
2009-03-28 18:23:08 +01:00
retry = ErrorPromptRetry("Error loading file!");
}
else
{
retry = 0;
2008-11-12 09:40:09 +01:00
}
}
2008-10-13 20:15:03 +02:00
}
// go back to checking if devices were inserted/removed
2009-05-06 19:27:20 +02:00
ResumeDeviceThread();
2009-03-28 18:23:08 +01:00
CancelAction();
return size;
}
2009-10-02 00:21:25 +02:00
u32 LoadFile(char * filepath, bool silent)
{
2009-10-02 00:21:25 +02:00
return LoadFile((char *)savebuffer, filepath, 0, silent);
2008-10-13 20:15:03 +02:00
}
2008-09-02 03:57:21 +02:00
/****************************************************************************
* SaveFile
* Write buffer to file
***************************************************************************/
u32
2009-10-02 00:21:25 +02:00
SaveFile (char * buffer, char *filepath, u32 datasize, bool silent)
2008-09-02 03:57:21 +02:00
{
u32 written = 0;
2009-03-28 18:23:08 +01:00
int retry = 1;
2009-10-02 00:21:25 +02:00
int device;
if(!FindDevice(filepath, &device))
return 0;
2009-03-28 18:23:08 +01:00
if(datasize == 0)
return 0;
2009-03-28 18:23:08 +01:00
ShowAction("Saving...");
2009-10-02 00:21:25 +02:00
if(device == DEVICE_MC_SLOTA || device == DEVICE_MC_SLOTB)
{
2009-10-02 00:21:25 +02:00
if(device == DEVICE_MC_SLOTA)
return SaveMCFile (buffer, CARD_SLOTA, StripDevice(filepath), datasize, silent);
2009-03-28 18:23:08 +01:00
else
2009-10-02 00:21:25 +02:00
return SaveMCFile (buffer, CARD_SLOTB, StripDevice(filepath), datasize, silent);
}
2009-03-28 18:23:08 +01:00
// stop checking if devices were removed/inserted
// since we're saving a file
2009-05-06 19:27:20 +02:00
HaltDeviceThread();
2009-03-28 18:23:08 +01:00
while(!written && retry == 1)
{
2009-10-02 00:21:25 +02:00
if(ChangeInterface(device, silent))
{
2009-10-02 00:21:25 +02:00
file = fopen (filepath, "wb");
2009-03-28 18:23:08 +01:00
if (file > 0)
{
u32 writesize, nextwrite;
while(written < datasize)
{
2009-05-30 08:28:00 +02:00
if(datasize - written > 4*1024) nextwrite=4*1024;
else nextwrite = datasize-written;
writesize = fwrite (buffer+written, 1, nextwrite, file);
if(writesize != nextwrite) break; // write failure
written += writesize;
}
if(written != datasize) written = 0;
2009-03-28 18:23:08 +01:00
fclose (file);
}
}
if(!written)
{
2009-10-02 00:21:25 +02:00
unmountRequired[device] = true;
2009-03-28 18:23:08 +01:00
if(!silent)
retry = ErrorPromptRetry("Error saving file!");
else
retry = 0;
}
2009-03-28 18:23:08 +01:00
}
// go back to checking if devices were inserted/removed
2009-05-06 19:27:20 +02:00
ResumeDeviceThread();
2009-03-28 18:23:08 +01:00
CancelAction();
2009-10-02 00:21:25 +02:00
return written;
}
2009-10-02 00:21:25 +02:00
u32 SaveFile(char * filepath, u32 datasize, bool silent)
{
2009-10-02 00:21:25 +02:00
return SaveFile((char *)savebuffer, filepath, datasize, silent);
2008-09-02 03:57:21 +02:00
}