CMS 3D CMS Logo

7zFile.cc
Go to the documentation of this file.
1 /* 7zFile.c -- File IO
2 2009-11-24 : Igor Pavlov : Public domain */
3 
4 #include "7zFile.h"
5 
6 #ifndef USE_WINDOWS_FILE
7 
8 #ifndef UNDER_CE
9 #include <cerrno>
10 #endif
11 
12 #else
13 
14 /*
15  ReadFile and WriteFile functions in Windows have BUG:
16  If you Read or Write 64MB or more (probably min_failure_size = 64MB - 32KB + 1)
17  from/to Network file, it returns ERROR_NO_SYSTEM_RESOURCES
18  (Insufficient system resources exist to complete the requested service).
19  Probably in some version of Windows there are problems with other sizes:
20  for 32 MB (maybe also for 16 MB).
21  And message can be "Network connection was lost"
22 */
23 
24 #define kChunkSizeMax (1 << 22)
25 
26 #endif
27 
29 #ifdef USE_WINDOWS_FILE
30  p->handle = INVALID_HANDLE_VALUE;
31 #else
32  p->file = nullptr;
33 #endif
34 }
35 
36 #if !defined(UNDER_CE) || !defined(USE_WINDOWS_FILE)
37 static WRes File_Open(CSzFile *p, const char *name, int writeMode) {
38 #ifdef USE_WINDOWS_FILE
39  p->handle = CreateFileA(name,
40  writeMode ? GENERIC_WRITE : GENERIC_READ,
41  FILE_SHARE_READ,
42  NULL,
43  writeMode ? CREATE_ALWAYS : OPEN_EXISTING,
44  FILE_ATTRIBUTE_NORMAL,
45  NULL);
46  return (p->handle != INVALID_HANDLE_VALUE) ? 0 : GetLastError();
47 #else
48  p->file = fopen(name, writeMode ? "wb+" : "rb");
49  return (p->file != nullptr) ? 0 :
50 #ifdef UNDER_CE
51  2; /* ENOENT */
52 #else
53  errno;
54 #endif
55 #endif
56 }
57 
58 WRes InFile_Open(CSzFile *p, const char *name) { return File_Open(p, name, 0); }
59 WRes OutFile_Open(CSzFile *p, const char *name) { return File_Open(p, name, 1); }
60 #endif
61 
62 #ifdef USE_WINDOWS_FILE
63 static WRes File_OpenW(CSzFile *p, const WCHAR *name, int writeMode) {
64  p->handle = CreateFileW(name,
65  writeMode ? GENERIC_WRITE : GENERIC_READ,
66  FILE_SHARE_READ,
67  NULL,
68  writeMode ? CREATE_ALWAYS : OPEN_EXISTING,
69  FILE_ATTRIBUTE_NORMAL,
70  NULL);
71  return (p->handle != INVALID_HANDLE_VALUE) ? 0 : GetLastError();
72 }
73 WRes InFile_OpenW(CSzFile *p, const WCHAR *name) { return File_OpenW(p, name, 0); }
74 WRes OutFile_OpenW(CSzFile *p, const WCHAR *name) { return File_OpenW(p, name, 1); }
75 #endif
76 
78 #ifdef USE_WINDOWS_FILE
79  if (p->handle != INVALID_HANDLE_VALUE) {
80  if (!CloseHandle(p->handle))
81  return GetLastError();
82  p->handle = INVALID_HANDLE_VALUE;
83  }
84 #else
85  if (p->file != nullptr) {
86  int res = fclose(p->file);
87  if (res != 0)
88  return res;
89  p->file = nullptr;
90  }
91 #endif
92  return 0;
93 }
94 
95 WRes File_Read(CSzFile *p, void *data, size_t *size) {
96  size_t originalSize = *size;
97  if (originalSize == 0)
98  return 0;
99 
100 #ifdef USE_WINDOWS_FILE
101 
102  *size = 0;
103  do {
104  DWORD curSize = (originalSize > kChunkSizeMax) ? kChunkSizeMax : (DWORD)originalSize;
105  DWORD processed = 0;
106  BOOL res = ReadFile(p->handle, data, curSize, &processed, NULL);
107  data = (void *)((Byte *)data + processed);
108  originalSize -= processed;
109  *size += processed;
110  if (!res)
111  return GetLastError();
112  if (processed == 0)
113  break;
114  } while (originalSize > 0);
115  return 0;
116 
117 #else
118 
119  *size = fread(data, 1, originalSize, p->file);
120  if (*size == originalSize)
121  return 0;
122  return ferror(p->file);
123 
124 #endif
125 }
126 
127 WRes File_Write(CSzFile *p, const void *data, size_t *size) {
128  size_t originalSize = *size;
129  if (originalSize == 0)
130  return 0;
131 
132 #ifdef USE_WINDOWS_FILE
133 
134  *size = 0;
135  do {
136  DWORD curSize = (originalSize > kChunkSizeMax) ? kChunkSizeMax : (DWORD)originalSize;
137  DWORD processed = 0;
138  BOOL res = WriteFile(p->handle, data, curSize, &processed, NULL);
139  data = (void *)((Byte *)data + processed);
140  originalSize -= processed;
141  *size += processed;
142  if (!res)
143  return GetLastError();
144  if (processed == 0)
145  break;
146  } while (originalSize > 0);
147  return 0;
148 
149 #else
150 
151  *size = fwrite(data, 1, originalSize, p->file);
152  if (*size == originalSize)
153  return 0;
154  return ferror(p->file);
155 
156 #endif
157 }
158 
160 #ifdef USE_WINDOWS_FILE
161 
162  LARGE_INTEGER value;
163  DWORD moveMethod;
164  value.LowPart = (DWORD)*pos;
165  value.HighPart = (LONG)((UInt64)*pos >> 16 >> 16); /* for case when UInt64 is 32-bit only */
166  switch (origin) {
167  case SZ_SEEK_SET:
168  moveMethod = FILE_BEGIN;
169  break;
170  case SZ_SEEK_CUR:
171  moveMethod = FILE_CURRENT;
172  break;
173  case SZ_SEEK_END:
174  moveMethod = FILE_END;
175  break;
176  default:
177  return ERROR_INVALID_PARAMETER;
178  }
179  value.LowPart = SetFilePointer(p->handle, value.LowPart, &value.HighPart, moveMethod);
180  if (value.LowPart == 0xFFFFFFFF) {
181  WRes res = GetLastError();
182  if (res != NO_ERROR)
183  return res;
184  }
185  *pos = ((Int64)value.HighPart << 32) | value.LowPart;
186  return 0;
187 
188 #else
189 
190  int moveMethod;
191  int res;
192  switch (origin) {
193  case SZ_SEEK_SET:
194  moveMethod = SEEK_SET;
195  break;
196  case SZ_SEEK_CUR:
197  moveMethod = SEEK_CUR;
198  break;
199  case SZ_SEEK_END:
200  moveMethod = SEEK_END;
201  break;
202  default:
203  return 1;
204  }
205  res = fseek(p->file, (long)*pos, moveMethod);
206  *pos = ftell(p->file);
207  return res;
208 
209 #endif
210 }
211 
213 #ifdef USE_WINDOWS_FILE
214 
215  DWORD sizeHigh;
216  DWORD sizeLow = GetFileSize(p->handle, &sizeHigh);
217  if (sizeLow == 0xFFFFFFFF) {
218  DWORD res = GetLastError();
219  if (res != NO_ERROR)
220  return res;
221  }
222  *length = (((UInt64)sizeHigh) << 32) + sizeLow;
223  return 0;
224 
225 #else
226 
227  long pos = ftell(p->file);
228  int res = fseek(p->file, 0, SEEK_END);
229  *length = ftell(p->file);
230  fseek(p->file, pos, SEEK_SET);
231  return res;
232 
233 #endif
234 }
235 
236 /* ---------- FileSeqInStream ---------- */
237 
238 static SRes FileSeqInStream_Read(void *pp, void *buf, size_t *size) {
240  return File_Read(&p->file, buf, size) == 0 ? SZ_OK : SZ_ERROR_READ;
241 }
242 
244 
245 /* ---------- FileInStream ---------- */
246 
247 static SRes FileInStream_Read(void *pp, void *buf, size_t *size) {
249  return (File_Read(&p->file, buf, size) == 0) ? SZ_OK : SZ_ERROR_READ;
250 }
251 
252 static SRes FileInStream_Seek(void *pp, Int64 *pos, ESzSeek origin) {
254  return File_Seek(&p->file, pos, origin);
255 }
256 
258  p->s.Read = FileInStream_Read;
259  p->s.Seek = FileInStream_Seek;
260 }
261 
262 /* ---------- FileOutStream ---------- */
263 
264 static size_t FileOutStream_Write(void *pp, const void *data, size_t size) {
266  File_Write(&p->file, data, &size);
267  return size;
268 }
269 
File_Write
WRes File_Write(CSzFile *p, const void *data, size_t *size)
Definition: 7zFile.cc:127
Byte
unsigned char Byte
Definition: Types.h:60
SZ_SEEK_END
Definition: Types.h:155
FileSeqInStream_Read
static SRes FileSeqInStream_Read(void *pp, void *buf, size_t *size)
Definition: 7zFile.cc:238
WRes
int WRes
Definition: Types.h:48
File_Open
static WRes File_Open(CSzFile *p, const char *name, int writeMode)
Definition: 7zFile.cc:37
File_GetLength
WRes File_GetLength(CSzFile *p, UInt64 *length)
Definition: 7zFile.cc:212
pos
Definition: PixelAliasList.h:18
UInt64
unsigned long long int UInt64
Definition: Types.h:88
7zFile.h
File_Construct
void File_Construct(CSzFile *p)
Definition: 7zFile.cc:28
SZ_ERROR_READ
#define SZ_ERROR_READ
Definition: Types.h:34
File_Seek
WRes File_Seek(CSzFile *p, Int64 *pos, ESzSeek origin)
Definition: 7zFile.cc:159
SZ_OK
#define SZ_OK
Definition: Types.h:25
FileInStream_Read
static SRes FileInStream_Read(void *pp, void *buf, size_t *size)
Definition: 7zFile.cc:247
File_Read
WRes File_Read(CSzFile *p, void *data, size_t *size)
Definition: 7zFile.cc:95
ESzSeek
ESzSeek
Definition: Types.h:155
FileOutStream_Write
static size_t FileOutStream_Write(void *pp, const void *data, size_t size)
Definition: 7zFile.cc:264
File_Close
WRes File_Close(CSzFile *p)
Definition: 7zFile.cc:77
FileOutStream_CreateVTable
void FileOutStream_CreateVTable(CFileOutStream *p)
Definition: 7zFile.cc:270
Int64
long long int Int64
Definition: Types.h:87
CFileInStream
Definition: 7zFile.h:60
AlCaHLTBitMon_ParallelJobs.p
def p
Definition: AlCaHLTBitMon_ParallelJobs.py:153
value
Definition: value.py:1
SZ_SEEK_SET
Definition: Types.h:155
SRes
int SRes
Definition: Types.h:43
CFileSeqInStream
Definition: 7zFile.h:53
OutFile_Open
WRes OutFile_Open(CSzFile *p, const char *name)
Definition: 7zFile.cc:59
res
Definition: Electron.h:6
visDQMUpload.buf
buf
Definition: visDQMUpload.py:160
NULL
#define NULL
Definition: scimark2.h:8
EcalSimRawData_cfi.writeMode
writeMode
Definition: EcalSimRawData_cfi.py:7
relativeConstraints.value
value
Definition: relativeConstraints.py:53
InFile_Open
WRes InFile_Open(CSzFile *p, const char *name)
Definition: 7zFile.cc:58
FileInStream_Seek
static SRes FileInStream_Seek(void *pp, Int64 *pos, ESzSeek origin)
Definition: 7zFile.cc:252
Skims_PA_cff.name
name
Definition: Skims_PA_cff.py:17
FileSeqInStream_CreateVTable
void FileSeqInStream_CreateVTable(CFileSeqInStream *p)
Definition: 7zFile.cc:243
data
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:79
createTree.pp
pp
Definition: createTree.py:17
CFileOutStream
Definition: 7zFile.h:67
SZ_SEEK_CUR
Definition: Types.h:155
FileInStream_CreateVTable
void FileInStream_CreateVTable(CFileInStream *p)
Definition: 7zFile.cc:257
findQualityFiles.size
size
Write out results.
Definition: findQualityFiles.py:443
CSzFile
Definition: 7zFile.h:23