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