在本文中复刻了 cp
实用程序的功能,我想默认使其递归,因为每次我想复制时都输入 -R
文件夹都会觉得有点重复,本文代码将与前文代码保持相似,我们只会更改程序的核心功能和一些变量名称以匹配用例
Pseudo Code 伪代码
function copy(src,dst)->result{
create dst directory
for entry in src{
get file_type
if file_type.is_dir(){
copy(entry.path(), dst.as_ref().join(entry.file_name()))?
} else {
fs::copy(entry.path(), dst.as_ref().join(entry.file_name()))?
}
}
}
args = command_line_arguments
remove the first element of the args vector
if args.length == 0 {
print error_message
}
else if args.contains("--help") {
print help_message
} else {
result = copy(args[0],args[1])
error handling
}
Looks pretty simple, we’ll create a lib.rs and do the basic setup tasks just like yesterday. We’ll create the following :
看起来很简单,我们将创建一个 lib.rs 并像昨天一样执行基本设置任务。我们将创建以下内容:
config
Structconfig
结构体- An implementation block 一个实现块
new
methodnew
方法help
methodhelp
方法not_enough_arguments
methodnot_enough_arguments
方法run
functionrun
函数
and then we’ll write the logic for our copy
function
然后我们将为 copy
函数编写逻辑
Creating a new cargo project and setting up the lib.rs file
创建一个新的 Cargo 项目并设置 lib.rs 文件
use std::path::Path;
use std::{fs, io};
pub struct Config<'a> {
pub files: &'a Vec<String>,
}
impl Config<'_> {
pub fn new(args: &Vec<String>) -> Config {
Config { files: args }
}
fn not_enough_arguments() {
eprintln!("cp: missing operand");
eprintln!("For help use: cp --help");
}
fn help() {
eprintln!("This is a cheap little clone of the cp utility in the GNU core utilities, the catch is that I made it in rust, check out more of my work at medium: https://shafinmurani.medium.com");
eprintln!("This is recursive by default so dont worry about anything, just run it :D");
eprintln!("To use this util: cp source destination/folder_name");
}
pub fn run(&self) {
if self.files.len() == 0 {
Self::not_enough_arguments();
} else if self.files.contains(&String::from("--help")) {
Self::help();
} else {
// copy_function
}
}
}
use std::path::Path;
: This imports thePath
struct from thestd::path
module. ThePath
struct represents file system paths and is used for manipulating and working with file paths.use std::path::Path;
:这会从std::path
模块导入Path
结构。Path
结构表示文件系统路径,用于操作和使用文件路径。use std::{fs, io};
: This imports thefs
module and theio
module from the standard library. These modules provide functionalities related to file system operations (fs
module) and input/output (io
module).use std::{fs, io};
:这会从标准库导入fs
模块和io
模块。这些模块提供与文件系统操作(fs
模块)和输入/输出(io
模块)相关的功能。
That’s all the setup tasks we need to do, now to creating a copy function,
这就是我们需要做的所有设置任务,现在创建一个复制功能,
Let’s first understand our requirements:
我们先来了解一下我们的需求:
This function, copy_dir_all
, is going to be responsible for recursively copying the contents of a directory from a source location to a destination location.
此函数 copy_dir_all
将负责将目录内容从源位置递归复制到目标位置。
This function will take two arguments: src
and dst
该函数将采用两个参数: src
和 dst
fn copy_dir_all(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> io::Result<()> {
fs::create_dir_all(&dst)?;
for entry in fs::read_dir(src)? {
let entry = entry?;
let ty = entry.file_type()?;
if ty.is_dir() {
Self::copy_dir_all(entry.path(), dst.as_ref().join(entry.file_name()))?;
} else {
fs::copy(entry.path(), dst.as_ref().join(entry.file_name()))?;
}
}
Ok(())
}
This function takes two arguments: src
and dst
, both of which must implement the AsRef<Path>
trait. AsRef<Path>
is a trait used for types that can be converted into a Path
. This allows flexibility in accepting different types as source and destination paths.
此函数采用两个参数: src
和 dst
,两者都必须实现 AsRef<Path>
特征。 AsRef<Path>
是用于可以转换为 Path
的类型的特征。这允许灵活地接受不同类型作为源路径和目标路径。
fs::create_dir_all(&dst)?;
This line attempts to create the destination directory recursively using fs::create_dir_all
. If the directory already exists, it will not raise an error. &dst
is used to pass a reference to the destination path.
此行尝试使用 fs::create_dir_all
递归创建目标目录。如果该目录已经存在,则不会引发错误。 &dst
用于传递对目标路径的引用。
for entry in fs::read_dir(src)? {
This line iterates over the entries (files and directories) in the source directory using fs::read_dir
. The read_dir
function returns an iterator over the entries in a directory.
此行使用 fs::read_dir
迭代源目录中的条目(文件和目录)。 read_dir
函数返回目录中条目的迭代器。
let entry = entry?;
This line unwraps the result of iterating over the directory entries, handling any potential errors that may occur.
此行解开目录条目迭代的结果,处理可能发生的任何潜在错误。
let ty = entry.file_type()?;
This line obtains the file type of the current entry. file_type
returns a FileType
representing the type of the file, which can be a directory, file, symbolic link, etc.
该行获取当前条目的文件类型。 file_type
返回表示文件类型的 FileType
,可以是目录、文件、符号链接等。
if ty.is_dir() {
This condition checks if the current entry is a directory.
此条件检查当前条目是否是目录。
Self::copy_dir_all(entry.path(), dst.as_ref().join(entry.file_name()))?;
If the current entry is a directory, the function recursively calls itself (copy_dir_all
) with the path of the subdirectory as the new source and the destination joined with the current entry's name.
如果当前条目是目录,则该函数递归调用自身 ( copy_dir_all
),并将子目录的路径作为新的源,并将目标与当前条目的名称连接起来。
} else {
fs::copy(entry.path(), dst.as_ref().join(entry.file_name()))?;
If the current entry is not a directory (i.e., it's a file), this block is executed.
如果当前条目不是目录(即,它是文件),则执行此块。
This line copies the file from the source path to the destination path using fs::copy
.
此行使用 fs::copy
将文件从源路径复制到目标路径。
The loop continues until all entries in the source directory have been processed.
循环继续,直到源目录中的所有条目都已处理完毕。
If the function executes without any errors, it returns Ok(())
, indicating success. This ends the function definition.
如果函数执行没有任何错误,则返回 Ok(())
,表示成功。函数定义到此结束。
Let’s implement this in our run
function,
让我们在 run
函数中实现它,
pub fn run(&self) {
if self.files.len() == 0 {
Self::not_enough_arguments();
} else if self.files.contains(&String::from("--help")) {
Self::help();
} else {
let result = Self::copy_dir_all(self.files[0].clone(), self.files[1].clone());
match result {
Ok(()) => {}
Err(e) => {
eprintln!("Application error: {}", e);
}
};
}
}
If there are no arguments provided:
如果没有提供参数:
- Print an error message indicating not enough arguments.
打印一条错误消息,指示参数不足。
Else if the --help
argument is present:
否则,如果存在 --help
参数:
- Print a help message explaining how to use the utility.
打印一条帮助消息,解释如何使用该实用程序。
Otherwise: 否则:
- Attempt to copy the contents of the source directory to the destination directory.
尝试将源目录的内容复制到目标目录。 - If successful, do nothing.
如果成功,则不执行任何操作。
If an error occurs: 如果发生错误:
- Print an error message containing the specific error encountered.
打印包含遇到的特定错误的错误消息。
Now the main.rs file 现在是 main.rs 文件
We’ll import the Config
struct, get the command line arguments, call the new
function on the Config
struct and run it, and hopefully it’ll work :D
我们将导入 Config
结构,获取命令行参数,在 Config
结构上调用 new
函数并运行它,希望它能工作:D
use cp::Config;
use std::env;
fn main() {
let mut args: Vec<String> = env::args().collect();
args.remove(0);
let config = Config::new(&args);
config.run();
}
Looks good? 看起来不错?
Practical 实际的
Thats the tree of the /tmp
directory on my docker container
这是我的 docker 容器上 /tmp
目录的树
.
└── test
├── test2 // we will copy this folder out to the /tmp directory
│ └── text_file2.txt
└── text_file.txt
Running this command : 运行这个命令:
# ~/learning_rust/linux_tools/cp/target/debug/cp test/test2/ ./test2
#BINARY #SRC #DESTINATION
and now taking a look at the tree of the /tmp
dir I get the following :
现在看看 /tmp
目录的树,我得到以下信息:
.
├── test
│ ├── test2
│ │ └── text_file2.txt
│ └── text_file.txt
└── test2
└── text_file2.txt
So, it does work :D
所以,它确实有效:D