xref: /petsc/src/sys/fileio/fretrieve.c (revision f1a722f8c411b335aaa5b04a3b4219fef8ff4c99)
17d0a6c19SBarry Smith 
2e5c89e4eSSatish Balay /*
3e5c89e4eSSatish Balay       Code for opening and closing files.
4e5c89e4eSSatish Balay */
5c6db04a5SJed Brown #include <petscsys.h>
6e5c89e4eSSatish Balay #if defined(PETSC_HAVE_PWD_H)
7e5c89e4eSSatish Balay #include <pwd.h>
8e5c89e4eSSatish Balay #endif
9e5c89e4eSSatish Balay #include <ctype.h>
10e5c89e4eSSatish Balay #include <sys/stat.h>
11e5c89e4eSSatish Balay #if defined(PETSC_HAVE_UNISTD_H)
12e5c89e4eSSatish Balay #include <unistd.h>
13e5c89e4eSSatish Balay #endif
14e5c89e4eSSatish Balay #if defined(PETSC_HAVE_SYS_UTSNAME_H)
15e5c89e4eSSatish Balay #include <sys/utsname.h>
16e5c89e4eSSatish Balay #endif
17e5c89e4eSSatish Balay #include <fcntl.h>
18e5c89e4eSSatish Balay #include <time.h>
19e5c89e4eSSatish Balay #if defined(PETSC_HAVE_SYS_SYSTEMINFO_H)
20e5c89e4eSSatish Balay #include <sys/systeminfo.h>
21e5c89e4eSSatish Balay #endif
22e5c89e4eSSatish Balay 
23480cf27aSJed Brown /*
24480cf27aSJed Brown    Private routine to delete tmp/shared storage
25480cf27aSJed Brown 
26480cf27aSJed Brown    This is called by MPI, not by users.
27480cf27aSJed Brown 
2812801b39SBarry Smith    Note: this is declared extern "C" because it is passed to MPI_Comm_create_keyval()
29480cf27aSJed Brown 
30480cf27aSJed Brown */
318cc058d9SJed Brown PETSC_EXTERN PetscMPIInt MPIAPI Petsc_DelTmpShared(MPI_Comm comm,PetscMPIInt keyval,void *count_val,void *extra_state)
32480cf27aSJed Brown {
33480cf27aSJed Brown   PetscErrorCode ierr;
34480cf27aSJed Brown 
35480cf27aSJed Brown   PetscFunctionBegin;
367d3de750SJacob Faibussowitsch   ierr = PetscInfo(NULL,"Deleting tmp/shared data in an MPI_Comm %ld\n",(long)comm);CHKERRMPI(ierr);
3712801b39SBarry Smith   ierr = PetscFree(count_val);CHKERRMPI(ierr);
38480cf27aSJed Brown   PetscFunctionReturn(MPI_SUCCESS);
39480cf27aSJed Brown }
40e5c89e4eSSatish Balay 
41e5c89e4eSSatish Balay /*@C
42e5c89e4eSSatish Balay    PetscGetTmp - Gets the name of the tmp directory
43e5c89e4eSSatish Balay 
44d083f849SBarry Smith    Collective
45e5c89e4eSSatish Balay 
46e5c89e4eSSatish Balay    Input Parameters:
47e5c89e4eSSatish Balay +  comm - MPI_Communicator that may share /tmp
48e5c89e4eSSatish Balay -  len - length of string to hold name
49e5c89e4eSSatish Balay 
50f899ff85SJose E. Roman    Output Parameter:
51e5c89e4eSSatish Balay .  dir - directory name
52e5c89e4eSSatish Balay 
53e5c89e4eSSatish Balay    Options Database Keys:
5410699b91SBarry Smith +    -shared_tmp  - indicates the directory is shared among the MPI ranks
5510699b91SBarry Smith .    -not_shared_tmp - indicates the directory is not shared among the MPI ranks
5610699b91SBarry Smith -    -tmp tmpdir - name of the directory you wish to use as /tmp
57e5c89e4eSSatish Balay 
58e5c89e4eSSatish Balay    Environmental Variables:
5910699b91SBarry Smith +     PETSC_SHARED_TMP - indicates the directory is shared among the MPI ranks
6010699b91SBarry Smith .     PETSC_NOT_SHARED_TMP - indicates the directory is not shared among the MPI ranks
6110699b91SBarry Smith -     PETSC_TMP - name of the directory you wish to use as /tmp
62e5c89e4eSSatish Balay 
63e5c89e4eSSatish Balay    Level: developer
64e5c89e4eSSatish Balay 
65e5c89e4eSSatish Balay @*/
667087cfbeSBarry Smith PetscErrorCode  PetscGetTmp(MPI_Comm comm,char dir[],size_t len)
67e5c89e4eSSatish Balay {
68e5c89e4eSSatish Balay   PetscErrorCode ierr;
69ace3abfcSBarry Smith   PetscBool      flg;
70e5c89e4eSSatish Balay 
71e5c89e4eSSatish Balay   PetscFunctionBegin;
72e5c89e4eSSatish Balay   ierr = PetscOptionsGetenv(comm,"PETSC_TMP",dir,len,&flg);CHKERRQ(ierr);
73e5c89e4eSSatish Balay   if (!flg) {
74e5c89e4eSSatish Balay     ierr = PetscStrncpy(dir,"/tmp",len);CHKERRQ(ierr);
75e5c89e4eSSatish Balay   }
76e5c89e4eSSatish Balay   PetscFunctionReturn(0);
77e5c89e4eSSatish Balay }
78e5c89e4eSSatish Balay 
79e5c89e4eSSatish Balay /*@C
80e5c89e4eSSatish Balay    PetscSharedTmp - Determines if all processors in a communicator share a
81e5c89e4eSSatish Balay          /tmp or have different ones.
82e5c89e4eSSatish Balay 
83d083f849SBarry Smith    Collective
84e5c89e4eSSatish Balay 
85e5c89e4eSSatish Balay    Input Parameters:
86e5c89e4eSSatish Balay .  comm - MPI_Communicator that may share /tmp
87e5c89e4eSSatish Balay 
88e5c89e4eSSatish Balay    Output Parameters:
89e5c89e4eSSatish Balay .  shared - PETSC_TRUE or PETSC_FALSE
90e5c89e4eSSatish Balay 
91e5c89e4eSSatish Balay    Options Database Keys:
9210699b91SBarry Smith +    -shared_tmp  - indicates the directory is shared among the MPI ranks
9310699b91SBarry Smith .    -not_shared_tmp - indicates the directory is not shared among the MPI ranks
9410699b91SBarry Smith -    -tmp tmpdir - name of the directory you wish to use as /tmp
95e5c89e4eSSatish Balay 
96e5c89e4eSSatish Balay    Environmental Variables:
9710699b91SBarry Smith +     PETSC_SHARED_TMP  - indicates the directory is shared among the MPI ranks
9810699b91SBarry Smith .     PETSC_NOT_SHARED_TMP - indicates the directory is not shared among the MPI ranks
9910699b91SBarry Smith -     PETSC_TMP - name of the directory you wish to use as /tmp
100e5c89e4eSSatish Balay 
101e5c89e4eSSatish Balay    Level: developer
102e5c89e4eSSatish Balay 
103e5c89e4eSSatish Balay    Notes:
104e5c89e4eSSatish Balay    Stores the status as a MPI attribute so it does not have
105e5c89e4eSSatish Balay     to be redetermined each time.
106e5c89e4eSSatish Balay 
107e5c89e4eSSatish Balay       Assumes that all processors in a communicator either
108e5c89e4eSSatish Balay        1) have a common /tmp or
109a8c7a070SBarry Smith        2) each has a separate /tmp
110e5c89e4eSSatish Balay       eventually we can write a fancier one that determines which processors
111e5c89e4eSSatish Balay       share a common /tmp.
112e5c89e4eSSatish Balay 
113e5c89e4eSSatish Balay    This will be very slow on runs with a large number of processors since
114e5c89e4eSSatish Balay    it requires O(p*p) file opens.
115e5c89e4eSSatish Balay 
116e5c89e4eSSatish Balay    If the environmental variable PETSC_TMP is set it will use this directory
117e5c89e4eSSatish Balay   as the "/tmp" directory.
118e5c89e4eSSatish Balay 
119e5c89e4eSSatish Balay @*/
1207087cfbeSBarry Smith PetscErrorCode  PetscSharedTmp(MPI_Comm comm,PetscBool  *shared)
121e5c89e4eSSatish Balay {
122e5c89e4eSSatish Balay   PetscErrorCode     ierr;
123e5c89e4eSSatish Balay   PetscMPIInt        size,rank,*tagvalp,sum,cnt,i;
124ace3abfcSBarry Smith   PetscBool          flg,iflg;
125e5c89e4eSSatish Balay   FILE               *fd;
126e5c89e4eSSatish Balay   static PetscMPIInt Petsc_Tmp_keyval = MPI_KEYVAL_INVALID;
127ed9cf6e9SBarry Smith   int                err;
128e5c89e4eSSatish Balay 
129e5c89e4eSSatish Balay   PetscFunctionBegin;
130ffc4695bSBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRMPI(ierr);
131e5c89e4eSSatish Balay   if (size == 1) {
132e5c89e4eSSatish Balay     *shared = PETSC_TRUE;
133e5c89e4eSSatish Balay     PetscFunctionReturn(0);
134e5c89e4eSSatish Balay   }
135e5c89e4eSSatish Balay 
1360298fd71SBarry Smith   ierr = PetscOptionsGetenv(comm,"PETSC_SHARED_TMP",NULL,0,&flg);CHKERRQ(ierr);
137e5c89e4eSSatish Balay   if (flg) {
138e5c89e4eSSatish Balay     *shared = PETSC_TRUE;
139e5c89e4eSSatish Balay     PetscFunctionReturn(0);
140e5c89e4eSSatish Balay   }
141e5c89e4eSSatish Balay 
1420298fd71SBarry Smith   ierr = PetscOptionsGetenv(comm,"PETSC_NOT_SHARED_TMP",NULL,0,&flg);CHKERRQ(ierr);
143e5c89e4eSSatish Balay   if (flg) {
144e5c89e4eSSatish Balay     *shared = PETSC_FALSE;
145e5c89e4eSSatish Balay     PetscFunctionReturn(0);
146e5c89e4eSSatish Balay   }
147e5c89e4eSSatish Balay 
148e5c89e4eSSatish Balay   if (Petsc_Tmp_keyval == MPI_KEYVAL_INVALID) {
149ffc4695bSBarry Smith     ierr = MPI_Comm_create_keyval(MPI_COMM_NULL_COPY_FN,Petsc_DelTmpShared,&Petsc_Tmp_keyval,NULL);CHKERRMPI(ierr);
150e5c89e4eSSatish Balay   }
151e5c89e4eSSatish Balay 
152ffc4695bSBarry Smith   ierr = MPI_Comm_get_attr(comm,Petsc_Tmp_keyval,(void**)&tagvalp,(int*)&iflg);CHKERRMPI(ierr);
153e5c89e4eSSatish Balay   if (!iflg) {
154e5c89e4eSSatish Balay     char filename[PETSC_MAX_PATH_LEN],tmpname[PETSC_MAX_PATH_LEN];
155e5c89e4eSSatish Balay 
156e5c89e4eSSatish Balay     /* This communicator does not yet have a shared tmp attribute */
157854ce69bSBarry Smith     ierr = PetscMalloc1(1,&tagvalp);CHKERRQ(ierr);
158ffc4695bSBarry Smith     ierr = MPI_Comm_set_attr(comm,Petsc_Tmp_keyval,tagvalp);CHKERRMPI(ierr);
159e5c89e4eSSatish Balay 
160e5c89e4eSSatish Balay     ierr = PetscOptionsGetenv(comm,"PETSC_TMP",tmpname,238,&iflg);CHKERRQ(ierr);
161e5c89e4eSSatish Balay     if (!iflg) {
162e5c89e4eSSatish Balay       ierr = PetscStrcpy(filename,"/tmp");CHKERRQ(ierr);
163e5c89e4eSSatish Balay     } else {
164e5c89e4eSSatish Balay       ierr = PetscStrcpy(filename,tmpname);CHKERRQ(ierr);
165e5c89e4eSSatish Balay     }
166e5c89e4eSSatish Balay 
167e5c89e4eSSatish Balay     ierr = PetscStrcat(filename,"/petsctestshared");CHKERRQ(ierr);
168ffc4695bSBarry Smith     ierr = MPI_Comm_rank(comm,&rank);CHKERRMPI(ierr);
169e5c89e4eSSatish Balay 
170e5c89e4eSSatish Balay     /* each processor creates a /tmp file and all the later ones check */
171e5c89e4eSSatish Balay     /* this makes sure no subset of processors is shared */
172e5c89e4eSSatish Balay     *shared = PETSC_FALSE;
173e5c89e4eSSatish Balay     for (i=0; i<size-1; i++) {
174e5c89e4eSSatish Balay       if (rank == i) {
175e5c89e4eSSatish Balay         fd = fopen(filename,"w");
1762c71b3e2SJacob Faibussowitsch         PetscCheckFalse(!fd,PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to open test file %s",filename);
177ed9cf6e9SBarry Smith         err = fclose(fd);
1782c71b3e2SJacob Faibussowitsch         PetscCheckFalse(err,PETSC_COMM_SELF,PETSC_ERR_SYS,"fclose() failed on file");
179e5c89e4eSSatish Balay       }
180ffc4695bSBarry Smith       ierr = MPI_Barrier(comm);CHKERRMPI(ierr);
181e5c89e4eSSatish Balay       if (rank >= i) {
182e5c89e4eSSatish Balay         fd = fopen(filename,"r");
183a297a907SKarl Rupp         if (fd) cnt = 1;
184a297a907SKarl Rupp         else cnt = 0;
185e5c89e4eSSatish Balay         if (fd) {
186ed9cf6e9SBarry Smith           err = fclose(fd);
1872c71b3e2SJacob Faibussowitsch           PetscCheckFalse(err,PETSC_COMM_SELF,PETSC_ERR_SYS,"fclose() failed on file");
188e5c89e4eSSatish Balay         }
189a297a907SKarl Rupp       } else cnt = 0;
190a297a907SKarl Rupp 
191820f2d46SBarry Smith       ierr = MPIU_Allreduce(&cnt,&sum,1,MPI_INT,MPI_SUM,comm);CHKERRMPI(ierr);
192a297a907SKarl Rupp       if (rank == i) unlink(filename);
193e5c89e4eSSatish Balay 
194e5c89e4eSSatish Balay       if (sum == size) {
195e5c89e4eSSatish Balay         *shared = PETSC_TRUE;
196e5c89e4eSSatish Balay         break;
1972c71b3e2SJacob Faibussowitsch       } else PetscCheckFalse(sum != 1,PETSC_COMM_SELF,PETSC_ERR_SUP_SYS,"Subset of processes share /tmp ");
198e5c89e4eSSatish Balay     }
199e5c89e4eSSatish Balay     *tagvalp = (int)*shared;
2007d3de750SJacob Faibussowitsch     ierr = PetscInfo(NULL,"processors %s %s\n",(*shared) ? "share":"do NOT share",(iflg ? tmpname:"/tmp"));CHKERRQ(ierr);
201a297a907SKarl Rupp   } else *shared = (PetscBool) *tagvalp;
202e5c89e4eSSatish Balay   PetscFunctionReturn(0);
203e5c89e4eSSatish Balay }
204e5c89e4eSSatish Balay 
205e5c89e4eSSatish Balay /*@C
206*f1a722f8SMatthew G. Knepley   PetscSharedWorkingDirectory - Determines if all processors in a communicator share a working directory or have different ones.
207e5c89e4eSSatish Balay 
208d083f849SBarry Smith   Collective
209e5c89e4eSSatish Balay 
2106b867d5aSJose E. Roman   Input Parameter:
211e5c89e4eSSatish Balay . comm - MPI_Communicator that may share working directory
212e5c89e4eSSatish Balay 
2136b867d5aSJose E. Roman   Output Parameter:
214e5c89e4eSSatish Balay . shared - PETSC_TRUE or PETSC_FALSE
215e5c89e4eSSatish Balay 
216e5c89e4eSSatish Balay   Options Database Keys:
21710699b91SBarry Smith + -shared_working_directory - indicates the directory is shared among the MPI ranks
21810699b91SBarry Smith - -not_shared_working_directory - indicates the directory is shared among the MPI ranks
219e5c89e4eSSatish Balay 
220e5c89e4eSSatish Balay   Environmental Variables:
22110699b91SBarry Smith + PETSC_SHARED_WORKING_DIRECTORY - indicates the directory is shared among the MPI ranks
2223222ab0cSSatish Balay - PETSC_NOT_SHARED_WORKING_DIRECTORY - indicates the directory is shared among the MPI ranks
223e5c89e4eSSatish Balay 
224e5c89e4eSSatish Balay   Level: developer
225e5c89e4eSSatish Balay 
226e5c89e4eSSatish Balay   Notes:
227*f1a722f8SMatthew G. Knepley   Stores the status as a MPI attribute so it does not have to be redetermined each time.
228e5c89e4eSSatish Balay 
229e5c89e4eSSatish Balay   Assumes that all processors in a communicator either
230*f1a722f8SMatthew G. Knepley $   1) have a common working directory or
231*f1a722f8SMatthew G. Knepley $   2) each has a separate working directory
232*f1a722f8SMatthew G. Knepley   eventually we can write a fancier one that determines which processors share a common working directory.
233e5c89e4eSSatish Balay 
234*f1a722f8SMatthew G. Knepley   This will be very slow on runs with a large number of processors since it requires O(p*p) file opens.
235e5c89e4eSSatish Balay @*/
2367087cfbeSBarry Smith PetscErrorCode PetscSharedWorkingDirectory(MPI_Comm comm, PetscBool *shared)
237e5c89e4eSSatish Balay {
238e5c89e4eSSatish Balay   PetscErrorCode     ierr;
239e5c89e4eSSatish Balay   PetscMPIInt        size,rank,*tagvalp,sum,cnt,i;
240ace3abfcSBarry Smith   PetscBool          flg,iflg;
241e5c89e4eSSatish Balay   FILE               *fd;
242e5c89e4eSSatish Balay   static PetscMPIInt Petsc_WD_keyval = MPI_KEYVAL_INVALID;
243ed9cf6e9SBarry Smith   int                err;
244e5c89e4eSSatish Balay 
245e5c89e4eSSatish Balay   PetscFunctionBegin;
246ffc4695bSBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRMPI(ierr);
247e5c89e4eSSatish Balay   if (size == 1) {
248e5c89e4eSSatish Balay     *shared = PETSC_TRUE;
249e5c89e4eSSatish Balay     PetscFunctionReturn(0);
250e5c89e4eSSatish Balay   }
251e5c89e4eSSatish Balay 
2520298fd71SBarry Smith   ierr = PetscOptionsGetenv(comm,"PETSC_SHARED_WORKING_DIRECTORY",NULL,0,&flg);CHKERRQ(ierr);
253e5c89e4eSSatish Balay   if (flg) {
254e5c89e4eSSatish Balay     *shared = PETSC_TRUE;
255e5c89e4eSSatish Balay     PetscFunctionReturn(0);
256e5c89e4eSSatish Balay   }
257e5c89e4eSSatish Balay 
2580298fd71SBarry Smith   ierr = PetscOptionsGetenv(comm,"PETSC_NOT_SHARED_WORKING_DIRECTORY",NULL,0,&flg);CHKERRQ(ierr);
259e5c89e4eSSatish Balay   if (flg) {
260e5c89e4eSSatish Balay     *shared = PETSC_FALSE;
261e5c89e4eSSatish Balay     PetscFunctionReturn(0);
262e5c89e4eSSatish Balay   }
263e5c89e4eSSatish Balay 
264e5c89e4eSSatish Balay   if (Petsc_WD_keyval == MPI_KEYVAL_INVALID) {
265ffc4695bSBarry Smith     ierr = MPI_Comm_create_keyval(MPI_COMM_NULL_COPY_FN,Petsc_DelTmpShared,&Petsc_WD_keyval,NULL);CHKERRMPI(ierr);
266e5c89e4eSSatish Balay   }
267e5c89e4eSSatish Balay 
268ffc4695bSBarry Smith   ierr = MPI_Comm_get_attr(comm,Petsc_WD_keyval,(void**)&tagvalp,(int*)&iflg);CHKERRMPI(ierr);
269e5c89e4eSSatish Balay   if (!iflg) {
270e5c89e4eSSatish Balay     char filename[PETSC_MAX_PATH_LEN];
271e5c89e4eSSatish Balay 
272e5c89e4eSSatish Balay     /* This communicator does not yet have a shared  attribute */
273854ce69bSBarry Smith     ierr = PetscMalloc1(1,&tagvalp);CHKERRQ(ierr);
274ffc4695bSBarry Smith     ierr = MPI_Comm_set_attr(comm,Petsc_WD_keyval,tagvalp);CHKERRMPI(ierr);
275e5c89e4eSSatish Balay 
276e5c89e4eSSatish Balay     ierr = PetscGetWorkingDirectory(filename,240);CHKERRQ(ierr);
277e5c89e4eSSatish Balay     ierr = PetscStrcat(filename,"/petsctestshared");CHKERRQ(ierr);
278ffc4695bSBarry Smith     ierr = MPI_Comm_rank(comm,&rank);CHKERRMPI(ierr);
279e5c89e4eSSatish Balay 
280e5c89e4eSSatish Balay     /* each processor creates a  file and all the later ones check */
281e5c89e4eSSatish Balay     /* this makes sure no subset of processors is shared */
282e5c89e4eSSatish Balay     *shared = PETSC_FALSE;
283e5c89e4eSSatish Balay     for (i=0; i<size-1; i++) {
284e5c89e4eSSatish Balay       if (rank == i) {
285e5c89e4eSSatish Balay         fd = fopen(filename,"w");
2862c71b3e2SJacob Faibussowitsch         PetscCheckFalse(!fd,PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to open test file %s",filename);
287ed9cf6e9SBarry Smith         err = fclose(fd);
2882c71b3e2SJacob Faibussowitsch         PetscCheckFalse(err,PETSC_COMM_SELF,PETSC_ERR_SYS,"fclose() failed on file");
289e5c89e4eSSatish Balay       }
290ffc4695bSBarry Smith       ierr = MPI_Barrier(comm);CHKERRMPI(ierr);
291e5c89e4eSSatish Balay       if (rank >= i) {
292e5c89e4eSSatish Balay         fd = fopen(filename,"r");
293a297a907SKarl Rupp         if (fd) cnt = 1;
294a297a907SKarl Rupp         else cnt = 0;
295e5c89e4eSSatish Balay         if (fd) {
296ed9cf6e9SBarry Smith           err = fclose(fd);
2972c71b3e2SJacob Faibussowitsch           PetscCheckFalse(err,PETSC_COMM_SELF,PETSC_ERR_SYS,"fclose() failed on file");
298e5c89e4eSSatish Balay         }
299a297a907SKarl Rupp       } else cnt = 0;
300a297a907SKarl Rupp 
301820f2d46SBarry Smith       ierr = MPIU_Allreduce(&cnt,&sum,1,MPI_INT,MPI_SUM,comm);CHKERRMPI(ierr);
302a297a907SKarl Rupp       if (rank == i) unlink(filename);
303e5c89e4eSSatish Balay 
304e5c89e4eSSatish Balay       if (sum == size) {
305e5c89e4eSSatish Balay         *shared = PETSC_TRUE;
306e5c89e4eSSatish Balay         break;
3072c71b3e2SJacob Faibussowitsch       } else PetscCheckFalse(sum != 1,PETSC_COMM_SELF,PETSC_ERR_SUP_SYS,"Subset of processes share working directory");
308e5c89e4eSSatish Balay     }
309e5c89e4eSSatish Balay     *tagvalp = (int)*shared;
310a297a907SKarl Rupp   } else *shared = (PetscBool) *tagvalp;
3117d3de750SJacob Faibussowitsch   ierr = PetscInfo(NULL,"processors %s working directory\n",(*shared) ? "shared" : "do NOT share");CHKERRQ(ierr);
312e5c89e4eSSatish Balay   PetscFunctionReturn(0);
313e5c89e4eSSatish Balay }
314e5c89e4eSSatish Balay 
315e5c89e4eSSatish Balay /*@C
3160c4f890aSBarry Smith     PetscFileRetrieve - Obtains a file from a URL or compressed
317e5c89e4eSSatish Balay         and copies into local disk space as uncompressed.
318e5c89e4eSSatish Balay 
319d083f849SBarry Smith     Collective
320e5c89e4eSSatish Balay 
321d8d19677SJose E. Roman     Input Parameters:
3220c4f890aSBarry Smith +   comm     - processors accessing the file
3230c4f890aSBarry Smith .   url      - name of file, including entire URL (with or without .gz)
3240c4f890aSBarry Smith -   llen     - length of localname
325e5c89e4eSSatish Balay 
326d8d19677SJose E. Roman     Output Parameters:
32708fb59bfSBarry Smith +   localname - name of local copy of file - valid on only process zero
32808fb59bfSBarry Smith -   found - if found or retrieved the file - valid on all processes
329e5c89e4eSSatish Balay 
33095452b02SPatrick Sanan     Notes:
33195452b02SPatrick Sanan     if the file already exists local this function just returns without downloading it.
332e5c89e4eSSatish Balay 
3330c4f890aSBarry Smith     Level: intermediate
334e5c89e4eSSatish Balay @*/
3350c4f890aSBarry Smith PetscErrorCode  PetscFileRetrieve(MPI_Comm comm,const char url[],char localname[],size_t llen,PetscBool  *found)
336e5c89e4eSSatish Balay {
33708fb59bfSBarry Smith   char           buffer[PETSC_MAX_PATH_LEN],*par,*tlocalname,name[PETSC_MAX_PATH_LEN];
338e5c89e4eSSatish Balay   FILE           *fp;
339e5c89e4eSSatish Balay   PetscErrorCode ierr;
340e5c89e4eSSatish Balay   PetscMPIInt    rank;
341e5c89e4eSSatish Balay   size_t         len = 0;
34208fb59bfSBarry Smith   PetscBool      flg1,flg2,flg3,flg4,download,compressed = PETSC_FALSE;
343e5c89e4eSSatish Balay 
344e5c89e4eSSatish Balay   PetscFunctionBegin;
345ffc4695bSBarry Smith   ierr = MPI_Comm_rank(comm,&rank);CHKERRMPI(ierr);
346dd400576SPatrick Sanan   if (rank == 0) {
347e5c89e4eSSatish Balay     *found = PETSC_FALSE;
348e5c89e4eSSatish Balay 
3490c4f890aSBarry Smith     ierr = PetscStrstr(url,".gz",&par);CHKERRQ(ierr);
35008fb59bfSBarry Smith     if (par) {
35108fb59bfSBarry Smith       ierr = PetscStrlen(par,&len);CHKERRQ(ierr);
35208fb59bfSBarry Smith       if (len == 3) compressed = PETSC_TRUE;
35308fb59bfSBarry Smith     }
354e5c89e4eSSatish Balay 
3550c4f890aSBarry Smith     ierr = PetscStrncmp(url,"ftp://",6,&flg1);CHKERRQ(ierr);
3560c4f890aSBarry Smith     ierr = PetscStrncmp(url,"http://",7,&flg2);CHKERRQ(ierr);
3570c4f890aSBarry Smith     ierr = PetscStrncmp(url,"file://",7,&flg3);CHKERRQ(ierr);
35808fb59bfSBarry Smith     ierr = PetscStrncmp(url,"https://",8,&flg4);CHKERRQ(ierr);
359a4772d12SBarry Smith     download = (PetscBool) (flg1 || flg2 || flg3 || flg4);
36008fb59bfSBarry Smith 
36108fb59bfSBarry Smith     if (!download && !compressed) {
3620c4f890aSBarry Smith       ierr = PetscStrncpy(localname,url,llen);CHKERRQ(ierr);
3630c4f890aSBarry Smith       ierr = PetscTestFile(url,'r',found);CHKERRQ(ierr);
364487e5849SBarry Smith       if (*found) {
3657d3de750SJacob Faibussowitsch         ierr = PetscInfo(NULL,"Found file %s\n",url);CHKERRQ(ierr);
366487e5849SBarry Smith       } else {
3677d3de750SJacob Faibussowitsch         ierr = PetscInfo(NULL,"Did not find file %s\n",url);CHKERRQ(ierr);
368487e5849SBarry Smith       }
36908fb59bfSBarry Smith       goto done;
370734f99bcSBarry Smith     }
371734f99bcSBarry Smith 
37205698389SBarry Smith     /* look for uncompressed file in requested directory */
37305698389SBarry Smith     if (compressed) {
37405698389SBarry Smith       ierr = PetscStrncpy(localname,url,llen);CHKERRQ(ierr);
37505698389SBarry Smith       ierr = PetscStrstr(localname,".gz",&par);CHKERRQ(ierr);
37605698389SBarry Smith       *par = 0; /* remove .gz extension */
37705698389SBarry Smith       ierr = PetscTestFile(localname,'r',found);CHKERRQ(ierr);
37805698389SBarry Smith       if (*found) goto done;
37905698389SBarry Smith     }
38005698389SBarry Smith 
38105698389SBarry Smith     /* look for file in current directory */
3820c4f890aSBarry Smith     ierr = PetscStrrchr(url,'/',&tlocalname);CHKERRQ(ierr);
3830c4f890aSBarry Smith     ierr = PetscStrncpy(localname,tlocalname,llen);CHKERRQ(ierr);
38408fb59bfSBarry Smith     if (compressed) {
38508fb59bfSBarry Smith       ierr = PetscStrstr(localname,".gz",&par);CHKERRQ(ierr);
38608fb59bfSBarry Smith       *par = 0; /* remove .gz extension */
38708fb59bfSBarry Smith     }
3880c4f890aSBarry Smith     ierr = PetscTestFile(localname,'r',found);CHKERRQ(ierr);
38908fb59bfSBarry Smith     if (*found) goto done;
390e5c89e4eSSatish Balay 
39108fb59bfSBarry Smith     if (download) {
39208fb59bfSBarry Smith       /* local file is not already here so use curl to get it */
39308fb59bfSBarry Smith       ierr = PetscStrncpy(localname,tlocalname,llen);CHKERRQ(ierr);
394e8eec0a8SKarl Rupp       ierr = PetscStrcpy(buffer,"curl --fail --silent --show-error ");CHKERRQ(ierr);
39508fb59bfSBarry Smith       ierr = PetscStrcat(buffer,url);CHKERRQ(ierr);
39608fb59bfSBarry Smith       ierr = PetscStrcat(buffer," > ");CHKERRQ(ierr);
39708fb59bfSBarry Smith       ierr = PetscStrcat(buffer,localname);CHKERRQ(ierr);
398e5c89e4eSSatish Balay #if defined(PETSC_HAVE_POPEN)
39908fb59bfSBarry Smith       ierr = PetscPOpen(PETSC_COMM_SELF,NULL,buffer,"r",&fp);CHKERRQ(ierr);
400016831caSBarry Smith       ierr = PetscPClose(PETSC_COMM_SELF,fp);CHKERRQ(ierr);
401e5c89e4eSSatish Balay #else
402e32f2f54SBarry Smith       SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP_SYS,"Cannot run external programs on this machine");
403e5c89e4eSSatish Balay #endif
4040c4f890aSBarry Smith       ierr = PetscTestFile(localname,'r',found);CHKERRQ(ierr);
4056e3a5469SBarry Smith       if (*found) {
4066e3a5469SBarry Smith         FILE      *fd;
4076e3a5469SBarry Smith         char      buf[1024],*str,*substring;
4086e3a5469SBarry Smith 
4096e3a5469SBarry Smith         /* check if the file didn't exist so it downloaded an HTML message instead */
4106e3a5469SBarry Smith         fd = fopen(localname,"r");
4112c71b3e2SJacob Faibussowitsch         PetscCheckFalse(!fd,PETSC_COMM_SELF,PETSC_ERR_PLIB,"PetscTestFile() indicates %s exists but fopen() cannot open it",localname);
4126e3a5469SBarry Smith         str = fgets(buf,sizeof(buf)-1,fd);
4136e3a5469SBarry Smith         while (str) {
4146e3a5469SBarry Smith           ierr = PetscStrstr(buf,"<!DOCTYPE html>",&substring);CHKERRQ(ierr);
4152c71b3e2SJacob Faibussowitsch           PetscCheckFalse(substring,PETSC_COMM_SELF,PETSC_ERR_PLIB,"Unable to download %s it does not appear to exist at this URL, dummy HTML file was downloaded",url);
4166e3a5469SBarry Smith           ierr = PetscStrstr(buf,"Not Found",&substring);CHKERRQ(ierr);
4172c71b3e2SJacob Faibussowitsch           PetscCheckFalse(substring,PETSC_COMM_SELF,PETSC_ERR_PLIB,"Unable to download %s it does not appear to exist at this URL, dummy HTML file was downloaded",url);
4186e3a5469SBarry Smith           str = fgets(buf,sizeof(buf)-1,fd);
4196e3a5469SBarry Smith         }
4206e3a5469SBarry Smith         fclose(fd);
4216e3a5469SBarry Smith       }
42208fb59bfSBarry Smith     } else if (compressed) {
42308fb59bfSBarry Smith       ierr = PetscTestFile(url,'r',found);CHKERRQ(ierr);
42408fb59bfSBarry Smith       if (!*found) goto done;
42508fb59bfSBarry Smith       ierr = PetscStrncpy(localname,url,llen);CHKERRQ(ierr);
42608fb59bfSBarry Smith     }
42708fb59bfSBarry Smith     if (compressed) {
42808fb59bfSBarry Smith       ierr = PetscStrrchr(localname,'/',&tlocalname);CHKERRQ(ierr);
42908fb59bfSBarry Smith       ierr = PetscStrncpy(name,tlocalname,PETSC_MAX_PATH_LEN);CHKERRQ(ierr);
43008fb59bfSBarry Smith       ierr = PetscStrstr(name,".gz",&par);CHKERRQ(ierr);
43108fb59bfSBarry Smith       *par = 0; /* remove .gz extension */
43208fb59bfSBarry Smith       /* uncompress file */
43308fb59bfSBarry Smith       ierr = PetscStrcpy(buffer,"gzip -c -d ");CHKERRQ(ierr);
43408fb59bfSBarry Smith       ierr = PetscStrcat(buffer,localname);CHKERRQ(ierr);
43508fb59bfSBarry Smith       ierr = PetscStrcat(buffer," > ");CHKERRQ(ierr);
43608fb59bfSBarry Smith       ierr = PetscStrcat(buffer,name);CHKERRQ(ierr);
43708fb59bfSBarry Smith #if defined(PETSC_HAVE_POPEN)
43808fb59bfSBarry Smith       ierr = PetscPOpen(PETSC_COMM_SELF,NULL,buffer,"r",&fp);CHKERRQ(ierr);
439016831caSBarry Smith       ierr = PetscPClose(PETSC_COMM_SELF,fp);CHKERRQ(ierr);
44008fb59bfSBarry Smith #else
44108fb59bfSBarry Smith       SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP_SYS,"Cannot run external programs on this machine");
44208fb59bfSBarry Smith #endif
44308fb59bfSBarry Smith       ierr = PetscStrncpy(localname,name,llen);CHKERRQ(ierr);
44408fb59bfSBarry Smith       ierr = PetscTestFile(localname,'r',found);CHKERRQ(ierr);
445e5c89e4eSSatish Balay     }
446e5c89e4eSSatish Balay   }
447955d42a0SBarry Smith   done:
448ffc4695bSBarry Smith   ierr = MPI_Bcast(found,1,MPIU_BOOL,0,comm);CHKERRMPI(ierr);
449ffc4695bSBarry Smith   ierr = MPI_Bcast(localname, llen, MPI_CHAR, 0, comm);CHKERRMPI(ierr);
450e5c89e4eSSatish Balay   PetscFunctionReturn(0);
451e5c89e4eSSatish Balay }
452