aboutsummaryrefslogtreecommitdiff
path: root/CPP/Windows/FileIO.cpp
diff options
context:
space:
mode:
authorIgor Pavlov <87184205+ip7z@users.noreply.github.com>2024-05-14 00:00:00 +0000
committerIgor Pavlov <87184205+ip7z@users.noreply.github.com>2024-05-15 23:55:04 +0500
commitfc662341e6f85da78ada0e443f6116b978f79f22 (patch)
tree1be1cc402a7a9cbc18d4eeea6b141354c2d559e3 /CPP/Windows/FileIO.cpp
parent5b39dc76f1bc82f941d5c800ab9f34407a06b53a (diff)
download7zip-24.05.tar.gz
7zip-24.05.tar.bz2
7zip-24.05.zip
24.0524.05
Diffstat (limited to 'CPP/Windows/FileIO.cpp')
-rw-r--r--CPP/Windows/FileIO.cpp125
1 files changed, 87 insertions, 38 deletions
diff --git a/CPP/Windows/FileIO.cpp b/CPP/Windows/FileIO.cpp
index 4ecbb7e..dc4de14 100644
--- a/CPP/Windows/FileIO.cpp
+++ b/CPP/Windows/FileIO.cpp
@@ -141,8 +141,17 @@ bool CFileBase::Close() throw()
141{ 141{
142 if (_handle == INVALID_HANDLE_VALUE) 142 if (_handle == INVALID_HANDLE_VALUE)
143 return true; 143 return true;
144 if (!::CloseHandle(_handle)) 144#if 0
145 return false; 145 if (!IsStdStream)
146#endif
147 {
148 if (!::CloseHandle(_handle))
149 return false;
150 }
151#if 0
152 IsStdStream = false;
153 IsStdPipeStream = false;
154#endif
146 _handle = INVALID_HANDLE_VALUE; 155 _handle = INVALID_HANDLE_VALUE;
147 return true; 156 return true;
148} 157}
@@ -457,7 +466,7 @@ bool CInFile::Open(CFSTR fileName)
457// for 32 MB (maybe also for 16 MB). 466// for 32 MB (maybe also for 16 MB).
458// And message can be "Network connection was lost" 467// And message can be "Network connection was lost"
459 468
460static const UInt32 kChunkSizeMax = (1 << 22); 469static const UInt32 kChunkSizeMax = 1 << 22;
461 470
462bool CInFile::Read1(void *data, UInt32 size, UInt32 &processedSize) throw() 471bool CInFile::Read1(void *data, UInt32 size, UInt32 &processedSize) throw()
463{ 472{
@@ -469,8 +478,14 @@ bool CInFile::Read1(void *data, UInt32 size, UInt32 &processedSize) throw()
469 478
470bool CInFile::ReadPart(void *data, UInt32 size, UInt32 &processedSize) throw() 479bool CInFile::ReadPart(void *data, UInt32 size, UInt32 &processedSize) throw()
471{ 480{
481#if 0
482 const UInt32 chunkSizeMax = (0 || IsStdStream) ? (1 << 20) : kChunkSizeMax;
483 if (size > chunkSizeMax)
484 size = chunkSizeMax;
485#else
472 if (size > kChunkSizeMax) 486 if (size > kChunkSizeMax)
473 size = kChunkSizeMax; 487 size = kChunkSizeMax;
488#endif
474 return Read1(data, size, processedSize); 489 return Read1(data, size, processedSize);
475} 490}
476 491
@@ -486,10 +501,10 @@ bool CInFile::Read(void *data, UInt32 size, UInt32 &processedSize) throw()
486 return false; 501 return false;
487 if (processedLoc == 0) 502 if (processedLoc == 0)
488 return true; 503 return true;
489 data = (void *)((unsigned char *)data + processedLoc); 504 data = (void *)((Byte *)data + processedLoc);
490 size -= processedLoc; 505 size -= processedLoc;
491 } 506 }
492 while (size > 0); 507 while (size);
493 return true; 508 return true;
494} 509}
495 510
@@ -506,29 +521,23 @@ bool CInFile::ReadFull(void *data, size_t size, size_t &processedSize) throw()
506 return false; 521 return false;
507 if (processedLoc == 0) 522 if (processedLoc == 0)
508 return true; 523 return true;
509 data = (void *)((unsigned char *)data + processedLoc); 524 data = (void *)((Byte *)data + processedLoc);
510 size -= processedLoc; 525 size -= processedLoc;
511 } 526 }
512 while (size > 0); 527 while (size);
513 return true; 528 return true;
514} 529}
515 530
516// ---------- COutFile --------- 531// ---------- COutFile ---------
517 532
518static inline DWORD GetCreationDisposition(bool createAlways)
519 { return createAlways? CREATE_ALWAYS: CREATE_NEW; }
520
521bool COutFile::Open(CFSTR fileName, DWORD shareMode, DWORD creationDisposition, DWORD flagsAndAttributes) 533bool COutFile::Open(CFSTR fileName, DWORD shareMode, DWORD creationDisposition, DWORD flagsAndAttributes)
522 { return CFileBase::Create(fileName, GENERIC_WRITE, shareMode, creationDisposition, flagsAndAttributes); } 534 { return CFileBase::Create(fileName, GENERIC_WRITE, shareMode, creationDisposition, flagsAndAttributes); }
523 535
524bool COutFile::Open(CFSTR fileName, DWORD creationDisposition) 536bool COutFile::Open_Disposition(CFSTR fileName, DWORD creationDisposition)
525 { return Open(fileName, FILE_SHARE_READ, creationDisposition, FILE_ATTRIBUTE_NORMAL); } 537 { return Open(fileName, FILE_SHARE_READ, creationDisposition, FILE_ATTRIBUTE_NORMAL); }
526 538
527bool COutFile::Create(CFSTR fileName, bool createAlways) 539bool COutFile::Create_ALWAYS_with_Attribs(CFSTR fileName, DWORD flagsAndAttributes)
528 { return Open(fileName, GetCreationDisposition(createAlways)); } 540 { return Open(fileName, FILE_SHARE_READ, CREATE_ALWAYS, flagsAndAttributes); }
529
530bool COutFile::CreateAlways(CFSTR fileName, DWORD flagsAndAttributes)
531 { return Open(fileName, FILE_SHARE_READ, GetCreationDisposition(true), flagsAndAttributes); }
532 541
533bool COutFile::SetTime(const FILETIME *cTime, const FILETIME *aTime, const FILETIME *mTime) throw() 542bool COutFile::SetTime(const FILETIME *cTime, const FILETIME *aTime, const FILETIME *mTime) throw()
534 { return BOOLToBool(::SetFileTime(_handle, cTime, aTime, mTime)); } 543 { return BOOLToBool(::SetFileTime(_handle, cTime, aTime, mTime)); }
@@ -557,10 +566,10 @@ bool COutFile::Write(const void *data, UInt32 size, UInt32 &processedSize) throw
557 return false; 566 return false;
558 if (processedLoc == 0) 567 if (processedLoc == 0)
559 return true; 568 return true;
560 data = (const void *)((const unsigned char *)data + processedLoc); 569 data = (const void *)((const Byte *)data + processedLoc);
561 size -= processedLoc; 570 size -= processedLoc;
562 } 571 }
563 while (size != 0); 572 while (size);
564 return true; 573 return true;
565} 574}
566 575
@@ -574,10 +583,10 @@ bool COutFile::WriteFull(const void *data, size_t size) throw()
574 return false; 583 return false;
575 if (processedLoc == 0) 584 if (processedLoc == 0)
576 return (size == 0); 585 return (size == 0);
577 data = (const void *)((const unsigned char *)data + processedLoc); 586 data = (const void *)((const Byte *)data + processedLoc);
578 size -= processedLoc; 587 size -= processedLoc;
579 } 588 }
580 while (size != 0); 589 while (size);
581 return true; 590 return true;
582} 591}
583 592
@@ -786,11 +795,11 @@ bool CInFile::ReadFull(void *data, size_t size, size_t &processed) throw()
786 return false; 795 return false;
787 if (res == 0) 796 if (res == 0)
788 break; 797 break;
789 data = (void *)((unsigned char *)data + (size_t)res); 798 data = (void *)((Byte *)data + (size_t)res);
790 size -= (size_t)res;
791 processed += (size_t)res; 799 processed += (size_t)res;
800 size -= (size_t)res;
792 } 801 }
793 while (size > 0); 802 while (size);
794 return true; 803 return true;
795} 804}
796 805
@@ -798,23 +807,63 @@ bool CInFile::ReadFull(void *data, size_t size, size_t &processed) throw()
798///////////////////////// 807/////////////////////////
799// COutFile 808// COutFile
800 809
801bool COutFile::Create(const char *name, bool createAlways) 810bool COutFile::OpenBinary_forWrite_oflag(const char *name, int oflag)
802{ 811{
803 Path = name; // change it : set it only if open is success. 812 Path = name; // change it : set it only if open is success.
804 if (createAlways) 813 return OpenBinary(name, oflag, mode_for_Create);
805 {
806 Close();
807 _handle = ::creat(name, mode_for_Create);
808 return _handle != -1;
809 }
810 return OpenBinary(name, O_CREAT | O_EXCL | O_WRONLY, mode_for_Create);
811} 814}
812 815
813bool COutFile::Open(const char *name, DWORD creationDisposition) 816
817/*
818 windows exist non-exist posix
819 CREATE_NEW Fail Create O_CREAT | O_EXCL
820 CREATE_ALWAYS Trunc Create O_CREAT | O_TRUNC
821 OPEN_ALWAYS Open Create O_CREAT
822 OPEN_EXISTING Open Fail 0
823 TRUNCATE_EXISTING Trunc Fail O_TRUNC ???
824
825 // O_CREAT = If the file exists, this flag has no effect except as noted under O_EXCL below.
826 // If O_CREAT and O_EXCL are set, open() shall fail if the file exists.
827 // O_TRUNC : If the file exists and the file is successfully opened, its length shall be truncated to 0.
828*/
829bool COutFile::Open_EXISTING(const char *name)
830 { return OpenBinary_forWrite_oflag(name, O_WRONLY); }
831bool COutFile::Create_ALWAYS(const char *name)
832 { return OpenBinary_forWrite_oflag(name, O_WRONLY | O_CREAT | O_TRUNC); }
833bool COutFile::Create_NEW(const char *name)
834 { return OpenBinary_forWrite_oflag(name, O_WRONLY | O_CREAT | O_EXCL); }
835bool COutFile::Create_ALWAYS_or_Open_ALWAYS(const char *name, bool createAlways)
836{
837 return OpenBinary_forWrite_oflag(name,
838 createAlways ?
839 O_WRONLY | O_CREAT | O_TRUNC :
840 O_WRONLY | O_CREAT);
841}
842/*
843bool COutFile::Create_ALWAYS_or_NEW(const char *name, bool createAlways)
844{
845 return OpenBinary_forWrite_oflag(name,
846 createAlways ?
847 O_WRONLY | O_CREAT | O_TRUNC :
848 O_WRONLY | O_CREAT | O_EXCL);
849}
850bool COutFile::Open_Disposition(const char *name, DWORD creationDisposition)
814{ 851{
815 UNUSED_VAR(creationDisposition) // FIXME 852 int flag;
816 return Create(name, false); 853 switch (creationDisposition)
854 {
855 case CREATE_NEW: flag = O_WRONLY | O_CREAT | O_EXCL; break;
856 case CREATE_ALWAYS: flag = O_WRONLY | O_CREAT | O_TRUNC; break;
857 case OPEN_ALWAYS: flag = O_WRONLY | O_CREAT; break;
858 case OPEN_EXISTING: flag = O_WRONLY; break;
859 case TRUNCATE_EXISTING: flag = O_WRONLY | O_TRUNC; break;
860 default:
861 SetLastError(EINVAL);
862 return false;
863 }
864 return OpenBinary_forWrite_oflag(name, flag);
817} 865}
866*/
818 867
819ssize_t COutFile::write_part(const void *data, size_t size) throw() 868ssize_t COutFile::write_part(const void *data, size_t size) throw()
820{ 869{
@@ -833,11 +882,11 @@ ssize_t COutFile::write_full(const void *data, size_t size, size_t &processed) t
833 return res; 882 return res;
834 if (res == 0) 883 if (res == 0)
835 break; 884 break;
836 data = (const void *)((const unsigned char *)data + (size_t)res); 885 data = (const void *)((const Byte *)data + (size_t)res);
837 size -= (size_t)res;
838 processed += (size_t)res; 886 processed += (size_t)res;
887 size -= (size_t)res;
839 } 888 }
840 while (size > 0); 889 while (size);
841 return (ssize_t)processed; 890 return (ssize_t)processed;
842} 891}
843 892