Date: 2016-07-24T16:47:32+00:00

Actual source code: fdir.c
petsc-3.7.3 2016-07-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

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

     31: PetscErrorCode PetscMkdir(const char dir[])
     32: {
     33:   int err;
     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:      return(0);
     42: }

     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