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