Check if we don't remove current directory in removeDir() (test-chdir).
authorFrantišek Dvořák <valtri@civ.zcu.cz>
Thu, 10 Oct 2013 12:44:13 +0000 (14:44 +0200)
committerFrantišek Dvořák <valtri@civ.zcu.cz>
Thu, 10 Oct 2013 12:44:13 +0000 (14:44 +0200)
src/VfsNs.cpp

index 7732a4a..7c8267e 100644 (file)
@@ -371,7 +371,28 @@ void VfsCatalog::rename(const std::string& oldPath, const std::string& newPath)
 
 void VfsCatalog::removeDir(const std::string& path) throw (DmException)
 {
-  wrapCall(rmdir(path.c_str()));
+  char *cwd = NULL, *rmd = NULL;
+
+  // check if we are not removing current working directory
+  cwd = get_current_dir_name();
+  try {
+    changeDir(path);
+    rmd = get_current_dir_name();
+    if (strcmp(cwd, rmd) == 0) {
+      vfsThrow(EINVAL, "removing current working directory");
+    }
+    ::chdir(cwd);
+    // OK, we can try to remove
+    wrapCall(rmdir(path.c_str()));
+  } catch (...) {
+    if (cwd) ::chdir(cwd);
+    free(cwd);
+    free(rmd);
+    throw;
+  }
+
+  free(cwd);
+  free(rmd);
 }