| 1 | /* |
| 2 | * Open Chinese Convert |
| 3 | * |
| 4 | * Copyright 2010-2026 Carbo Kuo and contributors |
| 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | */ |
| 18 | |
| 19 | #include "src/tools/PlatformIO.hpp" |
| 20 | |
| 21 | #include <cstdlib> |
| 22 | #include <cwchar> |
| 23 | |
| 24 | #ifdef _WIN32 |
| 25 | #ifndef NOMINMAX |
| 26 | #define NOMINMAX |
| 27 | #endif |
| 28 | #include <windows.h> |
| 29 | #include <shellapi.h> |
| 30 | |
| 31 | #include <fcntl.h> |
| 32 | #include <io.h> |
| 33 | #include <sys/stat.h> |
| 34 | |
| 35 | #include "src/WinUtil.hpp" |
| 36 | #else |
| 37 | #include <sys/stat.h> |
| 38 | #include <unistd.h> |
| 39 | #endif |
| 40 | |
| 41 | namespace opencc { |
| 42 | namespace tools { |
| 43 | |
| 44 | namespace { |
| 45 | |
| 46 | #ifdef _WIN32 |
| 47 | std::wstring ParentDirectory(const std::wstring& fileName) { |
| 48 | const size_t slash = fileName.find_last_of(L"\\/"); |
| 49 | if (slash == std::wstring::npos) { |
| 50 | return L".\\"; |
| 51 | } |
| 52 | return fileName.substr(0, slash + 1); |
| 53 | } |
| 54 | #else |
| 55 | std::string ParentDirectory(const std::string& fileName) { |
| 56 | const size_t slash = fileName.find_last_of('/'); |
| 57 | if (slash == std::string::npos) { |
| 58 | return "."; |
| 59 | } |
| 60 | if (slash == 0) { |
| 61 | return "/"; |
| 62 | } |
| 63 | return fileName.substr(0, slash); |
| 64 | } |
| 65 | #endif |
| 66 | |
| 67 | } // namespace |
| 68 | |
| 69 | FILE* OpenFileUtf8(const std::string& fileName, const char* mode) { |
| 70 | #ifdef _WIN32 |
| 71 | return _wfopen(internal::WideFromUtf8(fileName).c_str(), |
| 72 | internal::WideFromUtf8(mode).c_str()); |
| 73 | #else |
| 74 | return fopen(fileName.c_str(), mode); |
| 75 | #endif |
| 76 | } |
| 77 | |
| 78 | FILE* CreateTempFileNearUtf8(const std::string& targetFileName, |
| 79 | std::string* fileName) { |
| 80 | #ifdef _WIN32 |
| 81 | if (fileName == nullptr) { |
| 82 | return nullptr; |
| 83 | } |
| 84 | |
| 85 | const std::wstring target = internal::WideFromUtf8(targetFileName); |
| 86 | const std::wstring parent = ParentDirectory(target); |
| 87 | const DWORD processId = GetCurrentProcessId(); |
| 88 | const ULONGLONG tick = GetTickCount64(); |
| 89 | |
| 90 | for (int i = 0; i < 100; i++) { |
| 91 | const std::wstring tempFileName = |
| 92 | parent + L".opencc-" + std::to_wstring(processId) + L"-" + |
| 93 | std::to_wstring(tick) + L"-" + std::to_wstring(i) + L".tmp"; |
| 94 | const int fd = _wopen(tempFileName.c_str(), |
| 95 | _O_CREAT | _O_EXCL | _O_BINARY | _O_WRONLY, |
| 96 | _S_IREAD | _S_IWRITE); |
| 97 | if (fd == -1) { |
| 98 | continue; |
| 99 | } |
| 100 | FILE* file = _wfdopen(fd, L"wb"); |
| 101 | if (file == nullptr) { |
| 102 | _close(fd); |
| 103 | DeleteFileW(tempFileName.c_str()); |
| 104 | return nullptr; |
| 105 | } |
| 106 | *fileName = internal::Utf8FromWide(tempFileName); |
| 107 | return file; |
| 108 | } |
| 109 | return nullptr; |
| 110 | #else |
| 111 | if (fileName == nullptr) { |
| 112 | return nullptr; |
| 113 | } |
| 114 | |
| 115 | std::string tempFileName = ParentDirectory(targetFileName); |
| 116 | tempFileName += "/"; |
| 117 | tempFileName += "openccXXXXXX"; |
| 118 | |
| 119 | std::vector<char> pathBuffer(tempFileName.begin(), tempFileName.end()); |
| 120 | pathBuffer.push_back('\0'); |
| 121 | const int fd = mkstemp(pathBuffer.data()); |
| 122 | if (fd == -1) { |
| 123 | return nullptr; |
| 124 | } |
| 125 | FILE* file = fdopen(fd, "wb"); |
| 126 | if (file == nullptr) { |
| 127 | close(fd); |
| 128 | std::remove(pathBuffer.data()); |
| 129 | return nullptr; |
| 130 | } |
| 131 | *fileName = pathBuffer.data(); |
| 132 | return file; |
| 133 | #endif |
| 134 | } |
| 135 | |
| 136 | bool GetRealPathUtf8(const std::string& fileName, std::string* realPath) { |
| 137 | #ifdef _WIN32 |
| 138 | if (realPath == nullptr) { |
| 139 | return false; |
| 140 | } |
| 141 | |
| 142 | const std::wstring wideFileName = internal::WideFromUtf8(fileName); |
| 143 | HANDLE handle = |
| 144 | CreateFileW(wideFileName.c_str(), 0, |
| 145 | FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, |
| 146 | nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr); |
| 147 | if (handle == INVALID_HANDLE_VALUE) { |
| 148 | return false; |
| 149 | } |
| 150 | |
| 151 | const DWORD flags = FILE_NAME_NORMALIZED | VOLUME_NAME_DOS; |
| 152 | const DWORD required = GetFinalPathNameByHandleW(handle, nullptr, 0, flags); |
| 153 | if (required == 0) { |
| 154 | CloseHandle(handle); |
| 155 | return false; |
| 156 | } |
| 157 | std::wstring resolved(static_cast<size_t>(required) + 1, L'\0'); |
| 158 | const DWORD copied = |
| 159 | GetFinalPathNameByHandleW(handle, &resolved[0], |
| 160 | static_cast<DWORD>(resolved.size()), flags); |
| 161 | CloseHandle(handle); |
| 162 | if (copied == 0 || copied >= resolved.size()) { |
| 163 | return false; |
| 164 | } |
| 165 | resolved.resize(copied); |
| 166 | *realPath = internal::Utf8FromWide(resolved); |
| 167 | return true; |
| 168 | #else |
| 169 | if (realPath == nullptr) { |
| 170 | return false; |
| 171 | } |
| 172 | char* resolved = realpath(fileName.c_str(), nullptr); |
| 173 | if (resolved == nullptr) { |
| 174 | return false; |
| 175 | } |
| 176 | *realPath = resolved; |
| 177 | std::free(resolved); |
| 178 | return true; |
| 179 | #endif |
| 180 | } |
| 181 | |
| 182 | bool HasMultipleLinksUtf8(const std::string& fileName) { |
| 183 | #ifdef _WIN32 |
| 184 | const std::wstring wideFileName = internal::WideFromUtf8(fileName); |
| 185 | HANDLE handle = |
| 186 | CreateFileW(wideFileName.c_str(), 0, |
| 187 | FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, |
| 188 | nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr); |
| 189 | if (handle == INVALID_HANDLE_VALUE) { |
| 190 | return false; |
| 191 | } |
| 192 | BY_HANDLE_FILE_INFORMATION fileInfo; |
| 193 | const bool success = GetFileInformationByHandle(handle, &fileInfo) != 0; |
| 194 | CloseHandle(handle); |
| 195 | return success && fileInfo.nNumberOfLinks > 1; |
| 196 | #else |
| 197 | struct stat fileStat; |
| 198 | if (stat(fileName.c_str(), &fileStat) != 0) { |
| 199 | return false; |
| 200 | } |
| 201 | return fileStat.st_nlink > 1; |
| 202 | #endif |
| 203 | } |
| 204 | |
| 205 | bool IsSameFileUtf8(const std::string& first, const std::string& second) { |
| 206 | #ifdef _WIN32 |
| 207 | const std::wstring wideFirst = internal::WideFromUtf8(first); |
| 208 | const std::wstring wideSecond = internal::WideFromUtf8(second); |
| 209 | |
| 210 | HANDLE firstHandle = |
| 211 | CreateFileW(wideFirst.c_str(), 0, |
| 212 | FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, |
| 213 | nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr); |
| 214 | if (firstHandle == INVALID_HANDLE_VALUE) { |
| 215 | return false; |
| 216 | } |
| 217 | HANDLE secondHandle = |
| 218 | CreateFileW(wideSecond.c_str(), 0, |
| 219 | FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, |
| 220 | nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr); |
| 221 | if (secondHandle == INVALID_HANDLE_VALUE) { |
| 222 | CloseHandle(firstHandle); |
| 223 | return false; |
| 224 | } |
| 225 | |
| 226 | BY_HANDLE_FILE_INFORMATION firstInfo = {}; |
| 227 | BY_HANDLE_FILE_INFORMATION secondInfo = {}; |
| 228 | const bool success = |
| 229 | GetFileInformationByHandle(firstHandle, &firstInfo) != 0 && |
| 230 | GetFileInformationByHandle(secondHandle, &secondInfo) != 0; |
| 231 | CloseHandle(secondHandle); |
| 232 | CloseHandle(firstHandle); |
| 233 | |
| 234 | if (!success) { |
| 235 | return false; |
| 236 | } |
| 237 | return firstInfo.dwVolumeSerialNumber == secondInfo.dwVolumeSerialNumber && |
| 238 | firstInfo.nFileIndexHigh == secondInfo.nFileIndexHigh && |
| 239 | firstInfo.nFileIndexLow == secondInfo.nFileIndexLow; |
| 240 | #else |
| 241 | struct stat firstStat; |
| 242 | struct stat secondStat; |
| 243 | if (stat(first.c_str(), &firstStat) != 0 || |
| 244 | stat(second.c_str(), &secondStat) != 0) { |
| 245 | return false; |
| 246 | } |
| 247 | return firstStat.st_dev == secondStat.st_dev && |
| 248 | firstStat.st_ino == secondStat.st_ino; |
| 249 | #endif |
| 250 | } |
| 251 | |
| 252 | int PreserveMetadataUtf8(const std::string& source, |
| 253 | const std::string& target) { |
| 254 | #ifdef _WIN32 |
| 255 | (void)source; |
| 256 | (void)target; |
| 257 | return 0; |
| 258 | #else |
| 259 | struct stat sourceStat; |
| 260 | if (stat(source.c_str(), &sourceStat) != 0) { |
| 261 | return -1; |
| 262 | } |
| 263 | if (chown(target.c_str(), sourceStat.st_uid, sourceStat.st_gid) != 0) { |
| 264 | return -1; |
| 265 | } |
| 266 | if (chmod(target.c_str(), sourceStat.st_mode & 07777) != 0) { |
| 267 | return -1; |
| 268 | } |
| 269 | return 0; |
| 270 | #endif |
| 271 | } |
| 272 | |
| 273 | int ReplaceFileUtf8(const std::string& source, const std::string& target) { |
| 274 | #ifdef _WIN32 |
| 275 | return ReplaceFileW(internal::WideFromUtf8(target).c_str(), |
| 276 | internal::WideFromUtf8(source).c_str(), nullptr, 0, |
| 277 | nullptr, nullptr) |
| 278 | ? 0 |
| 279 | : -1; |
| 280 | #else |
| 281 | return rename(source.c_str(), target.c_str()); |
| 282 | #endif |
| 283 | } |
| 284 | |
| 285 | int RemoveFileUtf8(const std::string& fileName) { |
| 286 | #ifdef _WIN32 |
| 287 | return _wremove(internal::WideFromUtf8(fileName).c_str()); |
| 288 | #else |
| 289 | return std::remove(fileName.c_str()); |
| 290 | #endif |
| 291 | } |
| 292 | |
| 293 | #ifdef _WIN32 |
| 294 | std::vector<std::string> GetUtf8CommandLineArgs() { |
| 295 | int argc = 0; |
| 296 | wchar_t** argv = CommandLineToArgvW(GetCommandLineW(), &argc); |
| 297 | std::vector<std::string> args; |
| 298 | if (argv == nullptr) { |
| 299 | return args; |
| 300 | } |
| 301 | args.reserve(static_cast<size_t>(argc)); |
| 302 | for (int i = 0; i < argc; i++) { |
| 303 | args.push_back(internal::Utf8FromWide(argv[i])); |
| 304 | } |
| 305 | LocalFree(argv); |
| 306 | return args; |
| 307 | } |
| 308 | #endif |
| 309 | |
| 310 | } // namespace tools |
| 311 | } // namespace opencc |