Actual source code: ftest.c

petsc-3.4.5 2014-06-29
  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)

 26: static PetscErrorCode PetscTestOwnership(const char fname[], char mode, uid_t fuid, gid_t fgid, int fmode, PetscBool  *flg)
 27: {
 28:   int            m = R_OK;

 32:   if (mode == 'r') m = R_OK;
 33:   else if (mode == 'w') m = W_OK;
 34:   else if (mode == 'x') m = X_OK;
 35:   else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG, "Mode must be one of r, w, or x");
 36: #if defined(PETSC_HAVE_ACCESS)
 37:   if (!access(fname, m)) {
 38:     PetscInfo1(NULL,"System call access() succeeded on file %s\n",fname);
 39:     *flg = PETSC_TRUE;
 40:   } else {
 41:     PetscInfo1(NULL,"System call access() failed on file %s\n",fname);
 42:     *flg = PETSC_FALSE;
 43:   }
 44: #else
 45:   if (m == X_OK) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP, "Unable to check execute permission for file %s", fname);
 46:   if (!_access(fname, m)) *flg = PETSC_TRUE;
 47: #endif
 48:   return(0);
 49: }

 51: #else  /* PETSC_HAVE_ACCESS or PETSC_HAVE__ACCESS */

 55: static PetscErrorCode PetscTestOwnership(const char fname[], char mode, uid_t fuid, gid_t fgid, int fmode, PetscBool  *flg)
 56: {
 57:   uid_t          uid;
 58:   gid_t          *gid = NULL;
 59:   int            numGroups;
 60:   int            rbit = S_IROTH;
 61:   int            wbit = S_IWOTH;
 62:   int            ebit = S_IXOTH;

 66:   /* Get the number of supplementary group IDs */
 67: #if !defined(PETSC_MISSING_GETGROUPS)
 68:   numGroups = getgroups(0, gid); if (numGroups < 0) SETERRQ(PETSC_COMM_SELF,numGroups, "Unable to count supplementary group IDs");
 69:   PetscMalloc((numGroups+1) * sizeof(gid_t), &gid);
 70: #else
 71:   numGroups = 0;
 72: #endif

 74:   /* Get the (effective) user and group of the caller */
 75:   uid    = geteuid();
 76:   gid[0] = getegid();

 78:   /* Get supplementary group IDs */
 79: #if !defined(PETSC_MISSING_GETGROUPS)
 80:   getgroups(numGroups, gid+1); if (ierr < 0) SETERRQ(PETSC_COMM_SELF,ierr, "Unable to obtain supplementary group IDs");
 81: #endif

 83:   /* Test for accessibility */
 84:   if (fuid == uid) {
 85:     rbit = S_IRUSR;
 86:     wbit = S_IWUSR;
 87:     ebit = S_IXUSR;
 88:   } else {
 89:     int g;

 91:     for (g = 0; g <= numGroups; g++) {
 92:       if (fgid == gid[g]) {
 93:         rbit = S_IRGRP;
 94:         wbit = S_IWGRP;
 95:         ebit = S_IXGRP;
 96:         break;
 97:       }
 98:     }
 99:   }
100:   PetscFree(gid);

102:   if (mode == 'r') {
103:     if (fmode & rbit) *flg = PETSC_TRUE;
104:   } else if (mode == 'w') {
105:     if (fmode & wbit) *flg = PETSC_TRUE;
106:   } else if (mode == 'x') {
107:     if (fmode & ebit) *flg = PETSC_TRUE;
108:   }
109:   return(0);
110: }

112: #endif /* PETSC_HAVE_ACCESS */

116: static PetscErrorCode PetscGetFileStat(const char fname[], uid_t *fileUid, gid_t *fileGid, int *fileMode,PetscBool  *exists)
117: {
118:   struct stat    statbuf;

122: #if defined(PETSC_HAVE_STAT_NO_CONST)
123:   stat((char*) fname, &statbuf);
124: #else
125:   stat(fname, &statbuf);
126: #endif
127:   if (ierr) {
128: #if defined(EOVERFLOW)
129:     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");
130: #endif
131:     PetscInfo1(NULL,"System call stat() failed on file %s\n",fname);
132:     *exists = PETSC_FALSE;
133:   } else {
134:     PetscInfo1(NULL,"System call stat() succeeded on file %s\n",fname);
135:     *exists   = PETSC_TRUE;
136:     *fileUid  = statbuf.st_uid;
137:     *fileGid  = statbuf.st_gid;
138:     *fileMode = statbuf.st_mode;
139:   }
140:   return(0);
141: }

145: PetscErrorCode  PetscTestFile(const char fname[], char mode, PetscBool  *flg)
146: {
147:   uid_t          fuid;
148:   gid_t          fgid;
149:   int            fmode;
151:   PetscBool      exists;

154:   *flg = PETSC_FALSE;
155:   if (!fname) return(0);

157:   PetscGetFileStat(fname, &fuid, &fgid, &fmode,&exists);
158:   if (!exists) return(0);
159:   /* Except for systems that have this broken stat macros (rare), this is the correct way to check for a regular file */
160:   if (!S_ISREG(fmode)) return(0);

162:   PetscTestOwnership(fname, mode, fuid, fgid, fmode, flg);
163:   return(0);
164: }

168: PetscErrorCode  PetscTestDirectory(const char fname[],char mode,PetscBool  *flg)
169: {
170:   uid_t          fuid;
171:   gid_t          fgid;
172:   int            fmode;
174:   PetscBool      exists;

177:   *flg = PETSC_FALSE;
178:   if (!fname) return(0);

180:   PetscGetFileStat(fname, &fuid, &fgid, &fmode,&exists);
181:   if (!exists) return(0);
182:   /* Except for systems that have this broken stat macros (rare), this
183:      is the correct way to check for a directory */
184:   if (!S_ISDIR(fmode)) return(0);

186:   PetscTestOwnership(fname, mode, fuid, fgid, fmode, flg);
187:   return(0);
188: }

192: PetscErrorCode  PetscLs(MPI_Comm comm,const char libname[],char found[],size_t tlen,PetscBool  *flg)
193: {
195:   size_t         len;
196:   char           *f,program[PETSC_MAX_PATH_LEN];
197:   FILE           *fp;

200:   PetscStrcpy(program,"ls ");
201:   PetscStrcat(program,libname);
202: #if defined(PETSC_HAVE_POPEN)
203:   PetscPOpen(comm,NULL,program,"r",&fp);
204: #else
205:   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP_SYS,"Cannot run external programs on this machine");
206: #endif
207:   f = fgets(found,tlen,fp);
208:   if (f) *flg = PETSC_TRUE;
209:   else *flg = PETSC_FALSE;
210:   while (f) {
211:     PetscStrlen(found,&len);
212:     f    = fgets(found+len,tlen-len,fp);
213:   }
214:   if (*flg) {PetscInfo2(0,"ls on %s gives \n%s\n",libname,found);}
215: #if defined(PETSC_HAVE_POPEN)
216:   PetscPClose(comm,fp,NULL);
217: #else
218:   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP_SYS,"Cannot run external programs on this machine");
219: #endif
220:   return(0);
221: }