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: isgarbage.c /main/5 1998/04/10 08:04:25 mgreess $ */
28 #include "isam_impl.h"
30 #include <sys/types.h>
34 isgarbage(char * isfname)
36 char isfname2[MAXPATHLEN];
37 int isfd = -1, isfd2 = -1;
38 char buffer[ISMAXRECLEN];
41 struct keydesc keybuf;
45 snprintf(isfname2, sizeof(isfname2), "%s~", isfname);
47 if ((isfd = isopen(isfname, ISEXCLLOCK + ISINPUT)) == ISERROR) {
51 if (isindexinfo(isfd, (struct keydesc *)&info, 0) == ISERROR) {
55 if (strlen(isfname) + 5 >= ISMAXRECLEN)
56 recbuf = (char*) malloc(strlen(isfname) + 5);
60 sprintf(recbuf, "%s.rec", isfname);
61 if (stat(recbuf, &statbuf) < 0) {
65 iserase(isfname2); /* Delete any old backup (~) files. */
67 if ((isfd2 = isbuild(isfname2, info.di_recsize, nokey,
68 (DICTVARLENBIT&info.di_nkeys?ISVARLEN: ISFIXLEN)
69 + ISEXCLLOCK + ISINOUT)) == ISERROR) {
73 /* Copy all records */
75 while (isread(isfd, recbuf, ISNEXT) == ISOK) {
76 iswrite(isfd2, recbuf);
80 if (count != info.di_nrecords) {
84 /* Build all indexes from index info */
86 for (i = 1; i <= (info.di_nkeys & DICTNKEYSMASK); i++) {
87 if (isindexinfo(isfd, &keybuf, i) == ISERROR) {
90 if (i == 1 && keybuf.k_nparts != 0) {
91 /* Add primary index */
92 if (isaddprimary(isfd2, &keybuf) == ISERROR) {
97 /* Add secondary index */
98 if (isaddindex(isfd2, &keybuf) == ISERROR) {
105 (void)isclose(isfd2);
107 iserase(isfname); /* cannot abort at this point ! */
108 if (isrename(isfname2, isfname) == ISERROR) {
120 (void)isclose(isfd2);
121 (void)iserase(isfname2);
123 if ((recbuf != buffer) && (recbuf != NULL)) {