Cleanup around parent paths evaluation. "." is replaced by full current working direc...
authorFrantišek Dvořák <valtri@civ.zcu.cz>
Sat, 19 Oct 2013 19:15:59 +0000 (21:15 +0200)
committerFrantišek Dvořák <valtri@civ.zcu.cz>
Mon, 10 Feb 2014 13:51:25 +0000 (14:51 +0100)
src/VfsNs.cpp

index 35d4db4..4ccad7a 100644 (file)
@@ -919,6 +919,8 @@ ExtendedStat VfsCatalog::getParent(const std::string& path,
                                        std::string* parentPath,
                                        std::string* name) throw (DmException)
 {
+  bool absPath;
+
   if (path.empty())
     vfsThrow(EINVAL, "empty path");
 
@@ -927,15 +929,35 @@ ExtendedStat VfsCatalog::getParent(const std::string& path,
   *name = components.back();
   components.pop_back();
 
-  if (components.size() == 1 && components[0] == "/") *parentPath = "/";
-  else *parentPath = Url::joinPath(components);
-
-  // Get the files now
-  if (!parentPath->empty()) {
-    return this->vfsExtendedStat(*parentPath);
+  absPath = (path.size() >= 1 && path[0] == '/');
+  if (components.size() == 0) {
+    //
+    // Root directory or relative directory without subdirectories:
+    //   /
+    //   name
+    //
+    // We can't check parent of root.
+    // Single name is relative path with parent ".".
+    //
+    *parentPath = absPath ? "/" : this->getWorkingDir();
   } else {
-    return this->vfsExtendedStat(this->getWorkingDir());
+    //
+    // At least one directory:
+    //   /name
+    //   dir/name
+    //
+    // Single "/" would be removed by Url:joinPath().
+    //
+    if (components.size() == 1 && absPath) {
+      *parentPath = "/";
+    } else {
+      *parentPath = Url::joinPath(components);
+    }
   }
+
+  if (parentPath->empty()) *parentPath = ".";
+
+  return this->vfsExtendedStat(*parentPath);
 }