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