Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | // Windows/FileDir.cpp |
michael@0 | 2 | |
michael@0 | 3 | #include "StdAfx.h" |
michael@0 | 4 | |
michael@0 | 5 | #include "FileDir.h" |
michael@0 | 6 | #include "FileName.h" |
michael@0 | 7 | #include "FileFind.h" |
michael@0 | 8 | #include "Defs.h" |
michael@0 | 9 | #ifndef _UNICODE |
michael@0 | 10 | #include "../Common/StringConvert.h" |
michael@0 | 11 | #endif |
michael@0 | 12 | |
michael@0 | 13 | #ifndef _UNICODE |
michael@0 | 14 | extern bool g_IsNT; |
michael@0 | 15 | #endif |
michael@0 | 16 | |
michael@0 | 17 | namespace NWindows { |
michael@0 | 18 | namespace NFile { |
michael@0 | 19 | namespace NDirectory { |
michael@0 | 20 | |
michael@0 | 21 | #ifndef _UNICODE |
michael@0 | 22 | static inline UINT GetCurrentCodePage() { return ::AreFileApisANSI() ? CP_ACP : CP_OEMCP; } |
michael@0 | 23 | static UString GetUnicodePath(const CSysString &sysPath) |
michael@0 | 24 | { return MultiByteToUnicodeString(sysPath, GetCurrentCodePage()); } |
michael@0 | 25 | static CSysString GetSysPath(LPCWSTR sysPath) |
michael@0 | 26 | { return UnicodeStringToMultiByte(sysPath, GetCurrentCodePage()); } |
michael@0 | 27 | #endif |
michael@0 | 28 | |
michael@0 | 29 | bool MyGetWindowsDirectory(CSysString &path) |
michael@0 | 30 | { |
michael@0 | 31 | UINT needLength = ::GetWindowsDirectory(path.GetBuffer(MAX_PATH + 1), MAX_PATH + 1); |
michael@0 | 32 | path.ReleaseBuffer(); |
michael@0 | 33 | return (needLength > 0 && needLength <= MAX_PATH); |
michael@0 | 34 | } |
michael@0 | 35 | |
michael@0 | 36 | bool MyGetSystemDirectory(CSysString &path) |
michael@0 | 37 | { |
michael@0 | 38 | UINT needLength = ::GetSystemDirectory(path.GetBuffer(MAX_PATH + 1), MAX_PATH + 1); |
michael@0 | 39 | path.ReleaseBuffer(); |
michael@0 | 40 | return (needLength > 0 && needLength <= MAX_PATH); |
michael@0 | 41 | } |
michael@0 | 42 | |
michael@0 | 43 | #ifndef _UNICODE |
michael@0 | 44 | bool MyGetWindowsDirectory(UString &path) |
michael@0 | 45 | { |
michael@0 | 46 | if (g_IsNT) |
michael@0 | 47 | { |
michael@0 | 48 | UINT needLength = ::GetWindowsDirectoryW(path.GetBuffer(MAX_PATH + 1), MAX_PATH + 1); |
michael@0 | 49 | path.ReleaseBuffer(); |
michael@0 | 50 | return (needLength > 0 && needLength <= MAX_PATH); |
michael@0 | 51 | } |
michael@0 | 52 | CSysString sysPath; |
michael@0 | 53 | if (!MyGetWindowsDirectory(sysPath)) |
michael@0 | 54 | return false; |
michael@0 | 55 | path = GetUnicodePath(sysPath); |
michael@0 | 56 | return true; |
michael@0 | 57 | } |
michael@0 | 58 | |
michael@0 | 59 | bool MyGetSystemDirectory(UString &path) |
michael@0 | 60 | { |
michael@0 | 61 | if (g_IsNT) |
michael@0 | 62 | { |
michael@0 | 63 | UINT needLength = ::GetSystemDirectoryW(path.GetBuffer(MAX_PATH + 1), MAX_PATH + 1); |
michael@0 | 64 | path.ReleaseBuffer(); |
michael@0 | 65 | return (needLength > 0 && needLength <= MAX_PATH); |
michael@0 | 66 | } |
michael@0 | 67 | CSysString sysPath; |
michael@0 | 68 | if (!MyGetSystemDirectory(sysPath)) |
michael@0 | 69 | return false; |
michael@0 | 70 | path = GetUnicodePath(sysPath); |
michael@0 | 71 | return true; |
michael@0 | 72 | } |
michael@0 | 73 | #endif |
michael@0 | 74 | |
michael@0 | 75 | #ifndef _UNICODE |
michael@0 | 76 | bool MySetFileAttributes(LPCWSTR fileName, DWORD fileAttributes) |
michael@0 | 77 | { |
michael@0 | 78 | if (g_IsNT) |
michael@0 | 79 | return BOOLToBool(::SetFileAttributesW(fileName, fileAttributes)); |
michael@0 | 80 | return MySetFileAttributes(GetSysPath(fileName), fileAttributes); |
michael@0 | 81 | } |
michael@0 | 82 | |
michael@0 | 83 | bool MyRemoveDirectory(LPCWSTR pathName) |
michael@0 | 84 | { |
michael@0 | 85 | if (g_IsNT) |
michael@0 | 86 | return BOOLToBool(::RemoveDirectoryW(pathName)); |
michael@0 | 87 | return MyRemoveDirectory(GetSysPath(pathName)); |
michael@0 | 88 | } |
michael@0 | 89 | |
michael@0 | 90 | bool MyMoveFile(LPCWSTR existFileName, LPCWSTR newFileName) |
michael@0 | 91 | { |
michael@0 | 92 | if (g_IsNT) |
michael@0 | 93 | return BOOLToBool(::MoveFileW(existFileName, newFileName)); |
michael@0 | 94 | return MyMoveFile(GetSysPath(existFileName), GetSysPath(newFileName)); |
michael@0 | 95 | } |
michael@0 | 96 | #endif |
michael@0 | 97 | |
michael@0 | 98 | bool MyCreateDirectory(LPCTSTR pathName) { return BOOLToBool(::CreateDirectory(pathName, NULL)); } |
michael@0 | 99 | |
michael@0 | 100 | #ifndef _UNICODE |
michael@0 | 101 | bool MyCreateDirectory(LPCWSTR pathName) |
michael@0 | 102 | { |
michael@0 | 103 | if (g_IsNT) |
michael@0 | 104 | return BOOLToBool(::CreateDirectoryW(pathName, NULL)); |
michael@0 | 105 | return MyCreateDirectory(GetSysPath(pathName)); |
michael@0 | 106 | } |
michael@0 | 107 | #endif |
michael@0 | 108 | |
michael@0 | 109 | /* |
michael@0 | 110 | bool CreateComplexDirectory(LPCTSTR pathName) |
michael@0 | 111 | { |
michael@0 | 112 | NName::CParsedPath path; |
michael@0 | 113 | path.ParsePath(pathName); |
michael@0 | 114 | CSysString fullPath = path.Prefix; |
michael@0 | 115 | DWORD errorCode = ERROR_SUCCESS; |
michael@0 | 116 | for(int i = 0; i < path.PathParts.Size(); i++) |
michael@0 | 117 | { |
michael@0 | 118 | const CSysString &string = path.PathParts[i]; |
michael@0 | 119 | if(string.IsEmpty()) |
michael@0 | 120 | { |
michael@0 | 121 | if(i != path.PathParts.Size() - 1) |
michael@0 | 122 | return false; |
michael@0 | 123 | return true; |
michael@0 | 124 | } |
michael@0 | 125 | fullPath += path.PathParts[i]; |
michael@0 | 126 | if(!MyCreateDirectory(fullPath)) |
michael@0 | 127 | { |
michael@0 | 128 | DWORD errorCode = GetLastError(); |
michael@0 | 129 | if(errorCode != ERROR_ALREADY_EXISTS) |
michael@0 | 130 | return false; |
michael@0 | 131 | } |
michael@0 | 132 | fullPath += NName::kDirDelimiter; |
michael@0 | 133 | } |
michael@0 | 134 | return true; |
michael@0 | 135 | } |
michael@0 | 136 | */ |
michael@0 | 137 | |
michael@0 | 138 | bool CreateComplexDirectory(LPCTSTR _aPathName) |
michael@0 | 139 | { |
michael@0 | 140 | CSysString pathName = _aPathName; |
michael@0 | 141 | int pos = pathName.ReverseFind(TEXT(CHAR_PATH_SEPARATOR)); |
michael@0 | 142 | if (pos > 0 && pos == pathName.Length() - 1) |
michael@0 | 143 | { |
michael@0 | 144 | if (pathName.Length() == 3 && pathName[1] == ':') |
michael@0 | 145 | return true; // Disk folder; |
michael@0 | 146 | pathName.Delete(pos); |
michael@0 | 147 | } |
michael@0 | 148 | CSysString pathName2 = pathName; |
michael@0 | 149 | pos = pathName.Length(); |
michael@0 | 150 | while(true) |
michael@0 | 151 | { |
michael@0 | 152 | if(MyCreateDirectory(pathName)) |
michael@0 | 153 | break; |
michael@0 | 154 | if(::GetLastError() == ERROR_ALREADY_EXISTS) |
michael@0 | 155 | { |
michael@0 | 156 | NFind::CFileInfo fileInfo; |
michael@0 | 157 | if (!NFind::FindFile(pathName, fileInfo)) // For network folders |
michael@0 | 158 | return true; |
michael@0 | 159 | if (!fileInfo.IsDirectory()) |
michael@0 | 160 | return false; |
michael@0 | 161 | break; |
michael@0 | 162 | } |
michael@0 | 163 | pos = pathName.ReverseFind(TEXT(CHAR_PATH_SEPARATOR)); |
michael@0 | 164 | if (pos < 0 || pos == 0) |
michael@0 | 165 | return false; |
michael@0 | 166 | if (pathName[pos - 1] == ':') |
michael@0 | 167 | return false; |
michael@0 | 168 | pathName = pathName.Left(pos); |
michael@0 | 169 | } |
michael@0 | 170 | pathName = pathName2; |
michael@0 | 171 | while(pos < pathName.Length()) |
michael@0 | 172 | { |
michael@0 | 173 | pos = pathName.Find(TEXT(CHAR_PATH_SEPARATOR), pos + 1); |
michael@0 | 174 | if (pos < 0) |
michael@0 | 175 | pos = pathName.Length(); |
michael@0 | 176 | if(!MyCreateDirectory(pathName.Left(pos))) |
michael@0 | 177 | return false; |
michael@0 | 178 | } |
michael@0 | 179 | return true; |
michael@0 | 180 | } |
michael@0 | 181 | |
michael@0 | 182 | #ifndef _UNICODE |
michael@0 | 183 | |
michael@0 | 184 | bool CreateComplexDirectory(LPCWSTR _aPathName) |
michael@0 | 185 | { |
michael@0 | 186 | UString pathName = _aPathName; |
michael@0 | 187 | int pos = pathName.ReverseFind(WCHAR_PATH_SEPARATOR); |
michael@0 | 188 | if (pos > 0 && pos == pathName.Length() - 1) |
michael@0 | 189 | { |
michael@0 | 190 | if (pathName.Length() == 3 && pathName[1] == L':') |
michael@0 | 191 | return true; // Disk folder; |
michael@0 | 192 | pathName.Delete(pos); |
michael@0 | 193 | } |
michael@0 | 194 | UString pathName2 = pathName; |
michael@0 | 195 | pos = pathName.Length(); |
michael@0 | 196 | while(true) |
michael@0 | 197 | { |
michael@0 | 198 | if(MyCreateDirectory(pathName)) |
michael@0 | 199 | break; |
michael@0 | 200 | if(::GetLastError() == ERROR_ALREADY_EXISTS) |
michael@0 | 201 | { |
michael@0 | 202 | NFind::CFileInfoW fileInfo; |
michael@0 | 203 | if (!NFind::FindFile(pathName, fileInfo)) // For network folders |
michael@0 | 204 | return true; |
michael@0 | 205 | if (!fileInfo.IsDirectory()) |
michael@0 | 206 | return false; |
michael@0 | 207 | break; |
michael@0 | 208 | } |
michael@0 | 209 | pos = pathName.ReverseFind(WCHAR_PATH_SEPARATOR); |
michael@0 | 210 | if (pos < 0 || pos == 0) |
michael@0 | 211 | return false; |
michael@0 | 212 | if (pathName[pos - 1] == L':') |
michael@0 | 213 | return false; |
michael@0 | 214 | pathName = pathName.Left(pos); |
michael@0 | 215 | } |
michael@0 | 216 | pathName = pathName2; |
michael@0 | 217 | while(pos < pathName.Length()) |
michael@0 | 218 | { |
michael@0 | 219 | pos = pathName.Find(WCHAR_PATH_SEPARATOR, pos + 1); |
michael@0 | 220 | if (pos < 0) |
michael@0 | 221 | pos = pathName.Length(); |
michael@0 | 222 | if(!MyCreateDirectory(pathName.Left(pos))) |
michael@0 | 223 | return false; |
michael@0 | 224 | } |
michael@0 | 225 | return true; |
michael@0 | 226 | } |
michael@0 | 227 | |
michael@0 | 228 | #endif |
michael@0 | 229 | |
michael@0 | 230 | bool DeleteFileAlways(LPCTSTR name) |
michael@0 | 231 | { |
michael@0 | 232 | if(!::SetFileAttributes(name, 0)) |
michael@0 | 233 | return false; |
michael@0 | 234 | return BOOLToBool(::DeleteFile(name)); |
michael@0 | 235 | } |
michael@0 | 236 | |
michael@0 | 237 | #ifndef _UNICODE |
michael@0 | 238 | bool DeleteFileAlways(LPCWSTR name) |
michael@0 | 239 | { |
michael@0 | 240 | if (g_IsNT) |
michael@0 | 241 | { |
michael@0 | 242 | if(!MySetFileAttributes(name, 0)) |
michael@0 | 243 | return false; |
michael@0 | 244 | return BOOLToBool(::DeleteFileW(name)); |
michael@0 | 245 | } |
michael@0 | 246 | return DeleteFileAlways(GetSysPath(name)); |
michael@0 | 247 | } |
michael@0 | 248 | #endif |
michael@0 | 249 | |
michael@0 | 250 | static bool RemoveDirectorySubItems2(const CSysString pathPrefix, const NFind::CFileInfo &fileInfo) |
michael@0 | 251 | { |
michael@0 | 252 | if(fileInfo.IsDirectory()) |
michael@0 | 253 | return RemoveDirectoryWithSubItems(pathPrefix + fileInfo.Name); |
michael@0 | 254 | else |
michael@0 | 255 | return DeleteFileAlways(pathPrefix + fileInfo.Name); |
michael@0 | 256 | } |
michael@0 | 257 | |
michael@0 | 258 | bool RemoveDirectoryWithSubItems(const CSysString &path) |
michael@0 | 259 | { |
michael@0 | 260 | NFind::CFileInfo fileInfo; |
michael@0 | 261 | CSysString pathPrefix = path + NName::kDirDelimiter; |
michael@0 | 262 | { |
michael@0 | 263 | NFind::CEnumerator enumerator(pathPrefix + TCHAR(NName::kAnyStringWildcard)); |
michael@0 | 264 | while(enumerator.Next(fileInfo)) |
michael@0 | 265 | if(!RemoveDirectorySubItems2(pathPrefix, fileInfo)) |
michael@0 | 266 | return false; |
michael@0 | 267 | } |
michael@0 | 268 | if(!BOOLToBool(::SetFileAttributes(path, 0))) |
michael@0 | 269 | return false; |
michael@0 | 270 | return BOOLToBool(::RemoveDirectory(path)); |
michael@0 | 271 | } |
michael@0 | 272 | |
michael@0 | 273 | #ifndef _UNICODE |
michael@0 | 274 | static bool RemoveDirectorySubItems2(const UString pathPrefix, const NFind::CFileInfoW &fileInfo) |
michael@0 | 275 | { |
michael@0 | 276 | if(fileInfo.IsDirectory()) |
michael@0 | 277 | return RemoveDirectoryWithSubItems(pathPrefix + fileInfo.Name); |
michael@0 | 278 | else |
michael@0 | 279 | return DeleteFileAlways(pathPrefix + fileInfo.Name); |
michael@0 | 280 | } |
michael@0 | 281 | bool RemoveDirectoryWithSubItems(const UString &path) |
michael@0 | 282 | { |
michael@0 | 283 | NFind::CFileInfoW fileInfo; |
michael@0 | 284 | UString pathPrefix = path + UString(NName::kDirDelimiter); |
michael@0 | 285 | { |
michael@0 | 286 | NFind::CEnumeratorW enumerator(pathPrefix + UString(NName::kAnyStringWildcard)); |
michael@0 | 287 | while(enumerator.Next(fileInfo)) |
michael@0 | 288 | if(!RemoveDirectorySubItems2(pathPrefix, fileInfo)) |
michael@0 | 289 | return false; |
michael@0 | 290 | } |
michael@0 | 291 | if(!MySetFileAttributes(path, 0)) |
michael@0 | 292 | return false; |
michael@0 | 293 | return MyRemoveDirectory(path); |
michael@0 | 294 | } |
michael@0 | 295 | #endif |
michael@0 | 296 | |
michael@0 | 297 | #ifndef _WIN32_WCE |
michael@0 | 298 | |
michael@0 | 299 | bool MyGetShortPathName(LPCTSTR longPath, CSysString &shortPath) |
michael@0 | 300 | { |
michael@0 | 301 | DWORD needLength = ::GetShortPathName(longPath, shortPath.GetBuffer(MAX_PATH + 1), MAX_PATH + 1); |
michael@0 | 302 | shortPath.ReleaseBuffer(); |
michael@0 | 303 | return (needLength > 0 && needLength < MAX_PATH); |
michael@0 | 304 | } |
michael@0 | 305 | |
michael@0 | 306 | bool MyGetFullPathName(LPCTSTR fileName, CSysString &resultPath, int &fileNamePartStartIndex) |
michael@0 | 307 | { |
michael@0 | 308 | resultPath.Empty(); |
michael@0 | 309 | LPTSTR fileNamePointer = 0; |
michael@0 | 310 | LPTSTR buffer = resultPath.GetBuffer(MAX_PATH); |
michael@0 | 311 | DWORD needLength = ::GetFullPathName(fileName, MAX_PATH + 1, buffer, &fileNamePointer); |
michael@0 | 312 | resultPath.ReleaseBuffer(); |
michael@0 | 313 | if (needLength == 0 || needLength >= MAX_PATH) |
michael@0 | 314 | return false; |
michael@0 | 315 | if (fileNamePointer == 0) |
michael@0 | 316 | fileNamePartStartIndex = lstrlen(fileName); |
michael@0 | 317 | else |
michael@0 | 318 | fileNamePartStartIndex = (int)(fileNamePointer - buffer); |
michael@0 | 319 | return true; |
michael@0 | 320 | } |
michael@0 | 321 | |
michael@0 | 322 | #ifndef _UNICODE |
michael@0 | 323 | bool MyGetFullPathName(LPCWSTR fileName, UString &resultPath, int &fileNamePartStartIndex) |
michael@0 | 324 | { |
michael@0 | 325 | resultPath.Empty(); |
michael@0 | 326 | if (g_IsNT) |
michael@0 | 327 | { |
michael@0 | 328 | LPWSTR fileNamePointer = 0; |
michael@0 | 329 | LPWSTR buffer = resultPath.GetBuffer(MAX_PATH); |
michael@0 | 330 | DWORD needLength = ::GetFullPathNameW(fileName, MAX_PATH + 1, buffer, &fileNamePointer); |
michael@0 | 331 | resultPath.ReleaseBuffer(); |
michael@0 | 332 | if (needLength == 0 || needLength >= MAX_PATH) |
michael@0 | 333 | return false; |
michael@0 | 334 | if (fileNamePointer == 0) |
michael@0 | 335 | fileNamePartStartIndex = MyStringLen(fileName); |
michael@0 | 336 | else |
michael@0 | 337 | fileNamePartStartIndex = (int)(fileNamePointer - buffer); |
michael@0 | 338 | } |
michael@0 | 339 | else |
michael@0 | 340 | { |
michael@0 | 341 | CSysString sysPath; |
michael@0 | 342 | if (!MyGetFullPathName(GetSysPath(fileName), sysPath, fileNamePartStartIndex)) |
michael@0 | 343 | return false; |
michael@0 | 344 | UString resultPath1 = GetUnicodePath(sysPath.Left(fileNamePartStartIndex)); |
michael@0 | 345 | UString resultPath2 = GetUnicodePath(sysPath.Mid(fileNamePartStartIndex)); |
michael@0 | 346 | fileNamePartStartIndex = resultPath1.Length(); |
michael@0 | 347 | resultPath = resultPath1 + resultPath2; |
michael@0 | 348 | } |
michael@0 | 349 | return true; |
michael@0 | 350 | } |
michael@0 | 351 | #endif |
michael@0 | 352 | |
michael@0 | 353 | |
michael@0 | 354 | bool MyGetFullPathName(LPCTSTR fileName, CSysString &path) |
michael@0 | 355 | { |
michael@0 | 356 | int index; |
michael@0 | 357 | return MyGetFullPathName(fileName, path, index); |
michael@0 | 358 | } |
michael@0 | 359 | |
michael@0 | 360 | #ifndef _UNICODE |
michael@0 | 361 | bool MyGetFullPathName(LPCWSTR fileName, UString &path) |
michael@0 | 362 | { |
michael@0 | 363 | int index; |
michael@0 | 364 | return MyGetFullPathName(fileName, path, index); |
michael@0 | 365 | } |
michael@0 | 366 | #endif |
michael@0 | 367 | |
michael@0 | 368 | bool GetOnlyName(LPCTSTR fileName, CSysString &resultName) |
michael@0 | 369 | { |
michael@0 | 370 | int index; |
michael@0 | 371 | if (!MyGetFullPathName(fileName, resultName, index)) |
michael@0 | 372 | return false; |
michael@0 | 373 | resultName = resultName.Mid(index); |
michael@0 | 374 | return true; |
michael@0 | 375 | } |
michael@0 | 376 | |
michael@0 | 377 | #ifndef _UNICODE |
michael@0 | 378 | bool GetOnlyName(LPCWSTR fileName, UString &resultName) |
michael@0 | 379 | { |
michael@0 | 380 | int index; |
michael@0 | 381 | if (!MyGetFullPathName(fileName, resultName, index)) |
michael@0 | 382 | return false; |
michael@0 | 383 | resultName = resultName.Mid(index); |
michael@0 | 384 | return true; |
michael@0 | 385 | } |
michael@0 | 386 | #endif |
michael@0 | 387 | |
michael@0 | 388 | bool GetOnlyDirPrefix(LPCTSTR fileName, CSysString &resultName) |
michael@0 | 389 | { |
michael@0 | 390 | int index; |
michael@0 | 391 | if (!MyGetFullPathName(fileName, resultName, index)) |
michael@0 | 392 | return false; |
michael@0 | 393 | resultName = resultName.Left(index); |
michael@0 | 394 | return true; |
michael@0 | 395 | } |
michael@0 | 396 | |
michael@0 | 397 | #ifndef _UNICODE |
michael@0 | 398 | bool GetOnlyDirPrefix(LPCWSTR fileName, UString &resultName) |
michael@0 | 399 | { |
michael@0 | 400 | int index; |
michael@0 | 401 | if (!MyGetFullPathName(fileName, resultName, index)) |
michael@0 | 402 | return false; |
michael@0 | 403 | resultName = resultName.Left(index); |
michael@0 | 404 | return true; |
michael@0 | 405 | } |
michael@0 | 406 | #endif |
michael@0 | 407 | |
michael@0 | 408 | bool MyGetCurrentDirectory(CSysString &path) |
michael@0 | 409 | { |
michael@0 | 410 | DWORD needLength = ::GetCurrentDirectory(MAX_PATH + 1, path.GetBuffer(MAX_PATH + 1)); |
michael@0 | 411 | path.ReleaseBuffer(); |
michael@0 | 412 | return (needLength > 0 && needLength <= MAX_PATH); |
michael@0 | 413 | } |
michael@0 | 414 | |
michael@0 | 415 | #ifndef _UNICODE |
michael@0 | 416 | bool MySetCurrentDirectory(LPCWSTR path) |
michael@0 | 417 | { |
michael@0 | 418 | if (g_IsNT) |
michael@0 | 419 | return BOOLToBool(::SetCurrentDirectoryW(path)); |
michael@0 | 420 | return MySetCurrentDirectory(GetSysPath(path)); |
michael@0 | 421 | } |
michael@0 | 422 | bool MyGetCurrentDirectory(UString &path) |
michael@0 | 423 | { |
michael@0 | 424 | if (g_IsNT) |
michael@0 | 425 | { |
michael@0 | 426 | DWORD needLength = ::GetCurrentDirectoryW(MAX_PATH + 1, path.GetBuffer(MAX_PATH + 1)); |
michael@0 | 427 | path.ReleaseBuffer(); |
michael@0 | 428 | return (needLength > 0 && needLength <= MAX_PATH); |
michael@0 | 429 | } |
michael@0 | 430 | CSysString sysPath; |
michael@0 | 431 | if (!MyGetCurrentDirectory(sysPath)) |
michael@0 | 432 | return false; |
michael@0 | 433 | path = GetUnicodePath(sysPath); |
michael@0 | 434 | return true; |
michael@0 | 435 | } |
michael@0 | 436 | #endif |
michael@0 | 437 | #endif |
michael@0 | 438 | |
michael@0 | 439 | bool MySearchPath(LPCTSTR path, LPCTSTR fileName, LPCTSTR extension, |
michael@0 | 440 | CSysString &resultPath, UINT32 &filePart) |
michael@0 | 441 | { |
michael@0 | 442 | LPTSTR filePartPointer; |
michael@0 | 443 | DWORD value = ::SearchPath(path, fileName, extension, |
michael@0 | 444 | MAX_PATH, resultPath.GetBuffer(MAX_PATH + 1), &filePartPointer); |
michael@0 | 445 | filePart = (UINT32)(filePartPointer - (LPCTSTR)resultPath); |
michael@0 | 446 | resultPath.ReleaseBuffer(); |
michael@0 | 447 | return (value > 0 && value <= MAX_PATH); |
michael@0 | 448 | } |
michael@0 | 449 | |
michael@0 | 450 | #ifndef _UNICODE |
michael@0 | 451 | bool MySearchPath(LPCWSTR path, LPCWSTR fileName, LPCWSTR extension, |
michael@0 | 452 | UString &resultPath, UINT32 &filePart) |
michael@0 | 453 | { |
michael@0 | 454 | if (g_IsNT) |
michael@0 | 455 | { |
michael@0 | 456 | LPWSTR filePartPointer = 0; |
michael@0 | 457 | DWORD value = ::SearchPathW(path, fileName, extension, |
michael@0 | 458 | MAX_PATH, resultPath.GetBuffer(MAX_PATH + 1), &filePartPointer); |
michael@0 | 459 | filePart = (UINT32)(filePartPointer - (LPCWSTR)resultPath); |
michael@0 | 460 | resultPath.ReleaseBuffer(); |
michael@0 | 461 | return (value > 0 && value <= MAX_PATH); |
michael@0 | 462 | } |
michael@0 | 463 | |
michael@0 | 464 | CSysString sysPath; |
michael@0 | 465 | if (!MySearchPath( |
michael@0 | 466 | path != 0 ? (LPCTSTR)GetSysPath(path): 0, |
michael@0 | 467 | fileName != 0 ? (LPCTSTR)GetSysPath(fileName): 0, |
michael@0 | 468 | extension != 0 ? (LPCTSTR)GetSysPath(extension): 0, |
michael@0 | 469 | sysPath, filePart)) |
michael@0 | 470 | return false; |
michael@0 | 471 | UString resultPath1 = GetUnicodePath(sysPath.Left(filePart)); |
michael@0 | 472 | UString resultPath2 = GetUnicodePath(sysPath.Mid(filePart)); |
michael@0 | 473 | filePart = resultPath1.Length(); |
michael@0 | 474 | resultPath = resultPath1 + resultPath2; |
michael@0 | 475 | return true; |
michael@0 | 476 | } |
michael@0 | 477 | #endif |
michael@0 | 478 | |
michael@0 | 479 | bool MyGetTempPath(CSysString &path) |
michael@0 | 480 | { |
michael@0 | 481 | DWORD needLength = ::GetTempPath(MAX_PATH + 1, path.GetBuffer(MAX_PATH + 1)); |
michael@0 | 482 | path.ReleaseBuffer(); |
michael@0 | 483 | return (needLength > 0 && needLength <= MAX_PATH); |
michael@0 | 484 | } |
michael@0 | 485 | |
michael@0 | 486 | #ifndef _UNICODE |
michael@0 | 487 | bool MyGetTempPath(UString &path) |
michael@0 | 488 | { |
michael@0 | 489 | path.Empty(); |
michael@0 | 490 | if (g_IsNT) |
michael@0 | 491 | { |
michael@0 | 492 | DWORD needLength = ::GetTempPathW(MAX_PATH + 1, path.GetBuffer(MAX_PATH + 1)); |
michael@0 | 493 | path.ReleaseBuffer(); |
michael@0 | 494 | return (needLength > 0 && needLength <= MAX_PATH); |
michael@0 | 495 | } |
michael@0 | 496 | CSysString sysPath; |
michael@0 | 497 | if (!MyGetTempPath(sysPath)) |
michael@0 | 498 | return false; |
michael@0 | 499 | path = GetUnicodePath(sysPath); |
michael@0 | 500 | return true; |
michael@0 | 501 | } |
michael@0 | 502 | #endif |
michael@0 | 503 | |
michael@0 | 504 | UINT MyGetTempFileName(LPCTSTR dirPath, LPCTSTR prefix, CSysString &path) |
michael@0 | 505 | { |
michael@0 | 506 | UINT number = ::GetTempFileName(dirPath, prefix, 0, path.GetBuffer(MAX_PATH + 1)); |
michael@0 | 507 | path.ReleaseBuffer(); |
michael@0 | 508 | return number; |
michael@0 | 509 | } |
michael@0 | 510 | |
michael@0 | 511 | #ifndef _UNICODE |
michael@0 | 512 | UINT MyGetTempFileName(LPCWSTR dirPath, LPCWSTR prefix, UString &path) |
michael@0 | 513 | { |
michael@0 | 514 | if (g_IsNT) |
michael@0 | 515 | { |
michael@0 | 516 | UINT number = ::GetTempFileNameW(dirPath, prefix, 0, path.GetBuffer(MAX_PATH)); |
michael@0 | 517 | path.ReleaseBuffer(); |
michael@0 | 518 | return number; |
michael@0 | 519 | } |
michael@0 | 520 | CSysString sysPath; |
michael@0 | 521 | UINT number = MyGetTempFileName( |
michael@0 | 522 | dirPath ? (LPCTSTR)GetSysPath(dirPath): 0, |
michael@0 | 523 | prefix ? (LPCTSTR)GetSysPath(prefix): 0, |
michael@0 | 524 | sysPath); |
michael@0 | 525 | path = GetUnicodePath(sysPath); |
michael@0 | 526 | return number; |
michael@0 | 527 | } |
michael@0 | 528 | #endif |
michael@0 | 529 | |
michael@0 | 530 | UINT CTempFile::Create(LPCTSTR dirPath, LPCTSTR prefix, CSysString &resultPath) |
michael@0 | 531 | { |
michael@0 | 532 | Remove(); |
michael@0 | 533 | UINT number = MyGetTempFileName(dirPath, prefix, resultPath); |
michael@0 | 534 | if(number != 0) |
michael@0 | 535 | { |
michael@0 | 536 | _fileName = resultPath; |
michael@0 | 537 | _mustBeDeleted = true; |
michael@0 | 538 | } |
michael@0 | 539 | return number; |
michael@0 | 540 | } |
michael@0 | 541 | |
michael@0 | 542 | bool CTempFile::Create(LPCTSTR prefix, CSysString &resultPath) |
michael@0 | 543 | { |
michael@0 | 544 | CSysString tempPath; |
michael@0 | 545 | if(!MyGetTempPath(tempPath)) |
michael@0 | 546 | return false; |
michael@0 | 547 | if (Create(tempPath, prefix, resultPath) != 0) |
michael@0 | 548 | return true; |
michael@0 | 549 | if(!MyGetWindowsDirectory(tempPath)) |
michael@0 | 550 | return false; |
michael@0 | 551 | return (Create(tempPath, prefix, resultPath) != 0); |
michael@0 | 552 | } |
michael@0 | 553 | |
michael@0 | 554 | bool CTempFile::Remove() |
michael@0 | 555 | { |
michael@0 | 556 | if (!_mustBeDeleted) |
michael@0 | 557 | return true; |
michael@0 | 558 | _mustBeDeleted = !DeleteFileAlways(_fileName); |
michael@0 | 559 | return !_mustBeDeleted; |
michael@0 | 560 | } |
michael@0 | 561 | |
michael@0 | 562 | #ifndef _UNICODE |
michael@0 | 563 | |
michael@0 | 564 | UINT CTempFileW::Create(LPCWSTR dirPath, LPCWSTR prefix, UString &resultPath) |
michael@0 | 565 | { |
michael@0 | 566 | Remove(); |
michael@0 | 567 | UINT number = MyGetTempFileName(dirPath, prefix, resultPath); |
michael@0 | 568 | if(number != 0) |
michael@0 | 569 | { |
michael@0 | 570 | _fileName = resultPath; |
michael@0 | 571 | _mustBeDeleted = true; |
michael@0 | 572 | } |
michael@0 | 573 | return number; |
michael@0 | 574 | } |
michael@0 | 575 | |
michael@0 | 576 | bool CTempFileW::Create(LPCWSTR prefix, UString &resultPath) |
michael@0 | 577 | { |
michael@0 | 578 | UString tempPath; |
michael@0 | 579 | if(!MyGetTempPath(tempPath)) |
michael@0 | 580 | return false; |
michael@0 | 581 | if (Create(tempPath, prefix, resultPath) != 0) |
michael@0 | 582 | return true; |
michael@0 | 583 | if(!MyGetWindowsDirectory(tempPath)) |
michael@0 | 584 | return false; |
michael@0 | 585 | return (Create(tempPath, prefix, resultPath) != 0); |
michael@0 | 586 | } |
michael@0 | 587 | |
michael@0 | 588 | bool CTempFileW::Remove() |
michael@0 | 589 | { |
michael@0 | 590 | if (!_mustBeDeleted) |
michael@0 | 591 | return true; |
michael@0 | 592 | _mustBeDeleted = !DeleteFileAlways(_fileName); |
michael@0 | 593 | return !_mustBeDeleted; |
michael@0 | 594 | } |
michael@0 | 595 | |
michael@0 | 596 | #endif |
michael@0 | 597 | |
michael@0 | 598 | bool CreateTempDirectory(LPCTSTR prefix, CSysString &dirName) |
michael@0 | 599 | { |
michael@0 | 600 | /* |
michael@0 | 601 | CSysString prefix = tempPath + prefixChars; |
michael@0 | 602 | CRandom random; |
michael@0 | 603 | random.Init(); |
michael@0 | 604 | */ |
michael@0 | 605 | while(true) |
michael@0 | 606 | { |
michael@0 | 607 | CTempFile tempFile; |
michael@0 | 608 | if (!tempFile.Create(prefix, dirName)) |
michael@0 | 609 | return false; |
michael@0 | 610 | if (!::DeleteFile(dirName)) |
michael@0 | 611 | return false; |
michael@0 | 612 | /* |
michael@0 | 613 | UINT32 randomNumber = random.Generate(); |
michael@0 | 614 | TCHAR randomNumberString[32]; |
michael@0 | 615 | _stprintf(randomNumberString, _T("%04X"), randomNumber); |
michael@0 | 616 | dirName = prefix + randomNumberString; |
michael@0 | 617 | */ |
michael@0 | 618 | if(NFind::DoesFileExist(dirName)) |
michael@0 | 619 | continue; |
michael@0 | 620 | if (MyCreateDirectory(dirName)) |
michael@0 | 621 | return true; |
michael@0 | 622 | if (::GetLastError() != ERROR_ALREADY_EXISTS) |
michael@0 | 623 | return false; |
michael@0 | 624 | } |
michael@0 | 625 | } |
michael@0 | 626 | |
michael@0 | 627 | bool CTempDirectory::Create(LPCTSTR prefix) |
michael@0 | 628 | { |
michael@0 | 629 | Remove(); |
michael@0 | 630 | return (_mustBeDeleted = CreateTempDirectory(prefix, _tempDir)); |
michael@0 | 631 | } |
michael@0 | 632 | |
michael@0 | 633 | #ifndef _UNICODE |
michael@0 | 634 | |
michael@0 | 635 | bool CreateTempDirectory(LPCWSTR prefix, UString &dirName) |
michael@0 | 636 | { |
michael@0 | 637 | /* |
michael@0 | 638 | CSysString prefix = tempPath + prefixChars; |
michael@0 | 639 | CRandom random; |
michael@0 | 640 | random.Init(); |
michael@0 | 641 | */ |
michael@0 | 642 | while(true) |
michael@0 | 643 | { |
michael@0 | 644 | CTempFileW tempFile; |
michael@0 | 645 | if (!tempFile.Create(prefix, dirName)) |
michael@0 | 646 | return false; |
michael@0 | 647 | if (!DeleteFileAlways(dirName)) |
michael@0 | 648 | return false; |
michael@0 | 649 | /* |
michael@0 | 650 | UINT32 randomNumber = random.Generate(); |
michael@0 | 651 | TCHAR randomNumberString[32]; |
michael@0 | 652 | _stprintf(randomNumberString, _T("%04X"), randomNumber); |
michael@0 | 653 | dirName = prefix + randomNumberString; |
michael@0 | 654 | */ |
michael@0 | 655 | if(NFind::DoesFileExist(dirName)) |
michael@0 | 656 | continue; |
michael@0 | 657 | if (MyCreateDirectory(dirName)) |
michael@0 | 658 | return true; |
michael@0 | 659 | if (::GetLastError() != ERROR_ALREADY_EXISTS) |
michael@0 | 660 | return false; |
michael@0 | 661 | } |
michael@0 | 662 | } |
michael@0 | 663 | |
michael@0 | 664 | bool CTempDirectoryW::Create(LPCWSTR prefix) |
michael@0 | 665 | { |
michael@0 | 666 | Remove(); |
michael@0 | 667 | return (_mustBeDeleted = CreateTempDirectory(prefix, _tempDir)); |
michael@0 | 668 | } |
michael@0 | 669 | |
michael@0 | 670 | #endif |
michael@0 | 671 | |
michael@0 | 672 | }}} |