linux

如何用readdir实现目录同步

小樊
42
2025-07-16 13:17:24
栏目: 编程语言

使用 readdir 实现目录同步的基本思路是遍历源目录和目标目录,比较两者的文件和子目录,并根据需要进行创建、删除或更新操作。以下是一个使用 C 语言和 readdir 函数实现目录同步的示例代码:

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

// 函数声明
void sync_directory(const char *src, const char *dst);
int is_directory(const char *path);
void copy_file(const char *src, const char *dst);
void remove_file(const char *path);
void create_directory(const char *path);

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

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

    sync_directory(src_dir, dst_dir);

    return 0;
}

void sync_directory(const char *src, const char *dst) {
    DIR *src_dir = opendir(src);
    if (!src_dir) {
        perror("opendir");
        return;
    }

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

        char src_path[1024], dst_path[1024];
        snprintf(src_path, sizeof(src_path), "%s/%s", src, entry->d_name);
        snprintf(dst_path, sizeof(dst_path), "%s/%s", dst, 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 (!is_directory(dst_path)) {
                printf("Creating directory: %s
", dst_path);
                create_directory(dst_path);
            }
            sync_directory(src_path, dst_path);
        } else {
            if (S_ISDIR(dst_stat.st_mode)) {
                printf("Removing directory: %s
", dst_path);
                remove_directory(dst_path);
            }

            if (src_stat.st_mtime > dst_stat.st_mtime) {
                printf("Copying file: %s -> %s
", src_path, dst_path);
                copy_file(src_path, dst_path);
            }
        }
    }

    closedir(src_dir);

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

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

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

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

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

        if (!is_directory(src_path)) {
            printf("Removing file: %s
", dst_path);
            remove_file(dst_path);
        }
    }

    closedir(dst_dir);
}

int is_directory(const char *path) {
    struct stat statbuf;
    return stat(path, &statbuf) == 0 && S_ISDIR(statbuf.st_mode);
}

void copy_file(const char *src, const char *dst) {
    FILE *src_file = fopen(src, "rb");
    FILE *dst_file = fopen(dst, "wb");
    if (!src_file || !dst_file) {
        perror("fopen");
        return;
    }

    char buffer[1024];
    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);
}

void remove_file(const char *path) {
    if (remove(path) != 0) {
        perror("remove");
    }
}

void create_directory(const char *path) {
    if (mkdir(path, 0755) != 0) {
        perror("mkdir");
    }
}

说明

  1. 遍历目录:使用 opendirreaddir 遍历源目录和目标目录。
  2. 比较文件和目录:使用 stat 函数获取文件和目录的信息,并进行比较。
  3. 创建目录:如果源目录中的目录在目标目录中不存在,则创建该目录。
  4. 复制文件:如果源目录中的文件比目标目录中的文件更新,则复制该文件。
  5. 删除文件和目录:删除目标目录中存在但源目录中不存在的文件和目录。

注意事项

请根据实际需求进行调整和扩展。

0
看了该问题的人还看了