Merge branch 'master' into cde-next
[oweals/cde.git] / cde / lib / tt / mini_isam / isgarbage.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
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)
10  * any later version.
11  *
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
16  * details.
17  *
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
22  */
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"
29 #include <stdlib.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32
33 int
34 isgarbage(char * isfname)
35 {
36         char                    isfname2[MAXPATHLEN];
37         int                     isfd = -1, isfd2 = -1;
38         char                    buffer[ISMAXRECLEN];
39         char                    *recbuf = NULL;
40         struct dictinfo         info;
41         struct keydesc          keybuf;
42         struct stat             statbuf;
43         int                     count = 0,i;
44
45         snprintf(isfname2, sizeof(isfname2), "%s~", isfname);
46
47         if ((isfd = isopen(isfname, ISEXCLLOCK + ISINPUT)) == ISERROR) {
48                 goto ERROR;
49         }
50
51         if (isindexinfo(isfd, (struct keydesc *)&info, 0) == ISERROR) {
52                 goto ERROR;
53         }
54
55         if (strlen(isfname) + 5 >= ISMAXRECLEN)
56           recbuf = (char*) malloc(strlen(isfname) + 5);
57         else
58           recbuf = buffer;
59
60         sprintf(recbuf, "%s.rec", isfname);
61         if (stat(recbuf, &statbuf) < 0) {
62                 goto ERROR;
63         }
64
65         iserase(isfname2);      /* Delete any old backup (~) files. */
66
67         if ((isfd2 = isbuild(isfname2, info.di_recsize, nokey,
68                      (DICTVARLENBIT&info.di_nkeys?ISVARLEN: ISFIXLEN)
69                              + ISEXCLLOCK + ISINOUT)) == ISERROR) {
70                 goto ERROR;
71         }
72
73         /* Copy all records */
74
75         while (isread(isfd, recbuf, ISNEXT) == ISOK) {
76                 iswrite(isfd2, recbuf);
77                 count++;
78         }
79
80         if (count != info.di_nrecords) {
81                 goto ERROR;
82         }
83         
84         /* Build all indexes from index info */
85
86         for (i = 1; i <= (info.di_nkeys & DICTNKEYSMASK); i++) {
87                 if (isindexinfo(isfd, &keybuf, i) == ISERROR) {
88                         goto ERROR;
89                 }
90                 if (i == 1 && keybuf.k_nparts != 0) {
91                         /* Add primary index */
92                         if (isaddprimary(isfd2, &keybuf) == ISERROR) {
93                                 goto ERROR;
94                         }
95                 } 
96                 else if (i > 1) {
97                         /* Add secondary index */
98                         if (isaddindex(isfd2, &keybuf) == ISERROR) {
99                                 goto ERROR;
100                         }
101                 }
102         }
103
104         (void)isclose(isfd);
105         (void)isclose(isfd2);
106
107         iserase(isfname);               /* cannot abort at this point ! */
108         if (isrename(isfname2, isfname) == ISERROR) {
109                 return (ISERROR);
110         }
111
112         return (ISOK);
113
114       ERROR:
115
116         if (isfd != -1)
117           (void)isclose(isfd);
118
119         if (isfd2 != -1) {
120                 (void)isclose(isfd2);
121                 (void)iserase(isfname2);
122         }
123         if ((recbuf != buffer) && (recbuf != NULL)) {
124                 free(recbuf);
125         }
126         return (ISERROR);
127 }
128