コード例 #1
0
ファイル: ocinternal.c プロジェクト: ArtisticCoding/libmesh
static OCerror
createtempfile(OCstate* state, OCtree* tree)
{
    int stat = OC_NOERR;
    int fd = 0;
    char* name = NULL;

    stat = ocmktmp(TMPPATH1,&name, &fd);
    if(stat != OC_NOERR)
        stat = ocmktmp(TMPPATH2,&name,&fd);
    if(stat != OC_NOERR) goto fail;
#ifdef OCDEBUG
    oclog(OCLOGNOTE,"oc_open: using tmp file: %s",name);
#endif
    tree->data.filename = name; /* remember our tmp file name */
    tree->data.file = fdopen(fd,"w+");
    if(tree->data.file == NULL) return OC_EOPEN;
    /* unlink the temp file so it will automatically be reclaimed */
    if(ocdebug == 0) unlink(tree->data.filename);
    return stat;

fail:
    oclog(OCLOGERR,"oc_open: attempt to create tmp file failed: %s",name == NULL ? "[NULL]" : name);

    if(name != NULL) {free(name);name=NULL;}
    return stat;
}
コード例 #2
0
ファイル: ocinternal.c プロジェクト: MoonDav/netcdf-c
static OCerror
createtempfile(OCstate* state, OCtree* tree)
{
    int stat = OC_NOERR;
    char* path = NULL;
    char* name = NULL;
    int len;

    len =
	  strlen(ocglobalstate.tempdir)
	  + 1 /* '/' */
	  + strlen(DATADDSFILE);
    path = (char*)malloc(len+1);
    if(path == NULL) return OC_ENOMEM;
    occopycat(path,len,3,ocglobalstate.tempdir,"/",DATADDSFILE);
    stat = ocmktmp(path,&name);
    free(path);
    if(stat != OC_NOERR) goto fail;
#ifdef OCDEBUG
    oclog(OCLOGNOTE,"oc_open: creating tmp file: %s",name);
#endif
    tree->data.filename = name; /* remember our tmp file name */
    name = NULL;
    tree->data.file = fopen(tree->data.filename,"w+");
    if(tree->data.file == NULL) return OC_EOPEN;
    /* unlink the temp file so it will automatically be reclaimed */
    if(ocdebug == 0) unlink(tree->data.filename);
    return stat;

fail:
    if(name != NULL) {
        oclog(OCLOGERR,"oc_open: attempt to create tmp file failed: %s",name);
	free(name);
    } else {
        oclog(OCLOGERR,"oc_open: attempt to create tmp file failed: NULL");
    }
    return OCTHROW(stat);
}
コード例 #3
0
ファイル: ocinternal.c プロジェクト: MoonDav/netcdf-c
/*
    Set curl properties for link based on rc files etc.
*/
static OCerror
ocset_curlproperties(OCstate* state)
{
    OCerror stat = OC_NOERR;

    /* extract the relevant triples int state */
    ocrc_process(state);

    if(state->curlflags.useragent == NULL) {
        size_t len = strlen(DFALTUSERAGENT) + strlen(VERSION) + 1;
	char* agent = (char*)malloc(len+1);
	if(occopycat(agent,len,2,DFALTUSERAGENT,VERSION))
	    state->curlflags.useragent = agent;
	else
	    free(agent);
    }

    /* Some servers (e.g. thredds and columbia) appear to require a place
       to put cookies in order for some security functions to work
    */
    if(state->curlflags.cookiejar != NULL
       && strlen(state->curlflags.cookiejar) == 0) {
	free(state->curlflags.cookiejar);
	state->curlflags.cookiejar = NULL;
    }

    if(state->curlflags.cookiejar == NULL) {
	/* If no cookie file was defined, define a default */
	char tmp[OCPATHMAX+1];
        int stat;
	pid_t pid = getpid();
	snprintf(tmp,sizeof(tmp)-1,"%s/%s.%ld/",ocglobalstate.tempdir,OCDIR,(long)pid);
#ifdef _MSC_VER
	stat = mkdir(tmp);
#else
	stat = mkdir(tmp,S_IRUSR | S_IWUSR | S_IXUSR);
#endif
	if(stat != 0 && errno != EEXIST) {
	    fprintf(stderr,"Cannot create cookie directory\n");
	    goto fail;
	}
	errno = 0;
	/* Create the unique cookie file name */
	stat = ocmktmp(tmp,&state->curlflags.cookiejar);
	state->curlflags.createdflags |= COOKIECREATED;
	if(stat != OC_NOERR && errno != EEXIST) {
	    fprintf(stderr,"Cannot create cookie file\n");
	    goto fail;
	}
	errno = 0;
    }
    OCASSERT(state->curlflags.cookiejar != NULL);

    /* Make sure the cookie jar exists and can be read and written */
    {
	FILE* f = NULL;
	char* fname = state->curlflags.cookiejar;
	/* See if the file exists already */
        f = fopen(fname,"r");
	if(f == NULL) {
	    /* Ok, create it */
	    f = fopen(fname,"w+");
	    if(f == NULL) {
	        fprintf(stderr,"Cookie file cannot be read and written: %s\n",fname);
	        {stat = OC_EPERM; goto fail;}
	    }
	} else { /* test if file can be written */
	    fclose(f);
	    f = fopen(fname,"r+");
	    if(f == NULL) {
	        fprintf(stderr,"Cookie file is cannot be written: %s\n",fname);
	        {stat = OC_EPERM; goto fail;}
	    }
	}
	if(f != NULL) fclose(f);
    }

#if 0
    /* Create a netrc file if specified  and required,
       where required => >1 NETRC triples exist */
    if(ocrc_netrc_required(state)) {
	/* WARNING: it appears that a user+pwd was specified specifically, then
           the netrc file will be completely disabled. */
	if(state->creds.userpwd != NULL) {
  	    oclog(OCLOGWARN,"The rc file specifies both netrc and user+pwd; this will cause curl to ignore the netrc file");
	}
	stat = oc_build_netrc(state);
    }
#endif

    return stat;

fail:
    return OCTHROW(stat);
}
コード例 #4
0
ファイル: ocinternal.c プロジェクト: ArtisticCoding/libmesh
/*
    Set curl properties for link based on rc files etc.
*/
static int
ocsetcurlproperties(OCstate* state)
{
    CURLcode cstat = CURLE_OK;

    /* process the triple store wrt to this state */
    if(ocdodsrc_process(state) != OC_NOERR) {
	oclog(OCLOGERR,"Malformed .opendaprc configuration file");
	goto fail;
    }
    if(state->creds.username == NULL && state->creds.password == NULL) {
        if(state->uri->user != NULL && state->uri->password != NULL) {
	    /* this overrides .dodsrc */
            if(state->creds.password) free(state->creds.password);
            state->creds.password = nulldup(state->uri->password);
            if(state->creds.username) free(state->creds.username);
            state->creds.username = nulldup(state->uri->user);
	}
    }
    if(state->curlflags.useragent == NULL) {
        size_t len = strlen(DFALTUSERAGENT) + strlen(VERSION) + 1;
	char* agent = (char*)malloc(len+1);
	if(occopycat(agent,len,2,DFALTUSERAGENT,VERSION))
	    state->curlflags.useragent = agent;
	else
	    free(agent);
    }

    /* Some servers (e.g. thredds and columbia) appear to require a place
       to put cookies in order for some security functions to work
    */
    if(state->curlflags.cookiejar == NULL
       || *state->curlflags.cookiejar) {
#if 1
	/* Apparently anything non-null will work */
	state->curlflags.cookiejar = strdup("");
#else
	/* If no cookie file was defined, define a default */
	char* tmp;
	int fd;
        int stat;

        tmp = (char*)malloc(strlen(ocglobalstate.home)
				  +strlen("/")
				  +strlen(OCDIR)
				  +strlen("/")
				  +1);
	if(tmp == NULL)
	    return OC_ENOMEM;
	strcpy(tmp,ocglobalstate.home);
	strcat(tmp,"/");
	strcat(tmp,OCDIR);
	strcat(tmp,"/");
	stat = mkdir(tmp,S_IRUSR | S_IWUSR | S_IXUSR);
	if(stat != 0 && errno != EEXIST) {
	    fprintf(stderr,"Cannot create cookie file\n");
	    return stat;
	}
	errno = 0;
	/* Create the actual cookie file */
	stat = ocmktmp(tmp,&state->curlflags.cookiejar,&fd);
	close(fd);

#if 0
	fd = creat(tmp,S_IRUSR | S_IWUSR);
	if(fd < 0) {
	    fprintf(stderr,"Cannot create cookie file\n");
	    return OC_EPERM;
	}else
	    close(fd);
#endif
#endif
    }
    return OC_NOERR;

fail:
    if(cstat != CURLE_OK)
	oclog(OCLOGERR, "curl error: %s", curl_easy_strerror(cstat));
    return OC_ECURL;
}