linux

如何利用readdir实现目录同步功能

小樊
37
2025-06-24 06:07:52
栏目: 编程语言

readdir 是一个用于读取目录内容的函数,通常在 C 语言中使用。要实现目录同步功能,你需要比较源目录和目标目录的内容,并根据需要添加、删除或更新文件。以下是一个简单的示例,展示了如何使用 readdir 实现目录同步功能:

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

void sync_directories(const char *src_dir, const char *dst_dir);

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

    const char *src_dir = argv[1];
    const char *dst_dir = argv[2];

    sync_directories(src_dir, dst_dir);

    return 0;
}

void sync_directories(const char *src_dir, const char *dst_dir) {
    DIR *src_dirent = opendir(src_dir);
    if (!src_dirent) {
        perror("opendir");
        return;
    }

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

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

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

        struct stat src_stat, dst_stat;
        if (stat(src_path, &src_stat) == -1 || stat(dst_path, &dst_stat) == -1) {
            perror("stat");
            continue;
        }

        if (S_ISDIR(src_stat.st_mode)) {
            if (access(dst_path, F_OK) == -1) {
                mkdir(dst_path, src_stat.st_mode);
            } else {
                sync_directories(src_path, dst_path);
            }
        } else {
            if (access(dst_path, F_OK) == -1 || src_stat.st_mtime - dst_stat.st_mtime > 1) {
                FILE *src_file = fopen(src_path, "rb");
                FILE *dst_file = fopen(dst_path, "wb");

                if (src_file && dst_file) {
                    char buffer[4096];
                    size_t bytes_read;
                    while ((bytes_read = fread(buffer, 1, sizeof(buffer), src_file)) > 0) {
                        fwrite(buffer, 1, bytes_read, dst_file);
                    }

                    fclose(src_file);
                    fclose(dst_file);
                } else {
                    perror("fopen");
                }
            }
        }
    }

    closedir(src_dirent);

    // 删除目标目录中不存在于源目录的文件
    DIR *dst_dirent = opendir(dst_dir);
    if (!dst_dirent) {
        perror("opendir");
        return;
    }

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

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

        if (access(dst_path, F_OK) == -1) {
            remove(dst_path);
        }
    }

    closedir(dst_dirent);
}

这个示例程序接受两个命令行参数:源目录和目标目录。它会递归地比较这两个目录的内容,并根据需要创建、删除或更新文件。请注意,这个程序没有处理符号链接、设备文件等其他特殊文件类型,也没有处理权限问题。在实际使用中,你可能需要根据具体需求对其进行修改和优化。

0
看了该问题的人还看了