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