xref: /petsc/src/sys/fileio/ftest.c (revision d736bfeb4d37a01fcbdf00fe73fb60d6f0ba2142)
1 #define PETSC_DLL
2 
3 #include "petscsys.h"
4 #if defined(PETSC_HAVE_PWD_H)
5 #include <pwd.h>
6 #endif
7 #include <ctype.h>
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #if defined(PETSC_HAVE_UNISTD_H)
11 #include <unistd.h>
12 #endif
13 #if defined(PETSC_HAVE_STDLIB_H)
14 #include <stdlib.h>
15 #endif
16 #if defined(PETSC_HAVE_SYS_UTSNAME_H)
17 #include <sys/utsname.h>
18 #endif
19 #if defined(PETSC_HAVE_IO_H)
20 #include <io.h>
21 #endif
22 #if defined(PETSC_HAVE_SYS_SYSTEMINFO_H)
23 #include <sys/systeminfo.h>
24 #endif
25 
26 #if defined (PETSC_HAVE__ACCESS) || defined(PETSC_HAVE_ACCESS)
27 
28 #undef __FUNCT__
29 #define __FUNCT__ "PetscTestOwnership"
30 static PetscErrorCode PetscTestOwnership(const char fname[], char mode, uid_t fuid, gid_t fgid, int fmode, PetscTruth *flg)
31 {
32   int m = R_OK;
33 
34   PetscFunctionBegin;
35   if (mode == 'r') m = R_OK;
36   else if (mode == 'w') m = W_OK;
37   else if (mode == 'x') m = X_OK;
38   else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG, "Mode must be one of r, w, or x");
39 #if defined(PETSC_HAVE_ACCESS)
40   if(!access(fname, m))  *flg = PETSC_TRUE;
41 #else
42   if (m == X_OK) SETERRQ1(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 #undef __FUNCT__
51 #define __FUNCT__ "PetscTestOwnership"
52 static PetscErrorCode PetscTestOwnership(const char fname[], char mode, uid_t fuid, gid_t fgid, int fmode, PetscTruth *flg)
53 {
54   uid_t          uid;
55   gid_t          *gid = PETSC_NULL;
56   int            numGroups;
57   int            rbit = S_IROTH;
58   int            wbit = S_IWOTH;
59   int            ebit = S_IXOTH;
60   PetscErrorCode ierr;
61 
62   PetscFunctionBegin;
63   /* Get the number of supplementary group IDs */
64 #if !defined(PETSC_MISSING_GETGROUPS)
65   numGroups = getgroups(0, gid); if (numGroups < 0) SETERRQ(PETSC_COMM_SELF,numGroups, "Unable to count supplementary group IDs");
66   ierr = PetscMalloc((numGroups+1) * sizeof(gid_t), &gid);CHKERRQ(ierr);
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   ierr = getgroups(numGroups, gid+1); if (ierr < 0) SETERRQ(PETSC_COMM_SELF,ierr, "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   ierr = PetscFree(gid);CHKERRQ(ierr);
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 #undef __FUNCT__
112 #define __FUNCT__ "PetscGetFileStat"
113 static PetscErrorCode PetscGetFileStat(const char fname[], uid_t *fileUid, gid_t *fileGid, int *fileMode,PetscTruth *exists)
114 {
115   struct stat    statbuf;
116   PetscErrorCode ierr;
117 
118   PetscFunctionBegin;
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     *exists = PETSC_FALSE;
126   } else {
127     *exists = PETSC_TRUE;
128     *fileUid  = statbuf.st_uid;
129     *fileGid  = statbuf.st_gid;
130     *fileMode = statbuf.st_mode;
131   }
132   PetscFunctionReturn(0);
133 }
134 
135 #undef __FUNCT__
136 #define __FUNCT__ "PetscTestFile"
137 PetscErrorCode PETSC_DLLEXPORT PetscTestFile(const char fname[], char mode, PetscTruth *flg)
138 {
139   uid_t          fuid;
140   gid_t          fgid;
141   int            fmode;
142   PetscErrorCode ierr;
143   PetscTruth     exists;
144 
145   PetscFunctionBegin;
146   *flg = PETSC_FALSE;
147   if (!fname) PetscFunctionReturn(0);
148 
149   ierr = PetscGetFileStat(fname, &fuid, &fgid, &fmode,&exists);CHKERRQ(ierr);
150   if (!exists) PetscFunctionReturn(0);
151   /* Except for systems that have this broken stat macros (rare), this
152      is the correct way to check for a regular file */
153   if (!S_ISREG(fmode)) PetscFunctionReturn(0);
154 
155   ierr = PetscTestOwnership(fname, mode, fuid, fgid, fmode, flg);CHKERRQ(ierr);
156   PetscFunctionReturn(0);
157 }
158 
159 #undef __FUNCT__
160 #define __FUNCT__ "PetscTestDirectory"
161 PetscErrorCode PETSC_DLLEXPORT PetscTestDirectory(const char fname[],char mode,PetscTruth *flg)
162 {
163   uid_t          fuid;
164   gid_t          fgid;
165   int            fmode;
166   PetscErrorCode ierr;
167   PetscTruth     exists;
168 
169   PetscFunctionBegin;
170   *flg = PETSC_FALSE;
171   if (!fname) PetscFunctionReturn(0);
172 
173   ierr = PetscGetFileStat(fname, &fuid, &fgid, &fmode,&exists);CHKERRQ(ierr);
174   if (!exists) PetscFunctionReturn(0);
175   /* Except for systems that have this broken stat macros (rare), this
176      is the correct way to check for a directory */
177   if (!S_ISDIR(fmode)) PetscFunctionReturn(0);
178 
179   ierr = PetscTestOwnership(fname, mode, fuid, fgid, fmode, flg);CHKERRQ(ierr);
180   PetscFunctionReturn(0);
181 }
182 
183 #undef __FUNCT__
184 #define __FUNCT__ "PetscLs"
185 PetscErrorCode PETSC_DLLEXPORT PetscLs(MPI_Comm comm,const char libname[],char found[],size_t tlen,PetscTruth *flg)
186 {
187   PetscErrorCode ierr;
188   size_t         len;
189   char           *f,program[PETSC_MAX_PATH_LEN];
190   FILE           *fp;
191 
192   PetscFunctionBegin;
193   ierr   = PetscStrcpy(program,"ls ");CHKERRQ(ierr);
194   ierr   = PetscStrcat(program,libname);CHKERRQ(ierr);
195 #if defined(PETSC_HAVE_POPEN)
196   ierr   = PetscPOpen(comm,PETSC_NULL,program,"r",&fp);CHKERRQ(ierr);
197 #else
198   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP_SYS,"Cannot run external programs on this machine");
199 #endif
200   f      = fgets(found,tlen,fp);
201   if (f) *flg = PETSC_TRUE; else *flg = PETSC_FALSE;
202   while (f) {
203     ierr  = PetscStrlen(found,&len);CHKERRQ(ierr);
204     f     = fgets(found+len,tlen-len,fp);
205   }
206   if (*flg) {ierr = PetscInfo2(0,"ls on %s gives \n%s\n",libname,found);CHKERRQ(ierr);}
207 #if defined(PETSC_HAVE_POPEN)
208   ierr   = PetscPClose(comm,fp);CHKERRQ(ierr);
209 #else
210   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP_SYS,"Cannot run external programs on this machine");
211 #endif
212   PetscFunctionReturn(0);
213 }
214