DbLoad.c 8.8 KB

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