| 103 | // region File helpers |
| 104 | |
| 105 | static bool copyFileContents(const std::string& src, const std::string& dst) { |
| 106 | auto src_lock = file::getLock(src); |
| 107 | auto dst_lock = file::getLock(dst); |
| 108 | const bool same_lock = (src_lock.get() == dst_lock.get()); |
| 109 | |
| 110 | auto unlock_all = [&] { |
| 111 | if (!same_lock) dst_lock->unlock(); |
| 112 | src_lock->unlock(); |
| 113 | }; |
| 114 | |
| 115 | src_lock->lock(); |
| 116 | if (!same_lock) dst_lock->lock(); |
| 117 | |
| 118 | FILE* in = fopen(src.c_str(), "rb"); |
| 119 | if (in == nullptr) { |
| 120 | unlock_all(); |
| 121 | return false; |
| 122 | } |
| 123 | FILE* out = fopen(dst.c_str(), "wb"); |
| 124 | if (out == nullptr) { |
| 125 | fclose(in); |
| 126 | unlock_all(); |
| 127 | return false; |
| 128 | } |
| 129 | uint8_t buf[512]; |
| 130 | bool success = true; |
| 131 | size_t n; |
| 132 | while ((n = fread(buf, 1, sizeof(buf), in)) > 0) { |
| 133 | if (fwrite(buf, 1, n, out) != n) { |
| 134 | success = false; |
| 135 | break; |
| 136 | } |
| 137 | } |
| 138 | if (ferror(in)) { |
| 139 | success = false; |
| 140 | } |
| 141 | fclose(in); |
| 142 | if (fclose(out) != 0) { |
| 143 | success = false; |
| 144 | } |
| 145 | if (!success) { |
| 146 | remove(dst.c_str()); |
| 147 | } |
| 148 | unlock_all(); |
| 149 | return success; |
| 150 | } |
| 151 | |
| 152 | static bool copyRecursive(const std::string& src, const std::string& dst) { |
| 153 | if (file::isDirectory(src)) { |