ubuntu

如何在Ubuntu中高效使用copendir

小樊
43
2025-06-22 05:35:50
栏目: 智能运维

在Ubuntu中,copendir函数用于复制目录及其内容。为了高效地使用copaddir,请遵循以下步骤:

  1. 包含必要的头文件:
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
  1. 创建一个递归函数来复制目录及其内容:
int copy_directory_contents(const char *src, const char *dest) {
    DIR *dir = opendir(src);
    if (!dir) {
        perror("opendir");
        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 dest_path[PATH_MAX];
        snprintf(src_path, sizeof(src_path), "%s/%s", src, entry->d_name);
        snprintf(dest_path, sizeof(dest_path), "%s/%s", dest, entry->d_name);

        struct stat st;
        if (stat(src_path, &st) == -1) {
            perror("stat");
            closedir(dir);
            return -1;
        }

        if (S_ISDIR(st.st_mode)) {
            if (mkdir(dest_path, st.st_mode) == -1) {
                perror("mkdir");
                closedir(dir);
                return -1;
            }
            if (copy_directory_contents(src_path, dest_path) == -1) {
                closedir(dir);
                return -1;
            }
        } else {
            FILE *src_file = fopen(src_path, "rb");
            if (!src_file) {
                perror("fopen");
                closedir(dir);
                return -1;
            }

            FILE *dest_file = fopen(dest_path, "wb");
            if (!dest_file) {
                perror("fopen");
                fclose(src_file);
                closedir(dir);
                return -1;
            }

            char buffer[4096];
            size_t bytes_read;
            while ((bytes_read = fread(buffer, 1, sizeof(buffer), src_file)) > 0) {
                if (fwrite(buffer, 1, bytes_read, dest_file) != bytes_read) {
                    perror("fwrite");
                    fclose(src_file);
                    fclose(dest_file);
                    closedir(dir);
                    return -1;
                }
            }

            fclose(src_file);
            fclose(dest_file);
        }
    }

    closedir(dir);
    return 0;
}
  1. 在主函数中调用copy_directory_contents函数:
int main(int argc, char *argv[]) {
    if (argc != 3) {
        fprintf(stderr, "Usage: %s <source_directory> <destination_directory>\n", argv[0]);
        return 1;
    }

    const char *src = argv[1];
    const char *dest = argv[2];

    if (copy_directory_contents(src, dest) == 0) {
        printf("Directory copied successfully.\n");
    } else {
        fprintf(stderr, "Failed to copy directory.\n");
        return 1;
    }

    return 0;
}
  1. 编译并运行程序:
gcc -o copy_directory copy_directory.c
./copy_directory /path/to/source/directory /path/to/destination/directory

这个示例程序将递归地复制源目录及其内容到目标目录。为了提高效率,可以根据需要调整缓冲区大小。

0
看了该问题的人还看了