Cleanup around parent paths evaluation. "." is replaced by full current working direc...
authorFrantišek Dvořák <valtri@civ.zcu.cz>
Fri, 18 Oct 2013 08:42:24 +0000 (10:42 +0200)
committerFrantišek Dvořák <valtri@civ.zcu.cz>
Sat, 19 Oct 2013 19:15:59 +0000 (21:15 +0200)
src/VfsNs.cpp

index df597e3..a851ca0 100644 (file)
@@ -1184,6 +1184,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");
 
@@ -1192,15 +1194,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);
 }