温馨提示×

Linux copendir如何与其他函数配合使用

小樊
38
2025-12-05 07:00:52
栏目: 编程语言

在Linux中,cop毡ir函数通常用于复制目录及其内容。要与其他函数配合使用,您需要了解copafdir的工作原理以及如何处理错误和异常情况。以下是一个简单的示例,展示了如何使用copafdir与其他函数(如opendirreaddirclosedir)配合使用以复制目录结构。

#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>

int copy_file(const char *src, const char *dst) {
    int src_fd = open(src, O_RDONLY);
    if (src_fd < 0) {
        perror("open source file");
        return -1;
    }

    int dst_fd = open(dst, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
    if (dst_fd < 0) {
        perror("open destination file");
        close(src_fd);
        return -1;
    }

    ssize_t n;
    char buffer[4096];
    while ((n = read(src_fd, buffer, sizeof(buffer))) > 0) {
        if (write(dst_fd, buffer, n) != n) {
            perror("write to destination file");
            close(src_fd);
            close(dst_fd);
            return -1;
        }
    }

    if (n < 0) {
        perror("read from source file");
        close(src_fd);
        close(dst_fd);
        return -1;
    }

    close(src_fd);
    close(dst_fd);
    return 0;
}

int copafdir(const char *src, const char *dst) {
    struct stat st;
    if (stat(src, &st) < 0) {
        perror("stat source directory");
        return -1;
    }

    if (!S_ISDIR(st.st_mode)) {
        fprintf(stderr, "Source path is not a directory: %s\n", src);
        return -1;
    }

    int ret = mkdir(dst, st.st_mode);
    if (ret < 0 && errno != EEXIST) {
        perror("mkdir destination directory");
        return -1;
    }

    DIR *dir = opendir(src);
    if (!dir) {
        perror("opendir source directory");
        return -1;
    }

    struct dirent *entry;
    while ((entry = readdir(dir)) != NULL) {
        if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
            continue;
        }

        char src_path[PATH_MAX];
        char dst_path[PATH_MAX];
        snprintf(src_path, sizeof(src_path), "%s/%s", src, entry->d_name);
        snprintf(dst_path, sizeof(dst_path), "%s/%s", dst, entry->d_name);

        if (S_ISDIR(st.st_mode)) {
            if (copafdir(src_path, dst_path) < 0) {
                closedir(dir);
                return -1;
            }
        } else {
            if (copy_file(src_path, dst_path) < 0) {
                closedir(dir);
                return -1;
            }
        }
    }

    closedir(dir);
    return 0;
}

int main(int argc, char *argv[]) {
    if (argc != 3) {
        fprintf(stderr, "Usage: %s <source_directory> <destination_directory>\n", argv[0]);
        return 1;
    }

    if (copafdir(argv[1], argv[2]) < 0) {
        fprintf(stderr, "Failed to copy directory from %s to %s\n", argv[1], argv[2]);
        return 1;
    }

    printf("Directory copied successfully from %s to %s\n", argv[1], argv[2]);
    return 0;
}

这个示例中的copafdir函数首先检查源路径是否为目录,然后创建目标目录。接下来,它使用opendir打开源目录,并使用readdir遍历目录中的每个条目。对于每个条目,它检查是否为目录,如果是,则递归调用copafdir;否则,它调用copy_file函数复制文件。

请注意,这个示例仅用于演示目的,可能需要根据您的需求进行调整。在实际应用中,您可能需要处理更多的错误情况和边缘情况。

0