summaryrefslogtreecommitdiff
path: root/base/sfxfd.c
blob: df8a358e545ece0c2d9d580fd1faf729a13ae007 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/* Copyright (C) 2001-2023 Artifex Software, Inc.
   All Rights Reserved.

   This software is provided AS-IS with no warranty, either express or
   implied.

   This software is distributed under license and may not be copied,
   modified or distributed except as expressly authorized under the terms
   of the license contained in the file LICENSE in this distribution.

   Refer to licensing information at http://www.artifex.com or contact
   Artifex Software, Inc.,  39 Mesa Street, Suite 108A, San Francisco,
   CA 94129, USA, for further information.
*/


/* File stream implementation using direct OS calls */
/******
 ****** NOTE: THIS FILE MAY NOT COMPILE ON NON-UNIX PLATFORMS, AND MAY
 ****** REQUIRE EDITING ON SOME UNIX PLATFORMS.
 ******/
#include "stdio_.h"		/* includes std.h */
#include "errno_.h"
#include "memory_.h"
#include "unistd_.h"            /* for read, write, fsync, lseek */

#include "gdebug.h"
#include "gpcheck.h"
#include "stream.h"
#include "strimpl.h"

/*
 * This is an alternate implementation of file streams.  It still uses
 * FILE * in the interface, but it uses direct OS calls for I/O.
 * It also includes workarounds for the nasty habit of System V Unix
 * of breaking out of read and write operations with EINTR, EAGAIN,
 * and/or EWOULDBLOCK "errors".
 *
 * The interface should be identical to that of sfxstdio.c.  However, in
 * order to allow both implementations to exist in the same executable, we
 * optionally use different names for sread_file, swrite_file, and
 * sappend_file, and omit sread_subfile (the public procedures).
 * See sfxboth.c.
 */
#ifdef KEEP_FILENO_API
/* Provide prototypes to avoid compiler warnings. */
void
    sread_fileno(stream *, gp_file *, byte *, uint),
    swrite_fileno(stream *, gp_file *, byte *, uint),
    sappend_fileno(stream *, gp_file *, byte *, uint);
#else
#  define sread_fileno sread_file
#  define swrite_fileno swrite_file
#  define sappend_fileno sappend_file
#endif

/* Forward references for file stream procedures */
static int
    s_fileno_available(stream *, gs_offset_t *),
    s_fileno_read_seek(stream *, gs_offset_t),
    s_fileno_read_close(stream *),
    s_fileno_read_process(stream_state *, stream_cursor_read *,
                          stream_cursor_write *, bool);
static int
    s_fileno_write_seek(stream *, long),
    s_fileno_write_flush(stream *),
    s_fileno_write_close(stream *),
    s_fileno_write_process(stream_state *, stream_cursor_read *,
                           stream_cursor_write *, bool);
static int
    s_fileno_switch(stream *, bool);

/* Get the file descriptor number of a stream. */
static inline int
sfileno(const stream *s)
{
    return fileno(s->file);
}

/* Get the current position of a file descriptor. */
static inline gs_offset_t
ltell(int fd)
{
    return lseek(fd, 0L, SEEK_CUR);
}

/* Define the System V interrupts that require retrying a call. */
static bool
errno_is_retry(int errn)
{
    switch (errn) {
#ifdef EINTR
    case EINTR: return true;
#endif
#if defined(EAGAIN) && (!defined(EINTR) || EAGAIN != EINTR)
    case EAGAIN: return true;
#endif
#if defined(EWOULDBLOCK) && (!defined(EINTR) || EWOULDBLOCK != EINTR) && (!defined(EAGAIN) || EWOULDBLOCK != EAGAIN)
    case EWOULDBLOCK: return true;
#endif
    default: return false;
    }
}

/* ------ File reading ------ */

/* Initialize a stream for reading an OS file. */
void
sread_fileno(register stream * s, gp_file * file, byte * buf, uint len)
{
    static const stream_procs p = {
        s_fileno_available, s_fileno_read_seek, s_std_read_reset,
        s_std_read_flush, s_fileno_read_close, s_fileno_read_process,
        s_fileno_switch
    };
    /*
     * There is no really portable way to test seekability,
     * but this should work on most systems.
     */
    int fd = fileno(file);
    long curpos = ltell(fd);
    bool seekable = (curpos != -1L && lseek(fd, curpos, SEEK_SET) != -1L);

    s_std_init(s, buf, len, &p,
               (seekable ? s_mode_read + s_mode_seek : s_mode_read));
    if_debug2m('s', s->memory, "[s]read file="PRI_INTPTR", fd=%d\n",
               (intptr_t) file, fileno(file));
    s->file = file;
    s->file_modes = s->modes;
    s->file_offset = 0;
    s->file_limit = S_FILE_LIMIT_MAX;
}

/* Confine reading to a subfile.  This is primarily for reusable streams. */
/*
 * We omit this procedure if we are also include sfxstdio.c, which has an
 * identical definition.
 */
#ifndef KEEP_FILENO_API
int
sread_subfile(stream *s, gs_offset_t start, gs_offset_t length)
{
    if (s->file == 0 || s->modes != s_mode_read + s_mode_seek ||
        s->file_offset != 0 || s->file_limit != S_FILE_LIMIT_MAX ||
        ((s->position < start || s->position > start + length) &&
         sseek(s, start) < 0)
        )
        return ERRC;
    s->position -= start;
    s->file_offset = start;
    s->file_limit = length;
    return 0;
}
#endif

/* Procedures for reading from a file */
static int
s_fileno_available(register stream * s, gs_offset_t *pl)
{
    gs_offset_t max_avail = s->file_limit - stell(s);
    gs_offset_t buf_avail = sbufavailable(s);
    int fd = sfileno(s);

    *pl = min(max_avail, buf_avail);
    if (sseekable(s)) {
        gs_offset_t pos, end;

        pos = ltell(fd);
        if (pos < 0)
            return ERRC;
        end = lseek(fd, 0L, SEEK_END);
        if (lseek(fd, pos, SEEK_SET) < 0 || end < 0)
            return ERRC;
        buf_avail += end - pos;
        *pl = min(max_avail, buf_avail);
        if (*pl == 0)
            *pl = -1;		/* EOF */
    } else {
        if (*pl == 0)
            *pl = -1;		/* EOF */
    }
    return 0;
}
static int
s_fileno_read_seek(register stream * s, gs_offset_t pos)
{
    gs_offset_t end = s->cursor.r.limit - s->cbuf + 1;
    gs_offset_t offset = pos - s->position;

    if (offset >= 0 && offset <= end) {  /* Staying within the same buffer */
        s->cursor.r.ptr = s->cbuf + offset - 1;
        return 0;
    }
    if (pos < 0 || pos > s->file_limit ||
        lseek(sfileno(s), s->file_offset + pos, SEEK_SET) < 0
        )
        return ERRC;
    s->cursor.r.ptr = s->cursor.r.limit = s->cbuf - 1;
    s->end_status = 0;
    s->position = pos;
    return 0;
}
static int
s_fileno_read_close(stream * s)
{
    gp_file *file = s->file;

    if (file != 0) {
        s->file = 0;
        return (fclose(file) ? ERRC : 0);
    }
    return 0;
}

/* Process a buffer for a file reading stream. */
/* This is the first stream in the pipeline, so pr is irrelevant. */
static int
s_fileno_read_process(stream_state * st, stream_cursor_read * ignore_pr,
                      stream_cursor_write * pw, bool last)
{
    stream *s = (stream *)st;	/* no separate state */
    int fd = sfileno(s);
    uint max_count;
    int nread, status;

again:
    max_count = pw->limit - pw->ptr;
    status = 1;
    if (s->file_limit < S_FILE_LIMIT_MAX) {
        gs_offset_t limit_count = s->file_offset + s->file_limit - ltell(fd);

        if (max_count > limit_count)
            max_count = limit_count, status = EOFC;
    }
    /*
     * In the Mac MetroWerks compiler, the prototype for read incorrectly
     * declares the second argument of read as char * rather than void *.
     * Work around this here.
     */
    nread = read(fd, (void *)(pw->ptr + 1), max_count);
    if (nread > 0)
        pw->ptr += nread;
    else if (nread == 0)
        status = EOFC;
    else if (errno_is_retry(errno))	/* Handle System V interrupts */
        goto again;
    else
        status = ERRC;
    process_interrupts(s->memory);
    return status;
}

/* ------ File writing ------ */

/* Initialize a stream for writing an OS file. */
void
swrite_fileno(register stream * s, gp_file * file, byte * buf, uint len)
{
    static const stream_procs p = {
        s_std_noavailable, s_fileno_write_seek, s_std_write_reset,
        s_fileno_write_flush, s_fileno_write_close, s_fileno_write_process,
        s_fileno_switch
    };

    s_std_init(s, buf, len, &p,
               (file == stdout ? s_mode_write : s_mode_write + s_mode_seek));
    if_debug2m('s', s->memory, "[s]write file="PRI_INTPTR", fd=%d\n",
               (intptr_t) file, fileno(file));
    s->file = file;
    s->file_modes = s->modes;
    s->file_offset = 0;		/* in case we switch to reading later */
    s->file_limit = S_FILE_LIMIT_MAX;
}
/* Initialize for appending to an OS file. */
void
sappend_fileno(register stream * s, gp_file * file, byte * buf, uint len)
{
    swrite_fileno(s, file, buf, len);
    s->modes = s_mode_write + s_mode_append;	/* no seek */
    s->file_modes = s->modes;
    s->position = lseek(fileno(file), 0L, SEEK_END);
}
/* Procedures for writing on a file */
static int
s_fileno_write_seek(stream * s, gs_offset_t pos)
{
    /* We must flush the buffer to reposition. */
    int code = sflush(s);

    if (code < 0)
        return code;
    if (lseek(sfileno(s), pos, SEEK_SET) < 0)
        return ERRC;
    s->position = pos;
    return 0;
}
static int
s_fileno_write_flush(register stream * s)
{
    int result = s_process_write_buf(s, false);

    discard(fsync(sfileno(s)));
    return result;
}
static int
s_fileno_write_close(register stream * s)
{
    s_process_write_buf(s, true);
    return s_fileno_read_close(s);
}

/* Process a buffer for a file writing stream. */
/* This is the last stream in the pipeline, so pw is irrelevant. */
static int
s_fileno_write_process(stream_state * st, stream_cursor_read * pr,
                       stream_cursor_write * ignore_pw, bool last)
{
    int nwrite, status;
    uint count;

again:
    count = pr->limit - pr->ptr;
    /* Some versions of the DEC C library on AXP architectures */
    /* give an error on write if the count is zero! */
    if (count == 0) {
        process_interrupts((stream*)st->memory);
        return 0;
    }
    /* See above regarding the Mac MetroWorks compiler. */
    nwrite = write(sfileno((stream *)st), (const void *)(pr->ptr + 1), count);
    if (nwrite >= 0) {
        pr->ptr += nwrite;
        status = 0;
    } else if (errno_is_retry(errno))	/* Handle System V interrupts */
        goto again;
    else
        status = ERRC;
    process_interrupts((stream *)st->memory);
    return status;
}

/* ------ File switching ------ */

/* Switch a file stream to reading or writing. */
static int
s_fileno_switch(stream * s, bool writing)
{
    uint modes = s->file_modes;
    int fd = sfileno(s);
    long pos;

    if (writing) {
        if (!(s->file_modes & s_mode_write))
            return ERRC;
        pos = stell(s);
        if_debug2m('s', s->memory, "[s]switch "PRI_INTPTR" to write at %ld\n",
                   (intptr_t)s, pos);
        lseek(fd, pos, SEEK_SET);	/* pacify OS */
        if (modes & s_mode_append) {
            sappend_file(s, s->file, s->cbuf, s->cbsize);  /* sets position */
        } else {
            swrite_file(s, s->file, s->cbuf, s->cbsize);
            s->position = pos;
        }
        s->modes = modes;
    } else {
        if (!(s->file_modes & s_mode_read))
            return ERRC;
        pos = stell(s);
        if_debug2m('s', s->memory, "[s]switch "PRI_INTPTR" to read at %ld\n",
                   (intptr_t) s, pos);
        if (sflush(s) < 0)
            return ERRC;
        lseek(fd, 0L, SEEK_CUR);	/* pacify OS */
        sread_file(s, s->file, s->cbuf, s->cbsize);
        s->modes |= modes & s_mode_append;	/* don't lose append info */
        s->position = pos;
    }
    s->file_modes = modes;
    return 0;
}