int mkdir_deep(const char *szDirName, int secattr) { char DirName[260]; DirName[0] = 0; const char* p = szDirName; char* q = DirName; int ret = 0; while(*p) { if (('\\' == *p) || ('/' == *p)) { if (':' != *(p-1)) { ret = createdir(DirName, secattr); } } *q++ = *p++; *q = '\0'; } if (DirName[0]) { ret = createdir(DirName, secattr); } return ret; }
void ffsb_metaops(ffsb_thread_t *ft, ffsb_fs_t *fs, unsigned opnum) { struct benchfiles *bf = (struct benchfiles *)fs_get_opdata(fs, opnum); randdata_t *rd = ft_get_randdata(ft); createdir(bf, rd); createdir(bf, rd); removedir(bf, rd); renamedir(bf, rd); ft_incr_op(ft, opnum, 1, 0); }
//strcpy is safe here because filename is always limited to 256 chars int BuildPath(const char *filename) { char name[256], *next, *cur; strcpy(name, filename); cur = name; #ifdef _WIN32 if (isalpha(cur[0]) && cur[1] == ':' && cur[2] == PATH_SEPARATOR) cur += 3; #else if (*cur == PATH_SEPARATOR) cur++; #endif while ((next = strchr(cur, PATH_SEPARATOR))) { *next = '\0'; if (createdir(name) == -1 && errno != EEXIST) { perror("mkdir"); return 0; } *next = PATH_SEPARATOR; next++; cur = next; } return 1; }
const char *findfile(const char *filename, const char *mode) { static string s; if(homedir[0]) { formatstring(s)("%s%s", homedir, filename); if(fileexists(s, mode)) return s; if(mode[0]=='w' || mode[0]=='a') { string dirs; copystring(dirs, s); char *dir = strchr(dirs[0]==PATHDIV ? dirs+1 : dirs, PATHDIV); while(dir) { *dir = '\0'; if(!fileexists(dirs, "r") && !createdir(dirs)) return s; *dir = PATHDIV; dir = strchr(dir+1, PATHDIV); } return s; } } if(mode[0]=='w' || mode[0]=='a') return filename; loopv(packagedirs) { formatstring(s)("%s%s", packagedirs[i], filename); if(fileexists(s, mode)) return s; } return filename; }
int check_last_file(t_task_base *task, t_task_sub *sub) { char outfile[256] = {0x0}; get_localdir(task->hostname, task->filename, outfile); snprintf(task->tmpfile, sizeof(task->tmpfile), "%s_%ld_%ld", outfile, sub->start, sub->end); if (createdir(task->tmpfile)) { LOG(glogfd, LOG_ERROR, "dir %s create %m!\n", task->tmpfile); return LOCALFILE_DIR_E; } struct stat filestat; if (stat(task->tmpfile, &filestat)) return LOCALFILE_OK; off_t reqlen = sub->end - sub->start + 1; if (reqlen <= filestat.st_size) { task->getlen = 0; if (unlink(task->tmpfile)) { LOG(glogfd, LOG_ERROR, "unlink %s err %m!\n", task->tmpfile); return LOCALFILE_DIR_E; } return LOCALFILE_OK; } task->getlen = filestat.st_size; return LOCALFILE_OK; }
void sethomedir(const char *dir) { string tmpdir; copystring(tmpdir, dir); #ifdef WIN32 const char substitute[] = "?MYDOCUMENTS?"; if(!strncmp(dir, substitute, strlen(substitute))) { const char *regpath = "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders"; char *mydocuments = getregszvalue(HKEY_CURRENT_USER, regpath, "Personal"); if(mydocuments) { formatstring(tmpdir)("%s%s", mydocuments, dir+strlen(substitute)); delete[] mydocuments; } else { printf("failed to retrieve 'Personal' path from '%s'\n", regpath); } } #endif #ifndef STANDALONE clientlogf("Using home directory: %s", tmpdir); #endif if(fixpackagedir(tmpdir) > 0) { copystring(homedir, tmpdir); createdir(homedir); } }
int main(int argc, char **argv) { int reti; uint16_t vid; struct vol *vol; struct dir *retdir; struct path *path; /* initialize */ printf("Initializing\n============\n"); TEST(setuplog("default:note","/dev/tty")); TEST( afp_options_parse_cmdline(&obj, 3, &args[0]) ); TEST_int( afp_config_parse(&obj, NULL), 0); TEST_int( configinit(&obj), 0); TEST( cnid_init() ); TEST( load_volumes(&obj, NULL) ); TEST_int( dircache_init(8192), 0); obj.afp_version = 32; printf("\n"); /* now run tests */ printf("Running tests\n=============\n"); TEST_expr(vid = openvol(&obj, "test"), vid != 0); TEST_expr(vol = getvolbyvid(vid), vol != NULL); /* test directory.c stuff */ TEST_expr(retdir = dirlookup(vol, DIRDID_ROOT_PARENT), retdir != NULL); TEST_expr(retdir = dirlookup(vol, DIRDID_ROOT), retdir != NULL); TEST_expr(path = cname(vol, retdir, cnamewrap("Network Trash Folder")), path != NULL); TEST_expr(retdir = dirlookup(vol, DIRDID_ROOT), retdir != NULL); TEST_int(getfiledirparms(&obj, vid, DIRDID_ROOT_PARENT, "test"), 0); TEST_int(getfiledirparms(&obj, vid, DIRDID_ROOT, ""), 0); TEST_expr(reti = createdir(&obj, vid, DIRDID_ROOT, "dir1"), reti == 0 || reti == AFPERR_EXIST); TEST_int(getfiledirparms(&obj, vid, DIRDID_ROOT, "dir1"), 0); /* FIXME: this doesn't work although it should. "//" get translated to \000 \000 at means ".." ie this should getfiledirparms for DIRDID_ROOT_PARENT -- at least afair! TEST_int(getfiledirparms(&configs->obj, vid, DIRDID_ROOT, "//"), 0); */ TEST_int(createfile(&obj, vid, DIRDID_ROOT, "dir1/file1"), 0); TEST_int(delete(&obj, vid, DIRDID_ROOT, "dir1/file1"), 0); TEST_int(delete(&obj, vid, DIRDID_ROOT, "dir1"), 0); TEST_int(createfile(&obj, vid, DIRDID_ROOT, "file1"), 0); TEST_int(getfiledirparms(&obj, vid, DIRDID_ROOT, "file1"), 0); TEST_int(delete(&obj, vid, DIRDID_ROOT, "file1"), 0); /* test enumerate.c stuff */ TEST_int(enumerate(&obj, vid, DIRDID_ROOT), 0); }
bool preparedir(const char *destination) { string dir; copystring(dir, parentdir(destination)); vector<char *> dirs; while(!fileexists(dir, "r")) { dirs.add(newstring(dir)); copystring(dir, parentdir(dir)); } loopvrev(dirs) if(!createdir(dirs[i])) return false; return true; }
int open_tmp_localfile_4_write(t_task_base *task, int *fd, t_task_sub *sub) { char outfile[256] = {0x0}; get_localdir(task->hostname, task->filename, outfile); snprintf(task->tmpfile, sizeof(task->tmpfile), "%s_%d_%d", outfile, sub->idx, sub->count); if (createdir(task->tmpfile)) { LOG(glogfd, LOG_ERROR, "dir %s create %m!\n", task->tmpfile); return LOCALFILE_DIR_E; } //*fd = open(task->tmpfile, O_CREAT | O_WRONLY| O_LARGEFILE |O_APPEND, 0644); *fd = open(task->tmpfile, O_CREAT | O_WRONLY| O_LARGEFILE |O_TRUNC, 0644); if (*fd < 0) { LOG(glogfd, LOG_ERROR, "open %s err %m\n", task->tmpfile); return LOCALFILE_OPEN_E; } return LOCALFILE_OK; }
/* write header to output file -----------------------------------------------*/ static int outhead(const char *outfile, char **infile, int n, const prcopt_t *popt, const solopt_t *sopt) { FILE *fp=stdout; trace(3,"outhead: outfile=%s n=%d\n",outfile,n); if (*outfile) { createdir(outfile); if (!(fp=fopen(outfile,"w"))) { showmsg("error : open output file %s",outfile); return 0; } } /* output header */ outheader(fp,infile,n,popt,sopt); if (*outfile) fclose(fp); return 1; }
/* open output files ---------------------------------------------------------*/ static int openfile(FILE **ofp, char *files[], const char *file, const rnxopt_t *opt, nav_t *nav) { char path[1024]; int i; trace(3,"openfile:\n"); for (i=0;i<NOUTFILE;i++) { if (!*files[i]) continue; strcpy(path,files[i]); /* check overwrite input file and modify output file */ if (!strcmp(path,file)) strcat(path,"_"); /* create directory if not exist */ createdir(path); if (!(ofp[i]=fopen(path,"w"))) { showmsg("file open error: %s",path); for (i--;i>=0;i--) if (ofp[i]) fclose(ofp[i]); return 0; } /* write header to file */ switch (i) { case 0: outrnxobsh (ofp[0],opt,nav); break; case 1: outrnxnavh (ofp[1],opt,nav); break; case 2: outrnxgnavh(ofp[2],opt,nav); break; case 3: outrnxhnavh(ofp[3],opt,nav); break; case 4: outrnxqnavh(ofp[4],opt,nav); break; case 5: outrnxlnavh(ofp[5],opt,nav); break; case 6: outrnxcnavh(ofp[6],opt,nav); break; } } return 1; }
bool local::write(std::string path, std::istream &is) { createdir(dir_of(path)); std::ofstream file(path, std::ios::out | std::ios::binary | std::ios::trunc); if(file.fail()) { debugmanager()->error(path + ": open"); return false; } file << is.rdbuf(); if(file.fail()) { debugmanager()->error(path + ": write"); return false; } file.close(); if(file.fail()) { debugmanager()->error(path + ": close"); return false; } return true; }
const char *findfile(const char *filename, const char *mode) { while(filename[0] == PATHDIV) filename++; // skip leading pathdiv while(!strncmp(".." PATHDIVS, filename, 3)) filename += 3; // skip leading "../" (don't allow access to files below "AC root dir") static string s; formatstring(s)("%s%s", homedir, filename); // homedir may be "" findfilelocation = FFL_HOME; if(homedir[0] && fileexists(s, mode)) return s; if(mode[0]=='w' || mode[0]=='a') { // create missing directories, if necessary string dirs; copystring(dirs, s); char *dir = strchr(dirs[0]==PATHDIV ? dirs+1 : dirs, PATHDIV); while(dir) { *dir = '\0'; if(!fileexists(dirs, "r") && !createdir(dirs)) return s; *dir = PATHDIV; dir = strchr(dir+1, PATHDIV); } return s; } findfilelocation = FFL_ZIP; #ifndef STANDALONE formatstring(s)("zip://%s", filename); if(findzipfile(filename)) return s; #endif loopv(packagedirs) { findfilelocation++; formatstring(s)("%s%s", packagedirs[i], filename); if(fileexists(s, mode)) return s; } findfilelocation = FFL_WORKDIR; return filename; }
int getremote(char *filename, peer_t *peer, char *range, CURL *curlhandle) { #ifdef TIMEIT struct timeval start_time, end_time; unsigned long long s, e; #endif CURLcode curlcode; struct in_addr in; struct stat buf; FILE *file; useconds_t stall; char *tempfilename; char *dirfile, *basefile; char url[PATH_MAX]; char *dir; char *ptr; #ifdef TIMEIT gettimeofday(&start_time, NULL); #endif in.s_addr = peer->ip; #ifdef DEBUG logmsg("getremote: get file (%s) from (%s)\n", filename, inet_ntoa(in)); #endif status = HTTP_OK; /* * we know the file is not on the local hard disk (because getlocal() * is called before this function), so try to download the file * from a peer */ /* * first, let's see if the file systems have been formatted. if * so, then copy over all the files that were downloaded in the * first part of the installation (e.g., stage2.img, product.img) * and set up a symbolic link from the ramdisk area to the disk. */ if (stat("/mnt/sysimage", &buf) == 0) { if (stat("/mnt/sysimage/install", &buf) != 0) { if (stat("/install", &buf) == 0) { system("/usr/bin/cp -R /install /mnt/sysimage"); system("/usr/bin/rm -rf /install"); } else { /* * /install doesn't exist on the ramdisk, so * just create a directory on the hard disk */ mkdir("/mnt/sysimage/install", 0755); } symlink("/mnt/sysimage/install", "/install"); } } #ifdef TIMEIT gettimeofday(&end_time, NULL); s = (start_time.tv_sec * 1000000) + start_time.tv_usec; e = (end_time.tv_sec * 1000000) + end_time.tv_usec; logmsg("getremote:svc time1: %lld usec\n", (e - s)); #endif /* * make sure the destination directory exists */ if ((dir = strdup(filename)) != NULL) { if ((ptr = rindex(dir, '/')) != NULL) { *ptr = '\0'; if (stat(dir, &buf) != 0) { createdir(dir); } } free(dir); } #ifdef TIMEIT gettimeofday(&end_time, NULL); s = (start_time.tv_sec * 1000000) + start_time.tv_usec; e = (end_time.tv_sec * 1000000) + end_time.tv_usec; logmsg("getremote:svc time2: %lld usec\n", (e - s)); #endif if ((dirfile = strdup(filename)) == NULL) { logmsg("getremote:strdup failed:errno (%d)\n", errno); return(-1); } if ((basefile = strdup(filename)) == NULL) { logmsg("getremote:strdup failed:errno (%d)\n", errno); return(-1); } if ((tempfilename = tempnam(dirname(dirfile), basename(basefile))) == NULL) { free(dirfile); free(basefile); logmsg("getremote:tempnam():failed\n"); return(-1); } free(dirfile); free(basefile); /* * make a 'http://' url and get the file. */ if ((file = fopen(tempfilename, "w")) == NULL) { logmsg("getremote:fopen():failed\n"); free(tempfilename); return(-1); } #ifdef TIMEIT gettimeofday(&end_time, NULL); s = (start_time.tv_sec * 1000000) + start_time.tv_usec; e = (end_time.tv_sec * 1000000) + end_time.tv_usec; logmsg("getremote:svc time3: %lld usec\n", (e - s)); #endif /* * tell curl to save it to disk (save it to the file pointed * to by 'file' */ if ((curlcode = curl_easy_setopt(curlhandle, CURLOPT_WRITEDATA, file)) != CURLE_OK) { logmsg("getremote:curl_easy_setopt():failed:(%d)\n", curlcode); free(tempfilename); return(-1); } if (makeurl("http://", filename, inet_ntoa(in), url, sizeof(url)) != 0){ logmsg("getremote:makeurl():failed:(%d)", errno); free(tempfilename); return(-1); } if (fromip != NULL) { free(fromip); } fromip = strdup(inet_ntoa(in)); #ifdef TIMEIT gettimeofday(&end_time, NULL); s = (start_time.tv_sec * 1000000) + start_time.tv_usec; e = (end_time.tv_sec * 1000000) + end_time.tv_usec; logmsg("getremote:svc time4: %lld usec\n", (e - s)); #endif stall = 10000; while (stall < 1000000) { if (downloadfile(curlhandle, url, NULL) < 0) { status = HTTP_NOT_FOUND; } #ifdef DEBUG logmsg("getremote:download status %d : stall %d\n", status, stall); #endif if ((status >= HTTP_OK) && (status <= HTTP_MULTI_STATUS)) { /* * success. break out of loop */ break; } else { logmsg("getremote:downloadfile:failed:url %s\n", url); if (peer->state == DOWNLOADING) { stall *= 10; usleep(stall); } else { /* * don't return on failure here. we still need * to do some cleanup */ status = HTTP_NOT_FOUND; break; } } } fclose(file); #ifdef TIMEIT gettimeofday(&end_time, NULL); s = (start_time.tv_sec * 1000000) + start_time.tv_usec; e = (end_time.tv_sec * 1000000) + end_time.tv_usec; logmsg("getremote:svc time5: %lld usec\n", (e - s)); #endif /* * we downloaded the file from a peer, so read it and output it * to stdout */ if ((status >= HTTP_OK) && (status <= HTTP_MULTI_STATUS)) { /* * now do an atomic move */ if (rename(tempfilename, filename) < 0) { logmsg("getremote:rename():failed:(%d)\n", errno); free(tempfilename); return(-1); } if (outputfile(filename, range) != 0) { logmsg("getremote:outputfile():failed:(%d)\n", errno); free(tempfilename); return(-1); } } else { /* * on a failure, a zero-byte length file will be * left on the disk -- this is because of the fopen(). * remove this zero-length file. */ unlink(filename); unlink(tempfilename); free(tempfilename); return(-1); } #ifdef TIMEIT gettimeofday(&end_time, NULL); s = (start_time.tv_sec * 1000000) + start_time.tv_usec; e = (end_time.tv_sec * 1000000) + end_time.tv_usec; logmsg("getremote:svc time6: %lld usec\n", (e - s)); #endif free(tempfilename); return(0); }
void gen3Dfdfs(struct data *d,int mode,char *outdir,int type,int precision,int volindex) { char basename[MAXPATHLEN],dirname[MAXPATHLEN],filename[MAXPATHLEN]; int ne,ns; int start,end; int startpos,endpos,blockslices; int i; int slab,image,echo; int offline=FALSE; /* This function checks the output type requested and sets the output filename accordingly. 3D FDF data from each slice is output according to the specified mode using functions w3Dfdf() and wcomb3Dfdf(). These functions output data either from individual receivers (w3Dfdf) or using a combination of data from all receivers (wcomb3Dfdf). */ /* Check that type is valid */ if (!validtype(type) || (type == VJ)) { fprintf(stderr,"\n%s: %s()\n",__FILE__,__FUNCTION__); fprintf(stderr," Invalid 4th argument %s(*,*,*,'type',*,*)\n",__FUNCTION__); fflush(stderr); return; } /* For mask and reverse mask of magnitude we allow the start and end positions to be defined. */ switch(type) { case MK: start=(int)*val("maskstartslice",&d->p); if ((start<0) || (start>d->nv2)) start=1; start--; end=(int)*val("maskendslice",&d->p); if ((end<1) || (end>d->nv2)) end=d->nv2; break; case RMK: start=(int)*val("maskstartslice",&d->p); if ((start<0) || (start>d->nv2)) start=1; start--; end=(int)*val("maskendslice",&d->p); if ((end<1) || (end>d->nv2)) end=d->nv2; break; default: start=0; end=d->nv2; break; } /* end type switch */ startpos=d->startpos; endpos=d->endpos; if ((start>=endpos) || (end<=startpos)) { #ifdef DEBUG fprintf(stdout,"\n %s(): skipping writing of block %d (of %d)\n",__FUNCTION__,d->block+1,d->nblocks); #endif return; } /* Adjust start and end according to data block */ start=start-startpos; end=end-startpos; if (start<0) start=0; blockslices=endpos-startpos; if (end>blockslices) end=blockslices; /* Check for VnmrJ recon to figure if we are offline */ if (vnmrj_recon) strcpy(basename,vnmrj_path); else { for (i=0;i<=strlen(d->file)-9;i++) basename[i]=d->file[i]; basename[i]=0; offline=TRUE; } /* Number of echoes */ ne=(int)*val("ne",&d->p); if (ne < 1) ne=1; /* Set ne to 1 if 'ne' does not exist */ /* Number of slices (slabs) */ ns=nvals("pss",&d->p); if (ns < 1) ns=1; /* Set ns to 1 if 'ns' does not exist */ /* Allow for compressed multi-echo loop and multiple slabs */ image=(volindex-IMAGEOFFSET)/(ne*ns); slab=((volindex-IMAGEOFFSET)/ne)%ns; echo=(volindex-IMAGEOFFSET)%ne; switch(mode) { case 'i': /* Individual output */ for (i=0;i<d->nr;i++) { sprintf(dirname,"%s%s%.3d",basename,outdir,i+1); if (offline) strcat(dirname,".img"); createdir(dirname); sprintf(filename,"%s/slab%.3dimage%.3decho%.3d.fdf",dirname,slab+1,image+1,echo+1); w3Dfdf(filename,d,image,slab,echo,i,type,precision); /* Write procpar */ sprintf(filename,"%s/procpar",dirname); wprocpar(d,filename); } break; case 'c': /* Combined output (Magnitude only) */ sprintf(dirname,"%s%s",basename,outdir); if (offline) strcat(dirname,".img"); createdir(dirname); sprintf(filename,"%s/slab%.3dimage%.3decho%.3d.fdf",dirname,slab+1,image+1,echo+1); wcomb3Dfdf(filename,d,image,slab,echo,type,precision); /* Write procpar */ sprintf(filename,"%s/procpar",dirname); wprocpar(d,filename); break; case 's': /* Single receiver */ sprintf(dirname,"%s%s",basename,outdir); if (offline) strcat(dirname,".img"); createdir(dirname); sprintf(filename,"%s/slab%.3dimage%.3decho%.3d.fdf",dirname,slab+1,image+1,echo+1); w3Dfdf(filename,d,image,slab,echo,0,type,precision); /* Write procpar */ sprintf(filename,"%s/procpar",dirname); wprocpar(d,filename); break; case 'g': /* Geometry Factor output */ sprintf(dirname,"%s%s",basename,outdir); if (offline) strcat(dirname,".img"); createdir(dirname); sprintf(filename,"%s/slab%.3dimage%.3decho%.3d.fdf",dirname,slab+1,image+1,echo+1); w3Dfdf(filename,d,image,slab,echo,0,type,precision); /* Write procpar */ sprintf(filename,"%s/procpar",dirname); wprocpar(d,filename); break; case 'r': /* Relative SNR */ sprintf(dirname,"%s%s",basename,outdir); if (offline) strcat(dirname,".img"); createdir(dirname); sprintf(filename,"%s/slab%.3dimage%.3decho%.3d.fdf",dirname,slab+1,image+1,echo+1); w3Dfdf(filename,d,image,slab,echo,1,type,precision); /* Write procpar */ sprintf(filename,"%s/procpar",dirname); wprocpar(d,filename); break; default: fprintf(stderr,"\n%s: %s()\n",__FILE__,__FUNCTION__); fprintf(stderr," Invalid 2nd argument %s(*,'mode',*,*,*,*)\n",__FUNCTION__); fflush(stderr); break; } /* end mode switch */ }
bool extract_files(FILE *exefp) { uint8_t *buffer; uint8_t *file; uint32_t length; uint32_t crc; bool check_crc = true; bool first_crc_failure = true; buffer = (uint8_t *)malloc(MAX_FILE_SIZE); crc_init(); for(int i=0;;i++) { if (!files[i].filename) break; char outfilename[1024]; retro_create_path_string(outfilename, sizeof(outfilename), g_dir, files[i].filename); NX_LOG("[ %s ]\n", outfilename); // initialize header if any file = buffer; length = files[i].length; if (files[i].header) { memcpy(buffer, files[i].header, HEADER_LEN); file += HEADER_LEN; length += HEADER_LEN; } // read data from exe fseek(exefp, files[i].offset, SEEK_SET); fread(file, files[i].length, 1, exefp); if (check_crc) { crc = crc_calc(file, files[i].length); if (crc != files[i].crc) { NX_ERR("File '%s' failed CRC check.\n", outfilename); NX_ERR("\n"); NX_ERR("[I]gnore\n"); NX_ERR("Ignore [A]ll\n"); NX_ERR("[S]top\n"); #define IGNORE_BTN SDLK_i #define IGNORE_ALL_BTN SDLK_a #define STOP_BTN SDLK_s first_crc_failure = false; } } // write out the file createdir(outfilename); FILE *fp = fopen(outfilename, "wb"); if (!fp) { NX_ERR("Failed to open '%s' for writing.\n", outfilename); free(buffer); return 1; } fwrite(buffer, length, 1, fp); fclose(fp); } free(buffer); return 0; }
/* * -- configure * * do a first pass of the command line parameters to find all * configuration files. the options in those files are processed * before any other command line parameter. command line will * overwrite any other configuration, as well as the last config * file will overwrite previous config files. * */ void configure(struct _como * m, int argc, char ** argv) { cli_args_t cli_args; int config_file_exists; int c, i, j; DIR *d; if (m->cli_args.done_flag == 0) { parse_cmdline(&cli_args, argc, argv); if (cli_args.logflags != -1) { m->logflags = cli_args.logflags; m->cli_args.logflags_set = 1; } if (cli_args.dbdir != NULL) { safe_dup(&m->dbdir, cli_args.dbdir); m->cli_args.dbdir_set = 1; } if (cli_args.libdir != NULL) { safe_dup(&m->libdir, cli_args.libdir); m->cli_args.libdir_set = 1; } if (cli_args.query_port != -1) { m->node->query_port = cli_args.query_port; m->cli_args.query_port_set = 1; } if (cli_args.mem_size != 0) { m->mem_size = cli_args.mem_size; m->cli_args.mem_size_set = 1; } m->exit_when_done = cli_args.exit_when_done; } m->runmode = cli_args.runmode; m->inline_fd = (m->runmode == RUNMODE_INLINE) ? 1 /* stdout */ : -1; m->debug = cli_args.debug; /* * build list of config files */ config_file_exists = 0; for (c = 0; c < cli_args.cfg_files_count; c++) { config_file_exists = 1; parse_cfgfile(m, cli_args.cfg_files[c]); } if (!config_file_exists && m->runmode != RUNMODE_INLINE) parse_cfgfile(m, DEFAULT_CFGFILE); /* add default config file */ if (m->runmode == RUNMODE_INLINE) { char *conf_argv[2]; m->exit_when_done = 1; if (cli_args.sniffer != NULL) { add_sniffer(m, cli_args.sniffer, NULL, NULL); } /* prepare the arguments for do_config() */ conf_argv[0] = "module"; conf_argv[1] = cli_args.module; do_config(m, 2, conf_argv); if (cli_args.module_args != NULL) { conf_argv[0] = "args"; conf_argv[1] = cli_args.module_args; do_config(m, 2, conf_argv); } if (cli_args.filter != NULL) { conf_argv[0] = "filter"; conf_argv[1] = cli_args.filter; do_config(m, 2, conf_argv); } conf_argv[0] = "end"; do_config(m, 1, conf_argv); } /* * now look into the virtual nodes and replicate * all modules that have been found in the config file(s) * * these new modules will have the same name but will be * running the additional filter associated with the virtual * node and save data in the virtual node dbdir. * * XXX all virtual nodes will be running on demand and * the source is defined in the configuration (or assumed to * be a trace module). later there shouldn't be a need * for defining the source module anyway... * */ for (i = 0, j = m->module_last; i <= j; i++) { module_t * orig; int node_id; orig = &m->modules[i]; for (node_id = 1; node_id < m->node_count; node_id++) { module_t * mdl; char * nm; /* create a new module and copy it from new module */ mdl = copy_module(m, orig, node_id, -1, m->node[node_id].args); mdl->running = RUNNING_ON_DEMAND; /* append node id to module's output file */ asprintf(&nm, "%s-%d", mdl->output, mdl->node); safe_dup(&mdl->output, nm); free(nm); /* add the node filter to the module filter */ if (m->node[node_id].filter_str) { char * flt; if (!strcmp(mdl->filter_str, "all")) asprintf(&flt, "%s", m->node[node_id].filter_str); else asprintf(&flt,"(%s) and (%s)", m->node[node_id].filter_str, mdl->filter_str); mdl->filter_str = flt; /* FIXME: possible leak */ } /* add the node arguments to the module arguments */ if (m->node[node_id].args) { int k; for (k = 0; m->node[node_id].args[k]; k++) { /* * XXX we copy one argument at a time to avoid * having to count them first. FIX THIS */ mdl->args = copy_args(mdl->args, &m->node[node_id].args[k], 1); } } logmsg(LOGUI, "... module%s %s [%d][%d] ", (mdl->running == RUNNING_ON_DEMAND) ? " on-demand" : "", mdl->name, mdl->node, mdl->priority); logmsg(LOGUI, " filter %s; out %s (%uMB)\n", mdl->filter_str, mdl->output, mdl->streamsize/(1024*1024)); if (mdl->description != NULL) logmsg(LOGUI, " -- %s\n", mdl->description); } } /* * open the dbdir for all nodes (virtual ones included) */ if (m->runmode == RUNMODE_NORMAL) { if (m->dbdir == NULL) panicx("missing db-path"); d = opendir(m->dbdir); if (d == NULL) createdir(m->dbdir); else closedir(d); } /* * process the AS file */ asn_readfile(m->asnfile); }
int open_fifo(ngx_cycle_t *cycle){ ngx_proc_send_conf_t *pbcf; pbcf = ngx_proc_get_conf(cycle->conf_ctx, ngx_proc_send_module); int fd = 0; int off_fd = 0; createdir((char*)pbcf->mmap_idx.data, cycle); createdir((char *)pbcf->mmap_dat.data, cycle); if(access((char*)pbcf->mmap_idx.data, 0) == -1){ off_fd = open((char*)pbcf->mmap_idx.data, O_CREAT|O_RDWR, 0777); if(off_fd == -1){ ngx_log_error(NGX_LOG_ERR, cycle->log, 0, "create file %V failed", pbcf->mmap_idx); return -1; } lseek(off_fd, sizeof(long)*3-1, SEEK_SET); write(off_fd,"",1); read_write_mmap = (long*) mmap( NULL,sizeof(long)*3, PROT_READ|PROT_WRITE, MAP_SHARED, off_fd, 0); *read_write_mmap = (unsigned int)pbcf->mmap_dat_size; *(read_write_mmap + 1) = 0; *(read_write_mmap + 2) = 0; } else{ off_fd = open((char*)pbcf->mmap_idx.data, O_CREAT|O_RDWR, 0777); if(off_fd == -1){ ngx_log_error(NGX_LOG_ERR, cycle->log, 0, "open file %V failed", pbcf->mmap_idx); return -1; } if(get_file_size((char*)pbcf->mmap_idx.data) < (long)sizeof(long)*3){ lseek(off_fd, sizeof(long)*3-1, SEEK_SET); write(off_fd,"",1); read_write_mmap = (long*) mmap( NULL,sizeof(long)*2, PROT_READ|PROT_WRITE, MAP_SHARED, off_fd, 0); *read_write_mmap = (unsigned int)pbcf->mmap_dat_size; *(read_write_mmap + 1) = 0; *(read_write_mmap + 2) = 0; } read_write_mmap = (long*) mmap( NULL,sizeof(long)*3, PROT_READ|PROT_WRITE, MAP_SHARED, off_fd, 0); if( *read_write_mmap != (long)pbcf->mmap_dat_size ){ *read_write_mmap = (unsigned int)pbcf->mmap_dat_size; *(read_write_mmap + 1) = 0; *(read_write_mmap + 2) = 0; } } close(off_fd); if(access((char *)pbcf->mmap_dat.data, 0) == -1){ fd = open((char *)pbcf->mmap_dat.data, O_CREAT|O_RDWR, 0777); if(fd == -1){ ngx_log_error(NGX_LOG_ERR, cycle->log, 0, "open file %V failed", pbcf->mmap_dat); return -1; } lseek(fd, pbcf->mmap_dat_size - 1, SEEK_SET); write(fd,"",1); write_file = (char*) mmap( NULL, pbcf->mmap_dat_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); } else{ fd = open((char *)pbcf->mmap_dat.data, O_CREAT|O_RDWR, 0777); if(fd == -1){ ngx_log_error(NGX_LOG_ERR, cycle->log, 0, "open file %V failed", pbcf->mmap_dat); return -1; } if(get_file_size((char *)pbcf->mmap_dat.data) < (long)pbcf->mmap_dat_size){ lseek(fd, pbcf->mmap_dat_size - 1, SEEK_SET); write(off_fd,"",1); read_write_mmap = (long*) mmap(NULL, pbcf->mmap_dat_size, PROT_READ|PROT_WRITE, MAP_SHARED, off_fd, 0); } write_file = (char*) mmap(NULL, pbcf->mmap_dat_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); } close(fd); return 0; }
void vsmextract(int argc, char *argv[]) { TVSMFile f; // VSM file TVSMResource m; // processed resource char *fdir; // working directory char *fn; // filename int resno = 1; // resource number int x; char ch; char tmp[1024]; // get filename fn = fname(argv[2]); // can't open file or wrong format if (f.open(fn) == 0) errexit("Incorrect VSM file!\n"); // load resources list TVMSLInfo rlist; if (rlist.load() == 0) puts("Can't load 'resources.txt' file!\n"); TVSMInfo *rinfo; // generating output directory name if (argc <= 3) { fdir = new char[strlen(argv[2]) + 6]; strcpy(fdir, argv[2]); removespecialchars(fname(fdir)); strcat(fdir, "_OUT"); } else fdir = argv[3]; x = createdir(fdir); if (x < 0) errexit("Can't create output folder!"); // file information printf("VSM file: %s\n", fn); printf("Output folder: %s\n", fdir); // directory already exists if (x == 0) { puts("\nOutput directory already exist. Files in this folder " "may be overwritten. Continue? (y/N)\n"); ch = getch(); if ((ch != 'y') && (ch != 'Y')) return; } fileinfo(&f.header, f.computeChecksum()); puts(""); // write header into file sprintf(tmp, "%s\\header.bin", fdir); fsave(tmp, &f.header, sizeof(f.header)); // read resources while ( (x = f.read(&m)) > -1 ) { // get resource info if (rlist.find(m.id)) { rinfo = &rlist.get(); printf("resource %2d: 0x%04X 0x%04X (%s) - %s\n", resno, m.id, m.size, rinfo->ext, rinfo->descr); sprintf(tmp, "%s\\res%04X_%s.%s", fdir, m.id, rinfo->descr, rinfo->ext); } // there is no such resource on the list else { printf("resource %2d: 0x%04X 0x%04X (??\?) - UNKNOWN\n", resno, m.id, m.size); sprintf(tmp, "%s\\res%04X.bin", fdir, m.id); } if (fsave(tmp, m.data, m.size) == 0) printf("Can't write file '%s'!", tmp); delete [] m.data; resno++; } // error during resource read if (x < -1) errexit("Incorrect VSM file!\n"); // if footer exists if (f.footer_sz) { // save footer to file puts(""); sprintf(tmp, "%s\\footer.bin", fdir); if (fsave(tmp, f.footer, f.footer_sz) == 0) puts("Can't write file 'footer.bin'!"); // show footer info footerinfo(f.footer, f.footer_sz); } f.close(); delete [] fdir; }
/* * -- configure * * do a first pass of the command line parameters to find all * configuration files. the options in those files are processed * before any other command line parameter. command line will * overwrite any other configuration, as well as the last config * file will overwrite previous config files. * */ void configure(struct _como * m, int argc, char ** argv) { static cli_args_t cli_args; int config_file_exists; int c, i, j; DIR *d; if (cli_args.done == 0) { parse_cmdline(&cli_args, argc, argv); } m->running = cli_args.running; /* * build list of config files */ config_file_exists = 0; for (c = 0; c < cli_args.cfg_files_count; c++) { config_file_exists = 1; parse_cfgfile(m, cli_args.cfg_files[c]); } if (!config_file_exists && m->running == NORMAL) parse_cfgfile(m, DEFAULT_CFGFILE); /* add default config file */ /* use cli args to override cfg file settings */ if (cli_args.basedir != NULL) safe_dup(&m->basedir, cli_args.basedir); if (cli_args.libdir != NULL) safe_dup(&m->libdir, cli_args.libdir); if (cli_args.query_port != -1) m->node->query_port = cli_args.query_port; if (cli_args.mem_size != 0) m->mem_size = cli_args.mem_size; if (cli_args.logflags != -1) m->logflags = cli_args.logflags; m->debug = cli_args.debug; if (m->running == INLINE) { char *conf_argv[2]; if (cli_args.sniffer != NULL) { add_sniffer(m, cli_args.sniffer, NULL, NULL); } /* prepare the arguments for do_config() */ conf_argv[0] = "module"; conf_argv[1] = cli_args.module; do_config(m, 2, conf_argv); if (cli_args.module_args != NULL) { conf_argv[0] = "args"; conf_argv[1] = cli_args.module_args; do_config(m, 2, conf_argv); } if (cli_args.filter != NULL) { conf_argv[0] = "filter"; conf_argv[1] = cli_args.filter; do_config(m, 2, conf_argv); } conf_argv[0] = "end"; do_config(m, 1, conf_argv); } /* * now look into the virtual nodes and replicate * all modules that have been found in the config file(s) * * these new modules will have the same name but will be * running the additional filter associated with the virtual * node and save data in the virtual node basedir. */ for (i = 0, j = m->module_last; i <= j; i++) { node_t * node; module_t * orig; orig = &m->modules[i]; for (node = m->node; node; node = node->next) { module_t * mdl; char * nm; if (node->id == 0) break; /* master node. nothing to do */ /* create a new module and copy it from new module */ mdl = copy_module(m, orig, node->id, -1, NULL); /* append node id to module's output file */ asprintf(&nm, "%s-%d", mdl->output, mdl->node); safe_dup(&mdl->output, nm); free(nm); /* add the node filter to the module filter */ if (node->filter_str) { char * flt; if (!strcmp(mdl->filter_str, "all")) asprintf(&flt, "%s", node->filter_str); else asprintf(&flt,"(%s) and (%s)", node->filter_str, mdl->filter_str); mdl->filter_str = flt; /* FIXME: possible leak */ } logmsg(LOGUI, "... module %s [%d][%d] ", mdl->name, mdl->node, mdl->priority); logmsg(LOGUI, " filter %s; out %s (%uMB)\n", mdl->filter_str, mdl->output, mdl->streamsize/(1024*1024)); if (mdl->description != NULL) logmsg(LOGUI, " -- %s\n", mdl->description); } } /* * open the basedir for all nodes (virtual ones included) */ if (m->basedir == NULL) panicx("missing basedir"); d = opendir(m->basedir); if (d == NULL) createdir(m->basedir); else closedir(d); }
void ThumbCacheEnumerate(int level) { LPTCHEADER ptchdr; LPTCENTRY ptcent; unsigned char *thumbdata; unsigned int pos; gdImagePtr thumb; int nentries = 0, ndelentries = 0; if (!ThumbCacheBurstReadBegin(0)) { fprintf(stderr, "ERROR: failed to open cache for mapping\n"); return; } if (level >= TC_DUMP_IMGS) { if (!outpath[0]) { fprintf(stderr, "ERROR: must specify an output path\n"); return; } if (createdir(outpath) == -1 && errno != EEXIST) { perror("mkdir"); return; } if (chdir(outpath) == -1) { perror("chdir"); return; } } ptchdr = (LPTCHEADER)cachemap.addr; if (level >= TC_DUMP_INFO) { printf("Directory last modified: %s" "Thumb cache entries:\n" "file " "thumb key\tthumb len\tlast modified\n", asctime(localtime(&ptchdr->lastupdate))); } pos = sizeof(TCHEADER); while (pos < cachemap.maplen) { ptcent = (LPTCENTRY)((char *)cachemap.addr + pos); if (ptcent->mtime != TC_MTIME_DELETED) { if (level >= TC_DUMP_INFO) { printf("%-26s%f\t%d\t%s", ptcent->filename, ptcent->thumbkey, ptcent->thumbfsize, asctime(localtime(&ptcent->mtime))); } if (level >= TC_DUMP_IMGS) { thumbdata = (unsigned char *)ptcent + sizeof(TCENTRY) + ptcent->fnlen + 1; thumb = gdImageCreateFromPngPtr(ptcent->thumbfsize, thumbdata); if (!thumb) { fprintf(stderr, "ERROR: failed to create image from thumbcache\n"); continue; } if (!ImgSavePng(ptcent->filename, thumb)) { if (errno == ENOENT) { if (verbose) printf("creating directory structure for %s\n", ptcent->filename); if (!BuildPath(ptcent->filename)) { fprintf(stderr, "ERROR: failed to build " "directory to %s\n", ptcent->filename); gdImageDestroy(thumb); continue; } if (!ImgSavePng(ptcent->filename, thumb)) { fprintf(stderr, "ERROR: failed to save %s after " "building directory\n", ptcent->filename); gdImageDestroy(thumb); continue; } } else { fprintf(stderr, "ERROR: failed to save %s\n", ptcent->filename); gdImageDestroy(thumb); continue; } } gdImageDestroy(thumb); } nentries++; } else { ndelentries++; } pos += sizeof(TCENTRY) + ptcent->fnlen + 1 + ptcent->thumbfsize; } if (level >= TC_DUMP_IMGS && chdir(workdir) == -1) { perror("chdir"); return; } printf("Number of thumb cache entries: %d\n" "Number of deleted thumb cache entries: %d\n", nentries, ndelentries); ThumbCacheBurstReadEnd(); }
void gen3Draw(struct data *d,int output,char *outdir,int dataorder,int type,int precision) { char basename[MAXPATHLEN],dirname[MAXPATHLEN],filename[MAXPATHLEN]; int ne,ns; int i; int slab,image,echo; int volindex; /* This function checks the output type requested and sets the output filename accordingly. 3D raw data is output according to the specified mode using functions w3Draw() and wcomb3Draw(). These functions output data either from individual receivers (w3Draw) or using a combination of data from all receivers (wcomb3Draw). */ /* Check for VnmrJ recon to figure if we are offline */ if (vnmrj_recon) strcpy(basename,vnmrj_path); else { for (i=0;i<=strlen(d->file)-9;i++) basename[i]=d->file[i]; basename[i]=0; } /* Number of echoes */ ne=(int)*val("ne",&d->p); if (ne < 1) ne=1; /* Set ne to 1 if 'ne' does not exist */ /* Number of slices (slabs) */ ns=nvals("pss",&d->p); if (ns < 1) ns=1; /* Set ns to 1 if 'ns' does not exist */ /* Allow for compressed multi-echo loop and multiple slabs */ volindex=d->vol; image=volindex/(ne*ns); slab=(volindex/ne)%ns; echo=volindex%ne; switch(output) { case 'i': /* Individual output */ for (i=0;i<d->nr;i++) { sprintf(dirname,"%s%s%.3d",basename,outdir,i+1); createdir(dirname); sprintf(filename,"%s/slab%.3dimage%.3decho%.3d.raw",dirname,slab+1,image+1,echo+1); switch(dataorder) { case D3: w3DrawD3(filename,d,i); break; default: w3Draw(filename,d,i,type,precision); break; } } break; case 'c': /* Combined output (Magnitude only) */ sprintf(dirname,"%s%s",basename,outdir); createdir(dirname); sprintf(filename,"%s/slab%.3dimage%.3decho%.3d.raw",dirname,slab+1,image+1,echo+1); wcomb3Draw(filename,d,type,precision); break; case 's': /* Single receiver */ sprintf(dirname,"%s%s",basename,outdir); createdir(dirname); sprintf(filename,"%s/slab%.3dimage%.3decho%.3d.raw",dirname,slab+1,image+1,echo+1); switch(dataorder) { case D3: w3DrawD3(filename,d,0); break; default: w3Draw(filename,d,0,type,precision); break; } break; case 'g': /* Geometry Factor output */ sprintf(dirname,"%s%s",basename,outdir); createdir(dirname); sprintf(filename,"%s/slab%.3dimage%.3decho%.3d.raw",dirname,slab+1,image+1,echo+1); w3Draw(filename,d,0,type,precision); break; case 'r': /* Relative SNR */ sprintf(dirname,"%s%s",basename,outdir); createdir(dirname); sprintf(filename,"%s/slab%.3dimage%.3decho%.3d.raw",dirname,slab+1,image+1,echo+1); w3Draw(filename,d,1,type,precision); break; default: fprintf(stderr,"\n%s: %s()\n",__FILE__,__FUNCTION__); fprintf(stderr," Invalid 2nd argument %s(*,'mode',*,*,*,*)\n",__FUNCTION__); fflush(stderr); break; } /* end output switch */ }