735 lines
27 KiB
C
Raw Normal View History

2016-12-10 15:32:03 +01:00
#include "fsutil.h"
#include "fsinit.h"
#include "fsdrive.h"
#include "fsperm.h"
2016-10-30 16:20:09 +01:00
#include "sddata.h"
2017-01-27 18:32:52 +01:00
#include "vff.h"
2016-12-10 15:32:03 +01:00
#include "virtual.h"
2016-12-13 17:10:39 +01:00
#include "image.h"
#include "sha.h"
2016-07-13 19:59:36 +02:00
#include "sdmmc.h"
2016-04-05 20:34:50 +02:00
#include "ff.h"
2016-12-10 15:32:03 +01:00
#include "ui.h"
2016-03-11 01:29:14 +01:00
2017-06-09 01:45:00 +02:00
#define SKIP_CUR (1UL<<8)
#define OVERWRITE_CUR (1UL<<9)
#define _MAX_FS_OPT 8 // max file selector options
2016-07-13 19:59:36 +02:00
// Volume2Partition resolution table
PARTITION VolToPart[] = {
2017-04-24 01:57:34 +02:00
{0, 0}, {1, 0}, {2, 0}, {3, 0}, {4, 0},
2016-07-13 19:59:36 +02:00
{5, 0}, {6, 0}, {7, 0}, {8, 0}, {9, 0}
};
uint64_t GetSDCardSize() {
if (sdmmc_sdcard_init() != 0) return 0;
return (u64) getMMCDevice(1)->total_size * 512;
}
bool FormatSDCard(u64 hidden_mb, u32 cluster_size, const char* label) {
2016-07-13 19:59:36 +02:00
u8 mbr[0x200] = { 0 };
u8 ncsd[0x200] = { 0 };
2016-07-13 19:59:36 +02:00
u8 mbrdata[0x42] = {
0x80, 0x01, 0x01, 0x00, 0x0C, 0xFE, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x80, 0x01, 0x01, 0x00, 0x1C, 0xFE, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x55, 0xAA
};
u32 sd_size = getMMCDevice(1)->total_size;
u32 emu_sector = 1;
u32 emu_size = (u32) ((hidden_mb * 1024 * 1024) / 512);
u32 fat_sector = align(emu_sector + emu_size, 0x2000); // align to 4MB
u32 fat_size = (fat_sector < sd_size) ? sd_size - fat_sector : 0;
2016-07-13 19:59:36 +02:00
// FAT size check
if (fat_size < 0x80000) { // minimum free space: 256MB
2016-12-08 13:18:25 +01:00
ShowPrompt(false, "Error: SD card is too small");
2016-07-13 19:59:36 +02:00
return false;
}
sd_size = fat_size;
2016-07-13 19:59:36 +02:00
// build the MBR
memcpy(mbrdata + 0x08, &fat_sector, 4);
memcpy(mbrdata + 0x0C, &fat_size, 4);
memcpy(mbrdata + 0x18, &emu_sector, 4);
memcpy(mbrdata + 0x1C, &emu_size, 4);
2016-07-13 19:59:36 +02:00
memcpy(mbr + 0x1BE, mbrdata, 0x42);
2017-03-13 20:20:20 +01:00
if (hidden_mb) memcpy(mbr, "GATEWAYNAND", 12); // legacy
2016-07-13 19:59:36 +02:00
else memset(mbr + 0x1CE, 0, 0x10);
// one last warning....
2017-02-22 23:27:34 +01:00
// 0:/Nintendo 3DS/ write permission is ignored here, this warning is enough
if (!ShowUnlockSequence(5, "!WARNING!\n \nProceeding will format this SD.\nThis will irreversibly delete\nALL data on it."))
2016-07-13 19:59:36 +02:00
return false;
ShowString("Formatting SD, please wait...");
2016-07-13 19:59:36 +02:00
// write the MBR to disk
// !this assumes a fully deinitialized file system!
if ((sdmmc_sdcard_init() != 0) || (sdmmc_sdcard_writesectors(0, 1, mbr) != 0) ||
(emu_size && ((sdmmc_nand_readsectors(0, 1, ncsd) != 0) || (sdmmc_sdcard_writesectors(1, 1, ncsd) != 0)))) {
2016-12-08 13:18:25 +01:00
ShowPrompt(false, "Error: SD card i/o failure");
2016-07-13 19:59:36 +02:00
return false;
}
// format the SD card
2017-04-24 01:57:34 +02:00
VolToPart[0].pt = 1; // workaround to prevent FatFS rebuilding the MBR
2016-12-10 15:32:03 +01:00
InitSDCardFS();
UINT c_size = cluster_size;
bool ret = ((f_mkfs("0:", FM_FAT32, c_size, MAIN_BUFFER, MAIN_BUFFER_SIZE) == FR_OK) ||
(f_mkfs("0:", FM_FAT32, 0, MAIN_BUFFER, MAIN_BUFFER_SIZE) == FR_OK)) &&
(f_setlabel((label) ? label : "0:GM9SD") == FR_OK);
2016-12-10 15:32:03 +01:00
DeinitSDCardFS();
2017-04-24 01:57:34 +02:00
VolToPart[0].pt = 0; // revert workaround to prevent SD mount problems
2016-07-13 19:59:36 +02:00
return ret;
}
bool SetupBonusDrive(void) {
2017-02-22 18:44:39 +01:00
if (!ShowUnlockSequence(3, "Format the bonus drive?\nThis will irreversibly delete\nALL data on it."))
return false;
ShowString("Formatting drive, please wait...");
if (GetMountState() & IMG_NAND) InitImgFS(NULL);
bool ret = (f_mkfs("8:", FM_ANY, 0, MAIN_BUFFER, MAIN_BUFFER_SIZE) == FR_OK);
if (ret) {
f_setlabel("8:BONUS");
InitExtFS();
}
return ret;
}
2016-12-13 17:10:39 +01:00
bool FileUnlock(const char* path) {
FIL file;
2016-12-10 15:32:03 +01:00
if (!(DriveType(path) & DRV_FAT)) return true; // can't really check this
2016-12-13 17:10:39 +01:00
if (fx_open(&file, path, FA_READ | FA_OPEN_EXISTING) != FR_OK) {
char pathstr[32 + 1];
TruncateString(pathstr, path, 32, 8);
if (GetMountState() && (strncmp(path, GetMountPath(), 256) == 0) &&
(ShowPrompt(true, "%s\nFile is currently mounted.\nUnmount to unlock?", pathstr))) {
InitImgFS(NULL);
if (fx_open(&file, path, FA_READ | FA_OPEN_EXISTING) != FR_OK)
return false;
} else return false;
}
fx_close(&file);
return true;
}
2017-06-26 01:44:16 +02:00
bool FileSetData(const char* path, const void* data, size_t size, size_t foffset, bool create) {
2017-01-27 18:32:52 +01:00
UINT bw;
2016-05-27 01:21:05 +02:00
if (!CheckWritePermissions(path)) return false;
2017-01-27 18:32:52 +01:00
if ((DriveType(path) & DRV_FAT) && create) f_unlink(path);
return (fvx_qwrite(path, data, foffset, size, &bw) == FR_OK) && (bw == size);
2016-02-26 19:43:30 +01:00
}
2017-06-26 01:44:16 +02:00
size_t FileGetData(const char* path, void* data, size_t size, size_t foffset) {
2017-01-27 18:32:52 +01:00
UINT br;
if (fvx_qread(path, data, foffset, size, &br) != FR_OK) br = 0;
return br;
2016-03-11 01:29:14 +01:00
}
2016-04-10 15:55:39 +02:00
size_t FileGetSize(const char* path) {
2017-06-06 21:14:19 +02:00
FILINFO fno;
if (fvx_stat(path, &fno) != FR_OK)
return 0;
return fno.fsize;
2016-04-10 15:55:39 +02:00
}
bool FileGetSha256(const char* path, u8* sha256) {
bool ret = true;
2017-01-27 18:32:52 +01:00
FIL file;
u64 fsize;
2017-01-27 18:32:52 +01:00
if (fvx_open(&file, path, FA_READ | FA_OPEN_EXISTING) != FR_OK)
return false;
fsize = fvx_size(&file);
fvx_lseek(&file, 0);
ShowProgress(0, 0, path);
2017-01-27 18:32:52 +01:00
sha_init(SHA256_MODE);
for (u64 pos = 0; (pos < fsize) && ret; pos += MAIN_BUFFER_SIZE) {
UINT bytes_read = 0;
if (fvx_read(&file, MAIN_BUFFER, MAIN_BUFFER_SIZE, &bytes_read) != FR_OK)
ret = false;
if (!ShowProgress(pos + bytes_read, fsize, path))
ret = false;
sha_update(MAIN_BUFFER, bytes_read);
}
2017-01-27 18:32:52 +01:00
sha_get(sha256);
fvx_close(&file);
ShowProgress(1, 1, path);
return ret;
}
2016-12-11 15:40:44 +01:00
u32 FileFindData(const char* path, u8* data, u32 size_data, u32 offset_file) {
2017-01-27 18:32:52 +01:00
FIL file; // used for FAT & virtual
u64 found = (u64) -1;
u64 fsize = FileGetSize(path);
2017-01-27 18:32:52 +01:00
if (fvx_open(&file, path, FA_READ | FA_OPEN_EXISTING) != FR_OK)
return found;
2016-12-11 15:40:44 +01:00
// main routine
for (u32 pass = 0; pass < 2; pass++) {
bool show_progress = false;
u64 pos = (pass == 0) ? offset_file : 0;
u64 search_end = (pass == 0) ? fsize : offset_file + size_data;
search_end = (search_end > fsize) ? fsize : search_end;
for (; (pos < search_end) && (found == (u64) -1); pos += MAIN_BUFFER_SIZE - (size_data - 1)) {
UINT read_bytes = min(MAIN_BUFFER_SIZE, search_end - pos);
2017-01-27 18:32:52 +01:00
UINT btr;
fvx_lseek(&file, pos);
if ((fvx_read(&file, MAIN_BUFFER, read_bytes, &btr) != FR_OK) || (btr != read_bytes))
break;
2016-12-11 15:40:44 +01:00
for (u32 i = 0; i + size_data <= read_bytes; i++) {
if (memcmp(MAIN_BUFFER + i, data, size_data) == 0) {
found = pos + i;
break;
}
}
if (!show_progress && (found == (u64) -1) && (pos + read_bytes < fsize)) {
ShowProgress(0, 0, path);
show_progress = true;
}
if (show_progress && (!ShowProgress(pos + read_bytes, fsize, path)))
break;
}
}
2017-01-27 18:32:52 +01:00
fvx_close(&file);
return found;
}
2017-06-09 01:45:00 +02:00
bool FileInjectFile(const char* dest, const char* orig, u64 off_dest, u64 off_orig, u64 size, u32* flags) {
2016-06-17 19:28:43 +02:00
FIL ofile;
FIL dfile;
if (!CheckWritePermissions(dest)) return false;
if (strncmp(dest, orig, 256) == 0) {
ShowPrompt(false, "Error: Can't inject file into itself");
return false;
}
2016-06-17 19:28:43 +02:00
2017-01-27 18:32:52 +01:00
// open destination / origin
if (fvx_open(&dfile, dest, FA_WRITE | FA_OPEN_EXISTING) != FR_OK)
return false;
if ((fvx_open(&ofile, orig, FA_READ | FA_OPEN_EXISTING) != FR_OK) &&
(!FileUnlock(orig) || (fvx_open(&ofile, orig, FA_READ | FA_OPEN_EXISTING) != FR_OK))) {
fvx_close(&dfile);
return false;
2016-06-17 19:28:43 +02:00
}
2017-06-09 01:45:00 +02:00
fvx_lseek(&dfile, off_dest);
fvx_lseek(&ofile, off_orig);
if (!size && (off_orig < fvx_size(&ofile)))
size = fvx_size(&ofile) - off_orig;
2016-06-17 19:28:43 +02:00
// check file limits
2017-06-09 01:45:00 +02:00
if (off_dest + size > fvx_size(&dfile)) {
2016-06-17 19:28:43 +02:00
ShowPrompt(false, "Operation would write beyond end of file");
2017-01-27 18:32:52 +01:00
fvx_close(&dfile);
fvx_close(&ofile);
2016-06-17 19:28:43 +02:00
return false;
2017-06-09 01:45:00 +02:00
} else if (off_orig + size > fvx_size(&ofile)) {
ShowPrompt(false, "Not enough data in file");
fvx_close(&dfile);
fvx_close(&ofile);
return false;
2016-06-17 19:28:43 +02:00
}
2017-06-09 01:45:00 +02:00
bool ret = true;
2016-06-17 19:49:19 +02:00
ShowProgress(0, 0, orig);
2017-06-09 01:45:00 +02:00
for (u64 pos = 0; (pos < size) && ret; pos += MAIN_BUFFER_SIZE) {
UINT read_bytes = min(MAIN_BUFFER_SIZE, size - pos);
2016-06-17 19:28:43 +02:00
UINT bytes_read = read_bytes;
UINT bytes_written = read_bytes;
2017-06-09 01:45:00 +02:00
if ((fvx_read(&ofile, MAIN_BUFFER, read_bytes, &bytes_read) != FR_OK) ||
2017-01-27 18:32:52 +01:00
(fvx_write(&dfile, MAIN_BUFFER, read_bytes, &bytes_written) != FR_OK) ||
(bytes_read != bytes_written))
2016-06-17 19:28:43 +02:00
ret = false;
2017-06-09 01:45:00 +02:00
if (ret && !ShowProgress(pos + bytes_read, size, orig)) {
if (flags && (*flags & NO_CANCEL)) {
ShowPrompt(false, "Cancel is now allowed here");
ShowProgress(0, 0, orig);
ShowProgress(pos + bytes_read, size, orig);
} else ret = false;
}
2016-06-17 19:28:43 +02:00
}
ShowProgress(1, 1, orig);
2017-01-27 18:32:52 +01:00
fvx_close(&dfile);
fvx_close(&ofile);
2016-06-17 19:28:43 +02:00
return ret;
}
2017-02-28 16:46:24 +01:00
bool DirCreate(const char* cpath, const char* dirname) {
char npath[256]; // 256 is the maximum length of a full path
if (!CheckWritePermissions(cpath)) return false;
snprintf(npath, 255, "%s/%s", cpath, dirname);
return (fa_mkdir(npath) == FR_OK);
}
bool DirInfoWorker(char* fpath, bool virtual, u64* tsize, u32* tdirs, u32* tfiles) {
char* fname = fpath + strnlen(fpath, 256 - 1);
bool ret = true;
if (virtual) {
VirtualDir vdir;
VirtualFile vfile;
if (!GetVirtualDir(&vdir, fpath)) return false; // get dir reader object
while (ReadVirtualDir(&vfile, &vdir)) {
if (vfile.flags & VFLAG_DIR) {
(*tdirs)++;
*(fname++) = '/';
GetVirtualFilename(fname, &vfile, (256 - 1) - (fname - fpath));
if (!DirInfoWorker(fpath, virtual, tsize, tdirs, tfiles)) ret = false;
*(--fname) = '\0';
} else {
*tsize += vfile.size;
(*tfiles)++;
}
}
} else {
DIR pdir;
FILINFO fno;
if (fa_opendir(&pdir, fpath) != FR_OK) return false; // get dir reader object
while (f_readdir(&pdir, &fno) == FR_OK) {
if ((strncmp(fno.fname, ".", 2) == 0) || (strncmp(fno.fname, "..", 3) == 0))
continue; // filter out virtual entries
if (fno.fname[0] == 0) break; // end of dir
if (fno.fattrib & AM_DIR) {
(*tdirs)++;
*(fname++) = '/';
strncpy(fname, fno.fname, (256 - 1) - (fname - fpath));
if (!DirInfoWorker(fpath, virtual, tsize, tdirs, tfiles)) ret = false;
*(--fname) = '\0';
} else {
*tsize += fno.fsize;
(*tfiles)++;
}
}
f_closedir(&pdir);
}
return ret;
}
bool DirInfo(const char* path, u64* tsize, u32* tdirs, u32* tfiles) {
bool virtual = (DriveType(path) & DRV_VIRTUAL);
char fpath[256];
strncpy(fpath, path, 255);
*tsize = *tdirs = *tfiles = 0;
ShowString("Analyzing dir, please wait...");
bool res = DirInfoWorker(fpath, virtual, tsize, tdirs, tfiles);
return res;
}
bool PathExist(const char* path) {
return (fvx_stat(path, NULL) == FR_OK);
}
2017-06-09 16:21:41 +02:00
bool PathMoveCopyRec(char* dest, char* orig, u32* flags, bool move) {
bool to_virtual = GetVirtualSource(dest);
2017-06-09 01:45:00 +02:00
bool silent = (flags && (*flags & SILENT));
2017-06-09 16:21:41 +02:00
bool ret = false;
2016-11-30 15:56:22 +01:00
2017-06-09 16:21:41 +02:00
// check destination write permission (special paths only)
if (((*dest == '1') || (strncmp(dest, "0:/Nintendo 3DS", 16) == 0)) &&
(!flags || !(*flags & OVERRIDE_PERM)) &&
!CheckWritePermissions(dest)) return false;
2016-11-30 15:56:22 +01:00
FILINFO fno;
2017-06-06 21:14:19 +02:00
if (fvx_stat(orig, &fno) != FR_OK) return false; // origin does not exist
2017-06-09 16:21:41 +02:00
if (move && (to_virtual || fno.fattrib & AM_VRT)) return false; // trying to move a virtual file
// path string (for output)
char deststr[36 + 1];
TruncateString(deststr, dest, 36, 8);
// the copy process takes place here
2017-06-09 01:45:00 +02:00
if (!ShowProgress(0, 0, orig) && !(flags && (*flags & NO_CANCEL))) return false;
2017-06-09 16:21:41 +02:00
if (move && fvx_stat(dest, NULL) != FR_OK) { // moving if dest not existing
ret = (fvx_rename(orig, dest) == FR_OK);
2016-04-29 02:21:36 +02:00
} else if (fno.fattrib & AM_DIR) { // processing folders (same for move & copy)
DIR pdir;
char* fname = orig + strnlen(orig, 256);
2016-03-02 17:22:44 +01:00
// create the destination folder if it does not already exist
2017-06-09 16:21:41 +02:00
if (fvx_opendir(&pdir, dest) != FR_OK) {
if (fvx_mkdir(dest) != FR_OK) {
2017-06-09 01:45:00 +02:00
if (!silent) ShowPrompt(false, "%s\nError: Overwriting file with dir", deststr);
return false;
}
2017-06-09 16:21:41 +02:00
} else fvx_closedir(&pdir);
2017-06-06 21:14:19 +02:00
if (fvx_opendir(&pdir, orig) != FR_OK)
return false;
*(fname++) = '/';
2017-06-06 21:14:19 +02:00
while (fvx_readdir(&pdir, &fno) == FR_OK) {
if ((strncmp(fno.fname, ".", 2) == 0) || (strncmp(fno.fname, "..", 3) == 0))
continue; // filter out virtual entries
2016-07-20 00:27:38 +02:00
strncpy(fname, fno.fname, 256 - (fname - orig));
if (fno.fname[0] == 0) {
ret = true;
break;
2017-06-09 16:21:41 +02:00
} else {
// recursively process directory
char* oname = strrchr(orig, '/');
char* dname = dest + strnlen(dest, 255);
if (oname == NULL) return false; // not a proper origin path
*(dname++) = '/';
strncpy(dname, oname++, 256 - (dname - dest));
bool res = PathMoveCopyRec(dest, orig, flags, move);
*(--dname) = '\0';
if (!res) break;
}
}
2017-06-06 21:14:19 +02:00
fvx_closedir(&pdir);
2016-04-29 02:21:36 +02:00
} else if (move) { // moving if destination exists
2017-06-09 16:21:41 +02:00
if (fvx_stat(dest, &fno) != FR_OK) return false;
2016-04-29 02:21:36 +02:00
if (fno.fattrib & AM_DIR) {
2017-06-09 01:45:00 +02:00
if (!silent) ShowPrompt(false, "%s\nError: Overwriting dir with file", deststr);
2016-04-29 02:21:36 +02:00
return false;
}
2017-06-09 16:21:41 +02:00
if (fvx_unlink(dest) != FR_OK) return false;
ret = (fvx_rename(orig, dest) == FR_OK);
2016-04-29 02:21:36 +02:00
} else { // copying files
FIL ofile;
FIL dfile;
u64 fsize;
2017-06-06 21:14:19 +02:00
if (fvx_open(&ofile, orig, FA_READ | FA_OPEN_EXISTING) != FR_OK) {
if (!FileUnlock(orig) || (fvx_open(&ofile, orig, FA_READ | FA_OPEN_EXISTING) != FR_OK))
2016-12-13 17:10:39 +01:00
return false;
ShowProgress(0, 0, orig); // reinit progress bar
}
2017-06-06 21:14:19 +02:00
fsize = fvx_size(&ofile);
2017-06-09 16:21:41 +02:00
if (!to_virtual && (GetFreeSpace(dest) < fsize)) {
2017-06-09 01:45:00 +02:00
if (!silent) ShowPrompt(false, "%s\nError: Not enough space in drive", deststr);
2017-06-09 16:21:41 +02:00
fvx_close(&ofile);
return false;
}
2017-06-09 16:21:41 +02:00
if (fvx_open(&dfile, dest, FA_WRITE | FA_CREATE_ALWAYS) != FR_OK) {
2017-06-09 01:45:00 +02:00
if (!silent) ShowPrompt(false, "%s\nError: Cannot open destination file", deststr);
2017-06-09 16:21:41 +02:00
fvx_close(&ofile);
return false;
}
2017-06-09 16:21:41 +02:00
if (to_virtual && (fvx_size(&dfile) < fsize)) {
2017-06-09 01:45:00 +02:00
if (!silent) ShowPrompt(false, "%s\nError: Not enough virtual space", deststr);
2017-06-09 16:21:41 +02:00
fvx_close(&ofile);
fvx_close(&dfile);
return false;
}
fvx_lseek(&dfile, 0);
fvx_sync(&dfile);
2017-06-06 21:14:19 +02:00
fvx_lseek(&ofile, 0);
fvx_sync(&ofile);
ret = true;
2017-02-28 17:58:48 +01:00
if (flags && (*flags & CALC_SHA)) sha_init(SHA256_MODE);
for (u64 pos = 0; (pos < fsize) && ret; pos += MAIN_BUFFER_SIZE) {
UINT bytes_read = 0;
2016-03-02 17:22:44 +01:00
UINT bytes_written = 0;
2017-06-09 16:21:41 +02:00
if ((fvx_read(&ofile, MAIN_BUFFER, MAIN_BUFFER_SIZE, &bytes_read) != FR_OK) ||
(fvx_write(&dfile, MAIN_BUFFER, bytes_read, &bytes_written) != FR_OK) ||
2017-06-09 01:45:00 +02:00
(bytes_read != bytes_written))
ret = false;
2017-06-09 01:45:00 +02:00
if (ret && !ShowProgress(pos + bytes_read, fsize, orig)) {
if (flags && (*flags & NO_CANCEL)) {
ShowPrompt(false, "Cancel is now allowed here");
ShowProgress(0, 0, orig);
ShowProgress(pos + bytes_read, fsize, orig);
} else ret = false;
}
2017-02-28 17:58:48 +01:00
if (flags && (*flags & CALC_SHA))
sha_update(MAIN_BUFFER, bytes_read);
}
2016-03-02 17:22:44 +01:00
ShowProgress(1, 1, orig);
2017-06-06 21:14:19 +02:00
fvx_close(&ofile);
2017-06-09 16:21:41 +02:00
fvx_close(&dfile);
if (!ret) fvx_unlink(dest);
else if (!to_virtual && flags && (*flags & CALC_SHA)) {
2017-02-28 17:58:48 +01:00
u8 sha256[0x20];
char* ext_sha = dest + strnlen(dest, 256);
strncpy(ext_sha, ".sha", 256 - (ext_sha - dest));
sha_get(sha256);
FileSetData(dest, sha256, 0x20, 0, true);
}
}
return ret;
}
2017-06-09 16:21:41 +02:00
bool PathMoveCopy(const char* dest, const char* orig, u32* flags, bool move) {
// check permissions
if (!flags || !(*flags & OVERRIDE_PERM)) {
if (!CheckWritePermissions(dest)) return false;
if (move && !CheckDirWritePermissions(orig)) return false;
}
// reset local flags
if (flags) *flags = *flags & ~(SKIP_CUR|OVERWRITE_CUR);
// preparations
int ddrvtype = DriveType(dest);
int odrvtype = DriveType(orig);
2017-06-09 16:21:41 +02:00
char ldest[256]; // 256 is the maximum length of a full path
char lorig[256];
strncpy(ldest, dest, 255);
strncpy(lorig, orig, 255);
char deststr[36 + 1];
TruncateString(deststr, ldest, 36, 8);
// moving only for regular FAT drives (= not alias drives)
if (move && !(ddrvtype & odrvtype & DRV_STDFAT)) {
ShowPrompt(false, "Error: Only FAT files can be moved");
return false;
}
// is destination part of origin?
u32 olen = strnlen(lorig, 255);
if ((strncmp(ldest, lorig, olen) == 0) && (ldest[olen] == '/')) {
ShowPrompt(false, "%s\nError: Destination is part of origin", deststr);
return false;
}
if (!(ddrvtype & DRV_VIRTUAL)) { // FAT destination handling
// get destination name
char* dname = strrchr(ldest, '/');
if (!dname) return false;
dname++;
// check & fix destination == origin
while (strncmp(ldest, lorig, 255) == 0) {
if (!ShowStringPrompt(dname, 255 - (dname - ldest), "%s\nDestination equals origin\nChoose another name?", deststr))
return false;
}
// check if destination exists
if (flags && !(*flags & (OVERWRITE_CUR|OVERWRITE_ALL)) && (fa_stat(ldest, NULL) == FR_OK)) {
if (*flags & SKIP_ALL) {
*flags |= SKIP_CUR;
return true;
}
const char* optionstr[5] =
{"Choose new name", "Overwrite file(s)", "Skip file(s)", "Overwrite all", "Skip all"};
u32 user_select = ShowSelectPrompt((*flags & ASK_ALL) ? 5 : 3, optionstr,
"Destination already exists:\n%s", deststr);
if (user_select == 1) {
do {
if (!ShowStringPrompt(dname, 255 - (dname - ldest), "Choose new destination name"))
return false;
} while (fa_stat(ldest, NULL) == FR_OK);
} else if (user_select == 2) {
*flags |= OVERWRITE_CUR;
} else if (user_select == 3) {
*flags |= SKIP_CUR;
return true;
} else if (user_select == 4) {
*flags |= OVERWRITE_ALL;
} else if (user_select == 5) {
*flags |= (SKIP_CUR|SKIP_ALL);
return true;
} else {
return false;
}
}
// ensure the destination path exists
if (flags && (*flags & BUILD_PATH)) fvx_rmkpath(ldest);
// actual move / copy operation
bool same_drv = (strncmp(lorig, ldest, 2) == 0);
2017-06-09 01:45:00 +02:00
bool res = PathMoveCopyRec(ldest, lorig, flags, move && same_drv);
if (move && res && (!flags || !(*flags&SKIP_CUR))) PathDelete(lorig);
2017-06-09 16:21:41 +02:00
return res;
} else { // virtual destination handling
// can't write an SHA file to a virtual destination
if (flags) *flags |= ~CALC_SHA;
// prevent illegal operations
if (odrvtype & ddrvtype & (DRV_SYSNAND|DRV_EMUNAND|DRV_IMAGE)) {
ShowPrompt(false, "Copy operation is not allowed");
2017-06-09 16:21:41 +02:00
return false;
2016-11-30 15:56:22 +01:00
}
2017-06-09 16:21:41 +02:00
// check destination == origin
if (strncmp(ldest, lorig, 255) == 0) {
ShowPrompt(false, "%s\nDestination equals origin", deststr);
return false;
}
// actual virtual copy operation
DismountDriveType(DriveType(ldest)&(DRV_SYSNAND|DRV_EMUNAND|DRV_IMAGE));
bool res = PathMoveCopyRec(ldest, lorig, flags, false);
InitExtFS();
return res;
}
2016-04-29 02:21:36 +02:00
}
2017-06-09 16:21:41 +02:00
bool PathCopy(const char* destdir, const char* orig, u32* flags) {
// build full destination path (on top of destination directory)
char dest[256]; // maximum path name length in FAT
char* oname = strrchr(orig, '/');
if (oname == NULL) return false; // not a proper origin path
snprintf(dest, 255, "%s/%s", destdir, (++oname));
// virtual destination special handling
if (GetVirtualSource(destdir)) {
u64 osize = FileGetSize(orig);
VirtualFile dvfile;
if (!osize) return false;
if (!GetVirtualFile(&dvfile, dest)) {
VirtualDir vdir;
if (!GetVirtualDir(&vdir, destdir)) return false;
while (true) { // search by size should be a last resort solution
if (!ReadVirtualDir(&dvfile, &vdir)) return false;
if (dvfile.size == osize) break; // file found
}
if (!ShowPrompt(true, "Entry not found: %s\nInject into %s instead?", dest, dvfile.name))
return false;
snprintf(dest, 255, "%s/%s", destdir, dvfile.name);
} else if (osize < dvfile.size) { // if origin is smaller than destination...
char deststr[36 + 1];
char origstr[36 + 1];
char osizestr[32];
char dsizestr[32];
TruncateString(deststr, dest, 36, 8);
TruncateString(origstr, orig, 36, 8);
FormatBytes(osizestr, osize);
FormatBytes(dsizestr, dvfile.size);
if (dvfile.size > osize) {
if (!ShowPrompt(true, "File smaller than available space:\n%s (%s)\n%s (%s)\nContinue?", origstr, osizestr, deststr, dsizestr))
return false;
}
}
2016-03-21 23:19:23 +01:00
}
2017-06-09 16:21:41 +02:00
return PathMoveCopy(dest, orig, flags, false);
}
bool PathMove(const char* destdir, const char* orig, u32* flags) {
// build full destination path (on top of destination directory)
char dest[256]; // maximum path name length in FAT
char* oname = strrchr(orig, '/');
if (oname == NULL) return false; // not a proper origin path
snprintf(dest, 255, "%s/%s", destdir, (++oname));
return PathMoveCopy(dest, orig, flags, true);
}
bool PathDelete(const char* path) {
2017-02-15 16:34:25 +01:00
if (!CheckDirWritePermissions(path)) return false;
2017-06-06 21:14:19 +02:00
return (fvx_runlink(path) == FR_OK);
}
2016-03-14 23:38:43 +01:00
bool PathRename(const char* path, const char* newname) {
char npath[256]; // 256 is the maximum length of a full path
char* oldname = strrchr(path, '/');
2017-02-15 16:34:25 +01:00
if (!CheckDirWritePermissions(path)) return false;
2016-03-14 23:38:43 +01:00
if (!oldname) return false;
oldname++;
strncpy(npath, path, oldname - path);
strncpy(npath + (oldname - path), newname, 255 - (oldname - path));
2016-11-25 21:31:57 +01:00
return (f_rename(path, npath) == FR_OK);
2016-03-14 23:38:43 +01:00
}
bool FileSelector(char* result, const char* text, const char* path, const char* pattern, bool hide_ext, bool no_dirs) {
DirStruct* contents = (DirStruct*) (void*) TEMP_BUFFER;
char path_local[256];
strncpy(path_local, path, 256);
// main loop
while (true) {
u32 n_found = 0;
u32 pos = 0;
GetDirContents(contents, path_local);
while (pos < contents->n_entries) {
char opt_names[_MAX_FS_OPT+1][32+1];
DirEntry* res_entry[_MAX_FS_OPT+1] = { NULL };
u32 n_opt = 0;
for (; pos < contents->n_entries; pos++) {
DirEntry* entry = &(contents->entry[pos]);
if (!((entry->type == T_DIR) && (!no_dirs)) &&
!((entry->type == T_FILE) && (fvx_match_name(entry->name, pattern) == FR_OK)))
continue;
if (n_opt == _MAX_FS_OPT) {
snprintf(opt_names[n_opt++], 32, "[more...]");
break;
}
char temp_str[256];
snprintf(temp_str, 256, entry->name);
if (hide_ext && (entry->type == T_FILE)) {
char* dot = strrchr(temp_str, '.');
if (dot) *dot = '\0';
}
TruncateString(opt_names[n_opt], temp_str, 32, 8);
res_entry[n_opt++] = entry;
n_found++;
}
if ((pos >= contents->n_entries) && (n_opt < n_found))
snprintf(opt_names[n_opt++], 32, "[more...]");
if (!n_opt) break;
const char* optionstr[_MAX_FS_OPT+1] = { NULL };
for (u32 i = 0; i <= _MAX_FS_OPT; i++) optionstr[i] = opt_names[i];
u32 user_select = ShowSelectPrompt(n_opt, optionstr, text);
if (!user_select) return false;
DirEntry* res_local = res_entry[user_select-1];
if (res_local && (res_local->type == T_DIR)) { // selected dir
if (FileSelector(result, text, res_local->path, pattern, hide_ext, no_dirs))
return true;
break;
} else if (res_local && (res_local->type == T_FILE)) { // selected file
strncpy(result, res_local->path, 256);
return true;
}
}
if (!n_found) { // not a single matching entry found
char pathstr[32+1];
TruncateString(pathstr, path_local, 32, 8);
ShowPrompt(false, "%s\nNo usable entries found.", pathstr);
return false;
}
}
}
void CreateScreenshot() {
2016-02-26 19:43:30 +01:00
const u8 bmp_header[54] = {
0x42, 0x4D, 0x36, 0xCA, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x36, 0x00, 0x00, 0x00, 0x28, 0x00,
0x00, 0x00, 0x90, 0x01, 0x00, 0x00, 0xE0, 0x01, 0x00, 0x00, 0x01, 0x00, 0x18, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0xCA, 0x08, 0x00, 0x12, 0x0B, 0x00, 0x00, 0x12, 0x0B, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
2016-03-21 19:24:06 +01:00
u8* buffer = MAIN_BUFFER + 54;
2016-02-26 19:43:30 +01:00
u8* buffer_t = buffer + (400 * 240 * 3);
char filename[16];
static u32 n = 0;
for (; n < 1000; n++) {
snprintf(filename, 16, "0:/snap%03i.bmp", (int) n);
if (fa_stat(filename, NULL) != FR_OK) break;
2016-02-26 19:43:30 +01:00
}
if (n >= 1000) return;
2016-03-21 19:24:06 +01:00
memcpy(MAIN_BUFFER, bmp_header, 54);
2016-02-26 19:43:30 +01:00
memset(buffer, 0x1F, 400 * 240 * 3 * 2);
for (u32 x = 0; x < 400; x++)
for (u32 y = 0; y < 240; y++)
memcpy(buffer_t + (y*400 + x) * 3, TOP_SCREEN + (x*240 + y) * 3, 3);
2016-02-26 19:43:30 +01:00
for (u32 x = 0; x < 320; x++)
for (u32 y = 0; y < 240; y++)
memcpy(buffer + (y*400 + x + 40) * 3, BOT_SCREEN + (x*240 + y) * 3, 3);
2016-06-17 17:12:11 +02:00
FileSetData(filename, MAIN_BUFFER, 54 + (400 * 240 * 3 * 2), 0, true);
2016-02-26 19:43:30 +01:00
}