Coverage Report

Created: 2025-11-16 07:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ghostpdl/base/ramfs.c
Line
Count
Source
1
/* Copyright (C) 2001-2025 Artifex Software, Inc.
2
   All Rights Reserved.
3
4
   This software is provided AS-IS with no warranty, either express or
5
   implied.
6
7
   This software is distributed under license and may not be copied,
8
   modified or distributed except as expressly authorized under the terms
9
   of the license contained in the file LICENSE in this distribution.
10
11
   Refer to licensing information at http://www.artifex.com or contact
12
   Artifex Software, Inc.,  39 Mesa Street, Suite 108A, San Francisco,
13
   CA 94129, USA, for further information.
14
*/
15
16
/*
17
  memory-based simulated file system
18
 */
19
20
#include "unistd_.h"
21
#include "string_.h"
22
#include "gx.h"
23
#include "gserrors.h"
24
#include "gp.h"
25
#include "gscdefs.h"
26
#include "gsparam.h"
27
#include "gsstruct.h"
28
#include "ramfs.h"
29
30
0
#define MACROBLOCK_REALLOC_MAX 128
31
32
typedef struct _ramfs {
33
    struct _ramdirent * files;
34
    struct _ramfs_enum* active_enums;
35
    gs_memory_t *memory;
36
    int blocksfree;
37
    int last_error;
38
}__ramfs;
39
40
gs_private_st_simple(st_ramfs, struct _ramfs, "gsram_ramfs");
41
42
struct _ramdirent {
43
    char* filename;
44
    struct _ramfile* inode;
45
    struct _ramdirent* next;
46
};
47
48
gs_private_st_simple(st_ramdirent, struct _ramdirent, "gsram_ramdirent");
49
50
typedef struct _ramfile {
51
    ramfs* fs;
52
    int refcount;
53
    int size;
54
    int blocks;
55
    int blocklist_size;
56
    char** data;
57
} ramfile;
58
59
gs_private_st_simple(st_ramfile, struct _ramfile, "gsram_ramfile");
60
61
struct _ramhandle {
62
    ramfile * file;
63
    int last_error;
64
    int filepos;
65
    int mode;
66
};
67
68
gs_private_st_simple(st_ramhandle, struct _ramhandle, "gsram_ramhandle");
69
70
struct _ramfs_enum {
71
    ramfs* fs;
72
    ramdirent * current;
73
    struct _ramfs_enum* next;
74
};
75
76
gs_private_st_simple(st_ramfs_enum, struct _ramfs_enum, "gsram_ramfs_enum");
77
78
static void unlink_node(ramfile * inode);
79
static int ramfile_truncate(ramhandle * handle,int size);
80
81
ramfs * ramfs_new(gs_memory_t *mem, int size)
82
192k
{
83
192k
    ramfs * fs = gs_alloc_struct(mem->non_gc_memory, ramfs, &st_ramfs, "ramfs_new");
84
85
192k
    if (fs == NULL) {
86
0
        return NULL;
87
0
    }
88
192k
    size = size/(RAMFS_BLOCKSIZE/1024);
89
192k
    fs->files = NULL;
90
192k
    fs->active_enums = NULL;
91
192k
    fs->blocksfree = size;
92
192k
    fs->last_error = 0;
93
192k
    fs->memory = mem->non_gc_memory;
94
192k
    return fs;
95
192k
}
96
97
/* This function makes no attempt to check that there are no open files or
98
   enums.  If there are any when this function is called, memory leakage will
99
   result and any attempt to access the open files or enums will probably
100
   cause a segfault.  Caveat emptor, or something.
101
*/
102
void ramfs_destroy(gs_memory_t *mem, ramfs * fs)
103
192k
{
104
192k
    ramdirent * ent;
105
106
192k
    if(fs == NULL) return;
107
108
192k
    ent = fs->files;
109
192k
    while(ent) {
110
0
        ramdirent* prev;
111
0
        gs_free_object(fs->memory, ent->filename, "ramfs_destroy, filename");
112
0
        unlink_node(ent->inode);
113
0
        prev = ent;
114
0
        ent = ent->next;
115
0
        gs_free_object(fs->memory, prev, "ramfs_destroy, entry");
116
0
    }
117
192k
    gs_free_object(fs->memory, fs, "ramfs_destroy");
118
192k
}
119
120
0
int ramfs_error(const ramfs* fs) { return fs->last_error; }
121
122
static int resize(ramfile * file,int size)
123
0
{
124
    /* A resize request larger than an int will fail at a higher level
125
       so doing the following calculation as unsigned should be sufficient to
126
       avoid overflow at this level.
127
     */
128
0
    int newblocks = (int)(((unsigned int)size)+RAMFS_BLOCKSIZE-1)/RAMFS_BLOCKSIZE;
129
0
    void *buf;
130
131
0
    if(newblocks > file->blocks) {
132
        /* allocate blocks for file as necessary */
133
134
0
        if(newblocks-file->blocks > file->fs->blocksfree) {
135
0
            return -RAMFS_NOSPACE;
136
0
        }
137
0
        if(file->blocklist_size < newblocks) {
138
0
            int newsize = file->blocklist_size;
139
0
            if (newsize > MACROBLOCK_REALLOC_MAX) {
140
0
                newsize = ((newblocks+MACROBLOCK_REALLOC_MAX-1)/
141
0
                    MACROBLOCK_REALLOC_MAX) * MACROBLOCK_REALLOC_MAX;
142
0
            } else {
143
0
                if(!newsize) newsize = 1;
144
0
                while(newsize < newblocks) newsize *= 2;
145
0
            }
146
0
            buf = gs_alloc_bytes(file->fs->memory, (size_t)newsize * sizeof(char*), "ramfs resize");
147
0
            if (!buf)
148
0
                return gs_note_error(gs_error_VMerror);
149
0
            memcpy(buf, file->data, file->blocklist_size * sizeof(char *));
150
0
            gs_free_object(file->fs->memory, file->data, "ramfs resize, free buffer");
151
0
            file->data = buf;
152
0
            file->blocklist_size = newsize;
153
0
        }
154
0
        while(file->blocks<newblocks) {
155
0
            char * block = file->data[file->blocks] =
156
0
                (char *)gs_alloc_bytes_immovable(file->fs->memory, RAMFS_BLOCKSIZE, "ramfs resize");
157
0
            if(!block) {
158
0
                return -RAMFS_NOMEM;
159
0
            }
160
0
            file->blocks++;
161
0
            file->fs->blocksfree--;
162
0
        }
163
0
    } else if (newblocks < file->blocks) {
164
        /* don't bother shrinking the block array */
165
0
        file->fs->blocksfree += (file->blocks-newblocks);
166
0
        while(file->blocks > newblocks) {
167
0
            gs_free_object(file->fs->memory, file->data[--file->blocks], "ramfs resize");
168
0
        }
169
0
    }
170
0
    file->size = size;
171
0
    return 0;
172
0
}
173
174
static ramdirent * ramfs_findfile(const ramfs* fs,const char *filename)
175
0
{
176
0
    ramdirent * thisdirent = fs->files;
177
0
    while(thisdirent) {
178
0
        if(strcmp(thisdirent->filename,filename) == 0) break;
179
0
        thisdirent = thisdirent->next;
180
0
    }
181
0
    return thisdirent;
182
0
}
183
184
ramhandle * ramfs_open(gs_memory_t *mem, ramfs* fs,const char * filename,int mode)
185
0
{
186
0
    ramdirent * thisdirent;
187
0
    ramfile* file;
188
0
    ramhandle* handle;
189
190
0
    if(mode & (RAMFS_CREATE|RAMFS_APPEND)) mode |= RAMFS_WRITE;
191
192
0
    thisdirent = ramfs_findfile(fs,filename);
193
194
0
    if(!thisdirent) {
195
        /* create file? */
196
0
        char * dirent_filename;
197
198
0
        if(!(mode & RAMFS_CREATE)) {
199
0
            fs->last_error = RAMFS_NOTFOUND;
200
0
            return NULL;
201
0
        }
202
203
0
        thisdirent = gs_alloc_struct(fs->memory, ramdirent, &st_ramdirent, "new ram directory entry");
204
0
        file = gs_alloc_struct(fs->memory, ramfile, &st_ramfile, "new ram file");
205
0
        dirent_filename = (char *)gs_alloc_bytes(fs->memory, strlen(filename) + 1, "ramfs filename");
206
0
        if(!(thisdirent && file && dirent_filename)) {
207
0
            gs_free_object(fs->memory, thisdirent, "error, cleanup directory entry");
208
0
            gs_free_object(fs->memory, file, "error, cleanup ram file");
209
0
            gs_free_object(fs->memory, dirent_filename, "error, cleanup ram filename");
210
0
            fs->last_error = RAMFS_NOMEM;
211
0
            return NULL;
212
0
        }
213
0
        strcpy(dirent_filename,filename);
214
0
        thisdirent->filename = dirent_filename;
215
0
        file->refcount = 1;
216
0
        file->size = 0;
217
0
        file->blocks = 0;
218
0
        file->blocklist_size = 0;
219
0
        file->data = NULL;
220
0
        file->fs = fs;
221
0
        thisdirent->inode = file;
222
0
        thisdirent->next = fs->files;
223
0
        fs->files = thisdirent;
224
0
    } else {
225
0
        file = thisdirent->inode;
226
0
        file->refcount++;
227
0
    }
228
229
0
    handle = gs_alloc_struct(fs->memory, ramhandle, &st_ramhandle, "new ram directory entry");
230
0
    if(!handle) {
231
0
        fs->last_error = RAMFS_NOMEM;
232
0
        return NULL;
233
0
    }
234
0
    handle->file = file;
235
0
    handle->last_error = 0;
236
0
    handle->filepos = 0;
237
0
    handle->mode = mode;
238
239
0
    if(mode & RAMFS_TRUNC && file->size != 0) {
240
0
        resize(file,0);
241
0
    }
242
0
    return handle;
243
0
}
244
245
0
int ramfs_blocksize(ramfs * fs) { return RAMFS_BLOCKSIZE; }
246
0
int ramfs_blocksfree(ramfs * fs) { return fs->blocksfree; }
247
0
int ramfile_error(ramhandle * handle) { return handle->last_error; }
248
249
static void unlink_node(ramfile * inode)
250
0
{
251
0
    int c;
252
253
    /* remove the file and its data */
254
0
    for(c=0;c<inode->blocks;c++) {
255
0
        gs_free_object(inode->fs->memory, inode->data[c], "unlink node");
256
0
    }
257
0
    inode->fs->blocksfree += c;
258
0
    gs_free_object(inode->fs->memory, inode->data, "unlink node");
259
0
    gs_free_object(inode->fs->memory, inode, "unlink node");
260
0
}
261
262
int ramfs_unlink(ramfs * fs,const char *filename)
263
0
{
264
0
    ramdirent ** last;
265
0
    ramdirent * thisdirent;
266
0
    ramfs_enum* e;
267
268
0
    last = &fs->files;
269
0
    while(1) {
270
0
        if(!(thisdirent = *last)) {
271
0
            fs->last_error = RAMFS_NOTFOUND;
272
0
            return -1;
273
0
        }
274
0
        if(strcmp(thisdirent->filename,filename) == 0) break;
275
0
        last = &(thisdirent->next);
276
0
    }
277
278
0
    if (thisdirent->inode->refcount != 0) {
279
0
        fs->last_error = RAMFS_DELETEOPENFILE;
280
0
        return -1;
281
0
    }
282
283
0
    unlink_node(thisdirent->inode);
284
0
    gs_free_object(fs->memory, thisdirent->filename, "unlink");
285
0
    (*last) = thisdirent->next;
286
287
0
    e = fs->active_enums;
288
    /* advance enums that are pointing to the just-deleted file */
289
0
    while(e) {
290
0
        if(e->current == thisdirent) e->current = thisdirent->next;
291
0
        e = e->next;
292
0
    }
293
0
    gs_free_object(fs->memory, thisdirent, "unlink");
294
0
    return 0;
295
0
}
296
297
int ramfs_rename(ramfs * fs,const char* oldname,const char* newname)
298
0
{
299
0
    ramdirent * thisdirent;
300
0
    char * newnamebuf;
301
302
0
    thisdirent = ramfs_findfile(fs,oldname);
303
304
0
    if(!thisdirent) {
305
0
        fs->last_error = RAMFS_NOTFOUND;
306
0
        return -1;
307
0
    }
308
309
    /* just in case */
310
0
    if(strcmp(oldname,newname) == 0) return 0;
311
312
0
    newnamebuf = (char *)gs_alloc_bytes(fs->memory, strlen(newname)+1, "ramfs rename");
313
0
    if(!newnamebuf) {
314
0
        fs->last_error = RAMFS_NOMEM;
315
0
        return -1;
316
0
    }
317
318
    /* this may return RAMFS_NOTFOUND, which can be ignored. */
319
0
    ramfs_unlink(fs,newname);
320
321
0
    strcpy(newnamebuf,newname);
322
0
    gs_free_object(fs->memory, thisdirent->filename, "ramfs rename");
323
0
    thisdirent->filename = newnamebuf;
324
0
    return 0;
325
0
}
326
327
ramfs_enum * ramfs_enum_new(ramfs * fs)
328
0
{
329
0
    ramfs_enum * e;
330
331
0
    e = gs_alloc_struct(fs->memory, ramfs_enum, &st_ramfs_enum, "new ramfs enumerator");
332
0
    if(!e) {
333
0
        fs->last_error = RAMFS_NOMEM;
334
0
        return NULL;
335
0
    }
336
0
    e->current = fs->files;
337
0
    e->next = fs->active_enums;
338
0
    e->fs = fs;
339
0
    fs->active_enums = e;
340
0
    return e;
341
0
}
342
343
char* ramfs_enum_next(ramfs_enum * e)
344
0
{
345
0
    char * filename = NULL;
346
0
    if(e->current) {
347
0
        filename = e->current->filename;
348
0
        e->current = e->current->next;
349
0
    }
350
0
    return filename;
351
0
}
352
353
void ramfs_enum_end(ramfs_enum * e)
354
0
{
355
0
    ramfs_enum** last = &e->fs->active_enums;
356
0
    while(*last) {
357
0
        if(*last == e) {
358
0
            *last = e->next;
359
0
            break;
360
0
        }
361
0
        last = &(e->next);
362
0
    }
363
0
    gs_free_object(e->fs->memory, e, "free ramfs enumerator");
364
0
}
365
366
int ramfile_read(ramhandle * handle,void * buf,int len)
367
0
{
368
0
    ramfile * file = handle->file;
369
0
    int left;
370
0
    char *t = (char *)buf;
371
372
0
    if(len>file->size - handle->filepos) len = file->size-handle->filepos;
373
0
    if(len<0) return 0;
374
375
0
    left = len;
376
0
    while(left) {
377
0
        char * p = file->data[handle->filepos/RAMFS_BLOCKSIZE]+handle->filepos%RAMFS_BLOCKSIZE;
378
0
        int x = RAMFS_BLOCKSIZE-handle->filepos%RAMFS_BLOCKSIZE;
379
0
        if(x>left) x = left;
380
381
0
        memcpy(t,p,x);
382
0
        handle->filepos += x;
383
0
        left -= x;
384
0
        t += x;
385
0
    }
386
0
    return len;
387
0
}
388
389
int ramfile_write(ramhandle * handle,const void * buf,int len)
390
0
{
391
0
    ramfile * file = handle->file;
392
0
    int left;
393
0
    char *t = (char *)buf;
394
395
0
    if(!(handle->mode & RAMFS_WRITE)) {
396
0
        handle->last_error = RAMFS_NOACCESS;
397
0
        return -1;
398
0
    }
399
400
0
    if (len < 0 || handle->filepos + len < 0) {
401
0
        handle->last_error = RAMFS_BADRANGE;
402
0
        return -1;
403
0
    }
404
405
0
    if(handle->mode & RAMFS_APPEND) {
406
0
        handle->filepos = file->size;
407
0
    }
408
409
0
    if(file->size < handle->filepos) {
410
        /* if this fails then pass the error on */
411
0
        if(ramfile_truncate(handle,handle->filepos) == -1) return -1;
412
0
    }
413
414
0
    if(file->size < handle->filepos+len) {
415
0
        int x = resize(file,handle->filepos+len);
416
0
        if(x) {
417
0
            handle->last_error = -x;
418
0
            return -1;
419
0
        }
420
0
    }
421
422
    /* This is exactly the same as for reading, cept the copy is in the
423
       other direction. */
424
0
    left = len;
425
0
    while(left) {
426
0
        char * p = file->data[handle->filepos/RAMFS_BLOCKSIZE] +
427
0
            handle->filepos%RAMFS_BLOCKSIZE;
428
0
        int x = RAMFS_BLOCKSIZE-handle->filepos%RAMFS_BLOCKSIZE;
429
0
        if(x>left) x = left;
430
431
0
        memcpy(p,t,x);
432
0
        handle->filepos += x;
433
0
        left -= x;
434
0
        t += x;
435
0
    }
436
0
    return len;
437
0
}
438
439
int ramfile_seek(ramhandle * handle,gs_offset_t pos,int whence)
440
0
{
441
    /* Just set the handle's file position.  The effects become noticeable
442
       at the next read or write.
443
    */
444
0
    gs_offset_t newpos = handle->filepos;
445
446
0
    if(whence == RAMFS_SEEK_CUR) {
447
0
        newpos += pos;
448
0
    } else if(whence == RAMFS_SEEK_END) {
449
0
        newpos = handle->file->size+pos;
450
0
    } else {
451
0
        newpos = pos;
452
0
    }
453
454
0
    if(newpos < 0 || newpos != (int)newpos)
455
0
        return -1;
456
457
0
    handle->filepos = (int)newpos;
458
0
    return 0;
459
0
}
460
461
int ramfile_size(ramhandle * handle)
462
0
{
463
0
    return handle->file->size;
464
0
}
465
466
static int ramfile_truncate(ramhandle * handle,int size)
467
0
{
468
0
    ramfile * file = handle->file;
469
0
    int oldsize = file->size;
470
0
    int x = resize(file,size);
471
472
0
    if(x) {
473
0
        handle->last_error = -x;
474
0
        return -1;
475
0
    }
476
0
    if(oldsize >= size) return 0;
477
478
    /* file was expanded. fill the new space with zeros. */
479
0
    while(oldsize < file->size) {
480
0
        char * p = file->data[oldsize/RAMFS_BLOCKSIZE]+oldsize%RAMFS_BLOCKSIZE;
481
0
        int len = RAMFS_BLOCKSIZE - oldsize%RAMFS_BLOCKSIZE;
482
0
        if(len>file->size-oldsize) len = file->size-oldsize;
483
0
        oldsize += len;
484
0
        memset(p,0,len);
485
0
    }
486
0
    return 0;
487
0
}
488
489
void ramfile_close(ramhandle * handle)
490
0
{
491
0
    ramfile * file = handle->file;
492
0
    file->refcount--;
493
0
    gs_free_object(handle->file->fs->memory, handle, "ramfs close");
494
0
}
495
496
int ramfile_tell(ramhandle* handle)
497
0
{
498
0
    return handle->filepos;
499
0
}
500
501
int ramfile_eof(ramhandle* handle)
502
0
{
503
0
    return (handle->filepos >= handle->file->size);
504
0
}