copirdir
函數在 Linux 中用于復制目錄及其內容。它與 cp -r
命令的功能類似,但提供了程序化的接口。以下是對 copirdir
函數的詳細實例分析,包括其使用方法、參數說明以及示例代碼。
copirdir
函數簡介copirdir
是一個 POSIX 標準函數,定義在 <dirent.h>
頭文件中。它用于遞歸地復制一個目錄及其所有子目錄和文件到目標位置。
#include <dirent.h>
int copirdir(const char *srcdir, const char *dstdir);
srcdir
:源目錄的路徑。dstdir
:目標目錄的路徑。0
。-1
,并設置相應的 errno
。使用 copirdir
函數復制目錄的基本步驟如下:
<dirent.h>
和其他必要的頭文件。opendir
打開源目錄以讀取其內容。readdir
逐個讀取目錄中的條目。.
和 ..
。copirdir
或使用其他函數(如 copyfile
)復制文件。closedir
關閉打開的目錄。errno
處理可能出現的錯誤。以下是一個使用 copirdir
函數復制目錄的示例程序:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <sys/stat.h>
#include <errno.h>
// 自定義函數:復制文件
int copyfile(const char *src, const char *dst) {
FILE *sf = fopen(src, "rb");
if (!sf) {
perror("fopen source file");
return -1;
}
FILE *df = fopen(dst, "wb");
if (!df) {
perror("fopen destination file");
fclose(sf);
return -1;
}
size_t n;
char buffer[4096];
while ((n = fread(buffer, 1, sizeof(buffer), sf)) > 0) {
if (fwrite(buffer, 1, n, df) != n) {
perror("fwrite");
fclose(sf);
fclose(df);
return -1;
}
}
fclose(sf);
fclose(df);
return 0;
}
// copirdir 函數實現
int copirdir(const char *srcdir, const char *dstdir) {
struct stat st;
DIR *dp = opendir(srcdir);
if (!dp) {
perror("opendir");
return -1;
}
// 創建目標目錄
if (mkdir(dstdir, 0755) == -1 && errno != EEXIST) {
perror("mkdir");
closedir(dp);
return -1;
}
struct dirent *ep;
while ((ep = readdir(dp)) != NULL) {
if (strcmp(ep->d_name, ".") == 0 || strcmp(ep->d_name, "..") == 0)
continue;
char srcpath[PATH_MAX];
char dstpath[PATH_MAX];
snprintf(srcpath, sizeof(srcpath), "%s/%s", srcdir, ep->d_name);
snprintf(dstpath, sizeof(dstpath), "%s/%s", dstdir, ep->d_name);
if (stat(srcpath, &st) == -1) {
perror("stat");
closedir(dp);
return -1;
}
if (S_ISDIR(st.st_mode)) {
// 遞歸復制子目錄
if (copirdir(srcpath, dstpath) == -1)
goto cleanup;
} else {
// 復制文件
if (copyfile(srcpath, dstpath) == -1)
goto cleanup;
}
}
cleanup:
closedir(dp);
return 0;
}
int main(int argc, char *argv[]) {
if (argc != 3) {
fprintf(stderr, "Usage: %s <source_directory> <destination_directory>\n", argv[0]);
return EXIT_FAILURE;
}
const char *src = argv[1];
const char *dst = argv[2];
if (copirdir(src, dst) == 0) {
printf("Directory copied successfully from %s to %s\n", src, dst);
} else {
fprintf(stderr, "Failed to copy directory from %s to %s\n", src, dst);
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
copyfile
函數:
copirdir
函數:
stat
獲取文件或目錄的信息。copirdir
。copyfile
進行復制。main
函數:
copirdir
進行復制操作,并根據返回值輸出相應的信息。保存上述代碼為 copydir.c
,然后使用以下命令編譯:
gcc -o copydir copydir.c
運行程序,例如將 /path/to/source
復制到 /path/to/destination
:
./copydir /path/to/source /path/to/destination
lstat
并根據 S_ISLNK
判斷。copirdir
不保證復制操作的原子性。如果在復制過程中發生錯誤,可能會導致部分文件被復制。copyfile
函數,該函數需要自行實現或使用其他庫函數(如 sendfile
)進行優化。雖然 copirdir
提供了遞歸復制目錄的功能,但在實際開發中,許多程序員選擇使用系統調用或高級庫函數來實現類似功能,例如:
system("cp -r source destination")
:通過調用 shell 命令進行復制,簡單但可移植性較差。shutil
模塊(Python):在 Python 中,可以使用 shutil.copytree
進行目錄復制。librsync
、Boost.Filesystem
等,提供更強大的文件操作功能。選擇具體實現方式時,應考慮項目需求、性能要求和跨平臺兼容性等因素。