static char *backup_as_link(const char *path) { char *backup; char symlinkbuf[PATH_MAX]; ssize_t symlinklen; int success = 0; if ((backup = uncolog_get_linkname(&ufp)) == NULL) return NULL; if (is_symlink(path)) { if ((symlinklen = readlink(path, symlinkbuf, sizeof(symlinkbuf) - 1)) == -1) goto Exit; symlinkbuf[symlinklen] = '\0'; if (default_symlink(symlinkbuf, backup) != 0) goto Exit; } else { if (default_link(path, backup) != 0) goto Exit; } success = 1; Exit: if (! success) { free(backup); backup = NULL; } return backup; }
inline bool is_other(file_status s) noexcept { return (exists(s) && not is_regular_file(s) && not is_directory(s) && not is_symlink(s)); }
static int condition_test_path_is_symbolic_link(Condition *c) { assert(c); assert(c->parameter); assert(c->type == CONDITION_PATH_IS_SYMBOLIC_LINK); return is_symlink(c->parameter) > 0; }
/* This function is REALLY HOT. It gets called for every file */ int filename_filter(const char *path, const struct dirent *dir, void *baton) { const char *filename = dir->d_name; /* TODO: don't call strlen on filename every time we call filename_filter() */ size_t filename_len = strlen(filename); size_t i; scandir_baton_t *scandir_baton = (scandir_baton_t *)baton; const ignores *ig = scandir_baton->ig; const char *base_path = scandir_baton->base_path; const size_t base_path_len = scandir_baton->base_path_len; const char *path_start = path; char *temp; if (!opts.follow_symlinks && is_symlink(path, dir)) { log_debug("File %s ignored becaused it's a symlink", dir->d_name); return 0; } if (is_named_pipe(path, dir)) { log_debug("%s ignored because it's a named pipe", path); return 0; } for (i = 0; evil_hardcoded_ignore_files[i] != NULL; i++) { if (strcmp(filename, evil_hardcoded_ignore_files[i]) == 0) { return 0; } } if (!opts.search_hidden_files && filename[0] == '.') { return 0; } if (opts.search_all_files && !opts.path_to_agignore) { return 1; } for (i = 0; base_path[i] == path[i] && i < base_path_len; i++) { /* base_path always ends with "/\0" while path doesn't, so this is safe */ path_start = path + i + 2; } log_debug("path_start %s filename %s", path_start, filename); while (ig != NULL) { if (path_ignore_search(ig, path_start, filename)) { return 0; } if (is_directory(path, dir) && filename[filename_len - 1] != '/') { ag_asprintf(&temp, "%s/", filename); int rv = path_ignore_search(ig, path_start, temp); free(temp); if (rv) { return 0; } } ig = ig->parent; } return 1; }
bool Filter::is_valid( const Path& p ) const { if ( is_directory( p ) && !is_symlink( p ) ) { return is_folder_valid( p ); } return is_file_valid( p ); }
bool DirReader::check_simlink_and_advance() { if (is_symlink(dir->symlink_status())) { file_in.close(); dir++; std::cerr<<"we are in simlink - moving forward\nprocessing \t"<<dir->path().string()<<" ... \n"; file_in.open(dir->path().string()); return true; } return false; }
/* Collects files under specified file system tree. */ static void list_files_recursively(const char path[], int skip_dot_files, strlist_t *list) { int i; /* Obtain sorted list of files. */ int len; char **lst = list_sorted_files(path, &len); if(len < 0) { return; } /* Visit all subdirectories ignoring symbolic links to directories. */ for(i = 0; i < len && !ui_cancellation_requested(); ++i) { char *full_path; if(skip_dot_files && lst[i][0] == '.') { update_string(&lst[i], NULL); continue; } full_path = format_str("%s/%s", path, lst[i]); if(is_dir(full_path)) { if(!is_symlink(full_path)) { list_files_recursively(full_path, skip_dot_files, list); } free(full_path); update_string(&lst[i], NULL); } else { free(lst[i]); lst[i] = full_path; } show_progress("Listing...", 1000); } /* Append files. */ for(i = 0; i < len; ++i) { if(lst[i] != NULL) { list->nitems = put_into_string_array(&list->items, list->nitems, lst[i]); } } free(lst); }
file_status directory_entry::status() const { if (!status_known(_st)) { if (status_known(_lst) && ! is_symlink(_lst)) { _st = _lst; } else { _st = filesystem::status(_path); } } return _st; }
file_status directory_entry::status(error_code& ec) const noexcept { if (!status_known(_st)) { if (status_known(_lst) && ! is_symlink(_lst)) { _st = _lst; } else { _st = filesystem::status(_path, ec); } } return _st; }
inline int remove_all( const char* file_path, int& ec ) { if(is_symlink(file_path) || !is_directory(file_path)) return remove(file_path, ec); int count; { current_path_saver const cps; current_path(file_path); count = details::remove_all_rec(ec); } return count + remove(file_path, ec); }
int bd_readlink(const char *pPathLien, char *pBuffer, int sizeBuffer) { int iNode = find_iNode(pPathLien); if (iNode == -1) { return iNode; } int isSymlink = is_symlink(iNode); if (isSymlink == -1) { return -1; } int linesRead = bd_read(pPathLien, pBuffer, 0, sizeBuffer); return linesRead; }
void ObjectLibrary::processFolder(const char* filepath) { // Create object library path library_path(filepath); if(is_directory(library_path)) { recursive_directory_iterator iter(library_path); recursive_directory_iterator end; while(iter != end) { if(is_directory(iter->path())) { object_idx.push_back(images.size()); object_names.push_back(iter->path().filename().generic_string()); //std::cout << "Object: " << object_names.back() << " " << object_idx.back() << std::endl; // Handle Symlink Directories if(is_symlink(iter->path())) { iter = recursive_directory_iterator(canonical(read_symlink(iter->path()), library_path)); } } else { // Initialize object feature Mat img_object = imread(iter->path().generic_string(), CV_LOAD_IMAGE_COLOR); if( !img_object.data ) { throw std::runtime_error("Error Reading Image"); } ImageData img = processImage(img_object); img.name = iter->path().stem().generic_string(); //std::cout << "Object: " << object_names.back() << " Image: " << img.name << std::endl; if(img.name == object_names.back()) { object_img_idx.push_back(images.size()); //std::cout << object_names.back() << " " << images.size() << std::endl; } //-- Step 3: Add to object library images.push_back(std::move(img)); } ++iter; } } //std::cout << "Num Objects: " << object_idx.size() << std::endl; }
void hash_entry(const directory_entry &i, crypto_generichash_state &state) { auto &p = i.path(); size_t size = 0; if (is_regular_file(i.status())) size = (size_t)file_size(i.path()); if (is_regular(i.status())) { char chunk_buffer[16 * 1024]; size_t chunk_buffer_size = sizeof(chunk_buffer); size_t chunk_cnt = size / chunk_buffer_size; size_t last_chunk_size = size % chunk_buffer_size; std::ifstream file(p.native(), std::ifstream::binary); if (last_chunk_size != 0) ++chunk_cnt; else last_chunk_size = chunk_buffer_size; for (size_t chunk = 0; chunk < chunk_cnt; ++chunk) { size_t chunk_size = chunk_buffer_size; if (chunk == chunk_cnt - 1) chunk_size = last_chunk_size; file.read(&chunk_buffer[0], chunk_size); crypto_generichash_update(&state, (unsigned char *)&chunk_buffer[0], chunk_size); } return; } if (is_symlink(i.status())) { path sym_path(::read_symlink(p)); std::string s = sym_path.generic_string(); crypto_generichash_update(&state, (unsigned char *) s.c_str(), s.length()); return; } if (is_directory(i.status())) { crypto_generichash_update(&state, (const unsigned char *)"d", 1); return; } }
/* This function is REALLY HOT. It gets called for every file */ int filename_filter(const char *path, const struct dirent *dir, void *baton) { const char *filename = dir->d_name; size_t i; ignores *ig = (ignores*) baton; if (!opts.follow_symlinks && is_symlink(path, dir)) { log_debug("File %s ignored becaused it's a symlink", dir->d_name); return 0; } for (i = 0; evil_hardcoded_ignore_files[i] != NULL; i++) { if (strcmp(filename, evil_hardcoded_ignore_files[i]) == 0) { return 0; } } if (!opts.search_hidden_files && filename[0] == '.') { return 0; } if (opts.search_all_files && !opts.path_to_agignore) { return 1; } if (filename_ignore_search(ig, filename)) { return 0; } if (is_directory(path, dir) && filename[strlen(filename) - 1] != '/') { char *temp; asprintf(&temp, "%s/", filename); int rv = filename_ignore_search(ig, temp); free(temp); if (rv) { return 0; } } if (ig->parent != NULL) { return filename_filter(path, dir, (void *)(ig->parent)); } return 1; }
void file_manager::get_files_from_dir(path const & dir, thread_pool & thread_pool, suffix_array_builder & sa_builder) { vpath dir_entries; copy(directory_iterator(dir), directory_iterator(), std::back_inserter(dir_entries)); for (vpath::iterator it = dir_entries.begin(); it != dir_entries.end(); ++it) { if (is_symlink(*it)) { continue; } if (is_regular_file(*it)) { safe_add_file(*it, sa_builder); continue; } if (is_directory(*it)) { safe_add_visited_dir(*it, thread_pool, sa_builder); } } }
int traverse(const char path[], subtree_visitor visitor, void *param) { /* Duplication with traverse_subtree(), but this way traverse_subtree() can * use information from dirent structure to save some operations. */ if(is_symlink(path)) { /* Tread symbolic links to directories as files as well. */ return visitor(path, VA_FILE, param); } else if(is_dir(path)) { return traverse_subtree(path, visitor, param); } else { return visitor(path, VA_FILE, param); } }
inline int remove_all_rec( int& ec ) { int count = 0; for ( directory_iterator current_dir( "." ); *current_dir; ++current_dir ) { if(is_symlink(*current_dir) || !is_directory(*current_dir)) { count += remove(*current_dir, ec); continue; } { current_path_saver const cps; current_path(*current_dir); count += remove_all_rec(ec); } count += remove(*current_dir, ec); } return count; }
void MainLoop::loopOverDirectory(path directory) { assert(is_directory(directory)); directory_iterator end_itr; for( directory_iterator itr(directory); itr!=end_itr;++itr ) { path pathname=itr->path(); if (is_symlink(pathname)) { config_ptr->writeLog("I do not deal with symlink : "+ pathname.string()); } else if (is_directory( pathname )) { DealWithDirectory(pathname); } else if (is_regular_file(pathname)) { DealWithFile(pathname); } else { config_ptr->writeLog("I'm looping in the directory : "+directory.string()); string message="I'm looping in the directory : "+directory.string(); config_ptr->writeLog(message); throw std::string(message); } } }
int main(int argc, char* argv[]) { try { if (argc < 3) { std::cerr << "Usage: lha (archive) (filename) ..." << std::endl; return 1; } std::setlocale(LC_ALL, ""); // file_descriptor_sink supports 64bit offset ar::basic_lzh_file_sink<io_ex::file_descriptor_sink> lzh((io_ex::file_descriptor_sink( std::string(argv[1]), BOOST_IOS::binary))); for (int i = 2; i < argc; ++i) { ar::lha::header head; head.path = fs::path(argv[i]); const fs_ex::file_status& s = fs_ex::symlink_status(head.path); if (is_symlink(s)) head.link_path = fs_ex::symlink_target(head.path); else if (is_directory(s)) head.attributes = ar::msdos::attributes::directory; else head.file_size = s.file_size(); head.update_time = s.last_write_time().to_time_t(); if (s.has_attributes()) head.attributes = s.attributes(); if (s.has_creation_time()) { ar::lha::windows::timestamp ts; ts.creation_time = s.creation_time().to_windows_file_time(); ts.last_write_time = s.last_write_time().to_windows_file_time(); ts.last_access_time = s.last_access_time().to_windows_file_time(); head.timestamp = ts; } if (s.has_permissions()) head.permissions = s.permissions(); if (s.has_uid() && s.has_gid()) { ar::lha::posix::gid_uid owner; owner.gid = s.gid(); owner.uid = s.uid(); head.owner = owner; } #if defined(BOOST_WINDOWS) head.code_page = ::GetACP(); #endif lzh.create_entry(head); if (!fs::is_directory(head.path)) { try { io::copy( io_ex::file_descriptor_source( head.path.file_string(), std::ios_base::binary), lzh ); } catch (const ar::give_up_compression&) { lzh.rewind_entry(); io::copy( io_ex::file_descriptor_source( head.path.file_string(), std::ios_base::binary), lzh ); } } } lzh.close_archive(); return 0; } catch (const std::exception& e) { std::cerr << "Error: " << e.what() << std::endl; } return 1; }
inline bool is_symlink(const path& p, std::error_code& ec) noexcept { return is_symlink(detail::symlink_status(p, &ec)); }
/* * Processes a single man page source by using nroff to create * the preformatted cat page. */ static void process_page(char *mandir, char *src, char *cat, enum Ziptype zipped) { int src_test, cat_test; time_t src_mtime, cat_mtime; char cmd[MAXPATHLEN]; dev_t src_dev; ino_t src_ino; const char *link_name; src_test = test_path(src, &src_mtime); if (!(src_test & (TEST_FILE|TEST_READABLE))) { if (!(src_test & TEST_DIR)) { warnx("%s/%s: unreadable", mandir, src); exit_code = 1; if (rm_junk && is_symlink(src)) junk(mandir, src, "bogus symlink"); } return; } src_dev = test_st.st_dev; src_ino = test_st.st_ino; cat_test = test_path(cat, &cat_mtime); if (cat_test & (TEST_FILE|TEST_READABLE)) { if (!force && cat_mtime >= src_mtime) { if (verbose) { fprintf(stderr, "\t%s/%s: up to date\n", mandir, src); } return; } } /* * Is the man page a link to one we've already processed? */ if ((link_name = find_hashtable(links, src_ino, src_dev)) != NULL) { if (verbose || pretend) { fprintf(stderr, "%slink %s -> %s\n", verbose ? "\t" : "", cat, link_name); } if (!pretend) link(link_name, cat); return; } insert_hashtable(links, src_ino, src_dev, strdup(cat)); if (verbose || pretend) { fprintf(stderr, "%sformat %s -> %s\n", verbose ? "\t" : "", src, cat); if (pretend) return; } snprintf(tmp_file, sizeof tmp_file, "%s.tmp", cat); snprintf(cmd, sizeof cmd, "%scat %s | tbl | nroff -c -T%s -man | %s > %s.tmp", zipped == BZIP ? BZ2CAT_CMD : zipped == GZIP ? GZCAT_CMD : "", src, nroff_device, zipped == BZIP ? BZ2_CMD : zipped == GZIP ? GZ_CMD : "cat", cat); if (system(cmd) != 0) err(1, "formatting pipeline"); if (rename(tmp_file, cat) < 0) warn("%s", cat); tmp_file[0] = '\0'; }
/* TODO: Append matches to some data structure instead of just printing them out. * Then ag can have sweet summaries of matches/files scanned/time/etc. */ void search_dir(ignores *ig, const char *base_path, const char *path, const int depth) { struct dirent **dir_list = NULL; struct dirent *dir = NULL; scandir_baton_t scandir_baton; int results = 0; char *dir_full_path = NULL; const char *ignore_file = NULL; int i; /* find agignore/gitignore/hgignore/etc files to load ignore patterns from */ for (i = 0; opts.skip_vcs_ignores ? (i == 0) : (ignore_pattern_files[i] != NULL); i++) { ignore_file = ignore_pattern_files[i]; ag_asprintf(&dir_full_path, "%s/%s", path, ignore_file); if (strcmp(SVN_DIR, ignore_file) == 0) { load_svn_ignore_patterns(ig, dir_full_path); } else { load_ignore_patterns(ig, dir_full_path); } free(dir_full_path); dir_full_path = NULL; } if (opts.path_to_agignore) { load_ignore_patterns(ig, opts.path_to_agignore); } scandir_baton.ig = ig; scandir_baton.base_path = base_path; results = ag_scandir(path, &dir_list, &filename_filter, &scandir_baton); if (results == 0) { log_debug("No results found in directory %s", path); goto search_dir_cleanup; } else if (results == -1) { if (errno == ENOTDIR) { /* Not a directory. Probably a file. */ /* If we're only searching one file, don't print the filename header at the top. */ if (depth == 0 && opts.paths_len == 1) { opts.print_heading = -1; } search_file(path); } else { log_err("Error opening directory %s: %s", path, strerror(errno)); } goto search_dir_cleanup; } int offset_vector[3]; int rc = 0; work_queue_t *queue_item; for (i = 0; i < results; i++) { queue_item = NULL; dir = dir_list[i]; ag_asprintf(&dir_full_path, "%s/%s", path, dir->d_name); /* If a link points to a directory then we need to treat it as a directory. */ if (!opts.follow_symlinks && is_symlink(path, dir)) { log_debug("File %s ignored becaused it's a symlink", dir->d_name); goto cleanup; } if (!is_directory(path, dir)) { if (opts.file_search_regex) { rc = pcre_exec(opts.file_search_regex, NULL, dir_full_path, strlen(dir_full_path), 0, 0, offset_vector, 3); if (rc < 0) { /* no match */ log_debug("Skipping %s due to file_search_regex.", dir_full_path); goto cleanup; } else if (opts.match_files) { log_debug("match_files: file_search_regex matched for %s.", dir_full_path); pthread_mutex_lock(&print_mtx); print_path(dir_full_path, '\n'); pthread_mutex_unlock(&print_mtx); goto cleanup; } } queue_item = ag_malloc(sizeof(work_queue_t)); queue_item->path = dir_full_path; queue_item->next = NULL; pthread_mutex_lock(&work_queue_mtx); if (work_queue_tail == NULL) { work_queue = queue_item; } else { work_queue_tail->next = queue_item; } work_queue_tail = queue_item; pthread_mutex_unlock(&work_queue_mtx); pthread_cond_signal(&files_ready); log_debug("%s added to work queue", dir_full_path); } else if (opts.recurse_dirs) { if (depth < opts.max_search_depth) { log_debug("Searching dir %s", dir_full_path); ignores *child_ig = init_ignore(ig); search_dir(child_ig, base_path, dir_full_path, depth + 1); cleanup_ignore(child_ig); } else { log_err("Skipping %s. Use the --depth option to search deeper.", dir_full_path); } } cleanup: ; free(dir); dir = NULL; if (queue_item == NULL) { free(dir_full_path); dir_full_path = NULL; } } search_dir_cleanup: ; free(dir_list); dir_list = NULL; }
inline bool is_other(file_status f) { return exists(f) && !is_regular_file(f) && !is_directory(f) && !is_symlink(f); }
int main(int argc, char* argv[]) { try { if (argc < 3) { std::cerr << "Usage: archive (archive) (filename) ..." << std::endl; return 1; } std::setlocale(LC_ALL, ""); std::auto_ptr<archiver_base> arc_ptr; const std::string filename(argv[1]); if (algo::ends_with(filename, ".lzh")) { arc_ptr.reset(new archiver< ar::lzh_file_sink>(ar::lzh_file_sink(filename))); } else if (algo::ends_with(filename, ".tar")) { arc_ptr.reset(new archiver< ar::tar_file_sink>(ar::tar_file_sink(filename))); } else if (algo::ends_with(filename, ".zip")) { arc_ptr.reset(new archiver< ar::zip_file_sink>(ar::zip_file_sink(filename))); } else if ( algo::ends_with(filename, ".tar.bz2") || algo::ends_with(filename, ".tbz2") || algo::ends_with(filename, ".tb2") || algo::ends_with(filename, ".tbz") ) { arc_ptr.reset(new archiver< ar::tbz2_file_sink>(ar::tbz2_file_sink(filename))); } else if ( algo::ends_with(filename, ".tar.gz") || algo::ends_with(filename, ".tgz") ) { arc_ptr.reset(new archiver< ar::tgz_file_sink>(ar::tgz_file_sink(filename))); } else if (algo::ends_with(filename, ".gz")) { if (argc != 3) { throw std::runtime_error( "gzip cannot contain two files or more"); } fs::path ph(argv[2]); if (fs::is_directory(ph)) throw std::runtime_error("gzip cannot compress a directory"); io::gzip_params params; params.file_name = ph.leaf(); params.mtime = fs::last_write_time(ph); io::copy( io_ex::file_source(argv[2]), io::compose( io::gzip_compressor(params), io_ex::file_sink(filename) ) ); return 0; } else if (algo::ends_with(filename, ".bz2")) { if (argc != 3) { throw std::runtime_error( "bzip2 cannot contain two files or more"); } fs::path ph(argv[2]); if (fs::is_directory(ph)) throw std::runtime_error("bzip2 cannot compress a directory"); io::copy( io_ex::file_source(argv[2]), io::compose( io::bzip2_compressor(), io_ex::file_sink(filename) ) ); return 0; } else throw std::runtime_error("unsupported format"); for (int i = 2; i < argc; ++i) { entry e; e.path = fs::path(argv[i]); const fs_ex::file_status& s = fs_ex::symlink_status(e.path); if (is_symlink(s)) { e.type = fs_ex::symlink_file; e.link_path = fs_ex::symlink_target(e.path); } else if (is_directory(s)) e.type = fs_ex::directory_file; else { e.type = fs_ex::regular_file; e.file_size = fs::file_size(e.path); } if (s.has_attributes()) e.attributes = static_cast<boost::uint16_t>(s.attributes()); if (s.has_permissions()) e.permissions = s.permissions(); e.last_write_time = s.last_write_time(); e.last_access_time = s.last_access_time(); if (s.has_last_change_time()) e.last_change_time = s.last_change_time(); if (s.has_creation_time()) e.creation_time = s.creation_time(); if (s.has_uid()) e.uid = s.uid(); if (s.has_gid()) e.gid = s.gid(); arc_ptr->create_entry(e); if (!is_directory(s)) { try { io::copy( io_ex::file_source( e.path.file_string(), std::ios_base::binary), boost::ref(*arc_ptr) ); } catch (const ar::give_up_compression&) { arc_ptr->rewind_entry(); io::copy( io_ex::file_source( e.path.file_string(), std::ios_base::binary), boost::ref(*arc_ptr) ); } } } arc_ptr->close_archive(); return 0; } catch (const std::exception& e) { std::cerr << "Error: " << e.what() << std::endl; } return 1; }
int main(int argc, char* argv[]) { try { if (argc < 3) { std::cerr << "Usage: tar (archive) (filename) ..." << std::endl; return 1; } std::setlocale(LC_ALL, ""); // file_descriptor_sink supports 64bit offset ar::basic_tar_file_sink<io_ex::file_descriptor_sink> tar((io_ex::file_descriptor_sink( std::string(argv[1]), BOOST_IOS::binary))); for (int i = 2; i < argc; ++i) { ar::tar::header head; head.path = fs::path(argv[i]); const fs_ex::file_status& s = fs_ex::symlink_status(head.path); if (is_symlink(s)) { head.type_flag = ar::tar::type_flag::symlink; head.link_path = fs_ex::symlink_target(head.path); } else if (is_directory(s)) head.type_flag = ar::tar::type_flag::directory; else head.file_size = s.file_size(); head.modified_time = s.last_write_time(); head.access_time = s.last_access_time(); if (s.has_last_change_time()) head.change_time = s.last_change_time(); if (s.has_permissions()) head.permissions = s.permissions(); if (s.has_uid() && s.has_gid()) { head.uid = s.uid(); head.gid = s.gid(); } #if defined(BOOST_WINDOWS) head.user_name = "root"; head.group_name = "root"; #elif defined(BOOST_HAS_UNISTD_H) if (s.has_uid()) { if (passwd* p = ::getpwuid(s.uid())) head.user_name = p->pw_name; } if (s.has_gid()) { if (group* p = ::getgrgid(s.gid())) head.group_name = p->gr_name; } #endif tar.create_entry(head); if (!fs::is_directory(head.path)) { io::copy( io_ex::file_descriptor_source( head.path.file_string(), std::ios_base::binary), tar ); } } tar.close_archive(); return 0; } catch (const std::exception& e) { std::cerr << "Error: " << e.what() << std::endl; } return 1; }
int get_link_target(const char *link, char *buf, size_t buf_len) { LOG_FUNC_ENTER; #ifndef _WIN32 char *filename; ssize_t len; if(buf_len == 0) { return -1; } filename = strdup(link); chosp(filename); len = readlink(filename, buf, buf_len - 1); free(filename); if(len == -1) { return -1; } buf[len] = '\0'; return 0; #else char filename[PATH_MAX]; DWORD attr; wchar_t *utf16_filename; HANDLE hfile; char rdb[2048]; char *t; REPARSE_DATA_BUFFER *sbuf; WCHAR *path; if(!is_symlink(link)) { return -1; } copy_str(filename, sizeof(filename), link); chosp(filename); utf16_filename = utf8_to_utf16(filename); hfile = CreateFileW(utf16_filename, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT, NULL); free(utf16_filename); if(hfile == INVALID_HANDLE_VALUE) { LOG_WERROR(GetLastError()); return -1; } if(!DeviceIoControl(hfile, FSCTL_GET_REPARSE_POINT, NULL, 0, rdb, sizeof(rdb), &attr, NULL)) { LOG_WERROR(GetLastError()); CloseHandle(hfile); return -1; } CloseHandle(hfile); sbuf = (REPARSE_DATA_BUFFER *)rdb; path = sbuf->SymbolicLinkReparseBuffer.PathBuffer; path[sbuf->SymbolicLinkReparseBuffer.PrintNameOffset/sizeof(WCHAR) + sbuf->SymbolicLinkReparseBuffer.PrintNameLength/sizeof(WCHAR)] = L'\0'; t = to_multibyte(path + sbuf->SymbolicLinkReparseBuffer.PrintNameOffset/sizeof(WCHAR)); if(strncmp(t, "\\??\\", 4) == 0) strncpy(buf, t + 4, buf_len); else strncpy(buf, t, buf_len); buf[buf_len - 1] = '\0'; free(t); to_forward_slash(buf); return 0; #endif }
/* This function is REALLY HOT. It gets called for every file */ int filename_filter(const char *path, const struct dirent *dir, void *baton) { const char *filename = dir->d_name; if (!opts.search_hidden_files && filename[0] == '.') { return 0; } size_t i; for (i = 0; evil_hardcoded_ignore_files[i] != NULL; i++) { if (strcmp(filename, evil_hardcoded_ignore_files[i]) == 0) { return 0; } } if (!opts.follow_symlinks && is_symlink(path, dir)) { log_debug("File %s ignored becaused it's a symlink", dir->d_name); return 0; } if (is_named_pipe(path, dir)) { log_debug("%s ignored because it's a named pipe or socket", path); return 0; } if (opts.search_all_files && !opts.path_to_ignore) { return 1; } scandir_baton_t *scandir_baton = (scandir_baton_t *)baton; const char *base_path = scandir_baton->base_path; const size_t base_path_len = scandir_baton->base_path_len; const char *path_start = path; for (i = 0; base_path[i] == path[i] && i < base_path_len; i++) { /* base_path always ends with "/\0" while path doesn't, so this is safe */ path_start = path + i + 2; } log_debug("path_start %s filename %s", path_start, filename); const char *extension = strchr(filename, '.'); if (extension) { if (extension[1]) { // The dot is not the last character, extension starts at the next one ++extension; } else { // No extension extension = NULL; } } #ifdef HAVE_DIRENT_DNAMLEN size_t filename_len = dir->d_namlen; #else size_t filename_len = 0; #endif if (strncmp(filename, "./", 2) == 0) { #ifndef HAVE_DIRENT_DNAMLEN filename_len = strlen(filename); #endif filename++; filename_len--; } const ignores *ig = scandir_baton->ig; while (ig != NULL) { if (extension) { int match_pos = binary_search(extension, ig->extensions, 0, ig->extensions_len); if (match_pos >= 0) { log_debug("file %s ignored because name matches extension %s", filename, ig->extensions[match_pos]); return 0; } } if (path_ignore_search(ig, path_start, filename)) { return 0; } if (is_directory(path, dir)) { #ifndef HAVE_DIRENT_DNAMLEN if (!filename_len) { filename_len = strlen(filename); } #endif if (filename[filename_len - 1] != '/') { char *temp; ag_asprintf(&temp, "%s/", filename); int rv = path_ignore_search(ig, path_start, temp); free(temp); if (rv) { return 0; } } } ig = ig->parent; } log_debug("%s not ignored", filename); return 1; }
inline bool is_symlink (const std::string& a) { return is_symlink(a.c_str()); }
int putDirUtil( rcComm_t **myConn, char *srcDir, char *targColl, rodsEnv *myRodsEnv, rodsArguments_t *rodsArgs, dataObjInp_t *dataObjOprInp, bulkOprInp_t *bulkOprInp, rodsRestart_t *rodsRestart, bulkOprInfo_t *bulkOprInfo ) { char srcChildPath[MAX_NAME_LEN], targChildPath[MAX_NAME_LEN]; if ( srcDir == NULL || targColl == NULL ) { rodsLog( LOG_ERROR, "putDirUtil: NULL srcDir or targColl input" ); return USER__NULL_INPUT_ERR; } if ( isPathSymlink( rodsArgs, srcDir ) > 0 ) { return 0; } if ( rodsArgs->recursive != True ) { rodsLog( LOG_ERROR, "putDirUtil: -r option must be used for putting %s directory", srcDir ); return USER_INPUT_OPTION_ERR; } if ( rodsArgs->redirectConn == True && rodsArgs->force != True ) { int reconnFlag; if ( rodsArgs->reconnect == True ) { reconnFlag = RECONN_TIMEOUT; } else { reconnFlag = NO_RECONN; } /* reconnect to the resource server */ rstrcpy( dataObjOprInp->objPath, targColl, MAX_NAME_LEN ); redirectConnToRescSvr( myConn, dataObjOprInp, myRodsEnv, reconnFlag ); rodsArgs->redirectConn = 0; /* only do it once */ } rcComm_t *conn = *myConn; boost::filesystem::path srcDirPath( srcDir ); if ( !exists( srcDirPath ) || !is_directory( srcDirPath ) ) { rodsLog( LOG_ERROR, "putDirUtil: opendir local dir error for %s, errno = %d\n", srcDir, errno ); return USER_INPUT_PATH_ERR; } if ( rodsArgs->verbose == True ) { fprintf( stdout, "C- %s:\n", targColl ); } int bulkFlag = NON_BULK_OPR; if ( bulkOprInfo != NULL ) { bulkFlag = bulkOprInfo->flags; } int savedStatus = 0; boost::filesystem::directory_iterator end_itr; // default construction yields past-the-end for ( boost::filesystem::directory_iterator itr( srcDirPath ); itr != end_itr; ++itr ) { boost::filesystem::path p = itr->path(); snprintf( srcChildPath, MAX_NAME_LEN, "%s", p.c_str() ); if ( isPathSymlink( rodsArgs, srcChildPath ) > 0 ) { continue; } if ( !exists( p ) ) { rodsLog( LOG_ERROR, "putDirUtil: stat error for %s, errno = %d\n", srcChildPath, errno ); return USER_INPUT_PATH_ERR; } if ( is_symlink( p ) ) { boost::filesystem::path cp = read_symlink( p ); snprintf( srcChildPath, MAX_NAME_LEN, "%s/%s", srcDir, cp.c_str() ); p = boost::filesystem::path( srcChildPath ); } rodsLong_t dataSize = 0; dataObjOprInp->createMode = getPathStMode( p.c_str() ); objType_t childObjType; if ( is_regular_file( p ) ) { childObjType = DATA_OBJ_T; dataSize = file_size( p ); } else if ( is_directory( p ) ) { childObjType = COLL_OBJ_T; } else { rodsLog( LOG_ERROR, "putDirUtil: unknown local path type for %s", srcChildPath ); savedStatus = USER_INPUT_PATH_ERR; continue; } boost::filesystem::path childPath = p.filename(); snprintf( targChildPath, MAX_NAME_LEN, "%s/%s", targColl, childPath.c_str() ); if ( childObjType == DATA_OBJ_T ) { if ( bulkFlag == BULK_OPR_SMALL_FILES && file_size( p ) > MAX_BULK_OPR_FILE_SIZE ) { continue; } else if ( bulkFlag == BULK_OPR_LARGE_FILES && file_size( p ) <= MAX_BULK_OPR_FILE_SIZE ) { continue; } } int status = chkStateForResume( conn, rodsRestart, targChildPath, rodsArgs, childObjType, &dataObjOprInp->condInput, 1 ); if ( status < 0 ) { /* restart failed */ return status; } else if ( status == 0 ) { if ( bulkFlag == BULK_OPR_SMALL_FILES && ( rodsRestart->restartState & LAST_PATH_MATCHED ) != 0 ) { /* enable foreFlag one time */ setForceFlagForRestart( bulkOprInp, bulkOprInfo ); } continue; } if ( childObjType == DATA_OBJ_T ) { /* a file */ if ( bulkFlag == BULK_OPR_SMALL_FILES ) { status = bulkPutFileUtil( conn, srcChildPath, targChildPath, dataSize, dataObjOprInp->createMode, rodsArgs, bulkOprInp, bulkOprInfo ); } else { /* normal put */ status = putFileUtil( conn, srcChildPath, targChildPath, dataSize, rodsArgs, dataObjOprInp ); } if ( rodsRestart->fd > 0 ) { if ( status >= 0 ) { if ( bulkFlag == BULK_OPR_SMALL_FILES ) { if ( status > 0 ) { /* status is the number of files bulk loaded */ rodsRestart->curCnt += status; status = writeRestartFile( rodsRestart, targChildPath ); } } else { /* write the restart file */ rodsRestart->curCnt ++; status = writeRestartFile( rodsRestart, targChildPath ); } } } } else { /* a directory */ status = mkColl( conn, targChildPath ); if ( status < 0 ) { rodsLogError( LOG_ERROR, status, "putDirUtil: mkColl error for %s", targChildPath ); } status = putDirUtil( myConn, srcChildPath, targChildPath, myRodsEnv, rodsArgs, dataObjOprInp, bulkOprInp, rodsRestart, bulkOprInfo ); } if ( status < 0 && status != CAT_NO_ROWS_FOUND ) { rodsLogError( LOG_ERROR, status, "putDirUtil: put %s failed. status = %d", srcChildPath, status ); savedStatus = status; if ( rodsRestart->fd > 0 ) { break; } } } return savedStatus; }
/* This will survive recursive links, but it will not detect them */ static int rmtree_real(const char *path, int max_depth) { int ret; DIR *dir; struct dirent entry; struct dirent *result; char abs_name[FILENAME_MAX]; /* Bail if we're going too deep */ if (max_depth == 0) { ALOGE("rmtree: reached max recursion depth"); return -1; } /* If it's a symlink, remove it now to be safe. We don't want to follow one outside of a walnut's directory */ ret = is_symlink(path); if (ret == 1) { unlink(path); return 0; } else if (ret == -1) { ALOGE("rmtree: lstat %s: %s", path, strerror(errno)); return -1; } /* Try to treat path as a dir. Either opens it or tells us it's not */ dir = opendir(path); if (dir == NULL && errno == ENOTDIR) { if (unlink(path) == -1) { ALOGE("rmtree: unlink %s: %s", path, strerror(errno)); return -1; } return 0; } else if (dir == NULL) { ALOGE("rmtree: opendir %s: %s", path, strerror(errno)); return -1; } /* path is a directory. Iterate through its files */ for (ret = readdir_r(dir, &entry, &result); ret == 0 && result != NULL; ret = readdir_r(dir, &entry, &result)) { if (!strcmp(entry.d_name, ".") || !strcmp(entry.d_name, "..")) continue; snprintf(abs_name, FILENAME_MAX, "%s/%s", path, entry.d_name); if (rmtree_real(abs_name, max_depth-1) == -1) { closedir(dir); return -1; } } if (ret != 0) { closedir(dir); ALOGE("rmtree: readdir_r %s: %s", path, strerror(errno)); return -1; } closedir(dir); if (rmdir(path) == -1) { ALOGE("rmtree: rmdir %s: %s", path, strerror(errno)); return -1; } return 0; }