xref: /petsc/src/sys/fileio/ftest.c (revision a69119a591a03a9d906b29c0a4e9802e4d7c9795)
1 
2 #include <petscsys.h>
3 #include <errno.h>
4 #if defined(PETSC_HAVE_PWD_H)
5 #include <pwd.h>
6 #endif
7 #include <ctype.h>
8 #include <sys/stat.h>
9 #if defined(PETSC_HAVE_UNISTD_H)
10 #include <unistd.h>
11 #endif
12 #if defined(PETSC_HAVE_SYS_UTSNAME_H)
13 #include <sys/utsname.h>
14 #endif
15 #if defined(PETSC_HAVE_IO_H)
16 #include <io.h>
17 #endif
18 #if defined(PETSC_HAVE_SYS_SYSTEMINFO_H)
19 #include <sys/systeminfo.h>
20 #endif
21 
22 #if defined(PETSC_HAVE__ACCESS) || defined(PETSC_HAVE_ACCESS)
23 
24 static PetscErrorCode PetscTestOwnership(const char fname[], char mode, uid_t fuid, gid_t fgid, int fmode, PetscBool *flg) {
25   int m = R_OK;
26 
27   PetscFunctionBegin;
28   if (mode == 'r') m = R_OK;
29   else if (mode == 'w') m = W_OK;
30   else if (mode == 'x') m = X_OK;
31   else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Mode must be one of r, w, or x");
32 #if defined(PETSC_HAVE_ACCESS)
33   if (!access(fname, m)) {
34     PetscCall(PetscInfo(NULL, "System call access() succeeded on file %s\n", fname));
35     *flg = PETSC_TRUE;
36   } else {
37     PetscCall(PetscInfo(NULL, "System call access() failed on file %s\n", fname));
38     *flg = PETSC_FALSE;
39   }
40 #else
41   PetscCheck(m != X_OK, PETSC_COMM_SELF, PETSC_ERR_SUP, "Unable to check execute permission for file %s", fname);
42   if (!_access(fname, m)) *flg = PETSC_TRUE;
43 #endif
44   PetscFunctionReturn(0);
45 }
46 
47 #else /* PETSC_HAVE_ACCESS or PETSC_HAVE__ACCESS */
48 
49 static PetscErrorCode PetscTestOwnership(const char fname[], char mode, uid_t fuid, gid_t fgid, int fmode, PetscBool *flg) {
50   uid_t  uid;
51   gid_t *gid = NULL;
52   int    numGroups;
53   int    rbit = S_IROTH;
54   int    wbit = S_IWOTH;
55   int    ebit = S_IXOTH;
56 #if !defined(PETSC_MISSING_GETGROUPS)
57   int    err;
58 #endif
59 
60   PetscFunctionBegin;
61   /* Get the number of supplementary group IDs */
62 #if !defined(PETSC_MISSING_GETGROUPS)
63   numGroups = getgroups(0, gid);
64   PetscCheck(numGroups >= 0, PETSC_COMM_SELF, PETSC_ERR_SYS, "Unable to count supplementary group IDs");
65   PetscCall(PetscMalloc1(numGroups + 1, &gid));
66 #else
67   numGroups = 0;
68 #endif
69 
70   /* Get the (effective) user and group of the caller */
71   uid    = geteuid();
72   gid[0] = getegid();
73 
74   /* Get supplementary group IDs */
75 #if !defined(PETSC_MISSING_GETGROUPS)
76   err = getgroups(numGroups, gid + 1);
77   PetscCheck(err >= 0, PETSC_COMM_SELF, PETSC_ERR_SYS, "Unable to obtain supplementary group IDs");
78 #endif
79 
80   /* Test for accessibility */
81   if (fuid == uid) {
82     rbit = S_IRUSR;
83     wbit = S_IWUSR;
84     ebit = S_IXUSR;
85   } else {
86     int g;
87 
88     for (g = 0; g <= numGroups; g++) {
89       if (fgid == gid[g]) {
90         rbit = S_IRGRP;
91         wbit = S_IWGRP;
92         ebit = S_IXGRP;
93         break;
94       }
95     }
96   }
97   PetscCall(PetscFree(gid));
98 
99   if (mode == 'r') {
100     if (fmode & rbit) *flg = PETSC_TRUE;
101   } else if (mode == 'w') {
102     if (fmode & wbit) *flg = PETSC_TRUE;
103   } else if (mode == 'x') {
104     if (fmode & ebit) *flg = PETSC_TRUE;
105   }
106   PetscFunctionReturn(0);
107 }
108 
109 #endif /* PETSC_HAVE_ACCESS */
110 
111 static PetscErrorCode PetscGetFileStat(const char fname[], uid_t *fileUid, gid_t *fileGid, int *fileMode, PetscBool *exists) {
112   struct stat    statbuf;
113   PetscErrorCode ierr;
114 
115   PetscFunctionBegin;
116   *fileMode = 0;
117   *exists   = PETSC_FALSE;
118 #if defined(PETSC_HAVE_STAT_NO_CONST)
119   ierr = stat((char *)fname, &statbuf);
120 #else
121   ierr = stat(fname, &statbuf);
122 #endif
123   if (ierr) {
124 #if defined(EOVERFLOW)
125     PetscCheck(errno != EOVERFLOW, PETSC_COMM_SELF, PETSC_ERR_SYS, "EOVERFLOW in stat(), configure PETSc --with-large-file-io=1 to support files larger than 2GiB");
126 #endif
127     PetscCall(PetscInfo(NULL, "System call stat() failed on file %s\n", fname));
128     *exists = PETSC_FALSE;
129   } else {
130     PetscCall(PetscInfo(NULL, "System call stat() succeeded on file %s\n", fname));
131     *exists   = PETSC_TRUE;
132     *fileUid  = statbuf.st_uid;
133     *fileGid  = statbuf.st_gid;
134     *fileMode = statbuf.st_mode;
135   }
136   PetscFunctionReturn(0);
137 }
138 
139 /*@C
140    PetscTestFile - checks for the existence of a file
141 
142    Not Collective
143 
144    Input Parameters:
145 +  fname - the filename
146 -  mode - either 'r', 'w', 'x' or '\0'
147 
148    Output Parameter:
149 .  flg - the file exists and satisfies the mode
150 
151    Level: intermediate
152 
153    Notes: if mode is '\0', no permissions checks are performed
154 
155 .seealso: `PetscTestDirectory()`, `PetscLs()`
156 @*/
157 PetscErrorCode PetscTestFile(const char fname[], char mode, PetscBool *flg) {
158   uid_t     fuid;
159   gid_t     fgid;
160   int       fmode;
161   PetscBool exists;
162 
163   PetscFunctionBegin;
164   *flg = PETSC_FALSE;
165   if (!fname) PetscFunctionReturn(0);
166 
167   PetscCall(PetscGetFileStat(fname, &fuid, &fgid, &fmode, &exists));
168   if (!exists) PetscFunctionReturn(0);
169   /* Except for systems that have this broken stat macros (rare), this is the correct way to check for a regular file */
170   if (!S_ISREG(fmode)) PetscFunctionReturn(0);
171   /* return if asked to check for existence only */
172   if (mode == '\0') {
173     *flg = exists;
174     PetscFunctionReturn(0);
175   }
176   PetscCall(PetscTestOwnership(fname, mode, fuid, fgid, fmode, flg));
177   PetscFunctionReturn(0);
178 }
179 
180 /*@C
181    PetscTestDirectory - checks for the existence of a directory
182 
183    Not Collective
184 
185    Input Parameters:
186 +  dirname - the directory name
187 -  mode - either 'r', 'w', or 'x'
188 
189    Output Parameter:
190 .  flg - the directory exists and satisfies the mode
191 
192    Level: intermediate
193 
194 .seealso: `PetscTestFile()`, `PetscLs()`
195 @*/
196 PetscErrorCode PetscTestDirectory(const char dirname[], char mode, PetscBool *flg) {
197   uid_t     fuid;
198   gid_t     fgid;
199   int       fmode;
200   PetscBool exists;
201 
202   PetscFunctionBegin;
203   *flg = PETSC_FALSE;
204   if (!dirname) PetscFunctionReturn(0);
205 
206   PetscCall(PetscGetFileStat(dirname, &fuid, &fgid, &fmode, &exists));
207   if (!exists) PetscFunctionReturn(0);
208   /* Except for systems that have this broken stat macros (rare), this
209      is the correct way to check for a directory */
210   if (!S_ISDIR(fmode)) PetscFunctionReturn(0);
211 
212   PetscCall(PetscTestOwnership(dirname, mode, fuid, fgid, fmode, flg));
213   PetscFunctionReturn(0);
214 }
215 
216 /*@C
217    PetscLs - produce a listing of the files in a directory
218 
219    Collective
220 
221    Input Parameters:
222 +  comm - the MPI communicator
223 .  dirname - the directory name
224 -  tlen - the length of the buffer found[]
225 
226    Output Parameters:
227 +  found - listing of files
228 -  flg - the directory exists
229 
230    Level: intermediate
231 
232 .seealso: `PetscTestFile()`, `PetscLs()`
233 @*/
234 PetscErrorCode PetscLs(MPI_Comm comm, const char dirname[], char found[], size_t tlen, PetscBool *flg) {
235   size_t len;
236   char  *f, program[PETSC_MAX_PATH_LEN];
237   FILE  *fp;
238 
239   PetscFunctionBegin;
240   PetscCall(PetscStrcpy(program, "ls "));
241   PetscCall(PetscStrcat(program, dirname));
242 #if defined(PETSC_HAVE_POPEN)
243   PetscCall(PetscPOpen(comm, NULL, program, "r", &fp));
244 #else
245   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP_SYS, "Cannot run external programs on this machine");
246 #endif
247   f = fgets(found, tlen, fp);
248   if (f) *flg = PETSC_TRUE;
249   else *flg = PETSC_FALSE;
250   while (f) {
251     PetscCall(PetscStrlen(found, &len));
252     f = fgets(found + len, tlen - len, fp);
253   }
254   if (*flg) PetscCall(PetscInfo(NULL, "ls on %s gives \n%s\n", dirname, found));
255 #if defined(PETSC_HAVE_POPEN)
256   PetscCall(PetscPClose(comm, fp));
257 #else
258   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP_SYS, "Cannot run external programs on this machine");
259 #endif
260   PetscFunctionReturn(0);
261 }
262