1
0
Fork 0
mirror of https://github.com/suchmememanyskill/TegraExplorer.git synced 2024-12-27 03:46:02 +00:00
TegraExplorer/source/fs/fscopy.c
suchmememanyskill 513bd804b1 Add fw dumping
- also fatfs is stupid
- also close files properly on a failed copy
- also check for errors during folder readouts
- also make sure holding vol- doesn't dump the keys anyway
2020-12-28 02:29:58 +01:00

81 lines
No EOL
1.9 KiB
C

#include "fscopy.h"
#include <libs/fatfs/ff.h>
#include <utils/btn.h>
#include "../tegraexplorer/tconf.h"
#include "../gfx/gfx.h"
#include <mem/heap.h>
#include <string.h>
#include "../gfx/gfxutils.h"
ErrCode_t FileCopy(const char *locin, const char *locout, u8 options){
FIL in, out;
FILINFO in_info;
u64 sizeRemaining, toCopy;
u8 *buff;
u32 x, y;
ErrCode_t err = newErrCode(0);
int res = 0;
gfx_con_getpos(&x, &y);
if (!strcmp(locin, locout)){
return newErrCode(TE_ERR_SAME_LOC);
}
if ((res = f_open(&in, locin, FA_READ | FA_OPEN_EXISTING))){
return newErrCode(res);
}
if ((res = f_stat(locin, &in_info))){
return newErrCode(res);
}
if ((res = f_open(&out, locout, FA_CREATE_ALWAYS | FA_WRITE))){
return newErrCode(res);
}
if (options & COPY_MODE_PRINT){
gfx_printf("[ ]");
x += 16;
gfx_con_setpos(x, y);
}
buff = malloc(TConf.FSBuffSize);
sizeRemaining = f_size(&in);
const u64 totalsize = sizeRemaining;
while (sizeRemaining > 0){
toCopy = MIN(sizeRemaining, TConf.FSBuffSize);
if ((res = f_read(&in, buff, toCopy, NULL))){
err = newErrCode(res);
break;
}
if ((res = f_write(&out, buff, toCopy, NULL))){
err = newErrCode(res);
break;
}
sizeRemaining -= toCopy;
if (options & COPY_MODE_PRINT){
gfx_printf("%3d%%", (u32)(((totalsize - sizeRemaining) * 100) / totalsize));
gfx_con_setpos(x, y);
}
if (options & COPY_MODE_CANCEL && btn_read() & (BTN_VOL_DOWN | BTN_VOL_UP)){
f_unlink(locout);
break;
}
}
f_close(&in);
f_close(&out);
free(buff);
f_chmod(locout, in_info.fattrib, 0x3A);
//f_stat(locin, &in_info); //somehow stops fatfs from being weird
return err;
}