ram-io.c 3.54 KB
Newer Older
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
/* Database file handling.
 * Copyright (C) 1991, 1993-1999, 2001-2003  Lysator Academic Computer Association.
 *
 * This file is part of the LysKOM server.
 * 
 * LysKOM is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by 
 * the Free Software Foundation; either version 1, or (at your option) 
 * any later version.
 * 
 * LysKOM is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with LysKOM; see the file COPYING.  If not, write to
 * Lysator, c/o ISY, Linkoping University, S-581 83 Linkoping, SWEDEN,
 * or the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, 
 * MA 02139, USA.
 *
 * Please report bugs at http://bugzilla.lysator.liu.se/. 
 */

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <errno.h>
#include <stdio.h>
#include <assert.h>
#include "timewrap.h"

#include "server/smalloc.h"
#include "eintr.h"
#include "kom-types.h"
#include "ram-io.h"
#include "ram-parse.h"
#include "lyskomd.h"
#include "log.h"

42
/* The native output format is defined here. */
43
static int output_format = 3;
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

static long nr_dbfile_objects = 0;
static long nr_dbfile_files = 0;

static struct dbfile *
dbfile_new(void)
{
    struct dbfile *res = smalloc(sizeof(struct dbfile));
    res->fp = NULL;

    res->format = output_format;
    ++nr_dbfile_objects;
    return res;
}

int
dbfile_delete(struct dbfile *obj)
{
    int rv = 0;
    int errno_save = errno;
    
    if (obj->fp != NULL)
    {
	rv = i_fclose(obj->fp);
	errno_save = errno;
	--nr_dbfile_files;
    }

    sfree(obj);
    errno = errno_save;
    --nr_dbfile_objects;
    return rv;
}

void
set_output_format(int fmt)
{
    output_format = fmt;
    switch (fmt)
    {
    case 0:
    case 1:
    case 2:
        break;
    default:
        restart_kom("unknown output format selected: %d\n", fmt);
        break;
    }   
}

static int
get_version(struct dbfile *fp)
{
    int version;
    
    fparse_set_pos(fp, 5);
    if (dbfile_getc(fp) == '\n')
    {
	return 0;
    }
    version = fparse_long(fp);

    return version;
}

struct dbfile *
dbfile_open_read(const char *filename)
{
    struct dbfile *res = dbfile_new();
    res->fp = i_fopen(filename, "rb");
    if (res->fp == NULL)
    {
	int saved_errno = errno;
	dbfile_delete(res);
	errno = saved_errno;
	return NULL;
    }
    ++nr_dbfile_files;
    res->format = get_version(res);
    switch(res->format)
    {
    case 0:
    case 1:
    case 2:
128
    case 3:
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
	break;
    default:
        restart_kom("unknown input format selected: %d\n", res->format);
        break;
    }

    return res;
}

struct dbfile *
dbfile_open_write(const char *filename)
{
    struct dbfile *res = dbfile_new();
    if (res == NULL)
	return NULL;

    if ((res->fp = i_fopen(filename, "wb")) == NULL)
    {
	int saved_errno = errno;
	dbfile_delete(res);
	errno = saved_errno;
	return NULL;
    }

    ++nr_dbfile_files;
    return res;
}

void
dump_dbfile_stats(FILE *fp)
{
    fprintf(fp, "---%s:\n\tExisting dbfile objects:    %ld\n",
	    __FILE__, nr_dbfile_objects);
    fprintf(fp, "\tExisting dbfile files:    %ld\n", nr_dbfile_files);
}

165
long
166
dbfile_ftell(struct dbfile *fp)
167
{
168
    return ftell(fp->fp);
169
170
171
}

int
172
dbfile_getc(struct dbfile *fp)
173
{
174
    return getc(fp->fp);
175
176
177
}

int
178
dbfile_ungetc(struct dbfile *fp,
179
180
	      int c)
{
181
    return ungetc(c, fp->fp);
182
}