xref: /petsc/src/sys/fileio/grpath.c (revision 9371c9d470a9602b6d10a8bf50c9b2280a79e45a)
1 #define PETSC_DESIRE_FEATURE_TEST_MACROS /* for realpath() */
2 #include <petscsys.h>
3 #if defined(PETSC_HAVE_PWD_H)
4 #include <pwd.h>
5 #endif
6 #include <ctype.h>
7 #include <sys/stat.h>
8 #if defined(PETSC_HAVE_UNISTD_H)
9 #include <unistd.h>
10 #endif
11 #if defined(PETSC_HAVE_SYS_UTSNAME_H)
12 #include <sys/utsname.h>
13 #endif
14 #if defined(PETSC_HAVE_SYS_SYSTEMINFO_H)
15 #include <sys/systeminfo.h>
16 #endif
17 
18 /*@C
19    PetscGetRealPath - Get the path without symbolic links etc. and in absolute form.
20 
21    Not Collective
22 
23    Input Parameter:
24 .  path - path to resolve
25 
26    Output Parameter:
27 .  rpath - resolved path
28 
29    Level: developer
30 
31    Notes:
32    rpath is assumed to be of length PETSC_MAX_PATH_LEN.
33 
34    Systems that use the automounter often generate absolute paths
35    of the form "/tmp_mnt....".  However, the automounter will fail to
36    mount this path if it is not already mounted, so we remove this from
37    the head of the line.  This may cause problems if, for some reason,
38    /tmp_mnt is valid and not the result of the automounter.
39 
40 .seealso: `PetscGetFullPath()`
41 @*/
42 PetscErrorCode PetscGetRealPath(const char path[], char rpath[]) {
43   char      tmp3[PETSC_MAX_PATH_LEN];
44   PetscBool flg;
45 #if !defined(PETSC_HAVE_REALPATH) && defined(PETSC_HAVE_READLINK)
46   char   tmp1[PETSC_MAX_PATH_LEN], tmp4[PETSC_MAX_PATH_LEN], *tmp2;
47   size_t N, len, len1, len2;
48   int    n, m;
49 #endif
50 
51   PetscFunctionBegin;
52 #if defined(PETSC_HAVE_REALPATH)
53   PetscCheck(realpath(path, rpath), PETSC_COMM_SELF, PETSC_ERR_LIB, "realpath()");
54 #elif defined(PETSC_HAVE_READLINK)
55   /* Algorithm: we move through the path, replacing links with the real paths.   */
56   PetscCall(PetscStrcpy(rpath, path));
57   PetscCall(PetscStrlen(rpath, &N));
58   while (N) {
59     PetscCall(PetscStrncpy(tmp1, rpath, N));
60     tmp1[N] = 0;
61     n       = readlink(tmp1, tmp3, PETSC_MAX_PATH_LEN);
62     if (n > 0) {
63       tmp3[n] = 0; /* readlink does not automatically add 0 to string end */
64       if (tmp3[0] != '/') {
65         PetscCall(PetscStrchr(tmp1, '/', &tmp2));
66         PetscCall(PetscStrlen(tmp1, &len1));
67         PetscCall(PetscStrlen(tmp2, &len2));
68         m = len1 - len2;
69         PetscCall(PetscStrncpy(tmp4, tmp1, m));
70         tmp4[m] = 0;
71         PetscCall(PetscStrlen(tmp4, &len));
72         PetscCall(PetscStrlcat(tmp4, "/", PETSC_MAX_PATH_LEN));
73         PetscCall(PetscStrlcat(tmp4, tmp3, PETSC_MAX_PATH_LEN));
74         PetscCall(PetscGetRealPath(tmp4, rpath));
75         PetscCall(PetscStrlcat(rpath, path + N, PETSC_MAX_PATH_LEN));
76       } else {
77         PetscCall(PetscGetRealPath(tmp3, tmp1));
78         PetscCall(PetscStrncpy(rpath, tmp1, PETSC_MAX_PATH_LEN));
79         PetscCall(PetscStrlcat(rpath, path + N, PETSC_MAX_PATH_LEN));
80       }
81       PetscFunctionReturn(0);
82     }
83     PetscCall(PetscStrchr(tmp1, '/', &tmp2));
84     if (tmp2) {
85       PetscCall(PetscStrlen(tmp1, &len1));
86       PetscCall(PetscStrlen(tmp2, &len2));
87       N = len1 - len2;
88     } else {
89       PetscCall(PetscStrlen(tmp1, &N));
90     }
91   }
92   PetscCall(PetscStrncpy(rpath, path, PETSC_MAX_PATH_LEN));
93 #else /* Just punt */
94   PetscCall(PetscStrcpy(rpath, path));
95 #endif
96 
97   /* remove garbage some automounters put at the beginning of the path */
98   PetscCall(PetscStrncmp("/tmp_mnt/", rpath, 9, &flg));
99   if (flg) {
100     PetscCall(PetscStrcpy(tmp3, rpath + 8));
101     PetscCall(PetscStrcpy(rpath, tmp3));
102   }
103   PetscFunctionReturn(0);
104 }
105