Fix deleting file when copying to same dir and add recursive copying (#13)

* Fix deleting file when copying to same dir and add recursive copying

FIX: Deletes file when copying to same dir
ADD: Recursive copying (copy files and dirs within dirs) (also undoes disabling copying folders)

* Derp fix

* Derp fix (with undoing disabling folder copying)
This commit is contained in:
Epicpkmn11 2018-10-19 18:53:44 -05:00 committed by RocketRobz
parent a97bfd5430
commit 9651648836
4 changed files with 97 additions and 55 deletions

View File

@ -1,5 +1,12 @@
#include "fileOperations.h"
#include <nds.h> #include <nds.h>
#include <stdio.h> #include <stdio.h>
#include <dirent.h>
#include <vector>
#include "file_browse.h"
using namespace std;
#define copyBufSize 0x8000 #define copyBufSize 0x8000
@ -26,8 +33,36 @@ off_t getFileSize(const char *fileName)
return fsize; return fsize;
} }
void dirCopy(DirEntry* entry, int i, const char *destinationPath, const char *sourcePath) {
vector<DirEntry> dirContents;
dirContents.clear();
if (entry->isDirectory) chdir((sourcePath + ("/" + entry->name)).c_str());
getDirectoryContents(dirContents);
if (((int)dirContents.size()) == 1) mkdir((destinationPath + ("/" + entry->name)).c_str(), 0777);
if (((int)dirContents.size()) != 1) fcopy((sourcePath + ("/" + entry->name)).c_str(), (destinationPath + ("/" + entry->name)).c_str());
}
int fcopy(const char *sourcePath, const char *destinationPath) int fcopy(const char *sourcePath, const char *destinationPath)
{ {
DIR *isDir = opendir (sourcePath);
if (isDir != NULL) {
chdir(sourcePath);
vector<DirEntry> dirContents;
getDirectoryContents(dirContents);
DirEntry* entry = &dirContents.at(1);
mkdir(destinationPath, 0777);
for (int i = 1; i < ((int)dirContents.size()); i++) {
chdir(sourcePath);
entry = &dirContents.at(i);
dirCopy(entry, i, destinationPath, sourcePath);
}
chdir (destinationPath);
chdir ("..");
return 1;
} else {
FILE* sourceFile = fopen(sourcePath, "rb"); FILE* sourceFile = fopen(sourcePath, "rb");
off_t fsize = 0; off_t fsize = 0;
if (sourceFile) { if (sourceFile) {
@ -84,3 +119,5 @@ int fcopy(const char *sourcePath, const char *destinationPath)
return -1; return -1;
} }
closedir(isDir);
}

View File

@ -1,3 +1,5 @@
#include <nds.h>
#ifndef FILE_COPY #ifndef FILE_COPY
#define FILE_COPY #define FILE_COPY

View File

@ -49,13 +49,6 @@ using namespace std;
static char path[PATH_MAX]; static char path[PATH_MAX];
struct DirEntry {
string name;
off_t size;
bool isDirectory;
bool isApp;
} ;
bool nameEndsWith (const string& name) { bool nameEndsWith (const string& name) {
if (name.size() == 0) return false; if (name.size() == 0) return false;
@ -594,7 +587,7 @@ string browseForFile (void) {
getDirectoryContents (dirContents); getDirectoryContents (dirContents);
} }
} }
} else if ((strcmp(entry->name.c_str(), "..") != 0) && !entry->isDirectory) { } else if (strcmp(entry->name.c_str(), "..") != 0) {
snprintf(clipboard, sizeof(clipboard), "%s%s", path, entry->name.c_str()); snprintf(clipboard, sizeof(clipboard), "%s%s", path, entry->name.c_str());
snprintf(clipboardFilename, sizeof(clipboardFilename), "%s", entry->name.c_str()); snprintf(clipboardFilename, sizeof(clipboardFilename), "%s", entry->name.c_str());
clipboardOn = true; clipboardOn = true;

View File

@ -25,7 +25,17 @@
#include <string> #include <string>
#include <vector> #include <vector>
using namespace std;
struct DirEntry {
string name;
off_t size;
bool isDirectory;
bool isApp;
} ;
std::string browseForFile (void); std::string browseForFile (void);
void getDirectoryContents (vector<DirEntry>& dirContents);