TmpPath.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * Copyright 2005 - 2016 Zarafa and its licensors
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU Affero General Public License, version 3,
  6. * as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU Affero General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Affero General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. */
  17. #include <cstdlib>
  18. #include <cstring>
  19. #include <memory>
  20. #include <unistd.h>
  21. #include <sys/stat.h>
  22. #include <sys/types.h>
  23. #include "TmpPath.h"
  24. #include <kopano/ECConfig.h>
  25. namespace KC {
  26. TmpPath::TmpPath() {
  27. const char *dummy = NULL;
  28. if (path.empty()) {
  29. dummy = getenv("TMP");
  30. if (dummy)
  31. path = dummy;
  32. }
  33. if (path.empty()) {
  34. dummy = getenv("TEMP");
  35. if (dummy)
  36. path = dummy;
  37. }
  38. if (!path.empty()) {
  39. struct stat st;
  40. if (stat(path.c_str(), &st) == -1)
  41. path = "/tmp"; // what to do if we can't access that path either? FIXME
  42. }
  43. if (path.empty())
  44. path = "/tmp";
  45. }
  46. bool TmpPath::OverridePath(ECConfig *const ec) {
  47. bool rc = true;
  48. const char *newPath = ec->GetSetting("tmp_path");
  49. if (newPath == nullptr || newPath[0] == '\0')
  50. return true;
  51. path = newPath;
  52. size_t s = path.size();
  53. if (path.at(s - 1) == '/' && s > 1)
  54. path = path.substr(0, s - 1);
  55. struct stat st;
  56. if (stat(path.c_str(), &st) == -1) {
  57. path = "/tmp"; // what to do if we can't access that path either? FIXME
  58. rc = false;
  59. }
  60. setenv("TMP", newPath, 1);
  61. setenv("TEMP", newPath, 1);
  62. return rc;
  63. }
  64. TmpPath *TmpPath::getInstance()
  65. {
  66. static std::unique_ptr<TmpPath> instance(new TmpPath);
  67. return instance.get();
  68. }
  69. } /* namespace */