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 librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /* $XConsortium: garbage.c /main/6 1996/11/21 19:44:58 drk $ */
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.
31 #include <EUSCompat.h>
36 #define XOS_USE_NO_LOCKING
37 #define X_INCLUDE_TIME_H
38 #include <X11/Xos_r.h>
42 #include "cmscalendar.h"
48 static CSA_return_code dump_error;
52 * forward declaration of functions used within this file
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);
60 _DtCmsCollectOne(_DtCmsCalendar *cal)
64 char *bak, *temp, *clog;
69 status = rb_check_tree(APPT_TREE(cal));
70 clog = _DtCmsGetLogFN(cal->calendar);
71 temp = _DtCmsGetTmpFN(cal->calendar);
72 bak = _DtCmsGetBakFN(cal->calendar);
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");
82 /* Make sure that the temp file does not exist before garbage collect */
84 if (cal->fversion == _DtCMS_VERSION1)
85 stat = _DtCmsCreateLogV1(cal->owner, temp);
87 stat = _DtCmsCreateLogV2(cal->owner, temp);
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");
94 fprintf(stderr, "%s: file error on %s during garbage collection\n",
96 fprintf(stderr, "Reason: getpwnam() failed. %s%s\n",
97 "No passwd entry for owner of ",
100 fprintf(stderr, "damage: none\n\n");
105 if (cal->fversion == _DtCMS_VERSION1)
106 stat = _DtCmsDumpDataV1(temp, cal);
108 stat = _DtCmsDumpDataV2(temp, cal);
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"));
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:");
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,
145 extern CSA_return_code
146 _DtCmsDumpDataV1(char *file, _DtCmsCalendar *cal)
148 CSA_return_code stat;
150 dump_error = CSA_SUCCESS;
152 /* Keep the temp log file open during garbage collection. */
153 if ((fd = open(file, O_WRONLY | O_APPEND | O_SYNC)) < 0)
155 return (CSA_X_DT_E_BACKING_STORE_PROBLEM);
158 if ((stat = _DtCmsAppendAccessByFD(fd, access_read_4,
159 GET_R_ACCESS(cal))) != CSA_SUCCESS)
162 if ((stat = _DtCmsAppendAccessByFD(fd, access_write_4,
163 GET_W_ACCESS(cal))) != CSA_SUCCESS)
166 if ((stat = _DtCmsAppendAccessByFD (fd, access_delete_4,
167 GET_D_ACCESS(cal))) != CSA_SUCCESS)
170 if ((stat = _DtCmsAppendAccessByFD (fd, access_exec_4,
171 GET_X_ACCESS(cal))) != CSA_SUCCESS)
174 _DtCmsEnumerateUp(cal, visit1); /* dump the tree */
176 if (close(fd) == EOF)
177 return (CSA_X_DT_E_BACKING_STORE_PROBLEM);
182 extern CSA_return_code
183 _DtCmsDumpDataV2(char *file, _DtCmsCalendar *cal)
185 CSA_return_code stat;
187 dump_error = CSA_SUCCESS;
189 /* Keep the temp log file open during garbage collection. */
190 if ((fd = open(file, O_WRONLY | O_APPEND | O_SYNC)) < 0)
192 return (CSA_X_DT_E_BACKING_STORE_PROBLEM);
195 /* dump calendar attributes */
196 if (cal->num_attrs > 0) {
197 if ((stat = _DtCmsAppendCalAttrsByFD(fd, cal->num_attrs,
198 cal->attrs)) != CSA_SUCCESS)
202 if ((stat = _DtCmsAppendHTableByFD(fd, cal->entry_tbl->size,
203 cal->entry_tbl->names, cal->types)) != CSA_SUCCESS)
206 _DtCmsEnumerateUp(cal, visit2); /* dump the tree */
208 if (close(fd) == EOF)
209 return (CSA_X_DT_E_BACKING_STORE_PROBLEM);
217 boolean_t magic = B_FALSE;
219 _Xltimeparams localtime_buf;
221 tm = _XLocaltime(&t, localtime_buf);
223 if (tm->tm_hour == 3 && tm->tm_min == 41)
230 visit1(caddr_t node, caddr_t d)
232 boolean_t stop = B_FALSE;
234 switch (((Appt_4 *) d)->tag->tag)
238 * otherTags = events read in from files.
239 * Don't write to log.
245 * a little hack to get us off the
246 * totally hokey magic-time business.
248 if (magic_time(((Appt_4 *)d)->appt_id.tick)) {
249 ((Appt_4 *)d)->tag->showtime = 0;
254 if ((dump_error = _DtCmsAppendAppt4ByFD(fd, (Appt_4 *)d, _DtCmsLogAdd)) != CSA_SUCCESS)
261 visit2(caddr_t node, caddr_t d)
263 boolean_t stop = B_FALSE;
265 if ((dump_error = _DtCmsAppendEntryByFD(fd, (cms_entry *)d,
266 _DtCmsLogAdd)) != CSA_SUCCESS)
273 print_file_error(char *file, char *msg)
276 fprintf(stderr, "%s: (%s)%s\n", pgname, file, msg);
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");