file.hpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #ifndef _RAR_FILE_
  2. #define _RAR_FILE_
  3. #define FILE_USE_OPEN
  4. #ifdef _WIN_ALL
  5. typedef HANDLE FileHandle;
  6. #define FILE_BAD_HANDLE INVALID_HANDLE_VALUE
  7. #elif defined(FILE_USE_OPEN)
  8. typedef off_t FileHandle;
  9. #define FILE_BAD_HANDLE -1
  10. #else
  11. typedef FILE* FileHandle;
  12. #define FILE_BAD_HANDLE NULL
  13. #endif
  14. class RAROptions;
  15. enum FILE_HANDLETYPE {FILE_HANDLENORMAL,FILE_HANDLESTD};
  16. enum FILE_ERRORTYPE {FILE_SUCCESS,FILE_NOTFOUND,FILE_READERROR};
  17. enum FILE_MODE_FLAGS {
  18. // Request read only access to file. Default for Open.
  19. FMF_READ=0,
  20. // Request both read and write access to file. Default for Create.
  21. FMF_UPDATE=1,
  22. // Request write only access to file.
  23. FMF_WRITE=2,
  24. // Open files which are already opened for write by other programs.
  25. FMF_OPENSHARED=4,
  26. // Open files only if no other program is opened it even in shared mode.
  27. FMF_OPENEXCLUSIVE=8,
  28. // Provide read access to created file for other programs.
  29. FMF_SHAREREAD=16,
  30. // Use standard NTFS names without trailing dots and spaces.
  31. FMF_STANDARDNAMES=32,
  32. // Mode flags are not defined yet.
  33. FMF_UNDEFINED=256
  34. };
  35. enum FILE_READ_ERROR_MODE {
  36. FREM_ASK, // Propose to use the already read part, retry or abort.
  37. FREM_TRUNCATE, // Use the already read part without additional prompt.
  38. FREM_IGNORE // Try to skip unreadable block and read further.
  39. };
  40. class File
  41. {
  42. private:
  43. FileHandle hFile;
  44. bool LastWrite;
  45. FILE_HANDLETYPE HandleType;
  46. // If we read the user input in console prompts from stdin, we shall
  47. // process the available line immediately, not waiting for rest of data.
  48. // Otherwise apps piping user responses to multiple Ask() prompts can
  49. // hang if no more data is available yet and pipe isn't closed.
  50. // If we read RAR archive or other file data from stdin, we shall collect
  51. // the entire requested block as long as pipe isn't closed, so we get
  52. // complete archive headers, not split between different reads.
  53. bool LineInput;
  54. bool SkipClose;
  55. FILE_READ_ERROR_MODE ReadErrorMode;
  56. bool NewFile;
  57. bool AllowDelete;
  58. bool AllowExceptions;
  59. #ifdef _WIN_ALL
  60. bool NoSequentialRead;
  61. uint CreateMode;
  62. #endif
  63. bool PreserveAtime;
  64. bool TruncatedAfterReadError;
  65. int64 CurFilePos; // Used for forward seeks in stdin files.
  66. protected:
  67. bool OpenShared; // Set by 'Archive' class.
  68. public:
  69. wchar FileName[NM];
  70. FILE_ERRORTYPE ErrorType;
  71. public:
  72. File();
  73. virtual ~File();
  74. void operator = (File &SrcFile);
  75. // Several functions below are 'virtual', because they are redefined
  76. // by Archive for QOpen and by MultiFile for split files in WinRAR.
  77. virtual bool Open(const wchar *Name,uint Mode=FMF_READ);
  78. void TOpen(const wchar *Name);
  79. bool WOpen(const wchar *Name);
  80. bool Create(const wchar *Name,uint Mode=FMF_UPDATE|FMF_SHAREREAD);
  81. void TCreate(const wchar *Name,uint Mode=FMF_UPDATE|FMF_SHAREREAD);
  82. bool WCreate(const wchar *Name,uint Mode=FMF_UPDATE|FMF_SHAREREAD);
  83. virtual bool Close(); // 'virtual' for MultiFile class.
  84. bool Delete();
  85. bool Rename(const wchar *NewName);
  86. bool Write(const void *Data,size_t Size);
  87. virtual int Read(void *Data,size_t Size);
  88. int DirectRead(void *Data,size_t Size);
  89. virtual void Seek(int64 Offset,int Method);
  90. bool RawSeek(int64 Offset,int Method);
  91. virtual int64 Tell();
  92. void Prealloc(int64 Size);
  93. byte GetByte();
  94. void PutByte(byte Byte);
  95. bool Truncate();
  96. void Flush();
  97. void SetOpenFileTime(RarTime *ftm,RarTime *ftc=NULL,RarTime *fta=NULL);
  98. void SetCloseFileTime(RarTime *ftm,RarTime *fta=NULL);
  99. static void SetCloseFileTimeByName(const wchar *Name,RarTime *ftm,RarTime *fta);
  100. void GetOpenFileTime(RarTime *ft);
  101. virtual bool IsOpened() {return hFile!=FILE_BAD_HANDLE;} // 'virtual' for MultiFile class.
  102. int64 FileLength();
  103. void SetHandleType(FILE_HANDLETYPE Type) {HandleType=Type;}
  104. void SetLineInputMode(bool Mode) {LineInput=Mode;}
  105. FILE_HANDLETYPE GetHandleType() {return HandleType;}
  106. bool IsSeekable() {return HandleType!=FILE_HANDLESTD;}
  107. bool IsDevice();
  108. static bool RemoveCreated();
  109. FileHandle GetHandle() {return hFile;}
  110. void SetHandle(FileHandle Handle) {Close();hFile=Handle;}
  111. void SetReadErrorMode(FILE_READ_ERROR_MODE Mode) {ReadErrorMode=Mode;}
  112. int64 Copy(File &Dest,int64 Length=INT64NDF);
  113. void SetAllowDelete(bool Allow) {AllowDelete=Allow;}
  114. void SetExceptions(bool Allow) {AllowExceptions=Allow;}
  115. void SetPreserveAtime(bool Preserve) {PreserveAtime=Preserve;}
  116. bool IsTruncatedAfterReadError() {return TruncatedAfterReadError;}
  117. #ifdef _UNIX
  118. int GetFD()
  119. {
  120. #ifdef FILE_USE_OPEN
  121. return hFile;
  122. #else
  123. return fileno(hFile);
  124. #endif
  125. }
  126. #endif
  127. static size_t CopyBufferSize()
  128. {
  129. #ifdef _WIN_ALL
  130. // USB flash performance is poor with 64 KB buffer, 256+ KB resolved it.
  131. // For copying from HDD to same HDD the best performance was with 256 KB
  132. // buffer in XP and with 1 MB buffer in Win10.
  133. return WinNT()==WNT_WXP ? 0x40000:0x100000;
  134. #else
  135. return 0x100000;
  136. #endif
  137. }
  138. };
  139. #endif