| 1 |
/* |
| 2 |
* mptFileTemporary.h |
| 3 |
* ------------------ |
| 4 |
* Purpose: |
| 5 |
* Notes : Currently none. |
| 6 |
* Authors: OpenMPT Devs |
| 7 |
* The OpenMPT source code is released under the BSD license. Read LICENSE for more details. |
| 8 |
*/ |
| 9 |
|
| 10 |
|
| 11 |
#pragma once |
| 12 |
|
| 13 |
#include "openmpt/all/BuildSettings.hpp" |
| 14 |
|
| 15 |
#include "mpt/base/namespace.hpp" |
| 16 |
|
| 17 |
#if defined(MODPLUG_TRACKER) && MPT_OS_WINDOWS |
| 18 |
|
| 19 |
#include "mpt/fs/common_directories.hpp" |
| 20 |
#include "mpt/fs/fs.hpp" |
| 21 |
#include "mpt/io_file_unique/unique_basename.hpp" |
| 22 |
#include "mpt/io_file_unique/unique_tempfilename.hpp" |
| 23 |
#include "mpt/uuid/uuid.hpp" |
| 24 |
|
| 25 |
#include "mptPathString.h" |
| 26 |
#include "mptRandom.h" |
| 27 |
|
| 28 |
#if MPT_OS_WINDOWS |
| 29 |
#include <windows.h> |
| 30 |
#endif |
| 31 |
|
| 32 |
#endif // MODPLUG_TRACKER && MPT_OS_WINDOWS |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
OPENMPT_NAMESPACE_BEGIN |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
namespace mpt |
| 41 |
{ |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
#if defined(MODPLUG_TRACKER) && MPT_OS_WINDOWS |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
// Returns a new unique absolute path. |
| 50 |
class TemporaryPathname |
| 51 |
{ |
| 52 |
private: |
| 53 |
mpt::PathString m_Path; |
| 54 |
public: |
| 55 |
TemporaryPathname(const mpt::PathString &fileNameExtension = P_("tmp")) |
| 56 |
{ |
| 57 |
mpt::PathString prefix; |
| 58 |
#if defined(LIBOPENMPT_BUILD) |
| 59 |
prefix = P_("libopenmpt"); |
| 60 |
#else |
| 61 |
prefix = P_("OpenMPT"); |
| 62 |
#endif |
| 63 |
m_Path = mpt::PathString::FromNative(mpt::IO::unique_tempfilename{mpt::IO::unique_basename{prefix, mpt::UUID::GenerateLocalUseOnly(mpt::global_prng())}, fileNameExtension}); |
| 64 |
} |
| 65 |
public: |
| 66 |
mpt::PathString GetPathname() const |
| 67 |
{ |
| 68 |
return m_Path; |
| 69 |
} |
| 70 |
}; |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
// Scoped temporary file guard. Deletes the file when going out of scope. |
| 75 |
// The file itself is not created automatically. |
| 76 |
class TempFileGuard |
| 77 |
{ |
| 78 |
private: |
| 79 |
const mpt::PathString filename; |
| 80 |
public: |
| 81 |
TempFileGuard(const mpt::TemporaryPathname &pathname = mpt::TemporaryPathname{}) |
| 82 |
: filename(pathname.GetPathname()) |
| 83 |
{ |
| 84 |
return; |
| 85 |
} |
| 86 |
mpt::PathString GetFilename() const |
| 87 |
{ |
| 88 |
return filename; |
| 89 |
} |
| 90 |
~TempFileGuard() |
| 91 |
{ |
| 92 |
if(!filename.empty()) |
| 93 |
{ |
| 94 |
DeleteFile(mpt::support_long_path(filename.AsNative()).c_str()); |
| 95 |
} |
| 96 |
} |
| 97 |
}; |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
// Scoped temporary directory guard. Deletes the directory when going out of scope. |
| 102 |
// The directory itself is created automatically. |
| 103 |
class TempDirGuard |
| 104 |
{ |
| 105 |
private: |
| 106 |
mpt::PathString dirname; |
| 107 |
public: |
| 108 |
TempDirGuard(const mpt::TemporaryPathname &pathname = mpt::TemporaryPathname{}) |
| 109 |
: dirname(pathname.GetPathname().WithTrailingSlash()) |
| 110 |
{ |
| 111 |
if(dirname.empty()) |
| 112 |
{ |
| 113 |
return; |
| 114 |
} |
| 115 |
if(::CreateDirectory(mpt::support_long_path(dirname.AsNative()).c_str(), NULL) == 0) |
| 116 |
{ // fail |
| 117 |
dirname = mpt::PathString(); |
| 118 |
} |
| 119 |
} |
| 120 |
mpt::PathString GetDirname() const |
| 121 |
{ |
| 122 |
return dirname; |
| 123 |
} |
| 124 |
~TempDirGuard() |
| 125 |
{ |
| 126 |
if(!dirname.empty()) |
| 127 |
{ |
| 128 |
mpt::native_fs{}.delete_tree(dirname); |
| 129 |
} |
| 130 |
} |
| 131 |
}; |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
#endif // MODPLUG_TRACKER && MPT_OS_WINDOWS |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
} // namespace mpt |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
OPENMPT_NAMESPACE_END |