1
0
Fork 0
mirror of https://github.com/suchmememanyskill/TegraExplorer.git synced 2024-11-29 15:22:09 +00:00

Split fs.c file

+ Misc changes to code in io.c
This commit is contained in:
Such Meme, Many Skill 2020-01-06 18:42:57 +01:00
parent 6af2bb63b8
commit 00bd1cd083
6 changed files with 262 additions and 246 deletions

View file

@ -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;

View file

@ -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);
char *getnextloc(char *current, char *add);

248
source/tegraexplorer/io.c Normal file
View file

@ -0,0 +1,248 @@
#include <string.h>
#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;
}

10
source/tegraexplorer/io.h Normal file
View file

@ -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);

View file

@ -5,6 +5,7 @@
#include "../utils/util.h"
#include "tools.h"
#include "fs.h"
#include "io.h"
#include "../utils/btn.h"
#include "emmc.h"

View file

@ -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();