xref: /petsc/src/sys/fileio/fdir.c (revision 5896fb9e39e84ae2eb743f82d9026350ea94629a)
1 #define PETSC_DESIRE_FEATURE_TEST_MACROS /* for lstat() */
2 #include <petscsys.h>
3 #include <sys/stat.h>
4 #if defined(PETSC_HAVE_DIRECT_H)
5 #include <direct.h>
6 #endif
7 #if defined(PETSC_HAVE_IO_H)
8 #include <io.h>
9 #endif
10 #if defined (PETSC_HAVE_STDINT_H)
11 #include <stdint.h>
12 #endif
13 #if defined(PETSC_HAVE_UNISTD_H) /* for mkdtemp */
14 #include <unistd.h>
15 #endif
16 
17 PetscErrorCode PetscPathJoin(const char dname[],const char fname[],size_t n,char fullname[])
18 {
19   PetscErrorCode ierr;
20   size_t         l1,l2;
21   PetscFunctionBegin;
22   ierr = PetscStrlen(dname,&l1);CHKERRQ(ierr);
23   ierr = PetscStrlen(fname,&l2);CHKERRQ(ierr);
24   if ((l1+l2+2)>n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Path length is greater than buffer size");
25   ierr = PetscStrncpy(fullname,dname,n);CHKERRQ(ierr);
26   ierr = PetscStrlcat(fullname,"/",n);CHKERRQ(ierr);
27   ierr = PetscStrlcat(fullname,fname,n);CHKERRQ(ierr);
28   PetscFunctionReturn(0);
29 }
30 
31 PetscErrorCode PetscMkdir(const char dir[])
32 {
33   int            err;
34   PetscErrorCode ierr;
35   PetscBool      flg;
36 
37   PetscFunctionBegin;
38   ierr = PetscTestDirectory(dir,'w',&flg);CHKERRQ(ierr);
39   if (flg) PetscFunctionReturn(0);
40 #if defined(PETSC_HAVE__MKDIR) && defined(PETSC_HAVE_DIRECT_H)
41   err = _mkdir(dir);
42 #else
43   err = mkdir(dir,S_IRWXU|S_IRGRP|S_IXGRP);
44 #endif
45   if(err) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Could not create dir: %s",dir);
46   PetscFunctionReturn(0);
47 }
48 
49 /*@C
50   PetscMkdtemp - Create a folder with a unique name given a filename template.
51 
52   Not Collective
53 
54   Input Parameters:
55 . dir - file name template, the last six characters must be 'XXXXXX', and they will be modified upon return
56 
57   Level: developer
58 
59 .seealso: PetscMkdir()
60 @*/
61 PetscErrorCode PetscMkdtemp(char dir[])
62 {
63   PetscFunctionBegin;
64 #if defined(PETSC_HAVE_WINDOWS_H) && defined(PETSC_HAVE_IO_H) && defined(PETSC_HAVE__MKDIR) && defined(PETSC_HAVE_DIRECT_H)
65   {
66     int            err = 1;
67     PetscInt       i = 0,max_retry_time = 26;
68     size_t         len;
69     PetscErrorCode ierr;
70 
71     ierr = PetscStrlen(dir,&len);CHKERRQ(ierr);
72     while (err && i<max_retry_time) {
73       err = _mktemp_s(dir,len+1);
74       if (err) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Could not generate a unique name using the template: %s",dir);
75       err = _mkdir(dir);
76       i++;
77     }
78     if (err) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Exceeds maximum retry time when creating temporary dir: %s",dir);
79   }
80 #else
81   dir = mkdtemp(dir);
82   if(!dir) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Could not create temporary dir using the template: %s",dir);
83 #endif
84   PetscFunctionReturn(0);
85 }
86 
87 #if defined(PETSC_HAVE_DIRECT_H)
88 PetscErrorCode PetscRMTree(const char dir[])
89 {
90   PetscErrorCode ierr;
91   struct _finddata_t data;
92   char loc[PETSC_MAX_PATH_LEN];
93   PetscBool flg1, flg2;
94 #if defined (PETSC_HAVE_STDINT_H)
95   intptr_t handle;
96 #else
97   long handle;
98   #endif
99 
100   PetscFunctionBegin;
101   ierr = PetscPathJoin(dir,"*",PETSC_MAX_PATH_LEN,loc);CHKERRQ(ierr);
102   handle = _findfirst(loc, &data);
103   if (handle == -1) {
104     PetscBool flg;
105     ierr = PetscTestDirectory(loc,'r',&flg);CHKERRQ(ierr);
106     if (flg) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Cannot access directory to delete: %s",dir);
107     ierr = PetscTestFile(loc,'r',&flg);CHKERRQ(ierr);
108     if (flg) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Specified path is a file - not a dir: %s",dir);
109     PetscFunctionReturn(0); /* perhaps the dir was not yet created */
110   }
111   while (_findnext(handle, &data) != -1) {
112     ierr = PetscStrcmp(data.name, ".",&flg1);CHKERRQ(ierr);
113     ierr = PetscStrcmp(data.name, "..",&flg2);CHKERRQ(ierr);
114     if (flg1 || flg2) continue;
115     ierr = PetscPathJoin(dir,data.name,PETSC_MAX_PATH_LEN,loc);CHKERRQ(ierr);
116     if (data.attrib & _A_SUBDIR) {
117       ierr = PetscRMTree(loc);CHKERRQ(ierr);
118     } else{
119       if (remove(loc)) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Could not delete file: %s",loc);
120     }
121   }
122   _findclose(handle);
123   if (_rmdir(dir)) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Could not delete dir: %s",dir);
124   PetscFunctionReturn(0);
125 }
126 #else
127 #include <dirent.h>
128 #include <unistd.h>
129 PetscErrorCode PetscRMTree(const char dir[])
130 {
131   PetscErrorCode ierr;
132   struct dirent *data;
133   char loc[PETSC_MAX_PATH_LEN];
134   PetscBool flg1, flg2;
135   DIR *dirp;
136   struct stat statbuf;
137 
138   PetscFunctionBegin;
139   dirp = opendir(dir);
140   if(!dirp) {
141     PetscBool flg;
142     ierr = PetscTestDirectory(dir,'r',&flg);CHKERRQ(ierr);
143     if (flg) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Cannot access directory to delete: %s",dir);
144     ierr = PetscTestFile(dir,'r',&flg);CHKERRQ(ierr);
145     if (flg) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Specified path is a file - not a dir: %s",dir);
146     PetscFunctionReturn(0); /* perhaps the dir was not yet created */
147   }
148   while((data = readdir(dirp))) {
149     ierr = PetscStrcmp(data->d_name, ".",&flg1);CHKERRQ(ierr);
150     ierr = PetscStrcmp(data->d_name, "..",&flg2);CHKERRQ(ierr);
151     if (flg1 || flg2) continue;
152     ierr = PetscPathJoin(dir,data->d_name,PETSC_MAX_PATH_LEN,loc);CHKERRQ(ierr);
153     if (lstat(loc,&statbuf) <0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"cannot run lstat() on: %s",loc);
154     if (S_ISDIR(statbuf.st_mode)) {
155       ierr = PetscRMTree(loc);CHKERRQ(ierr);
156     } else {
157       if (unlink(loc)) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Could not delete file: %s",loc);
158     }
159   }
160   closedir(dirp);
161   if (rmdir(dir)) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Could not delete dir: %s",dir);
162   PetscFunctionReturn(0);
163 }
164 #endif
165