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