2008-12-11 18:38:29 +01:00
|
|
|
/*
|
|
|
|
* file_fat.c
|
|
|
|
*
|
2009-05-13 16:26:55 +02:00
|
|
|
* FAT loading support
|
2008-12-11 18:38:29 +01:00
|
|
|
*
|
2009-05-13 16:26:55 +02:00
|
|
|
* Eke-Eke (2008,2009)
|
2008-12-11 18:38:29 +01:00
|
|
|
*
|
2009-05-13 16:26:55 +02:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
2008-12-11 18:38:29 +01:00
|
|
|
*
|
2009-05-13 16:26:55 +02:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2008-12-10 19:16:30 +01:00
|
|
|
*
|
2009-05-13 16:26:55 +02:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
2008-12-11 18:38:29 +01:00
|
|
|
*
|
|
|
|
********************************************************************************/
|
|
|
|
|
2008-12-10 19:16:30 +01:00
|
|
|
#include "shared.h"
|
2009-05-19 18:14:43 +02:00
|
|
|
#include "gui.h"
|
2008-12-10 19:16:30 +01:00
|
|
|
#include "history.h"
|
2008-12-11 18:38:29 +01:00
|
|
|
#include "unzip.h"
|
|
|
|
#include "filesel.h"
|
|
|
|
#include "file_fat.h"
|
2009-05-25 19:07:34 +02:00
|
|
|
#include "file_dvd.h"
|
|
|
|
|
2008-12-10 19:16:30 +01:00
|
|
|
/* current FAT directory */
|
2010-05-07 20:25:27 +02:00
|
|
|
static char *fatdir;
|
2008-12-10 19:16:30 +01:00
|
|
|
|
|
|
|
/* current FAT device */
|
2010-05-07 20:25:27 +02:00
|
|
|
static int fatType = -1;
|
2008-12-10 19:16:30 +01:00
|
|
|
|
|
|
|
/***************************************************************************
|
2009-05-26 15:14:33 +02:00
|
|
|
* FAT_ClearDirectory
|
|
|
|
*
|
2010-05-07 20:25:27 +02:00
|
|
|
* Clear FAT access flag
|
2009-05-26 15:14:33 +02:00
|
|
|
***************************************************************************/
|
|
|
|
void FAT_ClearDirectory(void)
|
|
|
|
{
|
2010-05-07 20:25:27 +02:00
|
|
|
fatType = -1;
|
2009-05-26 15:14:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/***************************************************************************
|
|
|
|
* FAT_UpdateDirectory
|
2008-12-10 19:16:30 +01:00
|
|
|
*
|
2010-05-18 09:35:29 +02:00
|
|
|
* Update FAT current directory
|
|
|
|
* return zero if exiting root
|
2008-12-10 19:16:30 +01:00
|
|
|
***************************************************************************/
|
2010-05-07 20:25:27 +02:00
|
|
|
int FAT_UpdateDirectory(bool go_up, char *dirname)
|
2008-12-10 19:16:30 +01:00
|
|
|
{
|
|
|
|
int size=0;
|
|
|
|
char *test;
|
2010-05-28 14:08:00 +02:00
|
|
|
char temp[MAXPATHLEN];
|
2008-12-10 19:16:30 +01:00
|
|
|
|
|
|
|
/* go up to parent directory */
|
2010-05-07 20:25:27 +02:00
|
|
|
if (strcmp(dirname,"..") == 0)
|
2008-12-10 19:16:30 +01:00
|
|
|
{
|
|
|
|
/* determine last subdirectory namelength */
|
|
|
|
sprintf(temp,"%s",fatdir);
|
|
|
|
test= strtok(temp,"/");
|
|
|
|
while (test != NULL)
|
|
|
|
{
|
|
|
|
size = strlen(test);
|
|
|
|
test = strtok(NULL,"/");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* remove last subdirectory name */
|
|
|
|
size = strlen(fatdir) - size;
|
|
|
|
fatdir[size-1] = 0;
|
|
|
|
}
|
|
|
|
else if (go_up)
|
|
|
|
{
|
|
|
|
/* root has no parent directory */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* by default, simply append folder name */
|
2010-05-07 20:25:27 +02:00
|
|
|
sprintf(fatdir, "%s%s/",fatdir, dirname);
|
2008-12-10 19:16:30 +01:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***************************************************************************
|
|
|
|
* FAT_ParseDirectory
|
|
|
|
*
|
|
|
|
* List files into one FAT directory
|
|
|
|
***************************************************************************/
|
2009-05-19 18:14:43 +02:00
|
|
|
int FAT_ParseDirectory(void)
|
2008-12-10 19:16:30 +01:00
|
|
|
{
|
|
|
|
int nbfiles = 0;
|
|
|
|
char filename[MAXPATHLEN];
|
|
|
|
struct stat filestat;
|
|
|
|
|
|
|
|
/* open directory */
|
|
|
|
DIR_ITER *dir = diropen (fatdir);
|
|
|
|
if (dir == NULL)
|
|
|
|
{
|
2009-05-20 17:11:06 +02:00
|
|
|
GUI_WaitPrompt("Error","Unable to open directory !");
|
2009-05-19 18:14:43 +02:00
|
|
|
return -1;
|
2008-12-10 19:16:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
while ((dirnext(dir, filename, &filestat) == 0) && (nbfiles < MAXFILES))
|
|
|
|
{
|
|
|
|
if (strcmp(filename,".") != 0)
|
|
|
|
{
|
|
|
|
memset(&filelist[nbfiles], 0, sizeof (FILEENTRIES));
|
|
|
|
sprintf(filelist[nbfiles].filename,"%s",filename);
|
|
|
|
filelist[nbfiles].length = filestat.st_size;
|
|
|
|
filelist[nbfiles].flags = (filestat.st_mode & S_IFDIR) ? 1 : 0;
|
|
|
|
nbfiles++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dirclose(dir);
|
|
|
|
|
|
|
|
/* Sort the file list */
|
|
|
|
qsort(filelist, nbfiles, sizeof(FILEENTRIES), FileSortCallback);
|
|
|
|
|
|
|
|
return nbfiles;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* FAT_LoadFile
|
|
|
|
*
|
|
|
|
* This function will load a BIN, SMD or ZIP file from DVD into the ROM buffer.
|
|
|
|
* This functions return the actual size of data copied into the buffer
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
2009-05-25 19:07:34 +02:00
|
|
|
int FAT_LoadFile(u8 *buffer, u32 selection)
|
2008-12-10 19:16:30 +01:00
|
|
|
{
|
2010-05-07 20:25:27 +02:00
|
|
|
char fname[MAXPATHLEN];
|
|
|
|
int length = 0;
|
|
|
|
|
|
|
|
/* Loading from history */
|
|
|
|
if(fatType == TYPE_RECENT)
|
2010-05-09 02:53:19 +02:00
|
|
|
{
|
2010-05-07 20:25:27 +02:00
|
|
|
/* full filename */
|
|
|
|
sprintf(fname,"%s%s",history.entries[selection].filepath,filelist[selection].filename);
|
2008-12-10 19:16:30 +01:00
|
|
|
|
2010-05-07 20:25:27 +02:00
|
|
|
/* get the length of the file */
|
2008-12-10 19:16:30 +01:00
|
|
|
struct stat filestat;
|
2010-05-07 20:25:27 +02:00
|
|
|
if(stat(fname, &filestat) == 0)
|
|
|
|
length = filestat.st_size;
|
2008-12-10 19:16:30 +01:00
|
|
|
}
|
2010-05-07 20:25:27 +02:00
|
|
|
else
|
2008-12-10 19:16:30 +01:00
|
|
|
{
|
|
|
|
/* full filename */
|
|
|
|
sprintf(fname, "%s%s",fatdir,filelist[selection].filename);
|
|
|
|
|
2010-05-07 20:25:27 +02:00
|
|
|
/* get the length of the file */
|
|
|
|
length = filelist[selection].length;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (length > 0)
|
|
|
|
{
|
|
|
|
/* Open file */
|
2008-12-10 19:16:30 +01:00
|
|
|
FILE *sdfile = fopen(fname, "rb");
|
|
|
|
if (sdfile == NULL)
|
|
|
|
{
|
2009-05-20 17:11:06 +02:00
|
|
|
GUI_WaitPrompt("Error","Unable to open file !");
|
2008-12-10 19:16:30 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-05-07 20:25:27 +02:00
|
|
|
/* Add/move the file to the top of the history. */
|
|
|
|
if(fatType == TYPE_RECENT)
|
|
|
|
history_add_file(history.entries[selection].filepath, filelist[selection].filename);
|
|
|
|
else
|
|
|
|
history_add_file(fatdir, filelist[selection].filename);
|
|
|
|
|
2010-05-09 03:15:40 +02:00
|
|
|
/* file browser should be reinitialized */
|
|
|
|
if(fatType == TYPE_RECENT)
|
|
|
|
fatType = -1;
|
|
|
|
|
2008-12-10 19:16:30 +01:00
|
|
|
/* Read first data chunk */
|
2009-10-16 13:32:55 +02:00
|
|
|
unsigned char temp[FATCHUNK];
|
|
|
|
fread(temp, FATCHUNK, 1, sdfile);
|
2008-12-10 19:16:30 +01:00
|
|
|
fclose(sdfile);
|
|
|
|
|
2010-05-07 20:25:27 +02:00
|
|
|
/* Determine file type */
|
2008-12-10 19:16:30 +01:00
|
|
|
if (!IsZipFile ((char *) temp))
|
|
|
|
{
|
2010-05-28 14:08:00 +02:00
|
|
|
if (length > MAXROMSIZE)
|
|
|
|
{
|
|
|
|
GUI_WaitPrompt("Error","File size not supported !");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-12-10 19:16:30 +01:00
|
|
|
/* re-open and read file */
|
|
|
|
sdfile = fopen(fname, "rb");
|
|
|
|
if (sdfile)
|
|
|
|
{
|
2010-05-28 14:08:00 +02:00
|
|
|
char msg[64];
|
2009-05-19 18:14:43 +02:00
|
|
|
sprintf(msg,"Loading %d bytes ...", length);
|
2009-05-23 19:29:53 +02:00
|
|
|
GUI_MsgBoxOpen("Information",msg,1);
|
2010-05-07 20:25:27 +02:00
|
|
|
int done = 0;
|
2009-10-16 13:32:55 +02:00
|
|
|
while (length > FATCHUNK)
|
|
|
|
{
|
2010-05-07 20:25:27 +02:00
|
|
|
fread(buffer + done, FATCHUNK, 1, sdfile);
|
2009-10-16 13:32:55 +02:00
|
|
|
length -= FATCHUNK;
|
2010-05-07 20:25:27 +02:00
|
|
|
done += FATCHUNK;
|
2009-10-16 13:32:55 +02:00
|
|
|
}
|
2010-05-07 20:25:27 +02:00
|
|
|
fread(buffer + done, length, 1, sdfile);
|
|
|
|
done += length;
|
2008-12-10 19:16:30 +01:00
|
|
|
fclose(sdfile);
|
2010-05-07 20:25:27 +02:00
|
|
|
return done;
|
2008-12-10 19:16:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* unzip file */
|
2010-05-07 20:25:27 +02:00
|
|
|
return UnZipBuffer(buffer, 0, fname);
|
2008-12-10 19:16:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* OpenFAT
|
|
|
|
*
|
|
|
|
* Function to load a FAT directory and display to user.
|
|
|
|
****************************************************************************/
|
2009-05-19 18:14:43 +02:00
|
|
|
int FAT_Open(int type)
|
2008-12-10 19:16:30 +01:00
|
|
|
{
|
|
|
|
int max = 0;
|
|
|
|
|
2010-05-07 20:25:27 +02:00
|
|
|
if (type == TYPE_RECENT)
|
2008-12-10 19:16:30 +01:00
|
|
|
{
|
2010-05-07 20:25:27 +02:00
|
|
|
/* fetch history list */
|
|
|
|
int i;
|
|
|
|
for(i=0; i < NUM_HISTORY_ENTRIES; i++)
|
2008-12-10 19:16:30 +01:00
|
|
|
{
|
2010-05-07 20:25:27 +02:00
|
|
|
if(history.entries[i].filepath[0] > 0)
|
2008-12-10 19:16:30 +01:00
|
|
|
{
|
2010-05-07 20:25:27 +02:00
|
|
|
filelist[i].offset = 0;
|
|
|
|
filelist[i].length = 0;
|
|
|
|
filelist[i].flags = 0;
|
2010-05-28 14:08:00 +02:00
|
|
|
strncpy(filelist[i].filename,history.entries[i].filename, MAXJOLIET-1);
|
2010-05-07 20:25:27 +02:00
|
|
|
filelist[i].filename[MAXJOLIET-1] = '\0';
|
|
|
|
max++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Found the end of the list. */
|
|
|
|
break;
|
2008-12-10 19:16:30 +01:00
|
|
|
}
|
|
|
|
}
|
2010-05-07 20:25:27 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* default directory */
|
|
|
|
fatdir = config.sddir;
|
|
|
|
#ifdef HW_RVL
|
|
|
|
if (type == TYPE_USB)
|
|
|
|
fatdir = config.usbdir;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* verify current dir exists, otherwise browse from root */
|
|
|
|
DIR_ITER *dir = diropen(fatdir);
|
|
|
|
if (dir)
|
|
|
|
dirclose(dir);
|
|
|
|
#ifdef HW_RVL
|
|
|
|
else if (type == TYPE_USB)
|
|
|
|
sprintf (fatdir, "usb:/");
|
2008-12-10 19:16:30 +01:00
|
|
|
else
|
2010-05-07 20:25:27 +02:00
|
|
|
sprintf (fatdir, "sd:/");
|
2010-05-25 08:42:28 +02:00
|
|
|
#else
|
|
|
|
else
|
|
|
|
sprintf (fatdir, "/");
|
|
|
|
#endif
|
2008-12-10 19:16:30 +01:00
|
|
|
|
2010-05-07 20:25:27 +02:00
|
|
|
/* parse current directory */
|
|
|
|
max = FAT_ParseDirectory ();
|
|
|
|
}
|
2008-12-10 19:16:30 +01:00
|
|
|
|
2010-05-07 20:25:27 +02:00
|
|
|
if (max < 0)
|
|
|
|
return 0;
|
2008-12-10 19:16:30 +01:00
|
|
|
|
2010-05-07 20:25:27 +02:00
|
|
|
if (max == 0)
|
|
|
|
{
|
|
|
|
GUI_WaitPrompt("Error","No files found !");
|
|
|
|
return 0;
|
|
|
|
}
|
2008-12-10 19:16:30 +01:00
|
|
|
|
2010-05-07 20:25:27 +02:00
|
|
|
/* check if access type has changed */
|
|
|
|
if (type != fatType)
|
|
|
|
{
|
|
|
|
/* set current access type */
|
|
|
|
fatType = type;
|
|
|
|
|
|
|
|
/* reset File selector */
|
|
|
|
ClearSelector(max);
|
|
|
|
|
|
|
|
/* clear DVD access flag */
|
|
|
|
DVD_ClearDirectory();
|
2008-12-10 19:16:30 +01:00
|
|
|
}
|
|
|
|
|
2009-05-19 18:14:43 +02:00
|
|
|
return 1;
|
2008-12-10 19:16:30 +01:00
|
|
|
}
|