Coverage Report

Created: 2025-06-24 07:01

/src/ghostpdl/base/ramfs.c
Line
Count
Source (jump to first uncovered line)
1
/* Copyright (C) 2001-2023 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
162k
{
83
162k
    ramfs * fs = gs_alloc_struct(mem->non_gc_memory, ramfs, &st_ramfs, "ramfs_new");
84
85
162k
    if (fs == NULL) {
86
0
        return NULL;
87
0
    }
88
162k
    size = size/(RAMFS_BLOCKSIZE/1024);
89
162k
    fs->files = NULL;
90
162k
    fs->active_enums = NULL;
91
162k
    fs->blocksfree = size;
92
162k
    fs->last_error = 0;
93
162k
    fs->memory = mem->non_gc_memory;
94
162k
    return fs;
95
162k
}
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
162k
{
104
162k
    ramdirent * ent;
105
106
162k
    if(fs == NULL) return;
107
108
162k
    ent = fs->files;
109
162k
    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
162k
    gs_free_object(fs->memory, fs, "ramfs_destroy");
118
162k
}
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
0
    int newblocks = (size+RAMFS_BLOCKSIZE-1)/RAMFS_BLOCKSIZE;
125
0
    void *buf;
126
127
0
    if(newblocks > file->blocks) {
128
        /* allocate blocks for file as necessary */
129
130
0
        if(newblocks-file->blocks > file->fs->blocksfree) {
131
0
            return -RAMFS_NOSPACE;
132
0
        }
133
0
        if(file->blocklist_size < newblocks) {
134
0
            int newsize = file->blocklist_size;
135
0
            if (newsize > MACROBLOCK_REALLOC_MAX) {
136
0
                newsize = ((newblocks+MACROBLOCK_REALLOC_MAX-1)/
137
0
                    MACROBLOCK_REALLOC_MAX) * MACROBLOCK_REALLOC_MAX;
138
0
            } else {
139
0
                if(!newsize) newsize = 1;
140
0
                while(newsize < newblocks) newsize *= 2;
141
0
            }
142
0
            buf = gs_alloc_bytes(file->fs->memory, newsize * sizeof(char*), "ramfs resize");
143
0
            if (!buf)
144
0
                return gs_note_error(gs_error_VMerror);
145
0
            memcpy(buf, file->data, file->blocklist_size * sizeof(char *));
146
0
            gs_free_object(file->fs->memory, file->data, "ramfs resize, free buffer");
147
0
            file->data = buf;
148
0
            file->blocklist_size = newsize;
149
0
        }
150
0
        while(file->blocks<newblocks) {
151
0
            char * block = file->data[file->blocks] =
152
0
                (char *)gs_alloc_bytes_immovable(file->fs->memory, RAMFS_BLOCKSIZE, "ramfs resize");
153
0
            if(!block) {
154
0
                return -RAMFS_NOMEM;
155
0
            }
156
0
            file->blocks++;
157
0
            file->fs->blocksfree--;
158
0
        }
159
0
    } else if (newblocks < file->blocks) {
160
        /* don't bother shrinking the block array */
161
0
        file->fs->blocksfree += (file->blocks-newblocks);
162
0
        while(file->blocks > newblocks) {
163
0
            gs_free_object(file->fs->memory, file->data[--file->blocks], "ramfs resize");
164
0
        }
165
0
    }
166
0
    file->size = size;
167
0
    return 0;
168
0
}
169
170
static ramdirent * ramfs_findfile(const ramfs* fs,const char *filename)
171
0
{
172
0
    ramdirent * thisdirent = fs->files;
173
0
    while(thisdirent) {
174
0
        if(strcmp(thisdirent->filename,filename) == 0) break;
175
0
        thisdirent = thisdirent->next;
176
0
    }
177
0
    return thisdirent;
178
0
}
179
180
ramhandle * ramfs_open(gs_memory_t *mem, ramfs* fs,const char * filename,int mode)
181
0
{
182
0
    ramdirent * thisdirent;
183
0
    ramfile* file;
184
0
    ramhandle* handle;
185
186
0
    if(mode & (RAMFS_CREATE|RAMFS_APPEND)) mode |= RAMFS_WRITE;
187
188
0
    thisdirent = ramfs_findfile(fs,filename);
189
190
0
    if(!thisdirent) {
191
        /* create file? */
192
0
        char * dirent_filename;
193
194
0
        if(!(mode & RAMFS_CREATE)) {
195
0
            fs->last_error = RAMFS_NOTFOUND;
196
0
            return NULL;
197
0
        }
198
199
0
        thisdirent = gs_alloc_struct(fs->memory, ramdirent, &st_ramdirent, "new ram directory entry");
200
0
        file = gs_alloc_struct(fs->memory, ramfile, &st_ramfile, "new ram file");
201
0
        dirent_filename = (char *)gs_alloc_bytes(fs->memory, strlen(filename) + 1, "ramfs filename");
202
0
        if(!(thisdirent && file && dirent_filename)) {
203
0
            gs_free_object(fs->memory, thisdirent, "error, cleanup directory entry");
204
0
            gs_free_object(fs->memory, file, "error, cleanup ram file");
205
0
            gs_free_object(fs->memory, dirent_filename, "error, cleanup ram filename");
206
0
            fs->last_error = RAMFS_NOMEM;
207
0
            return NULL;
208
0
        }
209
0
        strcpy(dirent_filename,filename);
210
0
        thisdirent->filename = dirent_filename;
211
0
        file->refcount = 1;
212
0
        file->size = 0;
213
0
        file->blocks = 0;
214
0
        file->blocklist_size = 0;
215
0
        file->data = NULL;
216
0
        file->fs = fs;
217
0
        thisdirent->inode = file;
218
0
        thisdirent->next = fs->files;
219
0
        fs->files = thisdirent;
220
0
    }
221
0
    file = thisdirent->inode;
222
0
    file->refcount++;
223
224
0
    handle = gs_alloc_struct(fs->memory, ramhandle, &st_ramhandle, "new ram directory entry");
225
0
    if(!handle) {
226
0
        fs->last_error = RAMFS_NOMEM;
227
0
        return NULL;
228
0
    }
229
0
    handle->file = file;
230
0
    handle->last_error = 0;
231
0
    handle->filepos = 0;
232
0
    handle->mode = mode;
233
234
0
    if(mode & RAMFS_TRUNC) {
235
0
        resize(file,0);
236
0
    }
237
0
    return handle;
238
0
}
239
240
0
int ramfs_blocksize(ramfs * fs) { return RAMFS_BLOCKSIZE; }
241
0
int ramfs_blocksfree(ramfs * fs) { return fs->blocksfree; }
242
0
int ramfile_error(ramhandle * handle) { return handle->last_error; }
243
244
static void unlink_node(ramfile * inode)
245
0
{
246
0
    int c;
247
248
0
    --inode->refcount;
249
0
    if(inode->refcount) return;
250
251
    /* remove the file and its data */
252
0
    for(c=0;c<inode->blocks;c++) {
253
0
        gs_free_object(inode->fs->memory, inode->data[c], "unlink node");
254
0
    }
255
0
    inode->fs->blocksfree += c;
256
0
    gs_free_object(inode->fs->memory, inode->data, "unlink node");
257
0
    gs_free_object(inode->fs->memory, inode, "unlink node");
258
0
}
259
260
int ramfs_unlink(ramfs * fs,const char *filename)
261
0
{
262
0
    ramdirent ** last;
263
0
    ramdirent * thisdirent;
264
0
    ramfs_enum* e;
265
266
0
    last = &fs->files;
267
0
    while(1) {
268
0
        if(!(thisdirent = *last)) {
269
0
            fs->last_error = RAMFS_NOTFOUND;
270
0
            return -1;
271
0
        }
272
0
        if(strcmp(thisdirent->filename,filename) == 0) break;
273
0
        last = &(thisdirent->next);
274
0
    }
275
276
0
    unlink_node(thisdirent->inode);
277
0
    gs_free_object(fs->memory, thisdirent->filename, "unlink");
278
0
    (*last) = thisdirent->next;
279
280
0
    e = fs->active_enums;
281
    /* advance enums that are pointing to the just-deleted file */
282
0
    while(e) {
283
0
        if(e->current == thisdirent) e->current = thisdirent->next;
284
0
        e = e->next;
285
0
    }
286
0
    gs_free_object(fs->memory, thisdirent, "unlink");
287
0
    return 0;
288
0
}
289
290
int ramfs_rename(ramfs * fs,const char* oldname,const char* newname)
291
0
{
292
0
    ramdirent * thisdirent;
293
0
    char * newnamebuf;
294
295
0
    thisdirent = ramfs_findfile(fs,oldname);
296
297
0
    if(!thisdirent) {
298
0
        fs->last_error = RAMFS_NOTFOUND;
299
0
        return -1;
300
0
    }
301
302
    /* just in case */
303
0
    if(strcmp(oldname,newname) == 0) return 0;
304
305
0
    newnamebuf = (char *)gs_alloc_bytes(fs->memory, strlen(newname)+1, "ramfs rename");
306
0
    if(!newnamebuf) {
307
0
        fs->last_error = RAMFS_NOMEM;
308
0
        return -1;
309
0
    }
310
311
    /* this may return RAMFS_NOTFOUND, which can be ignored. */
312
0
    ramfs_unlink(fs,newname);
313
314
0
    strcpy(newnamebuf,newname);
315
0
    gs_free_object(fs->memory, thisdirent->filename, "ramfs rename");
316
0
    thisdirent->filename = newnamebuf;
317
0
    return 0;
318
0
}
319
320
ramfs_enum * ramfs_enum_new(ramfs * fs)
321
0
{
322
0
    ramfs_enum * e;
323
324
0
    e = gs_alloc_struct(fs->memory, ramfs_enum, &st_ramfs_enum, "new ramfs enumerator");
325
0
    if(!e) {
326
0
        fs->last_error = RAMFS_NOMEM;
327
0
        return NULL;
328
0
    }
329
0
    e->current = fs->files;
330
0
    e->next = fs->active_enums;
331
0
    e->fs = fs;
332
0
    fs->active_enums = e;
333
0
    return e;
334
0
}
335
336
char* ramfs_enum_next(ramfs_enum * e)
337
0
{
338
0
    char * filename = NULL;
339
0
    if(e->current) {
340
0
        filename = e->current->filename;
341
0
        e->current = e->current->next;
342
0
    }
343
0
    return filename;
344
0
}
345
346
void ramfs_enum_end(ramfs_enum * e)
347
0
{
348
0
    ramfs_enum** last = &e->fs->active_enums;
349
0
    while(*last) {
350
0
        if(*last == e) {
351
0
            *last = e->next;
352
0
            break;
353
0
        }
354
0
        last = &(e->next);
355
0
    }
356
0
    gs_free_object(e->fs->memory, e, "free ramfs enumerator");
357
0
}
358
359
int ramfile_read(ramhandle * handle,void * buf,int len)
360
0
{
361
0
    ramfile * file = handle->file;
362
0
    int left;
363
0
    char *t = (char *)buf;
364
365
0
    if(len>file->size - handle->filepos) len = file->size-handle->filepos;
366
0
    if(len<0) return 0;
367
368
0
    left = len;
369
0
    while(left) {
370
0
        char * p = file->data[handle->filepos/RAMFS_BLOCKSIZE]+handle->filepos%RAMFS_BLOCKSIZE;
371
0
        int x = RAMFS_BLOCKSIZE-handle->filepos%RAMFS_BLOCKSIZE;
372
0
        if(x>left) x = left;
373
374
0
        memcpy(t,p,x);
375
0
        handle->filepos += x;
376
0
        left -= x;
377
0
        t += x;
378
0
    }
379
0
    return len;
380
0
}
381
382
int ramfile_write(ramhandle * handle,const void * buf,int len)
383
0
{
384
0
    ramfile * file = handle->file;
385
0
    int left;
386
0
    char *t = (char *)buf;
387
388
0
    if(!(handle->mode & RAMFS_WRITE)) {
389
0
        handle->last_error = RAMFS_NOACCESS;
390
0
        return -1;
391
0
    }
392
393
0
    if (len < 0 || handle->filepos + len < 0) {
394
0
        handle->last_error = RAMFS_BADRANGE;
395
0
        return -1;
396
0
    }
397
398
0
    if(handle->mode & RAMFS_APPEND) {
399
0
        handle->filepos = file->size;
400
0
    }
401
402
0
    if(file->size < handle->filepos) {
403
        /* if this fails then pass the error on */
404
0
        if(ramfile_truncate(handle,handle->filepos) == -1) return -1;
405
0
    }
406
407
0
    if(file->size < handle->filepos+len) {
408
0
        int x = resize(file,handle->filepos+len);
409
0
        if(x) {
410
0
            handle->last_error = -x;
411
0
            return -1;
412
0
        }
413
0
    }
414
415
    /* This is exactly the same as for reading, cept the copy is in the
416
       other direction. */
417
0
    left = len;
418
0
    while(left) {
419
0
        char * p = file->data[handle->filepos/RAMFS_BLOCKSIZE] +
420
0
            handle->filepos%RAMFS_BLOCKSIZE;
421
0
        int x = RAMFS_BLOCKSIZE-handle->filepos%RAMFS_BLOCKSIZE;
422
0
        if(x>left) x = left;
423
424
0
        memcpy(p,t,x);
425
0
        handle->filepos += x;
426
0
        left -= x;
427
0
        t += x;
428
0
    }
429
0
    return len;
430
0
}
431
432
int ramfile_seek(ramhandle * handle,gs_offset_t pos,int whence)
433
0
{
434
    /* Just set the handle's file position.  The effects become noticeable
435
       at the next read or write.
436
    */
437
0
    gs_offset_t newpos = handle->filepos;
438
439
0
    if(whence == RAMFS_SEEK_CUR) {
440
0
        newpos += pos;
441
0
    } else if(whence == RAMFS_SEEK_END) {
442
0
        newpos = handle->file->size+pos;
443
0
    } else {
444
0
        newpos = pos;
445
0
    }
446
447
0
    if(newpos < 0 || newpos != (int)newpos)
448
0
        return -1;
449
450
0
    handle->filepos = (int)newpos;
451
0
    return 0;
452
0
}
453
454
int ramfile_size(ramhandle * handle)
455
0
{
456
0
    return handle->file->size;
457
0
}
458
459
static int ramfile_truncate(ramhandle * handle,int size)
460
0
{
461
0
    ramfile * file = handle->file;
462
0
    int oldsize = file->size;
463
0
    int x = resize(file,size);
464
465
0
    if(x) {
466
0
        handle->last_error = -x;
467
0
        return -1;
468
0
    }
469
0
    if(oldsize >= size) return 0;
470
471
    /* file was expanded. fill the new space with zeros. */
472
0
    while(oldsize < file->size) {
473
0
        char * p = file->data[oldsize/RAMFS_BLOCKSIZE]+oldsize%RAMFS_BLOCKSIZE;
474
0
        int len = RAMFS_BLOCKSIZE - oldsize%RAMFS_BLOCKSIZE;
475
0
        if(len>file->size-oldsize) len = file->size-oldsize;
476
0
        oldsize += len;
477
0
        memset(p,0,len);
478
0
    }
479
0
    return 0;
480
0
}
481
482
void ramfile_close(ramhandle * handle)
483
0
{
484
0
    ramfile * file = handle->file;
485
0
    unlink_node(file);
486
0
    gs_free_object(handle->file->fs->memory, handle, "ramfs close");
487
0
}
488
489
int ramfile_tell(ramhandle* handle)
490
0
{
491
0
    return handle->filepos;
492
0
}
493
494
int ramfile_eof(ramhandle* handle)
495
0
{
496
0
    return (handle->filepos >= handle->file->size);
497
0
}