xref: /petsc/src/sys/fileio/ftest.c (revision c4762a1b19cd2af06abeed90e8f9d34fb975dd94)
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   PetscErrorCode ierr;
28 
29   PetscFunctionBegin;
30   if (mode == 'r') m = R_OK;
31   else if (mode == 'w') m = W_OK;
32   else if (mode == 'x') m = X_OK;
33   else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG, "Mode must be one of r, w, or x");
34 #if defined(PETSC_HAVE_ACCESS)
35   if (!access(fname, m)) {
36     ierr = PetscInfo1(NULL,"System call access() succeeded on file %s\n",fname);CHKERRQ(ierr);
37     *flg = PETSC_TRUE;
38   } else {
39     ierr = PetscInfo1(NULL,"System call access() failed on file %s\n",fname);CHKERRQ(ierr);
40     *flg = PETSC_FALSE;
41   }
42 #else
43   if (m == X_OK) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP, "Unable to check execute permission for file %s", fname);
44   if (!_access(fname, m)) *flg = PETSC_TRUE;
45 #endif
46   PetscFunctionReturn(0);
47 }
48 
49 #else  /* PETSC_HAVE_ACCESS or PETSC_HAVE__ACCESS */
50 
51 static PetscErrorCode PetscTestOwnership(const char fname[], char mode, uid_t fuid, gid_t fgid, int fmode, PetscBool  *flg)
52 {
53   uid_t          uid;
54   gid_t          *gid = NULL;
55   int            numGroups;
56   int            rbit = S_IROTH;
57   int            wbit = S_IWOTH;
58   int            ebit = S_IXOTH;
59   PetscErrorCode ierr;
60 
61   PetscFunctionBegin;
62   /* Get the number of supplementary group IDs */
63 #if !defined(PETSC_MISSING_GETGROUPS)
64   numGroups = getgroups(0, gid); if (numGroups < 0) SETERRQ(PETSC_COMM_SELF,numGroups, "Unable to count supplementary group IDs");
65   ierr = PetscMalloc1(numGroups+1, &gid);CHKERRQ(ierr);
66 #else
67   numGroups = 0;
68 #endif
69 
70   /* Get the (effective) user and group of the caller */
71   uid    = geteuid();
72   gid[0] = getegid();
73 
74   /* Get supplementary group IDs */
75 #if !defined(PETSC_MISSING_GETGROUPS)
76   ierr = getgroups(numGroups, gid+1); if (ierr < 0) SETERRQ(PETSC_COMM_SELF,ierr, "Unable to obtain supplementary group IDs");
77 #endif
78 
79   /* Test for accessibility */
80   if (fuid == uid) {
81     rbit = S_IRUSR;
82     wbit = S_IWUSR;
83     ebit = S_IXUSR;
84   } else {
85     int g;
86 
87     for (g = 0; g <= numGroups; g++) {
88       if (fgid == gid[g]) {
89         rbit = S_IRGRP;
90         wbit = S_IWGRP;
91         ebit = S_IXGRP;
92         break;
93       }
94     }
95   }
96   ierr = PetscFree(gid);CHKERRQ(ierr);
97 
98   if (mode == 'r') {
99     if (fmode & rbit) *flg = PETSC_TRUE;
100   } else if (mode == 'w') {
101     if (fmode & wbit) *flg = PETSC_TRUE;
102   } else if (mode == 'x') {
103     if (fmode & ebit) *flg = PETSC_TRUE;
104   }
105   PetscFunctionReturn(0);
106 }
107 
108 #endif /* PETSC_HAVE_ACCESS */
109 
110 static PetscErrorCode PetscGetFileStat(const char fname[], uid_t *fileUid, gid_t *fileGid, int *fileMode,PetscBool  *exists)
111 {
112   struct stat    statbuf;
113   PetscErrorCode ierr;
114 
115   PetscFunctionBegin;
116   *fileMode = 0;
117   *exists = PETSC_FALSE;
118 #if defined(PETSC_HAVE_STAT_NO_CONST)
119   ierr = stat((char*) fname, &statbuf);
120 #else
121   ierr = stat(fname, &statbuf);
122 #endif
123   if (ierr) {
124 #if defined(EOVERFLOW)
125     if (errno == EOVERFLOW) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"EOVERFLOW in stat(), configure PETSc --with-large-file-io=1 to support files larger than 2GiB");
126 #endif
127     ierr    = PetscInfo1(NULL,"System call stat() failed on file %s\n",fname);CHKERRQ(ierr);
128     *exists = PETSC_FALSE;
129   } else {
130     ierr      = PetscInfo1(NULL,"System call stat() succeeded on file %s\n",fname);CHKERRQ(ierr);
131     *exists   = PETSC_TRUE;
132     *fileUid  = statbuf.st_uid;
133     *fileGid  = statbuf.st_gid;
134     *fileMode = statbuf.st_mode;
135   }
136   PetscFunctionReturn(0);
137 }
138 
139 /*@C
140    PetscTestFile - checks for the existence of a file
141 
142    Not Collective
143 
144    Input Parameter:
145 +  fname - the filename
146 -  mode - either 'r', 'w', 'x' or '\0'
147 
148    Output Parameter:
149 .  flg - the file exists and satisfies the mode
150 
151    Level: intermediate
152 
153    Notes: if mode is '\0', no permissions checks are performed
154 
155 .seealso: PetscTestDirectory(), PetscLs()
156 @*/
157 PetscErrorCode  PetscTestFile(const char fname[], char mode, PetscBool  *flg)
158 {
159   uid_t          fuid;
160   gid_t          fgid;
161   int            fmode;
162   PetscErrorCode ierr;
163   PetscBool      exists;
164 
165   PetscFunctionBegin;
166   *flg = PETSC_FALSE;
167   if (!fname) PetscFunctionReturn(0);
168 
169   ierr = PetscGetFileStat(fname, &fuid, &fgid, &fmode, &exists);CHKERRQ(ierr);
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   ierr = PetscTestOwnership(fname, mode, fuid, fgid, fmode, flg);CHKERRQ(ierr);
176   PetscFunctionReturn(0);
177 }
178 
179 /*@C
180    PetscTestDirectory - checks for the existence of a directory
181 
182    Not Collective
183 
184    Input Parameter:
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   PetscErrorCode ierr;
201   PetscBool      exists;
202 
203   PetscFunctionBegin;
204   *flg = PETSC_FALSE;
205   if (!dirname) PetscFunctionReturn(0);
206 
207   ierr = PetscGetFileStat(dirname, &fuid, &fgid, &fmode,&exists);CHKERRQ(ierr);
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   ierr = PetscTestOwnership(dirname, mode, fuid, fgid, fmode, flg);CHKERRQ(ierr);
214   PetscFunctionReturn(0);
215 }
216 
217 /*@C
218    PetscLs - produce a listing of the files in a directory
219 
220    Collective
221 
222    Input Parameter:
223 +  comm - the MPI communicator
224 .  dirname - the directory name
225 -  tlen - the length of the buffer found[]
226 
227    Output Parameter:
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   PetscErrorCode ierr;
238   size_t         len;
239   char           *f,program[PETSC_MAX_PATH_LEN];
240   FILE           *fp;
241 
242   PetscFunctionBegin;
243   ierr = PetscStrcpy(program,"ls ");CHKERRQ(ierr);
244   ierr = PetscStrcat(program,dirname);CHKERRQ(ierr);
245 #if defined(PETSC_HAVE_POPEN)
246   ierr = PetscPOpen(comm,NULL,program,"r",&fp);CHKERRQ(ierr);
247 #else
248   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP_SYS,"Cannot run external programs on this machine");
249 #endif
250   f = fgets(found,tlen,fp);
251   if (f) *flg = PETSC_TRUE;
252   else *flg = PETSC_FALSE;
253   while (f) {
254     ierr = PetscStrlen(found,&len);CHKERRQ(ierr);
255     f    = fgets(found+len,tlen-len,fp);
256   }
257   if (*flg) {ierr = PetscInfo2(NULL,"ls on %s gives \n%s\n",dirname,found);CHKERRQ(ierr);}
258 #if defined(PETSC_HAVE_POPEN)
259   ierr = PetscPClose(comm,fp);CHKERRQ(ierr);
260 #else
261   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP_SYS,"Cannot run external programs on this machine");
262 #endif
263   PetscFunctionReturn(0);
264 }
265