Actual source code: fdir.c

petsc-3.9.4 2018-09-11
Report Typos and Errors
  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

 14: PetscErrorCode PetscPathJoin(const char dname[],const char fname[],size_t n,char fullname[])
 15: {
 17:   size_t         l1,l2;
 19:   PetscStrlen(dname,&l1);
 20:   PetscStrlen(fname,&l2);
 21:   if ((l1+l2+2)>n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Path length is greater than buffer size");
 22:   PetscStrncpy(fullname,dname,n);
 23:   PetscStrlcat(fullname,"/",n);
 24:   PetscStrlcat(fullname,fname,n);
 25:   return(0);
 26: }

 28: PetscErrorCode PetscMkdir(const char dir[])
 29: {
 30:   int            err;
 32:   PetscBool      flg;

 35:   PetscTestDirectory(dir,'w',&flg);
 36:   if (flg) return(0);
 37: #if defined(PETSC_HAVE__MKDIR) && defined(PETSC_HAVE_DIRECT_H)
 38:   err = _mkdir(dir);
 39: #else
 40:   err = mkdir(dir,S_IRWXU|S_IRGRP|S_IXGRP);
 41: #endif
 42:   if(err) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Could not create dir: %s",dir);
 43:   return(0);
 44: }

 46: #if defined(PETSC_HAVE_DIRECT_H)
 47: PetscErrorCode PetscRMTree(const char dir[])
 48: {
 50:   struct _finddata_t data;
 51:   char loc[PETSC_MAX_PATH_LEN];
 52:   PetscBool flg1, flg2;
 53: #if defined (PETSC_HAVE_STDINT_H)
 54:   intptr_t handle;
 55: #else
 56:   long handle;
 57:   #endif

 60:   PetscPathJoin(dir,"*",PETSC_MAX_PATH_LEN,loc);
 61:   handle = _findfirst(loc, &data);
 62:   if (handle == -1) {
 63:     PetscBool flg;
 64:     PetscTestDirectory(loc,'r',&flg);
 65:     if (flg) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Cannot access directory to delete: %s",dir);
 66:     PetscTestFile(loc,'r',&flg);
 67:     if (flg) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Specified path is a file - not a dir: %s",dir);
 68:     return(0); /* perhaps the dir was not yet created */
 69:   }
 70:   while (_findnext(handle, &data) != -1) {
 71:     PetscStrcmp(data.name, ".",&flg1);
 72:     PetscStrcmp(data.name, "..",&flg2);
 73:     if (flg1 || flg2) continue;
 74:     PetscPathJoin(dir,data.name,PETSC_MAX_PATH_LEN,loc);
 75:     if (data.attrib & _A_SUBDIR) {
 76:       PetscRMTree(loc);
 77:     } else{
 78:       if (remove(loc)) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Could not delete file: %s",loc);
 79:     }
 80:   }
 81:   _findclose(handle);
 82:   if (_rmdir(dir)) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Could not delete dir: %s",dir);
 83:   return(0);
 84: }
 85: #else
 86: #include <dirent.h>
 87: #include <unistd.h>
 88: PetscErrorCode PetscRMTree(const char dir[])
 89: {
 91:   struct dirent *data;
 92:   char loc[PETSC_MAX_PATH_LEN];
 93:   PetscBool flg1, flg2;
 94:   DIR *dirp;
 95:   struct stat statbuf;

 98:   dirp = opendir(dir);
 99:   if(!dirp) {
100:     PetscBool flg;
101:     PetscTestDirectory(dir,'r',&flg);
102:     if (flg) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Cannot access directory to delete: %s",dir);
103:     PetscTestFile(dir,'r',&flg);
104:     if (flg) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Specified path is a file - not a dir: %s",dir);
105:     return(0); /* perhaps the dir was not yet created */
106:   }
107:   while((data = readdir(dirp))) {
108:     PetscStrcmp(data->d_name, ".",&flg1);
109:     PetscStrcmp(data->d_name, "..",&flg2);
110:     if (flg1 || flg2) continue;
111:     PetscPathJoin(dir,data->d_name,PETSC_MAX_PATH_LEN,loc);
112:     if (lstat(loc,&statbuf) <0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"cannot run lstat() on: %s",loc);
113:     if (S_ISDIR(statbuf.st_mode)) {
114:       PetscRMTree(loc);
115:     } else {
116:       if (unlink(loc)) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Could not delete file: %s",loc);
117:     }
118:   }
119:   closedir(dirp);
120:   if (rmdir(dir)) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Could not delete dir: %s",dir);
121:   return(0);
122: }
123: #endif