Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / programs / dtcm / server / garbage.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 librararies 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 /* $XConsortium: garbage.c /main/6 1996/11/21 19:44:58 drk $ */
24 /*
25  *  (c) Copyright 1993, 1994 Hewlett-Packard Company
26  *  (c) Copyright 1993, 1994 International Business Machines Corp.
27  *  (c) Copyright 1993, 1994 Novell, Inc.
28  *  (c) Copyright 1993, 1994 Sun Microsystems, Inc.
29  */
30
31 #include        <EUSCompat.h>
32 #include        <stdio.h>
33 #include        <fcntl.h>
34 #include        <unistd.h>
35 #include        <stdlib.h>
36 #define XOS_USE_NO_LOCKING
37 #define X_INCLUDE_TIME_H
38 #include <X11/Xos_r.h>
39 #include        "log.h"
40 #include        "rtable4.h"
41 #include        "cm.h"
42 #include        "cmscalendar.h"
43 #include        "tree.h"
44 #include        "garbage.h"
45
46 extern char *pgname;
47
48 static CSA_return_code dump_error;
49 static int fd;
50
51 /*
52  * forward declaration of functions used within this file
53  */
54 static boolean_t magic_time(time_t t);
55 static boolean_t visit1(caddr_t node, caddr_t d);
56 static boolean_t visit2(caddr_t node, caddr_t d);
57 static void print_file_error(char *file, char *msg);
58
59 extern void
60 _DtCmsCollectOne(_DtCmsCalendar *cal)
61 {
62         Rb_Status       status;
63         CSA_return_code         stat;
64         char *bak, *temp, *clog;
65
66         if (cal == NULL)
67                 return;
68
69         status  = rb_check_tree(APPT_TREE(cal));
70         clog    = _DtCmsGetLogFN(cal->calendar);
71         temp    = _DtCmsGetTmpFN(cal->calendar);
72         bak     = _DtCmsGetBakFN(cal->calendar);
73
74         if (status != rb_ok || clog==NULL || temp==NULL || bak==NULL) {
75                 fprintf(stderr, "%s: cannot acquire files to execute garbage collection.\n", pgname);
76                 fprintf(stderr, "possible causes: cannot find home directory.\n");
77                 fprintf(stderr, "\tNIS or your host server might be down.\n");
78                 fprintf(stderr, "damage: none\n\n");
79                 goto cleanup;
80         }
81
82         /* Make sure that the temp file does not exist before garbage collect */
83         unlink (temp);
84         if (cal->fversion == _DtCMS_VERSION1)
85                 stat = _DtCmsCreateLogV1(cal->owner, temp);
86         else
87                 stat = _DtCmsCreateLogV2(cal->owner, temp);
88
89         if (stat != CSA_SUCCESS) {
90                 if (stat == (CSA_X_DT_E_BACKING_STORE_PROBLEM))
91                         print_file_error(temp,
92                                 "file error during garbage collection");
93                 else {
94                         fprintf(stderr, "%s: file error on %s during garbage collection\n",
95                                 pgname, temp);
96                         fprintf(stderr, "Reason: getpwnam() failed. %s%s\n",
97                                 "No passwd entry for owner of ",
98                                 cal->calendar);
99
100                         fprintf(stderr, "damage: none\n\n");
101                 }
102                 goto cleanup;
103         }
104
105         if (cal->fversion == _DtCMS_VERSION1)
106                 stat = _DtCmsDumpDataV1(temp, cal);
107         else
108                 stat = _DtCmsDumpDataV2(temp, cal);
109
110         if (stat != CSA_SUCCESS) {
111                 print_file_error(temp,
112                         (stat == (CSA_X_DT_E_BACKING_STORE_PROBLEM) ?
113                          "file error during garbage collection" :
114                          "can't dump data structure to file during garbage collection"));
115                 goto cleanup;
116         }
117
118         /* mv -f .callog .calbak; mv -f temp .callog */
119         if (rename (clog, bak) < 0) {
120                 perror ("rpc.cmsd: Can't backup callog to .calbak.\nreason:");
121                 goto cleanup;
122         }
123
124         if (rename (temp, clog) < 0) {
125                 perror("rpc.cmsd: Can't move .caltemp to callog.\nreason:");
126                 fprintf(stderr, "%s: you may recover %s from %s.\n", pgname,
127                         clog, bak);
128         }
129
130 cleanup:
131         if (bak != NULL) {
132                 free(bak);
133                 bak = NULL;
134         }
135         if (temp != NULL) {
136                 free(temp);
137                 temp = NULL;
138         }
139         if (clog != NULL) {
140                 free(clog);
141                 clog = NULL;
142         }
143 }
144
145 extern CSA_return_code
146 _DtCmsDumpDataV1(char *file, _DtCmsCalendar *cal)
147 {
148         CSA_return_code stat;
149
150         dump_error = CSA_SUCCESS;
151
152         /* Keep the temp log file open during garbage collection. */
153         if ((fd = open(file, O_WRONLY | O_APPEND | O_SYNC)) < 0)
154         {
155                 return (CSA_X_DT_E_BACKING_STORE_PROBLEM);
156         }
157
158         if ((stat = _DtCmsAppendAccessByFD(fd, access_read_4,
159             GET_R_ACCESS(cal))) != CSA_SUCCESS)
160                 return (stat);
161
162         if ((stat = _DtCmsAppendAccessByFD(fd, access_write_4,
163             GET_W_ACCESS(cal))) != CSA_SUCCESS)
164                 return (stat);
165
166         if ((stat = _DtCmsAppendAccessByFD (fd, access_delete_4,
167             GET_D_ACCESS(cal))) != CSA_SUCCESS)
168                 return (stat);
169
170         if ((stat = _DtCmsAppendAccessByFD (fd, access_exec_4,
171             GET_X_ACCESS(cal))) != CSA_SUCCESS)
172                 return (stat);
173
174         _DtCmsEnumerateUp(cal, visit1); /* dump the tree */
175
176         if (close(fd) == EOF)
177                 return (CSA_X_DT_E_BACKING_STORE_PROBLEM);
178
179         return (dump_error);
180 }
181
182 extern CSA_return_code
183 _DtCmsDumpDataV2(char *file, _DtCmsCalendar *cal)
184 {
185         CSA_return_code stat;
186
187         dump_error = CSA_SUCCESS;
188
189         /* Keep the temp log file open during garbage collection. */
190         if ((fd = open(file, O_WRONLY | O_APPEND | O_SYNC)) < 0)
191         {
192                 return (CSA_X_DT_E_BACKING_STORE_PROBLEM);
193         }
194
195         /* dump calendar attributes */
196         if (cal->num_attrs > 0) {
197                 if ((stat = _DtCmsAppendCalAttrsByFD(fd, cal->num_attrs,
198                     cal->attrs)) != CSA_SUCCESS)
199                         return (stat);
200         }
201
202         if ((stat = _DtCmsAppendHTableByFD(fd, cal->entry_tbl->size,
203             cal->entry_tbl->names, cal->types)) != CSA_SUCCESS)
204                 return (stat);
205
206         _DtCmsEnumerateUp(cal, visit2); /* dump the tree */
207
208         if (close(fd) == EOF)
209                 return (CSA_X_DT_E_BACKING_STORE_PROBLEM);
210
211         return (dump_error);
212 }
213
214 static boolean_t
215 magic_time(time_t t)
216 {
217         boolean_t magic = B_FALSE;
218         struct tm *tm;
219         _Xltimeparams localtime_buf;
220
221         tm = _XLocaltime(&t, localtime_buf);
222
223         if (tm->tm_hour == 3 && tm->tm_min == 41)
224                  magic = B_TRUE;
225
226         return(magic);
227 }
228
229 static boolean_t
230 visit1(caddr_t node, caddr_t d)
231 {
232         boolean_t stop = B_FALSE;
233
234         switch (((Appt_4 *) d)->tag->tag)
235         {
236         case otherTag_4:
237                 /*
238                  * otherTags = events read in from files.
239                  * Don't write to log.
240                  */
241                 return(stop);
242
243         case appointment_4:
244                 /*
245                  * a little hack to get us off the
246                  * totally hokey magic-time business.
247                  */
248                 if (magic_time(((Appt_4 *)d)->appt_id.tick)) {
249                         ((Appt_4 *)d)->tag->showtime = 0;
250                 }
251                 break;
252         }
253
254         if ((dump_error = _DtCmsAppendAppt4ByFD(fd, (Appt_4 *)d, _DtCmsLogAdd)) != CSA_SUCCESS)
255                 stop = B_TRUE;
256
257         return(stop);
258 }
259
260 static boolean_t
261 visit2(caddr_t node, caddr_t d)
262 {
263         boolean_t stop = B_FALSE;
264
265         if ((dump_error = _DtCmsAppendEntryByFD(fd, (cms_entry *)d,
266             _DtCmsLogAdd)) != CSA_SUCCESS)
267                 stop = B_TRUE;
268
269         return(stop);
270 }
271
272 static void
273 print_file_error(char *file, char *msg)
274 {
275         if (file)
276                 fprintf(stderr, "%s: (%s)%s\n", pgname, file, msg);
277         else
278                 fprintf(stderr, "%s: %s\n", pgname, msg);
279         fprintf(stderr, "possible causes: %s, %s, %s, %s\n",
280                 "host server is down", "disk is full", "out of memory",
281                 "file protections have changed.");
282         fprintf(stderr, "damage: none\n\n");
283 }
284
285