int memcache_get (struct connection *c, const char *key, int key_len) { if (verbosity >= 3) { fprintf (stderr, "memcache_get: key='%s'\n", key); } struct fuse_file_info fi; if (key_len >= 5 && !strncmp (key, "stats", 5)) { int stats_len = filesys_prepare_stats(c); return_one_key (c, key, value_buff, stats_len); return 0; } cmd_get++; if (key_len <= 0) { return 0; } int r = -11; int x; unsigned int offset, length; inode_id_t inode; switch (key[0]) { case 'c': if (!strncmp (key, "creat", 5) && sscanf (key, "creat%d", &x) >= 1 && !parse_path (c)) { r = ff_create (value_buff, x, &fi); if (!r) { return_one_long (c, key, fi.fh); } } break; case 'o': if (!strcmp (key, "open") && !parse_path (c)) { r = ff_open (value_buff, &fi); if (!r) { return_one_long (c, key, fi.fh); } } break; case 'r': if (!strncmp (key, "read", 4) && sscanf (key, "read%u,%u,%lld", &offset, &length, &inode) >= 3) { fi.fh = inode; r = ff_read (NULL, value_buff, length, offset, &fi); if (r >= 0) { return_one_key (c, key, value_buff, r); } } } free_tmp_buffers (c); return 0; }
/* Opens the file with the given NAME. Returns the new file if successful or a null pointer otherwise. Fails if no file named NAME exists, or if an internal memory allocation fails. */ struct file * filesys_open (const char *name) { struct dir *dir; struct inode *inode = NULL; char parsed_name[NAME_MAX + 1]; if (*name == NULL) { return NULL; } bool success = parse_path (name, &dir, parsed_name); if ((strlen (parsed_name) > NAME_MAX)) { dir_close (dir); return NULL; } if (success) { if (dir != NULL) dir_lookup (dir, parsed_name, &inode); dir_close (dir); } struct file *result = file_open (inode); return result; }
/* Creates a file named NAME with the given INITIAL_SIZE. Returns true if successful, false otherwise. Fails if a file named NAME already exists, or if internal memory allocation fails. */ bool filesys_create (const char *name, off_t initial_size) { block_sector_t inode_sector = 0; struct dir *dir; char parsed_name[NAME_MAX + 1]; if (*name == NULL || (strlen (name) > NAME_MAX)) { return false; } bool success = parse_path (name, &dir, parsed_name); if (!success) { return success; } struct inode *inode; success = (dir != NULL && free_map_allocate (1, &inode_sector) && inode_create (inode_sector, initial_size, false) && dir_add (dir, parsed_name, inode_sector) && dir_lookup (dir, ".", &inode)); if (!success && inode_sector != 0) free_map_release (inode_sector, 1); dir_close (dir); return success; }
int syscall_open(const char *path, int type, mode_t mode) { int i, drive; int curdir_handle, new_handle; char name_comp[13], conv_name[11], dir_path[501]; if (strlen(path) > 500) return ELONGPATH; parse_path(path, &drive, dir_path, name_comp); if (dir_path[0] != 0) { curdir_handle = open_path(drive, dir_path); if (curdir_handle < 0) return curdir_handle; // Error } else { curdir_handle = get_curdir_handle(drive); increment_ref_count(curdir_handle); } // Last file name component. if (convert_name(name_comp, conv_name) < 0) { close_dir(curdir_handle); return EINVALIDNAME; // Error } mode = mode & 0x3f; // Valid bits if (mode == 0) mode = 0x20; else mode = mode & (~(FTYPE_DIR | FTYPE_VOLUME)); if (type == 0) type = O_RDONLY; else type = type & (O_RDONLY | O_WRONLY | O_CREAT | O_RDWR | O_APPEND | O_TRUNC); // Clear invalid bits new_handle = open_file(curdir_handle, conv_name, type, mode); close_dir(curdir_handle); if (new_handle >= 0) { // Store this into task file handle table and return its index for (i=3; i<20; i++) { if (fileinf->fhandles[i] < 0) break; } if (i < 20) { fileinf->fhandles[i] = new_handle; new_handle = i; } else { close_file(new_handle); new_handle = ENO_FREE_FILE_TABLE_SLOT; } } return new_handle; // may be failure or success }
/* Opens the file with the given NAME. Returns the new file if successful or a null pointer otherwise. Fails if no file named NAME exists, or if an internal memory allocation fails. */ struct file * filesys_open (const char *name) { char *cp_name; char *file_name; struct dir *dir; struct inode *inode = NULL; cp_name = malloc( sizeof(char) * (strlen(name)+1) ); if(cp_name == NULL) return NULL; file_name = malloc( sizeof(char) * (strlen(name)+1) ); if(file_name == NULL){ free(cp_name); return NULL; } /* copy name to cp_name */ strlcpy(cp_name, name, strlen(name)+1 ); dir = parse_path (cp_name, file_name); if (dir != NULL) dir_lookup (dir, file_name, &inode); dir_close (dir); free(file_name); free(cp_name); return file_open (inode); }
metawriter_ptr metawriter_create(const boost::property_tree::ptree &pt) { metawriter_ptr writer; string type = get_attr<string>(pt, "type"); optional<string> properties = get_opt_attr<string>(pt, "default-output"); if (type == "json") { string file = get_attr<string>(pt, "file"); metawriter_json_ptr json = metawriter_json_ptr(new metawriter_json(properties, parse_path(file))); optional<boolean> output_empty = get_opt_attr<boolean>(pt, "output-empty"); if (output_empty) { json->set_output_empty(*output_empty); } optional<boolean> pixel_coordinates = get_opt_attr<boolean>(pt, "pixel-coordinates"); if (pixel_coordinates) { json->set_pixel_coordinates(*pixel_coordinates); } writer = json; } else if (type == "inmem") { metawriter_inmem_ptr inmem = metawriter_inmem_ptr(new metawriter_inmem(properties)); writer = inmem; } else { throw config_error(string("Unknown type '") + type + "'"); } return writer; }
void Path::set_path (const std::string& colon_path) { std::vector<symbol> result; parse_path (colon_path, result); set_path (result); }
void parse_cmdline(char *cmdline, videomode_t *mode, char *log, int *kms) { char *p = cmdline; char c = *p++; while( c ) { if( c == '-') { switch(*p++) { case 'b': radeon_benchmarking = 1; break; case 'l': p = parse_path(p, log); break; case 'm': p = parse_mode(p, mode); break; case 'n': *kms = 0; }; }; c = *p++; }; };
void LogAnalyzer::run() { // la_log(LOG_INFO, "loganalyzer starting: built " __DATE__ " " __TIME__); // signal(SIGHUP, sighup_handler); init_config(); if (_show_version_information) { show_version_information(); exit(0); } if (_do_list_analyzers) { list_analyzers(); exit(0); } expand_names_to_run(); if (_use_telem_forwarder) { return run_live_analysis(); } if (_paths == NULL) { usage(); exit(1); } output_style = Analyze::OUTPUT_JSON; if (output_style_string != NULL) { output_style = Analyze::OUTPUT_JSON; if (strieq(output_style_string, "json")) { output_style = Analyze::OUTPUT_JSON; } else if(strieq(output_style_string, "plain-text")) { output_style = Analyze::OUTPUT_PLAINTEXT; } else if(strieq(output_style_string, "brief")) { output_style = Analyze::OUTPUT_BRIEF; } else if(strieq(output_style_string, "html")) { output_style = Analyze::OUTPUT_HTML; } else { usage(); exit(1); } } if (forced_format_string != NULL) { if (strieq(forced_format_string, "tlog")) { _force_format = log_format_tlog; } else if(strieq(forced_format_string, "df")) { _force_format = log_format_df; } else if(strieq(forced_format_string, "log")) { _force_format = log_format_log; } else { usage(); exit(1); } } for (uint8_t i=0; i<_pathcount; i++) { parse_path(_paths[i]); } }
void config_drop_file (const char *path) { struct config_file *file = NULL; parse_path (path, FALSE, NULL, &file, NULL); if (file) drop_file (file); }
void as_environment::set_variable ( const tu_string &varname, const as_value &val, const array<with_stack_entry>& with_stack ) // Given a path to variable, set its value. { IF_VERBOSE_ACTION ( log_msg ( "-------------- %s = %s\n", varname.c_str(), val.to_string() ) ); //xxxxxxxxxx // Path lookup rigamarole. character *target = get_target(); tu_string path; tu_string var; if ( parse_path ( varname, &path, &var ) ) { target = cast_to<character> ( find_target ( path.c_str() ) ); if ( target ) { target->set_member ( var, val ); } } else { set_variable_raw ( varname, val, with_stack ); } }
void config_clean_section (const char *path) { struct config_file *file; struct config_section *section = NULL; parse_path (path, FALSE, NULL, &file, §ion); if (section) drop_section (file, section); }
void test_parse_path(void** state) { const char* path = "/this/is/the/whole/path"; char dir[FILENAME_MAX], file[FILENAME_MAX]; parse_path(path, dir, file); assert_true(!strcmp(dir, "/this/is/the/whole")); assert_true(!strcmp(file, "path")); }
int syscall_mkdir(const char *pathname, mode_t mode) { int i, j, drive, res; int curdir_handle; char name_comp[13], conv_name[11], dir_path[501]; if (strlen(pathname) > 500) return ELONGPATH; parse_path(pathname, &drive, dir_path, name_comp); if (name_comp[0] == 0 && strlen(dir_path) > 0) { // remove the last component if (dir_path[strlen(dir_path)-1] == '/' || dir_path[strlen(dir_path)-1] == '\\') dir_path[strlen(dir_path)-1] = 0; j = 13; for (i=strlen(dir_path); (j>=0 && i>=0 && dir_path[i] != '/' && dir_path[i] != '\\'); i--) name_comp[--j] = dir_path[i]; if (j<0) j = 0; // Long name. Incorrect results if (i == 0) // special case dir_path[1] = 0; // only root dir else dir_path[i] = 0; // replace last / with null char for (i=0; i<=12; i++) name_comp[i] = name_comp[j++]; } if (dir_path[0] != 0) { curdir_handle = open_path(drive, dir_path); if (curdir_handle < 0) return curdir_handle; // Error } else { curdir_handle = get_curdir_handle(drive); increment_ref_count(curdir_handle); } // Last new dir name component. if (name_comp[0] == 0) { close_dir(curdir_handle); return EDUPLICATE_ENTRY; } if (convert_name(name_comp, conv_name) < 0) { close_dir(curdir_handle); return EINVALIDNAME; // Error } res = make_dir(curdir_handle, conv_name); close_dir(curdir_handle); if (res == 1) return 0; // Success else return res; // failure }
void config_clean_key (const char *path) { struct config_file *file; struct config_section *section; struct config_key *key; key = parse_path (path, FALSE, NULL, &file, §ion); if (key) drop_key (file, section, key); }
int syscall_creat(const char *path, mode_t mode) { int i, drive; int curdir_handle, new_handle; char name_comp[13], conv_name[11], dir_path[501]; if (strlen(path) > 500) return ELONGPATH; parse_path(path, &drive, dir_path, name_comp); if (dir_path[0] != 0) { curdir_handle = open_path(drive, dir_path); if (curdir_handle < 0) return curdir_handle; // Error } else { curdir_handle = get_curdir_handle(drive); increment_ref_count(curdir_handle); } // Last file name component. if (convert_name(name_comp, conv_name) < 0) { close_dir(curdir_handle); return EINVALIDNAME; // Error } mode = mode & 0x3f; // Valid bits if (mode == 0) mode = 0x20; else mode = mode & (~(FTYPE_DIR | FTYPE_VOLUME)); new_handle = create_file(curdir_handle, conv_name, mode); close_dir(curdir_handle); for (i=3; i<20; i++) { if (fileinf->fhandles[i] < 0) break; } if (i < 20) { fileinf->fhandles[i] = new_handle; new_handle = i; } else { close_file(new_handle); new_handle = ENO_FREE_FILE_TABLE_SLOT; } return new_handle; // may be failure or success }
uri_t* parse_uri(char* uri_string, int len) { uri_t* uri = NULL; char* tmp_uri = NULL; char* uri_pointer = NULL; if(!(tmp_uri = (char*)calloc((strlen(uri_string) + 1), sizeof(char)))) { printf("Could not alloc memory for tmp (uri string)!\n"); return NULL; } memcpy(tmp_uri, uri_string, strlen(uri_string)); if(!(uri = (uri_t*)calloc(1, sizeof(uri_t)))) { printf("Could not alloc memory for uri structure!\n"); free(tmp_uri); return NULL; } uri_pointer = parse_scheme(uri, tmp_uri); if(strcmp(uri_pointer, uri_string) == 0) { uri_pointer = parse_path(uri, uri_pointer); if(uri_pointer != NULL) { uri_pointer = parse_frag(uri, uri_pointer); } } else { uri_pointer = parse_authority(uri, uri_pointer); uri_pointer = parse_path(uri, uri_pointer); if(uri_pointer != NULL) { uri_pointer = parse_frag(uri, uri_pointer); } } free(tmp_uri); return uri; }
/* Handles path command-line argument. Returns zero on successful handling, * otherwise non-zero is returned. */ static int handle_path_arg(const char arg[], int select, const char dir[], args_t *args) { if(!is_path_arg(arg)) { return 1; } if(args->lwin_path[0] != '\0') { parse_path(dir, arg, args->rwin_path); args->rwin_handle = !select; } else { parse_path(dir, arg, args->lwin_path); args->lwin_handle = !select; } return 0; }
/*unpack observer 1st parameter : main unpack interface that gives access to observer, stream and mounted fs*/ static int extract_entry( struct UnpackInterface* unpacker, TypeFlag type, const char* name, int entry_size ){ /*parse path and create directories recursively*/ ZRT_LOG( L_INFO, "type=%s, name=%s, entry_size=%d", STR_ARCH_ENTRY_TYPE(type), name, entry_size ); /*setup path parser observer *observers callback will be called for every paursed subdir extracted from full path*/ struct ParsePathObserver path_observer; path_observer.callback_parse = callback_parse; path_observer.anyobj = unpacker->observer->mounts; /*run path parser*/ int parsed_dir_count = parse_path( &path_observer, name ); ZRT_LOG(L_INFO, "parsed_dir_count=%d", parsed_dir_count ); if ( type == ETypeDir ){ create_dir_and_cache_name(name, strlen(name)); } else{ int out_fd = unpacker->observer->mounts->open(name, O_WRONLY | O_CREAT, S_IRWXU); if (out_fd < 0) { ZRT_LOG( L_ERROR, "create new file error, name=%s", name ); return -1; } int should_write = entry_size; int write_err = 0; /*read file by blocks*/ while (entry_size > 0) { int len = (*unpacker->stream_reader->read)( unpacker->stream_reader, block, sizeof(block) ); if (len != sizeof(block)) { ZRT_LOG(L_ERROR, "read error. current file can't be saved name=%s", name); return -1; } int wrote; if (entry_size > sizeof(block)) { WRITE_FILE( &wrote, &write_err, out_fd, block, sizeof(block) ); } else { WRITE_FILE( &wrote, &write_err, out_fd, block, entry_size ); } if ( write_err ){ return -1; } entry_size -= sizeof(block); } ZRT_LOG(L_SHORT, "%7d B saved : %s", should_write, name); unpacker->observer->mounts->close(out_fd); } return 0; }
// helper function void update_dirs(char *path, int isValid, int new_len, char *new_name, mode_t mode, struct utimbuf *ut) { char *names[DIR_ENT_NUM]; int depth = parse_path(path, names); int i, j, found; int curr_blk, start_blk = ROOT_START_BLK; dir_ent directory[DIR_ENT_NUM]; //printf("DEBUG: set_file_length begin\n"); //printf("DEBUG: depth: %d\n", depth); for(i = 0; i < depth; i ++) { found = -1; disk->ops->read(disk, start_blk * 2, 2, (void *) directory); //printf("DEBUG: new start:%s\n", directory[0].name); curr_blk = start_blk; for(j = 0; j < DIR_ENT_NUM; j ++) { if(directory[j].valid && strcmp(directory[j].name, names[i]) == 0) { found = j; start_blk = directory[j].start; break; } } } // for unlink a file if(!isValid) directory[found].valid = isValid; // for set new length for the file else{ if(new_len >= 0) directory[found].length = new_len; if(new_name != NULL) strcpy(directory[found].name, new_name); if(mode != -1) directory[found].mode = mode; //printf("DEBUG: curr_blk:%d\n", curr_blk); //printf("DEBUG: dir start:%d\n", directory[found].start); directory[found].mtime = time(NULL); if(ut != NULL) directory[found].mtime = ut->modtime; } disk->ops->write(disk, curr_blk*2, 2, (void *) directory); //printf("%d\n", result); //printf("DEBUG: set_file_length end\n"); }
void getfile_function(char * from_path,char * to_path) { int id = parse_path (from_path,files,folders); if (id > 0) { printf("Getting %s to %s\n",from_path,to_path); if (LIBMTP_Get_File_To_File(device, id, to_path, progress, NULL) != 0 ) { printf("\nError getting file from MTP device.\n"); LIBMTP_Dump_Errorstack(device); LIBMTP_Clear_Errorstack(device); } } }
static void serial_close_handler(const uint8_t **pp, struct ScratchProtocol *sp) { char path[50]; if (!parse_path(pp, sp, path, sizeof(path))) return; if (sp->callbacks->serial_close(path, sp->serial_context)) { native_message_append_str(sp->nm, "1"); } else { native_message_append_str(sp->nm, "0"); } }
static void config_set_raw (const char *path, char *raw_value) { struct config_key *key; struct config_file *file; key = parse_path (path, TRUE, NULL, &file, NULL); if (key) { if (key->value) g_free (key->value); key->value = raw_value; if (file) file->dirty = TRUE; } }
int syscall_unlink(const char *pathname) { int drive; int curdir_handle; char name_comp[13], conv_name[11], dir_path[501]; struct dir_entry dent; int err; if (strlen(pathname) > 500) return ELONGPATH; parse_path(pathname, &drive, dir_path, name_comp); if (dir_path[0] != 0) { curdir_handle = open_path(drive, dir_path); if (curdir_handle < 0) return curdir_handle; // Error } else { curdir_handle = get_curdir_handle(drive); increment_ref_count(curdir_handle); } // Last file name component. if (convert_name(name_comp, conv_name) < 0) { err = EINVALIDNAME; // Error } else if (find_entry(curdir_handle, conv_name, &dent) == 1) { // Check whether it is a deletable file if ((dent.attrib & (FTYPE_READONLY | FTYPE_DIR | FTYPE_VOLUME)) == 0) { delete_dir_entry(curdir_handle, conv_name); free_cluster_chain(dent.start_cluster); err = 0; } else { err = EACCESS; // Error } } else { err = EFILE_NOT_FOUND; } close_dir(curdir_handle); return err; }
/* Creates directory. True if successful. */ bool filesys_mkdir (const char *dir) { bool success = false; /*The string *dir cannot be empty.*/ if (*dir == NULL) { return success; } /* Locates the directory where the new directory should be created. */ struct dir *create_dir; char parsed_name[NAME_MAX + 1]; parse_path (dir, &create_dir, parsed_name); block_sector_t sector; /*Find a free sector for the directory.*/ if (!free_map_allocate (1, §or)) { return success; } success = dir_create (sector, 16); if (!success) { free_map_release (sector, 1); return success; } success = dir_add (create_dir, parsed_name, sector); if (!success) { free_map_release (sector, 1); return success; } /* Get the dir struct of the directory that is just created and add "." and "..". */ struct inode *inode = inode_open (sector); struct dir *new_dir = dir_open (inode); ASSERT (inode != NULL); ASSERT (new_dir != NULL); dir_add (new_dir, ".", sector); dir_add (new_dir, "..", create_dir->inode->sector); dir_close (new_dir); dir_close (create_dir); return success; }
/* * run the program */ int main() { // holds the directories in the path char path_dirs[ MAX_PATHS ][ MAX_PATH_LEN ]; int num_dirs = parse_path( path_dirs ); char line[ LINE_LEN ]; command_t cmd; // get input print_prompt(); read_cmd( line ); parse_cmd( line, &cmd ); // until we get a line that starts with exit or quit while( !cmd.argv[0] || ( strcmp( cmd.argv[0], "quit" ) && strcmp( cmd.argv[0], "exit" ) ) ) { if( cmd.argv[0] ) cmd.name = lookup_path( cmd.argv, path_dirs, num_dirs ); if( cmd.name && cmd.argc > 0 ) { // create the child pid_t child_pid = fork(); if( child_pid < 0 ) perror( "fork" ); else if( child_pid == 0 ) // child execv( cmd.name, cmd.argv ); else // parent { if( !cmd.concurrent ) { int status; waitpid( child_pid, &status, 0 ); } } } cleanup_cmd( &cmd ); // get input print_prompt(); read_cmd( line ); parse_cmd( line, &cmd ); } return 0; }
char *find_cmd(char *cmd) { char *path; if (!(path = getenv("PATH"))) { write (2, "Could not get the environment, abort\n", strlen("Could not get the environment, abort\n")); return (NULL); } if (!access(cmd, X_OK)) return cmd; return (parse_path(path, cmd)); }
static char *config_get_raw_with_default (const char *path, int *def) { struct config_file *file; struct config_key *key; char *val; key = parse_path (path, FALSE, &val, &file, NULL); if (key) { if (def) *def = FALSE; val = key->value; } else { if (def) *def = TRUE; } return val; }
static int theme_parse_full_path(const char *v, void *data) { int rc; char **p = (char **)data; char *np; if (theme_relative) { if (!strncmp(v, "blank:", 6) || !strncmp(v, "box:", 4) || !strncmp(v, "spr:", 4)) /* hack for special files*/ return parse_path(v, data); rc = parse_path(v, data); if (rc || !*p || !*p[0]) return rc; if (curtheme_loading && curtheme_loading->type == THEME_GAME) { np = getfilepath(curtheme_loading->path, *p); if (!*np) return -1; free(*p); *p = np; } return 0; } return parse_full_path(v, data); }
int test_readline() { int i; int len = 1; int pos = 0; char** list = malloc(sizeof(char**)*len); char* c = getenv("PATH"); list = parse_path(list, &len, &pos, c); for (i=0; i<pos; ++i) printf("%s\n", list[i]); return 0; }