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