summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTor Egge <Tor.Egge@online.no>2023-07-21 10:26:58 +0200
committerTor Egge <Tor.Egge@online.no>2023-07-21 10:26:58 +0200
commita0a6e0137cc3741160a2d4d12905d65fac927cb0 (patch)
tree7dd49f685d04f92844cb65dae5cb9ce9bab32a25
parent535a452f4a968a73a1f3c1e6a18a1edb2d4ff2c2 (diff)
Use std::filesystem::current_path
-rw-r--r--config/src/tests/misc/configsystem.cpp14
-rw-r--r--searchcore/src/vespa/searchcore/proton/server/proton.cpp5
-rw-r--r--vespalib/src/tests/fastos/file_test.cpp10
-rw-r--r--vespalib/src/vespa/fastos/unix_file.cpp25
-rw-r--r--vespalib/src/vespa/fastos/unix_file.h3
-rw-r--r--vespalib/src/vespa/vespalib/io/fileutil.cpp24
-rw-r--r--vespalib/src/vespa/vespalib/io/fileutil.h15
7 files changed, 8 insertions, 88 deletions
diff --git a/config/src/tests/misc/configsystem.cpp b/config/src/tests/misc/configsystem.cpp
index db6764e6b17..c68c28b7c82 100644
--- a/config/src/tests/misc/configsystem.cpp
+++ b/config/src/tests/misc/configsystem.cpp
@@ -12,12 +12,10 @@ using namespace config;
namespace {
const char *VESPA_HOME="VESPA_HOME";
-char cwd[1_Ki];
}
TEST("require that bad home directory fails") {
- ASSERT_TRUE(nullptr != getcwd(cwd, sizeof(cwd)));
ASSERT_EQUAL(0, setenv(VESPA_HOME, "/nowhere/near/", 1));
vespa::Defaults::bootstrap("/nowhere/near/");
ConfigSystem configSystem;
@@ -25,27 +23,27 @@ TEST("require that bad home directory fails") {
}
TEST("require that incorrect pid file type fails") {
- ASSERT_TRUE(nullptr != getcwd(cwd, sizeof(cwd)));
+ std::string cwd = std::filesystem::current_path().string();
std::filesystem::remove_all(std::filesystem::path("var"));
std::filesystem::create_directories(std::filesystem::path("var/run/configproxy.pid"));
- ASSERT_EQUAL(0, setenv(VESPA_HOME, cwd, 1));
- vespa::Defaults::bootstrap(cwd);
+ ASSERT_EQUAL(0, setenv(VESPA_HOME, cwd.c_str(), 1));
+ vespa::Defaults::bootstrap(cwd.c_str());
ConfigSystem configSystem;
ASSERT_FALSE(configSystem.isUp());
std::filesystem::remove_all(std::filesystem::path("var"));
}
TEST("require that correct pid file succeeds") {
- ASSERT_TRUE(nullptr != getcwd(cwd, sizeof(cwd)));
+ std::string cwd = std::filesystem::current_path().string();
std::filesystem::remove_all(std::filesystem::path("var"));
std::filesystem::create_directories(std::filesystem::path("var/run"));
FastOS_File pid_file("var/run/configproxy.pid");
pid_file.OpenWriteOnlyTruncate();
ASSERT_TRUE(pid_file.Close());
- ASSERT_EQUAL(0, setenv(VESPA_HOME, cwd, 1));
- vespa::Defaults::bootstrap(cwd);
+ ASSERT_EQUAL(0, setenv(VESPA_HOME, cwd.c_str(), 1));
+ vespa::Defaults::bootstrap(cwd.c_str());
ConfigSystem configSystem;
ASSERT_TRUE(configSystem.isUp());
std::filesystem::remove_all(std::filesystem::path("var"));
diff --git a/searchcore/src/vespa/searchcore/proton/server/proton.cpp b/searchcore/src/vespa/searchcore/proton/server/proton.cpp
index d70bff52ed4..abeddb94a62 100644
--- a/searchcore/src/vespa/searchcore/proton/server/proton.cpp
+++ b/searchcore/src/vespa/searchcore/proton/server/proton.cpp
@@ -329,16 +329,15 @@ Proton::init(const BootstrapConfig::SP & configSnapshot)
break;
}
_protonDiskLayout = std::make_unique<ProtonDiskLayout>(_transport, protonConfig.basedir, protonConfig.tlsspec);
- vespalib::chdir(protonConfig.basedir);
+ std::filesystem::current_path(std::filesystem::path(protonConfig.basedir));
vespalib::alloc::MmapFileAllocatorFactory::instance().setup(protonConfig.basedir + "/swapdirs");
_tls->start(_transport, hwInfo.cpu().cores());
_flushEngine = std::make_unique<FlushEngine>(std::make_shared<flushengine::TlsStatsFactory>(_tls->getTransLogServer()),
strategy, flush.maxconcurrent, vespalib::from_s(flush.idleinterval));
_metricsEngine->addExternalMetrics(_summaryEngine->getMetrics());
- char tmp[1024];
LOG(debug, "Start proton server with root at %s and cwd at %s",
- protonConfig.basedir.c_str(), getcwd(tmp, sizeof(tmp)));
+ protonConfig.basedir.c_str(), std::filesystem::current_path().string().c_str());
_persistenceEngine = std::make_unique<PersistenceEngine>(*this, _diskMemUsageSampler->writeFilter(),
_diskMemUsageSampler->notifier(),
diff --git a/vespalib/src/tests/fastos/file_test.cpp b/vespalib/src/tests/fastos/file_test.cpp
index 464861f5784..99c7b858723 100644
--- a/vespalib/src/tests/fastos/file_test.cpp
+++ b/vespalib/src/tests/fastos/file_test.cpp
@@ -18,16 +18,6 @@ struct Generated {
~Generated() { std::filesystem::remove_all(std::filesystem::path("generated")); }
};
-TEST(FileTest, GetCurrentDirTest) {
- std::string currentDir = FastOS_File::getCurrentDirectory();
- EXPECT_FALSE(currentDir.empty());
- EXPECT_TRUE(FastOS_File::SetCurrentDirectory(".."));
- std::string parentDir = FastOS_File::getCurrentDirectory();
- EXPECT_FALSE(parentDir.empty());
- EXPECT_NE(currentDir, parentDir);
- EXPECT_TRUE(FastOS_File::SetCurrentDirectory(currentDir.c_str()));
-}
-
void MemoryMapTestImpl(int mmap_flags) {
Generated guard;
const int bufSize = 1000;
diff --git a/vespalib/src/vespa/fastos/unix_file.cpp b/vespalib/src/vespa/fastos/unix_file.cpp
index 9f61f6d517f..692674b95bd 100644
--- a/vespalib/src/vespa/fastos/unix_file.cpp
+++ b/vespalib/src/vespa/fastos/unix_file.cpp
@@ -116,9 +116,6 @@ FastOS_UNIX_File::Stat(const char *filename, FastOS_StatInfo *statInfo)
return rc;
}
-bool FastOS_UNIX_File::SetCurrentDirectory (const char *pathName) { return (chdir(pathName) == 0); }
-
-
int FastOS_UNIX_File::GetMaximumFilenameLength (const char *pathName)
{
return pathconf(pathName, _PC_NAME_MAX);
@@ -129,28 +126,6 @@ int FastOS_UNIX_File::GetMaximumPathLength(const char *pathName)
return pathconf(pathName, _PC_PATH_MAX);
}
-std::string
-FastOS_UNIX_File::getCurrentDirectory()
-{
- std::string res;
- int maxPathLen = FastOS_File::GetMaximumPathLength(".");
- if (maxPathLen == -1) {
- maxPathLen = 16384;
- } else if (maxPathLen < 512) {
- maxPathLen = 512;
- }
-
- char *currentDir = new char [maxPathLen + 1];
-
- if (getcwd(currentDir, maxPathLen) != nullptr) {
- res = currentDir;
- }
- delete [] currentDir;
-
- return res;
-}
-
-
unsigned int
FastOS_UNIX_File::CalcAccessFlags(unsigned int openFlags)
{
diff --git a/vespalib/src/vespa/fastos/unix_file.h b/vespalib/src/vespa/fastos/unix_file.h
index fc8d1ced69b..dad75dc561f 100644
--- a/vespalib/src/vespa/fastos/unix_file.h
+++ b/vespalib/src/vespa/fastos/unix_file.h
@@ -33,9 +33,6 @@ protected:
public:
static bool Stat(const char *filename, FastOS_StatInfo *statInfo);
- static std::string getCurrentDirectory();
-
- static bool SetCurrentDirectory (const char *pathName);
static int GetMaximumFilenameLength (const char *pathName);
static int GetMaximumPathLength (const char *pathName);
diff --git a/vespalib/src/vespa/vespalib/io/fileutil.cpp b/vespalib/src/vespa/vespalib/io/fileutil.cpp
index 2c817a2789b..6c169ab8d98 100644
--- a/vespalib/src/vespa/vespalib/io/fileutil.cpp
+++ b/vespalib/src/vespa/vespalib/io/fileutil.cpp
@@ -436,30 +436,6 @@ File::unlink()
return std::filesystem::remove(std::filesystem::path(_filename));
}
-string
-getCurrentDirectory()
-{
- MallocAutoPtr ptr = getcwd(0, 0);
- if (ptr.get() != 0) {
- return string(static_cast<char*>(ptr.get()));
- }
- asciistream ost;
- ost << "getCurrentDirectory(): Failed, errno(" << errno << "): " << safeStrerror(errno);
- throw IoException(ost.str(), IoException::getErrorType(errno), VESPA_STRLOC);
-}
-
-void
-chdir(const string & directory)
-{
- if (::chdir(directory.c_str()) != 0) {
- asciistream ost;
- ost << "chdir(" << directory << "): Failed, errno(" << errno << "): "
- << safeStrerror(errno);
- throw IoException(ost.str(), IoException::getErrorType(errno), VESPA_STRLOC);
- }
- LOG(debug, "chdir(%s): Working directory changed.", directory.c_str());
-}
-
namespace {
uint32_t diskAlignmentSize = 4_Ki;
diff --git a/vespalib/src/vespa/vespalib/io/fileutil.h b/vespalib/src/vespa/vespalib/io/fileutil.h
index f9769936122..4de36daa85f 100644
--- a/vespalib/src/vespa/vespalib/io/fileutil.h
+++ b/vespalib/src/vespa/vespalib/io/fileutil.h
@@ -209,21 +209,6 @@ public:
};
/**
- * Get the current working directory.
- *
- * @throw IoException On failure.
- */
-extern vespalib::string getCurrentDirectory();
-
-/**
- * Change working directory.
- *
- * @param directory The directory to change to.
- * @throw IoException If we failed to change to the new working directory.
- */
-extern void chdir(const vespalib::string & directory);
-
-/**
* List the contents of the given directory.
*/
using DirectoryList = std::vector<vespalib::string>;