1 /* $XConsortium: garbage.c /main/6 1996/11/21 19:44:58 drk $ */
3 * (c) Copyright 1993, 1994 Hewlett-Packard Company
4 * (c) Copyright 1993, 1994 International Business Machines Corp.
5 * (c) Copyright 1993, 1994 Novell, Inc.
6 * (c) Copyright 1993, 1994 Sun Microsystems, Inc.
14 #define XOS_USE_NO_LOCKING
15 #define X_INCLUDE_TIME_H
16 #include <X11/Xos_r.h>
20 #include "cmscalendar.h"
26 static CSA_return_code dump_error;
30 * forward declaration of functions used within this file
32 static boolean_t magic_time(time_t t);
33 static boolean_t visit1(caddr_t node, caddr_t d);
34 static boolean_t visit2(caddr_t node, caddr_t d);
35 static void print_file_error(char *file, char *msg);
38 _DtCmsCollectOne(_DtCmsCalendar *cal)
42 char *bak, *temp, *clog;
47 status = rb_check_tree(APPT_TREE(cal));
48 clog = _DtCmsGetLogFN(cal->calendar);
49 temp = _DtCmsGetTmpFN(cal->calendar);
50 bak = _DtCmsGetBakFN(cal->calendar);
52 if (status != rb_ok || clog==NULL || temp==NULL || bak==NULL) {
53 fprintf(stderr, "%s: cannot acquire files to execute garbage collection.\n", pgname);
54 fprintf(stderr, "possible causes: cannot find home directory.\n");
55 fprintf(stderr, "\tNIS or your host server might be down.\n");
56 fprintf(stderr, "damage: none\n\n");
60 /* Make sure that the temp file does not exist before garbage collect */
62 if (cal->fversion == _DtCMS_VERSION1)
63 stat = _DtCmsCreateLogV1(cal->owner, temp);
65 stat = _DtCmsCreateLogV2(cal->owner, temp);
67 if (stat != CSA_SUCCESS) {
68 if (stat == (CSA_X_DT_E_BACKING_STORE_PROBLEM))
69 print_file_error(temp,
70 "file error during garbage collection");
72 fprintf(stderr, "%s: file error on %s during garbage collection\n",
74 fprintf(stderr, "Reason: getpwnam() failed. %s%s\n",
75 "No passwd entry for owner of ",
78 fprintf(stderr, "damage: none\n\n");
83 if (cal->fversion == _DtCMS_VERSION1)
84 stat = _DtCmsDumpDataV1(temp, cal);
86 stat = _DtCmsDumpDataV2(temp, cal);
88 if (stat != CSA_SUCCESS) {
89 print_file_error(temp,
90 (stat == (CSA_X_DT_E_BACKING_STORE_PROBLEM) ?
91 "file error during garbage collection" :
92 "can't dump data structure to file during garbage collection"));
96 /* mv -f .callog .calbak; mv -f temp .callog */
97 if (rename (clog, bak) < 0) {
98 perror ("rpc.cmsd: Can't backup callog to .calbak.\nreason:");
102 if (rename (temp, clog) < 0) {
103 perror("rpc.cmsd: Can't move .caltemp to callog.\nreason:");
104 fprintf(stderr, "%s: you may recover %s from %s.\n", pgname,
123 extern CSA_return_code
124 _DtCmsDumpDataV1(char *file, _DtCmsCalendar *cal)
126 CSA_return_code stat;
128 dump_error = CSA_SUCCESS;
130 /* Keep the temp log file open during garbage collection. */
131 if ((fd = open(file, O_WRONLY | O_APPEND | O_SYNC)) < 0)
133 return (CSA_X_DT_E_BACKING_STORE_PROBLEM);
136 if ((stat = _DtCmsAppendAccessByFD(fd, access_read_4,
137 GET_R_ACCESS(cal))) != CSA_SUCCESS)
140 if ((stat = _DtCmsAppendAccessByFD(fd, access_write_4,
141 GET_W_ACCESS(cal))) != CSA_SUCCESS)
144 if ((stat = _DtCmsAppendAccessByFD (fd, access_delete_4,
145 GET_D_ACCESS(cal))) != CSA_SUCCESS)
148 if ((stat = _DtCmsAppendAccessByFD (fd, access_exec_4,
149 GET_X_ACCESS(cal))) != CSA_SUCCESS)
152 _DtCmsEnumerateUp(cal, visit1); /* dump the tree */
154 if (close(fd) == EOF)
155 return (CSA_X_DT_E_BACKING_STORE_PROBLEM);
160 extern CSA_return_code
161 _DtCmsDumpDataV2(char *file, _DtCmsCalendar *cal)
163 CSA_return_code stat;
165 dump_error = CSA_SUCCESS;
167 /* Keep the temp log file open during garbage collection. */
168 if ((fd = open(file, O_WRONLY | O_APPEND | O_SYNC)) < 0)
170 return (CSA_X_DT_E_BACKING_STORE_PROBLEM);
173 /* dump calendar attributes */
174 if (cal->num_attrs > 0) {
175 if ((stat = _DtCmsAppendCalAttrsByFD(fd, cal->num_attrs,
176 cal->attrs)) != CSA_SUCCESS)
180 if ((stat = _DtCmsAppendHTableByFD(fd, cal->entry_tbl->size,
181 cal->entry_tbl->names, cal->types)) != CSA_SUCCESS)
184 _DtCmsEnumerateUp(cal, visit2); /* dump the tree */
186 if (close(fd) == EOF)
187 return (CSA_X_DT_E_BACKING_STORE_PROBLEM);
195 boolean_t magic = B_FALSE;
197 _Xltimeparams localtime_buf;
199 tm = _XLocaltime(&t, localtime_buf);
201 if (tm->tm_hour == 3 && tm->tm_min == 41)
208 visit1(caddr_t node, caddr_t d)
210 boolean_t stop = B_FALSE;
212 switch (((Appt_4 *) d)->tag->tag)
216 * otherTags = events read in from files.
217 * Don't write to log.
223 * a little hack to get us off the
224 * totally hokey magic-time business.
226 if (magic_time(((Appt_4 *)d)->appt_id.tick)) {
227 ((Appt_4 *)d)->tag->showtime = 0;
232 if ((dump_error = _DtCmsAppendAppt4ByFD(fd, (Appt_4 *)d, _DtCmsLogAdd)) != CSA_SUCCESS)
239 visit2(caddr_t node, caddr_t d)
241 boolean_t stop = B_FALSE;
243 if ((dump_error = _DtCmsAppendEntryByFD(fd, (cms_entry *)d,
244 _DtCmsLogAdd)) != CSA_SUCCESS)
251 print_file_error(char *file, char *msg)
254 fprintf(stderr, "%s: (%s)%s\n", pgname, file, msg);
256 fprintf(stderr, "%s: %s\n", pgname, msg);
257 fprintf(stderr, "possible causes: %s, %s, %s, %s\n",
258 "host server is down", "disk is full", "out of memory",
259 "file protections have changed.");
260 fprintf(stderr, "damage: none\n\n");