Actual source code: ftest.c
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
22: #if defined(PETSC_HAVE__ACCESS) || defined(PETSC_HAVE_ACCESS)
24: static PetscErrorCode PetscTestOwnership(const char fname[], char mode, uid_t fuid, gid_t fgid, int fmode, PetscBool *flg)
25: {
26: int m = R_OK;
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: PetscInfo(NULL, "System call access() succeeded on file %s\n", fname);
35: *flg = PETSC_TRUE;
36: } else {
37: PetscInfo(NULL, "System call access() failed on file %s\n", fname);
38: *flg = PETSC_FALSE;
39: }
40: #else
42: if (!_access(fname, m)) *flg = PETSC_TRUE;
43: #endif
44: return 0;
45: }
47: #else /* PETSC_HAVE_ACCESS or PETSC_HAVE__ACCESS */
49: static PetscErrorCode PetscTestOwnership(const char fname[], char mode, uid_t fuid, gid_t fgid, int fmode, PetscBool *flg)
50: {
51: uid_t uid;
52: gid_t *gid = NULL;
53: int numGroups;
54: int rbit = S_IROTH;
55: int wbit = S_IWOTH;
56: int ebit = S_IXOTH;
57: #if !defined(PETSC_MISSING_GETGROUPS)
58: int err;
59: #endif
61: /* Get the number of supplementary group IDs */
62: #if !defined(PETSC_MISSING_GETGROUPS)
63: numGroups = getgroups(0, gid);
65: PetscMalloc1(numGroups + 1, &gid);
66: #else
67: numGroups = 0;
68: #endif
70: /* Get the (effective) user and group of the caller */
71: uid = geteuid();
72: gid[0] = getegid();
74: /* Get supplementary group IDs */
75: #if !defined(PETSC_MISSING_GETGROUPS)
76: err = getgroups(numGroups, gid + 1);
78: #endif
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;
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: PetscFree(gid);
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: return 0;
107: }
109: #endif /* PETSC_HAVE_ACCESS */
111: static PetscErrorCode PetscGetFileStat(const char fname[], uid_t *fileUid, gid_t *fileGid, int *fileMode, PetscBool *exists)
112: {
113: struct stat statbuf;
116: *fileMode = 0;
117: *exists = PETSC_FALSE;
118: #if defined(PETSC_HAVE_STAT_NO_CONST)
119: stat((char *)fname, &statbuf);
120: #else
121: stat(fname, &statbuf);
122: #endif
123: if (ierr) {
124: #if defined(EOVERFLOW)
126: #endif
127: PetscInfo(NULL, "System call stat() failed on file %s\n", fname);
128: *exists = PETSC_FALSE;
129: } else {
130: 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: return 0;
137: }
139: /*@C
140: PetscTestFile - checks for the existence of a file
142: Not Collective
144: Input Parameters:
145: + fname - the filename
146: - mode - either 'r', 'w', 'x' or '\0'
148: Output Parameter:
149: . flg - the file exists and satisfies the mode
151: Level: intermediate
153: Note:
154: If mode is '\0', no permissions checks are performed
156: .seealso: `PetscTestDirectory()`, `PetscLs()`
157: @*/
158: PetscErrorCode PetscTestFile(const char fname[], char mode, PetscBool *flg)
159: {
160: uid_t fuid;
161: gid_t fgid;
162: int fmode;
163: PetscBool exists;
165: *flg = PETSC_FALSE;
166: if (!fname) return 0;
168: PetscGetFileStat(fname, &fuid, &fgid, &fmode, &exists);
169: if (!exists) return 0;
170: /* Except for systems that have this broken stat macros (rare), this is the correct way to check for a regular file */
171: if (!S_ISREG(fmode)) return 0;
172: /* return if asked to check for existence only */
173: if (mode == '\0') {
174: *flg = exists;
175: return 0;
176: }
177: PetscTestOwnership(fname, mode, fuid, fgid, fmode, flg);
178: return 0;
179: }
181: /*@C
182: PetscTestDirectory - checks for the existence of a directory
184: Not Collective
186: Input Parameters:
187: + dirname - the directory name
188: - mode - either 'r', 'w', or 'x'
190: Output Parameter:
191: . flg - the directory exists and satisfies the mode
193: Level: intermediate
195: .seealso: `PetscTestFile()`, `PetscLs()`
196: @*/
197: PetscErrorCode PetscTestDirectory(const char dirname[], char mode, PetscBool *flg)
198: {
199: uid_t fuid;
200: gid_t fgid;
201: int fmode;
202: PetscBool exists;
204: *flg = PETSC_FALSE;
205: if (!dirname) return 0;
207: PetscGetFileStat(dirname, &fuid, &fgid, &fmode, &exists);
208: if (!exists) return 0;
209: /* Except for systems that have this broken stat macros (rare), this
210: is the correct way to check for a directory */
211: if (!S_ISDIR(fmode)) return 0;
213: PetscTestOwnership(dirname, mode, fuid, fgid, fmode, flg);
214: return 0;
215: }
217: /*@C
218: PetscLs - produce a listing of the files in a directory
220: Collective
222: Input Parameters:
223: + comm - the MPI communicator
224: . dirname - the directory name
225: - tlen - the length of the buffer found[]
227: Output Parameters:
228: + found - listing of files
229: - flg - the directory exists
231: Level: intermediate
233: .seealso: `PetscTestFile()`, `PetscLs()`
234: @*/
235: PetscErrorCode PetscLs(MPI_Comm comm, const char dirname[], char found[], size_t tlen, PetscBool *flg)
236: {
237: size_t len;
238: char *f, program[PETSC_MAX_PATH_LEN];
239: FILE *fp;
241: PetscStrcpy(program, "ls ");
242: PetscStrcat(program, dirname);
243: #if defined(PETSC_HAVE_POPEN)
244: PetscPOpen(comm, NULL, program, "r", &fp);
245: #else
246: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP_SYS, "Cannot run external programs on this machine");
247: #endif
248: f = fgets(found, tlen, fp);
249: if (f) *flg = PETSC_TRUE;
250: else *flg = PETSC_FALSE;
251: while (f) {
252: PetscStrlen(found, &len);
253: f = fgets(found + len, tlen - len, fp);
254: }
255: if (*flg) PetscInfo(NULL, "ls on %s gives \n%s\n", dirname, found);
256: #if defined(PETSC_HAVE_POPEN)
257: PetscPClose(comm, fp);
258: #else
259: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP_SYS, "Cannot run external programs on this machine");
260: #endif
261: return 0;
262: }