From f821f7f6d5ac49a441fbac048d3c6be0e2d27240 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Franti=C5=A1ek=20Dvo=C5=99=C3=A1k?= Date: Thu, 10 Oct 2013 14:44:13 +0200 Subject: [PATCH] Check if we don't remove current directory in removeDir() (test-chdir). --- src/VfsNs.cpp | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) 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); } -- 1.8.2.3