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 static PetscErrorCode PetscTestOwnership(const char fname[], char mode, uid_t fuid, gid_t fgid, int fmode, PetscBool *flg) 25 { 26 int m = R_OK; 27 28 PetscFunctionBegin; 29 if (mode == 'r') m = R_OK; 30 else if (mode == 'w') m = W_OK; 31 else if (mode == 'x') m = X_OK; 32 else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG, "Mode must be one of r, w, or x"); 33 #if defined(PETSC_HAVE_ACCESS) 34 if (!access(fname, m)) { 35 PetscCall(PetscInfo(NULL,"System call access() succeeded on file %s\n",fname)); 36 *flg = PETSC_TRUE; 37 } else { 38 PetscCall(PetscInfo(NULL,"System call access() failed on file %s\n",fname)); 39 *flg = PETSC_FALSE; 40 } 41 #else 42 PetscCheck(m != X_OK,PETSC_COMM_SELF,PETSC_ERR_SUP, "Unable to check execute permission for file %s", fname); 43 if (!_access(fname, m)) *flg = PETSC_TRUE; 44 #endif 45 PetscFunctionReturn(0); 46 } 47 48 #else /* PETSC_HAVE_ACCESS or PETSC_HAVE__ACCESS */ 49 50 static PetscErrorCode PetscTestOwnership(const char fname[], char mode, uid_t fuid, gid_t fgid, int fmode, PetscBool *flg) 51 { 52 uid_t uid; 53 gid_t *gid = NULL; 54 int numGroups; 55 int rbit = S_IROTH; 56 int wbit = S_IWOTH; 57 int ebit = S_IXOTH; 58 #if !defined(PETSC_MISSING_GETGROUPS) 59 int err; 60 #endif 61 62 PetscFunctionBegin; 63 /* Get the number of supplementary group IDs */ 64 #if !defined(PETSC_MISSING_GETGROUPS) 65 numGroups = getgroups(0, gid); PetscCheck(numGroups >= 0,PETSC_COMM_SELF,PETSC_ERR_SYS, "Unable to count supplementary group IDs"); 66 PetscCall(PetscMalloc1(numGroups+1, &gid)); 67 #else 68 numGroups = 0; 69 #endif 70 71 /* Get the (effective) user and group of the caller */ 72 uid = geteuid(); 73 gid[0] = getegid(); 74 75 /* Get supplementary group IDs */ 76 #if !defined(PETSC_MISSING_GETGROUPS) 77 err = getgroups(numGroups, gid+1); PetscCheck(err >= 0,PETSC_COMM_SELF,PETSC_ERR_SYS, "Unable to obtain supplementary group IDs"); 78 #endif 79 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; 87 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 PetscCall(PetscFree(gid)); 98 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 PetscFunctionReturn(0); 107 } 108 109 #endif /* PETSC_HAVE_ACCESS */ 110 111 static PetscErrorCode PetscGetFileStat(const char fname[], uid_t *fileUid, gid_t *fileGid, int *fileMode,PetscBool *exists) 112 { 113 struct stat statbuf; 114 PetscErrorCode ierr; 115 116 PetscFunctionBegin; 117 *fileMode = 0; 118 *exists = PETSC_FALSE; 119 #if defined(PETSC_HAVE_STAT_NO_CONST) 120 ierr = stat((char*) fname, &statbuf); 121 #else 122 ierr = stat(fname, &statbuf); 123 #endif 124 if (ierr) { 125 #if defined(EOVERFLOW) 126 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"); 127 #endif 128 PetscCall(PetscInfo(NULL,"System call stat() failed on file %s\n",fname)); 129 *exists = PETSC_FALSE; 130 } else { 131 PetscCall(PetscInfo(NULL,"System call stat() succeeded on file %s\n",fname)); 132 *exists = PETSC_TRUE; 133 *fileUid = statbuf.st_uid; 134 *fileGid = statbuf.st_gid; 135 *fileMode = statbuf.st_mode; 136 } 137 PetscFunctionReturn(0); 138 } 139 140 /*@C 141 PetscTestFile - checks for the existence of a file 142 143 Not Collective 144 145 Input Parameters: 146 + fname - the filename 147 - mode - either 'r', 'w', 'x' or '\0' 148 149 Output Parameter: 150 . flg - the file exists and satisfies the mode 151 152 Level: intermediate 153 154 Notes: if mode is '\0', no permissions checks are performed 155 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; 164 165 PetscFunctionBegin; 166 *flg = PETSC_FALSE; 167 if (!fname) PetscFunctionReturn(0); 168 169 PetscCall(PetscGetFileStat(fname, &fuid, &fgid, &fmode, &exists)); 170 if (!exists) PetscFunctionReturn(0); 171 /* Except for systems that have this broken stat macros (rare), this is the correct way to check for a regular file */ 172 if (!S_ISREG(fmode)) PetscFunctionReturn(0); 173 /* return if asked to check for existence only */ 174 if (mode == '\0') { *flg = exists; PetscFunctionReturn(0); } 175 PetscCall(PetscTestOwnership(fname, mode, fuid, fgid, fmode, flg)); 176 PetscFunctionReturn(0); 177 } 178 179 /*@C 180 PetscTestDirectory - checks for the existence of a directory 181 182 Not Collective 183 184 Input Parameters: 185 + dirname - the directory name 186 - mode - either 'r', 'w', or 'x' 187 188 Output Parameter: 189 . flg - the directory exists and satisfies the mode 190 191 Level: intermediate 192 193 .seealso: PetscTestFile(), PetscLs() 194 @*/ 195 PetscErrorCode PetscTestDirectory(const char dirname[],char mode,PetscBool *flg) 196 { 197 uid_t fuid; 198 gid_t fgid; 199 int fmode; 200 PetscBool exists; 201 202 PetscFunctionBegin; 203 *flg = PETSC_FALSE; 204 if (!dirname) PetscFunctionReturn(0); 205 206 PetscCall(PetscGetFileStat(dirname, &fuid, &fgid, &fmode,&exists)); 207 if (!exists) PetscFunctionReturn(0); 208 /* Except for systems that have this broken stat macros (rare), this 209 is the correct way to check for a directory */ 210 if (!S_ISDIR(fmode)) PetscFunctionReturn(0); 211 212 PetscCall(PetscTestOwnership(dirname, mode, fuid, fgid, fmode, flg)); 213 PetscFunctionReturn(0); 214 } 215 216 /*@C 217 PetscLs - produce a listing of the files in a directory 218 219 Collective 220 221 Input Parameters: 222 + comm - the MPI communicator 223 . dirname - the directory name 224 - tlen - the length of the buffer found[] 225 226 Output Parameters: 227 + found - listing of files 228 - flg - the directory exists 229 230 Level: intermediate 231 232 .seealso: PetscTestFile(), PetscLs() 233 @*/ 234 PetscErrorCode PetscLs(MPI_Comm comm,const char dirname[],char found[],size_t tlen,PetscBool *flg) 235 { 236 size_t len; 237 char *f,program[PETSC_MAX_PATH_LEN]; 238 FILE *fp; 239 240 PetscFunctionBegin; 241 PetscCall(PetscStrcpy(program,"ls ")); 242 PetscCall(PetscStrcat(program,dirname)); 243 #if defined(PETSC_HAVE_POPEN) 244 PetscCall(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 PetscCall(PetscStrlen(found,&len)); 253 f = fgets(found+len,tlen-len,fp); 254 } 255 if (*flg) PetscCall(PetscInfo(NULL,"ls on %s gives \n%s\n",dirname,found)); 256 #if defined(PETSC_HAVE_POPEN) 257 PetscCall(PetscPClose(comm,fp)); 258 #else 259 SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP_SYS,"Cannot run external programs on this machine"); 260 #endif 261 PetscFunctionReturn(0); 262 } 263