static HRESULT CopyFileSpec(LPCWSTR fromPath, LPCWSTR toPath, bool writeToDisk, UInt64 fileSize, UInt32 bufferSize, UInt64 progressStart, IProgress *progress) { NFile::NIO::CInFile inFile; if (!inFile.Open(fromPath)) return GetLastError(); if (fileSize == (UInt64)(Int64)-1) { if (!inFile.GetLength(fileSize)) ::GetLastError(); } NFile::NIO::COutFile outFile; if (writeToDisk) { if (!outFile.Open(toPath, FILE_SHARE_WRITE, OPEN_EXISTING, 0)) return GetLastError(); } else if (!outFile.Create(toPath, true)) return GetLastError(); CPhysTempBuffer tempBuffer; tempBuffer.buffer = MidAlloc(bufferSize); if (tempBuffer.buffer == 0) return E_OUTOFMEMORY; for (UInt64 pos = 0; pos < fileSize;) { UInt64 progressCur = progressStart + pos; RINOK(progress->SetCompleted(&progressCur)); UInt64 rem = fileSize - pos; UInt32 curSize = (UInt32)MyMin(rem, (UInt64)bufferSize); UInt32 processedSize; if (!inFile.Read(tempBuffer.buffer, curSize, processedSize)) return GetLastError(); if (processedSize == 0) break; curSize = processedSize; if (writeToDisk) { const UInt32 kMask = 0x1FF; curSize = (curSize + kMask) & ~kMask; if (curSize > bufferSize) return E_FAIL; } if (!outFile.Write(tempBuffer.buffer, curSize, processedSize)) return GetLastError(); if (curSize != processedSize) return E_FAIL; pos += curSize; } return S_OK; }
HRESULT CThreadSplit::ProcessVirt() { NFile::NIO::CInFile inFile; if (!inFile.Open(FilePath)) return GetLastError(); NFile::NIO::COutFile outFile; CMyBuffer bufferObject; if (!bufferObject.Allocate(kBufSize)) return E_OUTOFMEMORY; Byte *buffer = (Byte *)(void *)bufferObject; UInt64 curVolSize = 0; CVolSeqName seqName; seqName.SetNumDigits(NumVolumes); UInt64 length; if (!inFile.GetLength(length)) return GetLastError(); CProgressSync &sync = ProgressDialog.Sync; sync.SetProgress(length, 0); UInt64 pos = 0; UInt64 numFiles = 0; int volIndex = 0; for (;;) { UInt64 volSize; if (volIndex < VolumeSizes.Size()) volSize = VolumeSizes[volIndex]; else volSize = VolumeSizes.Back(); UInt32 needSize = (UInt32)(MyMin((UInt64)kBufSize, volSize - curVolSize)); UInt32 processedSize; if (!inFile.Read(buffer, needSize, processedSize)) return GetLastError(); if (processedSize == 0) break; needSize = processedSize; if (curVolSize == 0) { UString name = VolBasePath; name += L'.'; name += seqName.GetNextName(); sync.SetCurrentFileName(name); sync.SetNumFilesCur(numFiles++); if (!outFile.Create(name, false)) { HRESULT res = GetLastError(); ErrorPath1 = name; return res; } } if (!outFile.Write(buffer, needSize, processedSize)) return GetLastError(); if (needSize != processedSize) throw g_Message_FileWriteError; curVolSize += processedSize; if (curVolSize == volSize) { outFile.Close(); if (volIndex < VolumeSizes.Size()) volIndex++; curVolSize = 0; } pos += processedSize; RINOK(sync.SetPosAndCheckPaused(pos)); } sync.SetNumFilesCur(numFiles); return S_OK; }
void Process2() { // NCOM::CComInitializer comInitializer; ProgressDialog->WaitCreating(); NFile::NIO::CInFile inFile; if (!inFile.Open(FilePath)) throw L"Can not open file"; NFile::NIO::COutFile outFile; CMyBuffer bufferObject; if (!bufferObject.Allocate(kBufSize)) throw L"Can not allocate buffer"; Byte *buffer = (Byte *)(void *)bufferObject; UInt64 curVolSize = 0; CVolSeqName seqName; UInt64 length; if (!inFile.GetLength(length)) throw "error"; ProgressDialog->ProgressSynch.SetProgress(length, 0); UInt64 pos = 0; int volIndex = 0; for (;;) { UInt64 volSize; if (volIndex < VolumeSizes.Size()) volSize = VolumeSizes[volIndex]; else volSize = VolumeSizes.Back(); UInt32 needSize = (UInt32)(MyMin((UInt64)kBufSize, volSize - curVolSize)); UInt32 processedSize; if (!inFile.Read(buffer, needSize, processedSize)) throw L"Can not read input file"; if (processedSize == 0) break; needSize = processedSize; if (curVolSize == 0) { UString name = VolBasePath; name += L"."; name += seqName.GetNextName(); if (!outFile.Create(name, false)) throw L"Can not create output file"; ProgressDialog->ProgressSynch.SetCurrentFileName(name); } if (!outFile.Write(buffer, needSize, processedSize)) throw L"Can not write output file"; if (needSize != processedSize) throw L"Can not write output file"; curVolSize += processedSize; if (curVolSize == volSize) { outFile.Close(); if (volIndex < VolumeSizes.Size()) volIndex++; curVolSize = 0; } pos += processedSize; HRESULT res = ProgressDialog->ProgressSynch.SetPosAndCheckPaused(pos); if (res != S_OK) return; } }