From 00bd1cd08357f1d1c1aa0f8fb90bd0605536ce51 Mon Sep 17 00:00:00 2001 From: "Such Meme, Many Skill" Date: Mon, 6 Jan 2020 18:42:57 +0100 Subject: [PATCH] Split fs.c file + Misc changes to code in io.c --- source/tegraexplorer/fs.c | 244 +--------------------------------- source/tegraexplorer/fs.h | 4 +- source/tegraexplorer/io.c | 248 +++++++++++++++++++++++++++++++++++ source/tegraexplorer/io.h | 10 ++ source/tegraexplorer/te.c | 1 + source/tegraexplorer/tools.c | 1 + 6 files changed, 262 insertions(+), 246 deletions(-) create mode 100644 source/tegraexplorer/io.c create mode 100644 source/tegraexplorer/io.h diff --git a/source/tegraexplorer/fs.c b/source/tegraexplorer/fs.c index 16b305f..99f5fde 100644 --- a/source/tegraexplorer/fs.c +++ b/source/tegraexplorer/fs.c @@ -8,6 +8,7 @@ #include "../utils/btn.h" #include "../gfx/gfx.h" #include "../utils/util.h" +#include "io.h" fs_entry *fileobjects; char rootpath[10] = ""; @@ -123,126 +124,6 @@ fs_entry getfileobj(int spot){ return fileobjects[spot]; } -bool checkfile(char* path){ - FRESULT fr; - FILINFO fno; - - fr = f_stat(path, &fno); - - if (fr & FR_NO_FILE) - return false; - else - return true; -} - -void viewbytes(char *path){ - FIL in; - u8 print[2048]; - u32 size; - QWORD offset = 0; - int res; - - clearscreen(); - res = f_open(&in, path, FA_READ | FA_OPEN_EXISTING); - if (res != FR_OK){ - message(COLOR_RED, "File Opening Failed\nErrcode %d", res); - return; - } - - msleep(200); - - while (1){ - f_lseek(&in, offset * 16); - - res = f_read(&in, &print, 2048 * sizeof(u8), &size); - if (res != FR_OK){ - message(COLOR_RED, "Reading Failed!\nErrcode %d", res); - return; - } - - printbytes(print, size, offset * 16); - res = btn_read(); - - if (!res) - res = btn_wait(); - - if (res & BTN_VOL_DOWN && 2048 * sizeof(u8) == size) - offset++; - if (res & BTN_VOL_UP && offset > 0) - offset--; - if (res & BTN_POWER) - break; - } - f_close(&in); -} - -int copy(const char *locin, const char *locout, bool print, bool canCancel){ - FIL in, out; - FILINFO in_info; - u64 sizeoffile, sizecopied = 0, totalsize; - UINT temp1, temp2; - u8 *buff; - unsigned int x, y, i = 0; - int res; - - gfx_con_getpos(&x, &y); - - if (!strcmp(locin, locout)){ - return 21; - } - - if (f_open(&in, locin, FA_READ | FA_OPEN_EXISTING)){ - return 22; - } - - if (f_stat(locin, &in_info)) - return 22; - - if (f_open(&out, locout, FA_CREATE_ALWAYS | FA_WRITE)){ - return 23; - } - - buff = malloc (BUFSIZE); - sizeoffile = f_size(&in); - totalsize = sizeoffile; - - while (sizeoffile > 0){ - if ((res = f_read(&in, buff, (sizeoffile > BUFSIZE) ? BUFSIZE : sizeoffile, &temp1))) - return res; - if ((res = f_write(&out, buff, (sizeoffile > BUFSIZE) ? BUFSIZE : sizeoffile, &temp2))) - return res; - - if (temp1 != temp2) - return 24; - - sizeoffile -= temp1; - sizecopied += temp1; - - if (print && 10 > i++){ - gfx_printf("%k[%d%%]%k", COLOR_GREEN, ((sizecopied * 100) / totalsize) ,COLOR_WHITE); - gfx_con_setpos(x, y); - - i = 0; - - if (canCancel) - if (btn_read() & BTN_VOL_DOWN){ - f_unlink(locout); - break; - } - } - } - - if (in_info.fattrib & AM_ARC){ - f_chmod(locout, AM_ARC, AM_ARC); - } - - f_close(&in); - f_close(&out); - free(buff); - - return 0; -} - void copyfile(const char *path, const char *outfolder){ char *filename = strrchr(path, '/'); char *outstring; @@ -284,12 +165,6 @@ void copyfile(const char *path, const char *outfolder){ clipboardhelper = 0; } -u64 getfilesize(char *path){ - FILINFO fno; - f_stat(path, &fno); - return fno.fsize; -} - void addobject(char* name, int spot, bool isfol, bool isarc){ size_t length = strlen(name) + 1; u64 size = 0; @@ -325,23 +200,6 @@ void addobject(char* name, int spot, bool isfol, bool isarc){ fileobjects[spot].property |= (ISARC); } -int getfolderentryamount(const char *path){ - DIR dir; - FILINFO fno; - int folderamount = 0; - - if ((f_opendir(&dir, path))){ - return -1; - } - - while (!f_readdir(&dir, &fno) && fno.fname[0]){ - folderamount++; - } - - f_closedir(&dir); - return folderamount; -} - void clearfileobjects(){ if (fileobjects != NULL){ for (int i = 0; fileobjects[i].name != NULL; i++){ @@ -394,106 +252,6 @@ int delfile(const char *path, const char *filename){ return -1; } -void makestring(char *in, char **out){ - *out = (char *) malloc (strlen(in) + 1); - strcpy(*out, in); -} - -int del_recursive(char *path){ - DIR dir; - FILINFO fno; - int res; - char *localpath = NULL; - makestring(path, &localpath); - - if ((res = f_opendir(&dir, localpath))){ - message(COLOR_RED, "Error during f_opendir: %d", res); - return -1; - } - - while (!f_readdir(&dir, &fno) && fno.fname[0]){ - if (fno.fattrib & AM_DIR) - del_recursive(getnextloc(localpath, fno.fname)); - - else { - gfx_box(0, 47, 719, 63, COLOR_DEFAULT); - SWAPCOLOR(COLOR_RED); - gfx_printf("\r"); - gfx_print_length(37, fno.fname); - gfx_printf(" "); - - if ((res = f_unlink(getnextloc(localpath, fno.fname)))) - return res; - } - } - - f_closedir(&dir); - - if ((res = f_unlink(localpath))){ - return res; - } - - free(localpath); - - return 0; -} - -int copy_recursive(char *path, char *dstpath){ - DIR dir; - FILINFO fno; - int res; - char *startpath = NULL, *destpath = NULL, *destfoldername = NULL, *temp = NULL; - - makestring(path, &startpath); - makestring(strrchr(path, '/') + 1, &destfoldername); - - destpath = (char*) malloc (strlen(dstpath) + strlen(destfoldername) + 2); - sprintf(destpath, (dstpath[strlen(dstpath) - 1] == '/') ? "%s%s" : "%s/%s", dstpath, destfoldername); - - - if ((res = f_opendir(&dir, startpath))){ - message(COLOR_RED, "Error during f_opendir: %d", res); - return -1; - } - - f_mkdir(destpath); - - while (!f_readdir(&dir, &fno) && fno.fname[0]){ - if (fno.fattrib & AM_DIR){ - copy_recursive(getnextloc(startpath, fno.fname), destpath); - } - else { - gfx_box(0, 47, 719, 63, COLOR_DEFAULT); - SWAPCOLOR(COLOR_GREEN); - gfx_printf("\r"); - gfx_print_length(37, fno.fname); - gfx_printf(" "); - makestring(getnextloc(startpath, fno.fname), &temp); - - if ((res = copy(temp, getnextloc(destpath, fno.fname), true, false))){ - message(COLOR_RED, "Copy failed!\nErrcode %d", res); - return -1; - } - - free(temp); - } - } - - if (f_stat(startpath, &fno)) - return -2; - - if (fno.fattrib & AM_ARC){ - f_chmod(destpath, AM_ARC, AM_ARC); - } - - f_closedir(&dir); - free(startpath); - free(destpath); - free(destfoldername); - - return 0; -} - void copyfolder(char *in, char *out){ bool fatalerror = false; int res; diff --git a/source/tegraexplorer/fs.h b/source/tegraexplorer/fs.h index 9266f08..00e16ac 100644 --- a/source/tegraexplorer/fs.h +++ b/source/tegraexplorer/fs.h @@ -45,6 +45,4 @@ enum foldermenuoptions { int readfolder(const char *path); void fileexplorer(const char *startpath); -bool checkfile(char* path); -u64 getfilesize(char *path); -int copy(const char *locin, const char *locout, bool print, bool canCancel); \ No newline at end of file +char *getnextloc(char *current, char *add); \ No newline at end of file diff --git a/source/tegraexplorer/io.c b/source/tegraexplorer/io.c new file mode 100644 index 0000000..f3a6835 --- /dev/null +++ b/source/tegraexplorer/io.c @@ -0,0 +1,248 @@ +#include +#include "../mem/heap.h" +#include "gfx.h" +#include "fs.h" +#include "../utils/types.h" +#include "../libs/fatfs/ff.h" +#include "../utils/sprintf.h" +#include "../utils/btn.h" +#include "../gfx/gfx.h" +#include "../utils/util.h" +#include "io.h" + +bool checkfile(char* path){ + FRESULT fr; + FILINFO fno; + + fr = f_stat(path, &fno); + + return !(fr & FR_NO_FILE); +} + +void viewbytes(char *path){ + FIL in; + u8 print[2048]; + u32 size; + QWORD offset = 0; + int res; + + clearscreen(); + res = f_open(&in, path, FA_READ | FA_OPEN_EXISTING); + if (res != FR_OK){ + message(COLOR_RED, "File Opening Failed\nErrcode %d", res); + return; + } + + msleep(200); + + while (1){ + f_lseek(&in, offset * 16); + + res = f_read(&in, &print, 2048 * sizeof(u8), &size); + if (res != FR_OK){ + message(COLOR_RED, "Reading Failed!\nErrcode %d", res); + return; + } + + printbytes(print, size, offset * 16); + res = btn_read(); + + if (!res) + res = btn_wait(); + + if (res & BTN_VOL_DOWN && 2048 * sizeof(u8) == size) + offset++; + if (res & BTN_VOL_UP && offset > 0) + offset--; + if (res & BTN_POWER) + break; + } + f_close(&in); +} + +int copy(const char *locin, const char *locout, bool print, bool canCancel){ + FIL in, out; + FILINFO in_info; + u64 sizeoffile, sizecopied = 0, totalsize; + UINT temp1, temp2; + u8 *buff; + unsigned int x, y, i = 0; + int res; + + gfx_con_getpos(&x, &y); + + if (!strcmp(locin, locout)){ + return 21; + } + + if (f_open(&in, locin, FA_READ | FA_OPEN_EXISTING)){ + return 22; + } + + if (f_stat(locin, &in_info)) + return 22; + + if (f_open(&out, locout, FA_CREATE_ALWAYS | FA_WRITE)){ + return 23; + } + + if ((res = f_chmod(locout, in_info.fattrib, 0x3F))) + return res; + + buff = malloc (BUFSIZE); + sizeoffile = f_size(&in); + totalsize = sizeoffile; + + while (sizeoffile > 0){ + if ((res = f_read(&in, buff, (sizeoffile > BUFSIZE) ? BUFSIZE : sizeoffile, &temp1))) + return res; + if ((res = f_write(&out, buff, (sizeoffile > BUFSIZE) ? BUFSIZE : sizeoffile, &temp2))) + return res; + + if (temp1 != temp2) + return 24; + + sizeoffile -= temp1; + sizecopied += temp1; + + if (print && 10 > i++){ + gfx_printf("%k[%d%%]%k", COLOR_GREEN, ((sizecopied * 100) / totalsize) ,COLOR_WHITE); + gfx_con_setpos(x, y); + + i = 0; + + if (canCancel) + if (btn_read() & BTN_VOL_DOWN){ + f_unlink(locout); + break; + } + } + } + + f_close(&in); + f_close(&out); + free(buff); + + return 0; +} + +u64 getfilesize(char *path){ + FILINFO fno; + f_stat(path, &fno); + return fno.fsize; +} + +int getfolderentryamount(const char *path){ + DIR dir; + FILINFO fno; + int folderamount = 0; + + if ((f_opendir(&dir, path))){ + return -1; + } + + while (!f_readdir(&dir, &fno) && fno.fname[0]){ + folderamount++; + } + + f_closedir(&dir); + return folderamount; +} + +void makestring(char *in, char **out){ + *out = (char *) malloc (strlen(in) + 1); + strcpy(*out, in); +} + +int del_recursive(char *path){ + DIR dir; + FILINFO fno; + int res; + char *localpath = NULL; + makestring(path, &localpath); + + if ((res = f_opendir(&dir, localpath))){ + message(COLOR_RED, "Error during f_opendir: %d", res); + return -1; + } + + while (!f_readdir(&dir, &fno) && fno.fname[0]){ + if (fno.fattrib & AM_DIR) + del_recursive(getnextloc(localpath, fno.fname)); + + else { + gfx_box(0, 47, 719, 63, COLOR_DEFAULT); + SWAPCOLOR(COLOR_RED); + gfx_printf("\r"); + gfx_print_length(37, fno.fname); + gfx_printf(" "); + + if ((res = f_unlink(getnextloc(localpath, fno.fname)))) + return res; + } + } + + f_closedir(&dir); + + if ((res = f_unlink(localpath))){ + return res; + } + + free(localpath); + + return 0; +} + +int copy_recursive(char *path, char *dstpath){ + DIR dir; + FILINFO fno; + int res; + char *startpath = NULL, *destpath = NULL, *destfoldername = NULL, *temp = NULL; + + makestring(path, &startpath); + makestring(strrchr(path, '/') + 1, &destfoldername); + + destpath = (char*) malloc (strlen(dstpath) + strlen(destfoldername) + 2); + sprintf(destpath, (dstpath[strlen(dstpath) - 1] == '/') ? "%s%s" : "%s/%s", dstpath, destfoldername); + + + if ((res = f_opendir(&dir, startpath))){ + message(COLOR_RED, "Error during f_opendir: %d", res); + return 21; + } + + f_mkdir(destpath); + + if (f_stat(startpath, &fno)) + return 22; + + if ((res = f_chmod(destpath, fno.fattrib, 0x3F))) + return res; + + while (!f_readdir(&dir, &fno) && fno.fname[0]){ + if (fno.fattrib & AM_DIR){ + copy_recursive(getnextloc(startpath, fno.fname), destpath); + } + else { + gfx_box(0, 47, 719, 63, COLOR_DEFAULT); + SWAPCOLOR(COLOR_GREEN); + gfx_printf("\r"); + gfx_print_length(37, fno.fname); + gfx_printf(" "); + makestring(getnextloc(startpath, fno.fname), &temp); + + if ((res = copy(temp, getnextloc(destpath, fno.fname), true, false))){ + return res; + } + + free(temp); + } + } + + f_closedir(&dir); + free(startpath); + free(destpath); + free(destfoldername); + + return 0; +} \ No newline at end of file diff --git a/source/tegraexplorer/io.h b/source/tegraexplorer/io.h new file mode 100644 index 0000000..6b112e0 --- /dev/null +++ b/source/tegraexplorer/io.h @@ -0,0 +1,10 @@ +#pragma once + +bool checkfile(char* path); +void viewbytes(char *path); +int copy(const char *locin, const char *locout, bool print, bool canCancel); +u64 getfilesize(char *path); +int getfolderentryamount(const char *path); +void makestring(char *in, char **out); +int del_recursive(char *path); +int copy_recursive(char *path, char *dstpath); \ No newline at end of file diff --git a/source/tegraexplorer/te.c b/source/tegraexplorer/te.c index 1180cf1..1459cca 100644 --- a/source/tegraexplorer/te.c +++ b/source/tegraexplorer/te.c @@ -5,6 +5,7 @@ #include "../utils/util.h" #include "tools.h" #include "fs.h" +#include "io.h" #include "../utils/btn.h" #include "emmc.h" diff --git a/source/tegraexplorer/tools.c b/source/tegraexplorer/tools.c index 0ff03a2..6581c8d 100644 --- a/source/tegraexplorer/tools.c +++ b/source/tegraexplorer/tools.c @@ -12,6 +12,7 @@ #include "../soc/fuse.h" #include "emmc.h" #include "fs.h" +#include "io.h" extern bool sd_mount(); extern void sd_unmount();