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 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
|
// FSFolder.cpp
#include "StdAfx.h"
#include "Common/ComTry.h"
#include "Common/StringConvert.h"
#include "Common/UTFConvert.h"
#include "Windows/FileDir.h"
#include "Windows/FileIO.h"
#include "Windows/PropVariant.h"
#include "../../PropID.h"
#include "FSDrives.h"
#include "FSFolder.h"
#ifdef _WIN32
#include "NetFolder.h"
#endif
#include "SysIconUtils.h"
namespace NWindows {
namespace NFile {
bool GetLongPath(LPCWSTR path, UString &longPath);
}}
using namespace NWindows;
using namespace NFile;
using namespace NFind;
namespace NFsFolder {
static STATPROPSTG kProps[] =
{
{ NULL, kpidName, VT_BSTR},
{ NULL, kpidSize, VT_UI8},
{ NULL, kpidMTime, VT_FILETIME},
{ NULL, kpidCTime, VT_FILETIME},
{ NULL, kpidATime, VT_FILETIME},
{ NULL, kpidAttrib, VT_UI4},
{ NULL, kpidPackSize, VT_UI8},
{ NULL, kpidComment, VT_BSTR},
{ NULL, kpidPrefix, VT_BSTR}
};
HRESULT CFSFolder::Init(const UString &path, IFolderFolder *parentFolder)
{
_parentFolder = parentFolder;
_path = path;
#ifdef _WIN32
_findChangeNotification.FindFirst(_path, false,
FILE_NOTIFY_CHANGE_FILE_NAME |
FILE_NOTIFY_CHANGE_DIR_NAME |
FILE_NOTIFY_CHANGE_ATTRIBUTES |
FILE_NOTIFY_CHANGE_SIZE |
FILE_NOTIFY_CHANGE_LAST_WRITE /*|
FILE_NOTIFY_CHANGE_LAST_ACCESS |
FILE_NOTIFY_CHANGE_CREATION |
FILE_NOTIFY_CHANGE_SECURITY */);
if (!_findChangeNotification.IsHandleAllocated())
{
DWORD lastError = GetLastError();
CFindFile findFile;
CFileInfoW fi;
if (!findFile.FindFirst(_path + UString(L"*"), fi))
return lastError;
}
#endif
return S_OK;
}
HRESULT GetFolderSize(const UString &path, UInt64 &numFolders, UInt64 &numFiles, UInt64 &size, IProgress *progress)
{
RINOK(progress->SetCompleted(NULL));
numFiles = numFolders = size = 0;
CEnumeratorW enumerator(path + UString(WSTRING_PATH_SEPARATOR L"*"));
CFileInfoW fi;
while (enumerator.Next(fi))
{
if (fi.IsDir())
{
UInt64 subFolders, subFiles, subSize;
RINOK(GetFolderSize(path + UString(WSTRING_PATH_SEPARATOR) + fi.Name, subFolders, subFiles, subSize, progress));
numFolders += subFolders;
numFolders++;
numFiles += subFiles;
size += subSize;
}
else
{
numFiles++;
size += fi.Size;
}
}
return S_OK;
}
HRESULT CFSFolder::LoadSubItems(CDirItem &dirItem, const UString &path)
{
{
CEnumeratorW enumerator(path + L"*");
CDirItem fi;
while (enumerator.Next(fi))
{
fi.CompressedSizeIsDefined = false;
/*
if (!GetCompressedFileSize(_path + fi.Name,
fi.CompressedSize))
fi.CompressedSize = fi.Size;
*/
if (fi.IsDir())
{
// fi.Size = GetFolderSize(_path + fi.Name);
fi.Size = 0;
}
dirItem.Files.Add(fi);
}
}
if (!_flatMode)
return S_OK;
for (int i = 0; i < dirItem.Files.Size(); i++)
{
CDirItem &item = dirItem.Files[i];
if (item.IsDir())
LoadSubItems(item, path + item.Name + WCHAR_PATH_SEPARATOR);
}
return S_OK;
}
void CFSFolder::AddRefs(CDirItem &dirItem)
{
int i;
for (i = 0; i < dirItem.Files.Size(); i++)
{
CDirItem &item = dirItem.Files[i];
item.Parent = &dirItem;
_refs.Add(&item);
}
if (!_flatMode)
return;
for (i = 0; i < dirItem.Files.Size(); i++)
{
CDirItem &item = dirItem.Files[i];
if (item.IsDir())
AddRefs(item);
}
}
STDMETHODIMP CFSFolder::LoadItems()
{
// OutputDebugString(TEXT("Start\n"));
Int32 dummy;
WasChanged(&dummy);
Clear();
RINOK(LoadSubItems(_root, _path));
AddRefs(_root);
// OutputDebugString(TEXT("Finish\n"));
_commentsAreLoaded = false;
return S_OK;
}
static const wchar_t *kDescriptionFileName = L"descript.ion";
bool CFSFolder::LoadComments()
{
if (_commentsAreLoaded)
return true;
_comments.Clear();
_commentsAreLoaded = true;
NIO::CInFile file;
if (!file.Open(_path + kDescriptionFileName))
return false;
UInt64 length;
if (!file.GetLength(length))
return false;
if (length >= (1 << 28))
return false;
AString s;
char *p = s.GetBuffer((int)((size_t)length + 1));
UInt32 processedSize;
file.Read(p, (UInt32)length, processedSize);
p[length] = 0;
s.ReleaseBuffer();
if (processedSize != length)
return false;
file.Close();
UString unicodeString;
if (!ConvertUTF8ToUnicode(s, unicodeString))
return false;
return _comments.ReadFromString(unicodeString);
}
static bool IsAscii(const UString &testString)
{
for (int i = 0; i < testString.Length(); i++)
if (testString[i] >= 0x80)
return false;
return true;
}
bool CFSFolder::SaveComments()
{
NIO::COutFile file;
if (!file.Create(_path + kDescriptionFileName, true))
return false;
UString unicodeString;
_comments.SaveToString(unicodeString);
AString utfString;
ConvertUnicodeToUTF8(unicodeString, utfString);
UInt32 processedSize;
if (!IsAscii(unicodeString))
{
Byte bom [] = { 0xEF, 0xBB, 0xBF, 0x0D, 0x0A };
file.Write(bom , sizeof(bom), processedSize);
}
file.Write(utfString, utfString.Length(), processedSize);
_commentsAreLoaded = false;
return true;
}
STDMETHODIMP CFSFolder::GetNumberOfItems(UInt32 *numItems)
{
*numItems = _refs.Size();
return S_OK;
}
/*
STDMETHODIMP CFSFolder::GetNumberOfSubFolders(UInt32 *numSubFolders)
{
UInt32 numSubFoldersLoc = 0;
for (int i = 0; i < _files.Size(); i++)
if (_files[i].IsDir())
numSubFoldersLoc++;
*numSubFolders = numSubFoldersLoc;
return S_OK;
}
*/
#ifdef _WIN32
static bool MyGetCompressedFileSizeW(LPCWSTR fileName, UInt64 &size)
{
DWORD highPart;
DWORD lowPart = ::GetCompressedFileSizeW(fileName, &highPart);
if (lowPart == INVALID_FILE_SIZE && ::GetLastError() != NO_ERROR)
{
#ifdef WIN_LONG_PATH
{
UString longPath;
if (GetLongPath(fileName, longPath))
lowPart = ::GetCompressedFileSizeW(longPath, &highPart);
}
#endif
if (lowPart == INVALID_FILE_SIZE && ::GetLastError() != NO_ERROR)
return false;
}
size = (UInt64(highPart) << 32) | lowPart;
return true;
}
#endif
STDMETHODIMP CFSFolder::GetProperty(UInt32 itemIndex, PROPID propID, PROPVARIANT *value)
{
NCOM::CPropVariant prop;
if (itemIndex >= (UInt32)_refs.Size())
return E_INVALIDARG;
CDirItem &fi = *_refs[itemIndex];
switch(propID)
{
case kpidIsDir: prop = fi.IsDir(); break;
case kpidName: prop = fi.Name; break;
case kpidSize: if (!fi.IsDir()) prop = fi.Size; break;
case kpidPackSize:
if (!fi.CompressedSizeIsDefined)
{
fi.CompressedSizeIsDefined = true;
#ifdef _WIN32
if (fi.IsDir () ||
!MyGetCompressedFileSizeW(_path + GetRelPath(fi), fi.CompressedSize))
#endif
fi.CompressedSize = fi.Size;
}
prop = fi.CompressedSize;
break;
case kpidAttrib: prop = (UInt32)fi.Attrib; break;
case kpidCTime: prop = fi.CTime; break;
case kpidATime: prop = fi.ATime; break;
case kpidMTime: prop = fi.MTime; break;
case kpidComment:
{
LoadComments();
UString comment;
if (_comments.GetValue(GetRelPath(fi), comment))
prop = comment;
break;
}
case kpidPrefix:
{
if (_flatMode)
prop = GetPrefix(fi);
break;
}
}
prop.Detach(value);
return S_OK;
}
HRESULT CFSFolder::BindToFolderSpec(const wchar_t *name, IFolderFolder **resultFolder)
{
*resultFolder = 0;
CFSFolder *folderSpec = new CFSFolder;
CMyComPtr<IFolderFolder> subFolder = folderSpec;
RINOK(folderSpec->Init(_path + name + UString(WCHAR_PATH_SEPARATOR), 0));
*resultFolder = subFolder.Detach();
return S_OK;
}
UString CFSFolder::GetPrefix(const CDirItem &item) const
{
UString path;
CDirItem *cur = item.Parent;
while (cur->Parent != 0)
{
path = cur->Name + UString(WCHAR_PATH_SEPARATOR) + path;
cur = cur->Parent;
}
return path;
}
UString CFSFolder::GetRelPath(const CDirItem &item) const
{
return GetPrefix(item) + item.Name;
}
STDMETHODIMP CFSFolder::BindToFolder(UInt32 index, IFolderFolder **resultFolder)
{
*resultFolder = 0;
const CDirItem &fi = *_refs[index];
if (!fi.IsDir())
return E_INVALIDARG;
return BindToFolderSpec(GetRelPath(fi), resultFolder);
}
STDMETHODIMP CFSFolder::BindToFolder(const wchar_t *name, IFolderFolder **resultFolder)
{
return BindToFolderSpec(name, resultFolder);
}
STDMETHODIMP CFSFolder::BindToParentFolder(IFolderFolder **resultFolder)
{
*resultFolder = 0;
if (_parentFolder)
{
CMyComPtr<IFolderFolder> parentFolder = _parentFolder;
*resultFolder = parentFolder.Detach();
return S_OK;
}
if (_path.IsEmpty())
return E_INVALIDARG;
printf("CFSFolder::BindToParentFolder path='%ls'\n",(const wchar_t *)_path);
int pos = _path.ReverseFind(WCHAR_PATH_SEPARATOR);
if (pos < 0 || pos != _path.Length() - 1)
return E_FAIL;
UString parentPath = _path.Left(pos);
printf("CFSFolder::BindToParentFolder parentPath='%ls'\n",(const wchar_t *)parentPath);
pos = parentPath.ReverseFind(WCHAR_PATH_SEPARATOR);
if (pos < 0)
{
#ifdef _WIN32
parentPath.Empty();
CFSDrives *drivesFolderSpec = new CFSDrives;
CMyComPtr<IFolderFolder> drivesFolder = drivesFolderSpec;
drivesFolderSpec->Init();
*resultFolder = drivesFolder.Detach();
#else
parentPath = WSTRING_PATH_SEPARATOR;
CFSFolder *parentFolderSpec = new CFSFolder;
CMyComPtr<IFolderFolder> parentFolder = parentFolderSpec;
printf("CFSFolder::BindToParentFolder Init-0 with parentPath='%ls'\n",(const wchar_t *)parentPath);
RINOK(parentFolderSpec->Init(parentPath, 0));
*resultFolder = parentFolder.Detach();
#endif
return S_OK;
}
UString parentPathReduced = parentPath.Left(pos);
parentPath = parentPath.Left(pos + 1);
pos = parentPathReduced.ReverseFind(WCHAR_PATH_SEPARATOR);
if (pos == 1)
{
#ifdef _WIN32
if (parentPath[0] != WCHAR_PATH_SEPARATOR)
return E_FAIL;
CNetFolder *netFolderSpec = new CNetFolder;
CMyComPtr<IFolderFolder> netFolder = netFolderSpec;
netFolderSpec->Init(parentPath);
*resultFolder = netFolder.Detach();
#else
parentPath = WSTRING_PATH_SEPARATOR;
CFSFolder *parentFolderSpec = new CFSFolder;
CMyComPtr<IFolderFolder> parentFolder = parentFolderSpec;
printf("CFSFolder::BindToParentFolder Init-1 with parentPath='%ls'\n",(const wchar_t *)parentPath);
RINOK(parentFolderSpec->Init(parentPath, 0));
*resultFolder = parentFolder.Detach();
#endif // ifdef _WIN32
return S_OK;
}
CFSFolder *parentFolderSpec = new CFSFolder;
CMyComPtr<IFolderFolder> parentFolder = parentFolderSpec;
printf("CFSFolder::BindToParentFolder Init-2 with parentPath='%ls'\n",(const wchar_t *)parentPath);
RINOK(parentFolderSpec->Init(parentPath, 0));
*resultFolder = parentFolder.Detach();
return S_OK;
}
STDMETHODIMP CFSFolder::GetNumberOfProperties(UInt32 *numProperties)
{
*numProperties = sizeof(kProps) / sizeof(kProps[0]);
if (!_flatMode)
(*numProperties)--;
return S_OK;
}
STDMETHODIMP CFSFolder::GetPropertyInfo IMP_IFolderFolder_GetProp(kProps)
STDMETHODIMP CFSFolder::GetFolderProperty(PROPID propID, PROPVARIANT *value)
{
COM_TRY_BEGIN
NWindows::NCOM::CPropVariant prop;
switch(propID)
{
case kpidType: prop = L"FSFolder"; break;
case kpidPath: prop = _path; break;
}
prop.Detach(value);
return S_OK;
COM_TRY_END
}
STDMETHODIMP CFSFolder::WasChanged(Int32 *wasChanged)
{
bool wasChangedMain = false;
#ifdef _WIN32
for (;;)
{
if (!_findChangeNotification.IsHandleAllocated())
{
*wasChanged = BoolToInt(false);
return S_OK;
}
DWORD waitResult = ::WaitForSingleObject(_findChangeNotification, 0);
bool wasChangedLoc = (waitResult == WAIT_OBJECT_0);
if (wasChangedLoc)
{
_findChangeNotification.FindNext();
wasChangedMain = true;
}
else
break;
}
#endif
*wasChanged = BoolToInt(wasChangedMain);
return S_OK;
}
STDMETHODIMP CFSFolder::Clone(IFolderFolder **resultFolder)
{
CFSFolder *fsFolderSpec = new CFSFolder;
CMyComPtr<IFolderFolder> folderNew = fsFolderSpec;
fsFolderSpec->Init(_path, 0);
*resultFolder = folderNew.Detach();
return S_OK;
}
HRESULT CFSFolder::GetItemsFullSize(const UInt32 *indices, UInt32 numItems,
UInt64 &numFolders, UInt64 &numFiles, UInt64 &size, IProgress *progress)
{
numFiles = numFolders = size = 0;
UInt32 i;
for (i = 0; i < numItems; i++)
{
int index = indices[i];
if (index >= _refs.Size())
return E_INVALIDARG;
const CDirItem &fi = *_refs[index];
if (fi.IsDir())
{
UInt64 subFolders, subFiles, subSize;
RINOK(GetFolderSize(_path + GetRelPath(fi), subFolders, subFiles, subSize, progress));
numFolders += subFolders;
numFolders++;
numFiles += subFiles;
size += subSize;
}
else
{
numFiles++;
size += fi.Size;
}
}
return S_OK;
}
HRESULT CFSFolder::GetItemFullSize(int index, UInt64 &size, IProgress *progress)
{
const CDirItem &fi = *_refs[index];
if (fi.IsDir())
{
/*
CMyComPtr<IFolderFolder> subFolder;
RINOK(BindToFolder(index, &subFolder));
CMyComPtr<IFolderReload> aFolderReload;
subFolder.QueryInterface(&aFolderReload);
aFolderReload->Reload();
UInt32 numItems;
RINOK(subFolder->GetNumberOfItems(&numItems));
CMyComPtr<IFolderGetItemFullSize> aGetItemFullSize;
subFolder.QueryInterface(&aGetItemFullSize);
for (UInt32 i = 0; i < numItems; i++)
{
UInt64 size;
RINOK(aGetItemFullSize->GetItemFullSize(i, &size));
*totalSize += size;
}
*/
UInt64 numFolders, numFiles;
return GetFolderSize(_path + GetRelPath(fi), numFolders, numFiles, size, progress);
}
size = fi.Size;
return S_OK;
}
STDMETHODIMP CFSFolder::GetItemFullSize(UInt32 index, PROPVARIANT *value, IProgress *progress)
{
NCOM::CPropVariant prop;
if (index >= (UInt32)_refs.Size())
return E_INVALIDARG;
UInt64 size = 0;
HRESULT result = GetItemFullSize(index, size, progress);
prop = size;
prop.Detach(value);
return result;
}
HRESULT CFSFolder::GetComplexName(const wchar_t *name, UString &resultPath)
{
UString newName = name;
resultPath = _path + newName;
if (newName.Length() < 1)
return S_OK;
if (newName[0] == WCHAR_PATH_SEPARATOR)
{
resultPath = newName;
return S_OK;
}
if (newName.Length() < 2)
return S_OK;
if (newName[1] == L':')
resultPath = newName;
return S_OK;
}
STDMETHODIMP CFSFolder::CreateFolder(const wchar_t *name, IProgress * /* progress */)
{
UString processedName;
RINOK(GetComplexName(name, processedName));
if(NDirectory::MyCreateDirectory(processedName))
return S_OK;
if(::GetLastError() == ERROR_ALREADY_EXISTS)
return ::GetLastError();
if (!NDirectory::CreateComplexDirectory(processedName))
return ::GetLastError();
return S_OK;
}
STDMETHODIMP CFSFolder::CreateFile(const wchar_t *name, IProgress * /* progress */)
{
UString processedName;
RINOK(GetComplexName(name, processedName));
NIO::COutFile outFile;
if (!outFile.Create(processedName, false))
return ::GetLastError();
return S_OK;
}
STDMETHODIMP CFSFolder::Rename(UInt32 index, const wchar_t *newName, IProgress * /* progress */)
{
const CDirItem &fi = *_refs[index];
const UString fullPrefix = _path + GetPrefix(fi);
if (!NDirectory::MyMoveFile(fullPrefix + fi.Name, fullPrefix + newName))
return GetLastError();
return S_OK;
}
STDMETHODIMP CFSFolder::Delete(const UInt32 *indices, UInt32 numItems,IProgress *progress)
{
RINOK(progress->SetTotal(numItems));
for (UInt32 i = 0; i < numItems; i++)
{
const CDirItem &fi = *_refs[indices[i]];
const UString fullPath = _path + GetRelPath(fi);
bool result;
if (fi.IsDir())
result = NDirectory::RemoveDirectoryWithSubItems(fullPath);
else
result = NDirectory::DeleteFileAlways(fullPath);
if (!result)
return GetLastError();
UInt64 completed = i;
RINOK(progress->SetCompleted(&completed));
}
return S_OK;
}
STDMETHODIMP CFSFolder::SetProperty(UInt32 index, PROPID propID,
const PROPVARIANT *value, IProgress * /* progress */)
{
if (index >= (UInt32)_refs.Size())
return E_INVALIDARG;
CDirItem &fi = *_refs[index];
if (fi.Parent->Parent != 0)
return E_NOTIMPL;
switch(propID)
{
case kpidComment:
{
UString filename = fi.Name;
filename.Trim();
if (value->vt == VT_EMPTY)
_comments.DeletePair(filename);
else if (value->vt == VT_BSTR)
{
CTextPair pair;
pair.ID = filename;
pair.ID.Trim();
pair.Value = value->bstrVal;
pair.Value.Trim();
if (pair.Value.IsEmpty())
_comments.DeletePair(filename);
else
_comments.AddPair(pair);
}
else
return E_INVALIDARG;
SaveComments();
break;
}
default:
return E_NOTIMPL;
}
return S_OK;
}
STDMETHODIMP CFSFolder::GetSystemIconIndex(UInt32 index, Int32 *iconIndex)
{
#ifdef _WIN32
if (index >= (UInt32)_refs.Size())
return E_INVALIDARG;
const CDirItem &fi = *_refs[index];
*iconIndex = 0;
int iconIndexTemp;
if (GetRealIconIndex(_path + GetRelPath(fi), fi.Attributes, iconIndexTemp) != 0)
{
*iconIndex = iconIndexTemp;
return S_OK;
}
return GetLastError();
#endif
*iconIndex = 0;
return S_OK;
}
STDMETHODIMP CFSFolder::SetFlatMode(Int32 flatMode)
{
_flatMode = IntToBool(flatMode);
return S_OK;
}
}
|