]>
Commit | Line | Data |
---|---|---|
c441a31a | 1 | /* $Id$ |
d77bf7b9 | 2 | * |
59ec8dae | 3 | * This program dumps the Moira database to a series of output files |
4 | * which can be later read back into Moira in the event of a crash. | |
5eaef520 | 5 | * |
7ac48069 | 6 | * Copyright (C) 1988-1998 by the Massachusetts Institute of Technology. |
7 | * For copying and distribution information, please see the file | |
8 | * <mit-copyright.h>. | |
d77bf7b9 | 9 | */ |
10 | ||
d77bf7b9 | 11 | #include <mit-copyright.h> |
7ac48069 | 12 | #include <moira.h> |
d77bf7b9 | 13 | #include "dump_db.h" |
14 | ||
7ac48069 | 15 | #include <fcntl.h> |
16 | #include <stdio.h> | |
17 | #include <string.h> | |
18 | #include <unistd.h> | |
d77bf7b9 | 19 | |
7ac48069 | 20 | EXEC SQL INCLUDE sqlca; |
d77bf7b9 | 21 | |
7ac48069 | 22 | RCSID("$Header$"); |
d77bf7b9 | 23 | |
3d058c8a | 24 | EXEC SQL BEGIN DECLARE SECTION; |
5eaef520 | 25 | char *db = "moira"; |
3d058c8a | 26 | EXEC SQL END DECLARE SECTION; |
d77bf7b9 | 27 | |
5eaef520 | 28 | int main(int argc, char **argv) |
d77bf7b9 | 29 | { |
5eaef520 | 30 | char *prefix; |
5eaef520 | 31 | |
32 | if (argc != 2) | |
33 | { | |
34 | fprintf(stderr, "Usage: %s prefix\n", argv[0]); | |
35 | exit(1); | |
d77bf7b9 | 36 | } |
5eaef520 | 37 | prefix = argv[1]; |
d77bf7b9 | 38 | |
5eaef520 | 39 | EXEC SQL CONNECT :db IDENTIFIED BY :db; |
d77bf7b9 | 40 | |
5eaef520 | 41 | do_backups(prefix); |
d77bf7b9 | 42 | |
5eaef520 | 43 | EXEC SQL COMMIT; |
44 | exit(0); | |
d77bf7b9 | 45 | } |
46 | ||
7ac48069 | 47 | void dump_int(FILE *f, int n) |
d77bf7b9 | 48 | { |
5eaef520 | 49 | char buf[1024]; |
50 | sprintf(buf, "%d", n); | |
51 | dump_str(f, buf); | |
d77bf7b9 | 52 | } |
53 | ||
7ac48069 | 54 | void wpunt(void) |
d77bf7b9 | 55 | { |
5eaef520 | 56 | punt("can't write backup file"); |
d77bf7b9 | 57 | } |
58 | ||
7ac48069 | 59 | void dump_str(FILE *f, char *str) |
d77bf7b9 | 60 | { |
8114e5e1 | 61 | unsigned char *ibp, c; |
44d12d58 | 62 | int t; |
5eaef520 | 63 | |
7ac48069 | 64 | for (ibp = str; (c = *ibp); ibp++) |
5eaef520 | 65 | { |
66 | if (c < 32 || c > 126 || c == SEP_CHAR || c == '\\') | |
67 | { | |
7ac48069 | 68 | if (putc('\\', f) < 0) |
5eaef520 | 69 | wpunt(); |
70 | t = ((c >> 6) & 7) + '0'; | |
7ac48069 | 71 | if (putc(t, f) < 0) |
5eaef520 | 72 | wpunt(); |
73 | t = ((c >> 3) & 7) + '0'; | |
7ac48069 | 74 | if (putc(t, f) < 0) |
5eaef520 | 75 | wpunt(); |
76 | t = (c & 7) + '0'; | |
7ac48069 | 77 | if (putc(t, f) < 0) |
5eaef520 | 78 | wpunt(); |
79 | } | |
80 | else | |
81 | { | |
7ac48069 | 82 | if (putc(c, f) < 0) |
5eaef520 | 83 | wpunt(); |
d77bf7b9 | 84 | } |
85 | } | |
86 | } | |
87 | ||
7ac48069 | 88 | void safe_close(FILE *stream) |
d77bf7b9 | 89 | { |
5eaef520 | 90 | if (fflush(stream) == EOF) |
91 | punt("Unable to fflush"); | |
92 | if (fsync(fileno(stream)) != 0) | |
93 | punt("Unable to fsync"); | |
94 | fclose(stream); | |
95 | } | |
96 | ||
97 | FILE *open_file(char *prefix, char *suffix) | |
d77bf7b9 | 98 | { |
5eaef520 | 99 | char name[BUFSIZ]; |
100 | int fd; | |
101 | FILE *f; | |
102 | ||
103 | strcpy(name, prefix); | |
104 | strcat(name, suffix); | |
105 | ||
106 | fd = open(name, O_CREAT|O_WRONLY|O_EXCL, 0644); | |
107 | if (fd < 0) | |
108 | punt(name); | |
109 | f = fdopen(fd, "w"); | |
110 | if (!f) | |
111 | { | |
112 | fprintf(stderr, "fdopen of "); | |
113 | punt(name); | |
d77bf7b9 | 114 | } |
5eaef520 | 115 | fprintf(stderr, "Working on %s\n", name); |
116 | return f; | |
d77bf7b9 | 117 | } |
cebeca89 | 118 | |
119 | /* | |
120 | * Trim whitespace off the tail end of a string | |
121 | */ | |
122 | char *endtrim(char *save) | |
123 | { | |
124 | char *t, *s; | |
125 | ||
126 | s = save; | |
127 | for (t = s; *t; t++) | |
128 | continue; | |
129 | while (t > s) | |
130 | { | |
131 | --t; | |
132 | if (!isspace(*t)) | |
133 | { | |
134 | t++; | |
135 | break; | |
136 | } | |
137 | } | |
138 | if (*t) | |
139 | *t = '\0'; | |
140 | return s; | |
141 | } |