From: František Dvořák Date: Thu, 10 Oct 2013 12:44:13 +0000 (+0200) Subject: Check if we don't remove current directory in removeDir() (test-chdir). X-Git-Url: http://scientific.zcu.cz/git/?a=commitdiff_plain;h=f821f7f6d5ac49a441fbac048d3c6be0e2d27240;p=dmlite-plugins-vfs.git Check if we don't remove current directory in removeDir() (test-chdir). --- diff --git a/src/VfsNs.cpp b/src/VfsNs.cpp index 7732a4a..7c8267e 100644 --- a/src/VfsNs.cpp +++ b/src/VfsNs.cpp @@ -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); }