Welcome to mirror list, hosted at ThFree Co, Russian Federation.

PhysDriveFolder.cpp « FileManager « 7zip « CPP - github.com/kornelski/7z.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b69d3cb3222d21ed067051d2142bb542a1d4a93b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
// PhysDriveFolder.cpp

#include "StdAfx.h"

#include "PhysDriveFolder.h"

#include "Common/Alloc.h"

#include "Windows/PropVariant.h"
#include "Windows/FileDevice.h"
#include "Windows/FileSystem.h"

#include "../PropID.h"

using namespace NWindows;

static const UInt32 kBufferSize = (4 << 20);

static STATPROPSTG kProperties[] = 
{
  { NULL, kpidName, VT_BSTR},
  { NULL, kpidSize, VT_UI8}
};

CPhysDriveFolder::~CPhysDriveFolder()
{
  if (_buffer != 0)
    MyFree(_buffer);
}

HRESULT CPhysDriveFolder::Init(const UString &path)
{
  _prefix = L"\\\\.\\";
  _path = path;
  NFile::NDevice::CInFile inFile;
  if (!inFile.Open(GetFullPath()))
    return GetLastError();
  return S_OK;
}

STDMETHODIMP CPhysDriveFolder::LoadItems()
{
  _driveType = NFile::NSystem::MyGetDriveType(_path + L"\\");
  _name = _path.Left(1);
  _name += L'.';
  if (_driveType == DRIVE_CDROM)
    _name += L"iso";
  else
    _name += L"img";
  Int32 dummy;
  WasChanged(&dummy);
  return GetLength(_length);
}

STDMETHODIMP CPhysDriveFolder::GetNumberOfItems(UInt32 *numItems)
{
  *numItems = 1;
  return S_OK;
}

STDMETHODIMP CPhysDriveFolder::GetProperty(UInt32 itemIndex, PROPID propID, PROPVARIANT *value)
{
  NCOM::CPropVariant propVariant;
  if (itemIndex >= 1)
    return E_INVALIDARG;
  switch(propID)
  {
    case kpidIsFolder:
      propVariant = false;
      break;
    case kpidName:
      propVariant = _name;
      break;
    case kpidSize:
      propVariant = _length;
      break;
  }
  propVariant.Detach(value);
  return S_OK;
}

STDMETHODIMP CPhysDriveFolder::BindToFolder(UInt32 /* index */, IFolderFolder ** /* resultFolder */)
  { return E_NOTIMPL; }

STDMETHODIMP CPhysDriveFolder::BindToFolder(const wchar_t * /* name */, IFolderFolder ** /* resultFolder */)
  { return E_NOTIMPL; }

STDMETHODIMP CPhysDriveFolder::BindToParentFolder(IFolderFolder **resultFolder)
{
  *resultFolder = 0;
  return S_OK;
}

STDMETHODIMP CPhysDriveFolder::GetName(BSTR * /* name */)
  { return E_NOTIMPL; }

STDMETHODIMP CPhysDriveFolder::GetNumberOfProperties(UInt32 *numProperties)
{
  *numProperties = sizeof(kProperties) / sizeof(kProperties[0]);
  return S_OK;
}

STDMETHODIMP CPhysDriveFolder::GetPropertyInfo(UInt32 index,     
    BSTR *name, PROPID *propID, VARTYPE *varType)
{
  if (index >= sizeof(kProperties) / sizeof(kProperties[0]))
    return E_INVALIDARG;
  const STATPROPSTG &prop = kProperties[index];
  *propID = prop.propid;
  *varType = prop.vt;
  *name = 0;
  return S_OK;
}


STDMETHODIMP CPhysDriveFolder::GetTypeID(BSTR *name)
{
  CMyComBSTR temp = L"PhysDrive";
  *name = temp.Detach();
  return S_OK;
}

STDMETHODIMP CPhysDriveFolder::GetPath(BSTR *path)
{
  UString tempPath = GetFullPath() + L"\\";
  CMyComBSTR temp = tempPath;
  *path = temp.Detach();
  return S_OK;
}

STDMETHODIMP CPhysDriveFolder::WasChanged(Int32 *wasChanged)
{
  bool wasChangedMain = false;
  *wasChanged = BoolToInt(wasChangedMain);
  return S_OK;
}
 
STDMETHODIMP CPhysDriveFolder::Clone(IFolderFolder **resultFolder)
{
  CPhysDriveFolder *folderSpec = new CPhysDriveFolder;
  CMyComPtr<IFolderFolder> folderNew = folderSpec;
  folderSpec->Init(_path);
  *resultFolder = folderNew.Detach();
  return S_OK;
}

STDMETHODIMP CPhysDriveFolder::GetItemFullSize(UInt32 index, PROPVARIANT *value, IProgress * /* progress */)
{
  NCOM::CPropVariant propVariant;
  if (index >= 1)
    return E_INVALIDARG;
  UInt64 size = 0;
  HRESULT result = GetLength(size);
  propVariant = size;
  propVariant.Detach(value);
  return result;
}

STDMETHODIMP CPhysDriveFolder::CreateFolder(const wchar_t * /* name */, IProgress * /* progress */)
  { return E_NOTIMPL; }

STDMETHODIMP CPhysDriveFolder::CreateFile(const wchar_t * /* name */, IProgress * /* progress */)
  { return E_NOTIMPL; }

STDMETHODIMP CPhysDriveFolder::Rename(UInt32 /* index */, const wchar_t * /* newName */, IProgress * /* progress */)
  { return E_NOTIMPL; }

STDMETHODIMP CPhysDriveFolder::Delete(const UInt32 * /* indices */, UInt32  /* numItems */, IProgress * /* progress */)
  { return E_NOTIMPL; }

STDMETHODIMP CPhysDriveFolder::SetProperty(UInt32 index, PROPID /* propID */,
    const PROPVARIANT * /* value */, IProgress * /* progress */)
{
  if (index >= 1)
    return E_INVALIDARG;
  return E_NOTIMPL;
}

HRESULT CPhysDriveFolder::GetLength(UInt64 &length) const
{
  NFile::NDevice::CInFile inFile;
  if (!inFile.Open(GetFullPath()))
    return GetLastError();
  if (!inFile.GetLengthSmart(length))
    return GetLastError();
  return S_OK;
}

STDMETHODIMP CPhysDriveFolder::CopyTo(const UInt32 * /* indices */, UInt32 numItems, 
    const wchar_t *path, IFolderOperationsExtractCallback *callback)
{
  if (numItems == 0)
    return S_OK;
  UString destPath = path;
  if (destPath.IsEmpty())
    return E_INVALIDARG;
  bool directName = (destPath[destPath.Length() - 1] != L'\\');
  if (directName)
  {
    if (numItems > 1)
      return E_INVALIDARG;
  }
  else
    destPath += _name;

  UInt64 fileSize;
  if (GetLength(fileSize) == S_OK)
  {
    RINOK(callback->SetTotal(fileSize));
  }

  Int32 writeAskResult;
  CMyComBSTR destPathResult;
  RINOK(callback->AskWrite(GetFullPath(), BoolToInt(false), NULL, &fileSize, 
      destPath, &destPathResult, &writeAskResult));
  if (!IntToBool(writeAskResult))
    return S_OK;

  RINOK(callback->SetCurrentFilePath(GetFullPathWithName()));
  
  NFile::NDevice::CInFile inFile;
  if (!inFile.Open(GetFullPath()))
    return GetLastError();
  NFile::NIO::COutFile outFile;
  if (!outFile.Create(destPathResult, true))
    return GetLastError();
  if (_buffer == 0)
  {
    _buffer = MyAlloc(kBufferSize);
    if (_buffer == 0)
      return E_OUTOFMEMORY;
  }
  UInt64 pos = 0;
  UInt32 bufferSize = kBufferSize;
  if (_driveType == DRIVE_REMOVABLE)
    bufferSize = (18 << 10) * 4;
  pos = 0;
  while(pos < fileSize)
  {
    RINOK(callback->SetCompleted(&pos));
    UInt32 curSize = (UInt32)MyMin(fileSize - pos, (UInt64)bufferSize);
    UInt32 processedSize;
    if (!inFile.Read(_buffer, curSize, processedSize))
      return GetLastError();
    if (processedSize == 0)
      break;
    curSize = processedSize;
    if (!outFile.Write(_buffer, curSize, processedSize))
      return GetLastError();
    if (curSize != processedSize)
      return E_FAIL;
    pos += curSize;
  }
  return S_OK;
}

/////////////////////////////////////////////////
// Move Operations

STDMETHODIMP CPhysDriveFolder::MoveTo(
    const UInt32 * /* indices */, 
    UInt32 /* numItems */, 
    const wchar_t * /* path */,
    IFolderOperationsExtractCallback * /* callback */)
{
  return E_NOTIMPL;
}

STDMETHODIMP CPhysDriveFolder::CopyFrom(
    const wchar_t * /* fromFolderPath */,
    const wchar_t ** /* itemsPaths */, UInt32  /* numItems */, IProgress * /* progress */)
{
  return E_NOTIMPL;
}