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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
|
/* Copyright (C) 2001-2019 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., 1305 Grant Avenue - Suite 200, Novato,
CA 94945, U.S.A., +1(415)492-9861, for further information.
*/
/* %ram% file device implementation */
/*
* This file implements a simple ram-based file system.
*
* The fs has no subdirs, only a root directory. Files are stored as a
* resizable array of pointers to blocks. Timestamps are not implemented
* for performance reasons.
*
* The implementation is in two parts - a ramfs interface that works
* mostly like the unix file system calls, and a layer to hook this up
* to ghostscript.
*
* Macros define an upper limit on the number of blocks a ram device
* can take up, and (in ramfs.c) the size of each block.
*
* Routines for the gs stream interface were graciously stolen from
* sfxstdio.c et al.
*/
#include "string_.h"
#include "unistd_.h"
#include "gx.h"
#include "gserrors.h"
#include "gp.h"
#include "gscdefs.h"
#include "gsparam.h"
#include "gsstruct.h"
#include "gxiodev.h"
#include "gsutil.h"
#include "stream.h"
#include "ramfs.h"
/* Function prototypes */
static iodev_proc_init(iodev_ram_init);
static iodev_proc_finit(iodev_ram_finit);
static iodev_proc_open_file(ram_open_file);
static iodev_proc_delete_file(ram_delete);
static iodev_proc_rename_file(ram_rename);
static iodev_proc_file_status(ram_status);
static iodev_proc_enumerate_files(ram_enumerate_init);
static iodev_proc_enumerate_next(ram_enumerate_next);
static iodev_proc_enumerate_close(ram_enumerate_close);
static iodev_proc_get_params(ram_get_params);
static void ram_finalize(const gs_memory_t *memory, void * vptr);
const gx_io_device gs_iodev_ram = {
"%ram%", "FileSystem", {
iodev_ram_init, iodev_ram_finit, iodev_no_open_device,
ram_open_file, iodev_no_fopen, iodev_no_fclose,
ram_delete, ram_rename, ram_status,
ram_enumerate_init, ram_enumerate_next, ram_enumerate_close,
ram_get_params, iodev_no_put_params
},
NULL,
NULL
};
typedef struct ramfs_state_s {
gs_memory_t *memory;
ramfs* fs;
} ramfs_state;
#define GETRAMFS(state) (((ramfs_state*)(state))->fs)
gs_private_st_simple_final(st_ramfs_state, struct ramfs_state_s, "ramfs_state", ram_finalize);
typedef struct gsram_enum_s {
char *pattern;
ramfs_enum* e;
gs_memory_t *memory;
} gsram_enum;
gs_private_st_ptrs3(st_gsram_enum, struct gsram_enum_s, "gsram_enum",
gsram_enum_enum_ptrs, gsram_enum_reloc_ptrs, pattern, e, memory);
/* could make this runtime configurable later. It doesn't allocate
all the blocks in one go so it's not critical */
#define MAXBLOCKS 2000000
#define DEFAULT_BUFFER_SIZE 2048
/* stream stuff */
static int
s_ram_available(stream *, gs_offset_t *),
s_ram_read_seek(stream *, gs_offset_t),
s_ram_read_close(stream *),
s_ram_read_process(stream_state *, stream_cursor_read *,
stream_cursor_write *, bool);
static int
s_ram_write_seek(stream *, gs_offset_t),
s_ram_write_flush(stream *),
s_ram_write_close(stream *),
s_ram_write_process(stream_state *, stream_cursor_read *,
stream_cursor_write *, bool);
static int
s_ram_switch(stream *, bool);
static int
ramfs_errno_to_code(int error_number) {
switch (error_number) {
case RAMFS_NOTFOUND:
return_error(gs_error_undefinedfilename);
case RAMFS_NOACCESS:
return_error(gs_error_invalidfileaccess);
case RAMFS_NOMEM:
return_error(gs_error_VMerror);
/* just in case */
default:
return_error(gs_error_ioerror);
}
}
static void
sread_ram(register stream * s, ramhandle * file, byte * buf, uint len),
swrite_ram(register stream * s, ramhandle * file, byte * buf, uint len),
sappend_ram(register stream * s, ramhandle * file, byte * buf, uint len);
static int
ram_open_file(gx_io_device * iodev, const char *fname, uint len,
const char *file_access, stream ** ps, gs_memory_t * mem)
{
int code = 0;
ramhandle * file;
char fmode[4]; /* r/w/a, [+], [b], null */
int openmode=RAMFS_READ;
ramfs * fs;
char * namestr = NULL;
/* Is there a more efficient way to do this? */
namestr = (char *)gs_alloc_bytes(mem, len + 1, "temporary filename string");
if(!namestr)
return_error(gs_error_VMerror);
strncpy(namestr,fname,len);
namestr[len] = 0;
if (!iodev) {/*iodev = iodev_default;*/
gs_free_object(mem, namestr, "free temporary filename string");
return gs_note_error(gs_error_invalidaccess);
}
fs = GETRAMFS(iodev->state);
code = file_prepare_stream(fname, len, file_access, DEFAULT_BUFFER_SIZE,
ps, fmode, mem
);
if (code < 0) goto error;
if (fname == 0) {
gs_free_object(mem, namestr, "free temporary filename string");
return 0;
}
switch (fmode[0]) {
case 'a':
openmode = RAMFS_WRITE | RAMFS_APPEND;
break;
case 'r':
openmode = RAMFS_READ;
if (fmode[1] == '+')
openmode |= RAMFS_WRITE;
break;
case 'w':
openmode |= RAMFS_WRITE | RAMFS_TRUNC | RAMFS_CREATE;
if (fmode[1] == '+')
openmode |= RAMFS_READ;
}
/* For now, we cheat here in the same way that sfxstdio.c et al cheat -
append mode is faked by opening in write mode and seeking to EOF just
once. This is different from unix semantics, which seeks atomically
before each write, and is actually useful as a distinct mode. */
/* if (fmode[0] == 'a') openmode |= RAMFS_APPEND; */
file = ramfs_open(mem, fs,namestr,openmode);
if(!file) { code = ramfs_errno_to_code(ramfs_error(fs)); goto error; }
switch (fmode[0]) {
case 'a':
sappend_ram(*ps, file, (*ps)->cbuf, (*ps)->bsize);
break;
case 'r':
sread_ram(*ps, file, (*ps)->cbuf, (*ps)->bsize);
break;
case 'w':
swrite_ram(*ps, file, (*ps)->cbuf, (*ps)->bsize);
}
if (fmode[1] == '+') {
(*ps)->modes = (*ps)->file_modes |= s_mode_read | s_mode_write;
}
(*ps)->save_close = (*ps)->procs.close;
(*ps)->procs.close = file_close_file;
error:
gs_free_object(mem, namestr, "free temporary filename string");
/* XXX free stream stuff? */
return code;
}
/* Initialize a stream for reading an OS file. */
static void
sread_ram(register stream * s, ramhandle * file, byte * buf, uint len)
{
static const stream_procs p = {
s_ram_available, s_ram_read_seek, s_std_read_reset,
s_std_read_flush, s_ram_read_close, s_ram_read_process,
s_ram_switch
};
s_std_init(s, buf, len, &p,s_mode_read + s_mode_seek);
s->file = (gp_file *)file;
s->file_modes = s->modes;
s->file_offset = 0;
ramfile_seek(file, 0, RAMFS_SEEK_END);
s->file_limit = ramfile_tell(file);
ramfile_seek(file, 0, RAMFS_SEEK_SET);
}
/* Procedures for reading from a file */
static int
s_ram_available(register stream * s, gs_offset_t *pl)
{
long max_avail = s->file_limit - stell(s);
*pl = max_avail;
if(*pl == 0 && ramfile_eof((ramhandle*)s->file))
*pl = -1; /* EOF */
return 0;
}
static int
s_ram_read_seek(register stream * s, gs_offset_t pos)
{
uint end = s->cursor.r.limit - s->cbuf + 1;
long 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 ||
ramfile_seek((ramhandle*)s->file, s->file_offset + pos, RAMFS_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_ram_read_close(stream * s)
{
ramhandle *file = (ramhandle*)s->file;
if (file != 0) {
s->file = 0;
ramfile_close(file);
}
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_ram_read_process(stream_state * st, stream_cursor_read * ignore_pr,
stream_cursor_write * pw, bool last)
{
stream *s = (stream *)st; /* no separate state */
ramhandle *file = (ramhandle*)s->file;
uint max_count = pw->limit - pw->ptr;
int status = 1;
int count;
if (s->file_limit < S_FILE_LIMIT_MAX) {
long limit_count = s->file_offset + s->file_limit -
ramfile_tell(file);
if (max_count > limit_count)
max_count = limit_count, status = EOFC;
}
count = ramfile_read(file,pw->ptr + 1, max_count);
if (count < 0) return ERRC;
pw->ptr += count;
/* process_interrupts(s->memory); */
return ramfile_eof(file) ? EOFC : status;
}
/* ------ File writing ------ */
/* Initialize a stream for writing a file. */
static void
swrite_ram(register stream * s, ramhandle * file, byte * buf, uint len)
{
static const stream_procs p = {
s_std_noavailable, s_ram_write_seek, s_std_write_reset,
s_ram_write_flush, s_ram_write_close, s_ram_write_process,
s_ram_switch
};
s_std_init(s, buf, len, &p, s_mode_write + s_mode_seek);
s->file = (gp_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 a file. */
static void
sappend_ram(register stream * s, ramhandle * file, byte * buf, uint len)
{
swrite_ram(s, file, buf, len);
s->modes = s_mode_write + s_mode_append; /* no seek */
s->file_modes = s->modes;
ramfile_seek(file,0,RAMFS_SEEK_END);
s->position = ramfile_tell(file);
}
/* Procedures for writing on a file */
static int
s_ram_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 (ramfile_seek((ramhandle*)s->file, pos, RAMFS_SEEK_SET) != 0)
return ERRC;
s->position = pos;
return 0;
}
static int
s_ram_write_flush(register stream * s)
{
int result = s_process_write_buf(s, false);
return result;
}
static int
s_ram_write_close(register stream * s)
{
s_process_write_buf(s, true);
return s_ram_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_ram_write_process(stream_state * st, stream_cursor_read * pr,
stream_cursor_write * ignore_pw, bool last)
{
uint count = pr->limit - pr->ptr;
ramhandle *file = (ramhandle*)((stream *) st)->file;
int written = ramfile_write(file,pr->ptr + 1, count);
if (written < 0) return ERRC;
pr->ptr += written;
return 0;
}
static int
s_ram_switch(stream * s, bool writing)
{
uint modes = s->file_modes;
ramhandle *file = (ramhandle*)s->file;
long pos;
if (writing) {
if (!(s->file_modes & s_mode_write)) return ERRC;
pos = stell(s);
ramfile_seek(file, pos, RAMFS_SEEK_SET);
if (modes & s_mode_append) {
sappend_ram(s, file, s->cbuf, s->cbsize); /* sets position */
} else {
swrite_ram(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 (sflush(s) < 0) return ERRC;
sread_ram(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;
}
/* gx_io_device stuff */
static int
iodev_ram_init(gx_io_device * iodev, gs_memory_t * mem)
{
ramfs* fs = ramfs_new(mem, MAXBLOCKS);
ramfs_state* state = gs_alloc_struct(mem, ramfs_state, &st_ramfs_state,
"ramfs_init(state)"
);
if (fs && state) {
state->fs = fs;
state->memory = mem;
iodev->state = state;
return 0;
}
if(fs) ramfs_destroy(mem, fs);
if(state) gs_free_object(mem,state,"iodev_ram_init(state)");
return_error(gs_error_VMerror);
}
static void
iodev_ram_finit(gx_io_device * iodev, gs_memory_t * mem)
{
ramfs_state *state = (ramfs_state *)iodev->state;
if (state != NULL)
{
iodev->state = NULL;
gs_free_object(state->memory, state, "iodev_ram_finit");
}
return;
}
static void
ram_finalize(const gs_memory_t *memory, void * vptr)
{
ramfs* fs = GETRAMFS((ramfs_state*)vptr);
ramfs_destroy((gs_memory_t *)memory, fs);
GETRAMFS((ramfs_state*)vptr) = NULL;
}
static int
ram_delete(gx_io_device * iodev, const char *fname)
{
ramfs* fs = GETRAMFS(iodev->state);
if(ramfs_unlink(fs,fname)!=0) {
return_error(ramfs_errno_to_code(ramfs_error(fs)));
}
return 0;
}
static int
ram_rename(gx_io_device * iodev, const char *from, const char *to)
{
ramfs* fs = GETRAMFS(iodev->state);
if(ramfs_rename(fs,from,to)!=0) {
return_error(ramfs_errno_to_code(ramfs_error(fs)));
}
return 0;
}
static int
ram_status(gx_io_device * iodev, const char *fname, struct stat *pstat)
{
ramhandle * f;
ramfs* fs = GETRAMFS(iodev->state);
f = ramfs_open(((ramfs_state *)iodev->state)->memory, fs,fname,RAMFS_READ);
if(!f) return_error(ramfs_errno_to_code(ramfs_error(fs)));
memset(pstat, 0, sizeof(*pstat));
pstat->st_size = ramfile_size(f);
/* The Windows definition of struct stat doesn't include a st_blocks member
pstat->st_blocks = (pstat->st_size+blocksize-1)/blocksize;*/
/* XXX set mtime & ctime */
ramfile_close(f);
return 0;
}
static file_enum *
ram_enumerate_init(gs_memory_t * mem, gx_io_device *iodev, const char *pat,
uint patlen)
{
gsram_enum * penum = gs_alloc_struct(
mem, gsram_enum, &st_gsram_enum,
"ram_enumerate_files_init(file_enum)"
);
char *pattern = (char *)gs_alloc_bytes(
mem, patlen+1, "ram_enumerate_file_init(pattern)"
);
ramfs_enum * e = ramfs_enum_new(GETRAMFS(iodev->state));
if(penum && pattern && e) {
memcpy(pattern, pat, patlen);
pattern[patlen]=0;
penum->memory = mem;
penum->pattern = pattern;
penum->e = e;
return (file_enum *)penum;
}
if (penum) gs_free_object(mem,penum,"ramfs_enum_init(ramfs_enum)");
if (pattern)
gs_free_object(mem, pattern, "ramfs_enum_init(pattern)");
if(e) ramfs_enum_end(e);
return NULL;
}
static void
ram_enumerate_close(gs_memory_t * mem, file_enum *pfen)
{
gsram_enum *penum = (gsram_enum *)pfen;
gs_memory_t *mem2 = penum->memory;
(void)mem;
ramfs_enum_end(penum->e);
gs_free_object(mem2, penum->pattern, "ramfs_enum_init(pattern)");
gs_free_object(mem2, penum, "ramfs_enum_init(ramfs_enum)");
}
static uint
ram_enumerate_next(gs_memory_t * mem, file_enum *pfen, char *ptr, uint maxlen)
{
gsram_enum *penum = (gsram_enum *)pfen;
char * filename;
while ((filename = ramfs_enum_next(penum->e))) {
if (string_match((byte *)filename, strlen(filename),
(byte *)penum->pattern,
strlen(penum->pattern), 0)) {
if (strlen(filename) < maxlen)
memcpy(ptr, filename, strlen(filename));
return strlen(filename); /* if > maxlen, caller will detect rangecheck */
}
}
/* ran off end of list, close the enum */
ram_enumerate_close(mem, pfen);
return ~(uint)0;
}
static int
ram_get_params(gx_io_device * iodev, gs_param_list * plist)
{
int code;
int i0 = 0, so = 1;
bool btrue = true, bfalse = false;
ramfs* fs = GETRAMFS(iodev->state);
int BlockSize;
long Free, LogicalSize;
BlockSize = ramfs_blocksize(fs);
LogicalSize = MAXBLOCKS;
Free = ramfs_blocksfree(fs);
if (
(code = param_write_bool(plist, "HasNames", &btrue)) < 0 ||
(code = param_write_int (plist, "BlockSize", &BlockSize)) < 0 ||
(code = param_write_long(plist, "Free", &Free)) < 0 ||
(code = param_write_int (plist, "InitializeAction",&i0)) < 0 ||
(code = param_write_bool(plist, "Mounted", &btrue)) < 0 ||
(code = param_write_bool(plist, "Removable", &bfalse)) < 0 ||
(code = param_write_bool(plist, "Searchable", &btrue)) < 0 ||
(code = param_write_int (plist, "SearchOrder", &so)) < 0 ||
(code = param_write_bool(plist, "Writeable", &btrue)) < 0 ||
(code = param_write_long(plist, "LogicalSize", &LogicalSize)) < 0
)
return code;
return 0;
}
|