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 12 13 #undef __FUNCT__ 14 #define __FUNCT__ "PetscPathJoin" 15 PetscErrorCode PetscPathJoin(const char dname[],const char fname[],size_t n,char fullname[]) 16 { 17 PetscErrorCode ierr; 18 size_t l1,l2; 19 PetscFunctionBegin; 20 ierr = PetscStrlen(dname,&l1);CHKERRQ(ierr); 21 ierr = PetscStrlen(fname,&l2);CHKERRQ(ierr); 22 if ((l1+l2+2)>n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Path length is greater than buffer size"); 23 ierr = PetscStrcpy(fullname,dname);CHKERRQ(ierr); 24 ierr = PetscStrcat(fullname,"/");CHKERRQ(ierr); 25 ierr = PetscStrcat(fullname,fname);CHKERRQ(ierr); 26 PetscFunctionReturn(0); 27 } 28 29 #undef __FUNCT__ 30 #define __FUNCT__ "PetscMkdir" 31 PetscErrorCode PetscMkdir(const char dir[]) 32 { 33 int err; 34 PetscFunctionBegin; 35 #if defined(PETSC_HAVE__MKDIR) && defined(PETSC_HAVE_DIRECT_H) 36 err = _mkdir(dir); 37 #else 38 err = mkdir(dir,S_IRWXU|S_IRGRP|S_IXGRP); 39 #endif 40 if(err) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Could not create dir: %s",dir); 41 PetscFunctionReturn(0); 42 } 43 44 #undef __FUNCT__ 45 #define __FUNCT__ "PetscRMTree" 46 #if defined(PETSC_HAVE_DIRECT_H) 47 PetscErrorCode PetscRMTree(const char dir[]) 48 { 49 PetscErrorCode ierr; 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 58 59 PetscFunctionBegin; 60 ierr = PetscPathJoin(dir,"*",PETSC_MAX_PATH_LEN,loc);CHKERRQ(ierr); 61 handle = _findfirst(loc, &data); 62 if(handle == -1) { 63 PetscBool flg; 64 ierr = PetscTestDirectory(loc,'r',&flg);CHKERRQ(ierr); 65 if (flg) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Cannot access directory to delete: %s",dir); 66 ierr = PetscTestFile(loc,'r',&flg);CHKERRQ(ierr); 67 if (flg) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Specified path is a file - not a dir: %s",dir); 68 PetscFunctionReturn(0); /* perhaps the dir was not yet created */ 69 } 70 while(_findnext(handle, &data) != -1) { 71 ierr = PetscStrcmp(data.name, ".",&flg1);CHKERRQ(ierr); 72 ierr = PetscStrcmp(data.name, "..",&flg2);CHKERRQ(ierr); 73 if (flg1 || flg2) continue; 74 ierr = PetscPathJoin(dir,data.name,PETSC_MAX_PATH_LEN,loc);CHKERRQ(ierr); 75 if(data.attrib & _A_SUBDIR) { 76 ierr = PetscRMTree(loc);CHKERRQ(ierr); 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 PetscFunctionReturn(0); 84 } 85 #else 86 #include <dirent.h> 87 #include <unistd.h> 88 PetscErrorCode PetscRMTree(const char dir[]) 89 { 90 PetscErrorCode ierr; 91 struct dirent *data; 92 char loc[PETSC_MAX_PATH_LEN]; 93 PetscBool flg1, flg2; 94 DIR *dirp; 95 struct stat statbuf; 96 97 PetscFunctionBegin; 98 dirp = opendir(dir); 99 if(!dirp) { 100 PetscBool flg; 101 ierr = PetscTestDirectory(dir,'r',&flg);CHKERRQ(ierr); 102 if (flg) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Cannot access directory to delete: %s",dir); 103 ierr = PetscTestFile(dir,'r',&flg);CHKERRQ(ierr); 104 if (flg) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Specified path is a file - not a dir: %s",dir); 105 PetscFunctionReturn(0); /* perhaps the dir was not yet created */ 106 } 107 while((data = readdir(dirp))) { 108 ierr = PetscStrcmp(data->d_name, ".",&flg1);CHKERRQ(ierr); 109 ierr = PetscStrcmp(data->d_name, "..",&flg2);CHKERRQ(ierr); 110 if (flg1 || flg2) continue; 111 ierr = PetscPathJoin(dir,data->d_name,PETSC_MAX_PATH_LEN,loc);CHKERRQ(ierr); 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 ierr = PetscRMTree(loc);CHKERRQ(ierr); 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 PetscFunctionReturn(0); 122 } 123 #endif 124