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 24ace3abfcSBarry Smith static PetscErrorCode PetscTestOwnership(const char fname[], char mode, uid_t fuid, gid_t fgid, int fmode, PetscBool *flg) 25e5c89e4eSSatish Balay { 26e5c89e4eSSatish Balay int m = R_OK; 27e5c89e4eSSatish Balay 28e5c89e4eSSatish Balay PetscFunctionBegin; 29e5c89e4eSSatish Balay if (mode == 'r') m = R_OK; 30e5c89e4eSSatish Balay else if (mode == 'w') m = W_OK; 31e5c89e4eSSatish Balay else if (mode == 'x') m = X_OK; 32e32f2f54SBarry Smith else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG, "Mode must be one of r, w, or x"); 33e5c89e4eSSatish Balay #if defined(PETSC_HAVE_ACCESS) 347fe8d12eSJed Brown if (!access(fname, m)) { 359566063dSJacob Faibussowitsch PetscCall(PetscInfo(NULL,"System call access() succeeded on file %s\n",fname)); 367fe8d12eSJed Brown *flg = PETSC_TRUE; 377fe8d12eSJed Brown } else { 389566063dSJacob Faibussowitsch PetscCall(PetscInfo(NULL,"System call access() failed on file %s\n",fname)); 397fe8d12eSJed Brown *flg = PETSC_FALSE; 407fe8d12eSJed Brown } 41e5c89e4eSSatish Balay #else 42*08401ef6SPierre Jolivet PetscCheck(m != X_OK,PETSC_COMM_SELF,PETSC_ERR_SUP, "Unable to check execute permission for file %s", fname); 43e5c89e4eSSatish Balay if (!_access(fname, m)) *flg = PETSC_TRUE; 44e5c89e4eSSatish Balay #endif 45e5c89e4eSSatish Balay PetscFunctionReturn(0); 46e5c89e4eSSatish Balay } 47e5c89e4eSSatish Balay 48e5c89e4eSSatish Balay #else /* PETSC_HAVE_ACCESS or PETSC_HAVE__ACCESS */ 49e5c89e4eSSatish Balay 50ace3abfcSBarry Smith static PetscErrorCode PetscTestOwnership(const char fname[], char mode, uid_t fuid, gid_t fgid, int fmode, PetscBool *flg) 51e5c89e4eSSatish Balay { 52e5c89e4eSSatish Balay uid_t uid; 530298fd71SBarry Smith gid_t *gid = NULL; 54e5c89e4eSSatish Balay int numGroups; 55e5c89e4eSSatish Balay int rbit = S_IROTH; 56e5c89e4eSSatish Balay int wbit = S_IWOTH; 57e5c89e4eSSatish Balay int ebit = S_IXOTH; 582da392ccSBarry Smith #if !defined(PETSC_MISSING_GETGROUPS) 592da392ccSBarry Smith int err; 602da392ccSBarry Smith #endif 61e5c89e4eSSatish Balay 62e5c89e4eSSatish Balay PetscFunctionBegin; 63e5c89e4eSSatish Balay /* Get the number of supplementary group IDs */ 64e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_GETGROUPS) 65*08401ef6SPierre Jolivet numGroups = getgroups(0, gid); PetscCheck(numGroups >= 0,PETSC_COMM_SELF,PETSC_ERR_SYS, "Unable to count supplementary group IDs"); 669566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numGroups+1, &gid)); 67e5c89e4eSSatish Balay #else 68e5c89e4eSSatish Balay numGroups = 0; 69e5c89e4eSSatish Balay #endif 70e5c89e4eSSatish Balay 71e5c89e4eSSatish Balay /* Get the (effective) user and group of the caller */ 72e5c89e4eSSatish Balay uid = geteuid(); 73e5c89e4eSSatish Balay gid[0] = getegid(); 74e5c89e4eSSatish Balay 75e5c89e4eSSatish Balay /* Get supplementary group IDs */ 76e5c89e4eSSatish Balay #if !defined(PETSC_MISSING_GETGROUPS) 77*08401ef6SPierre Jolivet err = getgroups(numGroups, gid+1); 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 111ace3abfcSBarry Smith static PetscErrorCode PetscGetFileStat(const char fname[], uid_t *fileUid, gid_t *fileGid, int *fileMode,PetscBool *exists) 112e5c89e4eSSatish Balay { 113e5c89e4eSSatish Balay struct stat statbuf; 114e5c89e4eSSatish Balay PetscErrorCode ierr; 115e5c89e4eSSatish Balay 116e5c89e4eSSatish Balay PetscFunctionBegin; 11738ea73c8SJed Brown *fileMode = 0; 11838ea73c8SJed Brown *exists = PETSC_FALSE; 119e5c89e4eSSatish Balay #if defined(PETSC_HAVE_STAT_NO_CONST) 120e5c89e4eSSatish Balay ierr = stat((char*) fname, &statbuf); 121e5c89e4eSSatish Balay #else 122e5c89e4eSSatish Balay ierr = stat(fname, &statbuf); 123e5c89e4eSSatish Balay #endif 124e5c89e4eSSatish Balay if (ierr) { 1257ad82f04SSatish Balay #if defined(EOVERFLOW) 126*08401ef6SPierre 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"); 1277ad82f04SSatish Balay #endif 1289566063dSJacob Faibussowitsch PetscCall(PetscInfo(NULL,"System call stat() failed on file %s\n",fname)); 129e5c89e4eSSatish Balay *exists = PETSC_FALSE; 130e5c89e4eSSatish Balay } else { 1319566063dSJacob Faibussowitsch PetscCall(PetscInfo(NULL,"System call stat() succeeded on file %s\n",fname)); 132e5c89e4eSSatish Balay *exists = PETSC_TRUE; 133e5c89e4eSSatish Balay *fileUid = statbuf.st_uid; 134e5c89e4eSSatish Balay *fileGid = statbuf.st_gid; 135e5c89e4eSSatish Balay *fileMode = statbuf.st_mode; 136e5c89e4eSSatish Balay } 137e5c89e4eSSatish Balay PetscFunctionReturn(0); 138e5c89e4eSSatish Balay } 139e5c89e4eSSatish Balay 140e1d001d6SBarry Smith /*@C 14126b19047SBarry Smith PetscTestFile - checks for the existence of a file 14226b19047SBarry Smith 14326b19047SBarry Smith Not Collective 14426b19047SBarry Smith 145d8d19677SJose E. Roman Input Parameters: 14626b19047SBarry Smith + fname - the filename 14755819941SStefano Zampini - mode - either 'r', 'w', 'x' or '\0' 14826b19047SBarry Smith 14926b19047SBarry Smith Output Parameter: 15026b19047SBarry Smith . flg - the file exists and satisfies the mode 15126b19047SBarry Smith 1521b266c99SBarry Smith Level: intermediate 1531b266c99SBarry Smith 15455819941SStefano Zampini Notes: if mode is '\0', no permissions checks are performed 15555819941SStefano Zampini 15626b19047SBarry Smith .seealso: PetscTestDirectory(), PetscLs() 15726b19047SBarry Smith @*/ 1587087cfbeSBarry Smith PetscErrorCode PetscTestFile(const char fname[], char mode, PetscBool *flg) 159e5c89e4eSSatish Balay { 160e5c89e4eSSatish Balay uid_t fuid; 161e5c89e4eSSatish Balay gid_t fgid; 162e5c89e4eSSatish Balay int fmode; 163ace3abfcSBarry Smith PetscBool exists; 164e5c89e4eSSatish Balay 165e5c89e4eSSatish Balay PetscFunctionBegin; 166e5c89e4eSSatish Balay *flg = PETSC_FALSE; 167e5c89e4eSSatish Balay if (!fname) PetscFunctionReturn(0); 168e5c89e4eSSatish Balay 1699566063dSJacob Faibussowitsch PetscCall(PetscGetFileStat(fname, &fuid, &fgid, &fmode, &exists)); 170e5c89e4eSSatish Balay if (!exists) PetscFunctionReturn(0); 1717fe8d12eSJed Brown /* Except for systems that have this broken stat macros (rare), this is the correct way to check for a regular file */ 172e5c89e4eSSatish Balay if (!S_ISREG(fmode)) PetscFunctionReturn(0); 17355819941SStefano Zampini /* return if asked to check for existence only */ 17455819941SStefano Zampini if (mode == '\0') { *flg = exists; PetscFunctionReturn(0); } 1759566063dSJacob Faibussowitsch PetscCall(PetscTestOwnership(fname, mode, fuid, fgid, fmode, flg)); 176e5c89e4eSSatish Balay PetscFunctionReturn(0); 177e5c89e4eSSatish Balay } 178e5c89e4eSSatish Balay 179e1d001d6SBarry Smith /*@C 18026b19047SBarry Smith PetscTestDirectory - checks for the existence of a directory 18126b19047SBarry Smith 18226b19047SBarry Smith Not Collective 18326b19047SBarry Smith 184d8d19677SJose E. Roman Input Parameters: 18526b19047SBarry Smith + dirname - the directory name 18626b19047SBarry Smith - mode - either 'r', 'w', or 'x' 18726b19047SBarry Smith 18826b19047SBarry Smith Output Parameter: 18926b19047SBarry Smith . flg - the directory exists and satisfies the mode 19026b19047SBarry Smith 1911b266c99SBarry Smith Level: intermediate 1921b266c99SBarry Smith 19326b19047SBarry Smith .seealso: PetscTestFile(), PetscLs() 19426b19047SBarry Smith @*/ 19526b19047SBarry Smith PetscErrorCode PetscTestDirectory(const char dirname[],char mode,PetscBool *flg) 196e5c89e4eSSatish Balay { 197e5c89e4eSSatish Balay uid_t fuid; 198e5c89e4eSSatish Balay gid_t fgid; 199e5c89e4eSSatish Balay int fmode; 200ace3abfcSBarry Smith PetscBool exists; 201e5c89e4eSSatish Balay 202e5c89e4eSSatish Balay PetscFunctionBegin; 203e5c89e4eSSatish Balay *flg = PETSC_FALSE; 20426b19047SBarry Smith if (!dirname) PetscFunctionReturn(0); 205e5c89e4eSSatish Balay 2069566063dSJacob Faibussowitsch PetscCall(PetscGetFileStat(dirname, &fuid, &fgid, &fmode,&exists)); 207e5c89e4eSSatish Balay if (!exists) PetscFunctionReturn(0); 208e5c89e4eSSatish Balay /* Except for systems that have this broken stat macros (rare), this 209e5c89e4eSSatish Balay is the correct way to check for a directory */ 210e5c89e4eSSatish Balay if (!S_ISDIR(fmode)) PetscFunctionReturn(0); 211e5c89e4eSSatish Balay 2129566063dSJacob Faibussowitsch PetscCall(PetscTestOwnership(dirname, mode, fuid, fgid, fmode, flg)); 213e5c89e4eSSatish Balay PetscFunctionReturn(0); 214e5c89e4eSSatish Balay } 215e5c89e4eSSatish Balay 216e1d001d6SBarry Smith /*@C 21726b19047SBarry Smith PetscLs - produce a listing of the files in a directory 21826b19047SBarry Smith 219d083f849SBarry Smith Collective 22026b19047SBarry Smith 221d8d19677SJose E. Roman Input Parameters: 22226b19047SBarry Smith + comm - the MPI communicator 22326b19047SBarry Smith . dirname - the directory name 22426b19047SBarry Smith - tlen - the length of the buffer found[] 22526b19047SBarry Smith 226d8d19677SJose E. Roman Output Parameters: 22726b19047SBarry Smith + found - listing of files 22826b19047SBarry Smith - flg - the directory exists 22926b19047SBarry Smith 2301b266c99SBarry Smith Level: intermediate 2311b266c99SBarry Smith 23226b19047SBarry Smith .seealso: PetscTestFile(), PetscLs() 23326b19047SBarry Smith @*/ 23426b19047SBarry Smith PetscErrorCode PetscLs(MPI_Comm comm,const char dirname[],char found[],size_t tlen,PetscBool *flg) 235e5c89e4eSSatish Balay { 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