7zFile.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. /* 7zFile.c -- File IO
  2. 2023-04-02 : Igor Pavlov : Public domain */
  3. #include "Precomp.h"
  4. #include "7zFile.h"
  5. #ifndef USE_WINDOWS_FILE
  6. #include <errno.h>
  7. #ifndef USE_FOPEN
  8. #include <stdio.h>
  9. #include <fcntl.h>
  10. #ifdef _WIN32
  11. #include <io.h>
  12. typedef int ssize_t;
  13. typedef int off_t;
  14. #else
  15. #include <unistd.h>
  16. #endif
  17. #endif
  18. #else
  19. /*
  20. ReadFile and WriteFile functions in Windows have BUG:
  21. If you Read or Write 64MB or more (probably min_failure_size = 64MB - 32KB + 1)
  22. from/to Network file, it returns ERROR_NO_SYSTEM_RESOURCES
  23. (Insufficient system resources exist to complete the requested service).
  24. Probably in some version of Windows there are problems with other sizes:
  25. for 32 MB (maybe also for 16 MB).
  26. And message can be "Network connection was lost"
  27. */
  28. #endif
  29. #define kChunkSizeMax (1 << 22)
  30. void File_Construct(CSzFile *p)
  31. {
  32. #ifdef USE_WINDOWS_FILE
  33. p->handle = INVALID_HANDLE_VALUE;
  34. #elif defined(USE_FOPEN)
  35. p->file = NULL;
  36. #else
  37. p->fd = -1;
  38. #endif
  39. }
  40. #if !defined(UNDER_CE) || !defined(USE_WINDOWS_FILE)
  41. static WRes File_Open(CSzFile *p, const char *name, int writeMode)
  42. {
  43. #ifdef USE_WINDOWS_FILE
  44. p->handle = CreateFileA(name,
  45. writeMode ? GENERIC_WRITE : GENERIC_READ,
  46. FILE_SHARE_READ, NULL,
  47. writeMode ? CREATE_ALWAYS : OPEN_EXISTING,
  48. FILE_ATTRIBUTE_NORMAL, NULL);
  49. return (p->handle != INVALID_HANDLE_VALUE) ? 0 : GetLastError();
  50. #elif defined(USE_FOPEN)
  51. p->file = fopen(name, writeMode ? "wb+" : "rb");
  52. return (p->file != 0) ? 0 :
  53. #ifdef UNDER_CE
  54. 2; /* ENOENT */
  55. #else
  56. errno;
  57. #endif
  58. #else
  59. int flags = (writeMode ? (O_CREAT | O_EXCL | O_WRONLY) : O_RDONLY);
  60. #ifdef O_BINARY
  61. flags |= O_BINARY;
  62. #endif
  63. p->fd = open(name, flags, 0666);
  64. return (p->fd != -1) ? 0 : errno;
  65. #endif
  66. }
  67. WRes InFile_Open(CSzFile *p, const char *name) { return File_Open(p, name, 0); }
  68. WRes OutFile_Open(CSzFile *p, const char *name)
  69. {
  70. #if defined(USE_WINDOWS_FILE) || defined(USE_FOPEN)
  71. return File_Open(p, name, 1);
  72. #else
  73. p->fd = creat(name, 0666);
  74. return (p->fd != -1) ? 0 : errno;
  75. #endif
  76. }
  77. #endif
  78. #ifdef USE_WINDOWS_FILE
  79. static WRes File_OpenW(CSzFile *p, const WCHAR *name, int writeMode)
  80. {
  81. p->handle = CreateFileW(name,
  82. writeMode ? GENERIC_WRITE : GENERIC_READ,
  83. FILE_SHARE_READ, NULL,
  84. writeMode ? CREATE_ALWAYS : OPEN_EXISTING,
  85. FILE_ATTRIBUTE_NORMAL, NULL);
  86. return (p->handle != INVALID_HANDLE_VALUE) ? 0 : GetLastError();
  87. }
  88. WRes InFile_OpenW(CSzFile *p, const WCHAR *name) { return File_OpenW(p, name, 0); }
  89. WRes OutFile_OpenW(CSzFile *p, const WCHAR *name) { return File_OpenW(p, name, 1); }
  90. #endif
  91. WRes File_Close(CSzFile *p)
  92. {
  93. #ifdef USE_WINDOWS_FILE
  94. if (p->handle != INVALID_HANDLE_VALUE)
  95. {
  96. if (!CloseHandle(p->handle))
  97. return GetLastError();
  98. p->handle = INVALID_HANDLE_VALUE;
  99. }
  100. #elif defined(USE_FOPEN)
  101. if (p->file != NULL)
  102. {
  103. int res = fclose(p->file);
  104. if (res != 0)
  105. {
  106. if (res == EOF)
  107. return errno;
  108. return res;
  109. }
  110. p->file = NULL;
  111. }
  112. #else
  113. if (p->fd != -1)
  114. {
  115. if (close(p->fd) != 0)
  116. return errno;
  117. p->fd = -1;
  118. }
  119. #endif
  120. return 0;
  121. }
  122. WRes File_Read(CSzFile *p, void *data, size_t *size)
  123. {
  124. size_t originalSize = *size;
  125. *size = 0;
  126. if (originalSize == 0)
  127. return 0;
  128. #ifdef USE_WINDOWS_FILE
  129. do
  130. {
  131. const DWORD curSize = (originalSize > kChunkSizeMax) ? kChunkSizeMax : (DWORD)originalSize;
  132. DWORD processed = 0;
  133. const BOOL res = ReadFile(p->handle, data, curSize, &processed, NULL);
  134. data = (void *)((Byte *)data + processed);
  135. originalSize -= processed;
  136. *size += processed;
  137. if (!res)
  138. return GetLastError();
  139. // debug : we can break here for partial reading mode
  140. if (processed == 0)
  141. break;
  142. }
  143. while (originalSize > 0);
  144. #elif defined(USE_FOPEN)
  145. do
  146. {
  147. const size_t curSize = (originalSize > kChunkSizeMax) ? kChunkSizeMax : originalSize;
  148. const size_t processed = fread(data, 1, curSize, p->file);
  149. data = (void *)((Byte *)data + (size_t)processed);
  150. originalSize -= processed;
  151. *size += processed;
  152. if (processed != curSize)
  153. return ferror(p->file);
  154. // debug : we can break here for partial reading mode
  155. if (processed == 0)
  156. break;
  157. }
  158. while (originalSize > 0);
  159. #else
  160. do
  161. {
  162. const size_t curSize = (originalSize > kChunkSizeMax) ? kChunkSizeMax : originalSize;
  163. const ssize_t processed = read(p->fd, data, curSize);
  164. if (processed == -1)
  165. return errno;
  166. if (processed == 0)
  167. break;
  168. data = (void *)((Byte *)data + (size_t)processed);
  169. originalSize -= (size_t)processed;
  170. *size += (size_t)processed;
  171. // debug : we can break here for partial reading mode
  172. // break;
  173. }
  174. while (originalSize > 0);
  175. #endif
  176. return 0;
  177. }
  178. WRes File_Write(CSzFile *p, const void *data, size_t *size)
  179. {
  180. size_t originalSize = *size;
  181. *size = 0;
  182. if (originalSize == 0)
  183. return 0;
  184. #ifdef USE_WINDOWS_FILE
  185. do
  186. {
  187. const DWORD curSize = (originalSize > kChunkSizeMax) ? kChunkSizeMax : (DWORD)originalSize;
  188. DWORD processed = 0;
  189. const BOOL res = WriteFile(p->handle, data, curSize, &processed, NULL);
  190. data = (const void *)((const Byte *)data + processed);
  191. originalSize -= processed;
  192. *size += processed;
  193. if (!res)
  194. return GetLastError();
  195. if (processed == 0)
  196. break;
  197. }
  198. while (originalSize > 0);
  199. #elif defined(USE_FOPEN)
  200. do
  201. {
  202. const size_t curSize = (originalSize > kChunkSizeMax) ? kChunkSizeMax : originalSize;
  203. const size_t processed = fwrite(data, 1, curSize, p->file);
  204. data = (void *)((Byte *)data + (size_t)processed);
  205. originalSize -= processed;
  206. *size += processed;
  207. if (processed != curSize)
  208. return ferror(p->file);
  209. if (processed == 0)
  210. break;
  211. }
  212. while (originalSize > 0);
  213. #else
  214. do
  215. {
  216. const size_t curSize = (originalSize > kChunkSizeMax) ? kChunkSizeMax : originalSize;
  217. const ssize_t processed = write(p->fd, data, curSize);
  218. if (processed == -1)
  219. return errno;
  220. if (processed == 0)
  221. break;
  222. data = (const void *)((const Byte *)data + (size_t)processed);
  223. originalSize -= (size_t)processed;
  224. *size += (size_t)processed;
  225. }
  226. while (originalSize > 0);
  227. #endif
  228. return 0;
  229. }
  230. WRes File_Seek(CSzFile *p, Int64 *pos, ESzSeek origin)
  231. {
  232. #ifdef USE_WINDOWS_FILE
  233. DWORD moveMethod;
  234. UInt32 low = (UInt32)*pos;
  235. LONG high = (LONG)((UInt64)*pos >> 16 >> 16); /* for case when UInt64 is 32-bit only */
  236. // (int) to eliminate clang warning
  237. switch ((int)origin)
  238. {
  239. case SZ_SEEK_SET: moveMethod = FILE_BEGIN; break;
  240. case SZ_SEEK_CUR: moveMethod = FILE_CURRENT; break;
  241. case SZ_SEEK_END: moveMethod = FILE_END; break;
  242. default: return ERROR_INVALID_PARAMETER;
  243. }
  244. low = SetFilePointer(p->handle, (LONG)low, &high, moveMethod);
  245. if (low == (UInt32)0xFFFFFFFF)
  246. {
  247. WRes res = GetLastError();
  248. if (res != NO_ERROR)
  249. return res;
  250. }
  251. *pos = ((Int64)high << 32) | low;
  252. return 0;
  253. #else
  254. int moveMethod; // = origin;
  255. switch ((int)origin)
  256. {
  257. case SZ_SEEK_SET: moveMethod = SEEK_SET; break;
  258. case SZ_SEEK_CUR: moveMethod = SEEK_CUR; break;
  259. case SZ_SEEK_END: moveMethod = SEEK_END; break;
  260. default: return EINVAL;
  261. }
  262. #if defined(USE_FOPEN)
  263. {
  264. int res = fseek(p->file, (long)*pos, moveMethod);
  265. if (res == -1)
  266. return errno;
  267. *pos = ftell(p->file);
  268. if (*pos == -1)
  269. return errno;
  270. return 0;
  271. }
  272. #else
  273. {
  274. off_t res = lseek(p->fd, (off_t)*pos, moveMethod);
  275. if (res == -1)
  276. return errno;
  277. *pos = res;
  278. return 0;
  279. }
  280. #endif // USE_FOPEN
  281. #endif // USE_WINDOWS_FILE
  282. }
  283. WRes File_GetLength(CSzFile *p, UInt64 *length)
  284. {
  285. #ifdef USE_WINDOWS_FILE
  286. DWORD sizeHigh;
  287. DWORD sizeLow = GetFileSize(p->handle, &sizeHigh);
  288. if (sizeLow == 0xFFFFFFFF)
  289. {
  290. DWORD res = GetLastError();
  291. if (res != NO_ERROR)
  292. return res;
  293. }
  294. *length = (((UInt64)sizeHigh) << 32) + sizeLow;
  295. return 0;
  296. #elif defined(USE_FOPEN)
  297. long pos = ftell(p->file);
  298. int res = fseek(p->file, 0, SEEK_END);
  299. *length = ftell(p->file);
  300. fseek(p->file, pos, SEEK_SET);
  301. return res;
  302. #else
  303. off_t pos;
  304. *length = 0;
  305. pos = lseek(p->fd, 0, SEEK_CUR);
  306. if (pos != -1)
  307. {
  308. const off_t len2 = lseek(p->fd, 0, SEEK_END);
  309. const off_t res2 = lseek(p->fd, pos, SEEK_SET);
  310. if (len2 != -1)
  311. {
  312. *length = (UInt64)len2;
  313. if (res2 != -1)
  314. return 0;
  315. }
  316. }
  317. return errno;
  318. #endif
  319. }
  320. /* ---------- FileSeqInStream ---------- */
  321. static SRes FileSeqInStream_Read(ISeqInStreamPtr pp, void *buf, size_t *size)
  322. {
  323. Z7_CONTAINER_FROM_VTBL_TO_DECL_VAR_pp_vt_p(CFileSeqInStream)
  324. const WRes wres = File_Read(&p->file, buf, size);
  325. p->wres = wres;
  326. return (wres == 0) ? SZ_OK : SZ_ERROR_READ;
  327. }
  328. void FileSeqInStream_CreateVTable(CFileSeqInStream *p)
  329. {
  330. p->vt.Read = FileSeqInStream_Read;
  331. }
  332. /* ---------- FileInStream ---------- */
  333. static SRes FileInStream_Read(ISeekInStreamPtr pp, void *buf, size_t *size)
  334. {
  335. Z7_CONTAINER_FROM_VTBL_TO_DECL_VAR_pp_vt_p(CFileInStream)
  336. const WRes wres = File_Read(&p->file, buf, size);
  337. p->wres = wres;
  338. return (wres == 0) ? SZ_OK : SZ_ERROR_READ;
  339. }
  340. static SRes FileInStream_Seek(ISeekInStreamPtr pp, Int64 *pos, ESzSeek origin)
  341. {
  342. Z7_CONTAINER_FROM_VTBL_TO_DECL_VAR_pp_vt_p(CFileInStream)
  343. const WRes wres = File_Seek(&p->file, pos, origin);
  344. p->wres = wres;
  345. return (wres == 0) ? SZ_OK : SZ_ERROR_READ;
  346. }
  347. void FileInStream_CreateVTable(CFileInStream *p)
  348. {
  349. p->vt.Read = FileInStream_Read;
  350. p->vt.Seek = FileInStream_Seek;
  351. }
  352. /* ---------- FileOutStream ---------- */
  353. static size_t FileOutStream_Write(ISeqOutStreamPtr pp, const void *data, size_t size)
  354. {
  355. Z7_CONTAINER_FROM_VTBL_TO_DECL_VAR_pp_vt_p(CFileOutStream)
  356. const WRes wres = File_Write(&p->file, data, &size);
  357. p->wres = wres;
  358. return size;
  359. }
  360. void FileOutStream_CreateVTable(CFileOutStream *p)
  361. {
  362. p->vt.Write = FileOutStream_Write;
  363. }