splitkomdb.c 7.06 KB
Newer Older
1
2
/*
 * splitkomdb -- Split the database for efficient incremental backups
Per Cederqvist's avatar
Per Cederqvist committed
3
 * Copyright (C) 1999, 2001-2002  Lysator Academic Computer Association.
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
 *
 * 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 mail bug reports to bug-lyskom@lysator.liu.se. 
 */


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

#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#ifdef HAVE_STDLIB_H
#  include <stdlib.h>
#endif
39
#include "timewrap.h"
40
41
42
43
44
45
46
47

#include "kom-types.h"
#include "s-string.h"
#include "string-malloc.h"
#include "server/smalloc.h"
#include "kom-config.h"
#include "server-config.h"
#include "param.h"
Per Cederqvist's avatar
Per Cederqvist committed
48
#include "linkansi.h"
49
#include "eintr.h"
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

/* The current version of splitkomdb uses three files to store all state.
   You can restore the database with commands similar to these:

       cat $text_base $text_tail > lyskomd-texts
       cp $data_copy lyskomd-data

   Future versions of this program may use a more elaborate layout of
   the contents of the backup directory.  That is why you can
   configure the location of the directory, but not the file names
   used within that directory. */

static const char *text_base = NULL;
static const char *text_tail = NULL;
static const char *data_copy = NULL;


static const char *argv0;

static char *
create_filename(const char *suffix)
{
    char *res = smalloc(strlen(param.backup_dir) + 1 + strlen(suffix) + 1);
    sprintf(res, "%s/%s", param.backup_dir, suffix);
    return res;
}


static void
compute_filenames(void)
{
    text_base = create_filename("lyskomd-texts-base.backup");
    text_tail = create_filename("lyskomd-texts-tail.backup");
    data_copy = create_filename("lyskomd-data.backup");
}


static void
safe_remove(const char *filename)
{
    errno = 0;
    if (remove(filename) != 0 && errno != ENOENT)
    {
	perror(filename);
	exit(1);
    }
}

    
static void
usage(const char *arg0)
{
102
    fprintf(stderr, "usage: %s [-c config-file] [ -f ] [ -v ] [ -V ]\n",
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
	    arg0);
    exit(1);
}


static int
is_clean(FILE *fp)
{
    return (getc(fp) == 'C' &&
	    getc(fp) == 'L' &&
	    getc(fp) == 'E' &&
	    getc(fp) == 'A' &&
	    getc(fp) == 'N');
}

static void
copy_file(const char *from,
	  FILE *fromfp,
	  const char *to)
{
    char buf[BUFSIZ];
    FILE *tofp;
125
    size_t sz;
126

127
    if ((tofp = i_fopen(to, "w")) == NULL)
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
    {
	fprintf(stderr, "%s: opening ", argv0);
	perror(to);
	exit(1);
    }

    if (setvbuf(tofp, NULL, _IONBF, 0) != 0)
    {
	perror("setvbuf");
	exit(1);
    }
    
    while (1)
    {
	sz = fread(buf, 1, BUFSIZ, fromfp);
	if (ferror(fromfp))
	{
	    fprintf(stderr, "%s: error reading from ", argv0);
	    perror(from);
	    exit(1);
	}

	if (sz == 0)
	    break;

	if (fwrite(buf, 1, sz, tofp) != sz)
	{
	    fprintf(stderr, "%s: writing to ", argv0);
	    perror(to);
	    exit(1);
	}
    }

    if (ferror(tofp))
    {
	fprintf(stderr, "%s: writing to ", argv0);
	perror(to);
	exit(1);
    }

168
    if (i_fclose(tofp) != 0)
169
170
171
172
173
174
175
176
177
178
179
180
181
182
    {
	fprintf(stderr, "%s: closing ", argv0);
	perror(to);
	exit(1);
    }
}


static void
copy_db_file(void)
{
    FILE *fp;
    const char *from;

183
    fp = i_fopen((from = param.datafile_name), "r");
184
185
186
    if (fp == NULL || !is_clean(fp))
    {
	if (fp != NULL)
187
188
	    i_fclose(fp);
	fp = i_fopen((from = param.backupfile_name), "r");
189
190
191
192
193
194
195
196
	assert(is_clean(fp));
    }
    if (fseek(fp, 0, SEEK_SET) != 0)
    {
	perror("fseek");
	exit(1);
    }
    copy_file(from, fp, data_copy);
197
    i_fclose(fp);
198
199
200
201
202
203
204
205
206
207
208
209
}


static int
file_equal(const char *from,
	   FILE *fromfp,
	   const char *assumed_copy)
{
    FILE *copyfp;
    int c1;
    int c2;

210
    if ((copyfp = i_fopen(assumed_copy, "r")) == NULL)
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
    {
	if (errno == ENOENT)
	    return 0;

	fprintf(stderr, "%s: opening ", argv0);
	perror(assumed_copy);
	exit(1);
    }

    while (1)
    {
	if ((c1 = getc(copyfp)) == EOF)
	{
	    if (ferror(copyfp))
	    {
226
		i_fclose(copyfp);
227
228
229
230
		return 0;
	    }
	    else
	    {
231
		i_fclose(copyfp);
232
233
234
235
236
237
238
239
240
241
242
243
244
		return 1;
	    }
	}

	if ((c2 = getc(fromfp)) == EOF || c1 != c2)
	{
	    if (ferror(fromfp))
	    {
		fprintf(stderr, "%s: reading ", argv0);
		perror(from);
		exit(1);
	    }

245
	    i_fclose(copyfp);
246
247
248
249
250
251
252
253
254
255
256
	    return 0;
	}
    }
}


static void
copy_text_file(void)
{
    FILE *fromfp;

257
    if ((fromfp = i_fopen(param.textfile_name, "r")) == NULL)
258
259
260
261
262
263
264
265
266
    {
	fprintf(stderr, "%s: opening ", argv0);
	perror(param.textfile_name);
	exit(1);
    }

    if (file_equal(param.textfile_name, fromfp, text_base))
    {
	copy_file(param.textfile_name, fromfp, text_tail);
267
	i_fclose(fromfp);
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
    }
    else
    {
	safe_remove(text_base);
	safe_remove(text_tail);
	if (fseek(fromfp, 0, SEEK_SET) != 0)
	{
	    perror("fseek");
	    exit(1);
	}
	copy_file(param.textfile_name, fromfp, text_base);
    }
}

    
int
main (int    argc,
      char **argv)
{
    int i;
    char *default_config_file = NULL;
    char *config_file = NULL;
    int fulldump = 0;

    if (getuid() == 0 || geteuid() == 0)
    {
	fprintf(stderr, "%s: this program should run as lyskom, not root\n",
		argv[0]);
	exit(1);
    }

Per Cederqvist's avatar
Per Cederqvist committed
299
300
    link_ansi();

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
    argv0 = argv[0];

    /* Initialize the string handling package. */
    s_set_storage_management(string_malloc, string_realloc, string_free);

    /* Parse command line arguments. */
    for (i = 1; i < argc && argv[i][0] == '-'; i++)
    {
	if (argv[i][1] == '\0' || argv[i][2] != '\0')
	    usage(argv[0]);

	switch (argv[i][1])
	{
	case 'c':
	    if (config_file != NULL)
	    {
		fprintf(stderr, "%s: -c may only be used once\n", argv[0]);
		exit(1);
	    }
	    if (++i >= argc)
		usage(argv[0]);
	    config_file = argv[i];
	    break;

	case 'f':
	    if (fulldump != 0)
	    {
		fprintf(stderr, "%s: -f may only be used once\n", argv[0]);
		exit(1);
	    }
	    fulldump = 1;
	    break;

	case 'V':
	case 'v':
	    fprintf(stderr, "splitkomdb from %s-%s\n", PACKAGE, VERSION);
	    exit(0);

	default:
	    usage(argv[0]);
	}
    }

    if (i < argc)
	usage(argv[0]);

    /* Read in the configuration file. */

    if (config_file == NULL)
    {
	default_config_file = smalloc(strlen(DEFAULT_DBASE_DIR) +
				      strlen(CONFIG_FILE) + 2);
	sprintf(default_config_file, "%s/%s", DEFAULT_DBASE_DIR, CONFIG_FILE);
	config_file = default_config_file;
    }

    read_configuration(config_file);

    compute_filenames();

    if (fulldump)
    {
	safe_remove(text_base);
	safe_remove(text_tail);
	safe_remove(data_copy);
    }
    copy_db_file();
    copy_text_file();

    exit(0);
}