summaryrefslogtreecommitdiff
blob: 66893075dfdf6a0337a113320d9d52642e4d46b1 (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
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
/* Copyright (C) 2001-2020 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.
*/

/*
  memory-based simulated file system
 */

#include "unistd_.h"
#include "string_.h"
#include "gx.h"
#include "gserrors.h"
#include "gp.h"
#include "gscdefs.h"
#include "gsparam.h"
#include "gsstruct.h"
#include "ramfs.h"

#define MACROBLOCK_REALLOC_MAX 128

typedef struct _ramfs {
    struct _ramdirent * files;
    struct _ramfs_enum* active_enums;
    gs_memory_t *memory;
    int blocksfree;
    int last_error;
}__ramfs;

gs_private_st_simple(st_ramfs, struct _ramfs, "gsram_ramfs");

struct _ramdirent {
    char* filename;
    struct _ramfile* inode;
    struct _ramdirent* next;
};

gs_private_st_simple(st_ramdirent, struct _ramdirent, "gsram_ramdirent");

typedef struct _ramfile {
    ramfs* fs;
    int refcount;
    int size;
    int blocks;
    int blocklist_size;
    char** data;
} ramfile;

gs_private_st_simple(st_ramfile, struct _ramfile, "gsram_ramfile");

struct _ramhandle {
    ramfile * file;
    int last_error;
    int filepos;
    int mode;
};

gs_private_st_simple(st_ramhandle, struct _ramhandle, "gsram_ramhandle");

struct _ramfs_enum {
    ramfs* fs;
    ramdirent * current;
    struct _ramfs_enum* next;
};

gs_private_st_simple(st_ramfs_enum, struct _ramfs_enum, "gsram_ramfs_enum");

static void unlink_node(ramfile * inode);
static int ramfile_truncate(ramhandle * handle,int size);

ramfs * ramfs_new(gs_memory_t *mem, int size)
{
    ramfs * fs = gs_alloc_struct(mem->non_gc_memory, ramfs, &st_ramfs, "ramfs_new");

    if (fs == NULL) {
        return NULL;
    }
    size = size/(RAMFS_BLOCKSIZE/1024);
    fs->files = NULL;
    fs->active_enums = NULL;
    fs->blocksfree = size;
    fs->last_error = 0;
    fs->memory = mem->non_gc_memory;
    return fs;
}

/* This function makes no attempt to check that there are no open files or
   enums.  If there are any when this function is called, memory leakage will
   result and any attempt to access the open files or enums will probably
   cause a segfault.  Caveat emptor, or something.
*/
void ramfs_destroy(gs_memory_t *mem, ramfs * fs)
{
    ramdirent * ent;

    if(fs == NULL) return;

    ent = fs->files;
    while(ent) {
        ramdirent* prev;
        gs_free_object(fs->memory, ent->filename, "ramfs_destroy, filename");
        unlink_node(ent->inode);
        prev = ent;
        ent = ent->next;
        gs_free_object(fs->memory, prev, "ramfs_destroy, entry");
    }
    gs_free_object(fs->memory, fs, "ramfs_destroy");
}

int ramfs_error(const ramfs* fs) { return fs->last_error; }

static int resize(ramfile * file,int size)
{
    int newblocks = (size+RAMFS_BLOCKSIZE-1)/RAMFS_BLOCKSIZE;
    void *buf;

    if(newblocks > file->blocks) {
        /* allocate blocks for file as necessary */

        if(newblocks-file->blocks > file->fs->blocksfree) {
            return -RAMFS_NOSPACE;
        }
        if(file->blocklist_size < newblocks) {
            int newsize = file->blocklist_size;
            if (newsize > MACROBLOCK_REALLOC_MAX) {
                newsize = ((newblocks+MACROBLOCK_REALLOC_MAX-1)/
                    MACROBLOCK_REALLOC_MAX) * MACROBLOCK_REALLOC_MAX;
            } else {
                if(!newsize) newsize = 1;
                while(newsize < newblocks) newsize *= 2;
            }
            buf = gs_alloc_bytes(file->fs->memory, newsize * sizeof(char*), "ramfs resize");
            if (!buf)
                return gs_note_error(gs_error_VMerror);
            memcpy(buf, file->data, file->blocklist_size * sizeof(char *));
            gs_free_object(file->fs->memory, file->data, "ramfs resize, free buffer");
            file->data = buf;
            file->blocklist_size = newsize;
        }
        while(file->blocks<newblocks) {
            char * block = file->data[file->blocks] =
                (char *)gs_alloc_bytes_immovable(file->fs->memory, RAMFS_BLOCKSIZE, "ramfs resize");
            if(!block) {
                return -RAMFS_NOMEM;
            }
            file->blocks++;
            file->fs->blocksfree--;
        }
    } else if (newblocks < file->blocks) {
        /* don't bother shrinking the block array */
        file->fs->blocksfree += (file->blocks-newblocks);
        while(file->blocks > newblocks) {
            gs_free_object(file->fs->memory, file->data[--file->blocks], "ramfs resize");
        }
    }
    file->size = size;
    return 0;
}

static ramdirent * ramfs_findfile(const ramfs* fs,const char *filename)
{
    ramdirent * thisdirent = fs->files;
    while(thisdirent) {
        if(strcmp(thisdirent->filename,filename) == 0) break;
        thisdirent = thisdirent->next;
    }
    return thisdirent;
}

ramhandle * ramfs_open(gs_memory_t *mem, ramfs* fs,const char * filename,int mode)
{
    ramdirent * thisdirent;
    ramfile* file;
    ramhandle* handle;

    if(mode & (RAMFS_CREATE|RAMFS_APPEND)) mode |= RAMFS_WRITE;

    thisdirent = ramfs_findfile(fs,filename);

    if(!thisdirent) {
        /* create file? */
        char * dirent_filename;

        if(!(mode & RAMFS_CREATE)) {
            fs->last_error = RAMFS_NOTFOUND;
            return NULL;
        }

        thisdirent = gs_alloc_struct(fs->memory, ramdirent, &st_ramdirent, "new ram directory entry");
        file = gs_alloc_struct(fs->memory, ramfile, &st_ramfile, "new ram file");
        dirent_filename = (char *)gs_alloc_bytes(fs->memory, strlen(filename) + 1, "ramfs filename");
        if(!(thisdirent && file && dirent_filename)) {
            gs_free_object(fs->memory, thisdirent, "error, cleanup directory entry");
            gs_free_object(fs->memory, file, "error, cleanup ram file");
            gs_free_object(fs->memory, dirent_filename, "error, cleanup ram filename");
            fs->last_error = RAMFS_NOMEM;
            return NULL;
        }
        strcpy(dirent_filename,filename);
        thisdirent->filename = dirent_filename;
        file->refcount = 1;
        file->size = 0;
        file->blocks = 0;
        file->blocklist_size = 0;
        file->data = NULL;
        file->fs = fs;
        thisdirent->inode = file;
        thisdirent->next = fs->files;
        fs->files = thisdirent;
    }
    file = thisdirent->inode;
    file->refcount++;

    handle = gs_alloc_struct(fs->memory, ramhandle, &st_ramhandle, "new ram directory entry");
    if(!handle) {
        fs->last_error = RAMFS_NOMEM;
        return NULL;
    }
    handle->file = file;
    handle->last_error = 0;
    handle->filepos = 0;
    handle->mode = mode;

    if(mode & RAMFS_TRUNC) {
        resize(file,0);
    }
    return handle;
}

int ramfs_blocksize(ramfs * fs) { return RAMFS_BLOCKSIZE; }
int ramfs_blocksfree(ramfs * fs) { return fs->blocksfree; }
int ramfile_error(ramhandle * handle) { return handle->last_error; }

static void unlink_node(ramfile * inode)
{
    int c;

    --inode->refcount;
    if(inode->refcount) return;

    /* remove the file and its data */
    for(c=0;c<inode->blocks;c++) {
        gs_free_object(inode->fs->memory, inode->data[c], "unlink node");
    }
    inode->fs->blocksfree += c;
    gs_free_object(inode->fs->memory, inode->data, "unlink node");
    gs_free_object(inode->fs->memory, inode, "unlink node");
}

int ramfs_unlink(ramfs * fs,const char *filename)
{
    ramdirent ** last;
    ramdirent * thisdirent;
    ramfs_enum* e;

    last = &fs->files;
    while(1) {
        if(!(thisdirent = *last)) {
            fs->last_error = RAMFS_NOTFOUND;
            return -1;
        }
        if(strcmp(thisdirent->filename,filename) == 0) break;
        last = &(thisdirent->next);
    }

    unlink_node(thisdirent->inode);
    gs_free_object(fs->memory, thisdirent->filename, "unlink");
    (*last) = thisdirent->next;

    e = fs->active_enums;
    /* advance enums that are pointing to the just-deleted file */
    while(e) {
        if(e->current == thisdirent) e->current = thisdirent->next;
        e = e->next;
    }
    gs_free_object(fs->memory, thisdirent, "unlink");
    return 0;
}

int ramfs_rename(ramfs * fs,const char* oldname,const char* newname)
{
    ramdirent * thisdirent;
    char * newnamebuf;

    thisdirent = ramfs_findfile(fs,oldname);

    if(!thisdirent) {
        fs->last_error = RAMFS_NOTFOUND;
        return -1;
    }

    /* just in case */
    if(strcmp(oldname,newname) == 0) return 0;

    newnamebuf = (char *)gs_alloc_bytes(fs->memory, strlen(newname)+1, "ramfs rename");
    if(!newnamebuf) {
        fs->last_error = RAMFS_NOMEM;
        return -1;
    }

    /* this may return RAMFS_NOTFOUND, which can be ignored. */
    ramfs_unlink(fs,newname);

    strcpy(newnamebuf,newname);
    gs_free_object(fs->memory, thisdirent->filename, "ramfs rename");
    thisdirent->filename = newnamebuf;
    return 0;
}

ramfs_enum * ramfs_enum_new(ramfs * fs)
{
    ramfs_enum * e;

    e = gs_alloc_struct(fs->memory, ramfs_enum, &st_ramfs_enum, "new ramfs enumerator");
    if(!e) {
        fs->last_error = RAMFS_NOMEM;
        return NULL;
    }
    e->current = fs->files;
    e->next = fs->active_enums;
    e->fs = fs;
    fs->active_enums = e;
    return e;
}

char* ramfs_enum_next(ramfs_enum * e)
{
    char * filename = NULL;
    if(e->current) {
        filename = e->current->filename;
        e->current = e->current->next;
    }
    return filename;
}

void ramfs_enum_end(ramfs_enum * e)
{
    ramfs_enum** last = &e->fs->active_enums;
    while(*last) {
        if(*last == e) {
            *last = e->next;
            break;
        }
        last = &(e->next);
    }
    gs_free_object(e->fs->memory, e, "free ramfs enumerator");
}

int ramfile_read(ramhandle * handle,void * buf,int len)
{
    ramfile * file = handle->file;
    int left;
    char *t = (char *)buf;

    if(len>file->size - handle->filepos) len = file->size-handle->filepos;
    if(len<0) return 0;

    left = len;
    while(left) {
        char * p = file->data[handle->filepos/RAMFS_BLOCKSIZE]+handle->filepos%RAMFS_BLOCKSIZE;
        int x = RAMFS_BLOCKSIZE-handle->filepos%RAMFS_BLOCKSIZE;
        if(x>left) x = left;

        memcpy(t,p,x);
        handle->filepos += x;
        left -= x;
        t += x;
    }
    return len;
}

int ramfile_write(ramhandle * handle,const void * buf,int len)
{
    ramfile * file = handle->file;
    int left;
    char *t = (char *)buf;

    if(!(handle->mode & RAMFS_WRITE)) {
        handle->last_error = RAMFS_NOACCESS;
        return -1;
    }

    if(handle->mode & RAMFS_APPEND) {
        handle->filepos = file->size;
    }

    if(file->size < handle->filepos) {
        /* if this fails then pass the error on */
        if(ramfile_truncate(handle,handle->filepos) == -1) return -1;
    }

    if(file->size < handle->filepos+len) {
        int x = resize(file,handle->filepos+len);
        if(x) {
            handle->last_error = -x;
            return -1;
        }
    }

    /* This is exactly the same as for reading, cept the copy is in the
       other direction. */
    left = len;
    while(left) {
        char * p = file->data[handle->filepos/RAMFS_BLOCKSIZE] +
            handle->filepos%RAMFS_BLOCKSIZE;
        int x = RAMFS_BLOCKSIZE-handle->filepos%RAMFS_BLOCKSIZE;
        if(x>left) x = left;

        memcpy(p,t,x);
        handle->filepos += x;
        left -= x;
        t += x;
    }
    return len;
}

int ramfile_seek(ramhandle * handle,int pos,int whence)
{
    /* Just set the handle's file position.  The effects become noticeable
       at the next read or write.
    */
    if(whence == RAMFS_SEEK_CUR) {
        handle->filepos += pos;
    } else if(whence == RAMFS_SEEK_END) {
        handle->filepos = handle->file->size+pos;
    } else {
        handle->filepos = pos;
    }
    return 0;
}

int ramfile_size(ramhandle * handle)
{
    return handle->file->size;
}

static int ramfile_truncate(ramhandle * handle,int size)
{
    ramfile * file = handle->file;
    int oldsize = file->size;
    int x = resize(file,size);

    if(x) {
        handle->last_error = -x;
        return -1;
    }
    if(oldsize >= size) return 0;

    /* file was expanded. fill the new space with zeros. */
    while(oldsize < file->size) {
        char * p = file->data[oldsize/RAMFS_BLOCKSIZE]+oldsize%RAMFS_BLOCKSIZE;
        int len = RAMFS_BLOCKSIZE - oldsize%RAMFS_BLOCKSIZE;
        if(len>file->size-oldsize) len = file->size-oldsize;
        oldsize += len;
        memset(p,0,len);
    }
    return 0;
}

void ramfile_close(ramhandle * handle)
{
    ramfile * file = handle->file;
    unlink_node(file);
    gs_free_object(handle->file->fs->memory, handle, "ramfs close");
}

int ramfile_tell(ramhandle* handle)
{
    return handle->filepos;
}

int ramfile_eof(ramhandle* handle)
{
    return (handle->filepos >= handle->file->size);
}