Implement folder copy
+ Change error messaging internally + inproved recursive file deletion
This commit is contained in:
@@ -29,10 +29,11 @@ menu_item explfilemenu[9] = {
|
||||
{"View Hex", COLOR_GREEN, HEXVIEW, 1}
|
||||
};
|
||||
|
||||
menu_item explfoldermenu[4] = {
|
||||
menu_item explfoldermenu[5] = {
|
||||
{"-- Folder Menu --\n", COLOR_BLUE, -1, 0},
|
||||
{"Back", COLOR_WHITE, -1, 1},
|
||||
{"Return to main menu\n", COLOR_BLUE, EXITFOLDER, 1},
|
||||
{"Copy to clipboard", COLOR_VIOLET, COPYFOLDER, 1},
|
||||
{"Delete folder", COLOR_RED, DELETEFOLDER, 1}
|
||||
};
|
||||
|
||||
@@ -135,7 +136,7 @@ void viewbytes(char *path){
|
||||
clearscreen();
|
||||
res = f_open(&in, path, FA_READ | FA_OPEN_EXISTING);
|
||||
if (res != FR_OK){
|
||||
message("File Opening Failed", COLOR_RED);
|
||||
message(COLOR_RED, "File Opening Failed\nErrcode %d", res);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -146,7 +147,7 @@ void viewbytes(char *path){
|
||||
|
||||
res = f_read(&in, &print, 2048 * sizeof(u8), &size);
|
||||
if (res != FR_OK){
|
||||
message("Reading Failed", COLOR_RED);
|
||||
message(COLOR_RED, "Reading Failed!\nErrcode %d", res);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -166,8 +167,9 @@ void viewbytes(char *path){
|
||||
f_close(&in);
|
||||
}
|
||||
|
||||
int copy(const char *locin, const char *locout, bool print){
|
||||
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;
|
||||
@@ -183,6 +185,9 @@ int copy(const char *locin, const char *locout, bool print){
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (f_stat(locin, &in_info))
|
||||
return 1;
|
||||
|
||||
if (f_open(&out, locout, FA_CREATE_ALWAYS | FA_WRITE)){
|
||||
return 2;
|
||||
}
|
||||
@@ -204,18 +209,23 @@ int copy(const char *locin, const char *locout, bool print){
|
||||
sizecopied += temp1;
|
||||
|
||||
if (print && 10 > i++){
|
||||
gfx_printf("%k[%d%%/100%%]%k", COLOR_GREEN, ((sizecopied * 100) / totalsize) ,COLOR_WHITE);
|
||||
gfx_printf("%k[%d%%]%k", COLOR_GREEN, ((sizecopied * 100) / totalsize) ,COLOR_WHITE);
|
||||
gfx_con_setpos(x, y);
|
||||
|
||||
|
||||
i = 0;
|
||||
|
||||
if (btn_read() & BTN_VOL_DOWN){
|
||||
f_unlink(locout);
|
||||
break;
|
||||
}
|
||||
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);
|
||||
@@ -244,11 +254,11 @@ void copyfile(const char *path, const char *outfolder){
|
||||
if (strcmp(rootpath, "emmc:/"))
|
||||
f_rename(path, outstring);
|
||||
else
|
||||
message("\nMoving in emummc is not allowed!", COLOR_RED);
|
||||
message(COLOR_RED, "\nMoving in emummc is not allowed!");
|
||||
}
|
||||
|
||||
else if (clipboardhelper & OPERATIONCOPY) {
|
||||
res = copy(path, outstring, true);
|
||||
res = copy(path, outstring, true, true);
|
||||
if (res){
|
||||
gfx_printf("\n\n%kSomething went wrong while copying!\n\nErrcode: %d%k", COLOR_RED, res, COLOR_WHITE);
|
||||
btn_wait();
|
||||
@@ -256,7 +266,7 @@ void copyfile(const char *path, const char *outfolder){
|
||||
}
|
||||
|
||||
else {
|
||||
message("\nClipboard is empty!", COLOR_RED);
|
||||
message(COLOR_RED, "\nClipboard is empty!");
|
||||
}
|
||||
|
||||
free (outstring);
|
||||
@@ -346,9 +356,8 @@ int readfolder(const char *path){
|
||||
createfileobjects(getfolderentryamount(path));
|
||||
|
||||
if ((res = f_opendir(&dir, path))){
|
||||
char errmes[50] = "";
|
||||
sprintf(errmes, "Error during f_opendir: %d", res);
|
||||
message(errmes, COLOR_RED);
|
||||
message(COLOR_RED, "Error during f_opendir: %d", res);
|
||||
return -1;
|
||||
}
|
||||
|
||||
while (!f_readdir(&dir, &fno) && fno.fname[0]){
|
||||
@@ -373,18 +382,20 @@ 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;
|
||||
localpath = (char*) malloc (strlen(path) + 1);
|
||||
strcpy(localpath, path);
|
||||
char *localpath = NULL;
|
||||
makestring(path, &localpath);
|
||||
|
||||
if ((res = f_opendir(&dir, localpath))){
|
||||
char errmes[50] = "";
|
||||
sprintf(errmes, "Error during f_opendir: %d", res);
|
||||
message(errmes, COLOR_RED);
|
||||
message(COLOR_RED, "Error during f_opendir: %d", res);
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -392,8 +403,16 @@ int del_recursive(char *path){
|
||||
if (fno.fattrib & AM_DIR)
|
||||
del_recursive(getnextloc(localpath, fno.fname));
|
||||
|
||||
else if ((res = f_unlink(getnextloc(localpath, fno.fname))))
|
||||
return res;
|
||||
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);
|
||||
@@ -407,6 +426,86 @@ int del_recursive(char *path){
|
||||
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;
|
||||
|
||||
if (!strcmp(in, rootpath)){
|
||||
message(COLOR_RED, "In is root\nAborting!");
|
||||
fatalerror = true;
|
||||
}
|
||||
|
||||
if (strstr(out, in) != NULL && !fatalerror){
|
||||
message(COLOR_RED, "\nOut is a part of in!\nAborting");
|
||||
fatalerror = true;
|
||||
}
|
||||
|
||||
if (!fatalerror){
|
||||
clearscreen();
|
||||
gfx_printf("\nCopying folder, please wait\n");
|
||||
if ((res = copy_recursive(in, out)))
|
||||
message(COLOR_RED, "copy_recursive() failed!\nErrcode %d", res);
|
||||
}
|
||||
|
||||
clipboardhelper = 0;
|
||||
}
|
||||
|
||||
int filemenu(fs_entry file){
|
||||
int temp;
|
||||
strlcpy(explfilemenu[1].name, file.name, 43);
|
||||
@@ -448,26 +547,28 @@ int filemenu(fs_entry file){
|
||||
|
||||
int foldermenu(){
|
||||
int res;
|
||||
char temp[50] = "";
|
||||
|
||||
res = makemenu(explfoldermenu, 4);
|
||||
res = makemenu(explfoldermenu, 5);
|
||||
|
||||
switch (res){
|
||||
case EXITFOLDER:
|
||||
return -1;
|
||||
case DELETEFOLDER:
|
||||
if (makewaitmenu("Do you want to delete this folder?\nThe entire folder, with all subcontents\n will be deleted!!!\n\nPress vol+/- to cancel\n", "Press power to contine...", 7)){
|
||||
if (makewaitmenu("Do you want to delete this folder?\nThe entire folder, with all subcontents\n will be deleted!!!\n\nPress vol+/- to cancel\n", "Press power to contine...", 3)){
|
||||
clearscreen();
|
||||
gfx_printf("\nDeleting folder, please wait...");
|
||||
gfx_printf("\nDeleting folder, please wait...\n");
|
||||
if ((res = del_recursive(currentpath))){
|
||||
sprintf(temp, "Error during del_recursive()! %d", res);
|
||||
message(temp, COLOR_RED);
|
||||
message(COLOR_RED, "Error during del_recursive()! %d", res);
|
||||
}
|
||||
writecurpath(getprevloc(currentpath));
|
||||
return readfolder(currentpath) + 1;
|
||||
}
|
||||
break;
|
||||
case COPYFOLDER:
|
||||
writeclipboard(currentpath, false, true);
|
||||
break;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -502,7 +603,12 @@ void fileexplorer(const char *startpath){
|
||||
break;
|
||||
|
||||
case -1:
|
||||
copyfile(clipboard, currentpath);
|
||||
if (clipboardhelper & ISDIR){
|
||||
copyfolder(clipboard, currentpath);
|
||||
}
|
||||
else {
|
||||
copyfile(clipboard, currentpath);
|
||||
}
|
||||
amount = readfolder(currentpath);
|
||||
break;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user