1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- #include <cstdlib>
- #include <cstring>
- #include <memory>
- #include <unistd.h>
- #include <sys/stat.h>
- #include <sys/types.h>
- #include "TmpPath.h"
- #include <kopano/ECConfig.h>
- namespace KC {
- TmpPath::TmpPath() {
- const char *dummy = NULL;
- if (path.empty()) {
- dummy = getenv("TMP");
- if (dummy)
- path = dummy;
- }
- if (path.empty()) {
- dummy = getenv("TEMP");
- if (dummy)
- path = dummy;
- }
- if (!path.empty()) {
- struct stat st;
- if (stat(path.c_str(), &st) == -1)
- path = "/tmp";
- }
- if (path.empty())
- path = "/tmp";
- }
- bool TmpPath::OverridePath(ECConfig *const ec) {
- bool rc = true;
- const char *newPath = ec->GetSetting("tmp_path");
- if (newPath == nullptr || newPath[0] == '\0')
- return true;
- path = newPath;
- size_t s = path.size();
- if (path.at(s - 1) == '/' && s > 1)
- path = path.substr(0, s - 1);
- struct stat st;
- if (stat(path.c_str(), &st) == -1) {
- path = "/tmp";
- rc = false;
- }
- setenv("TMP", newPath, 1);
- setenv("TEMP", newPath, 1);
- return rc;
- }
- TmpPath *TmpPath::getInstance()
- {
- static std::unique_ptr<TmpPath> instance(new TmpPath);
- return instance.get();
- }
- }
|