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 #undef __FUNCT__ 25 #define __FUNCT__ "PetscTestOwnership" 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; 29 PetscErrorCode ierr; 30 31 PetscFunctionBegin; 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 ierr = PetscInfo1(NULL,"System call access() succeeded on file %s\n",fname);CHKERRQ(ierr); 39 *flg = PETSC_TRUE; 40 } else { 41 ierr = PetscInfo1(NULL,"System call access() failed on file %s\n",fname);CHKERRQ(ierr); 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 PetscFunctionReturn(0); 49 } 50 51 #else /* PETSC_HAVE_ACCESS or PETSC_HAVE__ACCESS */ 52 53 #undef __FUNCT__ 54 #define __FUNCT__ "PetscTestOwnership" 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; 63 PetscErrorCode ierr; 64 65 PetscFunctionBegin; 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 ierr = PetscMalloc1(numGroups+1, &gid);CHKERRQ(ierr); 70 #else 71 numGroups = 0; 72 #endif 73 74 /* Get the (effective) user and group of the caller */ 75 uid = geteuid(); 76 gid[0] = getegid(); 77 78 /* Get supplementary group IDs */ 79 #if !defined(PETSC_MISSING_GETGROUPS) 80 ierr = getgroups(numGroups, gid+1); if (ierr < 0) SETERRQ(PETSC_COMM_SELF,ierr, "Unable to obtain supplementary group IDs"); 81 #endif 82 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; 90 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 ierr = PetscFree(gid);CHKERRQ(ierr); 101 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 PetscFunctionReturn(0); 110 } 111 112 #endif /* PETSC_HAVE_ACCESS */ 113 114 #undef __FUNCT__ 115 #define __FUNCT__ "PetscGetFileStat" 116 static PetscErrorCode PetscGetFileStat(const char fname[], uid_t *fileUid, gid_t *fileGid, int *fileMode,PetscBool *exists) 117 { 118 struct stat statbuf; 119 PetscErrorCode ierr; 120 121 PetscFunctionBegin; 122 *fileMode = 0; 123 *exists = PETSC_FALSE; 124 #if defined(PETSC_HAVE_STAT_NO_CONST) 125 ierr = stat((char*) fname, &statbuf); 126 #else 127 ierr = stat(fname, &statbuf); 128 #endif 129 if (ierr) { 130 #if defined(EOVERFLOW) 131 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"); 132 #endif 133 ierr = PetscInfo1(NULL,"System call stat() failed on file %s\n",fname);CHKERRQ(ierr); 134 *exists = PETSC_FALSE; 135 } else { 136 ierr = PetscInfo1(NULL,"System call stat() succeeded on file %s\n",fname);CHKERRQ(ierr); 137 *exists = PETSC_TRUE; 138 *fileUid = statbuf.st_uid; 139 *fileGid = statbuf.st_gid; 140 *fileMode = statbuf.st_mode; 141 } 142 PetscFunctionReturn(0); 143 } 144 145 #undef __FUNCT__ 146 #define __FUNCT__ "PetscTestFile" 147 /*@C 148 PetscTestFile - checks for the existence of a file 149 150 Not Collective 151 152 Input Parameter: 153 + fname - the filename 154 - mode - either 'r', 'w', or 'x' 155 156 Output Parameter: 157 . flg - the file exists and satisfies the mode 158 159 Level: intermediate 160 161 .seealso: PetscTestDirectory(), PetscLs() 162 @*/ 163 PetscErrorCode PetscTestFile(const char fname[], char mode, PetscBool *flg) 164 { 165 uid_t fuid; 166 gid_t fgid; 167 int fmode; 168 PetscErrorCode ierr; 169 PetscBool exists; 170 171 PetscFunctionBegin; 172 *flg = PETSC_FALSE; 173 if (!fname) PetscFunctionReturn(0); 174 175 ierr = PetscGetFileStat(fname, &fuid, &fgid, &fmode,&exists);CHKERRQ(ierr); 176 if (!exists) PetscFunctionReturn(0); 177 /* Except for systems that have this broken stat macros (rare), this is the correct way to check for a regular file */ 178 if (!S_ISREG(fmode)) PetscFunctionReturn(0); 179 180 ierr = PetscTestOwnership(fname, mode, fuid, fgid, fmode, flg);CHKERRQ(ierr); 181 PetscFunctionReturn(0); 182 } 183 184 #undef __FUNCT__ 185 #define __FUNCT__ "PetscTestDirectory" 186 /*@C 187 PetscTestDirectory - checks for the existence of a directory 188 189 Not Collective 190 191 Input Parameter: 192 + dirname - the directory name 193 - mode - either 'r', 'w', or 'x' 194 195 Output Parameter: 196 . flg - the directory exists and satisfies the mode 197 198 Level: intermediate 199 200 .seealso: PetscTestFile(), PetscLs() 201 @*/ 202 PetscErrorCode PetscTestDirectory(const char dirname[],char mode,PetscBool *flg) 203 { 204 uid_t fuid; 205 gid_t fgid; 206 int fmode; 207 PetscErrorCode ierr; 208 PetscBool exists; 209 210 PetscFunctionBegin; 211 *flg = PETSC_FALSE; 212 if (!dirname) PetscFunctionReturn(0); 213 214 ierr = PetscGetFileStat(dirname, &fuid, &fgid, &fmode,&exists);CHKERRQ(ierr); 215 if (!exists) PetscFunctionReturn(0); 216 /* Except for systems that have this broken stat macros (rare), this 217 is the correct way to check for a directory */ 218 if (!S_ISDIR(fmode)) PetscFunctionReturn(0); 219 220 ierr = PetscTestOwnership(dirname, mode, fuid, fgid, fmode, flg);CHKERRQ(ierr); 221 PetscFunctionReturn(0); 222 } 223 224 #undef __FUNCT__ 225 #define __FUNCT__ "PetscLs" 226 /*@C 227 PetscLs - produce a listing of the files in a directory 228 229 Collective on MPI_Comm 230 231 Input Parameter: 232 + comm - the MPI communicator 233 . dirname - the directory name 234 - tlen - the length of the buffer found[] 235 236 Output Parameter: 237 + found - listing of files 238 - flg - the directory exists 239 240 Level: intermediate 241 242 .seealso: PetscTestFile(), PetscLs() 243 @*/ 244 PetscErrorCode PetscLs(MPI_Comm comm,const char dirname[],char found[],size_t tlen,PetscBool *flg) 245 { 246 PetscErrorCode ierr; 247 size_t len; 248 char *f,program[PETSC_MAX_PATH_LEN]; 249 FILE *fp; 250 251 PetscFunctionBegin; 252 ierr = PetscStrcpy(program,"ls ");CHKERRQ(ierr); 253 ierr = PetscStrcat(program,dirname);CHKERRQ(ierr); 254 #if defined(PETSC_HAVE_POPEN) 255 ierr = PetscPOpen(comm,NULL,program,"r",&fp);CHKERRQ(ierr); 256 #else 257 SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP_SYS,"Cannot run external programs on this machine"); 258 #endif 259 f = fgets(found,tlen,fp); 260 if (f) *flg = PETSC_TRUE; 261 else *flg = PETSC_FALSE; 262 while (f) { 263 ierr = PetscStrlen(found,&len);CHKERRQ(ierr); 264 f = fgets(found+len,tlen-len,fp); 265 } 266 if (*flg) {ierr = PetscInfo2(0,"ls on %s gives \n%s\n",dirname,found);CHKERRQ(ierr);} 267 #if defined(PETSC_HAVE_POPEN) 268 ierr = PetscPClose(comm,fp,NULL);CHKERRQ(ierr); 269 #else 270 SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP_SYS,"Cannot run external programs on this machine"); 271 #endif 272 PetscFunctionReturn(0); 273 } 274