fs::path filestorage::get_full_path(t_filestore file_type, const std::string &filename) { fs::path full_path = get_parent_path(file_type, filename); full_path += filename; switch (file_type) { case e_filestore_galaxy_wallet_PRV: { full_path += ".PRV"; break; } case e_filestore_galaxy_pub: { full_path += ".pub"; break; } case e_filestore_galaxy_sig: { full_path += ".sig"; break; } case e_filestore_local_path: { break; } } // _dbg3("full_path " << full_path); return full_path; }
int main(int argc, char* argv[]) { char caller_path[1024]; char message[2048]; int return_value=EXIT_ERROR; PinDialogInfo pindialog; // this struct contains all dialog objects gtk_init(&argc,&argv); // initialize gtk+ if(get_parent_path(caller_path, sizeof(caller_path)-2)>0) { if((argc==2) && (argv[1]!=NULL) && (strlen(argv[1])>0)) { snprintf(message,sizeof(message)-2,_MSG_(MSG_PLEASE_ENTER_PIN),caller_path,argv[1]); } else { fprintf(stderr,"Incorrect Parameter for <description of SPR>\n"); exit(EXIT_ERROR); } } else { fprintf(stderr,"Failed To Determine Parent Process. Aborting.\n"); exit(EXIT_ERROR); } // create new message dialog with CANCEL button in standard places, in center of user's screen /////////////////////////////////////////////////////////////////////////////////////////////////////////////////// pindialog.dialog=gtk_message_dialog_new(NULL,GTK_DIALOG_MODAL,GTK_MESSAGE_QUESTION,GTK_BUTTONS_NONE,message); gtk_dialog_set_default_response(GTK_DIALOG(pindialog.dialog),GTK_RESPONSE_OK); gtk_window_set_title(GTK_WINDOW(pindialog.dialog),_MSG_(MSG_PIN_CODE_REQUIRED)); gtk_window_set_position(GTK_WINDOW(pindialog.dialog), GTK_WIN_POS_CENTER); g_signal_connect (pindialog.dialog, "delete-event", G_CALLBACK (on_delete_event),&pindialog); // show all these widgets, and run the dialog as a modal dialog until it is closed by the user ////////////////////////////////////////////////////////////////////////////////////////////// gtk_widget_show_all(GTK_WIDGET(pindialog.dialog)); switch(gtk_dialog_run(GTK_DIALOG(pindialog.dialog))) { case GTK_RESPONSE_CANCEL: // if the use chose CANCEL printf("CANCEL\n"); // output CANCEL return_value=EXIT_OK; // and return OK (cancel is not an error) break; default: // otherwise printf("ERROR\n"); return_value=EXIT_ERROR; // output and return ERROR break; } // properly dispose of the dialog (which disposes of all it's children), and exit with specific return value ///////////////////////////////////////////////////////////////////////////////////////////////////////////// gtk_widget_destroy(pindialog.dialog); exit(return_value); }
// mknod static int mhdd_mknod(const char *path, mode_t mode, dev_t rdev) { mhdd_debug(MHDD_MSG, "mhdd_mknod: path = %s mode = %X\n", path, mode); int res, i; char *nod; char *parent = get_parent_path(path); if (!parent) { errno = ENOENT; return -errno; } int dir_id = find_path_id(parent); free(parent); if (dir_id == -1) { errno = ENOENT; return -errno; } for (i = 0; i < 2; i++) { if (i) { if ((dir_id = get_free_dir())<0) { errno = ENOSPC; return -errno; } create_parent_dirs(dir_id, path); } nod = create_path(mhdd.dirs[dir_id], path); if (S_ISREG(mode)) { res = open(nod, O_CREAT | O_EXCL | O_WRONLY, mode); if (res >= 0) res = close(res); } else if (S_ISFIFO(mode)) res = mkfifo(nod, mode); else res = mknod(nod, mode, rdev); if (res != -1) { if (getuid() == 0) { struct fuse_context * fcontext = fuse_get_context(); chown(nod, fcontext->uid, fcontext->gid); } free(nod); return 0; } free(nod); if (errno != ENOSPC) return -errno; } return -errno; }
// mkdir static int mhdd_mkdir(const char * path, mode_t mode) { mhdd_debug(MHDD_MSG, "mhdd_mkdir: %s mode = %04X\n", path, mode); if (find_path_id(path) != -1) { errno = EEXIST; return -errno; } char *parent = get_parent_path(path); if (!parent) { errno = EFAULT; return -errno; } if (find_path_id(parent) == -1) { free(parent); errno = EFAULT; return -errno; } free(parent); int dir_id = get_free_dir(); if (dir_id<0) { errno = ENOSPC; return -errno; } create_parent_dirs(dir_id, path); char *name = create_path(mhdd.dirs[dir_id], path); if (mkdir(name, mode) == 0) { if (getuid() == 0) { struct stat st; gid_t gid = fuse_get_context()->gid; if (lstat(name, &st) == 0) { /* parent directory is SGID'ed */ if (st.st_gid != getgid()) gid = st.st_gid; } chown(name, fuse_get_context()->uid, gid); } free(name); return 0; } free(name); return -errno; }
SCM scm_mmr_create_this_path(SCM path ,SCM mode) #define FUNC_NAME "create-this-path" { char *p = NULL; char *b = NULL; char *buf = NULL; SCM ret = SCM_BOOL_F; int m = 0777; int len = 0; int n = 0; SCM_VALIDATE_STRING(1 ,path); if(!SCM_UNBNDP(mode)) { SCM_VALIDATE_NUMBER(2 ,mode); m = scm_to_int(mode); } scm_dynwind_begin(0); p = scm_to_locale_string(path); scm_dynwind_free(p); len = strlen(p); buf = (char*)malloc(len+1); // Don't forget +1 for '\0' n = get_path_levels(p ,len); while(n >= 0) { int l = 0; b = get_parent_path(p ,&n ,len); l = b-p; memcpy(buf ,b ,l); buf[l+1] = '\0'; do_create(buf ,len); } free(buf); buf = NULL; scm_dynwind_end(); return ret; }
// symlink static int mhdd_symlink(const char *from, const char *to) { mhdd_debug(MHDD_MSG, "mhdd_symlink: from = %s to = %s\n", from, to); int i, res; char *parent = get_parent_path(to); if (!parent) { errno = ENOENT; return -errno; } int dir_id = find_path_id(parent); free(parent); if (dir_id == -1) { errno = ENOENT; return -errno; } for (i = 0; i < 2; i++) { if (i) { if ((dir_id = get_free_dir()) < 0) { errno = ENOSPC; return -errno; } create_parent_dirs(dir_id, to); } char *path_to = create_path(mhdd.dirs[dir_id], to); res = symlink(from, path_to); free(path_to); if (res == 0) return 0; if (errno != ENOSPC) return -errno; } return -errno; }
// main program /////////////////////////////////////////////////////////////////////////////////////////////////// int main(int argc, char* argv[]) { int return_value=EXIT_ERROR; PinDialogInfo pindialog; char caller_path[1024]; gtk_init(&argc,&argv); // initialize gtk+ // create new message dialog with CANCEL and OK buttons in standard places, in center of user's screen /////////////////////////////////////////////////////////////////////////////////////////////////////////////////// if(get_parent_path(caller_path, sizeof(caller_path)-2)>0) { pindialog.dialog=gtk_message_dialog_new(NULL,GTK_DIALOG_MODAL,GTK_MESSAGE_QUESTION,GTK_BUTTONS_NONE,_MSG_(MSG_PLEASE_ENTER_OLD_AND_NEW_PINS), caller_path); } else { fprintf(stderr,"Failed To Determine Parent Process. Aborting.\n"); exit(EXIT_ERROR); } pindialog.cancelbutton =GTK_BUTTON(gtk_dialog_add_button(GTK_DIALOG(pindialog.dialog),GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL)); pindialog.okbutton =GTK_BUTTON(gtk_dialog_add_button(GTK_DIALOG(pindialog.dialog),GTK_STOCK_OK, GTK_RESPONSE_OK)); gtk_dialog_set_default_response(GTK_DIALOG(pindialog.dialog),GTK_RESPONSE_OK); gtk_window_set_title(GTK_WINDOW(pindialog.dialog),_MSG_(MSG_CHANGE_PIN_CODE)); gtk_window_set_position(GTK_WINDOW(pindialog.dialog), GTK_WIN_POS_CENTER); g_signal_connect (pindialog.dialog, "delete-event", G_CALLBACK (on_delete_event),&pindialog); // create original, new, and verify new pin entry fields with labels, in a table /////////////////////////////////////////////////////////////////////////////////////////////////////////// pindialog.newPinsTable =gtk_table_new(3,2,TRUE); // table of 4 rows, 3 columns pindialog.originalPinLabel =gtk_label_new(_MSG_(MSG_CURRENT_PIN)); pindialog.newPin0Label =gtk_label_new(_MSG_(MSG_NEW_PIN)); pindialog.newPin1Label =gtk_label_new(_MSG_(MSG_NEW_PIN_AGAIN)); pindialog.originalPinEntry =gtk_entry_new(); pindialog.newPin0Entry =gtk_entry_new(); pindialog.newPin1Entry =gtk_entry_new(); // set max lengths gtk_entry_set_max_length(GTK_ENTRY(pindialog.originalPinEntry), MAX_PIN_LENGTH); gtk_entry_set_max_length(GTK_ENTRY(pindialog.newPin0Entry), MAX_PIN_LENGTH); gtk_entry_set_max_length(GTK_ENTRY(pindialog.newPin1Entry), MAX_PIN_LENGTH); // disable visibilities gtk_entry_set_visibility(GTK_ENTRY(pindialog.originalPinEntry), FALSE); gtk_entry_set_visibility(GTK_ENTRY(pindialog.newPin0Entry), FALSE); gtk_entry_set_visibility(GTK_ENTRY(pindialog.newPin1Entry), FALSE); // put labels and entries in a table gtk_table_attach(GTK_TABLE(pindialog.newPinsTable),pindialog.originalPinLabel, 0,1,0,1,(GtkAttachOptions)(GTK_SHRINK | GTK_FILL),(GtkAttachOptions)(GTK_SHRINK | GTK_FILL),2,2); gtk_table_attach(GTK_TABLE(pindialog.newPinsTable),pindialog.newPin0Label, 0,1,1,2,(GtkAttachOptions)(GTK_SHRINK | GTK_FILL),(GtkAttachOptions)(GTK_SHRINK | GTK_FILL),2,2); gtk_table_attach(GTK_TABLE(pindialog.newPinsTable),pindialog.newPin1Label, 0,1,2,3,(GtkAttachOptions)(GTK_SHRINK | GTK_FILL),(GtkAttachOptions)(GTK_SHRINK | GTK_FILL),2,2); gtk_table_attach(GTK_TABLE(pindialog.newPinsTable),pindialog.originalPinEntry, 1,2,0,1,(GtkAttachOptions)(GTK_SHRINK | GTK_FILL),(GtkAttachOptions)(GTK_SHRINK | GTK_FILL),2,2); gtk_table_attach(GTK_TABLE(pindialog.newPinsTable),pindialog.newPin0Entry, 1,2,1,2,(GtkAttachOptions)(GTK_SHRINK | GTK_FILL),(GtkAttachOptions)(GTK_SHRINK | GTK_FILL),2,2); gtk_table_attach(GTK_TABLE(pindialog.newPinsTable),pindialog.newPin1Entry, 1,2,2,3,(GtkAttachOptions)(GTK_SHRINK | GTK_FILL),(GtkAttachOptions)(GTK_SHRINK | GTK_FILL),2,2); // connect signals to filter and read inputs g_signal_connect(pindialog.originalPinEntry, "insert_text", G_CALLBACK(insert_only_digits), (gpointer)&pindialog); g_signal_connect(pindialog.newPin0Entry, "insert_text", G_CALLBACK(insert_only_digits), (gpointer)&pindialog); g_signal_connect(pindialog.newPin1Entry, "insert_text", G_CALLBACK(insert_only_digits), (gpointer)&pindialog); g_signal_connect(pindialog.originalPinEntry, "changed", G_CALLBACK(pins_changed), (gpointer)&pindialog); g_signal_connect(pindialog.newPin0Entry, "changed", G_CALLBACK(pins_changed), (gpointer)&pindialog); g_signal_connect(pindialog.newPin1Entry, "changed", G_CALLBACK(pins_changed), (gpointer)&pindialog); // add all these objects to the dialog /////////////////////////////////////////////////////////////////////////////////////////////////////////// gtk_container_set_border_width(GTK_CONTAINER(pindialog.dialog),10); gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(pindialog.dialog))),pindialog.newPinsTable, TRUE, TRUE,2); // initial state for OK button ///////////////////////////////////////////////////////////////////////////////////////////////////////// update_ok_button(&pindialog); // show all these widgets, and run the dialog as a modal dialog until it is closed by the user ////////////////////////////////////////////////////////////////////////////////////////////// gtk_widget_show_all(GTK_WIDGET(pindialog.dialog)); switch(gtk_dialog_run(GTK_DIALOG(pindialog.dialog))) { case GTK_RESPONSE_OK: // if the user chose OK { const char* oldpin=gtk_entry_get_text(GTK_ENTRY(pindialog.originalPinEntry)); const char* newpin=gtk_entry_get_text(GTK_ENTRY(pindialog.newPin0Entry)); printf("%s:%s\n",oldpin,newpin); // output the PINs to stdout return_value=EXIT_OK; // and return OK } break; default: // otherwise printf("CANCEL\n"); return_value=EXIT_OK; // output CANCEL and return ok (cancel is not an error) break; } // properly dispose of the dialog (which disposes of all it's children), and exit with specific return value ///////////////////////////////////////////////////////////////////////////////////////////////////////////// gtk_widget_destroy(pindialog.dialog); exit(return_value); }
static void do_nla(Scene *scene, Object *ob, int blocktype) { bPose *tpose= NULL; Key *key= NULL; ListBase tchanbase={NULL, NULL}, chanbase={NULL, NULL}; bActionStrip *strip, *striplast=NULL, *stripfirst=NULL; float striptime, frametime, length, actlength; float blendfac, stripframe; float scene_cfra= BKE_curframe(scene); int doit, dostride; if(blocktype==ID_AR) { copy_pose(&tpose, ob->pose, 1); rest_pose(ob->pose); // potentially destroying current not-keyed pose } else { key= ob_get_key(ob); } /* check on extend to left or right, when no strip is hit by 'cfra' */ for (strip=ob->nlastrips.first; strip; strip=strip->next) { /* escape loop on a hit */ if( scene_cfra >= strip->start && scene_cfra <= strip->end + 0.1f) /* note 0.1 comes back below */ break; if(scene_cfra < strip->start) { if(stripfirst==NULL) stripfirst= strip; else if(stripfirst->start > strip->start) stripfirst= strip; } else if(scene_cfra > strip->end) { if(striplast==NULL) striplast= strip; else if(striplast->end < strip->end) striplast= strip; } } if(strip==NULL) { /* extend */ if(striplast) scene_cfra= striplast->end; else if(stripfirst) scene_cfra= stripfirst->start; } /* and now go over all strips */ for (strip=ob->nlastrips.first; strip; strip=strip->next){ doit=dostride= 0; if (strip->act && !(strip->flag & ACTSTRIP_MUTE)) { /* so theres an action */ /* Determine if the current frame is within the strip's range */ length = strip->end-strip->start; actlength = strip->actend-strip->actstart; striptime = (scene_cfra-(strip->start)) / length; stripframe = (scene_cfra-(strip->start)) ; if (striptime>=0.0){ if(blocktype==ID_AR) rest_pose(tpose); /* To handle repeat, we add 0.1 frame extra to make sure the last frame is included */ if (striptime < 1.0f + 0.1f/length) { /* Handle path */ if ((strip->flag & ACTSTRIP_USESTRIDE) && (blocktype==ID_AR) && (ob->ipoflag & OB_DISABLE_PATH)==0){ Object *parent= get_parent_path(ob); if (parent) { Curve *cu = parent->data; float ctime, pdist; if (cu->flag & CU_PATH){ /* Ensure we have a valid path */ if(cu->path==NULL || cu->path->data==NULL) makeDispListCurveTypes(scene, parent, 0); if(cu->path) { /* Find the position on the path */ ctime= bsystem_time(scene, ob, scene_cfra, 0.0); if(calc_ipo_spec(cu->ipo, CU_SPEED, &ctime)==0) { /* correct for actions not starting on zero */ ctime= (ctime - strip->actstart)/cu->pathlen; CLAMP(ctime, 0.0, 1.0); } pdist = ctime*cu->path->totdist; if(tpose && strip->stridechannel[0]) { striptime= stridechannel_frame(parent, ob->size[0], strip, cu->path, pdist, tpose->stride_offset); } else { if (strip->stridelen) { striptime = pdist / strip->stridelen; striptime = (float)fmod (striptime+strip->actoffs, 1.0); } else striptime = 0; } frametime = (striptime * actlength) + strip->actstart; frametime= bsystem_time(scene, ob, frametime, 0.0); if(blocktype==ID_AR) { extract_pose_from_action (tpose, strip->act, frametime); } else if(blocktype==ID_OB) { extract_ipochannels_from_action(&tchanbase, &ob->id, strip->act, "Object", frametime); if(key) extract_ipochannels_from_action(&tchanbase, &key->id, strip->act, "Shape", frametime); } doit=dostride= 1; } } } } /* To handle repeat, we add 0.1 frame extra to make sure the last frame is included */ else { /* Mod to repeat */ if(strip->repeat!=1.0f) { float cycle= striptime*strip->repeat; striptime = (float)fmod (cycle, 1.0f + 0.1f/length); cycle-= striptime; if(blocktype==ID_AR) cyclic_offs_bone(ob, tpose, strip, cycle); } frametime = (striptime * actlength) + strip->actstart; frametime= nla_time(scene, frametime, (float)strip->repeat); if(blocktype==ID_AR) { extract_pose_from_action (tpose, strip->act, frametime); } else if(blocktype==ID_OB) { extract_ipochannels_from_action(&tchanbase, &ob->id, strip->act, "Object", frametime); if(key) extract_ipochannels_from_action(&tchanbase, &key->id, strip->act, "Shape", frametime); } doit=1; } } /* Handle extend */ else { if (strip->flag & ACTSTRIP_HOLDLASTFRAME){ /* we want the strip to hold on the exact fraction of the repeat value */ frametime = actlength * (strip->repeat-(int)strip->repeat); if(frametime<=0.000001f) frametime= actlength; /* rounding errors... */ frametime= bsystem_time(scene, ob, frametime+strip->actstart, 0.0); if(blocktype==ID_AR) extract_pose_from_action (tpose, strip->act, frametime); else if(blocktype==ID_OB) { extract_ipochannels_from_action(&tchanbase, &ob->id, strip->act, "Object", frametime); if(key) extract_ipochannels_from_action(&tchanbase, &key->id, strip->act, "Shape", frametime); } /* handle cycle hold */ if(strip->repeat!=1.0f) { if(blocktype==ID_AR) cyclic_offs_bone(ob, tpose, strip, strip->repeat-1.0f); } doit=1; } } /* Handle blendin & blendout */ if (doit){ /* Handle blendin */ if (strip->blendin>0.0 && stripframe<=strip->blendin && scene_cfra>=strip->start){ blendfac = stripframe/strip->blendin; } else if (strip->blendout>0.0 && stripframe>=(length-strip->blendout) && scene_cfra<=strip->end){ blendfac = (length-stripframe)/(strip->blendout); } else blendfac = 1; if(blocktype==ID_AR) {/* Blend this pose with the accumulated pose */ /* offset bone, for matching cycles */ blend_pose_offset_bone (strip, ob->pose, tpose, blendfac, strip->mode); blend_poses (ob->pose, tpose, blendfac, strip->mode); if(dostride) blend_pose_strides (ob->pose, tpose, blendfac, strip->mode); } else { blend_ipochannels(&chanbase, &tchanbase, blendfac, strip->mode); BLI_freelistN(&tchanbase); } } } } } if(blocktype==ID_OB) { execute_ipochannels(&chanbase); } else if(blocktype==ID_AR) { /* apply stride offset to object */ add_v3_v3(ob->obmat[3], ob->pose->stride_offset); } /* free */ if (tpose) free_pose(tpose); if(chanbase.first) BLI_freelistN(&chanbase); }
int main(int argc, char *argv[]) { if (argc != 3) { printf("Usage: ext2_mkdir <virtual_disk> <virtual_path>\n"); exit(1); } int fd = open(argv[1], O_RDWR); disk = mmap(NULL, 128 * 1024, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if(disk == MAP_FAILED) { perror("mmap"); exit(1); } struct ext2_inode *parent = inode_get(get_parent_path(argv[2]), disk); if(!parent) { printf("No such path exists\n"); exit(ENOENT); } if(inode_get(argv[2], disk)) { printf("Directory/File with that name already exists\n"); exit(EEXIST); } //get and set inode unsigned int new_inode_num = first_free_inode(disk); add_inode_bitmap(new_inode_num, disk); struct ext2_group_desc *gd = (struct ext2_group_desc *)(disk + (2 * EXT2_BLOCK_SIZE)); struct ext2_inode *inode_table = (struct ext2_inode *)(disk + (gd->bg_inode_table * EXT2_BLOCK_SIZE)); struct ext2_inode *new_inode = &(inode_table[new_inode_num - 1]); new_inode->i_mode = EXT2_S_IFDIR; //give it a block new_inode->i_size = EXT2_BLOCK_SIZE; new_inode->i_links_count = 2; new_inode->i_blocks = 2; unsigned int new_block_num = first_free_block(disk); add_block_bitmap(new_block_num, disk); new_inode->i_block[0] = new_block_num; int i; for(i = 1; i < 15; i++) { new_inode->i_block[i] = 0; } //set first dir_entry to itself struct ext2_dir_entry_2 *new_dir_entry = (struct ext2_dir_entry_2 *)(disk + (new_block_num * EXT2_BLOCK_SIZE)); new_dir_entry->inode = new_inode_num; new_dir_entry->rec_len = sizeof(struct ext2_dir_entry_2) + 4; new_dir_entry->name_len = 1; new_dir_entry->file_type = EXT2_FT_DIR; strncpy(new_dir_entry->name, ".", 1); //set another dir_entry to = parent new_dir_entry = (struct ext2_dir_entry_2 *)((char *)new_dir_entry + new_dir_entry->rec_len); new_dir_entry->inode = ((struct ext2_dir_entry_2 *)(disk + (parent->i_block[0] * EXT2_BLOCK_SIZE)))->inode; new_dir_entry->rec_len = EXT2_BLOCK_SIZE - (sizeof(struct ext2_dir_entry_2) + 4); new_dir_entry->name_len = 2; new_dir_entry->file_type = EXT2_FT_DIR; strncpy(new_dir_entry->name, "..", 2); parent->i_links_count++; //find empty dir_entry in parent and set unsigned int total_size; struct ext2_dir_entry_2 *prev_entry; char *sourceName = &(argv[2][strlen(get_parent_path(argv[2]))]); for(i = 0; i < 12 && parent->i_block[i]; i++) { prev_entry = (struct ext2_dir_entry_2 *)(disk + (parent->i_block[i] * EXT2_BLOCK_SIZE)); total_size = EXT2_BLOCK_SIZE; while(total_size) { int rounding_error = 0; if(strlen(sourceName) % 4) { rounding_error += 4 - (strlen(sourceName) % 4); } if(prev_entry->name_len % 4) { rounding_error += 4 - (prev_entry->name_len % 4); } if(prev_entry->rec_len >= (2 * sizeof(struct ext2_dir_entry_2)) + strlen(sourceName) + prev_entry->name_len + rounding_error) { break; } total_size -= prev_entry->rec_len; prev_entry = (struct ext2_dir_entry_2 *)((char *)prev_entry + prev_entry->rec_len); } if(total_size) { break; } } if(parent->i_block[i]) { unsigned int prev_size = sizeof(struct ext2_dir_entry_2) + prev_entry->name_len; if(prev_entry->name_len % 4) { prev_size += 4 - (prev_entry->name_len % 4); } new_dir_entry = (struct ext2_dir_entry_2 *)((char *)prev_entry + prev_size); new_dir_entry->inode = new_inode_num; new_dir_entry->rec_len = prev_entry->rec_len - prev_size; new_dir_entry->name_len = strlen(sourceName); new_dir_entry->file_type = EXT2_FT_DIR; strncpy(new_dir_entry->name, sourceName, new_dir_entry->name_len); prev_entry->rec_len = prev_size; } else { parent->i_block[i] = first_free_block(disk); add_block_bitmap(parent->i_block[i], disk); new_dir_entry = (struct ext2_dir_entry_2 *)(disk + (parent->i_block[i] * EXT2_BLOCK_SIZE)); new_dir_entry->inode = new_inode_num; new_dir_entry->rec_len = EXT2_BLOCK_SIZE; new_dir_entry->name_len = strlen(sourceName); new_dir_entry->file_type = EXT2_FT_DIR; strncpy(new_dir_entry->name, sourceName, new_dir_entry->name_len); parent->i_size += EXT2_BLOCK_SIZE; parent->i_blocks += 2; } return 0; }
int create_parent_dirs(int dir_id, const char *path) { mhdd_debug(MHDD_DEBUG, "create_parent_dirs: dir_id=%d, path=%s\n", dir_id, path); char *parent=get_parent_path(path); if (!parent) return 0; char *exists=find_path(parent); if (!exists) { free(parent); errno=EFAULT; return -errno; } char *path_parent=create_path(mhdd.dirs[dir_id], parent); struct stat st; // already exists if (stat(path_parent, &st)==0) { free(exists); free(path_parent); free(parent); return 0; } // create parent dirs int res=create_parent_dirs(dir_id, parent); if (res!=0) { free(path_parent); free(parent); free(exists); return res; } // get stat from exists dir if (stat(exists, &st)!=0) { free(exists); free(path_parent); free(parent); return -errno; } res=mkdir(path_parent, st.st_mode); if (res==0) { chown(path_parent, st.st_uid, st.st_gid); chmod(path_parent, st.st_mode); } else { res=-errno; mhdd_debug(MHDD_DEBUG, "create_parent_dirs: can not create dir %s: %s\n", path_parent, strerror(errno)); } #ifndef WITHOUT_XATTR // copy extended attributes of parent dir if (copy_xattrs(exists, path_parent) == -1) mhdd_debug(MHDD_MSG, "copy_xattrs: error copying xattrs from %s to %s\n", exists, path_parent); #endif free(exists); free(path_parent); free(parent); return res; }
// rename static int mhdd_rename(const char *from, const char *to) { mhdd_debug(MHDD_MSG, "mhdd_rename: from = %s to = %s\n", from, to); int i, res; struct stat sto, sfrom; char *obj_from, *obj_to; int from_is_dir = 0, to_is_dir = 0, from_is_file = 0, to_is_file = 0; int to_dir_is_empty = 1, to_is_link = 0; if (strcmp(from, to) == 0) return 0; /* seek for possible errors */ for (i = 0; i < mhdd.cdirs; i++) { obj_to = create_path(mhdd.dirs[i], to); obj_from = create_path(mhdd.dirs[i], from); if (stat(obj_to, &sto) == 0) { if (S_ISDIR(sto.st_mode)) { to_is_dir++; if (!dir_is_empty(obj_to)) to_dir_is_empty = 0; } else to_is_file++; if (lstat(obj_to, &sto) == 0) { if ((sto.st_mode & S_IFMT) == S_IFLNK) to_is_link++; } } if (stat(obj_from, &sfrom) == 0) { if (S_ISDIR (sfrom.st_mode)) from_is_dir++; else from_is_file++; } free(obj_from); free(obj_to); if (to_is_file && from_is_dir) return -ENOTDIR; if (to_is_file && to_is_dir) return -ENOTEMPTY; if (from_is_dir && !to_dir_is_empty && !to_is_link) return -ENOTEMPTY; } /* parent 'to' path doesn't exists */ char *pto = get_parent_path (to); if (find_path_id(pto) == -1) { free (pto); return -ENOENT; } free (pto); /* rename cycle */ for (i = 0; i < mhdd.cdirs; i++) { obj_to = create_path(mhdd.dirs[i], to); obj_from = create_path(mhdd.dirs[i], from); if (lstat(obj_from, &sfrom) == 0) { /* if from is dir and at the same time file, we only rename dir, using lstat because we should handle symlinks here as well */ if (from_is_dir && from_is_file) { if (!S_ISDIR(sfrom.st_mode)) { free(obj_from); free(obj_to); continue; } } create_parent_dirs(i, to); mhdd_debug(MHDD_MSG, "mhdd_rename: rename %s -> %s\n", obj_from, obj_to); res = rename(obj_from, obj_to); if (res == -1) { free(obj_from); free(obj_to); return -errno; } } else { /* from and to are files, so we must remove to files */ if (from_is_file && to_is_file && !from_is_dir) { if (stat(obj_to, &sto) == 0) { mhdd_debug(MHDD_MSG, "mhdd_rename: unlink %s\n", obj_to); if (unlink(obj_to) == -1) { free(obj_from); free(obj_to); return -errno; } } } } free(obj_from); free(obj_to); } return 0; }
// rename static int mhdd_rename(const char *from, const char *to) { mhdd_debug(MHDD_MSG, "mhdd_rename: from = %s to = %s\n", from, to); int i, res; struct stat sto, sfrom; char *obj_from, *obj_to; int from_is_dir = 0, to_is_dir = 0, from_is_file = 0, to_is_file = 0; int to_dir_is_empty = 1; if (strcmp(from, to) == 0) return 0; /* seek for possible errors */ for (i = 0; i < mhdd.cdirs; i++) { obj_to = create_path(mhdd.dirs[i], to); obj_from = create_path(mhdd.dirs[i], from); if (stat(obj_to, &sto) == 0) { if (S_ISDIR(sto.st_mode)) { to_is_dir++; if (!dir_is_empty(obj_to)) to_dir_is_empty = 0; } else to_is_file++; } if (stat(obj_from, &sfrom) == 0) { if (S_ISDIR (sfrom.st_mode)) from_is_dir++; else from_is_file++; } free(obj_from); free(obj_to); if (to_is_file && from_is_dir) return -ENOTDIR; if (to_is_file && to_is_dir) return -ENOTEMPTY; if (from_is_dir && !to_dir_is_empty) return -ENOTEMPTY; } /* parent 'to' path doesn't exists */ char *pto = get_parent_path (to); if (find_path_id(pto) == -1) { free (pto); return -ENOENT; } free (pto); int *renamed_on = calloc(mhdd.cdirs, sizeof(int)); if (!renamed_on) return -ENOMEM; /* rename first, then unlink, so we never see a nonexistent file */ for (i = 0; i < mhdd.cdirs; i++) { obj_to = create_path(mhdd.dirs[i], to); obj_from = create_path(mhdd.dirs[i], from); if (stat(obj_from, &sfrom) == 0) { /* if from is dir and at the same time file, we only rename dir */ if (from_is_dir && from_is_file) { if (!S_ISDIR(sfrom.st_mode)) { free(obj_from); free(obj_to); continue; } } create_parent_dirs(i, to); mhdd_debug(MHDD_MSG, "mhdd_rename: rename %s -> %s\n", obj_from, obj_to); res = rename(obj_from, obj_to); if (res == -1) { free(obj_from); free(obj_to); free(renamed_on); return -errno; } renamed_on[i] = 1; } free(obj_from); free(obj_to); } /* now unlink */ for (i = 0; i < mhdd.cdirs; i++) { /* don't delete if we already renamed. */ if (renamed_on[i]) continue; obj_to = create_path(mhdd.dirs[i], to); obj_from = create_path(mhdd.dirs[i], from); if (stat(obj_from, &sfrom) != 0) { /* from and to are files, so we must remove to files */ if (from_is_file && to_is_file && !from_is_dir) { if (stat(obj_to, &sto) == 0) { mhdd_debug(MHDD_MSG, "mhdd_rename: unlink %s\n", obj_to); if (unlink(obj_to) == -1) { free(obj_from); free(obj_to); return -errno; } } } } free(obj_from); free(obj_to); } free(renamed_on); return 0; }