xref: /petsc/src/sys/fileio/fdir.c (revision 524fe776c8aa733ff2ef43b738fa4e354b69f6ec)
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   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 folder with a unique name given a filename 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 
139 /*@C
140   PetscRMTree - delete a directory and all of its children
141 
142   Input Parameter:
143 .  dir - the name of the directory
144 
145   Level: advanced
146 
147 .seealso: `PetscMkdtemp()`, `PetscMkdir()`
148 @*/
149 PetscErrorCode PetscRMTree(const char dir[])
150 {
151   struct dirent *data;
152   char           loc[PETSC_MAX_PATH_LEN];
153   PetscBool      flg1, flg2;
154   DIR           *dirp;
155   struct stat    statbuf;
156 
157   PetscFunctionBegin;
158   dirp = opendir(dir);
159   if (!dirp) {
160     PetscBool flg;
161     PetscCall(PetscTestDirectory(dir, 'r', &flg));
162     PetscCheck(!flg, PETSC_COMM_SELF, PETSC_ERR_FILE_UNEXPECTED, "Cannot access directory to delete: %s", dir);
163     PetscCall(PetscTestFile(dir, 'r', &flg));
164     PetscCheck(!flg, PETSC_COMM_SELF, PETSC_ERR_FILE_UNEXPECTED, "Specified path is a file - not a dir: %s", dir);
165     PetscFunctionReturn(PETSC_SUCCESS); /* perhaps the dir was not yet created */
166   }
167   while ((data = readdir(dirp))) {
168     PetscCall(PetscStrcmp(data->d_name, ".", &flg1));
169     PetscCall(PetscStrcmp(data->d_name, "..", &flg2));
170     if (flg1 || flg2) continue;
171     PetscCall(PetscPathJoin(dir, data->d_name, PETSC_MAX_PATH_LEN, loc));
172     PetscCheck(lstat(loc, &statbuf) >= 0, PETSC_COMM_SELF, PETSC_ERR_FILE_UNEXPECTED, "cannot run lstat() on: %s", loc);
173     if (S_ISDIR(statbuf.st_mode)) {
174       PetscCall(PetscRMTree(loc));
175     } else {
176       PetscCheck(!unlink(loc), PETSC_COMM_SELF, PETSC_ERR_FILE_UNEXPECTED, "Could not delete file: %s", loc);
177     }
178   }
179   closedir(dirp);
180   PetscCheck(!rmdir(dir), PETSC_COMM_SELF, PETSC_ERR_FILE_UNEXPECTED, "Could not delete dir: %s", dir);
181   PetscFunctionReturn(PETSC_SUCCESS);
182 }
183 #endif
184