2012-02-27 17:05:10 +01:00
|
|
|
/*////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
fsop contains coomprensive set of function for file and folder handling
|
|
|
|
|
|
|
|
en exposed s_fsop fsop structure can be used by callback to update operation status
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <ogcsys.h>
|
|
|
|
#include <ogc/lwp_watchdog.h>
|
|
|
|
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/statvfs.h>
|
|
|
|
|
|
|
|
#include "fileOps.h"
|
|
|
|
#include "memory/mem2.hpp"
|
|
|
|
#include "utils.h"
|
2012-03-03 18:16:11 +01:00
|
|
|
#include "gecko.h"
|
2012-02-27 17:05:10 +01:00
|
|
|
|
2012-03-03 18:16:11 +01:00
|
|
|
#define SET(a, b) a = b; DCFlushRange(&a, sizeof(a));
|
|
|
|
#define STACKSIZE 8192
|
|
|
|
|
|
|
|
static u8 *buff = NULL;
|
|
|
|
static FILE *fs = NULL, *ft = NULL;
|
2012-03-05 18:27:45 +01:00
|
|
|
static u32 bytes;
|
2012-03-03 18:16:11 +01:00
|
|
|
static u32 block = 32768;
|
|
|
|
static u32 blockIdx = 0;
|
|
|
|
static u32 blockInfo[2] = {0,0};
|
|
|
|
static u32 blockReady = 0;
|
2012-05-12 18:03:14 +02:00
|
|
|
static s32 stopThread;
|
2012-03-05 10:48:13 +01:00
|
|
|
static u64 folderSize = 0;
|
2012-02-27 17:05:10 +01:00
|
|
|
|
|
|
|
// return false if the file doesn't exist
|
2012-05-13 17:13:33 +02:00
|
|
|
bool fsop_GetFileSizeBytes(char *path, size_t *filesize) // for me stats st_size report always 0 :(
|
2012-02-27 17:05:10 +01:00
|
|
|
{
|
|
|
|
FILE *f;
|
|
|
|
size_t size = 0;
|
2012-03-03 18:16:11 +01:00
|
|
|
|
2012-02-27 17:05:10 +01:00
|
|
|
f = fopen(path, "rb");
|
2012-05-13 17:13:33 +02:00
|
|
|
if(!f)
|
2012-02-27 17:05:10 +01:00
|
|
|
{
|
2012-05-13 17:13:33 +02:00
|
|
|
if(filesize)
|
|
|
|
*filesize = size;
|
2012-02-27 17:05:10 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Get file size
|
|
|
|
fseek( f, 0, SEEK_END);
|
|
|
|
size = ftell(f);
|
2012-05-13 17:13:33 +02:00
|
|
|
if(filesize)
|
|
|
|
*filesize = size;
|
|
|
|
fclose(f);
|
2012-02-27 17:05:10 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Recursive fsop_GetFolderBytes
|
|
|
|
*/
|
|
|
|
u64 fsop_GetFolderBytes (char *source)
|
|
|
|
{
|
|
|
|
DIR *pdir;
|
|
|
|
struct dirent *pent;
|
|
|
|
char newSource[300];
|
|
|
|
u64 bytes = 0;
|
2012-03-03 18:16:11 +01:00
|
|
|
|
|
|
|
pdir = opendir(source);
|
|
|
|
|
2012-02-27 17:05:10 +01:00
|
|
|
while ((pent=readdir(pdir)) != NULL)
|
|
|
|
{
|
|
|
|
// Skip it
|
|
|
|
if (strcmp (pent->d_name, ".") == 0 || strcmp (pent->d_name, "..") == 0)
|
|
|
|
continue;
|
2012-03-03 18:16:11 +01:00
|
|
|
|
2012-05-04 14:30:43 +02:00
|
|
|
snprintf(newSource, sizeof(newSource), "%s/%s", source, pent->d_name);
|
2012-03-03 18:16:11 +01:00
|
|
|
|
2012-02-27 17:05:10 +01:00
|
|
|
// If it is a folder... recurse...
|
|
|
|
if (fsop_DirExist (newSource))
|
|
|
|
{
|
|
|
|
bytes += fsop_GetFolderBytes (newSource);
|
|
|
|
}
|
|
|
|
else // It is a file !
|
|
|
|
{
|
|
|
|
size_t s;
|
|
|
|
fsop_GetFileSizeBytes (newSource, &s);
|
|
|
|
bytes += s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir(pdir);
|
2012-03-03 18:16:11 +01:00
|
|
|
|
2012-02-27 17:05:10 +01:00
|
|
|
return bytes;
|
2012-03-03 18:16:11 +01:00
|
|
|
}
|
2012-02-27 17:05:10 +01:00
|
|
|
|
|
|
|
u32 fsop_GetFolderKb (char *source)
|
|
|
|
{
|
|
|
|
u32 ret = (u32) round ((double)fsop_GetFolderBytes (source) / 1000.0);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 fsop_GetFreeSpaceKb (char *path) // Return free kb on the device passed
|
|
|
|
{
|
|
|
|
struct statvfs s;
|
2012-03-03 18:16:11 +01:00
|
|
|
|
|
|
|
statvfs(path, &s);
|
|
|
|
|
2012-02-27 17:05:10 +01:00
|
|
|
u32 ret = (u32)round( ((double)s.f_bfree / 1000.0) * s.f_bsize);
|
2012-03-03 18:16:11 +01:00
|
|
|
|
2012-02-27 17:05:10 +01:00
|
|
|
return ret ;
|
|
|
|
}
|
|
|
|
|
2012-04-08 17:54:34 +02:00
|
|
|
bool fsop_FileExist(const char *fn)
|
|
|
|
{
|
|
|
|
FILE * f;
|
|
|
|
f = fopen(fn, "rb");
|
|
|
|
if (f)
|
|
|
|
{
|
|
|
|
fclose(f);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-02-27 17:05:10 +01:00
|
|
|
bool fsop_DirExist (char *path)
|
|
|
|
{
|
|
|
|
DIR *dir;
|
2012-03-03 18:16:11 +01:00
|
|
|
|
2012-02-27 17:05:10 +01:00
|
|
|
dir=opendir(path);
|
|
|
|
if (dir)
|
|
|
|
{
|
|
|
|
closedir(dir);
|
|
|
|
return true;
|
|
|
|
}
|
2012-03-03 18:16:11 +01:00
|
|
|
|
2012-02-27 17:05:10 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-05-12 18:03:14 +02:00
|
|
|
static void *thread_CopyFileReader()
|
2012-03-03 18:16:11 +01:00
|
|
|
{
|
|
|
|
u32 rb;
|
|
|
|
stopThread = 0;
|
|
|
|
DCFlushRange(&stopThread, sizeof(stopThread));
|
|
|
|
do
|
|
|
|
{
|
|
|
|
SET (rb, fread(&buff[blockIdx*block], 1, block, fs ));
|
|
|
|
SET (blockInfo[blockIdx], rb);
|
|
|
|
SET (blockReady, 1);
|
|
|
|
|
|
|
|
while (blockReady && !stopThread) usleep(1);
|
|
|
|
}
|
|
|
|
while (stopThread == 0);
|
|
|
|
|
|
|
|
stopThread = -1;
|
|
|
|
DCFlushRange(&stopThread, sizeof(stopThread));
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-02-27 17:05:10 +01:00
|
|
|
bool fsop_CopyFile (char *source, char *target, progress_callback_t spinner, void *spinner_data)
|
|
|
|
{
|
2012-03-03 18:16:11 +01:00
|
|
|
gprintf("Creating file: %s\n",target);
|
2012-02-27 17:05:10 +01:00
|
|
|
int err = 0;
|
2012-03-03 18:16:11 +01:00
|
|
|
|
2012-02-27 17:05:10 +01:00
|
|
|
u32 size;
|
2012-03-05 18:27:45 +01:00
|
|
|
u32 rb,wb;
|
2012-03-03 18:16:11 +01:00
|
|
|
|
2012-02-27 17:05:10 +01:00
|
|
|
fs = fopen(source, "rb");
|
|
|
|
if (!fs)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ft = fopen(target, "wt");
|
|
|
|
if (!ft)
|
|
|
|
{
|
2012-05-13 17:13:33 +02:00
|
|
|
fclose(fs);
|
2012-02-27 17:05:10 +01:00
|
|
|
return false;
|
|
|
|
}
|
2012-03-03 18:16:11 +01:00
|
|
|
|
2012-02-27 17:05:10 +01:00
|
|
|
//Get file size
|
|
|
|
fseek (fs, 0, SEEK_END);
|
|
|
|
size = ftell(fs);
|
|
|
|
|
|
|
|
if (size == 0)
|
|
|
|
{
|
2012-05-13 17:13:33 +02:00
|
|
|
fclose(fs);
|
|
|
|
fclose(ft);
|
2012-02-27 17:05:10 +01:00
|
|
|
return true;
|
|
|
|
}
|
2012-03-03 18:16:11 +01:00
|
|
|
|
2012-02-27 17:05:10 +01:00
|
|
|
// Return to beginning....
|
|
|
|
fseek(fs, 0, SEEK_SET);
|
2012-03-03 18:16:11 +01:00
|
|
|
|
2012-05-13 19:25:26 +02:00
|
|
|
u8 *threadStack = NULL;
|
2012-03-03 18:16:11 +01:00
|
|
|
lwp_t hthread = LWP_THREAD_NULL;
|
|
|
|
|
|
|
|
buff = MEM2_alloc(block*2);
|
2012-05-13 19:25:26 +02:00
|
|
|
if(buff == NULL)
|
|
|
|
return false;
|
2012-03-03 18:16:11 +01:00
|
|
|
|
|
|
|
blockIdx = 0;
|
|
|
|
blockReady = 0;
|
|
|
|
blockInfo[0] = 0;
|
|
|
|
blockInfo[1] = 0;
|
|
|
|
|
|
|
|
threadStack = MEM2_alloc(STACKSIZE);
|
2012-05-13 19:25:26 +02:00
|
|
|
if(threadStack == NULL)
|
|
|
|
return false;
|
|
|
|
|
2012-03-03 18:16:11 +01:00
|
|
|
LWP_CreateThread (&hthread, thread_CopyFileReader, NULL, threadStack, STACKSIZE, 30);
|
|
|
|
|
|
|
|
while (stopThread != 0)
|
|
|
|
usleep (5);
|
|
|
|
|
|
|
|
u32 bi;
|
2012-02-27 17:05:10 +01:00
|
|
|
do
|
|
|
|
{
|
2012-03-03 18:16:11 +01:00
|
|
|
while (!blockReady) usleep (1); // Let's wait for incoming block from the thread
|
2012-02-27 17:05:10 +01:00
|
|
|
|
2012-03-03 18:16:11 +01:00
|
|
|
bi = blockIdx;
|
|
|
|
|
|
|
|
// let's th thread to read the next buff
|
|
|
|
SET (blockIdx, 1 - blockIdx);
|
|
|
|
SET (blockReady, 0);
|
|
|
|
|
|
|
|
rb = blockInfo[bi];
|
|
|
|
// write current block
|
|
|
|
wb = fwrite(&buff[bi*block], 1, rb, ft);
|
|
|
|
|
2012-02-27 17:05:10 +01:00
|
|
|
if (wb != wb) err = 1;
|
|
|
|
if (rb == 0) err = 1;
|
|
|
|
bytes += rb;
|
2012-03-03 18:16:11 +01:00
|
|
|
|
2012-03-05 10:48:13 +01:00
|
|
|
if (spinner)
|
|
|
|
spinner(bytes, folderSize, spinner_data);
|
2012-02-27 17:05:10 +01:00
|
|
|
}
|
|
|
|
while (bytes < size && err == 0);
|
|
|
|
|
2012-03-03 18:16:11 +01:00
|
|
|
stopThread = 1;
|
|
|
|
DCFlushRange(&stopThread, sizeof(stopThread));
|
|
|
|
|
|
|
|
while (stopThread != -1)
|
|
|
|
usleep (5);
|
|
|
|
|
|
|
|
LWP_JoinThread(hthread, NULL);
|
|
|
|
MEM2_free(threadStack);
|
|
|
|
|
|
|
|
stopThread = 1;
|
|
|
|
DCFlushRange(&stopThread, sizeof(stopThread));
|
|
|
|
|
2012-05-13 17:13:33 +02:00
|
|
|
fclose(fs);
|
|
|
|
fclose(ft);
|
2012-02-27 17:05:10 +01:00
|
|
|
MEM2_free(buff);
|
|
|
|
|
2012-03-03 18:16:11 +01:00
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
unlink (target);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-02-27 17:05:10 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Recursive copyfolder
|
|
|
|
*/
|
|
|
|
static bool doCopyFolder (char *source, char *target, progress_callback_t spinner, void *spinner_data)
|
|
|
|
{
|
|
|
|
DIR *pdir;
|
|
|
|
struct dirent *pent;
|
|
|
|
char newSource[300], newTarget[300];
|
|
|
|
bool ret = true;
|
2012-03-03 18:16:11 +01:00
|
|
|
|
2012-02-27 17:05:10 +01:00
|
|
|
// If target folder doesn't exist, create it !
|
2012-03-02 18:45:08 +01:00
|
|
|
if (!fsop_DirExist(target))
|
2012-02-27 17:05:10 +01:00
|
|
|
{
|
2012-03-03 18:16:11 +01:00
|
|
|
gprintf("Creating directory: %s\n",target);
|
2012-03-02 18:45:08 +01:00
|
|
|
makedir(target);
|
2012-02-27 17:05:10 +01:00
|
|
|
}
|
|
|
|
|
2012-03-03 18:16:11 +01:00
|
|
|
pdir = opendir(source);
|
|
|
|
|
2012-02-27 17:05:10 +01:00
|
|
|
while ((pent=readdir(pdir)) != NULL && ret == true)
|
|
|
|
{
|
|
|
|
// Skip it
|
|
|
|
if (strcmp (pent->d_name, ".") == 0 || strcmp (pent->d_name, "..") == 0)
|
|
|
|
continue;
|
2012-03-03 18:16:11 +01:00
|
|
|
|
2012-05-04 14:30:43 +02:00
|
|
|
snprintf(newSource, sizeof(newSource), "%s/%s", source, pent->d_name);
|
|
|
|
snprintf(newTarget, sizeof(newTarget), "%s/%s", target, pent->d_name);
|
2012-02-27 17:05:10 +01:00
|
|
|
|
|
|
|
// If it is a folder... recurse...
|
2012-03-02 18:45:08 +01:00
|
|
|
if (fsop_DirExist(newSource))
|
2012-02-27 17:05:10 +01:00
|
|
|
{
|
2012-03-02 18:45:08 +01:00
|
|
|
ret = doCopyFolder(newSource, newTarget, spinner, spinner_data);
|
2012-02-27 17:05:10 +01:00
|
|
|
}
|
|
|
|
else // It is a file !
|
|
|
|
{
|
2012-03-02 18:45:08 +01:00
|
|
|
ret = fsop_CopyFile(newSource, newTarget, spinner, spinner_data);
|
2012-02-27 17:05:10 +01:00
|
|
|
}
|
|
|
|
}
|
2012-03-03 18:16:11 +01:00
|
|
|
|
2012-02-27 17:05:10 +01:00
|
|
|
closedir(pdir);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2012-03-05 10:48:13 +01:00
|
|
|
|
2012-02-27 17:05:10 +01:00
|
|
|
bool fsop_CopyFolder (char *source, char *target, progress_callback_t spinner, void *spinner_data)
|
|
|
|
{
|
2012-03-03 18:16:11 +01:00
|
|
|
gprintf("DML game USB->SD job started!\n");
|
2012-02-27 17:05:10 +01:00
|
|
|
|
2012-03-05 18:27:45 +01:00
|
|
|
bytes = 0;
|
2012-03-05 10:48:13 +01:00
|
|
|
folderSize = fsop_GetFolderBytes(source);
|
2012-03-03 18:16:11 +01:00
|
|
|
return doCopyFolder(source, target, spinner, spinner_data);
|
2012-02-27 17:05:10 +01:00
|
|
|
}
|
2012-03-05 10:48:13 +01:00
|
|
|
|
|
|
|
void fsop_deleteFolder(char *source)
|
|
|
|
{
|
|
|
|
DIR *pdir;
|
|
|
|
struct dirent *pent;
|
|
|
|
char newSource[300];
|
|
|
|
|
|
|
|
pdir = opendir(source);
|
|
|
|
|
|
|
|
while ((pent=readdir(pdir)) != NULL)
|
|
|
|
{
|
|
|
|
// Skip it
|
|
|
|
if (strcmp (pent->d_name, ".") == 0 || strcmp (pent->d_name, "..") == 0)
|
|
|
|
continue;
|
|
|
|
|
2012-05-04 14:30:43 +02:00
|
|
|
snprintf (newSource, sizeof(newSource), "%s/%s", source, pent->d_name);
|
2012-03-05 10:48:13 +01:00
|
|
|
|
|
|
|
// If it is a folder... recurse...
|
|
|
|
if (fsop_DirExist(newSource))
|
|
|
|
{
|
|
|
|
fsop_deleteFolder(newSource);
|
|
|
|
}
|
|
|
|
else // It is a file !
|
|
|
|
{
|
|
|
|
gprintf("Deleting file: %s\n",newSource);
|
|
|
|
remove(newSource);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir(pdir);
|
|
|
|
gprintf("Deleting directory: %s\n",source);
|
|
|
|
unlink(source);
|
|
|
|
}
|