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 PetscCheckFalse(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 PetscErrorCode ierr; 59 #if !defined(PETSC_MISSING_GETGROUPS) 60 int err; 61 #endif 62 63 PetscFunctionBegin; 64 /* Get the number of supplementary group IDs */ 65 #if !defined(PETSC_MISSING_GETGROUPS) 66 numGroups = getgroups(0, gid); PetscCheckFalse(numGroups < 0,PETSC_COMM_SELF,PETSC_ERR_SYS, "Unable to count supplementary group IDs"); 67 PetscCall(PetscMalloc1(numGroups+1, &gid)); 68 #else 69 numGroups = 0; 70 #endif 71 72 /* Get the (effective) user and group of the caller */ 73 uid = geteuid(); 74 gid[0] = getegid(); 75 76 /* Get supplementary group IDs */ 77 #if !defined(PETSC_MISSING_GETGROUPS) 78 err = getgroups(numGroups, gid+1); PetscCheckFalse(err < 0,PETSC_COMM_SELF,PETSC_ERR_SYS, "Unable to obtain supplementary group IDs"); 79 #endif 80 81 /* Test for accessibility */ 82 if (fuid == uid) { 83 rbit = S_IRUSR; 84 wbit = S_IWUSR; 85 ebit = S_IXUSR; 86 } else { 87 int g; 88 89 for (g = 0; g <= numGroups; g++) { 90 if (fgid == gid[g]) { 91 rbit = S_IRGRP; 92 wbit = S_IWGRP; 93 ebit = S_IXGRP; 94 break; 95 } 96 } 97 } 98 PetscCall(PetscFree(gid)); 99 100 if (mode == 'r') { 101 if (fmode & rbit) *flg = PETSC_TRUE; 102 } else if (mode == 'w') { 103 if (fmode & wbit) *flg = PETSC_TRUE; 104 } else if (mode == 'x') { 105 if (fmode & ebit) *flg = PETSC_TRUE; 106 } 107 PetscFunctionReturn(0); 108 } 109 110 #endif /* PETSC_HAVE_ACCESS */ 111 112 static PetscErrorCode PetscGetFileStat(const char fname[], uid_t *fileUid, gid_t *fileGid, int *fileMode,PetscBool *exists) 113 { 114 struct stat statbuf; 115 PetscErrorCode ierr; 116 117 PetscFunctionBegin; 118 *fileMode = 0; 119 *exists = PETSC_FALSE; 120 #if defined(PETSC_HAVE_STAT_NO_CONST) 121 ierr = stat((char*) fname, &statbuf); 122 #else 123 ierr = stat(fname, &statbuf); 124 #endif 125 if (ierr) { 126 #if defined(EOVERFLOW) 127 PetscCheckFalse(errno == EOVERFLOW,PETSC_COMM_SELF,PETSC_ERR_SYS,"EOVERFLOW in stat(), configure PETSc --with-large-file-io=1 to support files larger than 2GiB"); 128 #endif 129 PetscCall(PetscInfo(NULL,"System call stat() failed on file %s\n",fname)); 130 *exists = PETSC_FALSE; 131 } else { 132 PetscCall(PetscInfo(NULL,"System call stat() succeeded on file %s\n",fname)); 133 *exists = PETSC_TRUE; 134 *fileUid = statbuf.st_uid; 135 *fileGid = statbuf.st_gid; 136 *fileMode = statbuf.st_mode; 137 } 138 PetscFunctionReturn(0); 139 } 140 141 /*@C 142 PetscTestFile - checks for the existence of a file 143 144 Not Collective 145 146 Input Parameters: 147 + fname - the filename 148 - mode - either 'r', 'w', 'x' or '\0' 149 150 Output Parameter: 151 . flg - the file exists and satisfies the mode 152 153 Level: intermediate 154 155 Notes: if mode is '\0', no permissions checks are performed 156 157 .seealso: PetscTestDirectory(), PetscLs() 158 @*/ 159 PetscErrorCode PetscTestFile(const char fname[], char mode, PetscBool *flg) 160 { 161 uid_t fuid; 162 gid_t fgid; 163 int fmode; 164 PetscBool exists; 165 166 PetscFunctionBegin; 167 *flg = PETSC_FALSE; 168 if (!fname) PetscFunctionReturn(0); 169 170 PetscCall(PetscGetFileStat(fname, &fuid, &fgid, &fmode, &exists)); 171 if (!exists) PetscFunctionReturn(0); 172 /* Except for systems that have this broken stat macros (rare), this is the correct way to check for a regular file */ 173 if (!S_ISREG(fmode)) PetscFunctionReturn(0); 174 /* return if asked to check for existence only */ 175 if (mode == '\0') { *flg = exists; PetscFunctionReturn(0); } 176 PetscCall(PetscTestOwnership(fname, mode, fuid, fgid, fmode, flg)); 177 PetscFunctionReturn(0); 178 } 179 180 /*@C 181 PetscTestDirectory - checks for the existence of a directory 182 183 Not Collective 184 185 Input Parameters: 186 + dirname - the directory name 187 - mode - either 'r', 'w', or 'x' 188 189 Output Parameter: 190 . flg - the directory exists and satisfies the mode 191 192 Level: intermediate 193 194 .seealso: PetscTestFile(), PetscLs() 195 @*/ 196 PetscErrorCode PetscTestDirectory(const char dirname[],char mode,PetscBool *flg) 197 { 198 uid_t fuid; 199 gid_t fgid; 200 int fmode; 201 PetscBool exists; 202 203 PetscFunctionBegin; 204 *flg = PETSC_FALSE; 205 if (!dirname) PetscFunctionReturn(0); 206 207 PetscCall(PetscGetFileStat(dirname, &fuid, &fgid, &fmode,&exists)); 208 if (!exists) PetscFunctionReturn(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)) PetscFunctionReturn(0); 212 213 PetscCall(PetscTestOwnership(dirname, mode, fuid, fgid, fmode, flg)); 214 PetscFunctionReturn(0); 215 } 216 217 /*@C 218 PetscLs - produce a listing of the files in a directory 219 220 Collective 221 222 Input Parameters: 223 + comm - the MPI communicator 224 . dirname - the directory name 225 - tlen - the length of the buffer found[] 226 227 Output Parameters: 228 + found - listing of files 229 - flg - the directory exists 230 231 Level: intermediate 232 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; 240 241 PetscFunctionBegin; 242 PetscCall(PetscStrcpy(program,"ls ")); 243 PetscCall(PetscStrcat(program,dirname)); 244 #if defined(PETSC_HAVE_POPEN) 245 PetscCall(PetscPOpen(comm,NULL,program,"r",&fp)); 246 #else 247 SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP_SYS,"Cannot run external programs on this machine"); 248 #endif 249 f = fgets(found,tlen,fp); 250 if (f) *flg = PETSC_TRUE; 251 else *flg = PETSC_FALSE; 252 while (f) { 253 PetscCall(PetscStrlen(found,&len)); 254 f = fgets(found+len,tlen-len,fp); 255 } 256 if (*flg) PetscCall(PetscInfo(NULL,"ls on %s gives \n%s\n",dirname,found)); 257 #if defined(PETSC_HAVE_POPEN) 258 PetscCall(PetscPClose(comm,fp)); 259 #else 260 SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP_SYS,"Cannot run external programs on this machine"); 261 #endif 262 PetscFunctionReturn(0); 263 } 264