f7187348ae83dd130fc5af4ecbefcb4d8fa01ca9
[oweals/cde.git] / cde / lib / DtSvc / DtUtil1 / DbLoad.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 libraries 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  * +SNOTICE
25  * 
26  * $XConsortium: DbLoad.c /main/7 1996/08/28 14:38:07 rswiston $
27  * 
28  * RESTRICTED CONFIDENTIAL INFORMATION:
29  * 
30  * The information in this document is subject to special restrictions in a
31  * confidential disclosure agreement bertween HP, IBM, Sun, USL, SCO and
32  * Univel.  Do not distribute this document outside HP, IBM, Sun, USL, SCO,
33  * or Univel wihtout Sun's specific written approval.  This documment and all
34  * copies and derivative works thereof must be returned or destroyed at Sun's
35  * request.
36  * 
37  * Copyright 1993 Sun Microsystems, Inc.  All rights reserved.
38  * 
39  * +ENOTICE
40  */
41  
42 /*                                                                      *
43  * (c) Copyright 1993, 1994 Hewlett-Packard Company                     *
44  * (c) Copyright 1993, 1994 International Business Machines Corp.       *
45  * (c) Copyright 1993, 1994 Sun Microsystems, Inc.                      *
46  * (c) Copyright 1993, 1994 Novell, Inc.                                *
47  */
48
49 #include <stdio.h>
50 #include <sys/types.h>
51
52 #ifdef __hpux
53 #include <ndir.h>
54 #else
55
56 #if defined(sun) || defined(CSRG_BASED)
57 #include <dirent.h>
58 #else
59 #include <sys/dir.h>
60 #endif                          /* sun || CSRD_BASED */
61
62 #endif                          /* __hpux */
63
64 #include <ctype.h>
65 #include <string.h>
66
67 #ifdef NLS16
68 #include <limits.h>
69 #endif
70
71 #include <sys/stat.h>
72 #include <sys/param.h>          /* MAXPATHLEN, MAXHOSTNAMELEN */
73 #include <X11/Xlib.h>
74 #include <X11/Intrinsic.h>
75 #include <X11/StringDefs.h>
76 #include <Dt/DtP.h>
77 #include <Dt/Connect.h>
78 #include <Dt/FileUtil.h>
79 #include <Dt/DtNlUtils.h>
80 #include <Dt/Action.h>
81 #include <Dt/ActionDbP.h>
82 #include <Dt/ActionP.h>
83 #include <Dt/ActionFind.h>
84 #include <Dt/DbUtil.h>
85 #include <Dt/DtPStrings.h>
86 #include <Dt/Utility.h>
87
88 #include <Dt/DtsDb.h>
89 #include <Dt/Dts.h>
90
91 #include "myassertP.h"
92 #include "DtSvcLock.h"
93
94 extern  void    _DtDtsDCConverter(DtDtsDbField * fields,
95                DtDbPathId pathId,
96                char *hostPrefix,
97                Boolean rejectionStatus);
98 extern  void    _DtDtsDAConverter(DtDtsDbField * fields,
99                DtDbPathId pathId,
100                char *hostPrefix,
101                Boolean rejectionStatus);
102
103 extern  void    _DtDtsSeqReset(void);
104 extern  int     _DtDtsNextDCSeq(void);
105 extern  int     _DtDtsNextDASeq(void);
106
107 char **_DtsDbListDb(void);
108
109 int      use_in_memory_db = False;
110
111 static void
112 _DtOAConverter(DtDtsDbField * fields,
113                DtDbPathId pathId,
114                char *hostPrefix,
115                Boolean rejectionStatus)
116 {
117         DtDtsDbDatabase *dc_db;
118         DtDtsDbDatabase *da_db;
119         DtDtsDbRecord  *dc_rec;
120         DtDtsDbRecord  *da_rec;
121         DtDtsDbField   *fld;
122
123         _DtSvcProcessLock();    
124         dc_db = _DtDtsDbGet(DtDTS_DC_NAME);
125         da_db = _DtDtsDbGet(DtDTS_DA_NAME);
126         if ( _DtDtsDbGetRecordByName(dc_db,
127                                 fields[0].fieldValue) ||
128                 _DtDtsDbGetRecordByName(da_db, 
129                                 fields[0].fieldValue))
130         {
131                 _DtSvcProcessUnlock();
132                 return;
133         }
134
135         /*
136          * Synthesize criteria record -- for this action
137          */
138         dc_rec = _DtDtsDbAddRecord(_DtDtsDbGet(DtDTS_DC_NAME));
139         dc_rec->recordName = XrmStringToQuark(fields[0].fieldValue);
140         dc_rec->seq = _DtDtsNextDCSeq();
141         dc_rec->pathId = (int)pathId;
142         fld = _DtDtsDbAddField(dc_rec);
143         fld->fieldName = XrmStringToQuark(DtDTS_NAME_PATTERN);
144         fld->fieldValue = strdup(fields[0].fieldValue);
145         fld = _DtDtsDbAddField(dc_rec);
146         fld->fieldName = XrmStringToQuark(DtDTS_MODE);
147         fld->fieldValue = strdup("fx");
148         fld = _DtDtsDbAddField(dc_rec);
149         fld->fieldName = XrmStringToQuark(DtDTS_DATA_ATTRIBUTES_NAME);
150         fld->fieldValue = strdup(fields[0].fieldValue);
151         /*
152          * Mark the criteria record as synthetic.
153          */
154         fld = _DtDtsDbAddField(dc_rec);
155         fld->fieldName = XrmStringToQuark(DtDTS_DA_IS_SYNTHETIC);
156         fld->fieldValue = strdup("True");
157
158         /*
159          * Synthesize attribute record -- for this action
160          */
161         da_rec = _DtDtsDbAddRecord(_DtDtsDbGet(DtDTS_DA_NAME));
162         da_rec->recordName = XrmStringToQuark(fields[0].fieldValue);
163         da_rec->seq = _DtDtsNextDASeq();
164         da_rec->pathId = (int)pathId;
165         fld = _DtDtsDbAddField(da_rec);
166         fld->fieldName = XrmStringToQuark(DtDTS_DA_ACTION_LIST);
167         fld->fieldValue = strdup(fields[0].fieldValue);
168         fld = _DtDtsDbAddField(da_rec);
169         fld->fieldName = XrmStringToQuark(DtDTS_DA_DATA_HOST);
170         fld->fieldValue = strdup(hostPrefix);
171
172         /*
173          * Mark the attribute record as synthetic.
174          */
175         fld = _DtDtsDbAddField(da_rec);
176         fld->fieldName = XrmStringToQuark(DtDTS_DA_IS_SYNTHETIC);
177         fld->fieldValue = strdup("True");
178
179         fld = _DtDtsDbAddField(da_rec);
180         fld->fieldName = XrmStringToQuark(DtDTS_DA_IS_ACTION);
181         fld->fieldValue = strdup("True");
182
183         fld = _DtDtsDbAddField(da_rec);
184         fld->fieldName = XrmStringToQuark(DtDTS_DA_IS_TEXT);
185         fld->fieldValue = strdup("False");
186
187         /*
188          * Use the action name as the default copy_to action
189          */
190         fld = _DtDtsDbAddField(da_rec);
191         fld->fieldName = XrmStringToQuark(DtDTS_DA_COPY_TO_ACTION);
192         fld->fieldValue = strdup(fields[0].fieldValue);
193         _DtSvcProcessUnlock();
194 }
195
196
197 /******************************************************************************
198  *
199  * DtDbLoad -
200  *
201  *   Reads in the file types and action databases.
202  *   From the Default DtDatabaseDirPaths. 
203  *
204  *****************************************************************************/
205
206 void _DtDbLoad(DtDirPaths *dirs)
207 {
208         _DtDtsMMUnLoad();
209 }
210
211 void
212 DtDbLoad(void)
213 {
214         DtDirPaths      *dirs;
215
216         /* Load the requested database files */
217         dirs = _DtGetDatabaseDirPaths();
218         _DtDbLoad(dirs);
219         _DtFreeDatabaseDirPaths(dirs);
220 }
221
222 /******************************************************************************
223  *
224  * _DtDbLoad(dirs) -
225  *
226  *   Reads in the file types and action databases.
227  *   From the the given directory path.  This function is not part of the
228  *   public API but may be used internally to avoid repeated generation of
229  *   the default databasedir path.
230  *
231  *****************************************************************************/
232 int
233 _DtDtsMMCreateDb(DtDirPaths *dirs, const char *CacheFile, int override)
234 {
235         DtDbRecordDesc  recordDescriptions[3];
236         DtDbConverter   criteriaConverters[2];
237         DtDbConverter   attributesConverters[2];
238         DtDbConverter   actionConverters[3];
239         static  int     beenCalled = 0;
240         char            **list;
241         int             i;
242
243         _DtSvcProcessLock();
244
245         use_in_memory_db = TRUE;
246
247         if ( beenCalled ) {
248                 /*
249                  * Free up existing database.
250                  */
251                 _DtDtsSeqReset();
252                 DtDtsRelease();
253                 _DtFreeActionDB();
254
255         }
256         beenCalled = 1;
257
258         /*
259          * Initialize the databases
260          * -- this call will initializae the Object databases then
261          *    add the action database.
262          */
263         (void) _DtDtsDbAddDatabase(_DtACTION_NAME);
264         
265         if ( !dirs )
266         {
267                 myassert(dirs); /* register an assertion failure */
268                 _DtSvcProcessUnlock();
269                 return(0);
270         }
271
272         /* Build up the record descriptions */
273         criteriaConverters[0] = (DtDbConverter) _DtDtsDCConverter;
274         criteriaConverters[1] = NULL;
275         recordDescriptions[0].recordKeyword = DtDTS_DC_NAME;
276         recordDescriptions[0].maxFields = _DtMAX_NUM_FIELDS;
277         recordDescriptions[0].converters = criteriaConverters;
278
279         /* Build up the ot record descriptions */
280         attributesConverters[0] = (DtDbConverter) _DtDtsDAConverter;
281         attributesConverters[1] = NULL;
282         recordDescriptions[1].recordKeyword = DtDTS_DA_NAME;
283         recordDescriptions[1].maxFields = _DtMAX_NUM_FIELDS;
284         recordDescriptions[1].converters = attributesConverters;
285
286         actionConverters[0] = (DtDbConverter) _DtActionConverter;
287         actionConverters[1] = (DtDbConverter) _DtOAConverter;
288         actionConverters[2] = NULL;
289         recordDescriptions[2].recordKeyword = _DtACTION_NAME;
290         recordDescriptions[2].maxFields = _ActDb_MAX_NUM_FIELDS;
291         recordDescriptions[2].converters = actionConverters;
292
293
294         _DtDbRead(dirs, ".dt", recordDescriptions, 3);
295
296         _DtSortActionDb();
297
298         /* 
299          * we may eventually want to return a count of the new records.
300          * for now we return a non-zero value to register success.
301          */
302
303         if ((!_DtDtsMMCreateFile(dirs, CacheFile)) ||
304             (!_DtDtsMMapDB(CacheFile)))
305         {
306                 _DtSvcProcessUnlock();
307                 return(0);
308         }
309         if(!override)
310         {
311                 unlink(CacheFile);
312         }       
313
314         /* now that we have built the databases delete the tmp Db memory
315            used for it (Too, bad we can't delete the memory associcated
316            with the Quarks) */
317         list = (char **)_DtsDbListDb();
318         for(i = 0; list[i]; i++)
319         {
320                 _DtDtsDbDeleteDb(_DtDtsDbGet(list[i]));
321                 free(list[i]);
322         }
323         free(list);
324         use_in_memory_db = FALSE;
325
326         _DtSvcProcessUnlock();
327         return(1);
328 }