int copy_file(char *in_path, char *out_path) { ssize_t c; // log_add(DEBUGGING, "In copy_file\n"); process_path(in_path, out_path); strcat(out_filename, FORMAT); // log_add(DEBUGGING, "out %s\n", out_filename); if ((fdin = open(in_filename, O_RDONLY)) < 0) { //log_add(FATAL, "Can't open input file\n"); return errno; } if (fstat(fdin, &statbuf) < 0) { // log_add(FATAL, "Can't determine the input file size\n"); return errno; } if ((fdout = open(out_filename, O_RDWR | O_CREAT | O_TRUNC | O_APPEND, statbuf.st_mode)) < 0 ) { //log_add(FATAL, "Can't create output file\n"); return errno; } while((c = read(fdin, buffer, SIZE)) > 0) { if (write(fdout, buffer, c) < 0) { // log_add(FATAL, "Writing failed\n"); return errno; } } return 0; }
/******************************************************************************* Process a directory. Open it, read all it's entries (objects) and call process_path for each one (EXCEPT '.' and '..') and close it. *******************************************************************************/ void process_directory(char *pathname, regex_t *extregexpptr, int recursiondepth) { DIR *dirptr; struct dirent *direntptr; char newpathname[MAXPATHLENGTH]; char dirpath[MAXPATHLENGTH]; if (strcmp(pathname, "/")) { sprintf(dirpath, "%s%c", pathname, PATHDELIMITERCHAR); /* not "/" */ } else { sprintf(dirpath, "%c", PATHDELIMITERCHAR); /* pathname is "/" */ } if ((dirptr=opendir(pathname)) == (DIR*)NULL) { fprintf(stderr, "opendir error"); perror(pathname); returncode = 1; return; } while ((direntptr=readdir(dirptr)) != (struct dirent *)NULL) { if (strcmp(direntptr->d_name, ".") && strcmp(direntptr->d_name, "..")) { /* create newpathname from pathname/objectname */ sprintf(newpathname, "%s%s", dirpath, direntptr->d_name); process_path(newpathname, extregexpptr, recursiondepth+1); } } if (closedir(dirptr)) { perror(pathname); returncode = 1; } }
static void update_one(const char *path) { if (!verify_path(path)) { fprintf(stderr, "Ignoring path %s\n", path); return; } if (mark_valid_only) { if (mark_ce_flags(path, CE_VALID, mark_valid_only == MARK_FLAG)) die("Unable to mark file %s", path); return; } if (mark_skip_worktree_only) { if (mark_ce_flags(path, CE_SKIP_WORKTREE, mark_skip_worktree_only == MARK_FLAG)) die("Unable to mark file %s", path); return; } if (force_remove) { if (remove_file_from_cache(path)) die("git update-index: unable to remove %s", path); report("remove '%s'", path); return; } if (process_path(path)) die("Unable to process path %s", path); report("add '%s'", path); }
char *sftp_find_realpath(struct allocator *a, const char *path, unsigned flags) { char *cwd, *abspath, *result = 0; size_t nresult = 0; D(("sftp_find_realpath '%s' %#x", path, flags)); /* Default is current directory */ if(path[0] == 0) path = "."; /* Convert relative paths to absolute paths */ if(path[0] != '/') { if(!(cwd = sftp_getcwd(a))) return 0; assert(cwd[0] == '/'); abspath = sftp_alloc(a, strlen(cwd) + strlen(path) + 2); strcpy(abspath, cwd); strcat(abspath, "/"); strcat(abspath, path); path = abspath; D(("convert relative path to '%s'", path)); } /* The result always starts with a / */ result = append(a, result, &nresult, "/"); /* All the work happens below */ return process_path(a, result, &nresult, path, flags); }
static void update_one(const char *path, const char *prefix, int prefix_length) { const char *p = prefix_path(prefix, prefix_length, path); if (!verify_path(p)) { fprintf(stderr, "Ignoring path %s\n", path); goto free_return; } if (mark_valid_only) { if (mark_valid(p)) die("Unable to mark file %s", path); goto free_return; } if (force_remove) { if (remove_file_from_cache(p)) die("git update-index: unable to remove %s", path); report("remove '%s'", path); goto free_return; } if (process_path(p)) die("Unable to process path %s", path); report("add '%s'", path); free_return: if (p < path || p > path + strlen(path)) free((char*)p); }
void process_directory(const char* path) { /* * Update the number of directories seen, use opendir() to open the * directory, and then use readdir() to loop through the entries * and process them. You have to be careful not to process the * "." and ".." directory entries, or you'll end up spinning in * (infinite) loops. Also make sure you closedir() when you're done. * * You'll also want to use chdir() to move into this new directory, * with a matching call to chdir() to move back out of it when you're * done. */ ++num_dirs; DIR *dir = opendir(path); struct dirent *entry; chdir(path); while((entry = readdir(dir)) != NULL) { if(strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) { process_path(entry->d_name); } } closedir(dir); chdir(".."); }
/* rename oldpath to newpath */ int fs_rename(const char *oldpath, const char *newpath) { struct inode_s *dir, *last_dir, *ino; char path[MAX_PATH], name[MAX_NAME]; dir = last_dir = ino = NULL; /* get last component of path */ process_path(newpath, path, name); if (name[0] == '\0') { char tmp[MAX_PATH]; process_path(oldpath, tmp, name); } dir = current_dir(); /* get last directory of target path */ if ( (last_dir = find_inode(dir, path, FS_SEARCH_GET)) == NULL) goto err; /* remove entry from the old directory */ if ( (ino = find_inode(dir, oldpath, FS_SEARCH_REMOVE)) == NULL) goto err; /* new entry in the last directory of path (or old one if file exists) */ if (add_entry(last_dir, ino->i_num, name) == ERROR) goto err; /* check if oldpath was a dir, and update '..' in that case */ if (IS_DIR(ino->i_mode)) { add_entry(ino, last_dir->i_num, ".."); last_dir->i_nlinks++; } release_inode(last_dir); release_inode(dir); release_inode(ino); return OK; err: release_inode(last_dir); release_inode(dir); release_inode(ino); return ERROR; }
//----------------------------------------------------------------- bool FileExists(const std::string& filename) { std::string abs; if (process_path(filename, abs)) { try { return fs::exists(abs); } catch (...) { } } return false; }
//----------------------------------------------------------------- int GetFileSize(const std::string& filename) { std::string abs; if (process_path(filename, abs)) { try { return (int)fs::file_size(abs); } catch (...) { } } return -1; }
//----------------------------------------------------------------- bool IsDirectory(const std::string& filename) { std::string abs; if (process_path(filename, abs)) { try { return fs::is_directory(abs); } catch (...) { } } return false; }
//----------------------------------------------------------------- bool CreateDirectory(const std::string& directory) { std::string abs; if (process_path(directory, abs)) { try { return fs::create_directory(abs); } catch (...) { } } return false; }
//----------------------------------------------------------------- bool IsFile(const std::string& filename) { std::string abs; if (process_path(filename, abs)) { try { return fs::is_regular_file(abs); } catch (...) { } } return false; }
//----------------------------------------------------------------- int GetFileModTime(const std::string& filename) { std::string abs; if (process_path(filename, abs)) { try { return (int)fs::last_write_time(abs); } catch (...) { } } return -1; }
/* Initializes the driver, processes the specified arguments and reports the * clusters found. */ void process_args(int argc, char** argv) { size_t i; memset(&recorded_dirs, 0, sizeof(DirList)); for (i = 0; i < BUCKET_COUNT; i++) init_file_list(&buckets[i]); if (argc) { /* Read file names from command line */ for (i = 0; i < argc; i++) { kill_trailing_slashes(argv[i]); process_path(argv[i], 0); } } else { char* path; /* Read file names from stdin */ while ((path = read_path(stdin))) { kill_trailing_slashes(path); process_path(path, 0); free(path); } } if (unique_files_flag) process_uniques(); else process_clusters(); for (i = 0; i < BUCKET_COUNT; i++) free_file_list(&buckets[i]); free(recorded_dirs.dirs); memset(&recorded_dirs, 0, sizeof(DirList)); }
//----------------------------------------------------------------- bool RemoveFile(const std::string& filename) { std::string abs; if (process_path(filename, abs)) { try { fs::remove(abs); return true; } catch (...) { } } return false; }
static void do_process_top_dir (char *pathname, char *base, int mode, ino_t inum, struct stat *pstat) { (void) pstat; process_path (pathname, base, false, ".", mode, inum); complete_pending_execdirs (); }
//----------------------------------------------------------------- IFile* OpenFile(const std::string& filename, int mode) { std::string abs; if (process_path(filename, abs)) { RefPtr<File> file = File::Create(); if (file->open(abs, mode)) { file->setName(filename); return file.release(); } } return 0; }
void process_dir(char* pathname) { DIR *dir = opendir(pathname); if(dir == NULL) fprintf(stderr,"%s: \"%s\": %s\n", program_name, pathname, strerror_l(errno, locale)); else { ++current_level; process_path(pathname); --current_level; } closedir(dir); }
int mozilla2john(int argc, char **argv) { int i; if (argc < 2) { fprintf(stderr, "Usage: mozilla2john [key3.db files]\n"); return -1; } for (i = 1; i < argc; i++) process_path(argv[i]); return 0; }
void process_directory(const char* path) { struct dirent *dirThing; DIR *directory = opendir(path); chdir(path); while((dirThing = readdir(directory)) != 0){ if(!(strcmp(dirThing->d_name, ".") == 0 || strcmp(dirThing->d_name, "..") == 0)){ process_path(dirThing->d_name); } } closedir(directory); num_dirs++; chdir(".."); }
//----------------------------------------------------------------- bool EnumerateFiles(const std::string& directory, std::vector<std::string>& fileList) { std::string abs; if (process_path(directory, abs)) { try { fileList.clear(); fs::directory_iterator end_iter; for (fs::directory_iterator iter(abs); iter != end_iter; ++iter) { fileList.push_back(iter->path().filename()); } return true; } catch (...) { } } return false; }
/* On success, returns a Potrace state st with st->status == POTRACE_STATUS_OK. On failure, returns NULL if no Potrace state could be created (with errno set), or returns an incomplete Potrace state (with st->status == POTRACE_STATUS_INCOMPLETE, and with errno set). Complete or incomplete Potrace state can be freed with potrace_state_free(). */ potrace_state_t *potrace_trace(const potrace_param_t *param, const potrace_bitmap_t *bm) { int r; path_t *plist = NULL; potrace_state_t *st; progress_t prog; progress_t subprog; /* prepare private progress bar state */ prog.callback = param->progress.callback; prog.data = param->progress.data; prog.min = param->progress.min; prog.max = param->progress.max; prog.epsilon = param->progress.epsilon; prog.d_prev = param->progress.min; /* allocate state object */ st = (potrace_state_t *)malloc(sizeof(potrace_state_t)); if (!st) { return NULL; } progress_subrange_start(0.0, 0.1, &prog, &subprog); /* process the image */ r = bm_to_pathlist(bm, &plist, param, &subprog); if (r) { free(st); return NULL; } st->status = POTRACE_STATUS_OK; st->plist = plist; st->priv = NULL; /* private state currently unused */ progress_subrange_end(&prog, &subprog); progress_subrange_start(0.1, 1.0, &prog, &subprog); /* partial success. */ r = process_path(plist, param, &subprog); if (r) { st->status = POTRACE_STATUS_INCOMPLETE; } progress_subrange_end(&prog, &subprog); return st; }
int main (int argc, char *argv[]) { // Ensure an argument was provided. if (argc != 2) { printf ("Usage: %s <path>\n", argv[0]); printf (" where <path> is the file or root of the tree you want to summarize.\n"); return 1; } num_dirs = 0; num_regular = 0; process_path(argv[1]); printf("Processed all the files from <%s>.\n", argv[1]); printf("There were %d directories.\n", num_dirs); printf("There were %d regular files.\n", num_regular); return 0; }
void process_directory(const char* path) { ++num_dirs; DIR *dir = opendir(path); struct dirent *entry; chdir(path); while((entry = readdir(dir)) != NULL) { if(strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0){ process_path(entry->d_name); } } closedir(dir); chdir(".."); }
/* Recurses into a directory, collecting all or all non-hidden files, * according to the specified options. */ static void process_directory(const char* path, const struct stat* sb, int depth) { DIR* dir; struct dirent* dir_entry; char* child_path; const char* name; if (has_recorded_directory(sb->st_dev, sb->st_ino)) return; record_directory(sb->st_dev, sb->st_ino); dir = opendir(path); if (!dir) { if (!quiet_flag) warning("%s: %s", path, strerror(errno)); return; } while ((dir_entry = readdir(dir))) { name = dir_entry->d_name; if (name[0] == '.') { if (!all_files_flag) continue; if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0) continue; } if (asprintf(&child_path, "%s/%s", path, name) < 0) error(_("Out of memory")); process_path(child_path, depth); free(child_path); } closedir(dir); }
/* create new directory */ int fs_mkdir(const char *pathname, mode_t mode) { struct inode_s *ino, *dir, *tmpdir; char path[MAX_PATH], name[MAX_NAME]; ino = dir = tmpdir = NULL; process_path(pathname, path, name); tmpdir = current_dir(); /* get inode numbers from parent and new dir */ if ( (dir = find_inode(tmpdir, path, FS_SEARCH_GET)) == NULL) goto err; release_inode(tmpdir); if ( (ino = find_inode(dir, name, FS_SEARCH_ADD)) == NULL) goto err; /* fill new dir inode */ fill_inode(ino, mode); ino->i_mode = (ino->i_mode & ~I_TYPE) | I_DIRECTORY; /* add '.' */ empty_entry(ino, ino->i_num, "."); ino->i_nlinks++; /* and '..' */ empty_entry(ino, dir->i_num, ".."); dir->i_nlinks++; dir->i_dirty = 1; /* update dir size */ ino->i_size = DIRENTRY_SIZE * 2; release_inode(dir); release_inode(ino); return OK; err: release_inode(tmpdir); release_inode(dir); release_inode(ino); return ERROR; }
int extract_main(int argc, char** argv) { parse_extract_options(argc, argv); std::ofstream ofs; if (not opt::output_file.empty()) { ofs.open(opt::output_file); os_p = &ofs; } else { os_p = &std::cout; } for (unsigned i = 0; i < opt::paths.size(); ++i) { process_path(opt::paths[i]); } return 0; }
static void update_one(const char *path) { int stat_errno = 0; struct stat st; if (mark_valid_only || mark_skip_worktree_only || force_remove || mark_fsmonitor_only) st.st_mode = 0; else if (lstat(path, &st) < 0) { st.st_mode = 0; stat_errno = errno; } /* else stat is valid */ if (!verify_path(path, st.st_mode)) { fprintf(stderr, "Ignoring path %s\n", path); return; } if (mark_valid_only) { if (mark_ce_flags(path, CE_VALID, mark_valid_only == MARK_FLAG)) die("Unable to mark file %s", path); return; } if (mark_skip_worktree_only) { if (mark_ce_flags(path, CE_SKIP_WORKTREE, mark_skip_worktree_only == MARK_FLAG)) die("Unable to mark file %s", path); return; } if (mark_fsmonitor_only) { if (mark_ce_flags(path, CE_FSMONITOR_VALID, mark_fsmonitor_only == MARK_FLAG)) die("Unable to mark file %s", path); return; } if (force_remove) { if (remove_file_from_cache(path)) die("git update-index: unable to remove %s", path); report("remove '%s'", path); return; } if (process_path(path, &st, stat_errno)) die("Unable to process path %s", path); report("add '%s'", path); }
void process_top_path(char* pathname) { bool free_path = false; //On ajoute un / à la fin du pathname si il n'en contient pas déjà un if(pathname[strlen(pathname)-1] == '/') path_process = pathname; else { free_path = true; path_process = malloc(strlen(pathname)+1); if(path_process == NULL) { error_mem(); exit(EXIT_FAILURE); } memcpy(path_process, pathname, strlen(pathname)); path_process[strlen(pathname)] = '/'; } /*On se place dans le dossier de départ pour que les commandes suivantes comprennent les chemins relatifs */ if(chdir(path_process) != -1) { //Si l'ouverture s'est bien passé on execute process_path struct stat stat_file; if(lstat(pathname, &stat_file) != -1) { if(current_level >= mindepth) apply_predicates(pathname, &stat_file); if(!S_ISLNK(stat_file.st_mode)) process_path(path_process); } } else { if(errno == EACCES) { //Si l'ouveture ne s'est pas bien passé et que c'est un problème de permissions struct stat stat_file; if(lstat(path_process, &stat_file) != -1 && current_level >= mindepth) { apply_predicates(pathname, &stat_file); } } fprintf(stderr,"%s: \"%s\": %s\n", program_name, pathname, strerror_l(errno, locale)); } if(free_path) free(path_process); }
/* remove directory (only if it is empty) */ int fs_rmdir(const char *pathname) { struct inode_s *ino, *dir, *tmpdir; char path[MAX_PATH], name[MAX_NAME]; ino = dir = tmpdir = NULL; process_path(pathname, path, name); tmpdir = current_dir(); /* get inode numbers from parent and new dir */ if ( (dir = find_inode(tmpdir, path, FS_SEARCH_GET)) == NULL) goto err; release_inode(tmpdir); if ( (ino = find_inode(dir, name, FS_SEARCH_ADD)) == NULL) goto err; /* check if its a dir and it is empty */ if (!IS_DIR(ino->i_mode) || ino->i_size != DIRENTRY_SIZE * 2) goto err; /* this cant give an error */ release_inode(find_inode(dir, name, FS_SEARCH_REMOVE)); /* free blocks and inode */ rm_inode(ino->i_num); release_inode(dir); release_inode(ino); return OK; err: release_inode(tmpdir); release_inode(dir); release_inode(ino); return ERROR; }