Actual source code: ftest.c
petsc-3.3-p7 2013-05-11
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/types.h>
9: #include <sys/stat.h>
10: #if defined(PETSC_HAVE_UNISTD_H)
11: #include <unistd.h>
12: #endif
13: #if defined(PETSC_HAVE_STDLIB_H)
14: #include <stdlib.h>
15: #endif
16: #if defined(PETSC_HAVE_SYS_UTSNAME_H)
17: #include <sys/utsname.h>
18: #endif
19: #if defined(PETSC_HAVE_IO_H)
20: #include <io.h>
21: #endif
22: #if defined(PETSC_HAVE_SYS_SYSTEMINFO_H)
23: #include <sys/systeminfo.h>
24: #endif
26: #if defined (PETSC_HAVE__ACCESS) || defined(PETSC_HAVE_ACCESS)
30: static PetscErrorCode PetscTestOwnership(const char fname[], char mode, uid_t fuid, gid_t fgid, int fmode, PetscBool *flg)
31: {
32: int m = R_OK;
36: if (mode == 'r') m = R_OK;
37: else if (mode == 'w') m = W_OK;
38: else if (mode == 'x') m = X_OK;
39: else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG, "Mode must be one of r, w, or x");
40: #if defined(PETSC_HAVE_ACCESS)
41: if (!access(fname, m)) {
42: PetscInfo1(PETSC_NULL,"System call access() succeeded on file %s\n",fname);
43: *flg = PETSC_TRUE;
44: } else {
45: PetscInfo1(PETSC_NULL,"System call access() failed on file %s\n",fname);
46: *flg = PETSC_FALSE;
47: }
48: #else
49: if (m == X_OK) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP, "Unable to check execute permission for file %s", fname);
50: if(!_access(fname, m)) *flg = PETSC_TRUE;
51: #endif
52: return(0);
53: }
55: #else /* PETSC_HAVE_ACCESS or PETSC_HAVE__ACCESS */
59: static PetscErrorCode PetscTestOwnership(const char fname[], char mode, uid_t fuid, gid_t fgid, int fmode, PetscBool *flg)
60: {
61: uid_t uid;
62: gid_t *gid = PETSC_NULL;
63: int numGroups;
64: int rbit = S_IROTH;
65: int wbit = S_IWOTH;
66: int ebit = S_IXOTH;
70: /* Get the number of supplementary group IDs */
71: #if !defined(PETSC_MISSING_GETGROUPS)
72: numGroups = getgroups(0, gid); if (numGroups < 0) SETERRQ(PETSC_COMM_SELF,numGroups, "Unable to count supplementary group IDs");
73: PetscMalloc((numGroups+1) * sizeof(gid_t), &gid);
74: #else
75: numGroups = 0;
76: #endif
78: /* Get the (effective) user and group of the caller */
79: uid = geteuid();
80: gid[0] = getegid();
82: /* Get supplementary group IDs */
83: #if !defined(PETSC_MISSING_GETGROUPS)
84: getgroups(numGroups, gid+1); if (ierr < 0) SETERRQ(PETSC_COMM_SELF,ierr, "Unable to obtain supplementary group IDs");
85: #endif
87: /* Test for accessibility */
88: if (fuid == uid) {
89: rbit = S_IRUSR;
90: wbit = S_IWUSR;
91: ebit = S_IXUSR;
92: } else {
93: int g;
95: for(g = 0; g <= numGroups; g++) {
96: if (fgid == gid[g]) {
97: rbit = S_IRGRP;
98: wbit = S_IWGRP;
99: ebit = S_IXGRP;
100: break;
101: }
102: }
103: }
104: PetscFree(gid);
106: if (mode == 'r') {
107: if (fmode & rbit) *flg = PETSC_TRUE;
108: } else if (mode == 'w') {
109: if (fmode & wbit) *flg = PETSC_TRUE;
110: } else if (mode == 'x') {
111: if (fmode & ebit) *flg = PETSC_TRUE;
112: }
113: return(0);
114: }
116: #endif /* PETSC_HAVE_ACCESS */
120: static PetscErrorCode PetscGetFileStat(const char fname[], uid_t *fileUid, gid_t *fileGid, int *fileMode,PetscBool *exists)
121: {
122: struct stat statbuf;
126: #if defined(PETSC_HAVE_STAT_NO_CONST)
127: stat((char*) fname, &statbuf);
128: #else
129: stat(fname, &statbuf);
130: #endif
131: if (ierr) {
132: #if defined(EOVERFLOW)
133: if (errno == EOVERFLOW) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"EOVERFLOW in stat(), configure PETSc --with-large-file-io=1 to support files larger than 2GiB");
134: #endif
135: PetscInfo1(PETSC_NULL,"System call stat() failed on file %s\n",fname);
136: *exists = PETSC_FALSE;
137: } else {
138: PetscInfo1(PETSC_NULL,"System call stat() succeeded on file %s\n",fname);
139: *exists = PETSC_TRUE;
140: *fileUid = statbuf.st_uid;
141: *fileGid = statbuf.st_gid;
142: *fileMode = statbuf.st_mode;
143: }
144: return(0);
145: }
149: PetscErrorCode PetscTestFile(const char fname[], char mode, PetscBool *flg)
150: {
151: uid_t fuid;
152: gid_t fgid;
153: int fmode;
155: PetscBool exists;
158: *flg = PETSC_FALSE;
159: if (!fname) return(0);
161: PetscGetFileStat(fname, &fuid, &fgid, &fmode,&exists);
162: if (!exists) return(0);
163: /* Except for systems that have this broken stat macros (rare), this is the correct way to check for a regular file */
164: if (!S_ISREG(fmode)) return(0);
166: PetscTestOwnership(fname, mode, fuid, fgid, fmode, flg);
167: return(0);
168: }
172: PetscErrorCode PetscTestDirectory(const char fname[],char mode,PetscBool *flg)
173: {
174: uid_t fuid;
175: gid_t fgid;
176: int fmode;
178: PetscBool exists;
181: *flg = PETSC_FALSE;
182: if (!fname) return(0);
184: PetscGetFileStat(fname, &fuid, &fgid, &fmode,&exists);
185: if (!exists) return(0);
186: /* Except for systems that have this broken stat macros (rare), this
187: is the correct way to check for a directory */
188: if (!S_ISDIR(fmode)) return(0);
190: PetscTestOwnership(fname, mode, fuid, fgid, fmode, flg);
191: return(0);
192: }
196: PetscErrorCode PetscLs(MPI_Comm comm,const char libname[],char found[],size_t tlen,PetscBool *flg)
197: {
199: size_t len;
200: char *f,program[PETSC_MAX_PATH_LEN];
201: FILE *fp;
204: PetscStrcpy(program,"ls ");
205: PetscStrcat(program,libname);
206: #if defined(PETSC_HAVE_POPEN)
207: PetscPOpen(comm,PETSC_NULL,program,"r",&fp);
208: #else
209: SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP_SYS,"Cannot run external programs on this machine");
210: #endif
211: f = fgets(found,tlen,fp);
212: if (f) *flg = PETSC_TRUE; else *flg = PETSC_FALSE;
213: while (f) {
214: PetscStrlen(found,&len);
215: f = fgets(found+len,tlen-len,fp);
216: }
217: if (*flg) {PetscInfo2(0,"ls on %s gives \n%s\n",libname,found);}
218: #if defined(PETSC_HAVE_POPEN)
219: PetscPClose(comm,fp);
220: #else
221: SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP_SYS,"Cannot run external programs on this machine");
222: #endif
223: return(0);
224: }