xref: /petsc/src/sys/fileio/ftest.c (revision 9371c9d470a9602b6d10a8bf50c9b2280a79e45a)
1e5c89e4eSSatish Balay 
2c6db04a5SJed Brown #include <petscsys.h>
37fe8d12eSJed Brown #include <errno.h>
4e5c89e4eSSatish Balay #if defined(PETSC_HAVE_PWD_H)
5e5c89e4eSSatish Balay #include <pwd.h>
6e5c89e4eSSatish Balay #endif
7e5c89e4eSSatish Balay #include <ctype.h>
8e5c89e4eSSatish Balay #include <sys/stat.h>
9e5c89e4eSSatish Balay #if defined(PETSC_HAVE_UNISTD_H)
10e5c89e4eSSatish Balay #include <unistd.h>
11e5c89e4eSSatish Balay #endif
12e5c89e4eSSatish Balay #if defined(PETSC_HAVE_SYS_UTSNAME_H)
13e5c89e4eSSatish Balay #include <sys/utsname.h>
14e5c89e4eSSatish Balay #endif
15e5c89e4eSSatish Balay #if defined(PETSC_HAVE_IO_H)
16e5c89e4eSSatish Balay #include <io.h>
17e5c89e4eSSatish Balay #endif
18e5c89e4eSSatish Balay #if defined(PETSC_HAVE_SYS_SYSTEMINFO_H)
19e5c89e4eSSatish Balay #include <sys/systeminfo.h>
20e5c89e4eSSatish Balay #endif
21e5c89e4eSSatish Balay 
22e5c89e4eSSatish Balay #if defined(PETSC_HAVE__ACCESS) || defined(PETSC_HAVE_ACCESS)
23e5c89e4eSSatish Balay 
24*9371c9d4SSatish Balay static PetscErrorCode PetscTestOwnership(const char fname[], char mode, uid_t fuid, gid_t fgid, int fmode, PetscBool *flg) {
25e5c89e4eSSatish Balay   int m = R_OK;
26e5c89e4eSSatish Balay 
27e5c89e4eSSatish Balay   PetscFunctionBegin;
28e5c89e4eSSatish Balay   if (mode == 'r') m = R_OK;
29e5c89e4eSSatish Balay   else if (mode == 'w') m = W_OK;
30e5c89e4eSSatish Balay   else if (mode == 'x') m = X_OK;
31e32f2f54SBarry Smith   else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Mode must be one of r, w, or x");
32e5c89e4eSSatish Balay #if defined(PETSC_HAVE_ACCESS)
337fe8d12eSJed Brown   if (!access(fname, m)) {
349566063dSJacob Faibussowitsch     PetscCall(PetscInfo(NULL, "System call access() succeeded on file %s\n", fname));
357fe8d12eSJed Brown     *flg = PETSC_TRUE;
367fe8d12eSJed Brown   } else {
379566063dSJacob Faibussowitsch     PetscCall(PetscInfo(NULL, "System call access() failed on file %s\n", fname));
387fe8d12eSJed Brown     *flg = PETSC_FALSE;
397fe8d12eSJed Brown   }
40e5c89e4eSSatish Balay #else
4108401ef6SPierre Jolivet   PetscCheck(m != X_OK, PETSC_COMM_SELF, PETSC_ERR_SUP, "Unable to check execute permission for file %s", fname);
42e5c89e4eSSatish Balay   if (!_access(fname, m)) *flg = PETSC_TRUE;
43e5c89e4eSSatish Balay #endif
44e5c89e4eSSatish Balay   PetscFunctionReturn(0);
45e5c89e4eSSatish Balay }
46e5c89e4eSSatish Balay 
47e5c89e4eSSatish Balay #else /* PETSC_HAVE_ACCESS or PETSC_HAVE__ACCESS */
48e5c89e4eSSatish Balay 
49*9371c9d4SSatish Balay static PetscErrorCode PetscTestOwnership(const char fname[], char mode, uid_t fuid, gid_t fgid, int fmode, PetscBool *flg) {
50e5c89e4eSSatish Balay   uid_t  uid;
510298fd71SBarry Smith   gid_t *gid = NULL;
52e5c89e4eSSatish Balay   int    numGroups;
53e5c89e4eSSatish Balay   int    rbit = S_IROTH;
54e5c89e4eSSatish Balay   int    wbit = S_IWOTH;
55e5c89e4eSSatish Balay   int    ebit = S_IXOTH;
562da392ccSBarry Smith #if !defined(PETSC_MISSING_GETGROUPS)
572da392ccSBarry Smith   int    err;
582da392ccSBarry Smith #endif
59e5c89e4eSSatish Balay 
60e5c89e4eSSatish Balay   PetscFunctionBegin;
61e5c89e4eSSatish Balay   /* Get the number of supplementary group IDs */
62e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_GETGROUPS)
63*9371c9d4SSatish Balay   numGroups = getgroups(0, gid);
64*9371c9d4SSatish Balay   PetscCheck(numGroups >= 0, PETSC_COMM_SELF, PETSC_ERR_SYS, "Unable to count supplementary group IDs");
659566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(numGroups + 1, &gid));
66e5c89e4eSSatish Balay #else
67e5c89e4eSSatish Balay   numGroups = 0;
68e5c89e4eSSatish Balay #endif
69e5c89e4eSSatish Balay 
70e5c89e4eSSatish Balay   /* Get the (effective) user and group of the caller */
71e5c89e4eSSatish Balay   uid    = geteuid();
72e5c89e4eSSatish Balay   gid[0] = getegid();
73e5c89e4eSSatish Balay 
74e5c89e4eSSatish Balay   /* Get supplementary group IDs */
75e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_GETGROUPS)
76*9371c9d4SSatish Balay   err = getgroups(numGroups, gid + 1);
77*9371c9d4SSatish Balay   PetscCheck(err >= 0, PETSC_COMM_SELF, PETSC_ERR_SYS, "Unable to obtain supplementary group IDs");
78e5c89e4eSSatish Balay #endif
79e5c89e4eSSatish Balay 
80e5c89e4eSSatish Balay   /* Test for accessibility */
81e5c89e4eSSatish Balay   if (fuid == uid) {
82e5c89e4eSSatish Balay     rbit = S_IRUSR;
83e5c89e4eSSatish Balay     wbit = S_IWUSR;
84e5c89e4eSSatish Balay     ebit = S_IXUSR;
85e5c89e4eSSatish Balay   } else {
86e5c89e4eSSatish Balay     int g;
87e5c89e4eSSatish Balay 
88e5c89e4eSSatish Balay     for (g = 0; g <= numGroups; g++) {
89e5c89e4eSSatish Balay       if (fgid == gid[g]) {
90e5c89e4eSSatish Balay         rbit = S_IRGRP;
91e5c89e4eSSatish Balay         wbit = S_IWGRP;
92e5c89e4eSSatish Balay         ebit = S_IXGRP;
93e5c89e4eSSatish Balay         break;
94e5c89e4eSSatish Balay       }
95e5c89e4eSSatish Balay     }
96e5c89e4eSSatish Balay   }
979566063dSJacob Faibussowitsch   PetscCall(PetscFree(gid));
98e5c89e4eSSatish Balay 
99e5c89e4eSSatish Balay   if (mode == 'r') {
100e5c89e4eSSatish Balay     if (fmode & rbit) *flg = PETSC_TRUE;
101e5c89e4eSSatish Balay   } else if (mode == 'w') {
102e5c89e4eSSatish Balay     if (fmode & wbit) *flg = PETSC_TRUE;
103e5c89e4eSSatish Balay   } else if (mode == 'x') {
104e5c89e4eSSatish Balay     if (fmode & ebit) *flg = PETSC_TRUE;
105e5c89e4eSSatish Balay   }
106e5c89e4eSSatish Balay   PetscFunctionReturn(0);
107e5c89e4eSSatish Balay }
108e5c89e4eSSatish Balay 
109e5c89e4eSSatish Balay #endif /* PETSC_HAVE_ACCESS */
110e5c89e4eSSatish Balay 
111*9371c9d4SSatish Balay static PetscErrorCode PetscGetFileStat(const char fname[], uid_t *fileUid, gid_t *fileGid, int *fileMode, PetscBool *exists) {
112e5c89e4eSSatish Balay   struct stat    statbuf;
113e5c89e4eSSatish Balay   PetscErrorCode ierr;
114e5c89e4eSSatish Balay 
115e5c89e4eSSatish Balay   PetscFunctionBegin;
11638ea73c8SJed Brown   *fileMode = 0;
11738ea73c8SJed Brown   *exists   = PETSC_FALSE;
118e5c89e4eSSatish Balay #if defined(PETSC_HAVE_STAT_NO_CONST)
119e5c89e4eSSatish Balay   ierr = stat((char *)fname, &statbuf);
120e5c89e4eSSatish Balay #else
121e5c89e4eSSatish Balay   ierr = stat(fname, &statbuf);
122e5c89e4eSSatish Balay #endif
123e5c89e4eSSatish Balay   if (ierr) {
1247ad82f04SSatish Balay #if defined(EOVERFLOW)
12508401ef6SPierre Jolivet     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");
1267ad82f04SSatish Balay #endif
1279566063dSJacob Faibussowitsch     PetscCall(PetscInfo(NULL, "System call stat() failed on file %s\n", fname));
128e5c89e4eSSatish Balay     *exists = PETSC_FALSE;
129e5c89e4eSSatish Balay   } else {
1309566063dSJacob Faibussowitsch     PetscCall(PetscInfo(NULL, "System call stat() succeeded on file %s\n", fname));
131e5c89e4eSSatish Balay     *exists   = PETSC_TRUE;
132e5c89e4eSSatish Balay     *fileUid  = statbuf.st_uid;
133e5c89e4eSSatish Balay     *fileGid  = statbuf.st_gid;
134e5c89e4eSSatish Balay     *fileMode = statbuf.st_mode;
135e5c89e4eSSatish Balay   }
136e5c89e4eSSatish Balay   PetscFunctionReturn(0);
137e5c89e4eSSatish Balay }
138e5c89e4eSSatish Balay 
139e1d001d6SBarry Smith /*@C
14026b19047SBarry Smith    PetscTestFile - checks for the existence of a file
14126b19047SBarry Smith 
14226b19047SBarry Smith    Not Collective
14326b19047SBarry Smith 
144d8d19677SJose E. Roman    Input Parameters:
14526b19047SBarry Smith +  fname - the filename
14655819941SStefano Zampini -  mode - either 'r', 'w', 'x' or '\0'
14726b19047SBarry Smith 
14826b19047SBarry Smith    Output Parameter:
14926b19047SBarry Smith .  flg - the file exists and satisfies the mode
15026b19047SBarry Smith 
1511b266c99SBarry Smith    Level: intermediate
1521b266c99SBarry Smith 
15355819941SStefano Zampini    Notes: if mode is '\0', no permissions checks are performed
15455819941SStefano Zampini 
155db781477SPatrick Sanan .seealso: `PetscTestDirectory()`, `PetscLs()`
15626b19047SBarry Smith @*/
157*9371c9d4SSatish Balay PetscErrorCode PetscTestFile(const char fname[], char mode, PetscBool *flg) {
158e5c89e4eSSatish Balay   uid_t     fuid;
159e5c89e4eSSatish Balay   gid_t     fgid;
160e5c89e4eSSatish Balay   int       fmode;
161ace3abfcSBarry Smith   PetscBool exists;
162e5c89e4eSSatish Balay 
163e5c89e4eSSatish Balay   PetscFunctionBegin;
164e5c89e4eSSatish Balay   *flg = PETSC_FALSE;
165e5c89e4eSSatish Balay   if (!fname) PetscFunctionReturn(0);
166e5c89e4eSSatish Balay 
1679566063dSJacob Faibussowitsch   PetscCall(PetscGetFileStat(fname, &fuid, &fgid, &fmode, &exists));
168e5c89e4eSSatish Balay   if (!exists) PetscFunctionReturn(0);
1697fe8d12eSJed Brown   /* Except for systems that have this broken stat macros (rare), this is the correct way to check for a regular file */
170e5c89e4eSSatish Balay   if (!S_ISREG(fmode)) PetscFunctionReturn(0);
17155819941SStefano Zampini   /* return if asked to check for existence only */
172*9371c9d4SSatish Balay   if (mode == '\0') {
173*9371c9d4SSatish Balay     *flg = exists;
174*9371c9d4SSatish Balay     PetscFunctionReturn(0);
175*9371c9d4SSatish Balay   }
1769566063dSJacob Faibussowitsch   PetscCall(PetscTestOwnership(fname, mode, fuid, fgid, fmode, flg));
177e5c89e4eSSatish Balay   PetscFunctionReturn(0);
178e5c89e4eSSatish Balay }
179e5c89e4eSSatish Balay 
180e1d001d6SBarry Smith /*@C
18126b19047SBarry Smith    PetscTestDirectory - checks for the existence of a directory
18226b19047SBarry Smith 
18326b19047SBarry Smith    Not Collective
18426b19047SBarry Smith 
185d8d19677SJose E. Roman    Input Parameters:
18626b19047SBarry Smith +  dirname - the directory name
18726b19047SBarry Smith -  mode - either 'r', 'w', or 'x'
18826b19047SBarry Smith 
18926b19047SBarry Smith    Output Parameter:
19026b19047SBarry Smith .  flg - the directory exists and satisfies the mode
19126b19047SBarry Smith 
1921b266c99SBarry Smith    Level: intermediate
1931b266c99SBarry Smith 
194db781477SPatrick Sanan .seealso: `PetscTestFile()`, `PetscLs()`
19526b19047SBarry Smith @*/
196*9371c9d4SSatish Balay PetscErrorCode PetscTestDirectory(const char dirname[], char mode, PetscBool *flg) {
197e5c89e4eSSatish Balay   uid_t     fuid;
198e5c89e4eSSatish Balay   gid_t     fgid;
199e5c89e4eSSatish Balay   int       fmode;
200ace3abfcSBarry Smith   PetscBool exists;
201e5c89e4eSSatish Balay 
202e5c89e4eSSatish Balay   PetscFunctionBegin;
203e5c89e4eSSatish Balay   *flg = PETSC_FALSE;
20426b19047SBarry Smith   if (!dirname) PetscFunctionReturn(0);
205e5c89e4eSSatish Balay 
2069566063dSJacob Faibussowitsch   PetscCall(PetscGetFileStat(dirname, &fuid, &fgid, &fmode, &exists));
207e5c89e4eSSatish Balay   if (!exists) PetscFunctionReturn(0);
208e5c89e4eSSatish Balay   /* Except for systems that have this broken stat macros (rare), this
209e5c89e4eSSatish Balay      is the correct way to check for a directory */
210e5c89e4eSSatish Balay   if (!S_ISDIR(fmode)) PetscFunctionReturn(0);
211e5c89e4eSSatish Balay 
2129566063dSJacob Faibussowitsch   PetscCall(PetscTestOwnership(dirname, mode, fuid, fgid, fmode, flg));
213e5c89e4eSSatish Balay   PetscFunctionReturn(0);
214e5c89e4eSSatish Balay }
215e5c89e4eSSatish Balay 
216e1d001d6SBarry Smith /*@C
21726b19047SBarry Smith    PetscLs - produce a listing of the files in a directory
21826b19047SBarry Smith 
219d083f849SBarry Smith    Collective
22026b19047SBarry Smith 
221d8d19677SJose E. Roman    Input Parameters:
22226b19047SBarry Smith +  comm - the MPI communicator
22326b19047SBarry Smith .  dirname - the directory name
22426b19047SBarry Smith -  tlen - the length of the buffer found[]
22526b19047SBarry Smith 
226d8d19677SJose E. Roman    Output Parameters:
22726b19047SBarry Smith +  found - listing of files
22826b19047SBarry Smith -  flg - the directory exists
22926b19047SBarry Smith 
2301b266c99SBarry Smith    Level: intermediate
2311b266c99SBarry Smith 
232db781477SPatrick Sanan .seealso: `PetscTestFile()`, `PetscLs()`
23326b19047SBarry Smith @*/
234*9371c9d4SSatish Balay PetscErrorCode PetscLs(MPI_Comm comm, const char dirname[], char found[], size_t tlen, PetscBool *flg) {
235e5c89e4eSSatish Balay   size_t len;
236e5c89e4eSSatish Balay   char  *f, program[PETSC_MAX_PATH_LEN];
237e5c89e4eSSatish Balay   FILE  *fp;
238e5c89e4eSSatish Balay 
239e5c89e4eSSatish Balay   PetscFunctionBegin;
2409566063dSJacob Faibussowitsch   PetscCall(PetscStrcpy(program, "ls "));
2419566063dSJacob Faibussowitsch   PetscCall(PetscStrcat(program, dirname));
242e5c89e4eSSatish Balay #if defined(PETSC_HAVE_POPEN)
2439566063dSJacob Faibussowitsch   PetscCall(PetscPOpen(comm, NULL, program, "r", &fp));
244e5c89e4eSSatish Balay #else
245e32f2f54SBarry Smith   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP_SYS, "Cannot run external programs on this machine");
246e5c89e4eSSatish Balay #endif
247e5c89e4eSSatish Balay   f = fgets(found, tlen, fp);
248a297a907SKarl Rupp   if (f) *flg = PETSC_TRUE;
249a297a907SKarl Rupp   else *flg = PETSC_FALSE;
250e5c89e4eSSatish Balay   while (f) {
2519566063dSJacob Faibussowitsch     PetscCall(PetscStrlen(found, &len));
252e5c89e4eSSatish Balay     f = fgets(found + len, tlen - len, fp);
253e5c89e4eSSatish Balay   }
2549566063dSJacob Faibussowitsch   if (*flg) PetscCall(PetscInfo(NULL, "ls on %s gives \n%s\n", dirname, found));
255e5c89e4eSSatish Balay #if defined(PETSC_HAVE_POPEN)
2569566063dSJacob Faibussowitsch   PetscCall(PetscPClose(comm, fp));
257e5c89e4eSSatish Balay #else
258e32f2f54SBarry Smith   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP_SYS, "Cannot run external programs on this machine");
259e5c89e4eSSatish Balay #endif
260e5c89e4eSSatish Balay   PetscFunctionReturn(0);
261e5c89e4eSSatish Balay }
262