8 #include "../Common/MyString.h"
11 #include "../Common/StringConvert.h"
21 #if defined(WIN_LONG_PATH) && defined(_UNICODE)
22 #define WIN_LONG_PATH2
26 bool GetLongPathBase(LPCWSTR s, UString &res)
29 int len = MyStringLen(s);
31 if (len < 1 || c == L'\\' || c == L'.' && (len == 1 || len == 2 && s[1] == L'.'))
36 isAbs = (s[1] == L':' && s[2] == L'\\' && (c >= L'a' && c <= L'z' || c >= L'A' && c <= L'Z'));
40 DWORD needLength = ::GetCurrentDirectoryW(MAX_PATH + 1, curDir.GetBuffer(MAX_PATH + 1));
41 curDir.ReleaseBuffer();
42 if (needLength == 0 || needLength > MAX_PATH)
44 if (curDir[curDir.Length() - 1] != L'\\')
47 res = UString(L"\\\\?\\") + curDir + s;
51 bool GetLongPath(LPCWSTR path, UString &longPath)
53 if (GetLongPathBase(path, longPath))
54 return !longPath.IsEmpty();
61 CFileBase::~CFileBase() { Close(); }
63 bool CFileBase::Create(LPCTSTR fileName, DWORD desiredAccess,
64 DWORD shareMode, DWORD creationDisposition, DWORD flagsAndAttributes)
68 _handle = ::CreateFile(fileName, desiredAccess, shareMode,
69 (LPSECURITY_ATTRIBUTES)NULL, creationDisposition,
70 flagsAndAttributes, (HANDLE)NULL);
72 if (_handle == INVALID_HANDLE_VALUE)
75 if (GetLongPath(fileName, longPath))
76 _handle = ::CreateFileW(longPath, desiredAccess, shareMode,
77 (LPSECURITY_ATTRIBUTES)NULL, creationDisposition,
78 flagsAndAttributes, (HANDLE)NULL);
81 return (_handle != INVALID_HANDLE_VALUE);
85 bool CFileBase::Create(LPCWSTR fileName, DWORD desiredAccess,
86 DWORD shareMode, DWORD creationDisposition, DWORD flagsAndAttributes)
89 return Create(UnicodeStringToMultiByte(fileName, ::AreFileApisANSI() ? CP_ACP : CP_OEMCP),
90 desiredAccess, shareMode, creationDisposition, flagsAndAttributes);
93 _handle = ::CreateFileW(fileName, desiredAccess, shareMode,
94 (LPSECURITY_ATTRIBUTES)NULL, creationDisposition,
95 flagsAndAttributes, (HANDLE)NULL);
97 if (_handle == INVALID_HANDLE_VALUE)
100 if (GetLongPath(fileName, longPath))
101 _handle = ::CreateFileW(longPath, desiredAccess, shareMode,
102 (LPSECURITY_ATTRIBUTES)NULL, creationDisposition,
103 flagsAndAttributes, (HANDLE)NULL);
106 return (_handle != INVALID_HANDLE_VALUE);
110 bool CFileBase::Close()
112 if (_handle == INVALID_HANDLE_VALUE)
114 if (!::CloseHandle(_handle))
116 _handle = INVALID_HANDLE_VALUE;
120 bool CFileBase::GetPosition(UInt64 &position) const
122 return Seek(0, FILE_CURRENT, position);
125 bool CFileBase::GetLength(UInt64 &length) const
128 DWORD sizeLow = ::GetFileSize(_handle, &sizeHigh);
129 if(sizeLow == 0xFFFFFFFF)
130 if(::GetLastError() != NO_ERROR)
132 length = (((UInt64)sizeHigh) << 32) + sizeLow;
136 bool CFileBase::Seek(Int64 distanceToMove, DWORD moveMethod, UInt64 &newPosition) const
139 value.QuadPart = distanceToMove;
140 value.LowPart = ::SetFilePointer(_handle, value.LowPart, &value.HighPart, moveMethod);
141 if (value.LowPart == 0xFFFFFFFF)
142 if(::GetLastError() != NO_ERROR)
144 newPosition = value.QuadPart;
148 bool CFileBase::Seek(UInt64 position, UInt64 &newPosition)
150 return Seek(position, FILE_BEGIN, newPosition);
153 bool CFileBase::SeekToBegin()
156 return Seek(0, newPosition);
159 bool CFileBase::SeekToEnd(UInt64 &newPosition)
161 return Seek(0, FILE_END, newPosition);
164 bool CFileBase::GetFileInformation(CByHandleFileInfo &fileInfo) const
166 BY_HANDLE_FILE_INFORMATION winFileInfo;
167 if(!::GetFileInformationByHandle(_handle, &winFileInfo))
169 fileInfo.Attributes = winFileInfo.dwFileAttributes;
170 fileInfo.CreationTime = winFileInfo.ftCreationTime;
171 fileInfo.LastAccessTime = winFileInfo.ftLastAccessTime;
172 fileInfo.LastWriteTime = winFileInfo.ftLastWriteTime;
173 fileInfo.VolumeSerialNumber = winFileInfo.dwFileAttributes;
174 fileInfo.Size = (((UInt64)winFileInfo.nFileSizeHigh) << 32) + winFileInfo.nFileSizeLow;
175 fileInfo.NumberOfLinks = winFileInfo.nNumberOfLinks;
176 fileInfo.FileIndex = (((UInt64)winFileInfo.nFileIndexHigh) << 32) + winFileInfo.nFileIndexLow;
180 /////////////////////////
183 bool CInFile::Open(LPCTSTR fileName, DWORD shareMode, DWORD creationDisposition, DWORD flagsAndAttributes)
184 { return Create(fileName, GENERIC_READ, shareMode, creationDisposition, flagsAndAttributes); }
186 bool CInFile::OpenShared(LPCTSTR fileName, bool shareForWrite)
187 { return Open(fileName, FILE_SHARE_READ | (shareForWrite ? FILE_SHARE_WRITE : 0), OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL); }
189 bool CInFile::Open(LPCTSTR fileName)
190 { return OpenShared(fileName, false); }
193 bool CInFile::Open(LPCWSTR fileName, DWORD shareMode, DWORD creationDisposition, DWORD flagsAndAttributes)
194 { return Create(fileName, GENERIC_READ, shareMode, creationDisposition, flagsAndAttributes); }
196 bool CInFile::OpenShared(LPCWSTR fileName, bool shareForWrite)
197 { return Open(fileName, FILE_SHARE_READ | (shareForWrite ? FILE_SHARE_WRITE : 0), OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL); }
199 bool CInFile::Open(LPCWSTR fileName)
200 { return OpenShared(fileName, false); }
203 // ReadFile and WriteFile functions in Windows have BUG:
204 // If you Read or Write 64MB or more (probably min_failure_size = 64MB - 32KB + 1)
205 // from/to Network file, it returns ERROR_NO_SYSTEM_RESOURCES
206 // (Insufficient system resources exist to complete the requested service).
208 // Probably in some version of Windows there are problems with other sizes:
209 // for 32 MB (maybe also for 16 MB).
210 // And message can be "Network connection was lost"
212 static UInt32 kChunkSizeMax = (1 << 22);
214 bool CInFile::ReadPart(void *data, UInt32 size, UInt32 &processedSize)
216 if (size > kChunkSizeMax)
217 size = kChunkSizeMax;
218 DWORD processedLoc = 0;
219 bool res = BOOLToBool(::ReadFile(_handle, data, size, &processedLoc, NULL));
220 processedSize = (UInt32)processedLoc;
224 bool CInFile::Read(void *data, UInt32 size, UInt32 &processedSize)
229 UInt32 processedLoc = 0;
230 bool res = ReadPart(data, size, processedLoc);
231 processedSize += processedLoc;
234 if (processedLoc == 0)
236 data = (void *)((unsigned char *)data + processedLoc);
237 size -= processedLoc;
243 /////////////////////////
246 bool COutFile::Open(LPCTSTR fileName, DWORD shareMode, DWORD creationDisposition, DWORD flagsAndAttributes)
247 { return CFileBase::Create(fileName, GENERIC_WRITE, shareMode, creationDisposition, flagsAndAttributes); }
249 static inline DWORD GetCreationDisposition(bool createAlways)
250 { return createAlways? CREATE_ALWAYS: CREATE_NEW; }
252 bool COutFile::Open(LPCTSTR fileName, DWORD creationDisposition)
253 { return Open(fileName, FILE_SHARE_READ, creationDisposition, FILE_ATTRIBUTE_NORMAL); }
255 bool COutFile::Create(LPCTSTR fileName, bool createAlways)
256 { return Open(fileName, GetCreationDisposition(createAlways)); }
260 bool COutFile::Open(LPCWSTR fileName, DWORD shareMode, DWORD creationDisposition, DWORD flagsAndAttributes)
261 { return CFileBase::Create(fileName, GENERIC_WRITE, shareMode, creationDisposition, flagsAndAttributes); }
263 bool COutFile::Open(LPCWSTR fileName, DWORD creationDisposition)
264 { return Open(fileName, FILE_SHARE_READ, creationDisposition, FILE_ATTRIBUTE_NORMAL); }
266 bool COutFile::Create(LPCWSTR fileName, bool createAlways)
267 { return Open(fileName, GetCreationDisposition(createAlways)); }
271 bool COutFile::SetTime(const FILETIME *creationTime, const FILETIME *lastAccessTime, const FILETIME *lastWriteTime)
272 { return BOOLToBool(::SetFileTime(_handle, creationTime, lastAccessTime, lastWriteTime)); }
274 bool COutFile::SetLastWriteTime(const FILETIME *lastWriteTime)
275 { return SetTime(NULL, NULL, lastWriteTime); }
277 bool COutFile::WritePart(const void *data, UInt32 size, UInt32 &processedSize)
279 if (size > kChunkSizeMax)
280 size = kChunkSizeMax;
281 DWORD processedLoc = 0;
282 bool res = BOOLToBool(::WriteFile(_handle, data, size, &processedLoc, NULL));
283 processedSize = (UInt32)processedLoc;
287 bool COutFile::Write(const void *data, UInt32 size, UInt32 &processedSize)
292 UInt32 processedLoc = 0;
293 bool res = WritePart(data, size, processedLoc);
294 processedSize += processedLoc;
297 if (processedLoc == 0)
299 data = (const void *)((const unsigned char *)data + processedLoc);
300 size -= processedLoc;
306 bool COutFile::SetEndOfFile() { return BOOLToBool(::SetEndOfFile(_handle)); }
308 bool COutFile::SetLength(UInt64 length)
311 if(!Seek(length, newPosition))
313 if(newPosition != length)
315 return SetEndOfFile();