2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these libraries and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /*%% (c) Copyright 1993, 1994 Hewlett-Packard Company */
24 /*%% (c) Copyright 1993, 1994 International Business Machines Corp. */
25 /*%% (c) Copyright 1993, 1994 Sun Microsystems, Inc. */
26 /*%% (c) Copyright 1993, 1994 Novell, Inc. */
27 /*%% $TOG: isrepair.c /main/5 1998/04/10 08:04:42 mgreess $ */
28 /* @(#)isrepair.c 1.7 93/07/30
29 * Copyright (c) 1988, 1993 by Sun Microsystems, Inc.
36 * Repair an ISAM file.
40 #include "isam_impl.h"
48 * err = isrepair(isfname, verbose)
50 * isrepair repairs an ISAM file.
52 * The algorithm used is as the following:
53 * 1. Read the control page of the possibly damaged file. We assume
54 * that the control page is not damaged.
55 * 2. Open a new ISAM file with ~ suffix.
56 * 3. Scan .rec file (and .var file if variable length records file)
57 * retrieve all records not marked as deleted, and write them
59 * 4. Delete the old ISAM file.
60 * 5. Rename ~ file to the original file name.
61 * 6. Build all indexes.
63 * verbose option (if set to nonzero) will print messages to stdout.
66 Static char *rp_readrecord_v(), *rp_readrecord_f();
67 Static int printkey(int, struct keydesc *, int (*)(const char *, ...));
68 Static void cmd_error(const char *, int (*)(const char *, ...));
69 Static int typeletter();
70 Static int rp_readcntlpg();
71 static int isnoprintf(const char *, ...);
73 int isrepair(char *isfname, int verbose)
75 extern char *rp_readrecord_v(), *rp_readrecord_f();
76 char cntlpg[ISCNTLSIZE];
77 int datfd = -1, indfd = -1, varfd = -1;
78 int minreclen, maxreclen;
79 int nrecords_fromcntl;
81 char nameBuf [MAXPATHLEN];
84 struct keydesc2 keydesc2;
86 long offset, recfile_end;
89 int nrecords_found, diff;
91 struct keydesc keydesc;
92 int (*print)(const char *, ...);
99 print = verbose ? printf : isnoprintf;
100 datfd = indfd = varfd = -1;
105 if (strlen(isfname) + 10 >= MAXPATHLEN)
106 namebuf = (char*) malloc(strlen(isfname) + 10);
110 (void)strcpy(namebuf, isfname);
111 _makedat_isfname(namebuf);
112 datfd = open(namebuf, O_RDONLY);
114 if(fcntl(datfd, F_SETFD, 1) == -1) {
120 (void)strcpy(namebuf, isfname);
121 _makeind_isfname(namebuf);
122 indfd = open(namebuf, O_RDONLY);
124 if(fcntl(indfd, F_SETFD, 1) == -1) {
130 (void)strcpy(namebuf, isfname);
131 _makevar_isfname(namebuf);
132 varfd = open(namebuf, O_RDONLY);
134 if(fcntl(varfd, F_SETFD, 1) == -1) {
140 (void)print("Reading control page from %s.rec file...\n",
142 if (rp_readcntlpg(datfd, cntlpg) == ISERROR) {
143 (void)print("Cannot read the control page\n");
148 * Check magic. Repair only ISAM files!!!
151 if (strncmp(cntlpg + CP_MAGIC_OFF, ISMAGIC, strlen(ISMAGIC)) != 0) {
152 (void)print("Bad magic in %s.rec\n", isfname);
156 varflag = ldint(cntlpg + CP_VARFLAG_OFF);
157 minreclen = ldint(cntlpg + CP_MINRECLEN_OFF);
158 maxreclen = ldint(cntlpg + CP_MAXRECLEN_OFF);
161 * Check for maxreclen field value of -1. This could have occurred due to
162 * ISMAXRECLEN being incorrectly set to 65535 in an earlier version. If
163 * this field is -1 and it's a variable length record, reset to the new
164 * value of MAXRECLEN. This means that this field will be repaired when
165 * the control block is written back to disk.
168 if (maxreclen == -1 && varflag) {
169 maxreclen = ISMAXRECLEN;
172 lastrecno = ldlong(cntlpg + CP_LASTRECNO_OFF);
173 nrecords_fromcntl = ldlong(cntlpg + CP_NRECORDS_OFF);
176 * Open output file. Use ~ as suffix.
178 (void)sprintf(namebuf, "%s~", isfname);
179 (void)print("Opening temporary ISAM file '%s'...\n",
181 isreclen = minreclen;
182 if ((isfd = isbuild(namebuf, maxreclen, nokey, ISINOUT + ISEXCLLOCK +
183 (varflag?ISVARLEN:0))) == ISERROR) {
184 (void)print("Cannot open temporary ISAM file %s\n",
186 if (iserrno == EEXIST)
187 (void)print("File %s already exists\n", namebuf);
192 * Scan .rec file and read all undeleted records.
194 (void)print("Salvaging records from %s.rec%s file...\n",
195 isfname, varflag?" (and .var file)" : "");
198 recfile_end = lseek(datfd, 0L, 2);
202 while (recnum <= lastrecno && offset < recfile_end - minreclen) {
205 prec = rp_readrecord_v(datfd, varfd, offset, minreclen, maxreclen);
206 offset += minreclen + LONGSIZE;
209 prec = rp_readrecord_f(datfd, offset, minreclen);
210 offset += minreclen + 1;
214 if (iswrrec(isfd, recnum, prec) == ISERROR) {
215 cmd_error("iswrrec", print);
223 diff = nrecords_found - nrecords_fromcntl;
226 (void)print("All records found - total %d records\n",
229 (void)print("%d records found - %d records %s than in header\n",
230 nrecords_found, diff, diff > 0 ?
234 * Close all file descriptors.
250 (void) sigfillset(&allsignals);
251 (void) sigprocmask(SIG_SETMASK, &allsignals, &oldmask);
253 (void)print("Erasing ISAM file '%s'...\n", isfname);
254 /* if (iserase(isfname) != ISOK) {
255 cmd_error("iserase", print);
259 if (strlen(isfname) + 5 >= MAXPATHLEN)
260 buffer = (char*) malloc(strlen(isfname) + 5);
264 (void)sprintf(buffer,"%s.rec", isfname);
265 (void)unlink(buffer);
266 (void)sprintf(buffer,"%s.ind", isfname);
267 (void)unlink(buffer);
268 (void)sprintf(buffer,"%s.var", isfname);
269 (void)unlink(buffer);
271 (void)sprintf(namebuf, "%s~", isfname);
272 (void)print("Renaming ISAM file '%s' to '%s'...\n",
274 if (isrename(namebuf, isfname) != ISOK) {
275 cmd_error("isrename", print);
280 * Re-open the file and add keys.
282 if (ldshort(cntlpg + CP_NKEYS_OFF) > 0) {
283 (void)print("Adding keys...\n");
285 if ((isfd = isopen(isfname, ISEXCLLOCK + ISINOUT +
286 (varflag?ISVARLEN:0))) == ISERROR) {
287 cmd_error("isopen", print);
291 for (i = 0; i < ldshort(cntlpg + CP_NKEYS_OFF); i++) {
292 ldkey(&keydesc2, cntlpg + CP_KEYS_OFF + i * K2_LEN);
293 _iskey_itox(&keydesc2, &keydesc);
295 if (keydesc.k_nparts == 0) /* special case for no primary */
298 printkey (i+1, &keydesc, print);
301 if (isaddprimary(isfd, &keydesc) == ISERROR) {
302 cmd_error("isaddprimary", print);
308 if (isaddindex(isfd, &keydesc) == ISERROR) {
309 cmd_error("isaddindex", print);
317 (void)sigprocmask(SIG_SETMASK, &oldmask, NULL);
319 print("...File repaired\n");
320 if (buffer != Buffer) free(buffer);
324 (void)print("\007Didn't repair ISAM file '%s'\n", isfname);
335 if (namebuf != nameBuf) free(namebuf);
336 if (buffer != Buffer) free(buffer);
341 /******* low level data access used by the 'repair' utility ******************/
343 static char recordbuffer[ISMAXRECLEN + LONGSIZE];
345 /* rp_readcntlpg() - Read the control page */
347 rp_readcntlpg(int datfd, char *cntlpg)
349 if (read (datfd, cntlpg, ISCNTLSIZE) != ISCNTLSIZE)
355 /* rp_readrecord_f() - Read a record from .rec file */
357 rp_readrecord_f(int datfd, long offset, int reclen)
359 if (lseek(datfd, offset, 0) != offset)
360 return ((char *) NULL);
362 if (read(datfd, recordbuffer, reclen + 1) != (reclen + 1))
363 return ((char *) NULL);
365 if (recordbuffer[0] == FL_RECDELETED)
366 return ((char *) NULL);
368 return (recordbuffer + 1);
371 /* rp_readrecord_v() - Read a record from .rec file */
373 rp_readrecord_v(int datfd, int varfd, long offset, int minreclen, int maxreclen)
376 char frameheadbuf [2 * SHORTSIZE];
379 if (lseek(datfd, offset, 0) != offset)
380 return ((char *) NULL);
382 if (read(datfd, recordbuffer, minreclen + LONGSIZE) != (minreclen + LONGSIZE))
383 return ((char *) NULL);
385 if ((tailoff = ldlong(recordbuffer)) == VL_RECDELETED)
386 return ((char *) NULL);
388 isreclen = minreclen;
390 /* Recover tail of the record */
391 if (tailoff != VL_RECNOTAIL) {
393 if (lseek(varfd, tailoff, 0) != tailoff)
396 if (read(varfd, frameheadbuf, 2 * SHORTSIZE) != 2 * SHORTSIZE)
399 taillen = (int) ldshort(frameheadbuf + VR_TAILLEN_OFF);
401 if (taillen <= 0 || taillen + minreclen > maxreclen)
404 if (read(varfd, recordbuffer + LONGSIZE + isreclen, taillen) != taillen)
411 return (recordbuffer + LONGSIZE);
415 isnoprintf(const char *s, ...)
421 printkey(int n, struct keydesc *pkdesc, int (*print)(const char *, ...))
426 if (pkdesc->k_nparts == 0) {
427 print("%3d: --- NO PRIMARY KEY ---\n", n);
432 print("P%3d: %s ", n, (pkdesc->k_flags&ISDUPS) ?
435 print(" %3d: %s ", n, (pkdesc->k_flags&ISDUPS) ?
439 for (i = 0; i < pkdesc->k_nparts; i++) {
440 pk = pkdesc->k_part + i;
441 print(" %d%c%d%s", pk->kp_start,
442 typeletter(pk->kp_type & ~ISDESC), pk->kp_leng,
443 (pk->kp_type & ISDESC)?"D":" ");
450 cmd_error(const char *str, int (*print)(const char *, ...))
452 (void)print("%s: ISAM error %d\n", str, iserrno);