Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / lib / DtSearch / dbchange.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 /*
24  *   COMPONENT_NAME: austext
25  *
26  *   FUNCTIONS: database_has_changed
27  *              file_has_changed
28  *
29  *   ORIGINS: 27
30  *
31  *
32  *   (C) COPYRIGHT International Business Machines Corp. 1995
33  *   All Rights Reserved
34  *   Licensed Materials - Property of IBM
35  *   US Government Users Restricted Rights - Use, duplication or
36  *   disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
37  */
38 /******************************* DBCHANGE.C ********************************
39  * $XConsortium: dbchange.c /main/4 1996/05/07 13:26:54 drk $
40  * March 1995.
41  * Extracted from oe.c so it could also be called by server daemon.
42  * When called only in the engine it was only called by a child
43  * of the daemon, so the original daemon was never updated and
44  * every single engine call resulted in an engine reinitialization.
45  *
46  * $Log$
47  * Revision 2.2  1995/10/26  15:31:24  miker
48  * Added prolog.
49  *
50  * Revision 2.1  1995/09/22  19:47:12  miker
51  * Freeze DtSearch 0.1, AusText 2.1.8
52  *
53  * Revision 1.2  1995/08/31  22:18:48  miker
54  * Minor changes for DtSearch including msg catalogs and
55  * replacing socblk with usrblk for head of dblist.
56  */
57 #include "SearchE.h"
58 #include <errno.h>
59 #include <sys/stat.h>
60
61 #define PROGNAME        "DBCHANGE"
62
63 /************************************************/
64 /*                                              */
65 /*                file_has_changed              */
66 /*                                              */
67 /************************************************/
68 /* If site config file or any d99 file has changed
69  * since program initialization, close all databases and reinitialize.
70  * This usually happens when the administrator has updated
71  * a copy of one of the databases in a different directory.
72  * He then changes the PATH parameter for that database in the
73  * site config file.  By closing databases and reinitializing,
74  * an administrator can swap databases without bringing the users down.
75  * The test for the d99 files is included because in some systems,
76  * like cose, a database may change without a corresponding
77  * change in the site config file.
78  * Databases cannot be swapped during an ongoing search.
79  * If the passed file's mod time is unaccessible or has changed,
80  * appends msg to msglist, reinitializes engine if necessary,
81  * sets up usrblk.retncode, and returns TRUE.
82  * Otherwise returns FALSE.
83  */
84 static int      file_has_changed (char *fname, time_t origtime)
85 {
86     struct stat     statbuf;
87     char            sprintbuf[1024];
88
89     if (stat (fname, &statbuf) == -1) {
90         sprintf (sprintbuf, catgets (dtsearch_catd, 10, 1300,
91                 "%s Unable to comply with request; cannot access status\n"
92                 "  of database file '%s': %s"),
93             PROGNAME "1300", fname, strerror (errno));
94         DtSearchAddMessage (sprintbuf);
95         usrblk.retncode = OE_NOOP;
96         return TRUE;
97     }
98     if (origtime != statbuf.st_mtime) {
99         strcpy (sprintbuf, nowstring (&origtime));
100         fprintf (aa_stderr,
101             "%s %s reinitialized AusText engine:\n"
102             "   changed file:       %s\n"
103             "   old file time:      %s\n"
104             "   current file time:  %s.\n",
105             PROGNAME "1312", aa_argv0, fname,
106             sprintbuf, nowstring (&statbuf.st_mtime));
107         if (!(usrblk.flags & USR_NO_INFOMSGS)) {
108             sprintf (sprintbuf, catgets (dtsearch_catd, 10, 1313,
109                     "%s *** REQUEST CANCELED *** %s Engine reinitialized\n"
110                     "  due to modification of file %s, probably caused by\n"
111                     "  update to one or more databases."),
112                 PROGNAME "1313", OE_prodname, fname);
113             DtSearchAddMessage (sprintbuf);
114         }
115         oe_uninitialize ();
116         oe_initialize ();
117         usrblk.retncode = OE_REINIT;
118         return TRUE;
119     }
120     return FALSE;
121 }  /* file_has_changed() */
122
123
124 /************************************************/
125 /*                                              */
126 /*            database_has_changed              */
127 /*                                              */
128 /************************************************/
129 /* Verify that none of the databases has changed since the last call.
130  * Don't check until after first initialize.
131  * Returns TRUE if any changes, else FALSE.
132  */
133 int             database_has_changed (void)
134 {
135     char            fnamebuf[256];
136     DBLK           *db;
137
138     if (OE_sitecnfg_mtime == 0L)
139         return FALSE;
140     if (file_has_changed (OE_sitecnfg_fname, OE_sitecnfg_mtime))
141         return TRUE;
142     for (db = usrblk.dblist; db != NULL; db = db->link) {
143         sprintf (fnamebuf, "%s%s" EXT_DTBS, db->path, db->name);
144         if (file_has_changed (fnamebuf, db->iimtime))
145             return TRUE;
146     }
147     return FALSE;
148 }  /* database_has_changed() */
149
150 /******************************* DBCHANGE.C ********************************/