1 // WorkDir.cpp
2 
3 #include "StdAfx.h"
4 
5 #include "../../../Common/StringConvert.h"
6 #include "../../../Common/Wildcard.h"
7 
8 #include "../../../Windows/FileName.h"
9 
10 #include "WorkDir.h"
11 
12 using namespace NWindows;
13 using namespace NFile;
14 using namespace NDir;
15 
16 FString GetWorkDir(const NWorkDir::CInfo &workDirInfo, const FString &path, FString &fileName)
17 {
18   NWorkDir::NMode::EEnum mode = workDirInfo.Mode;
19 
20   #if defined(_WIN32) && !defined(UNDER_CE)
21   if (workDirInfo.ForRemovableOnly)
22   {
23     mode = NWorkDir::NMode::kCurrent;
24     FString prefix = path.Left(3);
25     if (prefix[1] == FTEXT(':') && prefix[2] == FTEXT('\\'))
26     {
27       UINT driveType = GetDriveType(GetSystemString(prefix, ::AreFileApisANSI() ? CP_ACP : CP_OEMCP));
28       if (driveType == DRIVE_CDROM || driveType == DRIVE_REMOVABLE)
29         mode = workDirInfo.Mode;
30     }
31     /*
32     CParsedPath parsedPath;
33     parsedPath.ParsePath(archiveName);
34     UINT driveType = GetDriveType(parsedPath.Prefix);
35     if ((driveType != DRIVE_CDROM) && (driveType != DRIVE_REMOVABLE))
36       mode = NZipSettings::NWorkDir::NMode::kCurrent;
37     */
38   }
39   #endif
40 
41   int pos = path.ReverseFind_PathSepar() + 1;
42   fileName = path.Ptr(pos);
43 
44   switch (mode)
45   {
46     case NWorkDir::NMode::kCurrent:
47     {
48       return path.Left(pos);
49     }
50     case NWorkDir::NMode::kSpecified:
51     {
52       FString tempDir = workDirInfo.Path;
53       NName::NormalizeDirPathPrefix(tempDir);
54       return tempDir;
55     }
56     default:
57     {
58       FString tempDir;
59       if (!MyGetTempPath(tempDir))
60         throw 141717;
61       return tempDir;
62     }
63   }
64 }
65 
66 HRESULT CWorkDirTempFile::CreateTempFile(const FString &originalPath)
67 {
68   NWorkDir::CInfo workDirInfo;
69   workDirInfo.Load();
70   FString namePart;
71   FString workDir = GetWorkDir(workDirInfo, originalPath, namePart);
72   CreateComplexDir(workDir);
73   CTempFile tempFile;
74   _outStreamSpec = new COutFileStream;
75   OutStream = _outStreamSpec;
76   if (!_tempFile.Create(workDir + namePart, &_outStreamSpec->File))
77   {
78     DWORD error = GetLastError();
79     return error ? error : E_FAIL;
80   }
81   _originalPath = originalPath;
82   return S_OK;
83 }
84 
85 HRESULT CWorkDirTempFile::MoveToOriginal(bool deleteOriginal)
86 {
87   OutStream.Release();
88   if (!_tempFile.MoveTo(_originalPath, deleteOriginal))
89   {
90     DWORD error = GetLastError();
91     return error ? error : E_FAIL;
92   }
93   return S_OK;
94 }
95