1e5c89e4eSSatish Balay 2c6db04a5SJed Brown #include <petscsys.h> 37fe8d12eSJed Brown #include <errno.h> 4e5c89e4eSSatish Balay #if defined(PETSC_HAVE_PWD_H) 5e5c89e4eSSatish Balay #include <pwd.h> 6e5c89e4eSSatish Balay #endif 7e5c89e4eSSatish Balay #include <ctype.h> 8e5c89e4eSSatish Balay #include <sys/stat.h> 9e5c89e4eSSatish Balay #if defined(PETSC_HAVE_UNISTD_H) 10e5c89e4eSSatish Balay #include <unistd.h> 11e5c89e4eSSatish Balay #endif 12e5c89e4eSSatish Balay #if defined(PETSC_HAVE_SYS_UTSNAME_H) 13e5c89e4eSSatish Balay #include <sys/utsname.h> 14e5c89e4eSSatish Balay #endif 15e5c89e4eSSatish Balay #if defined(PETSC_HAVE_IO_H) 16e5c89e4eSSatish Balay #include <io.h> 17e5c89e4eSSatish Balay #endif 18e5c89e4eSSatish Balay #if defined(PETSC_HAVE_SYS_SYSTEMINFO_H) 19e5c89e4eSSatish Balay #include <sys/systeminfo.h> 20e5c89e4eSSatish Balay #endif 21e5c89e4eSSatish Balay 22e5c89e4eSSatish Balay #if defined(PETSC_HAVE__ACCESS) || defined(PETSC_HAVE_ACCESS) 23e5c89e4eSSatish Balay 249371c9d4SSatish Balay static PetscErrorCode PetscTestOwnership(const char fname[], char mode, uid_t fuid, gid_t fgid, int fmode, PetscBool *flg) { 25e5c89e4eSSatish Balay int m = R_OK; 26e5c89e4eSSatish Balay 27e5c89e4eSSatish Balay PetscFunctionBegin; 28e5c89e4eSSatish Balay if (mode == 'r') m = R_OK; 29e5c89e4eSSatish Balay else if (mode == 'w') m = W_OK; 30e5c89e4eSSatish Balay else if (mode == 'x') m = X_OK; 31e32f2f54SBarry Smith else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Mode must be one of r, w, or x"); 32e5c89e4eSSatish Balay #if defined(PETSC_HAVE_ACCESS) 337fe8d12eSJed Brown if (!access(fname, m)) { 349566063dSJacob Faibussowitsch PetscCall(PetscInfo(NULL, "System call access() succeeded on file %s\n", fname)); 357fe8d12eSJed Brown *flg = PETSC_TRUE; 367fe8d12eSJed Brown } else { 379566063dSJacob Faibussowitsch PetscCall(PetscInfo(NULL, "System call access() failed on file %s\n", fname)); 387fe8d12eSJed Brown *flg = PETSC_FALSE; 397fe8d12eSJed Brown } 40e5c89e4eSSatish Balay #else 4108401ef6SPierre Jolivet PetscCheck(m != X_OK, PETSC_COMM_SELF, PETSC_ERR_SUP, "Unable to check execute permission for file %s", fname); 42e5c89e4eSSatish Balay if (!_access(fname, m)) *flg = PETSC_TRUE; 43e5c89e4eSSatish Balay #endif 44e5c89e4eSSatish Balay PetscFunctionReturn(0); 45e5c89e4eSSatish Balay } 46e5c89e4eSSatish Balay 47e5c89e4eSSatish Balay #else /* PETSC_HAVE_ACCESS or PETSC_HAVE__ACCESS */ 48e5c89e4eSSatish Balay 499371c9d4SSatish Balay static PetscErrorCode PetscTestOwnership(const char fname[], char mode, uid_t fuid, gid_t fgid, int fmode, PetscBool *flg) { 50e5c89e4eSSatish Balay uid_t uid; 510298fd71SBarry Smith gid_t *gid = NULL; 52e5c89e4eSSatish Balay int numGroups; 53e5c89e4eSSatish Balay int rbit = S_IROTH; 54e5c89e4eSSatish Balay int wbit = S_IWOTH; 55e5c89e4eSSatish Balay int ebit = S_IXOTH; 562da392ccSBarry Smith #if !defined(PETSC_MISSING_GETGROUPS) 572da392ccSBarry Smith int err; 582da392ccSBarry Smith #endif 59e5c89e4eSSatish Balay 60e5c89e4eSSatish Balay PetscFunctionBegin; 61e5c89e4eSSatish Balay /* Get the number of supplementary group IDs */ 62e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_GETGROUPS) 639371c9d4SSatish Balay numGroups = getgroups(0, gid); 649371c9d4SSatish Balay PetscCheck(numGroups >= 0, PETSC_COMM_SELF, PETSC_ERR_SYS, "Unable to count supplementary group IDs"); 659566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numGroups + 1, &gid)); 66e5c89e4eSSatish Balay #else 67e5c89e4eSSatish Balay numGroups = 0; 68e5c89e4eSSatish Balay #endif 69e5c89e4eSSatish Balay 70e5c89e4eSSatish Balay /* Get the (effective) user and group of the caller */ 71e5c89e4eSSatish Balay uid = geteuid(); 72e5c89e4eSSatish Balay gid[0] = getegid(); 73e5c89e4eSSatish Balay 74e5c89e4eSSatish Balay /* Get supplementary group IDs */ 75e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_GETGROUPS) 769371c9d4SSatish Balay err = getgroups(numGroups, gid + 1); 779371c9d4SSatish Balay PetscCheck(err >= 0, PETSC_COMM_SELF, PETSC_ERR_SYS, "Unable to obtain supplementary group IDs"); 78e5c89e4eSSatish Balay #endif 79e5c89e4eSSatish Balay 80e5c89e4eSSatish Balay /* Test for accessibility */ 81e5c89e4eSSatish Balay if (fuid == uid) { 82e5c89e4eSSatish Balay rbit = S_IRUSR; 83e5c89e4eSSatish Balay wbit = S_IWUSR; 84e5c89e4eSSatish Balay ebit = S_IXUSR; 85e5c89e4eSSatish Balay } else { 86e5c89e4eSSatish Balay int g; 87e5c89e4eSSatish Balay 88e5c89e4eSSatish Balay for (g = 0; g <= numGroups; g++) { 89e5c89e4eSSatish Balay if (fgid == gid[g]) { 90e5c89e4eSSatish Balay rbit = S_IRGRP; 91e5c89e4eSSatish Balay wbit = S_IWGRP; 92e5c89e4eSSatish Balay ebit = S_IXGRP; 93e5c89e4eSSatish Balay break; 94e5c89e4eSSatish Balay } 95e5c89e4eSSatish Balay } 96e5c89e4eSSatish Balay } 979566063dSJacob Faibussowitsch PetscCall(PetscFree(gid)); 98e5c89e4eSSatish Balay 99e5c89e4eSSatish Balay if (mode == 'r') { 100e5c89e4eSSatish Balay if (fmode & rbit) *flg = PETSC_TRUE; 101e5c89e4eSSatish Balay } else if (mode == 'w') { 102e5c89e4eSSatish Balay if (fmode & wbit) *flg = PETSC_TRUE; 103e5c89e4eSSatish Balay } else if (mode == 'x') { 104e5c89e4eSSatish Balay if (fmode & ebit) *flg = PETSC_TRUE; 105e5c89e4eSSatish Balay } 106e5c89e4eSSatish Balay PetscFunctionReturn(0); 107e5c89e4eSSatish Balay } 108e5c89e4eSSatish Balay 109e5c89e4eSSatish Balay #endif /* PETSC_HAVE_ACCESS */ 110e5c89e4eSSatish Balay 1119371c9d4SSatish Balay static PetscErrorCode PetscGetFileStat(const char fname[], uid_t *fileUid, gid_t *fileGid, int *fileMode, PetscBool *exists) { 112e5c89e4eSSatish Balay struct stat statbuf; 113e5c89e4eSSatish Balay PetscErrorCode ierr; 114e5c89e4eSSatish Balay 115e5c89e4eSSatish Balay PetscFunctionBegin; 11638ea73c8SJed Brown *fileMode = 0; 11738ea73c8SJed Brown *exists = PETSC_FALSE; 118e5c89e4eSSatish Balay #if defined(PETSC_HAVE_STAT_NO_CONST) 119e5c89e4eSSatish Balay ierr = stat((char *)fname, &statbuf); 120e5c89e4eSSatish Balay #else 121e5c89e4eSSatish Balay ierr = stat(fname, &statbuf); 122e5c89e4eSSatish Balay #endif 123e5c89e4eSSatish Balay if (ierr) { 1247ad82f04SSatish Balay #if defined(EOVERFLOW) 12508401ef6SPierre Jolivet 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"); 1267ad82f04SSatish Balay #endif 1279566063dSJacob Faibussowitsch PetscCall(PetscInfo(NULL, "System call stat() failed on file %s\n", fname)); 128e5c89e4eSSatish Balay *exists = PETSC_FALSE; 129e5c89e4eSSatish Balay } else { 1309566063dSJacob Faibussowitsch PetscCall(PetscInfo(NULL, "System call stat() succeeded on file %s\n", fname)); 131e5c89e4eSSatish Balay *exists = PETSC_TRUE; 132e5c89e4eSSatish Balay *fileUid = statbuf.st_uid; 133e5c89e4eSSatish Balay *fileGid = statbuf.st_gid; 134e5c89e4eSSatish Balay *fileMode = statbuf.st_mode; 135e5c89e4eSSatish Balay } 136e5c89e4eSSatish Balay PetscFunctionReturn(0); 137e5c89e4eSSatish Balay } 138e5c89e4eSSatish Balay 139e1d001d6SBarry Smith /*@C 14026b19047SBarry Smith PetscTestFile - checks for the existence of a file 14126b19047SBarry Smith 14226b19047SBarry Smith Not Collective 14326b19047SBarry Smith 144d8d19677SJose E. Roman Input Parameters: 14526b19047SBarry Smith + fname - the filename 14655819941SStefano Zampini - mode - either 'r', 'w', 'x' or '\0' 14726b19047SBarry Smith 14826b19047SBarry Smith Output Parameter: 14926b19047SBarry Smith . flg - the file exists and satisfies the mode 15026b19047SBarry Smith 1511b266c99SBarry Smith Level: intermediate 1521b266c99SBarry Smith 153*811af0c4SBarry Smith Note: 154*811af0c4SBarry Smith If mode is '\0', no permissions checks are performed 15555819941SStefano Zampini 156db781477SPatrick Sanan .seealso: `PetscTestDirectory()`, `PetscLs()` 15726b19047SBarry Smith @*/ 1589371c9d4SSatish Balay PetscErrorCode PetscTestFile(const char fname[], char mode, PetscBool *flg) { 159e5c89e4eSSatish Balay uid_t fuid; 160e5c89e4eSSatish Balay gid_t fgid; 161e5c89e4eSSatish Balay int fmode; 162ace3abfcSBarry Smith PetscBool exists; 163e5c89e4eSSatish Balay 164e5c89e4eSSatish Balay PetscFunctionBegin; 165e5c89e4eSSatish Balay *flg = PETSC_FALSE; 166e5c89e4eSSatish Balay if (!fname) PetscFunctionReturn(0); 167e5c89e4eSSatish Balay 1689566063dSJacob Faibussowitsch PetscCall(PetscGetFileStat(fname, &fuid, &fgid, &fmode, &exists)); 169e5c89e4eSSatish Balay if (!exists) PetscFunctionReturn(0); 1707fe8d12eSJed Brown /* Except for systems that have this broken stat macros (rare), this is the correct way to check for a regular file */ 171e5c89e4eSSatish Balay if (!S_ISREG(fmode)) PetscFunctionReturn(0); 17255819941SStefano Zampini /* return if asked to check for existence only */ 1739371c9d4SSatish Balay if (mode == '\0') { 1749371c9d4SSatish Balay *flg = exists; 1759371c9d4SSatish Balay PetscFunctionReturn(0); 1769371c9d4SSatish Balay } 1779566063dSJacob Faibussowitsch PetscCall(PetscTestOwnership(fname, mode, fuid, fgid, fmode, flg)); 178e5c89e4eSSatish Balay PetscFunctionReturn(0); 179e5c89e4eSSatish Balay } 180e5c89e4eSSatish Balay 181e1d001d6SBarry Smith /*@C 18226b19047SBarry Smith PetscTestDirectory - checks for the existence of a directory 18326b19047SBarry Smith 18426b19047SBarry Smith Not Collective 18526b19047SBarry Smith 186d8d19677SJose E. Roman Input Parameters: 18726b19047SBarry Smith + dirname - the directory name 18826b19047SBarry Smith - mode - either 'r', 'w', or 'x' 18926b19047SBarry Smith 19026b19047SBarry Smith Output Parameter: 19126b19047SBarry Smith . flg - the directory exists and satisfies the mode 19226b19047SBarry Smith 1931b266c99SBarry Smith Level: intermediate 1941b266c99SBarry Smith 195db781477SPatrick Sanan .seealso: `PetscTestFile()`, `PetscLs()` 19626b19047SBarry Smith @*/ 1979371c9d4SSatish Balay PetscErrorCode PetscTestDirectory(const char dirname[], char mode, PetscBool *flg) { 198e5c89e4eSSatish Balay uid_t fuid; 199e5c89e4eSSatish Balay gid_t fgid; 200e5c89e4eSSatish Balay int fmode; 201ace3abfcSBarry Smith PetscBool exists; 202e5c89e4eSSatish Balay 203e5c89e4eSSatish Balay PetscFunctionBegin; 204e5c89e4eSSatish Balay *flg = PETSC_FALSE; 20526b19047SBarry Smith if (!dirname) PetscFunctionReturn(0); 206e5c89e4eSSatish Balay 2079566063dSJacob Faibussowitsch PetscCall(PetscGetFileStat(dirname, &fuid, &fgid, &fmode, &exists)); 208e5c89e4eSSatish Balay if (!exists) PetscFunctionReturn(0); 209e5c89e4eSSatish Balay /* Except for systems that have this broken stat macros (rare), this 210e5c89e4eSSatish Balay is the correct way to check for a directory */ 211e5c89e4eSSatish Balay if (!S_ISDIR(fmode)) PetscFunctionReturn(0); 212e5c89e4eSSatish Balay 2139566063dSJacob Faibussowitsch PetscCall(PetscTestOwnership(dirname, mode, fuid, fgid, fmode, flg)); 214e5c89e4eSSatish Balay PetscFunctionReturn(0); 215e5c89e4eSSatish Balay } 216e5c89e4eSSatish Balay 217e1d001d6SBarry Smith /*@C 21826b19047SBarry Smith PetscLs - produce a listing of the files in a directory 21926b19047SBarry Smith 220d083f849SBarry Smith Collective 22126b19047SBarry Smith 222d8d19677SJose E. Roman Input Parameters: 22326b19047SBarry Smith + comm - the MPI communicator 22426b19047SBarry Smith . dirname - the directory name 22526b19047SBarry Smith - tlen - the length of the buffer found[] 22626b19047SBarry Smith 227d8d19677SJose E. Roman Output Parameters: 22826b19047SBarry Smith + found - listing of files 22926b19047SBarry Smith - flg - the directory exists 23026b19047SBarry Smith 2311b266c99SBarry Smith Level: intermediate 2321b266c99SBarry Smith 233db781477SPatrick Sanan .seealso: `PetscTestFile()`, `PetscLs()` 23426b19047SBarry Smith @*/ 2359371c9d4SSatish Balay PetscErrorCode PetscLs(MPI_Comm comm, const char dirname[], char found[], size_t tlen, PetscBool *flg) { 236e5c89e4eSSatish Balay size_t len; 237e5c89e4eSSatish Balay char *f, program[PETSC_MAX_PATH_LEN]; 238e5c89e4eSSatish Balay FILE *fp; 239e5c89e4eSSatish Balay 240e5c89e4eSSatish Balay PetscFunctionBegin; 2419566063dSJacob Faibussowitsch PetscCall(PetscStrcpy(program, "ls ")); 2429566063dSJacob Faibussowitsch PetscCall(PetscStrcat(program, dirname)); 243e5c89e4eSSatish Balay #if defined(PETSC_HAVE_POPEN) 2449566063dSJacob Faibussowitsch PetscCall(PetscPOpen(comm, NULL, program, "r", &fp)); 245e5c89e4eSSatish Balay #else 246e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP_SYS, "Cannot run external programs on this machine"); 247e5c89e4eSSatish Balay #endif 248e5c89e4eSSatish Balay f = fgets(found, tlen, fp); 249a297a907SKarl Rupp if (f) *flg = PETSC_TRUE; 250a297a907SKarl Rupp else *flg = PETSC_FALSE; 251e5c89e4eSSatish Balay while (f) { 2529566063dSJacob Faibussowitsch PetscCall(PetscStrlen(found, &len)); 253e5c89e4eSSatish Balay f = fgets(found + len, tlen - len, fp); 254e5c89e4eSSatish Balay } 2559566063dSJacob Faibussowitsch if (*flg) PetscCall(PetscInfo(NULL, "ls on %s gives \n%s\n", dirname, found)); 256e5c89e4eSSatish Balay #if defined(PETSC_HAVE_POPEN) 2579566063dSJacob Faibussowitsch PetscCall(PetscPClose(comm, fp)); 258e5c89e4eSSatish Balay #else 259e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP_SYS, "Cannot run external programs on this machine"); 260e5c89e4eSSatish Balay #endif 261e5c89e4eSSatish Balay PetscFunctionReturn(0); 262e5c89e4eSSatish Balay } 263