gwenhywfar  4.8.0beta
pathmanager.c
Go to the documentation of this file.
1 /***************************************************************************
2  begin : Mon Mar 01 2004
3  copyright : (C) 2004 by Martin Preuss
4  email : martin@libchipcard.de
5 
6  ***************************************************************************
7  * *
8  * This library is free software; you can redistribute it and/or *
9  * modify it under the terms of the GNU Lesser General Public *
10  * License as published by the Free Software Foundation; either *
11  * version 2.1 of the License, or (at your option) any later version. *
12  * *
13  * This library is distributed in the hope that it will be useful, *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
16  * Lesser General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU Lesser General Public *
19  * License along with this library; if not, write to the Free Software *
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
21  * MA 02111-1307 USA *
22  * *
23  ***************************************************************************/
24 
25 #ifdef HAVE_CONFIG_H
26 # include <config.h>
27 #endif
28 
29 #define DISABLE_DEBUGLOG
30 
31 
32 #include "pathmanager_p.h"
33 #include <gwenhywfar/db.h>
34 #include <gwenhywfar/debug.h>
35 #include <gwenhywfar/directory.h>
36 
37 
38 #include <stdlib.h>
39 #include <stdio.h>
40 #include <assert.h>
41 #include <string.h>
42 #include <unistd.h>
43 #include <errno.h>
44 
45 #ifdef OS_WIN32
46 # include <windows.h>
47 #endif
48 
49 
51 
52 
55 
56  return 0;
57 }
58 
59 
60 
63  gwen__paths=0;
64  return 0;
65 }
66 
67 
68 
69 int GWEN_PathManager_DefinePath(const char *destLib,
70  const char *pathName) {
71  GWEN_DB_NODE *dbT;
72 
73  assert(destLib);
74  assert(pathName);
75  assert(gwen__paths);
77  destLib);
78  assert(dbT);
79  if (GWEN_DB_GetGroup(dbT, GWEN_PATH_FLAGS_NAMEMUSTEXIST, pathName)) {
81  "Path \"%s/%s\" already exists",
82  destLib, pathName);
83  return GWEN_ERROR_INVALID;
84  }
85  dbT=GWEN_DB_GetGroup(dbT,
87  pathName);
88  return 0;
89 }
90 
91 
92 
93 int GWEN_PathManager_UndefinePath(const char *destLib,
94  const char *pathName) {
95  GWEN_DB_NODE *dbT;
96 
97  assert(destLib);
98  assert(pathName);
99  assert(gwen__paths);
101  destLib);
102  if (!dbT)
103  return GWEN_ERROR_NOT_FOUND;
104 
106  pathName);
107  if (!dbT)
108  return GWEN_ERROR_NOT_FOUND;
109  GWEN_DB_UnlinkGroup(dbT);
110  GWEN_DB_Group_free(dbT);
111  return 0;
112 }
113 
114 
115 
116 
117 int GWEN_PathManager_AddPath(const char *callingLib,
118  const char *destLib,
119  const char *pathName,
120  const char *pathValue) {
121  GWEN_DB_NODE *dbT;
122  GWEN_BUFFER *buf;
123 
124  assert(destLib);
125  assert(pathName);
126  assert(pathValue);
127  assert(gwen__paths);
129  destLib);
130  if (!dbT)
131  return GWEN_ERROR_NOT_FOUND;
133  pathName);
134  if (!dbT)
135  return GWEN_ERROR_NOT_FOUND;
137  "pair");
138  assert(dbT);
139 
140  if (callingLib) {
142  "lib", callingLib);
143  }
144 
145  buf=GWEN_Buffer_new(0, 256, 0, 1);
146  GWEN_Directory_OsifyPath(pathValue, buf, 1);
147 
149  "path",
150  GWEN_Buffer_GetStart(buf));
151  GWEN_Buffer_free(buf);
152 
153  return 0;
154 }
155 
156 
157 
158 int GWEN_PathManager_AddRelPath(const char *callingLib,
159  const char *destLib,
160  const char *pathName,
161  const char *pathValue,
163  char cwd[256];
164 
165  switch(rm) {
167  const char *pcwd;
168 
169  pcwd=getcwd(cwd, sizeof(cwd)-1);
170  if (pcwd) {
171  GWEN_BUFFER *buf;
172  int rv;
173 
174  buf=GWEN_Buffer_new(0, 256, 0, 1);
175  GWEN_Buffer_AppendString(buf, cwd);
176  if (*pathValue!=GWEN_DIR_SEPARATOR)
178  GWEN_Buffer_AppendString(buf, pathValue);
179  rv=GWEN_PathManager_AddPath(callingLib, destLib, pathName,
180  GWEN_Buffer_GetStart(buf));
181  GWEN_Buffer_free(buf);
182  return rv;
183  }
184  else {
185  DBG_ERROR(GWEN_LOGDOMAIN, "getcwd(): %s", strerror(errno));
186  return GWEN_ERROR_IO;
187  }
188  break;
189  }
190 
192  int rv;
193 
194  rv=GWEN_Directory_GetPrefixDirectory(cwd, sizeof(cwd)-1);
195  if (rv) {
196  DBG_INFO(GWEN_LOGDOMAIN, "here (%d)", rv);
197  return rv;
198  }
199  else {
200  GWEN_BUFFER *buf;
201 
202  buf=GWEN_Buffer_new(0, 256, 0, 1);
203  GWEN_Buffer_AppendString(buf, cwd);
204  if (*pathValue!=GWEN_DIR_SEPARATOR)
206  GWEN_Buffer_AppendString(buf, pathValue);
208  "Adding path [%s]",
209  GWEN_Buffer_GetStart(buf));
210  rv=GWEN_PathManager_AddPath(callingLib, destLib, pathName,
211  GWEN_Buffer_GetStart(buf));
212  GWEN_Buffer_free(buf);
213  return rv;
214  }
215  }
216 
218  GWEN_BUFFER *buf;
219  int rv;
220 
221  rv=GWEN_Directory_GetHomeDirectory(cwd, sizeof(cwd)-1);
222  if (rv) {
224  "Could not determine HOME directory (%d)",
225  rv);
226  return rv;
227  }
228  buf=GWEN_Buffer_new(0, 256, 0, 1);
229  GWEN_Buffer_AppendString(buf, cwd);
230  if (*pathValue!=GWEN_DIR_SEPARATOR)
232  GWEN_Buffer_AppendString(buf, pathValue);
233  rv=GWEN_PathManager_AddPath(callingLib, destLib, pathName,
234  GWEN_Buffer_GetStart(buf));
235  GWEN_Buffer_free(buf);
236  return rv;
237  }
238 
239  default:
240  DBG_INFO(GWEN_LOGDOMAIN, "Unknown relative mode %d", rm);
241  return GWEN_ERROR_INVALID;
242  }
243 
244 }
245 
246 
247 
248 int GWEN_PathManager_InsertPath(const char *callingLib,
249  const char *destLib,
250  const char *pathName,
251  const char *pathValue) {
252  GWEN_DB_NODE *dbT;
253 
254  assert(destLib);
255  assert(pathName);
256  assert(pathValue);
257  assert(gwen__paths);
259  destLib);
260  if (!dbT)
261  return GWEN_ERROR_NOT_FOUND;
263  pathName);
264  if (!dbT)
265  return GWEN_ERROR_NOT_FOUND;
266 
267  dbT=GWEN_DB_GetGroup(dbT,
270  "pair");
271  assert(dbT);
272 
273  if (callingLib) {
275  "lib", callingLib);
276  }
278  "path", pathValue);
279 
280  return 0;
281 }
282 
283 
284 
285 int GWEN_PathManager_RemovePath(const char *callingLib,
286  const char *destLib,
287  const char *pathName,
288  const char *pathValue) {
289  GWEN_DB_NODE *dbT;
290  const char *s;
291  const char *p;
292 
293  assert(gwen__paths);
295  destLib);
296  if (!dbT)
297  return GWEN_ERROR_NOT_FOUND;
299  pathName);
300  if (!dbT)
301  return GWEN_ERROR_NOT_FOUND;
302 
303  dbT=GWEN_DB_FindFirstGroup(dbT, "pair");
304  while(dbT) {
305  p=GWEN_DB_GetCharValue(dbT, "path", 0, 0);
306  assert(p);
307  s=GWEN_DB_GetCharValue(dbT, "lib", 0, 0);
308 
309  if (
310  (
311  (!callingLib && !s) ||
312  (callingLib && s && strcasecmp(s, callingLib)==0)
313  ) &&
314  strcasecmp(p, pathValue)==0
315  )
316  break;
317  dbT=GWEN_DB_FindNextGroup(dbT, "pair");
318  }
319 
320  if (dbT) {
321  GWEN_DB_UnlinkGroup(dbT);
322  GWEN_DB_Group_free(dbT);
323  return 0;
324  }
325  else
326  return 1;
327 
328 }
329 
330 
331 
332 int GWEN_PathManager_RemovePaths(const char *callingLib) {
333  GWEN_DB_NODE *dbT;
334  const char *s;
335 
336  assert(gwen__paths);
337  GWEN_DB_DeleteGroup(gwen__paths, callingLib);
338 
340  while(dbT) {
341  GWEN_DB_NODE *dbN;
342 
343  dbN=GWEN_DB_GetFirstGroup(dbT);
344  while(dbN) {
345  GWEN_DB_NODE *dbNN;
346 
347  dbNN=GWEN_DB_FindFirstGroup(dbN, "pair");
348  while(dbNN) {
349  GWEN_DB_NODE *dbNext;
350 
351  dbNext=GWEN_DB_FindNextGroup(dbNN, "pair");
352  s=GWEN_DB_GetCharValue(dbNN, "lib", 0, 0);
353  assert(s);
354 
355  if (s && strcasecmp(s, callingLib)==0) {
356  GWEN_DB_UnlinkGroup(dbNN);
357  GWEN_DB_Group_free(dbNN);
358  }
359  dbNN=dbNext;
360  } /* while pairs */
361  dbN=GWEN_DB_GetNextGroup(dbN);
362  } /* while paths */
363  dbT=GWEN_DB_GetNextGroup(dbT);
364  } /* while destLibs */
365 
366  return 0;
367 }
368 
369 
370 
371 int GWEN_PathManager_PathChanged(const char *destLib,
372  const char *pathName) {
373  GWEN_DB_NODE *dbT;
374 
375  assert(gwen__paths);
377  destLib);
378  if (!dbT)
379  return GWEN_ERROR_NOT_FOUND;
380 
382  pathName);
383  if (!dbT)
384  return GWEN_ERROR_NOT_FOUND;
385 
387  return 1;
388 
389  return 0;
390 }
391 
392 
393 
395  const char *pathName) {
396  GWEN_DB_NODE *dbT;
397 
398  assert(gwen__paths);
400  destLib);
401  if (dbT) {
403  pathName);
404  if (dbT) {
405  GWEN_STRINGLIST *sl;
406  int i;
407  const char *s;
408  GWEN_DB_NODE *dbN;
409 
410  sl=GWEN_StringList_new();
411 
412  /* then add all paths from other libs */
413  dbN=GWEN_DB_FindFirstGroup(dbT, "pair");
414  while(dbN) {
415  for (i=0; ; i++) {
416  s=GWEN_DB_GetCharValue(dbN, "path", i, 0);
417  if (!s)
418  break;
419  GWEN_StringList_AppendString(sl, s, 0, 1);
420  }
421 
422  dbN=GWEN_DB_FindNextGroup(dbN, "pair");
423  }
424 
425  if (GWEN_StringList_Count(sl)==0) {
427  DBG_DEBUG(GWEN_LOGDOMAIN, "no entries");
428  return 0;
429  }
430 
431  return sl;
432  }
433  }
434 
435  return 0;
436 }
437 
438 
439 
440 int GWEN_PathManager_FindFile(const char *destLib,
441  const char *pathName,
442  const char *fileName,
443  GWEN_BUFFER *fbuf) {
444  GWEN_DB_NODE *dbT;
445 
446  assert(gwen__paths);
448  destLib);
449  if (dbT) {
451  pathName);
452  if (dbT) {
453  int i;
454  const char *s;
455  GWEN_DB_NODE *dbN;
456  GWEN_BUFFER *tbuf;
457 
458  tbuf=GWEN_Buffer_new(0, 256, 0, 1);
459 
460  /* check all paths */
461  dbN=GWEN_DB_FindFirstGroup(dbT, "pair");
462  while(dbN) {
463  for (i=0; ; i++) {
464  s=GWEN_DB_GetCharValue(dbN, "path", i, 0);
465  if (!s)
466  break;
467  else {
468  FILE *f;
469 
470  GWEN_Buffer_AppendString(tbuf, s);
472  GWEN_Buffer_AppendString(tbuf, fileName);
473  DBG_DEBUG(GWEN_LOGDOMAIN, "Trying \"%s\"",
474  GWEN_Buffer_GetStart(tbuf));
475  f=fopen(GWEN_Buffer_GetStart(tbuf), "r");
476  if (f) {
477  fclose(f);
479  "File \"%s\" found in folder \"%s\"",
480  fileName,
481  s);
482  GWEN_Buffer_AppendBuffer(fbuf, tbuf);
483  GWEN_Buffer_free(tbuf);
484  return 0;
485  }
486  GWEN_Buffer_Reset(tbuf);
487  }
488  }
489 
490  dbN=GWEN_DB_FindNextGroup(dbN, "pair");
491  }
492  GWEN_Buffer_free(tbuf);
493  }
494  }
495 
496  DBG_INFO(GWEN_LOGDOMAIN, "File \"%s\" not found", fileName);
497  return GWEN_ERROR_NOT_FOUND;
498 }
499 
500 
501 
503  const char *pathName,
504  const char *subFolderName,
505  GWEN_STRINGLIST *sl,
506  const char *mask) {
507  GWEN_DB_NODE *dbT;
508 
509  assert(gwen__paths);
511  destLib);
512  if (dbT) {
514  pathName);
515  if (dbT) {
516  int i;
517  const char *s;
518  GWEN_DB_NODE *dbN;
519  GWEN_BUFFER *tbuf;
520 
521  tbuf=GWEN_Buffer_new(0, 256, 0, 1);
522 
523  /* check all paths */
524  dbN=GWEN_DB_FindFirstGroup(dbT, "pair");
525  while(dbN) {
526  for (i=0; ; i++) {
527  s=GWEN_DB_GetCharValue(dbN, "path", i, 0);
528  if (!s)
529  break;
530  else {
531  GWEN_Buffer_AppendString(tbuf, s);
532  if (subFolderName && *subFolderName) {
534  GWEN_Buffer_AppendString(tbuf, subFolderName);
535  }
536 
537  DBG_DEBUG(GWEN_LOGDOMAIN, "Trying \"%s\"",
538  GWEN_Buffer_GetStart(tbuf));
540  GWEN_Buffer_Reset(tbuf);
541  }
542  }
543 
544  dbN=GWEN_DB_FindNextGroup(dbN, "pair");
545  }
546  GWEN_Buffer_free(tbuf);
547  }
548  }
549 
550  return 0;
551 }
552 
553 
554 
555 
556 
557 #ifdef OS_WIN32
558 int GWEN_PathManager_AddPathFromWinReg(const char *callingLib,
559  const char *destLib,
560  const char *pathName,
561  const char *keypath,
562  const char *varname){
563  HKEY hkey;
564  TCHAR nbuffer[MAX_PATH];
565  BYTE vbuffer[MAX_PATH];
566  DWORD nsize;
567  DWORD vsize;
568  DWORD typ;
569  int i;
570 
571  snprintf(nbuffer, sizeof(nbuffer), keypath);
572 
573  /* open the key */
574  if (RegOpenKey(HKEY_LOCAL_MACHINE, nbuffer, &hkey)){
575  DBG_INFO(GWEN_LOGDOMAIN, "RegOpenKey %s failed.", keypath);
576  return 1;
577  }
578 
579  /* find the variablename */
580  for (i=0;; i++) {
581  nsize=sizeof(nbuffer);
582  vsize=sizeof(vbuffer);
583  if (ERROR_SUCCESS!=RegEnumValue(hkey,
584  i, /* index */
585  nbuffer,
586  &nsize,
587  0, /* reserved */
588  &typ,
589  vbuffer,
590  &vsize))
591  break;
592  if (strcasecmp(nbuffer, varname)==0 && typ==REG_SZ) {
593  /* variable found */
594  RegCloseKey(hkey);
595  return GWEN_PathManager_AddPath(callingLib,
596  destLib,
597  pathName,
598  (char*)vbuffer);
599  }
600  } /* for */
601 
602  RegCloseKey(hkey);
604  "In RegKey \"%s\" the variable \"%s\" does not exist",
605  keypath, varname);
606  return 1;
607 }
608 
609 #else /* OS_WIN32 */
610 
611 
613  GWEN_UNUSED const char *destLib,
614  GWEN_UNUSED const char *pathName,
615  GWEN_UNUSED const char *keypath,
616  GWEN_UNUSED const char *varname){
617  return 0;
618 }
619 
620 #endif /* OS_WIN32 */
621 
622