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
41 #include <X11/Xos_r.h>
45 #include "cmscalendar.h"
51 static CSA_return_code dump_error;
55 * forward declaration of functions used within this file
57 static boolean_t magic_time(time_t t);
58 static boolean_t visit1(caddr_t node, caddr_t d);
59 static boolean_t visit2(caddr_t node, caddr_t d);
60 static void print_file_error(char *file, char *msg);
63 _DtCmsCollectOne(_DtCmsCalendar *cal)
67 char *bak, *temp, *clog;
72 status = rb_check_tree(APPT_TREE(cal));
73 clog = _DtCmsGetLogFN(cal->calendar);
74 temp = _DtCmsGetTmpFN(cal->calendar);
75 bak = _DtCmsGetBakFN(cal->calendar);
77 if (status != rb_ok || clog==NULL || temp==NULL || bak==NULL) {
78 fprintf(stderr, "%s: cannot acquire files to execute garbage collection.\n", pgname);
79 fprintf(stderr, "possible causes: cannot find home directory.\n");
80 fprintf(stderr, "\tNIS or your host server might be down.\n");
81 fprintf(stderr, "damage: none\n\n");
85 /* Make sure that the temp file does not exist before garbage collect */
87 if (cal->fversion == _DtCMS_VERSION1)
88 stat = _DtCmsCreateLogV1(cal->owner, temp);
90 stat = _DtCmsCreateLogV2(cal->owner, temp);
92 if (stat != CSA_SUCCESS) {
93 if (stat == (CSA_X_DT_E_BACKING_STORE_PROBLEM))
94 print_file_error(temp,
95 "file error during garbage collection");
97 fprintf(stderr, "%s: file error on %s during garbage collection\n",
99 fprintf(stderr, "Reason: getpwnam() failed. %s%s\n",
100 "No passwd entry for owner of ",
103 fprintf(stderr, "damage: none\n\n");
108 if (cal->fversion == _DtCMS_VERSION1)
109 stat = _DtCmsDumpDataV1(temp, cal);
111 stat = _DtCmsDumpDataV2(temp, cal);
113 if (stat != CSA_SUCCESS) {
114 print_file_error(temp,
115 (stat == (CSA_X_DT_E_BACKING_STORE_PROBLEM) ?
116 "file error during garbage collection" :
117 "can't dump data structure to file during garbage collection"));
121 /* mv -f .callog .calbak; mv -f temp .callog */
122 if (rename (clog, bak) < 0) {
123 perror ("rpc.cmsd: Can't backup callog to .calbak.\nreason:");
127 if (rename (temp, clog) < 0) {
128 perror("rpc.cmsd: Can't move .caltemp to callog.\nreason:");
129 fprintf(stderr, "%s: you may recover %s from %s.\n", pgname,
148 extern CSA_return_code
149 _DtCmsDumpDataV1(char *file, _DtCmsCalendar *cal)
151 CSA_return_code stat;
153 dump_error = CSA_SUCCESS;
155 /* Keep the temp log file open during garbage collection. */
156 if ((fd = open(file, O_WRONLY | O_APPEND | O_SYNC)) < 0)
158 return (CSA_X_DT_E_BACKING_STORE_PROBLEM);
161 if ((stat = _DtCmsAppendAccessByFD(fd, access_read_4,
162 GET_R_ACCESS(cal))) != CSA_SUCCESS)
165 if ((stat = _DtCmsAppendAccessByFD(fd, access_write_4,
166 GET_W_ACCESS(cal))) != CSA_SUCCESS)
169 if ((stat = _DtCmsAppendAccessByFD (fd, access_delete_4,
170 GET_D_ACCESS(cal))) != CSA_SUCCESS)
173 if ((stat = _DtCmsAppendAccessByFD (fd, access_exec_4,
174 GET_X_ACCESS(cal))) != CSA_SUCCESS)
177 _DtCmsEnumerateUp(cal, visit1); /* dump the tree */
179 if (close(fd) == EOF)
180 return (CSA_X_DT_E_BACKING_STORE_PROBLEM);
185 extern CSA_return_code
186 _DtCmsDumpDataV2(char *file, _DtCmsCalendar *cal)
188 CSA_return_code stat;
190 dump_error = CSA_SUCCESS;
192 /* Keep the temp log file open during garbage collection. */
193 if ((fd = open(file, O_WRONLY | O_APPEND | O_SYNC)) < 0)
195 return (CSA_X_DT_E_BACKING_STORE_PROBLEM);
198 /* dump calendar attributes */
199 if (cal->num_attrs > 0) {
200 if ((stat = _DtCmsAppendCalAttrsByFD(fd, cal->num_attrs,
201 cal->attrs)) != CSA_SUCCESS)
205 if ((stat = _DtCmsAppendHTableByFD(fd, cal->entry_tbl->size,
206 cal->entry_tbl->names, cal->types)) != CSA_SUCCESS)
209 _DtCmsEnumerateUp(cal, visit2); /* dump the tree */
211 if (close(fd) == EOF)
212 return (CSA_X_DT_E_BACKING_STORE_PROBLEM);
220 boolean_t magic = B_FALSE;
222 _Xltimeparams localtime_buf;
224 tm = _XLocaltime(&t, localtime_buf);
226 if (tm->tm_hour == 3 && tm->tm_min == 41)
233 visit1(caddr_t node, caddr_t d)
235 boolean_t stop = B_FALSE;
237 switch (((Appt_4 *) d)->tag->tag)
241 * otherTags = events read in from files.
242 * Don't write to log.
248 * a little hack to get us off the
249 * totally hokey magic-time business.
251 if (magic_time(((Appt_4 *)d)->appt_id.tick)) {
252 ((Appt_4 *)d)->tag->showtime = 0;
257 if ((dump_error = _DtCmsAppendAppt4ByFD(fd, (Appt_4 *)d, _DtCmsLogAdd)) != CSA_SUCCESS)
264 visit2(caddr_t node, caddr_t d)
266 boolean_t stop = B_FALSE;
268 if ((dump_error = _DtCmsAppendEntryByFD(fd, (cms_entry *)d,
269 _DtCmsLogAdd)) != CSA_SUCCESS)
276 print_file_error(char *file, char *msg)
279 fprintf(stderr, "%s: (%s)%s\n", pgname, file, msg);
281 fprintf(stderr, "%s: %s\n", pgname, msg);
282 fprintf(stderr, "possible causes: %s, %s, %s, %s\n",
283 "host server is down", "disk is full", "out of memory",
284 "file protections have changed.");
285 fprintf(stderr, "damage: none\n\n");