static int wrapfs_open(struct inode *inode, struct file *file) { int err = 0; struct dentry *dentry = file->f_path.dentry; struct dentry *parent = NULL; int size; /* don't open unhashed/deleted files */ if (d_unhashed(dentry)) { err = -ENOENT; goto out_err; } file->private_data = kzalloc(sizeof(struct wrapfs_file_info), GFP_KERNEL); if (!WRAPFS_F(file)) { err = -ENOMEM; goto out_err; } atomic_set(&WRAPFS_F(file)->generation, atomic_read(&WRAPFS_I(inode)->generation)); size = sizeof(struct file *) * 2; WRAPFS_F(file)->lower_files = kzalloc(size, GFP_KERNEL); if (unlikely(!WRAPFS_F(file)->lower_files)) { err = -ENOMEM; goto out_err; } parent = dget_parent(dentry); if (S_ISDIR(inode->i_mode)) err = __open_dir(inode, file, parent); else err = __open_file(inode, file, parent); /* if(!err) fsstack_copy_attr_all(inode, wrapfs_lower_inode(inode)); */ out_err: dput(parent); return err; }
int write_dense_matrix(const char * const filename, const coord_t * const * const coords, const vtx_t n, const size_t m) { int rv; file_t * file; vtx_t i; size_t d; if ((rv = __open_file(filename,"w",&file)) != BOWSTRING_SUCCESS) { return rv; } for (i=0;i<n;++i) { for (d=0;d<m;++d) { dl_fprintf(file,PF_COORD_T" ",coords[d][i]); } dl_fprintf(file,"\n"); } dl_close_file(file); return BOWSTRING_SUCCESS; }
int read_snap_graph(const char * const filename, vtx_t * const r_nvtxs, adj_t ** const r_xadj, vtx_t ** const r_adjncy, wgt_t ** const r_vwgt, wgt_t ** const r_adjwgt) { file_t * file; int rv, do_adjwgt; vtx_t nvtxs, i, j, u, v, maxv; adj_t nedges; ssize_t ll; size_t bufsize; char * line = NULL,*eptr, * sptr; wgt_t w; vtx_t * labels = NULL; adj_t * xadj = NULL; vtx_t * adjncy = NULL; wgt_t * adjwgt = NULL; bufsize = BUFFERSIZE; if ((rv = __open_file(filename,"r",&file)) != BOWSTRING_SUCCESS) { goto ERROR; } line = char_alloc(bufsize); do_adjwgt = 0; nedges = 0; /* first pass to count edges and vertices */ maxv = 0; while((ll = dl_get_next_line(file,&line,&bufsize)) >= 0) { if (ll == 0) { /* ignore blank lines */ continue; } else { if (COMMENT_CHARS[(unsigned int)line[0]]) { /* skip comments */ continue; } } sptr = line; i = (vtx_t)strtoull(sptr,&eptr,10); sptr = eptr; j = (vtx_t)strtoull(sptr,&eptr,10); sptr = eptr; w = (wgt_t)strtod(sptr,&eptr); if (eptr != sptr) { /* if anyone is missing weight, we'll assign 1 */ printf("Parsing edge weight from SNAP graph\n"); do_adjwgt = 1; } if (i > maxv) { maxv = i; } if (j > maxv) { maxv = j; } /* count edges in both directions */ nedges+=2; } nvtxs = maxv+1; labels = vtx_init_alloc(NULL_VTX,nvtxs); xadj = adj_calloc(nvtxs+1); adjncy = vtx_alloc(nedges); if (do_adjwgt) { adjwgt = wgt_alloc(nedges); } dl_reset_file(file); /* second pass to populate xadj */ nvtxs = 0; while((ll = dl_get_next_line(file,&line,&bufsize)) >= 0) { if (ll == 0) { /* ignore blank lines */ continue; } else { if (COMMENT_CHARS[(unsigned int)line[0]]) { /* skip comments */ continue; } } sptr = line; i = strtoull(sptr,&eptr,10); if (labels[i] == NULL_VTX) { u = labels[i] = nvtxs++; } else { u = labels[i]; } sptr = eptr; j = strtoull(sptr,&eptr,10); if (labels[j] == NULL_VTX) { v = labels[j] = nvtxs++; } else { v = labels[j]; } ++xadj[u+1]; ++xadj[v+1]; } xadj[0] = 0; adj_prefixsum_exc(xadj+1,nvtxs); dl_reset_file(file); DL_ASSERT(xadj[1] == 0,"Broken xadj for loading\n"); /* final pass */ w = 0; /* stops uninitialized complaints */ while((ll = dl_get_next_line(file,&line,&bufsize)) >= 0) { if (ll == 0) { /* ignore blank lines */ continue; } else { if (COMMENT_CHARS[(unsigned int)line[0]]) { /* skip comments */ continue; } } sptr = line; u = labels[strtoull(sptr,&eptr,10)]; sptr = eptr; v = labels[strtoull(sptr,&eptr,10)]; sptr = eptr; if (do_adjwgt) { w = (wgt_t)strtod(sptr,&eptr); if (eptr == sptr) { /* if anyone is missing weight, we'll assign 1 */ w = 1; } } adjncy[xadj[u+1]] = v; adjncy[xadj[v+1]] = u; if (do_adjwgt) { adjwgt[xadj[u+1]] = w; adjwgt[xadj[v+1]] = w; } ++xadj[u+1]; ++xadj[v+1]; } dl_free(labels); dl_free(line); dl_close_file(file); /* sort the edges */ if (do_adjwgt) { for (i=0;i<nvtxs;++i) { __quicksort_edges(adjncy+xadj[i],adjwgt+xadj[i],xadj[i+1]-xadj[i]); } } else { for (i=0;i<nvtxs;++i) { vtx_quicksort(adjncy+xadj[i],xadj[i+1]-xadj[i]); } } /* remove duplicates */ nedges = 0; for (i=0;i<nvtxs;++i) { j=xadj[i]; xadj[i] = nedges; if (j<xadj[i+1]) { adjncy[nedges] = adjncy[j]; if (do_adjwgt) { adjwgt[nedges] = adjwgt[j]; } ++nedges; ++j; } for (;j<xadj[i+1];++j) { if (adjncy[j] != adjncy[j-1]) { adjncy[nedges] = adjncy[j]; if (do_adjwgt) { adjwgt[nedges] = adjwgt[j]; } ++nedges; } } } xadj[nvtxs] = nedges; if (r_nvtxs) { *r_nvtxs = nvtxs; } if (r_xadj) { *r_xadj = xadj; } else if (xadj) { dl_free(xadj); } if (r_adjncy) { *r_adjncy = adjncy; } else if (adjncy) { dl_free(adjncy); } /* doesn't support vwgt */ if (r_vwgt) { *r_vwgt = NULL; } if (r_adjwgt) { *r_adjwgt = adjwgt; } else if (adjwgt) { dl_free(adjwgt); } return BOWSTRING_SUCCESS; ERROR: if (line) { dl_free(line); } if (labels) { dl_free(labels); } if (xadj) { dl_free(xadj); } if (adjncy) { dl_free(adjncy); } if (adjwgt) { dl_free(adjwgt); } return rv; }
int read_stp_graph( char const * const filename, vtx_t * const r_nvtxs, adj_t ** const r_xadj, vtx_t ** const r_adjncy, wgt_t ** const r_vwgt, wgt_t ** const r_adjwgt) { int rv; vtx_t i, k, nvtxs; adj_t nedges, j, l; wgt_t w; ssize_t ll; char * line = NULL; size_t bufsize = BUFFERSIZE; file_t * ifile = NULL; vtx_t * x = NULL; vtx_t * y = NULL; wgt_t * z = NULL; adj_t * xadj = NULL; vtx_t * adjncy = NULL; wgt_t * adjwgt = NULL; wgt_t * vwgt = NULL; if ((rv = __open_file(filename,"r",&ifile)) != BOWSTRING_SUCCESS) { goto END; } line = char_alloc(bufsize); /* find the graph section */ if (!__find_section(ifile,"GRAPH",&bufsize)) { eprintf("Could not find graph section\n"); rv = BOWSTRING_ERROR_INVALIDINPUT; goto END; } /* read the number of vertices/nodes */ if ((ll = dl_get_next_line(ifile,&line,&bufsize)) < 0 || sscanf(line,"Nodes%*[ \t]"RF_VTX_T,&nvtxs) != 1) { eprintf("Could not read number of nodes from graph section\n"); rv = BOWSTRING_ERROR_INVALIDINPUT; goto END; } /* read the number of edges */ if ((ll = dl_get_next_line(ifile,&line,&bufsize)) < 0 || sscanf(line,"Edges%*[ \t]"RF_ADJ_T,&nedges) != 1) { eprintf("Could not read number of edges from graph section\n"); rv = BOWSTRING_ERROR_INVALIDINPUT; goto END; } nedges *=2; /* allocate graph */ xadj = adj_calloc(nvtxs+1); adjncy = vtx_alloc(nedges); adjwgt = wgt_alloc(nedges); /* allocate graph buffers */ x = vtx_alloc(nedges/2); y = vtx_alloc(nedges/2); z = wgt_alloc(nedges/2); /* read in edges */ for (j=0;j<nedges/2;++j) { while ((ll = dl_get_next_line(ifile,&line,&bufsize)) == 0) { /* blank line */ } if (ll < 0) { eprintf("Could not find edge #"PF_ADJ_T": '%s'\n",j,line); rv = BOWSTRING_ERROR_INVALIDINPUT; goto END; } else { if (dl_strncmp_nocase(line,"END",3) == 0) { wprintf("Only half the number of edges listed :"PF_ADJ_T"/"PF_ADJ_T \ ".\n",j,nedges/2); nedges = j*2; goto NODEWEIGHTS; } else if (line[0] == 'E') { if (sscanf(line,"E%*[ \t]"RF_VTX_T"%*[ \t]"RF_VTX_T"%*[ \t]"RF_WGT_T, \ &i,&k,&w) != 3) { eprintf("Could not read edge #"PF_ADJ_T": '%s'\n",j,line); rv = BOWSTRING_ERROR_INVALIDINPUT; goto END; } } else if (line[0] == 'A') { /* handle broken files with arcs listed instead of edges */ if (sscanf(line,"A%*[ \t]"RF_VTX_T"%*[ \t]"RF_VTX_T"%*[ \t]"RF_WGT_T, \ &i,&k,&w) != 3) { eprintf("Could not read arc #"PF_ADJ_T": '%s'\n",j,line); rv = BOWSTRING_ERROR_INVALIDINPUT; goto END; } } else { eprintf("Bad label for edge #"PF_ADJ_T": '%s'\n",j,line); rv = BOWSTRING_ERROR_INVALIDINPUT; goto END; } ++xadj[i]; ++xadj[k]; --i; --k; x[j] = i; y[j] = k; z[j] = w; } } /* skip blank lines */ while ((ll = dl_get_next_line(ifile,&line,&bufsize)) == 0) { /* blank line */ } /* check the end of the edge list */ if (ll < 0 || dl_strncmp_nocase(line,"END",3) != 0) { eprintf("Invalid line at end of section: '%s'\n",line); rv = BOWSTRING_ERROR_INVALIDINPUT; goto END; } NODEWEIGHTS: /* find the node weight section if it exists */ if (r_vwgt && __find_section(ifile,"NODEWEIGHTS",&bufsize)) { vwgt = wgt_alloc(nvtxs); for (i=0;i<nvtxs;++i) { while ((ll = dl_get_next_line(ifile,&line,&bufsize)) == 0) { /* blank line */ } if (ll < 0 || sscanf(line,"NW%*[ \t]"RF_WGT_T,&w) != 1) { eprintf("Could not read vertex weight for vertex "PF_VTX_T"\n",i); rv = BOWSTRING_ERROR_INVALIDINPUT; goto END; } else { vwgt[i] = w; } } /* skip blank lines */ while ((ll = dl_get_next_line(ifile,&line,&bufsize)) == 0) { /* blank line */ } /* check the end of the vertex weight list */ if (ll < 0 || dl_strncmp_nocase(line,"END",3) != 0) { eprintf("Invalid line at end of section: '%s'\n",line); rv = BOWSTRING_ERROR_INVALIDINPUT; goto END; } } /* close the file */ dl_close_file(ifile); ifile = NULL; /* build edges */ adj_prefixsum_exc(xadj+1,nvtxs); for (j=0;j<nedges/2;++j) { i = x[j]; k = y[j]; w = z[j]; /* vertex a */ l = xadj[i+1]++; adjncy[l] = k; adjwgt[l] = w; /* vertex b */ l = xadj[k+1]++; adjncy[l] = i; adjwgt[l] = w; } *r_nvtxs = nvtxs; *r_xadj = xadj; xadj = NULL; *r_adjncy = adjncy; adjncy = NULL; if (r_adjwgt) { *r_adjwgt = adjwgt; } else { dl_free(adjwgt); } adjwgt = NULL; if (r_vwgt) { *r_vwgt = vwgt; } vwgt = NULL; END: if (line) { dl_free(line); } if (ifile) { dl_close_file(ifile); } if (xadj) { dl_free(xadj); } if (adjncy) { dl_free(adjncy); } if (adjwgt) { dl_free(adjwgt); } if (vwgt) { dl_free(vwgt); } if (x) { dl_free(x); } if (y) { dl_free(y); } if (z) { dl_free(z); } return rv; }
int write_stp_graph( char const * const filename, vtx_t const nvtxs, adj_t const * const xadj, vtx_t const * const adjncy, wgt_t const * const vwgt, wgt_t const * const adjwgt) { int rv; file_t * file; vtx_t i, k; adj_t j; if ((rv = __open_file(filename,"w",&file)) != BOWSTRING_SUCCESS) { return rv; } /* print the header line */ dl_fprintf(file,"%s\n",__STP_HEADER); dl_fprintf(file,"\n"); /* print comment section */ dl_fprintf(file,"%s Comment\n",__SECTION_STR); dl_fprintf(file,"Name \"Saved Graph File\"\n"); dl_fprintf(file,"Creator \"Bowstring v%d.%d.%d\"\n",BOWSTRING_VER_MAJOR, \ BOWSTRING_VER_MINOR,BOWSTRING_VER_SUBMINOR); dl_fprintf(file,"END\n"); dl_fprintf(file,"\n"); /* print the graph numbers */ dl_fprintf(file,"%s Graph\n",__SECTION_STR); dl_fprintf(file,"Nodes "PF_VTX_T"\n",nvtxs); dl_fprintf(file,"Edges "PF_ADJ_T"\n",xadj[nvtxs]/2); /* print out the edges */ for (i=0;i<nvtxs;++i) { for (j=xadj[i];j<xadj[i+1];++j) { k = adjncy[j]; if (i < k) { if (adjwgt) { dl_fprintf(file,"E "PF_VTX_T" "PF_VTX_T" "PF_WGT_T"\n",i+1,k+1, \ adjwgt[j]); } else { dl_fprintf(file,"E "PF_VTX_T" "PF_VTX_T" 1\n",i+1,k+1); } } } } dl_fprintf(file,"END\n"); dl_fprintf(file,"\n"); /* print node weights */ if (vwgt) { dl_fprintf(file,"%s NodeWeights\n",__SECTION_STR); for (i=0;i<nvtxs;++i) { dl_fprintf(file,"NW "PF_WGT_T"\n",vwgt[i]); } dl_fprintf(file,"END\n"); } /* print footer */ dl_fprintf(file,"\n"); dl_fprintf(file,"EOF"); dl_close_file(file); return BOWSTRING_SUCCESS; }
int unionfs_open(struct inode *inode, struct file *file) { int err = 0; struct file *lower_file = NULL; struct dentry *dentry = file->f_path.dentry; struct dentry *parent; int bindex = 0, bstart = 0, bend = 0; int size; int valid = 0; unionfs_read_lock(inode->i_sb, UNIONFS_SMUTEX_PARENT); parent = unionfs_lock_parent(dentry, UNIONFS_DMUTEX_PARENT); unionfs_lock_dentry(dentry, UNIONFS_DMUTEX_CHILD); /* don't open unhashed/deleted files */ if (d_deleted(dentry)) { err = -ENOENT; goto out_nofree; } /* XXX: should I change 'false' below to the 'willwrite' flag? */ valid = __unionfs_d_revalidate(dentry, parent, false); if (unlikely(!valid)) { err = -ESTALE; goto out_nofree; } file->private_data = kzalloc(sizeof(struct unionfs_file_info), GFP_KERNEL); if (unlikely(!UNIONFS_F(file))) { err = -ENOMEM; goto out_nofree; } fbstart(file) = -1; fbend(file) = -1; atomic_set(&UNIONFS_F(file)->generation, atomic_read(&UNIONFS_I(inode)->generation)); size = sizeof(struct file *) * sbmax(inode->i_sb); UNIONFS_F(file)->lower_files = kzalloc(size, GFP_KERNEL); if (unlikely(!UNIONFS_F(file)->lower_files)) { err = -ENOMEM; goto out; } size = sizeof(int) * sbmax(inode->i_sb); UNIONFS_F(file)->saved_branch_ids = kzalloc(size, GFP_KERNEL); if (unlikely(!UNIONFS_F(file)->saved_branch_ids)) { err = -ENOMEM; goto out; } bstart = fbstart(file) = dbstart(dentry); bend = fbend(file) = dbend(dentry); /* * open all directories and make the unionfs file struct point to * these lower file structs */ if (S_ISDIR(inode->i_mode)) err = __open_dir(inode, file); /* open a dir */ else err = __open_file(inode, file, parent); /* open a file */ /* freeing the allocated resources, and fput the opened files */ if (err) { for (bindex = bstart; bindex <= bend; bindex++) { lower_file = unionfs_lower_file_idx(file, bindex); if (!lower_file) continue; branchput(dentry->d_sb, bindex); /* fput calls dput for lower_dentry */ fput(lower_file); } } out: if (err) { kfree(UNIONFS_F(file)->lower_files); kfree(UNIONFS_F(file)->saved_branch_ids); kfree(UNIONFS_F(file)); } out_nofree: if (!err) { unionfs_postcopyup_setmnt(dentry); unionfs_copy_attr_times(inode); unionfs_check_file(file); unionfs_check_inode(inode); } unionfs_unlock_dentry(dentry); unionfs_unlock_parent(dentry, parent); unionfs_read_unlock(inode->i_sb); return err; }
bool_t Com_LoadBomTextFile(const wchar_t *path, cmTxtBom_t *bom, cmString_t *out) { FILE *file = NULL; cmTxtBom_t enc; wchar_t c; bool_t is_ok; txtReadStatus_t status; Com_ASSERT(path != NULL && out != NULL); is_ok = true; Com_ClearString(out); /***************************************************/ file = __open_file(path, L"rb"); /***************************************************/ if(!file) { is_ok = false; goto FAILED_POINT; } if(!__dectect_encoding(file, &enc)) { goto FAILED_POINT; } if(enc == COM_TXT_BOM_ASCII) { byte_t tmp[1024]; size_t rn; cmBuffer_t *ascii_buf = Com_CreateBuffer(1024); do{ rn = fread((void*)tmp, 1, 1024, file); if(rn > 0) { Com_InsertBuffer(ascii_buf, tmp, rn); } }while(!feof(file) && !ferror(file)); if(ferror(file)) { status = TXT_READ_INVALID; }else { tmp[0] = '\0'; Com_InsertBuffer(ascii_buf, tmp, 1); status = TXT_READ_EOF; } if(status != TXT_READ_INVALID) { wchar_t *str; str = Com_str_convto_wcs(COM_CP_ACP, (const char*)Com_GetBufferData(ascii_buf), Com_GetBufferAvailable(ascii_buf)); if(!str) { status = TXT_READ_INVALID; } if(out && str) { Com_AppendString(out, str); } if(str) { Com_DEL(str); str = NULL; } if(ascii_buf) { Com_DestroyBuffer(ascii_buf); } ascii_buf = NULL; } }else { do{ status = __read_wchar(file, enc, &c); if(status == TXT_READ_OK && out) { Com_AppendCharToString(out, c); } }while(status == TXT_READ_OK); } if(status == TXT_READ_INVALID) { is_ok = false; goto FAILED_POINT; } if(bom) { *bom = enc; } FAILED_POINT: if(file) { fclose(file); file = NULL; } return is_ok; }