int caching_list_directory(const char *path, dir_entry **list) { pthread_mutex_lock(&dmut); if (!strcmp(path, "/")) path = ""; dir_cache *cw; for (cw = dcache; cw; cw = cw->next) if (!strcmp(cw->path, path)) break; if (!cw) { if (!list_directory(path, list)) return 0; cw = new_cache(path); } else if (cache_timeout > 0 && (time(NULL) - cw->cached > cache_timeout)) { if (!list_directory(path, list)) return 0; free_dir_list(cw->entries); cw->cached = time(NULL); } else *list = cw->entries; cw->entries = *list; pthread_mutex_unlock(&dmut); return 1; }
int list_directory(char * directory ){ struct direct **files; struct stat statbuf ; int filter_dots(); int count = scandir(directory, &files, filter_dots, alphasort); //int count = get_files(top_dir,files); int i = 0; char * current_file; if(count == 0){ printf("<directory name=\"%s\"/>\n", directory); } else { printf("<directory name=\"%s\">\n", directory); chdir(directory); for (i=0;i<count;i++){ current_file = files[i]->d_name ; int e = stat(current_file , &statbuf); if(e != 0){ perror("e"); } //if directory if(S_ISDIR((&statbuf)->st_mode)){ list_directory(current_file ); } else printf("<file>%s</file>\n",current_file); } chdir("../"); printf("</directory>\n"); } }
static void run_collect(COLLECT_CALLBACK_INFO *info, const char *input) { char dir[1024]; int dirlen = 0; /* get the directory */ path_directory(input, dir, sizeof(dir)); dirlen = strlen(dir); info->path_len = dirlen+1; /* set the start string */ if(dirlen) info->start_str = input + dirlen + 1; else info->start_str = input; for(info->start_len = 0; info->start_str[info->start_len]; info->start_len++) { if(info->start_str[info->start_len] == '*') break; } /* set the end string */ if(info->start_str[info->start_len]) info->end_str = info->start_str + info->start_len + 1; else info->end_str = info->start_str + info->start_len; info->end_len = strlen(info->end_str); /* search the path */ list_directory(dir, collect_callback, info); }
samrpc_result_t * samrpc_list_directory_6_svr( file_restrictions_more_arg_t *arg, struct svc_req *req /* ARGSUSED */ ) { int ret = -1; sqm_lst_t *lst = NULL; Trace(TR_DEBUG, "List directory max[%d], filepath[%s], restrictions[%s]", arg->maxentries, arg->dir, arg->restrictions); /* free previous result */ xdr_free(xdr_samrpc_result_t, (char *)&rpc_result); SAMRPC_CHECK_TIMESTAMP(arg->ctx->handle->timestamp); Trace(TR_DEBUG, "Calling native library to list dir"); ret = list_directory( arg->ctx, arg->maxentries, arg->dir, arg->file, arg->restrictions, arg->morefiles, &lst); SAMRPC_SET_RESULT(ret, SAM_STRING_LIST, lst); Trace(TR_DEBUG, "List directory files return [%d] with [%d] entries", ret, (lst != NULL) ? lst->length : -1); return (&rpc_result); }
int cmd_ls_exec(CLI *cli, char *argv[]) { int argc = cli_num_args(argv); char path[PATH_MAX]; if (argc > 2) { printf("Too many arguments to 'ls'\n"); return (-1); } if (argc < 2) { cli_clean_path(cli, NULL, path); // cwd } else { cli_clean_path(cli, argv[1], path); } if (!fl_is_dir(path)) { printf("Invalid directory '%s'\n", argv[1]); return (-1); } list_directory(cli, path); return (0); }
int main(int argc, char *argv[]){ while(global_args_t_init(global_args) != 0); if(read_args(argc, argv) == -1){print_usage(argv); return -1; } if(global_args.i_num == 1){print_info(argc, argv);} else if(global_args.l_num == 1){list_directory(argc, argv);} else if(global_args.r_num == 1 && global_args.o_num == 1){recover_file(argc, argv);} else if(global_args.x_num == 1){cleanse_file(argc, argv);} return 0; }
int main( int argc, char *argv[]) { int i; /* For each directory in command line */ i = 1; while (i < argc) { list_directory (argv[i]); i++; } /* List current working directory if no arguments on command line */ if (argc == 1) { list_directory ("."); } return EXIT_SUCCESS; }
static FilenameList io_directory_subdirectories(const char* path) { FilenameList list; DirListing listing = list_directory(path); for (int i = 0; i < listing.size(); i++) { if (listing[i].is_directory) { list.push_back(listing[i].name); } } return list; }
int lf_listdir(lua_State *L) { LISTDIR_CALLBACK_INFO info; info.lua = L; info.i = 1; /* create the table */ lua_newtable(L); /* add all the entries */ if(strlen(lua_tostring(L, 1)) < 1) list_directory(context_get_path(L), listdir_callback, &info); else { char buffer[1024]; path_join(context_get_path(L), -1, lua_tostring(L,1), -1, buffer, sizeof(buffer)); list_directory(buffer, listdir_callback, &info); } return 1; }
int main(int argc, char* argv[]) { char dev_name[1024]; char target[1024]; char dest[1024]; //check argument format if(argc <= 1 || strcmp(argv[1],"-d") != 0) //first option must be -d { print_usage(argv[0]); return 0; } char opt; opterr = 0; //disable default error message int flag = 0; while((opt = getopt(argc,argv,"d:l:r:o:")) != -1) { switch(opt) { case 'd': strcpy(dev_name, optarg); break; case 'l': strcpy(target, optarg); flag += 4; break; case 'r': strcpy(target, optarg); flag += 2; break; case 'o': strcpy(dest, optarg); flag += 1; break; default: // opt == '?' i.e. missing option argument or option not found print_usage(argv[0]); return 0; } } if(flag == 3 || flag == 4) { list_directory(dev_name,target); } else { print_usage(argv[0]); return 0; } return 0; }
int main_list( int argc, char **argv ) { initialize( argv[1] ); switch ( PARSE_command_line( argc, argv ) ) { case LIST : list_directory(); break; case SORT_LIST : sort_list(); list_directory(); break; case LIST_STACK : list_stack(); break; default : CD_usage_error( "[ stack | by name ]" ); } return 0; }
int main(int argc, char *argv[]) { extern int optind, optopt; extern char *optarg; char c; unsigned long long offset = 0, nfiles = 0; int ii, rv; errno = 0; while ((c = getopt(argc, argv, "n:o:")) != EOF) { switch (c) { case 'n': nfiles = atoll(optarg); break; case 'o': offset = atoll(optarg); break; default: fprintf(stderr, "Unrecognized option: -%c\n", optopt); return (-1); } } if (argc == optind) { rv = list_directory(".", offset, nfiles); } else { for (ii = optind; ii < argc; ii++) { if ((rv = list_directory(argv[ii], offset, nfiles)) != 0) break; } } return (rv); }
void filesystem::remove_directory_content(const char * path, abort_callback & abort) { class myCallback : public directory_callback { public: bool on_entry(filesystem * p_owner,abort_callback & p_abort,const char * p_url,bool p_is_subdirectory,const t_filestats & p_stats) { if (p_is_subdirectory) p_owner->list_directory(p_url, *this, p_abort); try { p_owner->remove(p_url, p_abort); } catch(exception_io_not_found) {} return true; } }; myCallback cb; list_directory(path, cb, abort); }
void process_path(const std::string& path) { LOG(info) << "processing_path: " << path << "\n"; if (is_directory(path)) { auto dir_list = list_directory(path); for (const auto& fn : dir_list) { if (fn == "." or fn == "..") continue; std::string full_fn = path + "/" + fn; if (is_directory(full_fn)) { if (opt::recurse) { opt::paths.push_back(full_fn); } else { LOG(info) << "ignoring_subdir: " << full_fn << "\n"; } } else if (fast5::File::is_valid_file(full_fn)) { process_file(full_fn); } else { LOG(info) << "ignoring_file: " << full_fn << "\n"; } } } else // not is_directory; must be a fast5 file { if (fast5::File::is_valid_file(path)) { process_file(path); } else { LOG(error) << "path [" << path << "] is neither a fast5 file nor a directory\n"; exit(EXIT_FAILURE); } } } // process_path
/// /// \brief ³õʼ»¯½Å±¾Ä£¿é /// \date 2017/08/28 /// \author albert.xu /// xgc_bool InitServerScript( ini_reader &ini ) { _Lua = luaL_newstate(); XGC_ASSERT_RETURN( _Lua, false ); luaL_openlibs( _Lua ); getGlobalNamespace( _Lua ) .addCFunction( "DBG", luaLog<0> ) .addCFunction( "INF", luaLog<1> ) .addCFunction( "WRN", luaLog<2> ) .addCFunction( "ERR", luaLog<3> ) ; LuaRef package = getGlobal( _Lua, "package" ); std::stringstream package_path( package["path"].cast< xgc::string >() ); auto count = ini.get_item_count( "LuaScripts", "Path" ); for( xgc_size i = 0; i < count; ++i ) { auto path = ini.get_item_value( "LuaScripts", "Path", i, xgc_nullptr ); list_directory( path, [&package_path]( xgc_lpcstr root, xgc_lpcstr relative, xgc_lpcstr file )->bool{ if( xgc_nullptr == file ) { package_path << root << "/" << relative << "/?;"; package_path << root << "/" << relative << "/?.lua;"; } return true; }, -1 ); } package["path"] = package_path.str(); auto luaMain = ini.get_item_value( "LuaScripts", "Main", "main.lua" ); if( luaMain ) { luaDoFile( luaMain ); } return true; }
int file_completion(char *path, int index) { static int dir_in_use = 0; static char *head, *tail = NULL; static int tail_offset; char nbuf[FILENAME], buffer[FILENAME]; char *dir, *base; if (path) { if (dir_in_use) { close_directory(); dir_in_use = 0; } if (index < 0) return 0; head = path; tail = path + index; } if (!dir_in_use) { path = head; *tail = NUL; if (*path == '|') return -1; /* no completion for pipes */ if (*path == '+' || *path == '~') { if (!expand_file_name(nbuf, path, 0x11)) return 0; /* no completions */ } else strcpy(nbuf, path); if ((base = strrchr(nbuf, '/'))) { if (base == nbuf) { dir = "/"; base++; } else { *base++ = NUL; dir = nbuf; } } else { base = nbuf; dir = "."; } tail_offset = strlen(base); dir_in_use = list_directory(dir, base); return dir_in_use; } if (index) return compl_help_directory(); if (!next_directory(buffer, 1)) return 0; strcpy(tail, buffer + tail_offset); return 1; }
/* To reduce redundant error handling code [calls to abort_connection()] * most of the function is build around conditions that will assign the error * code to @state if anything goes wrong. The rest of the function will then just * do the necessary cleanups. If all works out we end up with @state being S_OK * resulting in a cache entry being created with the fragment data generated by * either reading the file content or listing a directory. */ void file_protocol_handler(struct connection *connection) { unsigned char *redirect_location = NULL; struct string page, name; struct connection_state state; int set_dir_content_type = 0; if (get_cmd_opt_bool((const unsigned char *)"anonymous")) { if (strcmp((const char *)connection->uri->string, "file:///dev/stdin") || isatty(STDIN_FILENO)) { abort_connection(connection, connection_state(S_FILE_ANONYMOUS)); return; } } #ifdef CONFIG_CGI if (!execute_cgi(connection)) return; #endif /* CONFIG_CGI */ /* Treat /dev/stdin in special way */ if (!strcmp((const char *)connection->uri->string, "file:///dev/stdin")) { int fd = open("/dev/stdin", O_RDONLY); if (fd == -1) { abort_connection(connection, connection_state(-errno)); return; } set_nonblocking_fd(fd); if (!init_http_connection_info(connection, 1, 0, 1)) { abort_connection(connection, connection_state(S_OUT_OF_MEM)); close(fd); return; } connection->socket->fd = fd; connection->data_socket->fd = -1; read_from_stdin(connection); return; } /* This function works on already simplified file-scheme URI pre-chewed * by transform_file_url(). By now, the function contains no hostname * part anymore, possibly relative path is converted to an absolute one * and uri->data is just the final path to file/dir we should try to * show. */ if (!init_string(&name) || !add_uri_to_string(&name, connection->uri, URI_PATH)) { done_string(&name); abort_connection(connection, connection_state(S_OUT_OF_MEM)); return; } decode_uri_string(&name); /* In Win32, file_is_dir seems to always return 0 if the name * ends with a directory separator. */ if ((name.length > 0 && dir_sep(name.source[name.length - 1])) || file_is_dir(name.source)) { /* In order for global history and directory listing to * function properly the directory url must end with a * directory separator. */ if (name.source[0] && !dir_sep(name.source[name.length - 1])) { redirect_location = (unsigned char *)STRING_DIR_SEP; state = connection_state(S_OK); } else { state = list_directory(connection, name.source, &page); set_dir_content_type = 1; } } else { state = read_encoded_file(&name, &page); /* FIXME: If state is now S_ENCODE_ERROR we should try loading * the file undecoded. --jonas */ } done_string(&name); if (is_in_state(state, S_OK)) { struct cache_entry *cached; /* Try to add fragment data to the connection cache if either * file reading or directory listing worked out ok. */ cached = connection->cached = get_cache_entry(connection->uri); if (!connection->cached) { if (!redirect_location) done_string(&page); state = connection_state(S_OUT_OF_MEM); } else if (redirect_location) { if (!redirect_cache(cached, redirect_location, 1, 0)) state = connection_state(S_OUT_OF_MEM); } else { add_fragment(cached, 0, page.source, page.length); connection->from += page.length; if (!cached->head && set_dir_content_type) { unsigned char *head; /* If the system charset somehow * changes after the directory listing * has been generated, it should be * parsed with the original charset. */ head = straconcat((const unsigned char *)"\r\nContent-Type: text/html; charset=", get_cp_mime_name(get_cp_index((const unsigned char *)"System")), "\r\n", (unsigned char *) NULL); /* Not so gracefully handle failed memory * allocation. */ if (!head) state = connection_state(S_OUT_OF_MEM); /* Setup directory listing for viewing. */ mem_free_set(&cached->head, head); } done_string(&page); } } abort_connection(connection, state); }
int handle_request(int connfd) { char *buf = (char *) malloc(MAXLINE); char *method, *uri, *version; char *fullpath; int nread; struct stat fs; int is_dir; int error_code = 0, ret = 0; rio_t rio; rio_initb(&rio, connfd); nread = rio_readlineb(&rio, buf, MAXLINE - 1); if (nread == 0) { error_code = 400; goto fail; } else if (buf[nread - 1] != '\n') { error_code = 414; goto fail; } else { buf[nread - 1] = 0; if (nread >= 2 && buf[nread - 2] == '\r') { buf[nread - 2] = 0; } } method = buf; for (uri = method; *uri != 0; ++uri) { if (*uri == ' ') { *uri = 0; ++uri; break; } } for (version = uri; *version != 0; ++version) { if (*version == ' ') { *version = 0; ++version; break; } } if (*method == 0 || *uri == 0 || *version == 0) { /* missing some fields in http request header */ error_code = 400; goto fail; } if (strcmp(method, "GET") != 0) { /* only allow GET method */ error_code = 405; goto fail; } goto next; fail: free(buf); if (error_code > 0) { send_error(connfd, error_code); return 0; } next: /* translate path and dispatch HTTP request according to path */ uri = translate_path(uri); fullpath = (char *) malloc(strlen(uri) + 2); fullpath[0] = '.'; strcpy(fullpath + 1, uri); if (stat(fullpath, &fs) != 0) { send_error(connfd, 404); } else { is_dir = S_ISDIR(fs.st_mode); if (is_dir) { if (fullpath[(int) strlen(fullpath) - 1] != '/') { send_response(connfd, 301, NULL); send_header(connfd, "Location", "%s/", fullpath); end_headers(connfd); } else { ret = list_directory(connfd, fullpath); } } else { ret = fetch_file(connfd, fullpath); } } free(fullpath); free(buf); return ret; }
int main(int argc, char* argv[]) { unsigned char dev_name[1025]; unsigned char target[1025]; unsigned char dest[1025]; //check argument format if(argc <= 1 || strcmp(argv[1],"-d") != 0) //first option must be -d { print_usage(argv[0]); return 0; } char opt; opterr = 0; //disable default error message int flag = 0; while((opt = getopt(argc,argv,"d:l:r:o:")) != -1) { switch(opt) { case 'd': strcpy(dev_name, optarg); break; case 'l': strcpy(target, optarg); flag += 4; break; case 'r': strcpy(target, optarg); flag += 2; break; case 'o': strcpy(dest, optarg); flag += 1; break; default: // opt == '?' i.e. missing option argument or option not found print_usage(argv[0]); return 0; } } //open & read boot entry struct BootEntry bootent; int disk = open(dev_name,O_RDONLY); pread(disk,&bootent,sizeof(bootent),0); //calculate start_of_FAT & start_of_Data unsigned int start_of_FAT = bootent.BPB_RsvdSecCnt * bootent.BPB_BytsPerSec; unsigned int start_of_Data = start_of_FAT + bootent.BPB_FATSz32 * bootent.BPB_NumFATs * bootent.BPB_BytsPerSec; //read FAT array unsigned int fat_size = bootent.BPB_FATSz32 * bootent.BPB_BytsPerSec; //in bytes unsigned int fat[fat_size / 4]; pread(disk, fat, fat_size, start_of_FAT); DiskInfo diskinfo; diskinfo.dev_name = dev_name; diskinfo.disk_fd = disk; diskinfo.fat = fat; diskinfo.start_of_Data = start_of_Data; diskinfo.byte_per_cluster = (bootent.BPB_SecPerClus * bootent.BPB_BytsPerSec); diskinfo.no_dirent_per_cluster = diskinfo.byte_per_cluster / 32; if(flag == 4) { list_directory(diskinfo,target); } else if(flag == 3) { if(recover_main(diskinfo,target,dest) == -1) print_usage(argv[0]); } else { print_usage(argv[0]); } close(disk); return 0; }
unsigned char init_saving_engine(){ list_directory(); }
/* * NAME * cli_find_cmd * * SYNOPSIS * #include "cli.h" * static cli_record_t * *cli_find_cmd(char *name) * * DESCRIPTION * An internal function to scan the current directory for the * specified entry. * * INPUT PARAMETERS * name pointer to the string to look for (null terminated). * * RETURN VALUE * NULL string NOT found * ptr Pointer to entry record if a match was found * */ static cli_record_t *cli_find_cmd (cli_internal_data_t* data, char *name) { cli_record_t *cmd_rec; //DEBUG_PRINTF("\nAttempting to Find \"%s\" in directory [%s]\n", // name, current_directory->name ); /* * check for global command search */ if (*name == '!') { DEBUG_PRINTF(data, "\nGlobal command search! \r\n"); name++; cmd_rec = data->list; while (cmd_rec != 0) { DEBUG_PRINTF(data, "\nglobal comparing \"%s\" with entry \"%s\"\r\n", name, cmd_rec->name); if (str_exact(cmd_rec->name, name, CLI_ENTRY_LEN)) { DEBUG_PRINTF(data, "\nGlobal match found for -%s- \r\n", name); return (cmd_rec); } cmd_rec = cmd_rec->cmd_list; } cliprintf(data, "\n*Unknown entry \"%s\"\r\n", name); return (0); } /* * now compare to existing command and directory entries */ cmd_rec = data->current_directory->link2subdir; while (cmd_rec != NULL) { DEBUG_PRINTF(data, "\ncomparing \"%s\" with entry \"%s\"\r\n", name, cmd_rec->name); if (str_exact(cmd_rec->name, name, CLI_ENTRY_LEN)) { DEBUG_PRINTF(data, "\nMatch found for -%s- \r\n", name); return (cmd_rec); } cmd_rec = cmd_rec->next_entry; } /* * check to see if the user wants to go back one directory */ if (str_exact(name, CLI_UP_ONE, 2)) { DEBUG_PRINTF(data, "\nGo up one dir level \r\n"); data->current_directory = data->current_directory->parent_dir; return (0); } /* * check to see if user wants to go to the root directory */ if (str_exact(name, CLI_ROOT_DIR, 1)) { DEBUG_PRINTF(data, "\nGo to root \r\n"); data->current_directory = &data->cli_root_element; return (0); } /* * check to see if user wants to list entries */ if (str_exact(name, CLI_LIST_DIR, 2)) { DEBUG_PRINTF(data, "\nList directory \r\n"); list_directory(data); return (0); } if (str_exact(name, CLI_GLOBAL_LIST, 2)) { DEBUG_PRINTF(data, "\nGlobal List \r\n"); global_command_list(data); return (0); } if (str_exact(name, CLI_H, 1) || str_exact(name, CLI_HELP, 4) || str_exact(name, CLI_QUESTION, 1)) { cli_help(data); return (NULL); } /* did not find a match */ cliprintf(data, "\n*Unknown entry \"%s\"\r\n", name); return (0); }
/** * Re-scan our local directory and re-build the index. */ static void update_directory () { static size_t initial_allocation = 32 * 1024; /* initial size for response buffer */ struct MHD_Response *response; struct ResponseDataContext rdc; unsigned int language_idx; unsigned int category_idx; const struct Language *language; const char *category; char dir_name[128]; struct stat sbuf; rdc.buf_len = initial_allocation; if (NULL == (rdc.buf = malloc (rdc.buf_len))) { update_cached_response (NULL); return; } rdc.off = snprintf (rdc.buf, rdc.buf_len, "%s", INDEX_PAGE_HEADER); for (language_idx = 0; NULL != languages[language_idx].dirname; language_idx++) { language = &languages[language_idx]; if (0 != stat (language->dirname, &sbuf)) continue; /* empty */ /* we ensured always +1k room, filenames are ~256 bytes, so there is always still enough space for the header without need for an additional reallocation check. */ rdc.off += snprintf (&rdc.buf[rdc.off], rdc.buf_len - rdc.off, "<h2>%s</h2>\n", language->longname); for (category_idx = 0; NULL != categories[category_idx]; category_idx++) { category = categories[category_idx]; snprintf (dir_name, sizeof (dir_name), "%s/%s", language->dirname, category); if (0 != stat (dir_name, &sbuf)) continue; /* empty */ /* we ensured always +1k room, filenames are ~256 bytes, so there is always still enough space for the header without need for an additional reallocation check. */ rdc.off += snprintf (&rdc.buf[rdc.off], rdc.buf_len - rdc.off, "<h3>%s</h3>\n", category); if (MHD_NO == list_directory (&rdc, dir_name)) { free (rdc.buf); update_cached_response (NULL); return; } } } /* we ensured always +1k room, filenames are ~256 bytes, so there is always still enough space for the footer without need for a final reallocation check. */ rdc.off += snprintf (&rdc.buf[rdc.off], rdc.buf_len - rdc.off, "%s", INDEX_PAGE_FOOTER); initial_allocation = rdc.buf_len; /* remember for next time */ response = MHD_create_response_from_buffer (rdc.off, rdc.buf, MHD_RESPMEM_MUST_FREE); mark_as_html (response); #if FORCE_CLOSE (void) MHD_add_response_header (response, MHD_HTTP_HEADER_CONNECTION, "close"); #endif update_cached_response (response); }