1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/other-licenses/7zstub/src/Windows/FileDir.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,672 @@ 1.4 +// Windows/FileDir.cpp 1.5 + 1.6 +#include "StdAfx.h" 1.7 + 1.8 +#include "FileDir.h" 1.9 +#include "FileName.h" 1.10 +#include "FileFind.h" 1.11 +#include "Defs.h" 1.12 +#ifndef _UNICODE 1.13 +#include "../Common/StringConvert.h" 1.14 +#endif 1.15 + 1.16 +#ifndef _UNICODE 1.17 +extern bool g_IsNT; 1.18 +#endif 1.19 + 1.20 +namespace NWindows { 1.21 +namespace NFile { 1.22 +namespace NDirectory { 1.23 + 1.24 +#ifndef _UNICODE 1.25 +static inline UINT GetCurrentCodePage() { return ::AreFileApisANSI() ? CP_ACP : CP_OEMCP; } 1.26 +static UString GetUnicodePath(const CSysString &sysPath) 1.27 + { return MultiByteToUnicodeString(sysPath, GetCurrentCodePage()); } 1.28 +static CSysString GetSysPath(LPCWSTR sysPath) 1.29 + { return UnicodeStringToMultiByte(sysPath, GetCurrentCodePage()); } 1.30 +#endif 1.31 + 1.32 +bool MyGetWindowsDirectory(CSysString &path) 1.33 +{ 1.34 + UINT needLength = ::GetWindowsDirectory(path.GetBuffer(MAX_PATH + 1), MAX_PATH + 1); 1.35 + path.ReleaseBuffer(); 1.36 + return (needLength > 0 && needLength <= MAX_PATH); 1.37 +} 1.38 + 1.39 +bool MyGetSystemDirectory(CSysString &path) 1.40 +{ 1.41 + UINT needLength = ::GetSystemDirectory(path.GetBuffer(MAX_PATH + 1), MAX_PATH + 1); 1.42 + path.ReleaseBuffer(); 1.43 + return (needLength > 0 && needLength <= MAX_PATH); 1.44 +} 1.45 + 1.46 +#ifndef _UNICODE 1.47 +bool MyGetWindowsDirectory(UString &path) 1.48 +{ 1.49 + if (g_IsNT) 1.50 + { 1.51 + UINT needLength = ::GetWindowsDirectoryW(path.GetBuffer(MAX_PATH + 1), MAX_PATH + 1); 1.52 + path.ReleaseBuffer(); 1.53 + return (needLength > 0 && needLength <= MAX_PATH); 1.54 + } 1.55 + CSysString sysPath; 1.56 + if (!MyGetWindowsDirectory(sysPath)) 1.57 + return false; 1.58 + path = GetUnicodePath(sysPath); 1.59 + return true; 1.60 +} 1.61 + 1.62 +bool MyGetSystemDirectory(UString &path) 1.63 +{ 1.64 + if (g_IsNT) 1.65 + { 1.66 + UINT needLength = ::GetSystemDirectoryW(path.GetBuffer(MAX_PATH + 1), MAX_PATH + 1); 1.67 + path.ReleaseBuffer(); 1.68 + return (needLength > 0 && needLength <= MAX_PATH); 1.69 + } 1.70 + CSysString sysPath; 1.71 + if (!MyGetSystemDirectory(sysPath)) 1.72 + return false; 1.73 + path = GetUnicodePath(sysPath); 1.74 + return true; 1.75 +} 1.76 +#endif 1.77 + 1.78 +#ifndef _UNICODE 1.79 +bool MySetFileAttributes(LPCWSTR fileName, DWORD fileAttributes) 1.80 +{ 1.81 + if (g_IsNT) 1.82 + return BOOLToBool(::SetFileAttributesW(fileName, fileAttributes)); 1.83 + return MySetFileAttributes(GetSysPath(fileName), fileAttributes); 1.84 +} 1.85 + 1.86 +bool MyRemoveDirectory(LPCWSTR pathName) 1.87 +{ 1.88 + if (g_IsNT) 1.89 + return BOOLToBool(::RemoveDirectoryW(pathName)); 1.90 + return MyRemoveDirectory(GetSysPath(pathName)); 1.91 +} 1.92 + 1.93 +bool MyMoveFile(LPCWSTR existFileName, LPCWSTR newFileName) 1.94 +{ 1.95 + if (g_IsNT) 1.96 + return BOOLToBool(::MoveFileW(existFileName, newFileName)); 1.97 + return MyMoveFile(GetSysPath(existFileName), GetSysPath(newFileName)); 1.98 +} 1.99 +#endif 1.100 + 1.101 +bool MyCreateDirectory(LPCTSTR pathName) { return BOOLToBool(::CreateDirectory(pathName, NULL)); } 1.102 + 1.103 +#ifndef _UNICODE 1.104 +bool MyCreateDirectory(LPCWSTR pathName) 1.105 +{ 1.106 + if (g_IsNT) 1.107 + return BOOLToBool(::CreateDirectoryW(pathName, NULL)); 1.108 + return MyCreateDirectory(GetSysPath(pathName)); 1.109 +} 1.110 +#endif 1.111 + 1.112 +/* 1.113 +bool CreateComplexDirectory(LPCTSTR pathName) 1.114 +{ 1.115 + NName::CParsedPath path; 1.116 + path.ParsePath(pathName); 1.117 + CSysString fullPath = path.Prefix; 1.118 + DWORD errorCode = ERROR_SUCCESS; 1.119 + for(int i = 0; i < path.PathParts.Size(); i++) 1.120 + { 1.121 + const CSysString &string = path.PathParts[i]; 1.122 + if(string.IsEmpty()) 1.123 + { 1.124 + if(i != path.PathParts.Size() - 1) 1.125 + return false; 1.126 + return true; 1.127 + } 1.128 + fullPath += path.PathParts[i]; 1.129 + if(!MyCreateDirectory(fullPath)) 1.130 + { 1.131 + DWORD errorCode = GetLastError(); 1.132 + if(errorCode != ERROR_ALREADY_EXISTS) 1.133 + return false; 1.134 + } 1.135 + fullPath += NName::kDirDelimiter; 1.136 + } 1.137 + return true; 1.138 +} 1.139 +*/ 1.140 + 1.141 +bool CreateComplexDirectory(LPCTSTR _aPathName) 1.142 +{ 1.143 + CSysString pathName = _aPathName; 1.144 + int pos = pathName.ReverseFind(TEXT(CHAR_PATH_SEPARATOR)); 1.145 + if (pos > 0 && pos == pathName.Length() - 1) 1.146 + { 1.147 + if (pathName.Length() == 3 && pathName[1] == ':') 1.148 + return true; // Disk folder; 1.149 + pathName.Delete(pos); 1.150 + } 1.151 + CSysString pathName2 = pathName; 1.152 + pos = pathName.Length(); 1.153 + while(true) 1.154 + { 1.155 + if(MyCreateDirectory(pathName)) 1.156 + break; 1.157 + if(::GetLastError() == ERROR_ALREADY_EXISTS) 1.158 + { 1.159 + NFind::CFileInfo fileInfo; 1.160 + if (!NFind::FindFile(pathName, fileInfo)) // For network folders 1.161 + return true; 1.162 + if (!fileInfo.IsDirectory()) 1.163 + return false; 1.164 + break; 1.165 + } 1.166 + pos = pathName.ReverseFind(TEXT(CHAR_PATH_SEPARATOR)); 1.167 + if (pos < 0 || pos == 0) 1.168 + return false; 1.169 + if (pathName[pos - 1] == ':') 1.170 + return false; 1.171 + pathName = pathName.Left(pos); 1.172 + } 1.173 + pathName = pathName2; 1.174 + while(pos < pathName.Length()) 1.175 + { 1.176 + pos = pathName.Find(TEXT(CHAR_PATH_SEPARATOR), pos + 1); 1.177 + if (pos < 0) 1.178 + pos = pathName.Length(); 1.179 + if(!MyCreateDirectory(pathName.Left(pos))) 1.180 + return false; 1.181 + } 1.182 + return true; 1.183 +} 1.184 + 1.185 +#ifndef _UNICODE 1.186 + 1.187 +bool CreateComplexDirectory(LPCWSTR _aPathName) 1.188 +{ 1.189 + UString pathName = _aPathName; 1.190 + int pos = pathName.ReverseFind(WCHAR_PATH_SEPARATOR); 1.191 + if (pos > 0 && pos == pathName.Length() - 1) 1.192 + { 1.193 + if (pathName.Length() == 3 && pathName[1] == L':') 1.194 + return true; // Disk folder; 1.195 + pathName.Delete(pos); 1.196 + } 1.197 + UString pathName2 = pathName; 1.198 + pos = pathName.Length(); 1.199 + while(true) 1.200 + { 1.201 + if(MyCreateDirectory(pathName)) 1.202 + break; 1.203 + if(::GetLastError() == ERROR_ALREADY_EXISTS) 1.204 + { 1.205 + NFind::CFileInfoW fileInfo; 1.206 + if (!NFind::FindFile(pathName, fileInfo)) // For network folders 1.207 + return true; 1.208 + if (!fileInfo.IsDirectory()) 1.209 + return false; 1.210 + break; 1.211 + } 1.212 + pos = pathName.ReverseFind(WCHAR_PATH_SEPARATOR); 1.213 + if (pos < 0 || pos == 0) 1.214 + return false; 1.215 + if (pathName[pos - 1] == L':') 1.216 + return false; 1.217 + pathName = pathName.Left(pos); 1.218 + } 1.219 + pathName = pathName2; 1.220 + while(pos < pathName.Length()) 1.221 + { 1.222 + pos = pathName.Find(WCHAR_PATH_SEPARATOR, pos + 1); 1.223 + if (pos < 0) 1.224 + pos = pathName.Length(); 1.225 + if(!MyCreateDirectory(pathName.Left(pos))) 1.226 + return false; 1.227 + } 1.228 + return true; 1.229 +} 1.230 + 1.231 +#endif 1.232 + 1.233 +bool DeleteFileAlways(LPCTSTR name) 1.234 +{ 1.235 + if(!::SetFileAttributes(name, 0)) 1.236 + return false; 1.237 + return BOOLToBool(::DeleteFile(name)); 1.238 +} 1.239 + 1.240 +#ifndef _UNICODE 1.241 +bool DeleteFileAlways(LPCWSTR name) 1.242 +{ 1.243 + if (g_IsNT) 1.244 + { 1.245 + if(!MySetFileAttributes(name, 0)) 1.246 + return false; 1.247 + return BOOLToBool(::DeleteFileW(name)); 1.248 + } 1.249 + return DeleteFileAlways(GetSysPath(name)); 1.250 +} 1.251 +#endif 1.252 + 1.253 +static bool RemoveDirectorySubItems2(const CSysString pathPrefix, const NFind::CFileInfo &fileInfo) 1.254 +{ 1.255 + if(fileInfo.IsDirectory()) 1.256 + return RemoveDirectoryWithSubItems(pathPrefix + fileInfo.Name); 1.257 + else 1.258 + return DeleteFileAlways(pathPrefix + fileInfo.Name); 1.259 +} 1.260 + 1.261 +bool RemoveDirectoryWithSubItems(const CSysString &path) 1.262 +{ 1.263 + NFind::CFileInfo fileInfo; 1.264 + CSysString pathPrefix = path + NName::kDirDelimiter; 1.265 + { 1.266 + NFind::CEnumerator enumerator(pathPrefix + TCHAR(NName::kAnyStringWildcard)); 1.267 + while(enumerator.Next(fileInfo)) 1.268 + if(!RemoveDirectorySubItems2(pathPrefix, fileInfo)) 1.269 + return false; 1.270 + } 1.271 + if(!BOOLToBool(::SetFileAttributes(path, 0))) 1.272 + return false; 1.273 + return BOOLToBool(::RemoveDirectory(path)); 1.274 +} 1.275 + 1.276 +#ifndef _UNICODE 1.277 +static bool RemoveDirectorySubItems2(const UString pathPrefix, const NFind::CFileInfoW &fileInfo) 1.278 +{ 1.279 + if(fileInfo.IsDirectory()) 1.280 + return RemoveDirectoryWithSubItems(pathPrefix + fileInfo.Name); 1.281 + else 1.282 + return DeleteFileAlways(pathPrefix + fileInfo.Name); 1.283 +} 1.284 +bool RemoveDirectoryWithSubItems(const UString &path) 1.285 +{ 1.286 + NFind::CFileInfoW fileInfo; 1.287 + UString pathPrefix = path + UString(NName::kDirDelimiter); 1.288 + { 1.289 + NFind::CEnumeratorW enumerator(pathPrefix + UString(NName::kAnyStringWildcard)); 1.290 + while(enumerator.Next(fileInfo)) 1.291 + if(!RemoveDirectorySubItems2(pathPrefix, fileInfo)) 1.292 + return false; 1.293 + } 1.294 + if(!MySetFileAttributes(path, 0)) 1.295 + return false; 1.296 + return MyRemoveDirectory(path); 1.297 +} 1.298 +#endif 1.299 + 1.300 +#ifndef _WIN32_WCE 1.301 + 1.302 +bool MyGetShortPathName(LPCTSTR longPath, CSysString &shortPath) 1.303 +{ 1.304 + DWORD needLength = ::GetShortPathName(longPath, shortPath.GetBuffer(MAX_PATH + 1), MAX_PATH + 1); 1.305 + shortPath.ReleaseBuffer(); 1.306 + return (needLength > 0 && needLength < MAX_PATH); 1.307 +} 1.308 + 1.309 +bool MyGetFullPathName(LPCTSTR fileName, CSysString &resultPath, int &fileNamePartStartIndex) 1.310 +{ 1.311 + resultPath.Empty(); 1.312 + LPTSTR fileNamePointer = 0; 1.313 + LPTSTR buffer = resultPath.GetBuffer(MAX_PATH); 1.314 + DWORD needLength = ::GetFullPathName(fileName, MAX_PATH + 1, buffer, &fileNamePointer); 1.315 + resultPath.ReleaseBuffer(); 1.316 + if (needLength == 0 || needLength >= MAX_PATH) 1.317 + return false; 1.318 + if (fileNamePointer == 0) 1.319 + fileNamePartStartIndex = lstrlen(fileName); 1.320 + else 1.321 + fileNamePartStartIndex = (int)(fileNamePointer - buffer); 1.322 + return true; 1.323 +} 1.324 + 1.325 +#ifndef _UNICODE 1.326 +bool MyGetFullPathName(LPCWSTR fileName, UString &resultPath, int &fileNamePartStartIndex) 1.327 +{ 1.328 + resultPath.Empty(); 1.329 + if (g_IsNT) 1.330 + { 1.331 + LPWSTR fileNamePointer = 0; 1.332 + LPWSTR buffer = resultPath.GetBuffer(MAX_PATH); 1.333 + DWORD needLength = ::GetFullPathNameW(fileName, MAX_PATH + 1, buffer, &fileNamePointer); 1.334 + resultPath.ReleaseBuffer(); 1.335 + if (needLength == 0 || needLength >= MAX_PATH) 1.336 + return false; 1.337 + if (fileNamePointer == 0) 1.338 + fileNamePartStartIndex = MyStringLen(fileName); 1.339 + else 1.340 + fileNamePartStartIndex = (int)(fileNamePointer - buffer); 1.341 + } 1.342 + else 1.343 + { 1.344 + CSysString sysPath; 1.345 + if (!MyGetFullPathName(GetSysPath(fileName), sysPath, fileNamePartStartIndex)) 1.346 + return false; 1.347 + UString resultPath1 = GetUnicodePath(sysPath.Left(fileNamePartStartIndex)); 1.348 + UString resultPath2 = GetUnicodePath(sysPath.Mid(fileNamePartStartIndex)); 1.349 + fileNamePartStartIndex = resultPath1.Length(); 1.350 + resultPath = resultPath1 + resultPath2; 1.351 + } 1.352 + return true; 1.353 +} 1.354 +#endif 1.355 + 1.356 + 1.357 +bool MyGetFullPathName(LPCTSTR fileName, CSysString &path) 1.358 +{ 1.359 + int index; 1.360 + return MyGetFullPathName(fileName, path, index); 1.361 +} 1.362 + 1.363 +#ifndef _UNICODE 1.364 +bool MyGetFullPathName(LPCWSTR fileName, UString &path) 1.365 +{ 1.366 + int index; 1.367 + return MyGetFullPathName(fileName, path, index); 1.368 +} 1.369 +#endif 1.370 + 1.371 +bool GetOnlyName(LPCTSTR fileName, CSysString &resultName) 1.372 +{ 1.373 + int index; 1.374 + if (!MyGetFullPathName(fileName, resultName, index)) 1.375 + return false; 1.376 + resultName = resultName.Mid(index); 1.377 + return true; 1.378 +} 1.379 + 1.380 +#ifndef _UNICODE 1.381 +bool GetOnlyName(LPCWSTR fileName, UString &resultName) 1.382 +{ 1.383 + int index; 1.384 + if (!MyGetFullPathName(fileName, resultName, index)) 1.385 + return false; 1.386 + resultName = resultName.Mid(index); 1.387 + return true; 1.388 +} 1.389 +#endif 1.390 + 1.391 +bool GetOnlyDirPrefix(LPCTSTR fileName, CSysString &resultName) 1.392 +{ 1.393 + int index; 1.394 + if (!MyGetFullPathName(fileName, resultName, index)) 1.395 + return false; 1.396 + resultName = resultName.Left(index); 1.397 + return true; 1.398 +} 1.399 + 1.400 +#ifndef _UNICODE 1.401 +bool GetOnlyDirPrefix(LPCWSTR fileName, UString &resultName) 1.402 +{ 1.403 + int index; 1.404 + if (!MyGetFullPathName(fileName, resultName, index)) 1.405 + return false; 1.406 + resultName = resultName.Left(index); 1.407 + return true; 1.408 +} 1.409 +#endif 1.410 + 1.411 +bool MyGetCurrentDirectory(CSysString &path) 1.412 +{ 1.413 + DWORD needLength = ::GetCurrentDirectory(MAX_PATH + 1, path.GetBuffer(MAX_PATH + 1)); 1.414 + path.ReleaseBuffer(); 1.415 + return (needLength > 0 && needLength <= MAX_PATH); 1.416 +} 1.417 + 1.418 +#ifndef _UNICODE 1.419 +bool MySetCurrentDirectory(LPCWSTR path) 1.420 +{ 1.421 + if (g_IsNT) 1.422 + return BOOLToBool(::SetCurrentDirectoryW(path)); 1.423 + return MySetCurrentDirectory(GetSysPath(path)); 1.424 +} 1.425 +bool MyGetCurrentDirectory(UString &path) 1.426 +{ 1.427 + if (g_IsNT) 1.428 + { 1.429 + DWORD needLength = ::GetCurrentDirectoryW(MAX_PATH + 1, path.GetBuffer(MAX_PATH + 1)); 1.430 + path.ReleaseBuffer(); 1.431 + return (needLength > 0 && needLength <= MAX_PATH); 1.432 + } 1.433 + CSysString sysPath; 1.434 + if (!MyGetCurrentDirectory(sysPath)) 1.435 + return false; 1.436 + path = GetUnicodePath(sysPath); 1.437 + return true; 1.438 +} 1.439 +#endif 1.440 +#endif 1.441 + 1.442 +bool MySearchPath(LPCTSTR path, LPCTSTR fileName, LPCTSTR extension, 1.443 + CSysString &resultPath, UINT32 &filePart) 1.444 +{ 1.445 + LPTSTR filePartPointer; 1.446 + DWORD value = ::SearchPath(path, fileName, extension, 1.447 + MAX_PATH, resultPath.GetBuffer(MAX_PATH + 1), &filePartPointer); 1.448 + filePart = (UINT32)(filePartPointer - (LPCTSTR)resultPath); 1.449 + resultPath.ReleaseBuffer(); 1.450 + return (value > 0 && value <= MAX_PATH); 1.451 +} 1.452 + 1.453 +#ifndef _UNICODE 1.454 +bool MySearchPath(LPCWSTR path, LPCWSTR fileName, LPCWSTR extension, 1.455 + UString &resultPath, UINT32 &filePart) 1.456 +{ 1.457 + if (g_IsNT) 1.458 + { 1.459 + LPWSTR filePartPointer = 0; 1.460 + DWORD value = ::SearchPathW(path, fileName, extension, 1.461 + MAX_PATH, resultPath.GetBuffer(MAX_PATH + 1), &filePartPointer); 1.462 + filePart = (UINT32)(filePartPointer - (LPCWSTR)resultPath); 1.463 + resultPath.ReleaseBuffer(); 1.464 + return (value > 0 && value <= MAX_PATH); 1.465 + } 1.466 + 1.467 + CSysString sysPath; 1.468 + if (!MySearchPath( 1.469 + path != 0 ? (LPCTSTR)GetSysPath(path): 0, 1.470 + fileName != 0 ? (LPCTSTR)GetSysPath(fileName): 0, 1.471 + extension != 0 ? (LPCTSTR)GetSysPath(extension): 0, 1.472 + sysPath, filePart)) 1.473 + return false; 1.474 + UString resultPath1 = GetUnicodePath(sysPath.Left(filePart)); 1.475 + UString resultPath2 = GetUnicodePath(sysPath.Mid(filePart)); 1.476 + filePart = resultPath1.Length(); 1.477 + resultPath = resultPath1 + resultPath2; 1.478 + return true; 1.479 +} 1.480 +#endif 1.481 + 1.482 +bool MyGetTempPath(CSysString &path) 1.483 +{ 1.484 + DWORD needLength = ::GetTempPath(MAX_PATH + 1, path.GetBuffer(MAX_PATH + 1)); 1.485 + path.ReleaseBuffer(); 1.486 + return (needLength > 0 && needLength <= MAX_PATH); 1.487 +} 1.488 + 1.489 +#ifndef _UNICODE 1.490 +bool MyGetTempPath(UString &path) 1.491 +{ 1.492 + path.Empty(); 1.493 + if (g_IsNT) 1.494 + { 1.495 + DWORD needLength = ::GetTempPathW(MAX_PATH + 1, path.GetBuffer(MAX_PATH + 1)); 1.496 + path.ReleaseBuffer(); 1.497 + return (needLength > 0 && needLength <= MAX_PATH); 1.498 + } 1.499 + CSysString sysPath; 1.500 + if (!MyGetTempPath(sysPath)) 1.501 + return false; 1.502 + path = GetUnicodePath(sysPath); 1.503 + return true; 1.504 +} 1.505 +#endif 1.506 + 1.507 +UINT MyGetTempFileName(LPCTSTR dirPath, LPCTSTR prefix, CSysString &path) 1.508 +{ 1.509 + UINT number = ::GetTempFileName(dirPath, prefix, 0, path.GetBuffer(MAX_PATH + 1)); 1.510 + path.ReleaseBuffer(); 1.511 + return number; 1.512 +} 1.513 + 1.514 +#ifndef _UNICODE 1.515 +UINT MyGetTempFileName(LPCWSTR dirPath, LPCWSTR prefix, UString &path) 1.516 +{ 1.517 + if (g_IsNT) 1.518 + { 1.519 + UINT number = ::GetTempFileNameW(dirPath, prefix, 0, path.GetBuffer(MAX_PATH)); 1.520 + path.ReleaseBuffer(); 1.521 + return number; 1.522 + } 1.523 + CSysString sysPath; 1.524 + UINT number = MyGetTempFileName( 1.525 + dirPath ? (LPCTSTR)GetSysPath(dirPath): 0, 1.526 + prefix ? (LPCTSTR)GetSysPath(prefix): 0, 1.527 + sysPath); 1.528 + path = GetUnicodePath(sysPath); 1.529 + return number; 1.530 +} 1.531 +#endif 1.532 + 1.533 +UINT CTempFile::Create(LPCTSTR dirPath, LPCTSTR prefix, CSysString &resultPath) 1.534 +{ 1.535 + Remove(); 1.536 + UINT number = MyGetTempFileName(dirPath, prefix, resultPath); 1.537 + if(number != 0) 1.538 + { 1.539 + _fileName = resultPath; 1.540 + _mustBeDeleted = true; 1.541 + } 1.542 + return number; 1.543 +} 1.544 + 1.545 +bool CTempFile::Create(LPCTSTR prefix, CSysString &resultPath) 1.546 +{ 1.547 + CSysString tempPath; 1.548 + if(!MyGetTempPath(tempPath)) 1.549 + return false; 1.550 + if (Create(tempPath, prefix, resultPath) != 0) 1.551 + return true; 1.552 + if(!MyGetWindowsDirectory(tempPath)) 1.553 + return false; 1.554 + return (Create(tempPath, prefix, resultPath) != 0); 1.555 +} 1.556 + 1.557 +bool CTempFile::Remove() 1.558 +{ 1.559 + if (!_mustBeDeleted) 1.560 + return true; 1.561 + _mustBeDeleted = !DeleteFileAlways(_fileName); 1.562 + return !_mustBeDeleted; 1.563 +} 1.564 + 1.565 +#ifndef _UNICODE 1.566 + 1.567 +UINT CTempFileW::Create(LPCWSTR dirPath, LPCWSTR prefix, UString &resultPath) 1.568 +{ 1.569 + Remove(); 1.570 + UINT number = MyGetTempFileName(dirPath, prefix, resultPath); 1.571 + if(number != 0) 1.572 + { 1.573 + _fileName = resultPath; 1.574 + _mustBeDeleted = true; 1.575 + } 1.576 + return number; 1.577 +} 1.578 + 1.579 +bool CTempFileW::Create(LPCWSTR prefix, UString &resultPath) 1.580 +{ 1.581 + UString tempPath; 1.582 + if(!MyGetTempPath(tempPath)) 1.583 + return false; 1.584 + if (Create(tempPath, prefix, resultPath) != 0) 1.585 + return true; 1.586 + if(!MyGetWindowsDirectory(tempPath)) 1.587 + return false; 1.588 + return (Create(tempPath, prefix, resultPath) != 0); 1.589 +} 1.590 + 1.591 +bool CTempFileW::Remove() 1.592 +{ 1.593 + if (!_mustBeDeleted) 1.594 + return true; 1.595 + _mustBeDeleted = !DeleteFileAlways(_fileName); 1.596 + return !_mustBeDeleted; 1.597 +} 1.598 + 1.599 +#endif 1.600 + 1.601 +bool CreateTempDirectory(LPCTSTR prefix, CSysString &dirName) 1.602 +{ 1.603 + /* 1.604 + CSysString prefix = tempPath + prefixChars; 1.605 + CRandom random; 1.606 + random.Init(); 1.607 + */ 1.608 + while(true) 1.609 + { 1.610 + CTempFile tempFile; 1.611 + if (!tempFile.Create(prefix, dirName)) 1.612 + return false; 1.613 + if (!::DeleteFile(dirName)) 1.614 + return false; 1.615 + /* 1.616 + UINT32 randomNumber = random.Generate(); 1.617 + TCHAR randomNumberString[32]; 1.618 + _stprintf(randomNumberString, _T("%04X"), randomNumber); 1.619 + dirName = prefix + randomNumberString; 1.620 + */ 1.621 + if(NFind::DoesFileExist(dirName)) 1.622 + continue; 1.623 + if (MyCreateDirectory(dirName)) 1.624 + return true; 1.625 + if (::GetLastError() != ERROR_ALREADY_EXISTS) 1.626 + return false; 1.627 + } 1.628 +} 1.629 + 1.630 +bool CTempDirectory::Create(LPCTSTR prefix) 1.631 +{ 1.632 + Remove(); 1.633 + return (_mustBeDeleted = CreateTempDirectory(prefix, _tempDir)); 1.634 +} 1.635 + 1.636 +#ifndef _UNICODE 1.637 + 1.638 +bool CreateTempDirectory(LPCWSTR prefix, UString &dirName) 1.639 +{ 1.640 + /* 1.641 + CSysString prefix = tempPath + prefixChars; 1.642 + CRandom random; 1.643 + random.Init(); 1.644 + */ 1.645 + while(true) 1.646 + { 1.647 + CTempFileW tempFile; 1.648 + if (!tempFile.Create(prefix, dirName)) 1.649 + return false; 1.650 + if (!DeleteFileAlways(dirName)) 1.651 + return false; 1.652 + /* 1.653 + UINT32 randomNumber = random.Generate(); 1.654 + TCHAR randomNumberString[32]; 1.655 + _stprintf(randomNumberString, _T("%04X"), randomNumber); 1.656 + dirName = prefix + randomNumberString; 1.657 + */ 1.658 + if(NFind::DoesFileExist(dirName)) 1.659 + continue; 1.660 + if (MyCreateDirectory(dirName)) 1.661 + return true; 1.662 + if (::GetLastError() != ERROR_ALREADY_EXISTS) 1.663 + return false; 1.664 + } 1.665 +} 1.666 + 1.667 +bool CTempDirectoryW::Create(LPCWSTR prefix) 1.668 +{ 1.669 + Remove(); 1.670 + return (_mustBeDeleted = CreateTempDirectory(prefix, _tempDir)); 1.671 +} 1.672 + 1.673 +#endif 1.674 + 1.675 +}}}