Browse Source

put and get can now handle file paths

Denys 5 years ago
parent
commit
62279a392f
2 changed files with 40 additions and 10 deletions
  1. 9 4
      cli/include/fileman.h
  2. 31 6
      cli/src/fileman.cpp

+ 9 - 4
cli/include/fileman.h

@@ -19,7 +19,7 @@ private:
    *
    * Filestreams for put and get
    * Vector for holding received filenames from listing
-   * Names for put and get
+   * Paths and filenames for put and get
    * Size for internal read
    * Total and Remaining chunks for put, get and list
    * Boolean replacement for filestreams being open for list
@@ -28,7 +28,7 @@ private:
   std::ifstream putfile;
   std::ofstream getfile;
   std::vector<std::string> listdata;
-  std::string getname, putname;
+  std::string getpath, getname, putpath, putname;
   const unsigned int max_read_len = 8;
   int putsize;
   int putchunks;
@@ -61,8 +61,8 @@ public:
    *
    * Return true if successful, false otherwise
    */
-  bool openPut(const std::string &name);
-  bool openGet(const std::string &name);
+  bool openPut(const std::string &path);
+  bool openGet(const std::string &path);
   bool openList();
 
   /**
@@ -125,6 +125,11 @@ public:
    */
   void setGetChunks(int chunks);
   void setListChunks(int chunks);
+
+  /**
+   * Returns the filename of the passed (relative) path of a file
+   */
+  std::string pathToFilename(std::string path);
 };
 
 #endif

+ 31 - 6
cli/src/fileman.cpp

@@ -15,9 +15,10 @@ bool FileMan::isPutting() { return putfile.is_open(); }
 
 bool FileMan::isListing() { return islisting; }
 
-bool FileMan::openPut(const std::string &name) {
-  putname = name;
-  putfile.open(name, std::ios::ate | std::ios::binary);
+bool FileMan::openPut(const std::string &path) {
+  putpath = path;
+  putname = pathToFilename(path);
+  putfile.open(path, std::ios::ate | std::ios::binary);
   if (putfile.is_open()) {
     size_t size = putfile.tellg();
     putsize = size;
@@ -29,11 +30,12 @@ bool FileMan::openPut(const std::string &name) {
   return false;
 }
 
-bool FileMan::openGet(const std::string &name) {
-  getname = name;
+bool FileMan::openGet(const std::string &path) {
+  getpath = path;
+  getname = pathToFilename(path);
   getchunks = 0;
   getchunksRemaining = 0;
-  getfile.open(name, std::ios::app | std::ios::binary);
+  getfile.open(path, std::ios::app | std::ios::binary);
   if (getfile.tellp() != std::ios::beg) {
     closeGet();
     return false;
@@ -129,3 +131,26 @@ int FileMan::getPutSize() { return putsize; }
 int FileMan::getListRemainingChunks() { return listchunksRemaining; }
 
 int FileMan::getListChunks() { return listchunks; }
+
+std::string FileMan::pathToFilename(std::string path) {
+
+  int lastFoundIndex = -1;
+
+  for (int currentIndex = name.find("/"); currentIndex != string::npos;
+       currentIndex = name.find("/", currentIndex + 1)) {
+
+    // check if the "/" was escaped
+    if (currentIndex > 0 && cmd[currentIndex - 1] = '\\')
+      continue;
+
+    // check if the "/" is at the end of path name
+    if (currentIndex + 1 == name.length) {
+      // ERROR: INVALID FILENAME, BECAUSE ENDS WITH "/"
+    }
+
+    // otherwise we found a valid "/"
+    lastFoundIndex = currentIndex;
+  }
+
+  return name.substr(lastFoundIndex + 1);
+}