Day2 of restructuring/rewriting

This commit is contained in:
Such Meme, Many Skill
2020-03-18 23:58:32 +01:00
parent ff618bc285
commit 9d36f741d9
30 changed files with 981 additions and 1503 deletions

View File

@@ -0,0 +1,7 @@
#pragma once
#include "../common/types.h"
int filemenu(menu_entry file);
void copyfile(const char *src_in, const char *outfolder);
int foldermenu();
void copyfolder(char *in, char *out);

View File

@@ -0,0 +1,185 @@
#include <string.h>
#include "entrymenu.h"
#include "../common/common.h"
#include "../../libs/fatfs/ff.h"
#include "../../utils/btn.h"
#include "../../gfx/gfx.h"
#include "fsutils.h"
#include "fsactions.h"
#include "../utils/utils.h"
#include "../gfx/gfxutils.h"
#include "../../mem/heap.h"
#include "fsreader.h"
#include "../gfx/menu.h"
#include "../common/types.h"
#include "../../utils/sprintf.h"
#include "../utils/script.h"
extern char *currentpath;
extern char *clipboard;
extern u8 clipboardhelper;
extern int launch_payload(char *path);
int delfile(const char *path, const char *filename){
gfx_clearscreen();
SWAPCOLOR(COLOR_ORANGE);
gfx_printf("Are you sure you want to delete:\n%s\n\nPress vol+/- to cancel\n", filename);
if (gfx_makewaitmenu("Press power to delete", 3)){
f_unlink(path);
fsreader_readfolder(currentpath);
return 0;
}
else
return -1;
}
void viewbytes(char *path){
FIL in;
u8 print[2048];
u32 size;
QWORD offset = 0;
int res;
gfx_clearscreen();
if ((res = f_open(&in, path, FA_READ | FA_OPEN_EXISTING))){
gfx_errDisplay("viewbytes", res, 1);
return;
}
msleep(200);
while (1){
f_lseek(&in, offset * 16);
if ((res = f_read(&in, &print, 2048 * sizeof(u8), &size))){
gfx_errDisplay("viewbytes", res, 2);
return;
}
gfx_con_setpos(0, 31);
gfx_hexdump(offset * 16, print, size * sizeof(u8));
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);
}
void copyfile(const char *src_in, const char *outfolder){
char *in, *out, *filename;
int res;
gfx_clearscreen();
utils_copystring(src_in, &in);
utils_copystring(strrchr(in, '/') + 1, &filename);
utils_copystring(fsutil_getnextloc(outfolder, filename), &out);
gfx_printf("Note:\nTo stop the transfer hold Vol-\n\n%s\nProgress: ", filename);
if (!strcmp(in, out)){
gfx_errDisplay("gfxcopy", ERR_SAME_LOC, 1);
return;
}
if (clipboardhelper & OPERATIONMOVE){
if ((res = f_rename(in, out))){
gfx_errDisplay("gfxcopy", res, 2);
return;
}
}
else if (clipboardhelper & OPERATIONCOPY) {
if (fsact_copy(in, out, COPY_MODE_CANCEL | COPY_MODE_PRINT))
return;
}
else {
gfx_errDisplay("gfxcopy", ERR_EMPTY_CLIPBOARD, 3);
return;
}
free(in);
free(out);
free(filename);
fsreader_readfolder(currentpath);
clipboardhelper = 0;
}
int filemenu(menu_entry file){
int temp;
FILINFO attribs;
for (int i = 0; i < 3; i++)
if (fs_menu_file[i].name != NULL)
free(fs_menu_file[i].name);
utils_copystring(file.name, &fs_menu_file[0].name);
fs_menu_file[1].name = malloc(16);
fs_menu_file[2].name = malloc(16);
for (temp = 4; temp < 8; temp++)
if ((file.property & (1 << temp)))
break;
sprintf(fs_menu_file[1].name, "\nSize: %d %s", file.storage, gfx_file_size_names[temp - 4]);
if (f_stat(fsutil_getnextloc(currentpath, file.name), &attribs))
SETBIT(fs_menu_file[2].property, ISHIDE, 1);
else {
SETBIT(fs_menu_file[2].property, ISHIDE, 0);
sprintf(fs_menu_file[2].name, "Attribs: %c%c%c%c",
(attribs.fattrib & AM_RDO) ? 'R' : '-',
(attribs.fattrib & AM_SYS) ? 'S' : '-',
(attribs.fattrib & AM_HID) ? 'H' : '-',
(attribs.fattrib & AM_ARC) ? 'A' : '-');
}
SETBIT(fs_menu_file[7].property, ISHIDE, !(strstr(file.name, ".bin") != NULL && file.property & ISKB));
SETBIT(fs_menu_file[8].property, ISHIDE, !(strstr(file.name, ".tegrascript") != NULL));
SETBIT(fs_menu_file[10].property, ISHIDE, !(strstr(file.name, ".bis") != NULL));
SETBIT(fs_menu_file[11].property, ISHIDE, !(strstr(file.name, ".bis") != NULL));
temp = menu_make(fs_menu_file, 12, "-- File Menu --");
switch (temp){
case FILE_COPY:
fsreader_writeclipboard(fsutil_getnextloc(currentpath, file.name), OPERATIONCOPY);
break;
case FILE_MOVE:
fsreader_writeclipboard(fsutil_getnextloc(currentpath, file.name), OPERATIONMOVE);
break;
case FILE_DELETE:
delfile(fsutil_getnextloc(currentpath, file.name), file.name);
break;
case FILE_PAYLOAD:
launch_payload(fsutil_getnextloc(currentpath, file.name));
break;
case FILE_SCRIPT:
ParseScript(fsutil_getnextloc(currentpath, file.name));
break;
case FILE_HEXVIEW:
viewbytes(fsutil_getnextloc(currentpath, file.name));
break;
case FILE_DUMPBIS:
/*
clearscreen();
extract_bis_file(getnextloc(currentpath, file.name), currentpath);
btn_wait();
*/
break;
case FILE_RESTOREBIS:
gfx_message(COLOR_ORANGE, "Stubbed");
break;
}
return 0;
}

View File

@@ -0,0 +1,89 @@
#include <string.h>
#include "entrymenu.h"
#include "../common/common.h"
#include "../../libs/fatfs/ff.h"
#include "../../mem/heap.h"
#include "../gfx/menu.h"
#include "fsreader.h"
#include "../gfx/gfxutils.h"
#include "fsactions.h"
#include "fsutils.h"
#include "../../utils/sprintf.h"
extern char *currentpath;
extern char *clipboard;
extern u8 clipboardhelper;
void copyfolder(char *in, char *out){
int res;
res = strlen(in);
if ((*(in + res - 1) == '/')){
gfx_errDisplay("copyfolder", ERR_FOLDER_ROOT, 1);
}
else if (strstr(out, in) != NULL){
gfx_errDisplay("copyfolder", ERR_DEST_PART_OF_SRC, 2);
}
else if (!strcmp(in, out)){
gfx_errDisplay("copyfolder", ERR_SAME_LOC, 3);
}
else {
gfx_clearscreen();
gfx_printf("\nCopying folder, please wait\n");
fsact_copy_recursive(in, out);
fsreader_readfolder(currentpath);
}
clipboardhelper = 0;
}
int foldermenu(){
int res;
FILINFO attribs;
if (fs_menu_folder[0].name != NULL)
free(fs_menu_folder[0].name);
fs_menu_folder[0].name = malloc(16);
res = strlen(currentpath);
SETBIT(fs_menu_folder[3].property, ISHIDE, (*(currentpath + res - 1) == '/'));
SETBIT(fs_menu_folder[4].property, ISHIDE, (*(currentpath + res - 1) == '/'));
if (f_stat(currentpath, &attribs))
SETBIT(fs_menu_folder[0].property, ISHIDE, 1);
else {
SETBIT(fs_menu_folder[0].property, ISHIDE, 0);
sprintf(fs_menu_folder[0].name, "Attribs: %c%c%c%c",
(attribs.fattrib & AM_RDO) ? 'R' : '-',
(attribs.fattrib & AM_SYS) ? 'S' : '-',
(attribs.fattrib & AM_HID) ? 'H' : '-',
(attribs.fattrib & AM_ARC) ? 'A' : '-');
}
res = menu_make(fs_menu_folder, 5, currentpath);
switch (res){
case DIR_EXITFOLDER:
return -1;
case DIR_COPYFOLDER:
fsreader_writeclipboard(currentpath, OPERATIONCOPY | ISDIR);
break;
case DIR_DELETEFOLDER:
gfx_clearscreen();
gfx_printf("Do you want to delete this folder?\nThe entire folder, with all subcontents\n will be deleted!!!\n\nPress vol+/- to cancel\n");
if (gfx_makewaitmenu("Press power to contine...", 3)){
gfx_clearscreen();
gfx_printf("\nDeleting folder, please wait...\n");
fsact_del_recursive(currentpath);
fsreader_writecurpath(fsutil_getprevloc(currentpath));
fsreader_readfolder(currentpath);
}
break;
}
return 0;
}

View File

@@ -0,0 +1,193 @@
#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 "../../utils/btn.h"
#include "fsutils.h"
int fsact_copy(const char *locin, const char *locout, u8 options){
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)){
gfx_errDisplay("copy", ERR_SAME_LOC, 1);
return -1;
}
if ((res = f_open(&in, locin, FA_READ | FA_OPEN_EXISTING))){
gfx_errDisplay("copy", res, 2);
return -1;
}
if (f_stat(locin, &in_info)){
gfx_errDisplay("copy", res, 3);
return -1;
}
if (f_open(&out, locout, FA_CREATE_ALWAYS | FA_WRITE)){
gfx_errDisplay("copy", res, 4);
return -1;
}
buff = malloc (BUFSIZE);
sizeoffile = f_size(&in);
totalsize = sizeoffile;
while (sizeoffile > 0){
if ((res = f_read(&in, buff, (sizeoffile > BUFSIZE) ? BUFSIZE : sizeoffile, &temp1))){
gfx_errDisplay("copy", res, 5);
return -1;
}
if ((res = f_write(&out, buff, (sizeoffile > BUFSIZE) ? BUFSIZE : sizeoffile, &temp2))){
gfx_errDisplay("copy", res, 6);
return -1;
}
if (temp1 != temp2){
gfx_errDisplay("copy", ERR_DISK_WRITE_FAILED, 7);
return -1;
}
sizeoffile -= temp1;
sizecopied += temp1;
if (options & COPY_MODE_PRINT && 10 > i++){
gfx_printf("%k[%d%%]%k", COLOR_GREEN, ((sizecopied * 100) / totalsize) ,COLOR_WHITE);
gfx_con_setpos(x, y);
i = 0;
if (options & COPY_MODE_CANCEL)
if (btn_read() & BTN_VOL_DOWN){
f_unlink(locout);
break;
}
}
}
f_close(&in);
f_close(&out);
free(buff);
if ((res = f_chmod(locout, in_info.fattrib, 0x3A))){
gfx_errDisplay("copy", res, 8);
return -1;
}
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;
}
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");
gfx_printandclear(fno.fname, 37);
if ((res = f_unlink(fsutil_getnextloc(localpath, fno.fname)))){
gfx_errDisplay("del_recursive", res, 2);
return -1;
}
}
}
f_closedir(&dir);
if ((res = f_unlink(localpath))){
gfx_errDisplay("del_recursive", res, 3);
return -1;
}
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;
}
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");
gfx_printandclear(fno.fname, 37);
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;
}
free(temp);
}
}
f_closedir(&dir);
if ((res = (f_stat(startpath, &fno)))){
gfx_errDisplay("copy_recursive", res, 3);
return -1;
}
if ((res = f_chmod(destpath, fno.fattrib, 0x3A))){
gfx_errDisplay("copy_recursive", res, 4);
return -1;
}
free(startpath);
free(destpath);
free(destfoldername);
return 0;
}

View File

@@ -0,0 +1,6 @@
#pragma once
#include "../../utils/types.h"
int fsact_copy(const char *locin, const char *locout, u8 options);
int fsact_del_recursive(char *path);
int fsact_copy_recursive(char *path, char *dstpath);

View File

@@ -0,0 +1,115 @@
#include <string.h>
#include "fsmenu.h"
#include "fsreader.h"
#include "../gfx/menu.h"
#include "../gfx/gfxutils.h"
#include "fsutils.h"
#include "../common/common.h"
#include "../../libs/fatfs/ff.h"
#include "entrymenu.h"
extern char *currentpath;
extern char *clipboard;
extern u8 clipboardhelper;
int lastentry = 0;
void fileexplorer(const char *startpath, int type){
int res;
if (lastentry > 0 && lastentry == type)
clipboardhelper = 0;
lastentry = type;
fsreader_writecurpath(startpath);
fsreader_readfolder(currentpath);
/*
if (strcmp(rootpath, "emmc:/"))
explfilemenu[6].property = 1;
else
explfilemenu[6].property = -1;
*/
while (1){
res = menu_make(fsreader_files, fsutil_getfileobjamount(fsreader_files), currentpath);
switch (res){
case FILEMENU_RETURN:
if (!strcmp(startpath, currentpath))
return;
else {
fsreader_writecurpath(fsutil_getprevloc(currentpath));
fsreader_readfolder(currentpath);
}
break;
case FILEMENU_CLIPBOARD:
if (clipboardhelper & ISDIR)
copyfolder(clipboard, currentpath);
else
copyfile(clipboard, currentpath);
break;
case FILEMENU_CURFOLDER:
if (foldermenu())
return;
break;
default:
if(fsreader_files[res].property & ISDIR){
fsreader_writecurpath(fsutil_getnextloc(currentpath, fsreader_files[res].name));
fsreader_readfolder(currentpath);
}
else
filemenu(fsreader_files[res]);
break;
}
}
/*
while (1){
res = makefilemenu(fileobjects, getfileobjamount(), currentpath);
if (res < 1){
switch (res){
case -2:
if (!strcmp(rootpath, currentpath))
breakfree = true;
else {
writecurpath(getprevloc(currentpath));
readfolder(currentpath);
}
break;
case -1:
if (clipboardhelper & ISDIR)
copyfolder(clipboard, currentpath);
else
copyfile(clipboard, currentpath);
break;
case 0:
tempint = foldermenu();
if (tempint == -1)
breakfree = true;
break;
}
}
else {
if (fileobjects[res - 1].property & ISDIR){
writecurpath(getnextloc(currentpath, fileobjects[res - 1].name));
readfolder(currentpath);
}
else {
filemenu(fileobjects[res - 1]);
}
}
if (breakfree)
break;
}
*/
}

View File

@@ -0,0 +1,3 @@
#pragma once
void fileexplorer(const char *startpath, int type);

View File

@@ -0,0 +1,106 @@
#include "fsreader.h"
#include "../common/types.h"
#include "../common/common.h"
#include "fsutils.h"
#include "../../utils/types.h"
#include "../../libs/fatfs/ff.h"
#include "../gfx/gfxutils.h"
#include "../utils/utils.h"
#include "../../mem/heap.h"
menu_entry *fsreader_files = NULL;
char *currentpath = NULL;
char *clipboard = NULL;
u8 clipboardhelper = 0;
void fsreader_writecurpath(const char *in){
if (currentpath != NULL)
free(currentpath);
utils_copystring(in, &currentpath);
}
void fsreader_writeclipboard(const char *in, u8 args){
if (clipboard != NULL)
free(clipboard);
clipboardhelper = args;
utils_copystring(in, &clipboard);
}
void clearfileobjects(){
if (fsreader_files != NULL){
for (int i = 0; fsreader_files[i].name != NULL; i++){
free(fsreader_files[i].name);
fsreader_files[i].name = NULL;
}
free(fsreader_files);
fsreader_files = NULL;
}
}
void createfileobjects(int size){
fsreader_files = calloc (size + 1, sizeof(menu_entry));
fsreader_files[size].name = NULL;
}
void addobject(char* name, int spot, u8 attribs){
u64 size = 0;
int sizes = 0;
fsreader_files[spot].property = 0;
if (fsreader_files[spot].name != NULL){
free(fsreader_files[spot].name);
fsreader_files[spot].name = NULL;
}
utils_copystring(name, &(fsreader_files[spot].name));
if (attribs & AM_DIR)
fsreader_files[spot].property |= (ISDIR);
else {
size = fsutil_getfilesize(fsutil_getnextloc(currentpath, name));
while (size > 1024){
size /= 1024;
sizes++;
}
if (sizes > 3)
sizes = 0;
fsreader_files[spot].property |= (1 << (4 + sizes));
fsreader_files[spot].storage = size;
}
if (attribs & AM_ARC)
fsreader_files[spot].property |= (ISARC);
}
int fsreader_readfolder(const char *path){
DIR dir;
FILINFO fno;
int folderamount, res;
clearfileobjects();
createfileobjects(fsutil_getfolderentryamount(path) + 3);
for (folderamount = 0; folderamount < 3; folderamount++){
utils_copystring(fs_menu_startdir[folderamount].name, &(fsreader_files[folderamount].name));
fsreader_files[folderamount].storage = fs_menu_startdir[folderamount].storage;
fsreader_files[folderamount].property = fs_menu_startdir[folderamount].property;
}
if ((res = f_opendir(&dir, path))){
gfx_errDisplay("readfolder", res, 0);
return -1;
}
while (!f_readdir(&dir, &fno) && fno.fname[0]){
addobject(fno.fname, folderamount++, fno.fattrib);
}
f_closedir(&dir);
return folderamount;
}

View File

@@ -0,0 +1,7 @@
#pragma once
#include "../common/types.h"
extern menu_entry *fsreader_files;
void fsreader_writecurpath(const char *in);
void fsreader_writeclipboard(const char *in, u8 args);
int fsreader_readfolder(const char *path);

View File

@@ -0,0 +1,90 @@
#include <string.h>
#include "fsutils.h"
#include "../utils/utils.h"
#include "../common/types.h"
#include "../../utils/types.h"
#include "../../libs/fatfs/ff.h"
#include "../../mem/heap.h"
#include "../../utils/sprintf.h"
#include "../gfx/gfxutils.h"
char *fsutil_getnextloc(const char *current, const char *add){
static char *ret;
if (ret != NULL){
free(ret);
ret = NULL;
}
size_t size = strlen(current) + strlen(add) + 2;
ret = (char*) malloc (size);
if (current[strlen(current) - 1] == '/')
sprintf(ret, "%s%s", current, add);
else
sprintf(ret, "%s/%s", current, add);
return ret;
}
char *fsutil_getprevloc(char *current){
static char *ret;
char *temp;
if (ret != NULL){
free(ret);
ret = NULL;
}
utils_copystring(current, &ret);
temp = strrchr(ret, '/');
if (*(temp - 1) == ':')
temp++;
*temp = '\0';
return ret;
}
int fsutil_getfileobjamount(menu_entry *entries){
int amount = 0;
while (entries[amount].name != NULL)
amount++;
return amount;
}
bool fsutil_checkfile(char* path){
FRESULT fr;
FILINFO fno;
fr = f_stat(path, &fno);
return !(fr & FR_NO_FILE);
}
u64 fsutil_getfilesize(char *path){
FILINFO fno;
f_stat(path, &fno);
return fno.fsize;
}
int fsutil_getfolderentryamount(const char *path){
DIR dir;
FILINFO fno;
int folderamount = 0, res;
if ((res = f_opendir(&dir, path))){
gfx_errDisplay("fsutil_getdircount", res, 0);
return -1;
}
while (!f_readdir(&dir, &fno) && fno.fname[0]){
folderamount++;
}
f_closedir(&dir);
return folderamount;
}

View File

@@ -0,0 +1,10 @@
#pragma once
#include "../common/types.h"
#include "../../utils/types.h"
char *fsutil_getnextloc(const char *current, const char *add);
char *fsutil_getprevloc(char *current);
int fsutil_getfileobjamount(menu_entry *entries);
bool fsutil_checkfile(char* path);
u64 fsutil_getfilesize(char *path);
int fsutil_getfolderentryamount(const char *path);