xref: /petsc/src/sys/fileio/ftest.c (revision db31f6defe33e3dfd9985704e6b8f54a7ffb6553)
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/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, PetscBool  *flg)
31 {
32   int            m = R_OK;
33   PetscErrorCode ierr;
34 
35   PetscFunctionBegin;
36   if (mode == 'r') m = R_OK;
37   else if (mode == 'w') m = W_OK;
38   else if (mode == 'x') m = X_OK;
39   else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG, "Mode must be one of r, w, or x");
40 #if defined(PETSC_HAVE_ACCESS)
41   if (!access(fname, m)) {
42     ierr = PetscInfo1(PETSC_NULL,"System call access() succeeded on file %s\n",fname);CHKERRQ(ierr);
43     *flg = PETSC_TRUE;
44   } else {
45     ierr = PetscInfo1(PETSC_NULL,"System call access() failed on file %s\n",fname);CHKERRQ(ierr);
46     *flg = PETSC_FALSE;
47   }
48 #else
49   if (m == X_OK) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP, "Unable to check execute permission for file %s", fname);
50   if(!_access(fname, m)) *flg = PETSC_TRUE;
51 #endif
52   PetscFunctionReturn(0);
53 }
54 
55 #else  /* PETSC_HAVE_ACCESS or PETSC_HAVE__ACCESS */
56 
57 #undef __FUNCT__
58 #define __FUNCT__ "PetscTestOwnership"
59 static PetscErrorCode PetscTestOwnership(const char fname[], char mode, uid_t fuid, gid_t fgid, int fmode, PetscBool  *flg)
60 {
61   uid_t          uid;
62   gid_t          *gid = PETSC_NULL;
63   int            numGroups;
64   int            rbit = S_IROTH;
65   int            wbit = S_IWOTH;
66   int            ebit = S_IXOTH;
67   PetscErrorCode ierr;
68 
69   PetscFunctionBegin;
70   /* Get the number of supplementary group IDs */
71 #if !defined(PETSC_MISSING_GETGROUPS)
72   numGroups = getgroups(0, gid); if (numGroups < 0) SETERRQ(PETSC_COMM_SELF,numGroups, "Unable to count supplementary group IDs");
73   ierr = PetscMalloc((numGroups+1) * sizeof(gid_t), &gid);CHKERRQ(ierr);
74 #else
75   numGroups = 0;
76 #endif
77 
78   /* Get the (effective) user and group of the caller */
79   uid    = geteuid();
80   gid[0] = getegid();
81 
82   /* Get supplementary group IDs */
83 #if !defined(PETSC_MISSING_GETGROUPS)
84   ierr = getgroups(numGroups, gid+1); if (ierr < 0) SETERRQ(PETSC_COMM_SELF,ierr, "Unable to obtain supplementary group IDs");
85 #endif
86 
87   /* Test for accessibility */
88   if (fuid == uid) {
89     rbit = S_IRUSR;
90     wbit = S_IWUSR;
91     ebit = S_IXUSR;
92   } else {
93     int g;
94 
95     for(g = 0; g <= numGroups; g++) {
96       if (fgid == gid[g]) {
97         rbit = S_IRGRP;
98         wbit = S_IWGRP;
99         ebit = S_IXGRP;
100         break;
101       }
102     }
103   }
104   ierr = PetscFree(gid);CHKERRQ(ierr);
105 
106   if (mode == 'r') {
107     if (fmode & rbit) *flg = PETSC_TRUE;
108   } else if (mode == 'w') {
109     if (fmode & wbit) *flg = PETSC_TRUE;
110   } else if (mode == 'x') {
111     if (fmode & ebit) *flg = PETSC_TRUE;
112   }
113   PetscFunctionReturn(0);
114 }
115 
116 #endif /* PETSC_HAVE_ACCESS */
117 
118 #undef __FUNCT__
119 #define __FUNCT__ "PetscGetFileStat"
120 static PetscErrorCode PetscGetFileStat(const char fname[], uid_t *fileUid, gid_t *fileGid, int *fileMode,PetscBool  *exists)
121 {
122   struct stat    statbuf;
123   PetscErrorCode ierr;
124 
125   PetscFunctionBegin;
126 #if defined(PETSC_HAVE_STAT_NO_CONST)
127   ierr = stat((char*) fname, &statbuf);
128 #else
129   ierr = stat(fname, &statbuf);
130 #endif
131   if (ierr) {
132     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");
133     ierr = PetscInfo1(PETSC_NULL,"System call stat() failed on file %s\n",fname);CHKERRQ(ierr);
134     *exists = PETSC_FALSE;
135   } else {
136      ierr = PetscInfo1(PETSC_NULL,"System call stat() succeeded on file %s\n",fname);CHKERRQ(ierr);
137     *exists = PETSC_TRUE;
138     *fileUid  = statbuf.st_uid;
139     *fileGid  = statbuf.st_gid;
140     *fileMode = statbuf.st_mode;
141   }
142   PetscFunctionReturn(0);
143 }
144 
145 #undef __FUNCT__
146 #define __FUNCT__ "PetscTestFile"
147 PetscErrorCode  PetscTestFile(const char fname[], char mode, PetscBool  *flg)
148 {
149   uid_t          fuid;
150   gid_t          fgid;
151   int            fmode;
152   PetscErrorCode ierr;
153   PetscBool      exists;
154 
155   PetscFunctionBegin;
156   *flg = PETSC_FALSE;
157   if (!fname) PetscFunctionReturn(0);
158 
159   ierr = PetscGetFileStat(fname, &fuid, &fgid, &fmode,&exists);CHKERRQ(ierr);
160   if (!exists) PetscFunctionReturn(0);
161   /* Except for systems that have this broken stat macros (rare), this is the correct way to check for a regular file */
162   if (!S_ISREG(fmode)) PetscFunctionReturn(0);
163 
164   ierr = PetscTestOwnership(fname, mode, fuid, fgid, fmode, flg);CHKERRQ(ierr);
165   PetscFunctionReturn(0);
166 }
167 
168 #undef __FUNCT__
169 #define __FUNCT__ "PetscTestDirectory"
170 PetscErrorCode  PetscTestDirectory(const char fname[],char mode,PetscBool  *flg)
171 {
172   uid_t          fuid;
173   gid_t          fgid;
174   int            fmode;
175   PetscErrorCode ierr;
176   PetscBool      exists;
177 
178   PetscFunctionBegin;
179   *flg = PETSC_FALSE;
180   if (!fname) PetscFunctionReturn(0);
181 
182   ierr = PetscGetFileStat(fname, &fuid, &fgid, &fmode,&exists);CHKERRQ(ierr);
183   if (!exists) PetscFunctionReturn(0);
184   /* Except for systems that have this broken stat macros (rare), this
185      is the correct way to check for a directory */
186   if (!S_ISDIR(fmode)) PetscFunctionReturn(0);
187 
188   ierr = PetscTestOwnership(fname, mode, fuid, fgid, fmode, flg);CHKERRQ(ierr);
189   PetscFunctionReturn(0);
190 }
191 
192 #undef __FUNCT__
193 #define __FUNCT__ "PetscLs"
194 PetscErrorCode  PetscLs(MPI_Comm comm,const char libname[],char found[],size_t tlen,PetscBool  *flg)
195 {
196   PetscErrorCode ierr;
197   size_t         len;
198   char           *f,program[PETSC_MAX_PATH_LEN];
199   FILE           *fp;
200 
201   PetscFunctionBegin;
202   ierr   = PetscStrcpy(program,"ls ");CHKERRQ(ierr);
203   ierr   = PetscStrcat(program,libname);CHKERRQ(ierr);
204 #if defined(PETSC_HAVE_POPEN)
205   ierr   = PetscPOpen(comm,PETSC_NULL,program,"r",&fp);CHKERRQ(ierr);
206 #else
207   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP_SYS,"Cannot run external programs on this machine");
208 #endif
209   f      = fgets(found,tlen,fp);
210   if (f) *flg = PETSC_TRUE; else *flg = PETSC_FALSE;
211   while (f) {
212     ierr  = PetscStrlen(found,&len);CHKERRQ(ierr);
213     f     = fgets(found+len,tlen-len,fp);
214   }
215   if (*flg) {ierr = PetscInfo2(0,"ls on %s gives \n%s\n",libname,found);CHKERRQ(ierr);}
216 #if defined(PETSC_HAVE_POPEN)
217   ierr   = PetscPClose(comm,fp);CHKERRQ(ierr);
218 #else
219   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP_SYS,"Cannot run external programs on this machine");
220 #endif
221   PetscFunctionReturn(0);
222 }
223