source: trunk/third/moira/backup/rest_db.pc @ 24319

Revision 24319, 4.2 KB checked in by broder, 14 years ago (diff)
New Moira snapshot from SVN.
Line 
1/* $Id: rest_db.pc 3956 2010-01-05 20:56:56Z zacheiss $
2 *
3 *      This program restores the Moira database from an mrbackup.
4 *
5 * Copyright (C) 1988-1998 by the Massachusetts Institute of Technology.
6 * For copying and distribution information, please see the file
7 * <mit-copyright.h>.
8 */
9
10#include <mit-copyright.h>
11#include <moira.h>
12#include "dump_db.h"
13
14#include <ctype.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <string.h>
18#include <unistd.h>
19
20EXEC SQL INCLUDE sqlca;
21
22RCSID("$HeadURL: svn+ssh://svn.mit.edu/moira/trunk/moira/backup/rest_db.pc $ $Id: rest_db.pc 3956 2010-01-05 20:56:56Z zacheiss $");
23
24int yes_or_no(char *prompt);
25
26int main(int argc, char **argv)
27{
28  char buf[BUFSIZ];
29  char *prefix;
30  EXEC SQL BEGIN DECLARE SECTION;
31  char *db;
32  EXEC SQL END DECLARE SECTION;
33
34  if (argc != 2)
35    {
36      fprintf(stderr, "Usage: %s database\n", argv[0]);
37      exit(1);
38    }
39  db = argv[1];
40
41  if (!yes_or_no("Do you *REALLY* want to wipe the moira database?"))
42    {
43      printf("I didn't think so\n");
44      exit(1);
45    }
46  sprintf(buf, "Have you initialized an empty database named %s?", db);
47  if (!yes_or_no(buf))
48    {
49      printf("You should have\n");
50      exit(1);
51    }
52
53  printf("Opening database: ");
54  fflush(stdout);
55  EXEC SQL CONNECT :db IDENTIFIED BY :db;
56  if (sqlca.sqlcode != 0)
57    {
58      com_err(argv[0], 0, "Database open failed");
59      exit(1);
60    }
61  printf(" done\n");
62
63  printf("Prefix of backup to restore: ");
64  fflush(stdout);
65  if (!fgets(buf, sizeof(buf), stdin))
66    return 1;
67 
68  prefix = strtrim(buf);
69
70  if (!yes_or_no("Are you SURE?"))
71    {
72      printf("I didn't think so\n");
73      exit(1);
74    }
75  do_restores(prefix);
76  printf("Restore complete\n");
77  EXEC SQL COMMIT;
78  exit(0);
79  /*NOTREACHED*/
80}
81
82int yes_or_no(char *prompt)
83{
84  char buf[BUFSIZ];
85  int ret;
86
87  int tt = open("/dev/tty", O_RDWR, 0);
88  FILE *o, *i;
89
90  char *cp;
91
92  if (tt < 0)
93    return 0;
94
95  fflush(stdout);
96  fflush(stderr);
97  o = fdopen(dup(tt), "w");
98  i = fdopen(dup(tt), "r");
99  close(tt);
100
101  for (;;)
102    {
103      fprintf(o, "%s (yes or no): ", prompt);
104      fflush(o);
105      if (!fgets(buf, BUFSIZ, i))
106        goto err;
107      for (cp = buf; *cp; cp++)
108        {
109          if (isupper(*cp))
110            *cp = tolower(*cp);
111        }
112      if (!strcmp(buf, "yes\n"))
113        {
114          ret = 1;
115          goto out;
116        }
117      if (!strcmp(buf, "no\n"))
118        {
119          ret = 0;
120          goto out;
121        }
122    }
123
124err:
125  ret = 0;
126
127out:
128  fclose(o);
129  fclose(i);
130  return ret;
131}
132
133int parse_int(FILE *f)
134{
135  int c;
136  int val = 0;
137  int sign = 1;
138  while ((c = getc(f)) != EOF && c != SEP_CHAR && c != '\n')
139    {
140      if (c == '-')
141        sign = -1;
142      else if (isdigit(c))
143        {
144          val *= 10;
145          val += (c - '0');
146        } else
147          fprintf(stderr, "non-digit in numeric field\n");
148    }
149  ungetc(c, f);
150  return val * sign;
151}
152
153void parse_str(FILE *f, char *buf, int maxlen)
154{
155  int c, len = 0;
156
157  while ((c = getc(f)) != EOF && c != SEP_CHAR && c != '\n' && len < maxlen)
158    {
159      if (c == '\\')
160        {
161          c = getc(f);
162          if (isdigit(c))
163            {
164              /* Expect three-digit octal number.. */
165              int c1, c2;
166              c1 = getc(f);
167              c2 = getc(f);
168              if (!isdigit(c1) || !isdigit(c2))
169                punt("Broken \\###");
170              /* Convert to ASCII code: */
171              *buf++ =  ((c - '0') << 6) + ((c1 - '0') << 3) + c2 - '0';
172              len++;
173            }
174          else if (c == '\\' || c == SEP_CHAR)
175            {
176              *buf++ = c;
177              len++;
178            }
179          else
180            punt ("Broken '\\'");
181        }
182      else
183        {
184          *buf++ = c;
185          len++;
186        }
187    }
188  *buf = '\0';
189  if (c == EOF)
190    return;
191
192  if (c != EOF && c != SEP_CHAR && c != '\n')
193    {
194      fprintf(stderr, "Field too wide, truncated\n");
195      while ((c = getc(f)) != EOF && c != SEP_CHAR && c != '\n')
196        ;
197      ungetc(c, f);
198    }
199  else
200    ungetc(c, f);
201}
202
203void parse_sep(FILE *f)
204{
205  if (getc(f) != SEP_CHAR)
206    punt("Expected Separator");
207}
208
209void parse_nl(FILE *f)
210{
211  if (getc(f) != '\n')
212    punt("Expected newline");
213}
214
215FILE *open_file(char *prefix, char *suffix)
216{
217  char name[BUFSIZ];
218  int fd;
219  FILE *f;
220
221  EXEC SQL COMMIT WORK;
222
223  strcpy(name, prefix);
224  strcat(name, suffix);
225
226  fd = open(name, O_RDONLY, 0);
227  if (fd < 0)
228    punt(name);
229  f = fdopen(fd, "r");
230  if (!f)
231    {
232      fprintf(stderr, "fdopen of ");
233      punt(name);
234    }
235  fprintf(stderr, "Working on %s\n", name);
236  return f;
237}
Note: See TracBrowser for help on using the repository browser.