Home | History | Annotate | Download | only in Common
      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   #ifndef UNDER_CE
     20   if (workDirInfo.ForRemovableOnly)
     21   {
     22     mode = NWorkDir::NMode::kCurrent;
     23     FString prefix = path.Left(3);
     24     if (prefix[1] == FTEXT(':') && prefix[2] == FTEXT('\\'))
     25     {
     26       UINT driveType = GetDriveType(GetSystemString(prefix, ::AreFileApisANSI() ? CP_ACP : CP_OEMCP));
     27       if (driveType == DRIVE_CDROM || driveType == DRIVE_REMOVABLE)
     28         mode = workDirInfo.Mode;
     29     }
     30     /*
     31     CParsedPath parsedPath;
     32     parsedPath.ParsePath(archiveName);
     33     UINT driveType = GetDriveType(parsedPath.Prefix);
     34     if ((driveType != DRIVE_CDROM) && (driveType != DRIVE_REMOVABLE))
     35       mode = NZipSettings::NWorkDir::NMode::kCurrent;
     36     */
     37   }
     38   #endif
     39   int pos = path.ReverseFind(FCHAR_PATH_SEPARATOR) + 1;
     40   fileName = path.Ptr(pos);
     41   switch (mode)
     42   {
     43     case NWorkDir::NMode::kCurrent:
     44     {
     45       return path.Left(pos);;
     46     }
     47     case NWorkDir::NMode::kSpecified:
     48     {
     49       FString tempDir = workDirInfo.Path;
     50       NName::NormalizeDirPathPrefix(tempDir);
     51       return tempDir;
     52     }
     53     default:
     54     {
     55       FString tempDir;
     56       if (!MyGetTempPath(tempDir))
     57         throw 141717;
     58       return tempDir;
     59     }
     60   }
     61 }
     62 
     63 HRESULT CWorkDirTempFile::CreateTempFile(const FString &originalPath)
     64 {
     65   NWorkDir::CInfo workDirInfo;
     66   workDirInfo.Load();
     67   FString namePart;
     68   FString workDir = GetWorkDir(workDirInfo, originalPath, namePart);
     69   CreateComplexDir(workDir);
     70   CTempFile tempFile;
     71   _outStreamSpec = new COutFileStream;
     72   OutStream = _outStreamSpec;
     73   if (!_tempFile.Create(workDir + namePart, &_outStreamSpec->File))
     74   {
     75     DWORD error = GetLastError();
     76     return error ? error : E_FAIL;
     77   }
     78   _originalPath = originalPath;
     79   return S_OK;
     80 }
     81 
     82 HRESULT CWorkDirTempFile::MoveToOriginal(bool deleteOriginal)
     83 {
     84   OutStream.Release();
     85   if (!_tempFile.MoveTo(_originalPath, deleteOriginal))
     86   {
     87     DWORD error = GetLastError();
     88     return error ? error : E_FAIL;
     89   }
     90   return S_OK;
     91 }
     92