1
0
Fork 0
mirror of https://github.com/suchmememanyskill/TegraExplorer.git synced 2024-11-26 22:02:08 +00:00
TegraExplorer/source/tegraexplorer/fs/fsactions.c

208 lines
5 KiB
C
Raw Normal View History

2020-03-18 22:58:32 +00:00
#include <string.h>
#include "fsactions.h"
#include "../common/common.h"
#include "../../libs/fatfs/ff.h"
#include "../../gfx/gfx.h"
#include "../gfx/gfxutils.h"
#include "../utils/utils.h"
#include "../../mem/heap.h"
#include "../../hid/hid.h"
2020-05-04 19:30:51 +01:00
#include "../../utils/btn.h"
2020-03-18 22:58:32 +00:00
#include "fsutils.h"
int fsact_copy(const char *locin, const char *locout, u8 options){
FIL in, out;
FILINFO in_info;
2020-05-04 19:30:51 +01:00
u64 sizeRemaining, toCopy;
2020-03-18 22:58:32 +00:00
UINT temp1, temp2;
2020-05-04 19:30:51 +01:00
u8 *buff, toPrint = options & COPY_MODE_PRINT, toCancel = options & COPY_MODE_CANCEL;
u32 x, y, i = 11;
2020-03-18 22:58:32 +00:00
int res;
gfx_con_getpos(&x, &y);
if (!strcmp(locin, locout)){
gfx_errDisplay("copy", ERR_SAME_LOC, 1);
return 1;
2020-03-18 22:58:32 +00:00
}
if ((res = f_open(&in, locin, FA_READ | FA_OPEN_EXISTING))){
gfx_errDisplay("copy", res, 2);
return 1;
2020-03-18 22:58:32 +00:00
}
if (f_stat(locin, &in_info)){
gfx_errDisplay("copy", res, 3);
return 1;
2020-03-18 22:58:32 +00:00
}
if (f_open(&out, locout, FA_CREATE_ALWAYS | FA_WRITE)){
gfx_errDisplay("copy", res, 4);
return 1;
2020-03-18 22:58:32 +00:00
}
2020-05-04 19:30:51 +01:00
if (toPrint){
SWAPCOLOR(COLOR_GREEN);
gfx_printf("[ ]");
x += 16;
gfx_con_setpos(x, y);
}
2020-03-18 22:58:32 +00:00
buff = malloc (BUFSIZE);
2020-05-04 19:30:51 +01:00
sizeRemaining = f_size(&in);
const u64 totalsize = sizeRemaining;
2020-03-18 22:58:32 +00:00
2020-05-04 19:30:51 +01:00
while (sizeRemaining > 0){
toCopy = MIN(sizeRemaining, BUFSIZE);
if ((res = f_read(&in, buff, toCopy, &temp1))){
2020-03-18 22:58:32 +00:00
gfx_errDisplay("copy", res, 5);
return 1;
2020-03-18 22:58:32 +00:00
}
2020-05-04 19:30:51 +01:00
if ((res = f_write(&out, buff, toCopy, &temp2))){
2020-03-18 22:58:32 +00:00
gfx_errDisplay("copy", res, 6);
return 1;
2020-03-18 22:58:32 +00:00
}
if (temp1 != temp2){
gfx_errDisplay("copy", ERR_DISK_WRITE_FAILED, 7);
return 1;
2020-03-18 22:58:32 +00:00
}
2020-05-04 19:30:51 +01:00
sizeRemaining -= toCopy;
2020-03-18 22:58:32 +00:00
2020-05-04 19:30:51 +01:00
if (toPrint && (i > 16 || !sizeRemaining)){
gfx_printf("%3d%%", (u32)(((totalsize - sizeRemaining) * 100) / totalsize));
2020-03-18 22:58:32 +00:00
gfx_con_setpos(x, y);
2020-05-04 19:30:51 +01:00
}
if (toCancel && i > 16){
if (btn_read() & (BTN_VOL_DOWN | BTN_VOL_UP)){
f_unlink(locout);
break;
}
}
2020-03-18 22:58:32 +00:00
2020-05-04 19:30:51 +01:00
if (options){
if (i++ > 16)
i = 0;
2020-03-18 22:58:32 +00:00
}
}
2020-05-04 19:30:51 +01:00
RESETCOLOR;
2020-03-18 22:58:32 +00:00
f_close(&in);
f_close(&out);
free(buff);
if ((res = f_chmod(locout, in_info.fattrib, 0x3A))){
gfx_errDisplay("copy", res, 8);
return 1;
2020-03-18 22:58:32 +00:00
}
f_stat(locin, &in_info); //somehow stops fatfs from being weird
return 0;
}
int fsact_del_recursive(char *path){
DIR dir;
FILINFO fno;
int res;
u32 x, y;
char *localpath = NULL;
gfx_con_getpos(&x, &y);
utils_copystring(path, &localpath);
if ((res = f_opendir(&dir, localpath))){
gfx_errDisplay("del_recursive", res, 1);
return 1;
2020-03-18 22:58:32 +00:00
}
while (!f_readdir(&dir, &fno) && fno.fname[0]){
if (fno.fattrib & AM_DIR){
fsact_del_recursive(fsutil_getnextloc(localpath, fno.fname));
}
else {
SWAPCOLOR(COLOR_RED);
gfx_printf("\r");
2020-05-01 23:50:08 +01:00
gfx_printandclear(fno.fname, 37, 720);
2020-03-18 22:58:32 +00:00
if ((res = f_unlink(fsutil_getnextloc(localpath, fno.fname)))){
gfx_errDisplay("del_recursive", res, 2);
return 1;
2020-03-18 22:58:32 +00:00
}
}
}
f_closedir(&dir);
if ((res = f_unlink(localpath))){
gfx_errDisplay("del_recursive", res, 3);
return 1;
2020-03-18 22:58:32 +00:00
}
free(localpath);
return 0;
}
int fsact_copy_recursive(char *path, char *dstpath){
DIR dir;
FILINFO fno;
int res;
u32 x, y;
char *startpath = NULL, *destpath = NULL, *destfoldername = NULL, *temp = NULL;
gfx_con_getpos(&x, &y);
utils_copystring(path, &startpath);
utils_copystring(strrchr(path, '/') + 1, &destfoldername);
utils_copystring(fsutil_getnextloc(dstpath, destfoldername), &destpath);
if ((res = f_opendir(&dir, startpath))){
gfx_errDisplay("copy_recursive", res, 1);
return 1;
2020-03-18 22:58:32 +00:00
}
f_mkdir(destpath);
while (!f_readdir(&dir, &fno) && fno.fname[0]){
if (fno.fattrib & AM_DIR){
fsact_copy_recursive(fsutil_getnextloc(startpath, fno.fname), destpath);
}
else {
SWAPCOLOR(COLOR_GREEN);
gfx_printf("\r");
2020-05-01 23:50:08 +01:00
gfx_printandclear(fno.fname, 37, 720);
2020-03-18 22:58:32 +00:00
utils_copystring(fsutil_getnextloc(startpath, fno.fname), &temp);
if ((res = fsact_copy(temp, fsutil_getnextloc(destpath, fno.fname), COPY_MODE_PRINT))){
gfx_errDisplay("copy_recursive", res, 2);
return 1;
2020-03-18 22:58:32 +00:00
}
free(temp);
}
}
f_closedir(&dir);
if ((res = (f_stat(startpath, &fno)))){
gfx_errDisplay("copy_recursive", res, 3);
return 1;
2020-03-18 22:58:32 +00:00
}
if ((res = f_chmod(destpath, fno.fattrib, 0x3A))){
gfx_errDisplay("copy_recursive", res, 4);
return 1;
2020-03-18 22:58:32 +00:00
}
free(startpath);
free(destpath);
free(destfoldername);
return 0;
}