TemporaryFile.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. //
  2. // TemporaryFile.cpp
  3. //
  4. // $Id: //poco/1.4/Foundation/src/TemporaryFile.cpp#3 $
  5. //
  6. // Library: Foundation
  7. // Package: Filesystem
  8. // Module: TemporaryFile
  9. //
  10. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #include "Poco/TemporaryFile.h"
  16. #include "Poco/Path.h"
  17. #include "Poco/Exception.h"
  18. #if !defined(POCO_VXWORKS)
  19. #include "Poco/Process.h"
  20. #endif
  21. #include "Poco/Mutex.h"
  22. #include <set>
  23. #include <sstream>
  24. namespace Poco {
  25. class TempFileCollector
  26. {
  27. public:
  28. TempFileCollector()
  29. {
  30. }
  31. ~TempFileCollector()
  32. {
  33. try
  34. {
  35. for (std::set<std::string>::iterator it = _files.begin(); it != _files.end(); ++it)
  36. {
  37. try
  38. {
  39. File f(*it);
  40. if (f.exists())
  41. f.remove(true);
  42. }
  43. catch (Exception&)
  44. {
  45. }
  46. }
  47. }
  48. catch (...)
  49. {
  50. poco_unexpected();
  51. }
  52. }
  53. void registerFile(const std::string& path)
  54. {
  55. FastMutex::ScopedLock lock(_mutex);
  56. Path p(path);
  57. _files.insert(p.absolute().toString());
  58. }
  59. private:
  60. std::set<std::string> _files;
  61. FastMutex _mutex;
  62. };
  63. TemporaryFile::TemporaryFile():
  64. File(tempName()),
  65. _keep(false)
  66. {
  67. }
  68. TemporaryFile::TemporaryFile(const std::string& tempDir):
  69. File(tempName(tempDir)),
  70. _keep(false)
  71. {
  72. }
  73. TemporaryFile::~TemporaryFile()
  74. {
  75. try
  76. {
  77. if (!_keep)
  78. {
  79. try
  80. {
  81. if (exists())
  82. remove(true);
  83. }
  84. catch (Exception&)
  85. {
  86. }
  87. }
  88. }
  89. catch (...)
  90. {
  91. poco_unexpected();
  92. }
  93. }
  94. void TemporaryFile::keep()
  95. {
  96. _keep = true;
  97. }
  98. void TemporaryFile::keepUntilExit()
  99. {
  100. _keep = true;
  101. registerForDeletion(path());
  102. }
  103. namespace
  104. {
  105. static TempFileCollector fc;
  106. }
  107. void TemporaryFile::registerForDeletion(const std::string& path)
  108. {
  109. fc.registerFile(path);
  110. }
  111. namespace
  112. {
  113. static FastMutex mutex;
  114. }
  115. std::string TemporaryFile::tempName(const std::string& tempDir)
  116. {
  117. std::ostringstream name;
  118. static unsigned long count = 0;
  119. mutex.lock();
  120. unsigned long n = count++;
  121. mutex.unlock();
  122. name << (tempDir.empty() ? Path::temp() : tempDir);
  123. if (name.str().at(name.str().size() - 1) != Path::separator())
  124. {
  125. name << Path::separator();
  126. }
  127. #if defined(POCO_VXWORKS)
  128. name << "tmp";
  129. #else
  130. name << "tmp" << Process::id();
  131. #endif
  132. for (int i = 0; i < 6; ++i)
  133. {
  134. name << char('a' + (n % 26));
  135. n /= 26;
  136. }
  137. return name.str();
  138. }
  139. } // namespace Poco