Actual source code: fdir.c

petsc-3.8.4 2018-03-24
Report Typos and Errors
  1:  #include <petscsys.h>
  2: #include <sys/stat.h>
  3: #if defined(PETSC_HAVE_DIRECT_H)
  4: #include <direct.h>
  5: #endif
  6: #if defined(PETSC_HAVE_IO_H)
  7: #include <io.h>
  8: #endif
  9: #if defined (PETSC_HAVE_STDINT_H)
 10: #include <stdint.h>
 11: #endif

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

 27: PetscErrorCode PetscMkdir(const char dir[])
 28: {
 29:   int err;
 31: #if defined(PETSC_HAVE__MKDIR) && defined(PETSC_HAVE_DIRECT_H)
 32:   err = _mkdir(dir);
 33: #else
 34:   err = mkdir(dir,S_IRWXU|S_IRGRP|S_IXGRP);
 35: #endif
 36:   if(err) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Could not create dir: %s",dir);
 37:   return(0);
 38: }

 40: #if defined(PETSC_HAVE_DIRECT_H)
 41: PetscErrorCode PetscRMTree(const char dir[])
 42: {
 44:   struct _finddata_t data;
 45:   char loc[PETSC_MAX_PATH_LEN];
 46:   PetscBool flg1, flg2;
 47: #if defined (PETSC_HAVE_STDINT_H)
 48:   intptr_t handle;
 49: #else
 50:   long handle;
 51:   #endif

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

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