7zFile.c 7.0 KB

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