renamefile-for-mrafslogs-20050414
[openafs.git] / src / util / dirpath.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #include <afsconfig.h>
11 #include <afs/param.h>
12
13 RCSID
14     ("$Header$");
15
16 #include <stddef.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <ctype.h>
20 #include <limits.h>
21 #include <errno.h>
22 #include <stdio.h>
23 #include "assert.h"
24 #include "afsutil.h"
25 #include "fileutil.h"
26 #ifdef AFS_PTHREAD_ENV
27 #include <pthread.h>
28 static pthread_once_t dirInit_once = PTHREAD_ONCE_INIT;
29 #endif
30 #ifdef AFS_NT40_ENV
31 #include <windows.h>
32 #include <WINNT\afssw.h>
33 #endif
34 #ifdef AFS_DARWIN_ENV
35 #include <unistd.h>
36 #endif
37
38 /* local vars */
39 /* static storage for path strings */
40 static char dirPathArray[AFSDIR_PATHSTRING_MAX][AFSDIR_PATH_MAX];
41
42 /* indicate if and how the dirpath module initialized. */
43 static int initFlag = 0;
44 static unsigned int initStatus = 0;
45
46
47 /* storage for dynamically-determined install dir (NT only; long and short) */
48 #ifdef AFS_NT40_ENV
49 static char ntServerInstallDirLong[AFSDIR_PATH_MAX];
50 static char ntServerInstallDirShort[AFSDIR_PATH_MAX];
51 static char ntClientConfigDirLong[AFSDIR_PATH_MAX];
52 static char ntClientConfigDirShort[AFSDIR_PATH_MAX];
53 #endif
54
55 /* storage for local afs server/client paths (top-level) */
56 static char afsSrvDirPath[AFSDIR_PATH_MAX];
57 static char afsClntDirPath[AFSDIR_PATH_MAX];
58
59 /* internal array init function */
60 static void initDirPathArray(void);
61
62 /* Additional macros for ease of use */
63 /* buf is expected to be atleast AFS_PATH_MAX bytes long */
64 #define AFSDIR_SERVER_DIRPATH(buf, dir)  \
65             (void) strcompose(buf, AFSDIR_PATH_MAX, serverPrefix, dir, NULL)
66
67 #define AFSDIR_SERVER_FILEPATH(buf, dir, file)  \
68             (void) strcompose(buf, AFSDIR_PATH_MAX, serverPrefix, dir, "/", file,  NULL)
69
70 #define AFSDIR_CLIENT_DIRPATH(buf, dir)  \
71             (void) strcompose(buf, AFSDIR_PATH_MAX, clientPrefix, dir, NULL)
72
73 #define AFSDIR_CLIENT_FILEPATH(buf, dir, file)  \
74             (void) strcompose(buf, AFSDIR_PATH_MAX,  clientPrefix, dir, "/", file,  NULL)
75
76
77 /* initAFSDirPath() -- External users call this function to initialize
78  * the dirpath module and/or to determine the initialization status.
79  */
80 unsigned int
81 initAFSDirPath(void)
82 {
83     if (initFlag == 0) {        /* not yet init'ed, so initialize */
84 #ifdef AFS_PTHREAD_ENV
85         pthread_once(&dirInit_once, initDirPathArray);
86 #else
87         initDirPathArray();
88 #endif
89     }
90     return initStatus;
91 }
92
93
94 /* initDirPathArray() -- Initializes the afs dir paths for the 
95  *     server and client installations.
96  *
97  *     For NT these are determined dynamically; for Unix they are static.
98  *
99  *     NT NOTE: If a particular component (client/server) is not installed
100  *              then we may not be able to initialize the paths to anything
101  *              meaningful.  In this case the paths are set to the local
102  *              temp directory to avoid later reference to an uninitialized
103  *              variable.  The initStatus flag is set to indicate which
104  *              paths (client/server) initialized properly for callers of
105  *              initAFSDirPath() who would like to know this information.
106  */
107 static void
108 initDirPathArray(void)
109 {
110     char *pathp;
111     const char *clientPrefix = "";
112     const char *serverPrefix = "";
113
114 #ifdef AFS_NT40_ENV
115     char *buf;
116     int status;
117
118     /* get the afs server software installation dir from the registry */
119     if (afssw_GetServerInstallDir(&buf)) {
120         /* failed; server not installed; use temp directory */
121         strcpy(ntServerInstallDirLong, gettmpdir());
122     } else {
123         strcpy(ntServerInstallDirLong, buf);
124         free(buf);
125         initStatus |= AFSDIR_SERVER_PATHS_OK;
126     }
127     FilepathNormalize(ntServerInstallDirLong);
128     status =
129         GetShortPathName(ntServerInstallDirLong, ntServerInstallDirShort,
130                          AFSDIR_PATH_MAX);
131     if (status == 0 || status > AFSDIR_PATH_MAX) {
132         /* can't convert path to short version; just use long version */
133         strcpy(ntServerInstallDirShort, ntServerInstallDirLong);
134     }
135     FilepathNormalize(ntServerInstallDirShort);
136
137     /* get the afs client configuration directory (/usr/vice/etc equivalent) */
138     if (afssw_GetClientInstallDir(&buf)) {
139         /* failed */
140         status = GetWindowsDirectory(ntClientConfigDirLong, AFSDIR_PATH_MAX);
141         if (status == 0 || status > AFSDIR_PATH_MAX) {
142             /* failed to get canonical Windows directory; use temp directory */
143             strcpy(ntClientConfigDirLong, gettmpdir());
144         } else {
145             initStatus |= AFSDIR_CLIENT_PATHS_OK;
146         }
147     } else {
148         strcpy(ntClientConfigDirLong, buf);
149         free(buf);
150         initStatus |= AFSDIR_CLIENT_PATHS_OK;
151     }
152     FilepathNormalize(ntClientConfigDirLong);
153
154     status =
155         GetShortPathName(ntClientConfigDirLong, ntClientConfigDirShort,
156                          AFSDIR_PATH_MAX);
157     if (status == 0 || status > AFSDIR_PATH_MAX) {
158         /* can't convert path to short version; just use long version */
159         strcpy(ntClientConfigDirShort, ntClientConfigDirLong);
160     }
161     FilepathNormalize(ntClientConfigDirShort);
162     clientPrefix = ntClientConfigDirShort;
163
164     /* setup the root server directory path (/usr/afs equivalent) */
165     strcpy(afsSrvDirPath, ntServerInstallDirShort);
166     strcat(afsSrvDirPath, AFSDIR_CANONICAL_SERVER_AFS_DIRPATH);
167
168     /* there is no root client directory path (/usr/vice equivalent) */
169     afsClntDirPath[0] = '\0';
170
171     /* setup top level dirpath (/usr equivalent); valid for server ONLY */
172     strcpy(dirPathArray[AFSDIR_USR_DIRPATH_ID], ntServerInstallDirShort);
173     serverPrefix = ntServerInstallDirShort;
174     strcat(dirPathArray[AFSDIR_USR_DIRPATH_ID], AFSDIR_CANONICAL_USR_DIRPATH);
175
176 #else /* AFS_NT40_ENV */
177     /* setup the root server directory path */
178     strcpy(afsSrvDirPath, AFSDIR_CANONICAL_SERVER_AFS_DIRPATH);
179
180     /* setup the root client directory path */
181 #ifdef AFS_DARWIN_ENV
182     if (access(AFSDIR_ALTERNATE_CLIENT_VICE_DIRPATH, F_OK) == 0)
183         strcpy(afsClntDirPath, AFSDIR_ALTERNATE_CLIENT_VICE_DIRPATH);
184     else
185 #endif
186         strcpy(afsClntDirPath, AFSDIR_CANONICAL_CLIENT_VICE_DIRPATH);
187
188     /* setup top level dirpath; valid for both server and client */
189     strcpy(dirPathArray[AFSDIR_USR_DIRPATH_ID], AFSDIR_CANONICAL_USR_DIRPATH);
190
191     initStatus |= (AFSDIR_CLIENT_PATHS_OK | AFSDIR_SERVER_PATHS_OK);
192 #endif /* AFS_NT40_ENV */
193
194     /* now initialize various dir and file paths exported by dirpath module */
195
196     /* server dir paths */
197     strcpy(dirPathArray[AFSDIR_SERVER_AFS_DIRPATH_ID], afsSrvDirPath);
198
199     pathp = dirPathArray[AFSDIR_SERVER_ETC_DIRPATH_ID];
200     AFSDIR_SERVER_DIRPATH(pathp, AFSDIR_SERVER_ETC_DIR);
201
202     pathp = dirPathArray[AFSDIR_SERVER_BIN_DIRPATH_ID];
203     AFSDIR_SERVER_DIRPATH(pathp, AFSDIR_SERVER_BIN_DIR);
204
205     pathp = dirPathArray[AFSDIR_SERVER_CORES_DIRPATH_ID];
206     AFSDIR_SERVER_DIRPATH(pathp, AFSDIR_CORES_DIR);
207
208     pathp = dirPathArray[AFSDIR_SERVER_DB_DIRPATH_ID];
209     AFSDIR_SERVER_DIRPATH(pathp, AFSDIR_DB_DIR);
210
211     pathp = dirPathArray[AFSDIR_SERVER_LOGS_DIRPATH_ID];
212     AFSDIR_SERVER_DIRPATH(pathp, AFSDIR_LOGS_DIR);
213
214     pathp = dirPathArray[AFSDIR_SERVER_LOCAL_DIRPATH_ID];
215     AFSDIR_SERVER_DIRPATH(pathp, AFSDIR_LOCAL_DIR);
216
217     pathp = dirPathArray[AFSDIR_SERVER_BACKUP_DIRPATH_ID];
218     AFSDIR_SERVER_DIRPATH(pathp, AFSDIR_BACKUP_DIR);
219
220     pathp = dirPathArray[AFSDIR_SERVER_MIGRATE_DIRPATH_ID];
221     AFSDIR_SERVER_DIRPATH(pathp, AFSDIR_MIGR_DIR);
222
223     pathp = dirPathArray[AFSDIR_SERVER_BIN_FILE_DIRPATH_ID];
224     AFSDIR_SERVER_DIRPATH(pathp, AFSDIR_BIN_FILE_DIR);
225
226     /* client dir path */
227 #ifdef AFS_NT40_ENV
228     strcpy(dirPathArray[AFSDIR_CLIENT_VICE_DIRPATH_ID],
229            "/NoUsrViceDirectoryOnWindows");
230     strcpy(dirPathArray[AFSDIR_CLIENT_ETC_DIRPATH_ID],
231            ntClientConfigDirShort);
232 #else
233     strcpy(dirPathArray[AFSDIR_CLIENT_VICE_DIRPATH_ID], afsClntDirPath);
234
235     pathp = dirPathArray[AFSDIR_CLIENT_ETC_DIRPATH_ID];
236 #ifdef AFS_DARWIN_ENV
237     if (access(AFSDIR_ALTERNATE_CLIENT_ETC_DIR, F_OK) == 0)
238         AFSDIR_CLIENT_DIRPATH(pathp, AFSDIR_ALTERNATE_CLIENT_ETC_DIR);
239     else
240 #endif
241         AFSDIR_CLIENT_DIRPATH(pathp, AFSDIR_CLIENT_ETC_DIR);
242 #endif /* AFS_NT40_ENV */
243
244     /* server file paths */
245     pathp = dirPathArray[AFSDIR_SERVER_THISCELL_FILEPATH_ID];
246     AFSDIR_SERVER_FILEPATH(pathp, AFSDIR_SERVER_ETC_DIR,
247                            AFSDIR_THISCELL_FILE);
248
249     pathp = dirPathArray[AFSDIR_SERVER_CELLSERVDB_FILEPATH_ID];
250     AFSDIR_SERVER_FILEPATH(pathp, AFSDIR_SERVER_ETC_DIR,
251                            AFSDIR_CELLSERVDB_FILE);
252
253     pathp = dirPathArray[AFSDIR_SERVER_NOAUTH_FILEPATH_ID];
254     AFSDIR_SERVER_FILEPATH(pathp, AFSDIR_LOCAL_DIR, AFSDIR_NOAUTH_FILE);
255
256     pathp = dirPathArray[AFSDIR_SERVER_BUDBLOG_FILEPATH_ID];
257     AFSDIR_SERVER_FILEPATH(pathp, AFSDIR_LOGS_DIR, AFSDIR_BUDBLOG_FILE);
258
259     pathp = dirPathArray[AFSDIR_SERVER_TAPECONFIG_FILEPATH_ID];
260     AFSDIR_SERVER_FILEPATH(pathp, AFSDIR_BACKUP_DIR, AFSDIR_TAPECONFIG_FILE);
261
262     pathp = dirPathArray[AFSDIR_SERVER_KALOGDB_FILEPATH_ID];
263     AFSDIR_SERVER_FILEPATH(pathp, AFSDIR_LOGS_DIR, AFSDIR_KALOGDB_FILE);
264
265     pathp = dirPathArray[AFSDIR_SERVER_KALOG_FILEPATH_ID];
266     AFSDIR_SERVER_FILEPATH(pathp, AFSDIR_LOGS_DIR, AFSDIR_KALOG_FILE);
267
268     pathp = dirPathArray[AFSDIR_SERVER_KADB_FILEPATH_ID];
269     AFSDIR_SERVER_FILEPATH(pathp, AFSDIR_DB_DIR, AFSDIR_KADB_FILE);
270
271     pathp = dirPathArray[AFSDIR_SERVER_NTPD_FILEPATH_ID];
272     AFSDIR_SERVER_FILEPATH(pathp, AFSDIR_SERVER_BIN_DIR, AFSDIR_NTPD_FILE);
273
274     pathp = dirPathArray[AFSDIR_SERVER_PRDB_FILEPATH_ID];
275     AFSDIR_SERVER_FILEPATH(pathp, AFSDIR_DB_DIR, AFSDIR_PRDB_FILE);
276
277     pathp = dirPathArray[AFSDIR_SERVER_PTLOG_FILEPATH_ID];
278     AFSDIR_SERVER_FILEPATH(pathp, AFSDIR_LOGS_DIR, AFSDIR_PTLOG_FILE);
279
280     pathp = dirPathArray[AFSDIR_SERVER_KCONF_FILEPATH_ID];
281     AFSDIR_SERVER_FILEPATH(pathp, AFSDIR_SERVER_ETC_DIR, AFSDIR_KCONF_FILE);
282
283     pathp = dirPathArray[AFSDIR_SERVER_VLDB_FILEPATH_ID];
284     AFSDIR_SERVER_FILEPATH(pathp, AFSDIR_DB_DIR, AFSDIR_VLDB_FILE);
285
286     pathp = dirPathArray[AFSDIR_SERVER_VLOG_FILEPATH_ID];
287     AFSDIR_SERVER_FILEPATH(pathp, AFSDIR_LOGS_DIR, AFSDIR_VLOG_FILE);
288
289     pathp = dirPathArray[AFSDIR_SERVER_CORELOG_FILEPATH_ID];
290     AFSDIR_SERVER_FILEPATH(pathp, AFSDIR_LOGS_DIR, AFSDIR_CORE_FILE);
291
292     pathp = dirPathArray[AFSDIR_SERVER_SLVGLOG_FILEPATH_ID];
293     AFSDIR_SERVER_FILEPATH(pathp, AFSDIR_LOGS_DIR, AFSDIR_SLVGLOG_FILE);
294
295     pathp = dirPathArray[AFSDIR_SERVER_SALVAGER_FILEPATH_ID];
296     AFSDIR_SERVER_FILEPATH(pathp, AFSDIR_SERVER_BIN_DIR,
297                            AFSDIR_SALVAGER_FILE);
298
299     pathp = dirPathArray[AFSDIR_SERVER_SLVGLOCK_FILEPATH_ID];
300     AFSDIR_SERVER_FILEPATH(pathp, AFSDIR_LOCAL_DIR, AFSDIR_SLVGLOCK_FILE);
301
302     pathp = dirPathArray[AFSDIR_SERVER_KEY_FILEPATH_ID];
303     AFSDIR_SERVER_FILEPATH(pathp, AFSDIR_SERVER_ETC_DIR, AFSDIR_KEY_FILE);
304
305     pathp = dirPathArray[AFSDIR_SERVER_ULIST_FILEPATH_ID];
306     AFSDIR_SERVER_FILEPATH(pathp, AFSDIR_SERVER_ETC_DIR, AFSDIR_ULIST_FILE);
307
308     pathp = dirPathArray[AFSDIR_SERVER_BOZCONF_FILEPATH_ID];
309     AFSDIR_SERVER_FILEPATH(pathp, AFSDIR_BOSCONFIG_DIR, AFSDIR_BOZCONF_FILE);
310
311     pathp = dirPathArray[AFSDIR_SERVER_BOZCONFNEW_FILEPATH_ID];
312     AFSDIR_SERVER_FILEPATH(pathp, AFSDIR_BOSCONFIG_DIR,
313                            AFSDIR_BOZCONFNEW_FILE);
314
315     pathp = dirPathArray[AFSDIR_SERVER_BOZLOG_FILEPATH_ID];
316     AFSDIR_SERVER_FILEPATH(pathp, AFSDIR_LOGS_DIR, AFSDIR_BOZLOG_FILE);
317
318     pathp = dirPathArray[AFSDIR_SERVER_BOZINIT_FILEPATH_ID];
319     AFSDIR_SERVER_FILEPATH(pathp, AFSDIR_BOSCONFIG_DIR, AFSDIR_BOZINIT_FILE);
320
321     pathp = dirPathArray[AFSDIR_SERVER_BOSVR_FILEPATH_ID];
322     AFSDIR_SERVER_FILEPATH(pathp, AFSDIR_BOSSERVER_DIR, AFSDIR_BOSVR_FILE);
323
324     pathp = dirPathArray[AFSDIR_SERVER_VOLSERLOG_FILEPATH_ID];
325     AFSDIR_SERVER_FILEPATH(pathp, AFSDIR_LOGS_DIR, AFSDIR_VOLSERLOG_FILE);
326
327     pathp = dirPathArray[AFSDIR_SERVER_ROOTVOL_FILEPATH_ID];
328     AFSDIR_SERVER_FILEPATH(pathp, AFSDIR_SERVER_ETC_DIR, AFSDIR_ROOTVOL_FILE);
329
330     pathp = dirPathArray[AFSDIR_SERVER_HOSTDUMP_FILEPATH_ID];
331     AFSDIR_SERVER_FILEPATH(pathp, AFSDIR_LOCAL_DIR, AFSDIR_HOSTDUMP_FILE);
332
333     pathp = dirPathArray[AFSDIR_SERVER_CLNTDUMP_FILEPATH_ID];
334     AFSDIR_SERVER_FILEPATH(pathp, AFSDIR_LOCAL_DIR, AFSDIR_CLNTDUMP_FILE);
335
336     pathp = dirPathArray[AFSDIR_SERVER_CBKDUMP_FILEPATH_ID];
337     AFSDIR_SERVER_FILEPATH(pathp, AFSDIR_LOCAL_DIR, AFSDIR_CBKDUMP_FILE);
338
339     pathp = dirPathArray[AFSDIR_SERVER_OLDSYSID_FILEPATH_ID];
340     AFSDIR_SERVER_FILEPATH(pathp, AFSDIR_LOCAL_DIR, AFSDIR_OLDSYSID_FILE);
341
342     pathp = dirPathArray[AFSDIR_SERVER_SYSID_FILEPATH_ID];
343     AFSDIR_SERVER_FILEPATH(pathp, AFSDIR_LOCAL_DIR, AFSDIR_SYSID_FILE);
344
345     pathp = dirPathArray[AFSDIR_SERVER_FILELOG_FILEPATH_ID];
346     AFSDIR_SERVER_FILEPATH(pathp, AFSDIR_LOGS_DIR, AFSDIR_FILELOG_FILE);
347
348     pathp = dirPathArray[AFSDIR_SERVER_AUDIT_FILEPATH_ID];
349     AFSDIR_SERVER_FILEPATH(pathp, AFSDIR_LOCAL_DIR, AFSDIR_AUDIT_FILE);
350
351     pathp = dirPathArray[AFSDIR_SERVER_NETINFO_FILEPATH_ID];
352     AFSDIR_SERVER_FILEPATH(pathp, AFSDIR_LOCAL_DIR, AFSDIR_NETINFO_FILE);
353
354     pathp = dirPathArray[AFSDIR_SERVER_NETRESTRICT_FILEPATH_ID];
355     AFSDIR_SERVER_FILEPATH(pathp, AFSDIR_LOCAL_DIR, AFSDIR_NETRESTRICT_FILE);
356
357     pathp = dirPathArray[AFSDIR_SERVER_WEIGHTING_CONSTANTS_FILEPATH_ID];
358     AFSDIR_SERVER_FILEPATH(pathp, AFSDIR_MIGR_DIR,
359                            AFSDIR_WEIGHTINGCONST_FILE);
360
361     pathp = dirPathArray[AFSDIR_SERVER_THRESHOLD_CONSTANTS_FILEPATH_ID];
362     AFSDIR_SERVER_FILEPATH(pathp, AFSDIR_MIGR_DIR,
363                            AFSDIR_THRESHOLDCONST_FILE);
364
365     pathp = dirPathArray[AFSDIR_SERVER_MIGRATELOG_FILEPATH_ID];
366     AFSDIR_SERVER_FILEPATH(pathp, AFSDIR_MIGR_DIR, AFSDIR_MIGRATE_LOGNAME);
367
368
369     /* client file paths */
370 #ifdef AFS_NT40_ENV
371     strcpy(dirPathArray[AFSDIR_CLIENT_THISCELL_FILEPATH_ID],
372            "/NoUsrViceEtcThisCellFileOnWindows");
373     sprintf(dirPathArray[AFSDIR_CLIENT_CELLSERVDB_FILEPATH_ID], "%s/%s",
374             ntClientConfigDirShort, AFSDIR_CELLSERVDB_FILE_NTCLIENT);
375     strcpy(dirPathArray[AFSDIR_CLIENT_CELLALIAS_FILEPATH_ID],
376            "/NoCellAliasOnWindows");
377 #else
378     pathp = dirPathArray[AFSDIR_CLIENT_THISCELL_FILEPATH_ID];
379     AFSDIR_CLIENT_FILEPATH(pathp, AFSDIR_CLIENT_ETC_DIR,
380                            AFSDIR_THISCELL_FILE);
381
382     pathp = dirPathArray[AFSDIR_CLIENT_CELLSERVDB_FILEPATH_ID];
383     AFSDIR_CLIENT_FILEPATH(pathp, AFSDIR_CLIENT_ETC_DIR,
384                            AFSDIR_CELLSERVDB_FILE);
385
386     pathp = dirPathArray[AFSDIR_CLIENT_CELLALIAS_FILEPATH_ID];
387     AFSDIR_CLIENT_FILEPATH(pathp, AFSDIR_CLIENT_ETC_DIR,
388                            AFSDIR_CELLALIAS_FILE);
389 #endif /* AFS_NT40_ENV */
390
391     pathp = dirPathArray[AFSDIR_CLIENT_NETINFO_FILEPATH_ID];
392     AFSDIR_CLIENT_FILEPATH(pathp, AFSDIR_CLIENT_ETC_DIR, AFSDIR_NETINFO_FILE);
393
394     pathp = dirPathArray[AFSDIR_CLIENT_NETRESTRICT_FILEPATH_ID];
395     AFSDIR_CLIENT_FILEPATH(pathp, AFSDIR_CLIENT_ETC_DIR,
396                            AFSDIR_NETRESTRICT_FILE);
397
398     initFlag = 1;               /* finished dirpath initialization */
399     return;
400 }
401
402 /* getDirPath - returns a const char pointer to the requested string
403  * from the internal path array.
404  * string_id - index into the path array 
405  */
406 const char *
407 getDirPath(afsdir_id_t string_id)
408 {
409     /* check if the array has been initialized */
410     if (initFlag == 0) {        /* no it's not, so initialize */
411 #ifdef AFS_PTHREAD_ENV
412         pthread_once(&dirInit_once, initDirPathArray);
413 #else
414         initDirPathArray();
415 #endif
416     }
417     return (const char *)dirPathArray[string_id];
418 }
419
420 /*
421  * LocalizePathHead() -- Make path relative to local part
422  *
423  * ConstructLocalPath takes a path  and a directory that path should
424  * be considered relative to.   This  function checks the given path
425  * for   a prefix  that represents a canonical path.  If such a prefix
426  * is found,  the path is adjusted to remove the prefix and the path
427  * is considered  relative to the local version of that path.
428  */
429
430 /* The following array  maps cannonical parts to local parts.  It
431  * might  seem reasonable to  simply construct an array in parallel to
432  * dirpatharray  but it turns out you don't want translations for all
433  local paths.
434 */
435
436 struct canonmapping {
437     const char *canonical;
438     const char *local;
439 };
440 static struct canonmapping CanonicalTranslations[] = {
441     {AFSDIR_CANONICAL_SERVER_ETC_DIRPATH, AFSDIR_SERVER_ETC_DIR},
442     {AFSDIR_CANONICAL_SERVER_LOGS_DIRPATH, AFSDIR_LOGS_DIR},
443     {AFSDIR_CANONICAL_SERVER_LOCAL_DIRPATH, AFSDIR_LOCAL_DIR},
444     {AFSDIR_CANONICAL_SERVER_BIN_DIRPATH, AFSDIR_SERVER_BIN_DIR},
445     {NULL, NULL}
446 };
447
448 static void
449 LocalizePathHead(const char **path, const char **relativeTo)
450 {
451     struct canonmapping *current;
452     for (current = CanonicalTranslations; current->local != NULL; current++) {
453         int canonlength = strlen(current->canonical);
454         if (strncmp(*path, current->canonical, canonlength) == 0) {
455             (*path) += canonlength;
456             if (**path == '/')
457                 (*path)++;
458             *relativeTo = current->local;
459             return;
460         }
461     }
462 }
463
464
465 #ifdef AFS_NT40_ENV
466 /* NT version of ConstructLocalPath() */
467
468 /*
469  * ConstructLocalPath() --  Convert a canonical (wire-format) path to a fully
470  *     specified local path.  Upon successful completion, *fullPathBufp is
471  *     set to an allocated buffer containing the fully specified local path
472  *     constructed from the cpath argument.
473  *
474  *     On NT, path construction proceeds as follows:
475  *         1) If cpath is fully qualified (i.e., starts with 'X:/') then the
476  *            path returned is equivalent to cpath.
477  *         2) If cpath begins with a drive letter but is not fully qualified,
478  *            i.e., it is drive relative, then the function fails with EINVAL.
479  *         3) If cpath begins with '/' (or '\') then the path returned is the
480  *            concatenation  AFS-server-install-dir + cpath after translating for localization.
481  *         4) Otherwise the path returned is the concatenation
482  *            AFS-server-install-dir + relativeTo + cpath.
483  *
484  *     Leading whitespace in cpath is ignored; the constructed path is
485  *     normalized (FilepathNormalize()).
486  *
487  * RETURN CODES: 0 if successful; errno code otherwise.
488  */
489 int
490 ConstructLocalPath(const char *cpath, const char *relativeTo,
491                    char **fullPathBufp)
492 {
493     int status = 0;
494     char *newPath = NULL;
495
496     if (initFlag == 0) {        /* dirpath module not yet initialized */
497 #ifdef AFS_PTHREAD_ENV
498         pthread_once(&dirInit_once, initDirPathArray);
499 #else
500         initDirPathArray();
501 #endif
502     }
503
504     *fullPathBufp = NULL;
505
506     while (isspace(*cpath)) {
507         cpath++;
508     }
509
510     LocalizePathHead(&cpath, &relativeTo);
511     if ((((*cpath >= 'a') && (*cpath <= 'z'))
512          || ((*cpath >= 'A') && (*cpath <= 'Z'))) && (*(cpath + 1) == ':')) {
513
514         /* cpath has a leading drive letter */
515         if ((*(cpath + 2) != '/') && (*(cpath + 2) != '\\')) {
516             /* drive letter relative path; this is not allowed */
517             status = EINVAL;
518         } else {
519             /* fully qualified path; just make a copy */
520             newPath = (char *)malloc(strlen(cpath) + 1);
521             if (!newPath) {
522                 status = ENOMEM;
523             } else {
524                 (void)strcpy(newPath, cpath);
525             }
526         }
527
528     } else {
529         /* cpath has NO leading drive letter; make relative to install dir */
530         size_t pathSize = strlen(ntServerInstallDirShort) + 2;
531
532         if ((*cpath == '/') || (*cpath == '\\')) {
533             /* construct path relative to install directory only */
534             pathSize += strlen(cpath);
535
536             newPath = (char *)malloc(pathSize);
537             if (!newPath) {
538                 status = ENOMEM;
539             } else {
540                 sprintf(newPath, "%s/%s", ntServerInstallDirShort, cpath);
541             }
542         } else {
543             /* construct path relative to 'relativeTo' (and install dir) */
544             pathSize += strlen(relativeTo) + 1 + strlen(cpath);
545
546             newPath = (char *)malloc(pathSize);
547             if (!newPath) {
548                 status = ENOMEM;
549             } else {
550                 sprintf(newPath, "%s/%s/%s", ntServerInstallDirShort,
551                         relativeTo, cpath);
552             }
553         }
554     }
555
556     if (status == 0) {
557         FilepathNormalize(newPath);
558
559         /* return buffer containing fully specified path */
560         *fullPathBufp = newPath;
561     }
562
563     return status;
564 }
565
566 #else
567 /* Unix version of ConstructLocalPath() */
568
569 /*
570  * ConstructLocalPath() --  Convert a canonical (wire-format) path to a fully
571  *     specified local path.  Upon successful completion, *fullPathBufp is
572  *     set to an allocated buffer containing the fully specified local path
573  *     constructed from the cpath argument.
574  *
575  *     On Unix, path construction proceeds as follows:
576  *         1) If cpath begins with '/' then the path returned is equivalent
577  *            to cpath.
578  *         2) Otherwise the path returned is the concatenation
579  *            relativeTo + cpath.
580  *
581  *     Leading whitespace in cpath is ignored; the constructed path is
582  *     normalized (FilepathNormalize()).
583  *
584  * RETURN CODES: 0 if successful; errno code otherwise.
585  */
586 int
587 ConstructLocalPath(const char *cpath, const char *relativeTo,
588                    char **fullPathBufp)
589 {
590     int status = 0;
591     char *newPath = NULL;
592
593     if (initFlag == 0) {        /* dirpath module not yet initialized */
594 #ifdef AFS_PTHREAD_ENV
595         pthread_once(&dirInit_once, initDirPathArray);
596 #else
597         initDirPathArray();
598 #endif
599     }
600
601     *fullPathBufp = NULL;
602
603     while (isspace(*cpath)) {
604         cpath++;
605     }
606
607     LocalizePathHead(&cpath, &relativeTo);
608     if (*cpath == '/') {
609         newPath = (char *)malloc(strlen(cpath) + 1);
610         if (!newPath) {
611             status = ENOMEM;
612         } else {
613             strcpy(newPath, cpath);
614         }
615     } else {
616         newPath = (char *)malloc(strlen(relativeTo) + 1 + strlen(cpath) + 1);
617         if (!newPath) {
618             status = ENOMEM;
619         } else {
620             sprintf(newPath, "%s/%s", relativeTo, cpath);
621         }
622     }
623
624     if (status == 0) {
625         FilepathNormalize(newPath);
626
627         /* return buffer containing fully specified path */
628         *fullPathBufp = newPath;
629     }
630
631     return status;
632 }
633 #endif /* AFS_NT40_ENV */
634
635
636 /*
637  * ConstructLocalBinPath() -- A convenience wrapper for ConstructLocalPath()
638  *     that specifies the canonical AFS server binary directory as the relative
639  *     directory.
640  */
641 int
642 ConstructLocalBinPath(const char *cpath, char **fullPathBufp)
643 {
644     return ConstructLocalPath(cpath, AFSDIR_CANONICAL_SERVER_BIN_DIRPATH,
645                               fullPathBufp);
646 }
647
648
649 /*
650  * ConstructLocalLogPath() -- A convenience wrapper for ConstructLocalPath()
651  *     that specifies the canonical AFS server logs directory as the relative
652  *     directory.
653  */
654 int
655 ConstructLocalLogPath(const char *cpath, char **fullPathBufp)
656 {
657     return ConstructLocalPath(cpath, AFSDIR_CANONICAL_SERVER_LOGS_DIRPATH,
658                               fullPathBufp);
659 }