Actual source code: fdir.c

  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

 17: static PetscErrorCode PetscPathJoin(const char dname[], const char fname[], size_t n, char fullname[])
 18: {
 19:   size_t l1, l2;

 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: }

 31: /*@
 32:   PetscMkdir - Create a directory

 34:   Not Collective

 36:   Input Parameter:
 37: . dir - the directory name

 39:   Level: advanced

 41: .seealso: `PetscMkdtemp()`, `PetscRMTree()`
 42: @*/
 43: PetscErrorCode PetscMkdir(const char dir[])
 44: {
 45:   int       err;
 46:   PetscBool flg;

 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: }

 60: /*@
 61:   PetscMkdtemp - Create a directory with a unique name given a name template.

 63:   Input Parameter:
 64: . dir - file name template, the last six characters must be 'XXXXXX', and they will be modified upon return

 66:   Level: advanced

 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;

 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: }

 98: /*@C
 99:   PetscRMTree - Recursively delete a directory tree on the current MPI process

101:   Not Collective

103:   Input Parameter:
104: . dir - path of the directory to delete

106:   Level: developer

108:   Note:
109:   All files and subdirectories rooted at `dir`, along with `dir` itself, are removed. Returns successfully if `dir` does not exist or is empty.

111: .seealso: `PetscMkdir()`, `PetscTestDirectory()`
112: @*/
113: #if defined(PETSC_HAVE_DIRECT_H)
114: PetscErrorCode PetscRMTree(const char dir[])
115: {
116:   struct _finddata_t data;
117:   char               loc[PETSC_MAX_PATH_LEN];
118:   PetscBool          flg1, flg2;
119:   #if defined(PETSC_HAVE_STDINT_H)
120:   intptr_t handle;
121:   #else
122:   long handle;
123:   #endif

125:   PetscFunctionBegin;
126:   PetscCall(PetscPathJoin(dir, "*", PETSC_MAX_PATH_LEN, loc));
127:   handle = _findfirst(loc, &data);
128:   if (handle == -1) {
129:     PetscBool flg;
130:     PetscCall(PetscTestDirectory(loc, 'r', &flg));
131:     PetscCheck(!flg, PETSC_COMM_SELF, PETSC_ERR_FILE_UNEXPECTED, "Cannot access directory to delete: %s", dir);
132:     PetscCall(PetscTestFile(loc, 'r', &flg));
133:     PetscCheck(!flg, PETSC_COMM_SELF, PETSC_ERR_FILE_UNEXPECTED, "Specified path is a file - not a dir: %s", dir);
134:     PetscFunctionReturn(PETSC_SUCCESS); /* perhaps the dir was not yet created */
135:   }
136:   while (_findnext(handle, &data) != -1) {
137:     PetscCall(PetscStrcmp(data.name, ".", &flg1));
138:     PetscCall(PetscStrcmp(data.name, "..", &flg2));
139:     if (flg1 || flg2) continue;
140:     PetscCall(PetscPathJoin(dir, data.name, PETSC_MAX_PATH_LEN, loc));
141:     if (data.attrib & _A_SUBDIR) {
142:       PetscCall(PetscRMTree(loc));
143:     } else {
144:       PetscCheck(!remove(loc), PETSC_COMM_SELF, PETSC_ERR_FILE_UNEXPECTED, "Could not delete file: %s", loc);
145:     }
146:   }
147:   _findclose(handle);
148:   PetscCheck(!_rmdir(dir), PETSC_COMM_SELF, PETSC_ERR_FILE_UNEXPECTED, "Could not delete dir: %s", dir);
149:   PetscFunctionReturn(PETSC_SUCCESS);
150: }
151: #else
152:   #include <dirent.h>
153:   #include <unistd.h>
154:   #include <errno.h>

156: PetscErrorCode PetscRMTree(const char dir[])
157: {
158:   struct dirent *data;
159:   char           loc[PETSC_MAX_PATH_LEN];
160:   PetscBool      flg1, flg2;
161:   DIR           *dirp;
162:   struct stat    statbuf;

164:   PetscFunctionBegin;
165:   dirp = opendir(dir);
166:   if (!dirp) {
167:     PetscBool flg;
168:     PetscCall(PetscTestDirectory(dir, 'r', &flg));
169:     PetscCheck(!flg, PETSC_COMM_SELF, PETSC_ERR_FILE_UNEXPECTED, "Cannot access directory to delete: %s", dir);
170:     PetscCall(PetscTestFile(dir, 'r', &flg));
171:     PetscCheck(!flg, PETSC_COMM_SELF, PETSC_ERR_FILE_UNEXPECTED, "Specified path is a file - not a dir: %s", dir);
172:     PetscFunctionReturn(PETSC_SUCCESS); /* perhaps the dir was not yet created */
173:   }
174:   while ((data = readdir(dirp))) {
175:     PetscCall(PetscStrcmp(data->d_name, ".", &flg1));
176:     PetscCall(PetscStrcmp(data->d_name, "..", &flg2));
177:     if (flg1 || flg2) continue;
178:     PetscCall(PetscPathJoin(dir, data->d_name, PETSC_MAX_PATH_LEN, loc));
179:     PetscCheck(lstat(loc, &statbuf) >= 0, PETSC_COMM_SELF, PETSC_ERR_FILE_UNEXPECTED, "cannot run lstat() on: %s due to \"%s\"", loc, strerror(errno));
180:     if (S_ISDIR(statbuf.st_mode)) {
181:       PetscCall(PetscRMTree(loc));
182:     } else {
183:       PetscCheck(!unlink(loc), PETSC_COMM_SELF, PETSC_ERR_FILE_UNEXPECTED, "Could not delete file: %s due to \"%s\"", loc, strerror(errno));
184:     }
185:   }
186:   closedir(dirp);
187:   PetscCheck(!rmdir(dir), PETSC_COMM_SELF, PETSC_ERR_FILE_UNEXPECTED, "Could not delete dir: %s due to \"%s\"", dir, strerror(errno));
188:   PetscFunctionReturn(PETSC_SUCCESS);
189: }
190: #endif