/** * initiate the cow-copy action */ int cow_cp(const char *path, int branch_ro, int branch_rw, bool copy_dir) { DBG("%s\n", path); // create the path to the file path_create_cutlast(path, branch_ro, branch_rw); char from[PATHLEN_MAX], to[PATHLEN_MAX]; if (BUILD_PATH(from, uopt.branches[branch_ro].path, path)) RETURN(-ENAMETOOLONG); if (BUILD_PATH(to, uopt.branches[branch_rw].path, path)) RETURN(-ENAMETOOLONG); setlocale(LC_ALL, ""); struct cow cow; cow.uid = getuid(); // Copy the umask for explicit mode setting. cow.umask = umask(0); umask(cow.umask); cow.from_path = from; cow.to_path = to; struct stat buf; lstat(cow.from_path, &buf); cow.stat = &buf; int res; switch (buf.st_mode & S_IFMT) { case S_IFLNK: res = copy_link(&cow); break; case S_IFDIR: if (copy_dir) { res = copy_directory(path, branch_ro, branch_rw); } else { res = path_create(path, branch_ro, branch_rw); } break; case S_IFBLK: case S_IFCHR: res = copy_special(&cow); break; case S_IFIFO: res = copy_fifo(&cow); break; case S_IFSOCK: USYSLOG(LOG_WARNING, "COW of sockets not supported: %s\n", cow.from_path); RETURN(1); default: res = copy_file(&cow); } RETURN(res); }
int bmx_filesystem_copydir(BBString * src, BBString * dst) { boost::filesystem::path s(bbStringToPath(src)); boost::filesystem::path d(bbStringToPath(dst)); boost::system::error_code e; copy_directory(s, d, e); return !e; }
/** * initiate the cow-copy action */ int cow_cp(const char *path, int branch_ro, int branch_rw) { DBG_IN(); // create the path to the file path_create_cutlast(path, branch_ro, branch_rw); char from[PATHLEN_MAX], to[PATHLEN_MAX]; snprintf(from, PATHLEN_MAX, "%s%s", uopt.branches[branch_ro].path, path); snprintf(to, PATHLEN_MAX, "%s%s", uopt.branches[branch_rw].path, path); setlocale(LC_ALL, ""); struct cow cow; cow.uid = getuid(); // Copy the umask for explicit mode setting. cow.umask = umask(0); umask(cow.umask); cow.from_path = from; cow.to_path = to; struct stat buf; lstat(cow.from_path, &buf); cow.stat = &buf; int res; switch (buf.st_mode & S_IFMT) { case S_IFLNK: res = copy_link(&cow); break; case S_IFDIR: res = copy_directory(path, branch_ro, branch_rw); break; case S_IFBLK: case S_IFCHR: res = copy_special(&cow); break; case S_IFIFO: res = copy_fifo(&cow); break; case S_IFSOCK: usyslog(LOG_WARNING, "COW of sockets not supported: %s\n", cow.from_path); return 1; default: res = copy_file(&cow); } return res; }
int copy_directory(const char *dir, const char *to) { struct stat keep_stat; if (stat(dir, &keep_stat) != 0) return fail("copy_directory"); // Check or create target directory. struct stat path_stat; if (stat(to, &path_stat) != 0) { if(mkdir(to, keep_stat.st_mode) != 0) return fail("copy_directory"); } if (lchown(to, keep_stat.st_uid, keep_stat.st_gid) != 0) return fail("copy_directory"); // Open directory. DIR *d; d = opendir(dir); if (!d) return fail("copy_directory"); // Travel all directory entries. struct dirent *e; while ((e = readdir(d)) != NULL) { char path[strlen(dir) + strlen(e->d_name) + 2]; strcpy(path, dir); strcat(path, "/"); strcat(path, e->d_name); char new_path[strlen(to) + strlen(e->d_name) + 2]; strcpy(new_path, to); strcat(new_path, "/"); strcat(new_path, e->d_name); // Copy file contents. if (e->d_type == DT_REG) { if (copy_file(path, new_path) != 0) return 1; } // Process sub directories recursively. else if (e->d_type == DT_DIR) { if (strcmp(e->d_name, ".") != 0 && strcmp(e->d_name, "..") != 0) { if (copy_directory(path, new_path) != 0) return 1; } } } closedir(d); if (rmdir(dir) != 0) return fail("copy_directory"); return 0; }
int move_directory(const char *dir, const char *to) { if (rename(dir, to) != 0) { if (errno == EXDEV) { if (copy_directory(dir, to) != 0) return 1; } else { return fail("move_directory"); } } return 0; }
//----------------------------------------------------------------------------- bool copy_directory( std::string source_path, std::string target_path ) { boost::system::error_code ec; // get the directory if( !get_directory( target_path ) ) return false; // clean this directory, i.e. remove all children but not this directory if( !clean_directory( target_path ) ) return false; boost::filesystem::path src_path( source_path ); boost::filesystem::path tgt_path( target_path ); typedef std::vector<boost::filesystem::path> paths_t; paths_t src_paths; copy( boost::filesystem::directory_iterator( src_path ), boost::filesystem::directory_iterator(), back_inserter( src_paths ) ); for( paths_t::const_iterator it = src_paths.begin(); it != src_paths.end(); it++ ) { boost::filesystem::path::iterator rel_path = --it->end(); boost::filesystem::path next_src_path = *it; boost::filesystem::path next_tgt_path = tgt_path; next_tgt_path /= *rel_path; //std::cout << next_src_path << " -> " << next_tgt_path << '\n'; if( boost::filesystem::is_directory( next_src_path, ec ) ) { // if it is a directory, recurse if( !copy_directory( next_src_path.string(), next_tgt_path.string() ) ) { return false; } } else { // otherwise, let boost handle the copy copy( next_src_path, next_tgt_path, ec ); if( ec != boost::system::errc::success ) { std::cerr << ec.message() << std::endl; return false; } } } return true; }
/* * builtin_cmd * * 내장 명령을 수행한다. * 내장 명령이 아니면 1을 리턴한다. */ int builtin_cmd(int argc, char **argv) { // 내장 명령어 문자열과 argv[0]을 비교하여 각각의 처리 함수 호출 if ( (!strcmp (argv[0], "quit")) || (!strcmp (argv[0], "exit")) ) { exit(0); } if ( !strcmp(argv[0], "ls")){ return list_files(argc, argv); } if ( !strcmp(argv[0], "ll")){ return list_all_files(argc, argv); } if ( !strcmp(argv[0], "cp")){ return copy_file(argc, argv); } if ( !strcmp(argv[0], "rm")){ return remove_file(argc, argv); } if ( !strcmp(argv[0], "move")){ return move_file(argc, argv); } if ( !strcmp(argv[0], "cd")){ return change_directory(argc, argv); } if ( !strcmp(argv[0], "pwd")){ return print_working_directory(); } if ( !strcmp(argv[0], "mkdir")){ return make_directory(argc, argv); } if ( !strcmp(argv[0], "rmdir")){ return remove_directory(argc, argv); } if ( !strcmp(argv[0], "cpdir")){ return copy_directory(argc, argv); } // 내장 명령어가 아님. printf("command not found\n"); return 1; }
static int copy_file(char *src, char *dest, struct options *opts) { int fin; int fout; struct stat st; char *buffer; int n; // Refuse to copy file unto itself if (!opts->force && strcmp(src, dest) == 0) { fprintf(stderr, "%s: cannot copy file unto itself\n"); return 1; } // Open source file fin = open(src, O_RDONLY | O_BINARY); if (fin < 0 || fstat(fin, &st) < 0) { perror(src); return 1; } // If source is a directory copy recursively if requested if (S_ISDIR(st.st_mode)) { close(fin); if (opts->recurse) { return copy_directory(src, dest, opts); } else { errno = EISDIR; perror(src); return 1; } } // Copy source file to destination if (opts->verbose) printf("%s -> %s\n", src, dest); if (opts->force) unlink(dest); fout = open(dest, O_WRONLY | O_CREAT | (opts->noclobber ? O_EXCL : O_TRUNC) | O_BINARY, 0666); buffer = malloc(BLKSIZE); if (fout < 0 || !buffer) { perror(dest); close(fin); return 1; } while ((n = read(fin, buffer, BLKSIZE)) != 0) { if (n < 0) { perror(src); break; } if (write(fout, buffer, n) != n) { perror(dest); break; } } fchmod(fout, st.st_mode); if (opts->preserve) { struct utimbuf times; times.modtime = st.st_mtime; times.actime = st.st_atime; times.ctime = -1; futime(fout, ×); fchown(fout, st.st_uid, st.st_gid); } free(buffer); close(fin); close(fout); return n; }