/*@C PetscGetFileFromPath - Finds a file from a name and a path string. A default can be provided. Not Collective Input Parameters: + path - A string containing "directory:directory:..." (without the quotes, of course). As a special case, if the name is a single FILE, that file is used. . defname - default name . name - file name to use with the directories from env - mode - file mode desired (usually r for readable, w for writable, or e for executable) Output Parameter: . fname - qualified file name Level: developer Developer Notes: Wrongly returns 1 as an error code sometimes. Maybe should have additional flag argument indicating if it found it. Most arguments likely should be const. Concepts: files^finding in path Concepts: path^searching for file @*/ PetscErrorCode PetscGetFileFromPath(char *path,char *defname,char *name,char *fname,char mode) { char *p,*cdir,trial[PETSC_MAX_PATH_LEN],*senv,*env; size_t ln; PetscErrorCode ierr; PetscBool flg; PetscFunctionBegin; /* Setup default */ ierr = PetscGetFullPath(defname,fname,PETSC_MAX_PATH_LEN);CHKERRQ(ierr); if (path) { /* Check to see if the path is a valid regular FILE */ ierr = PetscTestFile(path,mode,&flg);CHKERRQ(ierr); if (flg) { ierr = PetscStrcpy(fname,path);CHKERRQ(ierr); PetscFunctionReturn(1); } /* Make a local copy of path and mangle it */ ierr = PetscStrallocpy(path,&senv);CHKERRQ(ierr); env = senv; while (env) { /* Find next directory in env */ cdir = env; ierr = PetscStrchr(env,PETSC_PATH_SEPARATOR,&p);CHKERRQ(ierr); if (p) { *p = 0; env = p + 1; } else env = 0; /* Form trial file name */ ierr = PetscStrcpy(trial,cdir);CHKERRQ(ierr); ierr = PetscStrlen(trial,&ln);CHKERRQ(ierr); if (trial[ln-1] != '/') trial[ln++] = '/'; ierr = PetscStrcpy(trial + ln,name);CHKERRQ(ierr); ierr = PetscTestFile(path,mode,&flg);CHKERRQ(ierr); if (flg) { /* need PetscGetFullPath rather then copy in case path has . in it */ ierr = PetscGetFullPath(trial,fname,PETSC_MAX_PATH_LEN);CHKERRQ(ierr); ierr = PetscFree(senv);CHKERRQ(ierr); PetscFunctionReturn(1); } } ierr = PetscFree(senv);CHKERRQ(ierr); } ierr = PetscTestFile(path,mode,&flg);CHKERRQ(ierr); if (flg) PetscFunctionReturn(1); PetscFunctionReturn(0); }
/*@C PetscSSLInitializeContext - Set up an SSL context suitable for initiating HTTPS requests. Output Parameter: . octx - the SSL_CTX to be passed to PetscHTTPSConnect Level: advanced If PETSc was ./configure -with-ssl-certificate requires the user have created a self-signed certificate with $ saws/CA.pl -newcert (using the passphrase of password) $ cat newkey.pem newcert.pem > sslclient.pem and put the resulting file in either the current directory (with the application) or in the home directory. This seems kind of silly but it was all I could figure out. .seealso: PetscSSLDestroyContext(), PetscHTTPSConnect(), PetscHTTPSRequest() @*/ PetscErrorCode PetscSSLInitializeContext(SSL_CTX **octx) { SSL_CTX *ctx; #if defined(PETSC_USE_SSL_CERTIFICATE) char keyfile[PETSC_MAX_PATH_LEN]; PetscBool exists; PetscErrorCode ierr; #endif PetscFunctionBegin; if (!bio_err){ SSL_library_init(); SSL_load_error_strings(); bio_err = BIO_new_fp(stderr,BIO_NOCLOSE); } /* Set up a SIGPIPE handler */ signal(SIGPIPE,sigpipe_handle); /* suggested at https://mta.openssl.org/pipermail/openssl-dev/2015-May/001449.html */ #if (OPENSSL_VERSION_NUMBER >= 0x10100000L) ctx = SSL_CTX_new(TLS_client_method()); #else ctx = SSL_CTX_new(SSLv23_client_method()); #endif SSL_CTX_set_mode(ctx,SSL_MODE_AUTO_RETRY); #if defined(PETSC_USE_SSL_CERTIFICATE) /* Locate keyfile */ ierr = PetscStrcpy(keyfile,"sslclient.pem");CHKERRQ(ierr); ierr = PetscTestFile(keyfile,'r',&exists);CHKERRQ(ierr); if (!exists) { ierr = PetscGetHomeDirectory(keyfile,PETSC_MAX_PATH_LEN);CHKERRQ(ierr); ierr = PetscStrcat(keyfile,"/");CHKERRQ(ierr); ierr = PetscStrcat(keyfile,"sslclient.pem");CHKERRQ(ierr); ierr = PetscTestFile(keyfile,'r',&exists);CHKERRQ(ierr); if (!exists) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to locate sslclient.pem file in current directory or home directory"); } /* Load our keys and certificates*/ if (!(SSL_CTX_use_certificate_chain_file(ctx,keyfile))) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot read certificate file"); SSL_CTX_set_default_passwd_cb(ctx,password_cb); if (!(SSL_CTX_use_PrivateKey_file(ctx,keyfile,SSL_FILETYPE_PEM))) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot read key file"); #endif *octx = ctx; PetscFunctionReturn(0); }
PetscErrorCode PetscRMTree(const char dir[]) { PetscErrorCode ierr; struct dirent *data; char loc[PETSC_MAX_PATH_LEN]; PetscBool flg1, flg2; DIR *dirp; struct stat statbuf; PetscFunctionBegin; dirp = opendir(dir); if(!dirp) { PetscBool flg; ierr = PetscTestDirectory(dir,'r',&flg);CHKERRQ(ierr); if (flg) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Cannot access directory to delete: %s",dir); ierr = PetscTestFile(dir,'r',&flg);CHKERRQ(ierr); if (flg) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Specified path is a file - not a dir: %s",dir); PetscFunctionReturn(0); /* perhaps the dir was not yet created */ } while((data = readdir(dirp))) { ierr = PetscStrcmp(data->d_name, ".",&flg1);CHKERRQ(ierr); ierr = PetscStrcmp(data->d_name, "..",&flg2);CHKERRQ(ierr); if (flg1 || flg2) continue; ierr = PetscPathJoin(dir,data->d_name,PETSC_MAX_PATH_LEN,loc);CHKERRQ(ierr); if (lstat(loc,&statbuf) <0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"cannot run lstat() on: %s",loc); if (S_ISDIR(statbuf.st_mode)) { ierr = PetscRMTree(loc);CHKERRQ(ierr); } else { if (unlink(loc)) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Could not delete file: %s",loc); } } closedir(dirp); if (rmdir(dir)) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Could not delete dir: %s",dir); PetscFunctionReturn(0); }
static PetscErrorCode PetscCheckDebugger_Private(const char defaultDbg[], const char string[], const char *debugger[]) { PetscBool exists; char *f; PetscErrorCode ierr; PetscFunctionBegin; ierr = PetscStrstr(string, defaultDbg, &f);CHKERRQ(ierr); if (f) { ierr = PetscTestFile(string, 'x', &exists);CHKERRQ(ierr); if (exists) *debugger = string; else *debugger = defaultDbg; } PetscFunctionReturn(0); }
/* Box can only return an authorization code to a Webserver, hence we need to start one up and wait for the authorization code to arrive from Box */ static PetscErrorCode PetscBoxStartWebServer_Private(void) { PetscErrorCode ierr; int optionsLen = 5; const char *options[optionsLen]; struct mg_callbacks callbacks; struct mg_context *ctx; char keyfile[PETSC_MAX_PATH_LEN]; PetscBool exists; PetscFunctionBegin; options[0] = "listening_ports"; options[1] = "8081s"; ierr = PetscStrcpy(keyfile,"sslclient.pem");CHKERRQ(ierr); ierr = PetscTestFile(keyfile,'r',&exists);CHKERRQ(ierr); if (!exists) { ierr = PetscGetHomeDirectory(keyfile,PETSC_MAX_PATH_LEN);CHKERRQ(ierr); ierr = PetscStrcat(keyfile,"/");CHKERRQ(ierr); ierr = PetscStrcat(keyfile,"sslclient.pem");CHKERRQ(ierr); ierr = PetscTestFile(keyfile,'r',&exists);CHKERRQ(ierr); if (!exists) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to locate sslclient.pem file in current directory or home directory"); } options[2] = "ssl_certificate"; options[3] = keyfile; options[4] = NULL; /* Prepare callbacks structure. We have only one callback, the rest are NULL. */ ierr = PetscMemzero(&callbacks, sizeof(callbacks));CHKERRQ(ierr); callbacks.begin_request = PetscBoxWebServer_Private; ctx = mg_start(&callbacks, NULL, options); if (!ctx) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Unable to start up webserver"); while (!result) {}; PetscFunctionReturn(0); }
PetscErrorCode PetscRMTree(const char dir[]) { PetscErrorCode ierr; struct _finddata_t data; char loc[PETSC_MAX_PATH_LEN]; PetscBool flg1, flg2; #if defined (PETSC_HAVE_STDINT_H) intptr_t handle; #else long handle; #endif PetscFunctionBegin; ierr = PetscPathJoin(dir,"*",PETSC_MAX_PATH_LEN,loc);CHKERRQ(ierr); handle = _findfirst(loc, &data); if(handle == -1) { PetscBool flg; ierr = PetscTestDirectory(loc,'r',&flg);CHKERRQ(ierr); if (flg) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Cannot access directory to delete: %s",dir); ierr = PetscTestFile(loc,'r',&flg);CHKERRQ(ierr); if (flg) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Specified path is a file - not a dir: %s",dir); PetscFunctionReturn(0); /* perhaps the dir was not yet created */ } while(_findnext(handle, &data) != -1) { ierr = PetscStrcmp(data.name, ".",&flg1);CHKERRQ(ierr); ierr = PetscStrcmp(data.name, "..",&flg2);CHKERRQ(ierr); if (flg1 || flg2) continue; ierr = PetscPathJoin(dir,data.name,PETSC_MAX_PATH_LEN,loc);CHKERRQ(ierr); if(data.attrib & _A_SUBDIR) { ierr = PetscRMTree(loc);CHKERRQ(ierr); } else{ if (remove(loc)) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Could not delete file: %s",loc); } } _findclose(handle); if (_rmdir(dir)) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Could not delete dir: %s",dir); PetscFunctionReturn(0); }
/*@C PetscFileRetrieve - Obtains a library from a URL or compressed and copies into local disk space as uncompressed. Collective on MPI_Comm Input Parameter: + comm - processors accessing the library . libname - name of library, including entire URL (with or without .gz) - llen - length of llibname Output Parameter: + llibname - name of local copy of library - found - if found and retrieved the file Level: developer @*/ PetscErrorCode PetscFileRetrieve(MPI_Comm comm,const char libname[],char llibname[],size_t llen,PetscBool *found) { char buf[1024],tmpdir[PETSC_MAX_PATH_LEN],urlget[PETSC_MAX_PATH_LEN],*par; const char *pdir; FILE *fp; PetscErrorCode ierr; int i; PetscMPIInt rank; size_t len = 0; PetscBool flg1,flg2,flg3,sharedtmp,exists; #if defined(PETSC_HAVE_POPEN) int rval; #endif PetscFunctionBegin; *found = PETSC_FALSE; /* if file does not have an ftp:// or http:// or .gz then need not process file */ ierr = PetscStrstr(libname,".gz",&par);CHKERRQ(ierr); if (par) {ierr = PetscStrlen(par,&len);CHKERRQ(ierr);} ierr = PetscStrncmp(libname,"ftp://",6,&flg1);CHKERRQ(ierr); ierr = PetscStrncmp(libname,"http://",7,&flg2);CHKERRQ(ierr); ierr = PetscStrncmp(libname,"file://",7,&flg3);CHKERRQ(ierr); if (!flg1 && !flg2 && !flg3 && (!par || len != 3)) { ierr = PetscStrncpy(llibname,libname,llen);CHKERRQ(ierr); ierr = PetscTestFile(libname,'r',found);CHKERRQ(ierr); if (*found) { ierr = PetscInfo1(NULL,"Found file %s\n",libname);CHKERRQ(ierr); } else { ierr = PetscInfo1(NULL,"Did not find file %s\n",libname);CHKERRQ(ierr); } PetscFunctionReturn(0); } if (par && len == 3){ size_t llen; ierr = PetscStrlen(libname,&llen);CHKERRQ(ierr); ierr = PetscStrncpy(llibname,libname,llen);CHKERRQ(ierr); llibname[llen-len] = 0; ierr = PetscTestFile(llibname,'r',found);CHKERRQ(ierr); if (*found) { ierr = PetscInfo1(NULL,"Found uncompressed version of file %s\n",llibname);CHKERRQ(ierr); PetscFunctionReturn(0); } else { ierr = PetscInfo1(NULL,"Did not find uncompressed version of file %s\n",libname);CHKERRQ(ierr); } } /* Determine if all processors share a common /tmp */ ierr = PetscSharedTmp(comm,&sharedtmp);CHKERRQ(ierr); ierr = PetscOptionsGetenv(comm,"PETSC_TMP",tmpdir,PETSC_MAX_PATH_LEN,&flg1);CHKERRQ(ierr); ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); if (!rank || !sharedtmp) { /* Construct the script to get URL file */ ierr = PetscGetPetscDir(&pdir);CHKERRQ(ierr); ierr = PetscStrcpy(urlget,pdir);CHKERRQ(ierr); ierr = PetscStrcat(urlget,"/bin/urlget");CHKERRQ(ierr); ierr = PetscTestFile(urlget,'r',&exists);CHKERRQ(ierr); if (!exists) { ierr = PetscTestFile("urlget",'r',&exists);CHKERRQ(ierr); if (!exists) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Cannot locate PETSc script urlget in %s or current directory",urlget); ierr = PetscStrcpy(urlget,"urlget");CHKERRQ(ierr); } ierr = PetscStrcat(urlget," ");CHKERRQ(ierr); /* are we using an alternative /tmp? */ if (flg1) { ierr = PetscStrcat(urlget,"-tmp ");CHKERRQ(ierr); ierr = PetscStrcat(urlget,tmpdir);CHKERRQ(ierr); ierr = PetscStrcat(urlget," ");CHKERRQ(ierr); } ierr = PetscStrcat(urlget,libname);CHKERRQ(ierr); ierr = PetscStrcat(urlget," 2>&1 ");CHKERRQ(ierr); #if defined(PETSC_HAVE_POPEN) ierr = PetscPOpen(PETSC_COMM_SELF,NULL,urlget,"r",&fp);CHKERRQ(ierr); #else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP_SYS,"Cannot run external programs on this machine"); #endif if (!fgets(buf,1024,fp)) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"No output from ${PETSC_DIR}/bin/urlget in getting file %s",libname); ierr = PetscInfo1(0,"Message back from urlget: %s\n",buf);CHKERRQ(ierr); ierr = PetscStrncmp(buf,"Error",5,&flg1);CHKERRQ(ierr); ierr = PetscStrncmp(buf,"Traceback",9,&flg2);CHKERRQ(ierr); #if defined(PETSC_HAVE_POPEN) ierr = PetscPClose(PETSC_COMM_SELF,fp,&rval);CHKERRQ(ierr); #endif if (flg1 || flg2) *found = PETSC_FALSE; else { *found = PETSC_TRUE; /* Check for \n and make it 0 */ for (i=0; i<1024; i++) { if (buf[i] == '\n') { buf[i] = 0; break; } } ierr = PetscStrncpy(llibname,buf,llen);CHKERRQ(ierr); } } if (sharedtmp) { /* send library name to all processors */ ierr = MPI_Bcast(found,1,MPIU_BOOL,0,comm);CHKERRQ(ierr); if (*found) { ierr = MPI_Bcast(llibname,llen,MPI_CHAR,0,comm);CHKERRQ(ierr); ierr = MPI_Bcast(found,1,MPIU_BOOL,0,comm);CHKERRQ(ierr); } } PetscFunctionReturn(0); }