sfxfd.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /* Copyright (C) 1994, 2000 Aladdin Enterprises. All rights reserved.
  2. This file is part of AFPL Ghostscript.
  3. AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND. No author or
  4. distributor accepts any responsibility for the consequences of using it, or
  5. for whether it serves any particular purpose or works at all, unless he or
  6. she says so in writing. Refer to the Aladdin Free Public License (the
  7. "License") for full details.
  8. Every copy of AFPL Ghostscript must include a copy of the License, normally
  9. in a plain ASCII text file named PUBLIC. The License grants you the right
  10. to copy, modify and redistribute AFPL Ghostscript, but only under certain
  11. conditions described in the License. Among other things, the License
  12. requires that the copyright notice and this notice be preserved on all
  13. copies.
  14. */
  15. /*$Id: sfxfd.c,v 1.6 2000/12/19 03:35:40 alexcher Exp $ */
  16. /* File stream implementation using direct OS calls */
  17. /******
  18. ****** NOTE: THIS FILE MAY NOT COMPILE ON NON-UNIX PLATFORMS, AND MAY
  19. ****** REQUIRE EDITING ON SOME UNIX PLATFORMS.
  20. ******/
  21. #include "stdio_.h" /* includes std.h */
  22. #include "errno_.h"
  23. #include "memory_.h"
  24. #include "unistd_.h" /* for read, write, fsync, lseek */
  25. #include "gdebug.h"
  26. #include "gpcheck.h"
  27. #include "stream.h"
  28. #include "strimpl.h"
  29. /*
  30. * This is an alternate implementation of file streams. It still uses
  31. * FILE * in the interface, but it uses direct OS calls for I/O.
  32. * It also includes workarounds for the nasty habit of System V Unix
  33. * of breaking out of read and write operations with EINTR, EAGAIN,
  34. * and/or EWOULDBLOCK "errors".
  35. *
  36. * The interface should be identical to that of sfxstdio.c. However, in
  37. * order to allow both implementations to exist in the same executable, we
  38. * optionally use different names for sread_file, swrite_file, and
  39. * sappend_file, and omit sread_subfile (the public procedures).
  40. * See sfxboth.c.
  41. */
  42. #ifdef KEEP_FILENO_API
  43. /* Provide prototypes to avoid compiler warnings. */
  44. void
  45. sread_fileno(P4(stream *, FILE *, byte *, uint)),
  46. swrite_fileno(P4(stream *, FILE *, byte *, uint)),
  47. sappend_fileno(P4(stream *, FILE *, byte *, uint));
  48. #else
  49. # define sread_fileno sread_file
  50. # define swrite_fileno swrite_file
  51. # define sappend_fileno sappend_file
  52. #endif
  53. /* Forward references for file stream procedures */
  54. private int
  55. s_fileno_available(P2(stream *, long *)),
  56. s_fileno_read_seek(P2(stream *, long)),
  57. s_fileno_read_close(P1(stream *)),
  58. s_fileno_read_process(P4(stream_state *, stream_cursor_read *,
  59. stream_cursor_write *, bool));
  60. private int
  61. s_fileno_write_seek(P2(stream *, long)),
  62. s_fileno_write_flush(P1(stream *)),
  63. s_fileno_write_close(P1(stream *)),
  64. s_fileno_write_process(P4(stream_state *, stream_cursor_read *,
  65. stream_cursor_write *, bool));
  66. private int
  67. s_fileno_switch(P2(stream *, bool));
  68. /* Get the file descriptor number of a stream. */
  69. inline private int
  70. sfileno(const stream *s)
  71. {
  72. return fileno(s->file);
  73. }
  74. /* Get the current position of a file descriptor. */
  75. inline private long
  76. ltell(int fd)
  77. {
  78. return lseek(fd, 0L, SEEK_CUR);
  79. }
  80. /* Define the System V interrupts that require retrying a call. */
  81. private bool
  82. errno_is_retry(int errn)
  83. {
  84. switch (errn) {
  85. #ifdef EINTR
  86. case EINTR: return true;
  87. #endif
  88. #if defined(EAGAIN) && (!defined(EINTR) || EAGAIN != EINTR)
  89. case EAGAIN: return true;
  90. #endif
  91. #if defined(EWOULDBLOCK) && (!defined(EINTR) || EWOULDBLOCK != EINTR) && (!defined(EAGAIN) || EWOULDBLOCK != EAGAIN)
  92. case EWOULDBLOCK: return true;
  93. #endif
  94. default: return false;
  95. }
  96. }
  97. /* ------ File reading ------ */
  98. /* Initialize a stream for reading an OS file. */
  99. void
  100. sread_fileno(register stream * s, FILE * file, byte * buf, uint len)
  101. {
  102. static const stream_procs p = {
  103. s_fileno_available, s_fileno_read_seek, s_std_read_reset,
  104. s_std_read_flush, s_fileno_read_close, s_fileno_read_process,
  105. s_fileno_switch
  106. };
  107. /*
  108. * There is no really portable way to test seekability,
  109. * but this should work on most systems.
  110. */
  111. int fd = fileno(file);
  112. long curpos = ltell(fd);
  113. bool seekable = (curpos != -1L && lseek(fd, curpos, SEEK_SET) != -1L);
  114. s_std_init(s, buf, len, &p,
  115. (seekable ? s_mode_read + s_mode_seek : s_mode_read));
  116. if_debug2('s', "[s]read file=0x%lx, fd=%d\n", (ulong) file,
  117. fileno(file));
  118. s->file = file;
  119. s->file_modes = s->modes;
  120. s->file_offset = 0;
  121. s->file_limit = max_long;
  122. }
  123. /* Confine reading to a subfile. This is primarily for reusable streams. */
  124. /*
  125. * We omit this procedure if we are also include sfxstdio.c, which has an
  126. * identical definition.
  127. */
  128. #ifndef KEEP_FILENO_API
  129. int
  130. sread_subfile(stream *s, long start, long length)
  131. {
  132. if (s->file == 0 || s->modes != s_mode_read + s_mode_seek ||
  133. s->file_offset != 0 || s->file_limit != max_long ||
  134. ((s->position < start || s->position > start + length) &&
  135. sseek(s, start) < 0)
  136. )
  137. return ERRC;
  138. s->position -= start;
  139. s->file_offset = start;
  140. s->file_limit = length;
  141. return 0;
  142. }
  143. #endif
  144. /* Procedures for reading from a file */
  145. private int
  146. s_fileno_available(register stream * s, long *pl)
  147. {
  148. long max_avail = s->file_limit - stell(s);
  149. long buf_avail = sbufavailable(s);
  150. int fd = sfileno(s);
  151. *pl = min(max_avail, buf_avail);
  152. if (sseekable(s)) {
  153. long pos, end;
  154. pos = ltell(fd);
  155. if (pos < 0)
  156. return ERRC;
  157. end = lseek(fd, 0L, SEEK_END);
  158. if (lseek(fd, pos, SEEK_SET) < 0 || end < 0)
  159. return ERRC;
  160. buf_avail += end - pos;
  161. *pl = min(max_avail, buf_avail);
  162. if (*pl == 0)
  163. *pl = -1; /* EOF */
  164. } else {
  165. if (*pl == 0)
  166. *pl = -1; /* EOF */
  167. }
  168. return 0;
  169. }
  170. private int
  171. s_fileno_read_seek(register stream * s, long pos)
  172. {
  173. uint end = s->srlimit - s->cbuf + 1;
  174. long offset = pos - s->position;
  175. if (offset >= 0 && offset <= end) { /* Staying within the same buffer */
  176. s->srptr = s->cbuf + offset - 1;
  177. return 0;
  178. }
  179. if (pos < 0 || pos > s->file_limit ||
  180. lseek(sfileno(s), s->file_offset + pos, SEEK_SET) < 0
  181. )
  182. return ERRC;
  183. s->srptr = s->srlimit = s->cbuf - 1;
  184. s->end_status = 0;
  185. s->position = pos;
  186. return 0;
  187. }
  188. private int
  189. s_fileno_read_close(stream * s)
  190. {
  191. FILE *file = s->file;
  192. if (file != 0) {
  193. s->file = 0;
  194. return (fclose(file) ? ERRC : 0);
  195. }
  196. return 0;
  197. }
  198. /* Process a buffer for a file reading stream. */
  199. /* This is the first stream in the pipeline, so pr is irrelevant. */
  200. private int
  201. s_fileno_read_process(stream_state * st, stream_cursor_read * ignore_pr,
  202. stream_cursor_write * pw, bool last)
  203. {
  204. stream *s = (stream *)st; /* no separate state */
  205. int fd = sfileno(s);
  206. uint max_count;
  207. int nread, status;
  208. again:
  209. max_count = pw->limit - pw->ptr;
  210. status = 1;
  211. if (s->file_limit < max_long) {
  212. long limit_count = s->file_offset + s->file_limit - ltell(fd);
  213. if (max_count > limit_count)
  214. max_count = limit_count, status = EOFC;
  215. }
  216. /*
  217. * In the Mac MetroWerks compiler, the prototype for read incorrectly
  218. * declares the second argument of read as char * rather than void *.
  219. * Work around this here.
  220. */
  221. nread = read(fd, (void *)(pw->ptr + 1), max_count);
  222. if (nread > 0)
  223. pw->ptr += nread;
  224. else if (nread == 0)
  225. status = EOFC;
  226. else if (errno_is_retry(errno)) /* Handle System V interrupts */
  227. goto again;
  228. else
  229. status = ERRC;
  230. process_interrupts();
  231. return status;
  232. }
  233. /* ------ File writing ------ */
  234. /* Initialize a stream for writing an OS file. */
  235. void
  236. swrite_fileno(register stream * s, FILE * file, byte * buf, uint len)
  237. {
  238. static const stream_procs p = {
  239. s_std_noavailable, s_fileno_write_seek, s_std_write_reset,
  240. s_fileno_write_flush, s_fileno_write_close, s_fileno_write_process,
  241. s_fileno_switch
  242. };
  243. s_std_init(s, buf, len, &p,
  244. (file == stdout ? s_mode_write : s_mode_write + s_mode_seek));
  245. if_debug2('s', "[s]write file=0x%lx, fd=%d\n", (ulong) file,
  246. fileno(file));
  247. s->file = file;
  248. s->file_modes = s->modes;
  249. s->file_offset = 0; /* in case we switch to reading later */
  250. s->file_limit = max_long; /* ibid. */
  251. }
  252. /* Initialize for appending to an OS file. */
  253. void
  254. sappend_fileno(register stream * s, FILE * file, byte * buf, uint len)
  255. {
  256. swrite_fileno(s, file, buf, len);
  257. s->modes = s_mode_write + s_mode_append; /* no seek */
  258. s->file_modes = s->modes;
  259. s->position = lseek(fileno(file), 0L, SEEK_END);
  260. }
  261. /* Procedures for writing on a file */
  262. private int
  263. s_fileno_write_seek(stream * s, long pos)
  264. {
  265. /* We must flush the buffer to reposition. */
  266. int code = sflush(s);
  267. if (code < 0)
  268. return code;
  269. if (lseek(sfileno(s), pos, SEEK_SET) < 0)
  270. return ERRC;
  271. s->position = pos;
  272. return 0;
  273. }
  274. private int
  275. s_fileno_write_flush(register stream * s)
  276. {
  277. int result = s_process_write_buf(s, false);
  278. discard(fsync(sfileno(s)));
  279. return result;
  280. }
  281. private int
  282. s_fileno_write_close(register stream * s)
  283. {
  284. s_process_write_buf(s, true);
  285. return s_fileno_read_close(s);
  286. }
  287. /* Process a buffer for a file writing stream. */
  288. /* This is the last stream in the pipeline, so pw is irrelevant. */
  289. private int
  290. s_fileno_write_process(stream_state * st, stream_cursor_read * pr,
  291. stream_cursor_write * ignore_pw, bool last)
  292. {
  293. int nwrite, status;
  294. uint count;
  295. again:
  296. count = pr->limit - pr->ptr;
  297. /* Some versions of the DEC C library on AXP architectures */
  298. /* give an error on write if the count is zero! */
  299. if (count == 0) {
  300. process_interrupts();
  301. return 0;
  302. }
  303. /* See above regarding the Mac MetroWorks compiler. */
  304. nwrite = write(sfileno((stream *)st), (const void *)(pr->ptr + 1), count);
  305. if (nwrite >= 0) {
  306. pr->ptr += nwrite;
  307. status = 0;
  308. } else if (errno_is_retry(errno)) /* Handle System V interrupts */
  309. goto again;
  310. else
  311. status = ERRC;
  312. process_interrupts();
  313. return status;
  314. }
  315. /* ------ File switching ------ */
  316. /* Switch a file stream to reading or writing. */
  317. private int
  318. s_fileno_switch(stream * s, bool writing)
  319. {
  320. uint modes = s->file_modes;
  321. int fd = sfileno(s);
  322. long pos;
  323. if (writing) {
  324. if (!(s->file_modes & s_mode_write))
  325. return ERRC;
  326. pos = stell(s);
  327. if_debug2('s', "[s]switch 0x%lx to write at %ld\n",
  328. (ulong) s, pos);
  329. lseek(fd, pos, SEEK_SET); /* pacify OS */
  330. if (modes & s_mode_append) {
  331. sappend_file(s, s->file, s->cbuf, s->cbsize); /* sets position */
  332. } else {
  333. swrite_file(s, s->file, s->cbuf, s->cbsize);
  334. s->position = pos;
  335. }
  336. s->modes = modes;
  337. } else {
  338. if (!(s->file_modes & s_mode_read))
  339. return ERRC;
  340. pos = stell(s);
  341. if_debug2('s', "[s]switch 0x%lx to read at %ld\n",
  342. (ulong) s, pos);
  343. if (sflush(s) < 0)
  344. return ERRC;
  345. lseek(fd, 0L, SEEK_CUR); /* pacify OS */
  346. sread_file(s, s->file, s->cbuf, s->cbsize);
  347. s->modes |= modes & s_mode_append; /* don't lose append info */
  348. s->position = pos;
  349. }
  350. s->file_modes = modes;
  351. return 0;
  352. }