Fix file operations for very large files

This commit is contained in:
d0k3 2017-01-21 21:31:38 +01:00
parent cc35599b08
commit 40afd65e0e

View File

@ -160,14 +160,14 @@ bool FileGetSha256(const char* path, u8* sha256) {
ShowProgress(0, 0, path); ShowProgress(0, 0, path);
if (DriveType(path) & DRV_VIRTUAL) { // for virtual files if (DriveType(path) & DRV_VIRTUAL) { // for virtual files
VirtualFile vfile; VirtualFile vfile;
u32 fsize; u64 fsize;
if (!GetVirtualFile(&vfile, path)) if (!GetVirtualFile(&vfile, path))
return false; return false;
fsize = vfile.size; fsize = vfile.size;
sha_init(SHA256_MODE); sha_init(SHA256_MODE);
for (size_t pos = 0; (pos < fsize) && ret; pos += MAIN_BUFFER_SIZE) { for (u64 pos = 0; (pos < fsize) && ret; pos += MAIN_BUFFER_SIZE) {
UINT read_bytes = min(MAIN_BUFFER_SIZE, fsize - pos); UINT read_bytes = min(MAIN_BUFFER_SIZE, fsize - pos);
if (ReadVirtualFile(&vfile, MAIN_BUFFER, pos, read_bytes, NULL) != 0) if (ReadVirtualFile(&vfile, MAIN_BUFFER, pos, read_bytes, NULL) != 0)
ret = false; ret = false;
@ -178,7 +178,7 @@ bool FileGetSha256(const char* path, u8* sha256) {
sha_get(sha256); sha_get(sha256);
} else { // for regular FAT files } else { // for regular FAT files
FIL file; FIL file;
size_t fsize; u64 fsize;
if (fx_open(&file, path, FA_READ | FA_OPEN_EXISTING) != FR_OK) if (fx_open(&file, path, FA_READ | FA_OPEN_EXISTING) != FR_OK)
return false; return false;
@ -187,7 +187,7 @@ bool FileGetSha256(const char* path, u8* sha256) {
f_sync(&file); f_sync(&file);
sha_init(SHA256_MODE); sha_init(SHA256_MODE);
for (size_t pos = 0; (pos < fsize) && ret; pos += MAIN_BUFFER_SIZE) { for (u64 pos = 0; (pos < fsize) && ret; pos += MAIN_BUFFER_SIZE) {
UINT bytes_read = 0; UINT bytes_read = 0;
if (fx_read(&file, MAIN_BUFFER, MAIN_BUFFER_SIZE, &bytes_read) != FR_OK) if (fx_read(&file, MAIN_BUFFER, MAIN_BUFFER_SIZE, &bytes_read) != FR_OK)
ret = false; ret = false;
@ -205,8 +205,8 @@ bool FileGetSha256(const char* path, u8* sha256) {
u32 FileFindData(const char* path, u8* data, u32 size_data, u32 offset_file) { u32 FileFindData(const char* path, u8* data, u32 size_data, u32 offset_file) {
int drvtype = DriveType(path); int drvtype = DriveType(path);
u32 found = (u32) -1; u64 found = (u64) -1;
u32 fsize = FileGetSize(path); u64 fsize = FileGetSize(path);
// open FAT / virtual file // open FAT / virtual file
FIL file; // only used on FAT drives FIL file; // only used on FAT drives
@ -217,10 +217,10 @@ u32 FileFindData(const char* path, u8* data, u32 size_data, u32 offset_file) {
// main routine // main routine
for (u32 pass = 0; pass < 2; pass++) { for (u32 pass = 0; pass < 2; pass++) {
bool show_progress = false; bool show_progress = false;
u32 pos = (pass == 0) ? offset_file : 0; u64 pos = (pass == 0) ? offset_file : 0;
u32 search_end = (pass == 0) ? fsize : offset_file + size_data; u64 search_end = (pass == 0) ? fsize : offset_file + size_data;
search_end = (search_end > fsize) ? fsize : search_end; search_end = (search_end > fsize) ? fsize : search_end;
for (; (pos < search_end) && (found == (u32) -1); pos += MAIN_BUFFER_SIZE - (size_data - 1)) { for (; (pos < search_end) && (found == (u64) -1); pos += MAIN_BUFFER_SIZE - (size_data - 1)) {
UINT read_bytes = min(MAIN_BUFFER_SIZE, search_end - pos); UINT read_bytes = min(MAIN_BUFFER_SIZE, search_end - pos);
if (drvtype & DRV_FAT) { if (drvtype & DRV_FAT) {
UINT btr; UINT btr;
@ -238,7 +238,7 @@ u32 FileFindData(const char* path, u8* data, u32 size_data, u32 offset_file) {
break; break;
} }
} }
if (!show_progress && (found == (u32) -1) && (pos + read_bytes < fsize)) { if (!show_progress && (found == (u64) -1) && (pos + read_bytes < fsize)) {
ShowProgress(0, 0, path); ShowProgress(0, 0, path);
show_progress = true; show_progress = true;
} }
@ -256,8 +256,8 @@ bool FileInjectFile(const char* dest, const char* orig, u32 offset) {
VirtualFile ovfile; VirtualFile ovfile;
FIL ofile; FIL ofile;
FIL dfile; FIL dfile;
size_t osize; u64 osize;
size_t dsize; u64 dsize;
bool vdest; bool vdest;
bool vorig; bool vorig;
@ -314,7 +314,7 @@ bool FileInjectFile(const char* dest, const char* orig, u32 offset) {
ret = true; ret = true;
ShowProgress(0, 0, orig); ShowProgress(0, 0, orig);
for (size_t pos = 0; (pos < osize) && ret; pos += MAIN_BUFFER_SIZE) { for (u64 pos = 0; (pos < osize) && ret; pos += MAIN_BUFFER_SIZE) {
UINT read_bytes = min(MAIN_BUFFER_SIZE, osize - pos); UINT read_bytes = min(MAIN_BUFFER_SIZE, osize - pos);
UINT bytes_read = read_bytes; UINT bytes_read = read_bytes;
UINT bytes_written = read_bytes; UINT bytes_written = read_bytes;
@ -354,7 +354,7 @@ bool PathCopyVrtToVrt(const char* destdir, const char* orig) {
if (!GetVirtualFile(&dvfile, dest) || !GetVirtualFile(&ovfile, orig)) if (!GetVirtualFile(&dvfile, dest) || !GetVirtualFile(&ovfile, orig))
return false; return false;
u32 osize = ovfile.size; u64 osize = ovfile.size;
if (dvfile.size != osize) { // almost impossible, but so what... if (dvfile.size != osize) { // almost impossible, but so what...
ShowPrompt(false, "Virtual file size mismatch:\n%s\n%s", origstr, deststr); ShowPrompt(false, "Virtual file size mismatch:\n%s\n%s", origstr, deststr);
return false; return false;
@ -368,7 +368,7 @@ bool PathCopyVrtToVrt(const char* destdir, const char* orig) {
// unmount critical NAND drives // unmount critical NAND drives
DismountDriveType(DriveType(destdir)&(DRV_SYSNAND|DRV_EMUNAND|DRV_IMAGE)); DismountDriveType(DriveType(destdir)&(DRV_SYSNAND|DRV_EMUNAND|DRV_IMAGE));
if (!ShowProgress(0, 0, orig)) ret = false; if (!ShowProgress(0, 0, orig)) ret = false;
for (size_t pos = 0; (pos < osize) && ret; pos += MAIN_BUFFER_SIZE) { for (u64 pos = 0; (pos < osize) && ret; pos += MAIN_BUFFER_SIZE) {
UINT read_bytes = min(MAIN_BUFFER_SIZE, osize - pos); UINT read_bytes = min(MAIN_BUFFER_SIZE, osize - pos);
if (ReadVirtualFile(&ovfile, MAIN_BUFFER, pos, read_bytes, NULL) != 0) if (ReadVirtualFile(&ovfile, MAIN_BUFFER, pos, read_bytes, NULL) != 0)
ret = false; ret = false;
@ -396,7 +396,7 @@ bool PathCopyFatToVrt(const char* destdir, const char* orig) {
// FAT file size // FAT file size
FILINFO fno; FILINFO fno;
if (fa_stat(orig, &fno) != FR_OK) return false; // file does not exist if (fa_stat(orig, &fno) != FR_OK) return false; // file does not exist
u32 osize = fno.fsize; u64 osize = fno.fsize;
// virtual file // virtual file
if (!GetVirtualFile(&dvfile, dest)) { if (!GetVirtualFile(&dvfile, dest)) {
@ -439,7 +439,7 @@ bool PathCopyFatToVrt(const char* destdir, const char* orig) {
// unmount critical NAND drives // unmount critical NAND drives
DismountDriveType(DriveType(destdir)&(DRV_SYSNAND|DRV_EMUNAND|DRV_IMAGE)); DismountDriveType(DriveType(destdir)&(DRV_SYSNAND|DRV_EMUNAND|DRV_IMAGE));
if (!ShowProgress(0, 0, orig)) ret = false; if (!ShowProgress(0, 0, orig)) ret = false;
for (size_t pos = 0; (pos < osize) && ret; pos += MAIN_BUFFER_SIZE) { for (u64 pos = 0; (pos < osize) && ret; pos += MAIN_BUFFER_SIZE) {
UINT bytes_read = 0; UINT bytes_read = 0;
if (fx_read(&ofile, MAIN_BUFFER, MAIN_BUFFER_SIZE, &bytes_read) != FR_OK) if (fx_read(&ofile, MAIN_BUFFER, MAIN_BUFFER_SIZE, &bytes_read) != FR_OK)
ret = false; ret = false;
@ -541,7 +541,7 @@ bool PathCopyVrtToFat(char* dest, char* orig, u32* flags) {
} }
} else { // copying files } else { // copying files
FIL dfile; FIL dfile;
u32 osize = vfile.size; u64 osize = vfile.size;
if (GetFreeSpace(dest) < osize) { if (GetFreeSpace(dest) < osize) {
ShowPrompt(false, "Error: File is too big for destination"); ShowPrompt(false, "Error: File is too big for destination");
@ -556,7 +556,7 @@ bool PathCopyVrtToFat(char* dest, char* orig, u32* flags) {
f_sync(&dfile); f_sync(&dfile);
ret = true; ret = true;
for (size_t pos = 0; (pos < osize) && ret; pos += MAIN_BUFFER_SIZE) { for (u64 pos = 0; (pos < osize) && ret; pos += MAIN_BUFFER_SIZE) {
UINT read_bytes = min(MAIN_BUFFER_SIZE, osize - pos); UINT read_bytes = min(MAIN_BUFFER_SIZE, osize - pos);
UINT bytes_written = 0; UINT bytes_written = 0;
if (ReadVirtualFile(&vfile, MAIN_BUFFER, pos, read_bytes, NULL) != 0) if (ReadVirtualFile(&vfile, MAIN_BUFFER, pos, read_bytes, NULL) != 0)
@ -686,7 +686,7 @@ bool PathCopyWorker(char* dest, char* orig, u32* flags, bool move) {
} else { // copying files } else { // copying files
FIL ofile; FIL ofile;
FIL dfile; FIL dfile;
size_t fsize; u64 fsize;
if (fx_open(&ofile, orig, FA_READ | FA_OPEN_EXISTING) != FR_OK) { if (fx_open(&ofile, orig, FA_READ | FA_OPEN_EXISTING) != FR_OK) {
if (!FileUnlock(orig) || (fx_open(&ofile, orig, FA_READ | FA_OPEN_EXISTING) != FR_OK)) if (!FileUnlock(orig) || (fx_open(&ofile, orig, FA_READ | FA_OPEN_EXISTING) != FR_OK))
@ -713,7 +713,7 @@ bool PathCopyWorker(char* dest, char* orig, u32* flags, bool move) {
f_sync(&ofile); f_sync(&ofile);
ret = true; ret = true;
for (size_t pos = 0; (pos < fsize) && ret; pos += MAIN_BUFFER_SIZE) { for (u64 pos = 0; (pos < fsize) && ret; pos += MAIN_BUFFER_SIZE) {
UINT bytes_read = 0; UINT bytes_read = 0;
UINT bytes_written = 0; UINT bytes_written = 0;
if (fx_read(&ofile, MAIN_BUFFER, MAIN_BUFFER_SIZE, &bytes_read) != FR_OK) if (fx_read(&ofile, MAIN_BUFFER, MAIN_BUFFER_SIZE, &bytes_read) != FR_OK)