aboutsummaryrefslogtreecommitdiff
path: root/CPP/7zip/Common/FileStreams.cpp
diff options
context:
space:
mode:
authorIgor Pavlov <87184205+ip7z@users.noreply.github.com>2023-06-21 00:00:00 +0000
committerIgor Pavlov <87184205+ip7z@users.noreply.github.com>2023-12-17 14:59:19 +0500
commit5b39dc76f1bc82f941d5c800ab9f34407a06b53a (patch)
treefe5e17420300b715021a76328444088d32047963 /CPP/7zip/Common/FileStreams.cpp
parent93be7d4abfd4233228f58ee1fbbcd76d91be66a4 (diff)
download7zip-23.01.tar.gz
7zip-23.01.tar.bz2
7zip-23.01.zip
23.0123.01
Diffstat (limited to '')
-rw-r--r--CPP/7zip/Common/FileStreams.cpp125
1 files changed, 74 insertions, 51 deletions
diff --git a/CPP/7zip/Common/FileStreams.cpp b/CPP/7zip/Common/FileStreams.cpp
index 0349e90..4298636 100644
--- a/CPP/7zip/Common/FileStreams.cpp
+++ b/CPP/7zip/Common/FileStreams.cpp
@@ -12,17 +12,19 @@
12#include <pwd.h> 12#include <pwd.h>
13 13
14// for major()/minor(): 14// for major()/minor():
15#if defined(__FreeBSD__) || defined(BSD)
16#include <sys/types.h> 15#include <sys/types.h>
16#if defined(__FreeBSD__) || defined(BSD) || defined(__APPLE__)
17#else 17#else
18#ifndef major
18#include <sys/sysmacros.h> 19#include <sys/sysmacros.h>
19#endif 20#endif
20
21#endif 21#endif
22 22
23#endif // _WIN32
24
23#include "../../Windows/FileFind.h" 25#include "../../Windows/FileFind.h"
24 26
25#ifdef SUPPORT_DEVICE_FILE 27#ifdef Z7_DEVICE_FILE
26#include "../../../C/Alloc.h" 28#include "../../../C/Alloc.h"
27#include "../../Common/Defs.h" 29#include "../../Common/Defs.h"
28#endif 30#endif
@@ -47,15 +49,15 @@ static inline HRESULT ConvertBoolToHRESULT(bool result)
47} 49}
48 50
49 51
50#ifdef SUPPORT_DEVICE_FILE 52#ifdef Z7_DEVICE_FILE
51static const UInt32 kClusterSize = 1 << 18; 53static const UInt32 kClusterSize = 1 << 18;
52#endif 54#endif
53 55
54CInFileStream::CInFileStream(): 56CInFileStream::CInFileStream():
55 #ifdef SUPPORT_DEVICE_FILE 57 #ifdef Z7_DEVICE_FILE
56 VirtPos(0), 58 VirtPos(0),
57 PhyPos(0), 59 PhyPos(0),
58 Buf(0), 60 Buf(NULL),
59 BufSize(0), 61 BufSize(0),
60 #endif 62 #endif
61 #ifndef _WIN32 63 #ifndef _WIN32
@@ -73,7 +75,7 @@ CInFileStream::CInFileStream():
73 75
74CInFileStream::~CInFileStream() 76CInFileStream::~CInFileStream()
75{ 77{
76 #ifdef SUPPORT_DEVICE_FILE 78 #ifdef Z7_DEVICE_FILE
77 MidFree(Buf); 79 MidFree(Buf);
78 #endif 80 #endif
79 81
@@ -81,11 +83,11 @@ CInFileStream::~CInFileStream()
81 Callback->InFileStream_On_Destroy(this, CallbackRef); 83 Callback->InFileStream_On_Destroy(this, CallbackRef);
82} 84}
83 85
84STDMETHODIMP CInFileStream::Read(void *data, UInt32 size, UInt32 *processedSize) 86Z7_COM7F_IMF(CInFileStream::Read(void *data, UInt32 size, UInt32 *processedSize))
85{ 87{
86 #ifdef USE_WIN_FILE 88 #ifdef Z7_FILE_STREAMS_USE_WIN_FILE
87 89
88 #ifdef SUPPORT_DEVICE_FILE 90 #ifdef Z7_DEVICE_FILE
89 if (processedSize) 91 if (processedSize)
90 *processedSize = 0; 92 *processedSize = 0;
91 if (size == 0) 93 if (size == 0)
@@ -96,7 +98,7 @@ STDMETHODIMP CInFileStream::Read(void *data, UInt32 size, UInt32 *processedSize)
96 { 98 {
97 if (VirtPos >= File.Size) 99 if (VirtPos >= File.Size)
98 return VirtPos == File.Size ? S_OK : E_FAIL; 100 return VirtPos == File.Size ? S_OK : E_FAIL;
99 UInt64 rem = File.Size - VirtPos; 101 const UInt64 rem = File.Size - VirtPos;
100 if (size > rem) 102 if (size > rem)
101 size = (UInt32)rem; 103 size = (UInt32)rem;
102 } 104 }
@@ -104,13 +106,13 @@ STDMETHODIMP CInFileStream::Read(void *data, UInt32 size, UInt32 *processedSize)
104 { 106 {
105 const UInt32 mask = kClusterSize - 1; 107 const UInt32 mask = kClusterSize - 1;
106 const UInt64 mask2 = ~(UInt64)mask; 108 const UInt64 mask2 = ~(UInt64)mask;
107 UInt64 alignedPos = VirtPos & mask2; 109 const UInt64 alignedPos = VirtPos & mask2;
108 if (BufSize > 0 && BufStartPos == alignedPos) 110 if (BufSize > 0 && BufStartPos == alignedPos)
109 { 111 {
110 UInt32 pos = (UInt32)VirtPos & mask; 112 const UInt32 pos = (UInt32)VirtPos & mask;
111 if (pos >= BufSize) 113 if (pos >= BufSize)
112 return S_OK; 114 return S_OK;
113 UInt32 rem = MyMin(BufSize - pos, size); 115 const UInt32 rem = MyMin(BufSize - pos, size);
114 memcpy(data, Buf + pos, rem); 116 memcpy(data, Buf + pos, rem);
115 VirtPos += rem; 117 VirtPos += rem;
116 if (processedSize) 118 if (processedSize)
@@ -119,7 +121,7 @@ STDMETHODIMP CInFileStream::Read(void *data, UInt32 size, UInt32 *processedSize)
119 } 121 }
120 122
121 bool useBuf = false; 123 bool useBuf = false;
122 if ((VirtPos & mask) != 0 || ((ptrdiff_t)data & mask) != 0 ) 124 if ((VirtPos & mask) != 0 || ((size_t)(ptrdiff_t)data & mask) != 0 )
123 useBuf = true; 125 useBuf = true;
124 else 126 else
125 { 127 {
@@ -138,7 +140,7 @@ STDMETHODIMP CInFileStream::Read(void *data, UInt32 size, UInt32 *processedSize)
138 if (alignedPos != PhyPos) 140 if (alignedPos != PhyPos)
139 { 141 {
140 UInt64 realNewPosition; 142 UInt64 realNewPosition;
141 bool result = File.Seek((Int64)alignedPos, FILE_BEGIN, realNewPosition); 143 const bool result = File.Seek((Int64)alignedPos, FILE_BEGIN, realNewPosition);
142 if (!result) 144 if (!result)
143 return ConvertBoolToHRESULT(result); 145 return ConvertBoolToHRESULT(result);
144 PhyPos = realNewPosition; 146 PhyPos = realNewPosition;
@@ -155,7 +157,7 @@ STDMETHODIMP CInFileStream::Read(void *data, UInt32 size, UInt32 *processedSize)
155 if (!Buf) 157 if (!Buf)
156 return E_OUTOFMEMORY; 158 return E_OUTOFMEMORY;
157 } 159 }
158 bool result = File.Read1(Buf, readSize, BufSize); 160 const bool result = File.Read1(Buf, readSize, BufSize);
159 if (!result) 161 if (!result)
160 return ConvertBoolToHRESULT(result); 162 return ConvertBoolToHRESULT(result);
161 163
@@ -180,7 +182,7 @@ STDMETHODIMP CInFileStream::Read(void *data, UInt32 size, UInt32 *processedSize)
180 if (processedSize) 182 if (processedSize)
181 *processedSize = realProcessedSize; 183 *processedSize = realProcessedSize;
182 184
183 #ifdef SUPPORT_DEVICE_FILE 185 #ifdef Z7_DEVICE_FILE
184 VirtPos += realProcessedSize; 186 VirtPos += realProcessedSize;
185 PhyPos += realProcessedSize; 187 PhyPos += realProcessedSize;
186 #endif 188 #endif
@@ -188,7 +190,7 @@ STDMETHODIMP CInFileStream::Read(void *data, UInt32 size, UInt32 *processedSize)
188 if (result) 190 if (result)
189 return S_OK; 191 return S_OK;
190 192
191 #else // USE_WIN_FILE 193 #else // Z7_FILE_STREAMS_USE_WIN_FILE
192 194
193 if (processedSize) 195 if (processedSize)
194 *processedSize = 0; 196 *processedSize = 0;
@@ -199,7 +201,7 @@ STDMETHODIMP CInFileStream::Read(void *data, UInt32 size, UInt32 *processedSize)
199 *processedSize = (UInt32)res; 201 *processedSize = (UInt32)res;
200 return S_OK; 202 return S_OK;
201 } 203 }
202 #endif // USE_WIN_FILE 204 #endif // Z7_FILE_STREAMS_USE_WIN_FILE
203 205
204 { 206 {
205 const DWORD error = ::GetLastError(); 207 const DWORD error = ::GetLastError();
@@ -212,7 +214,7 @@ STDMETHODIMP CInFileStream::Read(void *data, UInt32 size, UInt32 *processedSize)
212} 214}
213 215
214#ifdef UNDER_CE 216#ifdef UNDER_CE
215STDMETHODIMP CStdInFileStream::Read(void *data, UInt32 size, UInt32 *processedSize) 217Z7_COM7F_IMF(CStdInFileStream::Read(void *data, UInt32 size, UInt32 *processedSize))
216{ 218{
217 size_t s2 = fread(data, 1, size, stdin); 219 size_t s2 = fread(data, 1, size, stdin);
218 int error = ferror(stdin); 220 int error = ferror(stdin);
@@ -223,7 +225,7 @@ STDMETHODIMP CStdInFileStream::Read(void *data, UInt32 size, UInt32 *processedSi
223 return E_FAIL; 225 return E_FAIL;
224} 226}
225#else 227#else
226STDMETHODIMP CStdInFileStream::Read(void *data, UInt32 size, UInt32 *processedSize) 228Z7_COM7F_IMF(CStdInFileStream::Read(void *data, UInt32 size, UInt32 *processedSize))
227{ 229{
228 #ifdef _WIN32 230 #ifdef _WIN32
229 231
@@ -259,14 +261,14 @@ STDMETHODIMP CStdInFileStream::Read(void *data, UInt32 size, UInt32 *processedSi
259 261
260#endif 262#endif
261 263
262STDMETHODIMP CInFileStream::Seek(Int64 offset, UInt32 seekOrigin, UInt64 *newPosition) 264Z7_COM7F_IMF(CInFileStream::Seek(Int64 offset, UInt32 seekOrigin, UInt64 *newPosition))
263{ 265{
264 if (seekOrigin >= 3) 266 if (seekOrigin >= 3)
265 return STG_E_INVALIDFUNCTION; 267 return STG_E_INVALIDFUNCTION;
266 268
267 #ifdef USE_WIN_FILE 269 #ifdef Z7_FILE_STREAMS_USE_WIN_FILE
268 270
269 #ifdef SUPPORT_DEVICE_FILE 271 #ifdef Z7_DEVICE_FILE
270 if (File.IsDeviceFile && (File.SizeDefined || seekOrigin != STREAM_SEEK_END)) 272 if (File.IsDeviceFile && (File.SizeDefined || seekOrigin != STREAM_SEEK_END))
271 { 273 {
272 switch (seekOrigin) 274 switch (seekOrigin)
@@ -293,7 +295,7 @@ STDMETHODIMP CInFileStream::Seek(Int64 offset, UInt32 seekOrigin, UInt64 *newPos
293 in case of error. So we don't need additional code below */ 295 in case of error. So we don't need additional code below */
294 // if (!result) { realNewPosition = 0; File.GetPosition(realNewPosition); } 296 // if (!result) { realNewPosition = 0; File.GetPosition(realNewPosition); }
295 297
296 #ifdef SUPPORT_DEVICE_FILE 298 #ifdef Z7_DEVICE_FILE
297 PhyPos = VirtPos = realNewPosition; 299 PhyPos = VirtPos = realNewPosition;
298 #endif 300 #endif
299 301
@@ -319,17 +321,19 @@ STDMETHODIMP CInFileStream::Seek(Int64 offset, UInt32 seekOrigin, UInt64 *newPos
319 #endif 321 #endif
320} 322}
321 323
322STDMETHODIMP CInFileStream::GetSize(UInt64 *size) 324Z7_COM7F_IMF(CInFileStream::GetSize(UInt64 *size))
323{ 325{
324 return ConvertBoolToHRESULT(File.GetLength(*size)); 326 return ConvertBoolToHRESULT(File.GetLength(*size));
325} 327}
326 328
327#ifdef USE_WIN_FILE 329#ifdef Z7_FILE_STREAMS_USE_WIN_FILE
328 330
329STDMETHODIMP CInFileStream::GetProps(UInt64 *size, FILETIME *cTime, FILETIME *aTime, FILETIME *mTime, UInt32 *attrib) 331Z7_COM7F_IMF(CInFileStream::GetProps(UInt64 *size, FILETIME *cTime, FILETIME *aTime, FILETIME *mTime, UInt32 *attrib))
330{ 332{
331 if (!_info_WasLoaded) 333 if (!_info_WasLoaded)
332 RINOK(ReloadProps()); 334 {
335 RINOK(ReloadProps())
336 }
333 const BY_HANDLE_FILE_INFORMATION &info = _info; 337 const BY_HANDLE_FILE_INFORMATION &info = _info;
334 /* 338 /*
335 BY_HANDLE_FILE_INFORMATION info; 339 BY_HANDLE_FILE_INFORMATION info;
@@ -346,10 +350,12 @@ STDMETHODIMP CInFileStream::GetProps(UInt64 *size, FILETIME *cTime, FILETIME *aT
346 } 350 }
347} 351}
348 352
349STDMETHODIMP CInFileStream::GetProps2(CStreamFileProps *props) 353Z7_COM7F_IMF(CInFileStream::GetProps2(CStreamFileProps *props))
350{ 354{
351 if (!_info_WasLoaded) 355 if (!_info_WasLoaded)
352 RINOK(ReloadProps()); 356 {
357 RINOK(ReloadProps())
358 }
353 const BY_HANDLE_FILE_INFORMATION &info = _info; 359 const BY_HANDLE_FILE_INFORMATION &info = _info;
354 /* 360 /*
355 BY_HANDLE_FILE_INFORMATION info; 361 BY_HANDLE_FILE_INFORMATION info;
@@ -370,17 +376,19 @@ STDMETHODIMP CInFileStream::GetProps2(CStreamFileProps *props)
370 } 376 }
371} 377}
372 378
373STDMETHODIMP CInFileStream::GetProperty(PROPID propID, PROPVARIANT *value) 379Z7_COM7F_IMF(CInFileStream::GetProperty(PROPID propID, PROPVARIANT *value))
374{ 380{
375 if (!_info_WasLoaded) 381 if (!_info_WasLoaded)
376 RINOK(ReloadProps()); 382 {
383 RINOK(ReloadProps())
384 }
377 385
378 if (!_info_WasLoaded) 386 if (!_info_WasLoaded)
379 return S_OK; 387 return S_OK;
380 388
381 NWindows::NCOM::CPropVariant prop; 389 NWindows::NCOM::CPropVariant prop;
382 390
383 #ifdef SUPPORT_DEVICE_FILE 391 #ifdef Z7_DEVICE_FILE
384 if (File.IsDeviceFile) 392 if (File.IsDeviceFile)
385 { 393 {
386 switch (propID) 394 switch (propID)
@@ -436,9 +444,9 @@ STDMETHODIMP CInFileStream::GetProperty(PROPID propID, PROPVARIANT *value)
436} 444}
437 445
438 446
439STDMETHODIMP CInFileStream::ReloadProps() 447Z7_COM7F_IMF(CInFileStream::ReloadProps())
440{ 448{
441 #ifdef SUPPORT_DEVICE_FILE 449 #ifdef Z7_DEVICE_FILE
442 if (File.IsDeviceFile) 450 if (File.IsDeviceFile)
443 { 451 {
444 memset(&_info, 0, sizeof(_info)); 452 memset(&_info, 0, sizeof(_info));
@@ -461,10 +469,12 @@ STDMETHODIMP CInFileStream::ReloadProps()
461 469
462#elif !defined(_WIN32) 470#elif !defined(_WIN32)
463 471
464STDMETHODIMP CInFileStream::GetProps(UInt64 *size, FILETIME *cTime, FILETIME *aTime, FILETIME *mTime, UInt32 *attrib) 472Z7_COM7F_IMF(CInFileStream::GetProps(UInt64 *size, FILETIME *cTime, FILETIME *aTime, FILETIME *mTime, UInt32 *attrib))
465{ 473{
466 if (!_info_WasLoaded) 474 if (!_info_WasLoaded)
467 RINOK(ReloadProps()); 475 {
476 RINOK(ReloadProps())
477 }
468 const struct stat &st = _info; 478 const struct stat &st = _info;
469 /* 479 /*
470 struct stat st; 480 struct stat st;
@@ -483,10 +493,12 @@ STDMETHODIMP CInFileStream::GetProps(UInt64 *size, FILETIME *cTime, FILETIME *aT
483 493
484// #include <stdio.h> 494// #include <stdio.h>
485 495
486STDMETHODIMP CInFileStream::GetProps2(CStreamFileProps *props) 496Z7_COM7F_IMF(CInFileStream::GetProps2(CStreamFileProps *props))
487{ 497{
488 if (!_info_WasLoaded) 498 if (!_info_WasLoaded)
489 RINOK(ReloadProps()); 499 {
500 RINOK(ReloadProps())
501 }
490 const struct stat &st = _info; 502 const struct stat &st = _info;
491 /* 503 /*
492 struct stat st; 504 struct stat st;
@@ -521,10 +533,12 @@ STDMETHODIMP CInFileStream::GetProps2(CStreamFileProps *props)
521 return S_OK; 533 return S_OK;
522} 534}
523 535
524STDMETHODIMP CInFileStream::GetProperty(PROPID propID, PROPVARIANT *value) 536Z7_COM7F_IMF(CInFileStream::GetProperty(PROPID propID, PROPVARIANT *value))
525{ 537{
526 if (!_info_WasLoaded) 538 if (!_info_WasLoaded)
527 RINOK(ReloadProps()); 539 {
540 RINOK(ReloadProps())
541 }
528 542
529 if (!_info_WasLoaded) 543 if (!_info_WasLoaded)
530 return S_OK; 544 return S_OK;
@@ -544,6 +558,11 @@ STDMETHODIMP CInFileStream::GetProperty(PROPID propID, PROPVARIANT *value)
544 case kpidMTime: PropVariant_SetFrom_FiTime(prop, ST_MTIME(st)); break; 558 case kpidMTime: PropVariant_SetFrom_FiTime(prop, ST_MTIME(st)); break;
545 case kpidPosixAttrib: prop = (UInt32)st.st_mode; break; 559 case kpidPosixAttrib: prop = (UInt32)st.st_mode; break;
546 560
561 #if defined(__APPLE__)
562 #pragma GCC diagnostic push
563 #pragma GCC diagnostic ignored "-Wsign-conversion"
564 #endif
565
547 case kpidDeviceMajor: 566 case kpidDeviceMajor:
548 { 567 {
549 // printf("\nst.st_rdev = %d\n", st.st_rdev); 568 // printf("\nst.st_rdev = %d\n", st.st_rdev);
@@ -563,6 +582,10 @@ STDMETHODIMP CInFileStream::GetProperty(PROPID propID, PROPVARIANT *value)
563 // prop = (UInt32)123456789; // for debug 582 // prop = (UInt32)123456789; // for debug
564 break; 583 break;
565 584
585 #if defined(__APPLE__)
586 #pragma GCC diagnostic pop
587 #endif
588
566 /* 589 /*
567 case kpidDevice: 590 case kpidDevice:
568 if (S_ISCHR(st.st_mode) || 591 if (S_ISCHR(st.st_mode) ||
@@ -632,7 +655,7 @@ STDMETHODIMP CInFileStream::GetProperty(PROPID propID, PROPVARIANT *value)
632} 655}
633 656
634 657
635STDMETHODIMP CInFileStream::ReloadProps() 658Z7_COM7F_IMF(CInFileStream::ReloadProps())
636{ 659{
637 _info_WasLoaded = (File.my_fstat(&_info) == 0); 660 _info_WasLoaded = (File.my_fstat(&_info) == 0);
638 if (!_info_WasLoaded) 661 if (!_info_WasLoaded)
@@ -653,9 +676,9 @@ HRESULT COutFileStream::Close()
653 return ConvertBoolToHRESULT(File.Close()); 676 return ConvertBoolToHRESULT(File.Close());
654} 677}
655 678
656STDMETHODIMP COutFileStream::Write(const void *data, UInt32 size, UInt32 *processedSize) 679Z7_COM7F_IMF(COutFileStream::Write(const void *data, UInt32 size, UInt32 *processedSize))
657{ 680{
658 #ifdef USE_WIN_FILE 681 #ifdef Z7_FILE_STREAMS_USE_WIN_FILE
659 682
660 UInt32 realProcessedSize; 683 UInt32 realProcessedSize;
661 const bool result = File.Write(data, size, realProcessedSize); 684 const bool result = File.Write(data, size, realProcessedSize);
@@ -680,12 +703,12 @@ STDMETHODIMP COutFileStream::Write(const void *data, UInt32 size, UInt32 *proces
680 #endif 703 #endif
681} 704}
682 705
683STDMETHODIMP COutFileStream::Seek(Int64 offset, UInt32 seekOrigin, UInt64 *newPosition) 706Z7_COM7F_IMF(COutFileStream::Seek(Int64 offset, UInt32 seekOrigin, UInt64 *newPosition))
684{ 707{
685 if (seekOrigin >= 3) 708 if (seekOrigin >= 3)
686 return STG_E_INVALIDFUNCTION; 709 return STG_E_INVALIDFUNCTION;
687 710
688 #ifdef USE_WIN_FILE 711 #ifdef Z7_FILE_STREAMS_USE_WIN_FILE
689 712
690 UInt64 realNewPosition = 0; 713 UInt64 realNewPosition = 0;
691 const bool result = File.Seek(offset, seekOrigin, realNewPosition); 714 const bool result = File.Seek(offset, seekOrigin, realNewPosition);
@@ -705,7 +728,7 @@ STDMETHODIMP COutFileStream::Seek(Int64 offset, UInt32 seekOrigin, UInt64 *newPo
705 #endif 728 #endif
706} 729}
707 730
708STDMETHODIMP COutFileStream::SetSize(UInt64 newSize) 731Z7_COM7F_IMF(COutFileStream::SetSize(UInt64 newSize))
709{ 732{
710 return ConvertBoolToHRESULT(File.SetLength_KeepPosition(newSize)); 733 return ConvertBoolToHRESULT(File.SetLength_KeepPosition(newSize));
711} 734}
@@ -717,7 +740,7 @@ HRESULT COutFileStream::GetSize(UInt64 *size)
717 740
718#ifdef UNDER_CE 741#ifdef UNDER_CE
719 742
720STDMETHODIMP CStdOutFileStream::Write(const void *data, UInt32 size, UInt32 *processedSize) 743Z7_COM7F_IMF(CStdOutFileStream::Write(const void *data, UInt32 size, UInt32 *processedSize))
721{ 744{
722 size_t s2 = fwrite(data, 1, size, stdout); 745 size_t s2 = fwrite(data, 1, size, stdout);
723 if (processedSize) 746 if (processedSize)
@@ -727,7 +750,7 @@ STDMETHODIMP CStdOutFileStream::Write(const void *data, UInt32 size, UInt32 *pro
727 750
728#else 751#else
729 752
730STDMETHODIMP CStdOutFileStream::Write(const void *data, UInt32 size, UInt32 *processedSize) 753Z7_COM7F_IMF(CStdOutFileStream::Write(const void *data, UInt32 size, UInt32 *processedSize))
731{ 754{
732 if (processedSize) 755 if (processedSize)
733 *processedSize = 0; 756 *processedSize = 0;