linux

如何用copendir实现目录同步功能

小樊
42
2025-12-13 05:05:38
栏目: 编程语言

使用 opendir 实现目录同步功能,通常涉及遍历源目录和目标目录,比较文件和子目录,并根据需要进行复制、删除或更新操作。以下是一个使用 C 语言和 opendir 函数实现目录同步的基本示例。

步骤概述

  1. 遍历源目录:使用 opendirreaddir 遍历源目录中的所有条目。
  2. 遍历目标目录:同样使用 opendirreaddir 遍历目标目录中的所有条目。
  3. 比较文件和目录
    • 如果源目录中有而目标目录中没有的条目,进行复制。
    • 如果目标目录中有而源目录中没有的条目,进行删除。
    • 如果条目都存在,比较修改时间或大小,决定是否需要更新。
  4. 递归处理子目录

示例代码

以下是一个简单的示例代码,演示如何使用 opendir 实现基本的目录同步功能:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#include <time.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 remove_directory(const char *path);

int main(int argc, char *argv[]) {
    if (argc != 3) {
        fprintf(stderr, "用法: %s <源目录> <目标目录>\n", argv[0]);
        return EXIT_FAILURE;
    }

    const char *source = argv[1];
    const char *destination = argv[2];

    sync_directory(source, destination);

    return EXIT_SUCCESS;
}

void sync_directory(const char *src, const char *dst) {
    DIR *dir_src = opendir(src);
    if (!dir_src) {
        perror("无法打开源目录");
        return;
    }

    struct dirent *entry;
    while ((entry = readdir(dir_src)) != 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 st_src, st_dst;
        if (stat(src_path, &st_src) == -1 || stat(dst_path, &st_dst) == -1) {
            perror("stat 失败");
            continue;
        }

        if (S_ISDIR(st_src.st_mode)) {
            if (!is_directory(dst_path)) {
                printf("创建目录: %s\n", dst_path);
                mkdir(dst_path, st_src.st_mode);
            }
            sync_directory(src_path, dst_path); // 递归同步子目录
        } else {
            if (!is_directory(dst_path)) {
                printf("复制文件: %s -> %s\n", src_path, dst_path);
                copy_file(src_path, dst_path);
            } else {
                // 如果目标路径是目录,可以选择删除或移动文件
                printf("目标路径是目录,跳过文件复制: %s\n", src_path);
            }
        }
    }

    closedir(dir_src);

    // 删除目标目录中有而源目录中没有的文件
    DIR *dir_dst = opendir(dst);
    if (!dir_dst) {
        perror("无法打开目标目录");
        return;
    }

    while ((entry = readdir(dir_dst)) != 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 st_dst;
        if (stat(dst_path, &st_dst) == -1) {
            perror("stat 失败");
            continue;
        }

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

        struct stat st_src;
        if (stat(src_path, &st_src) == -1) {
            printf("删除目标中多余的文件: %s\n", dst_path);
            remove_file(dst_path);
        }
    }

    closedir(dir_dst);
}

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

void copy_file(const char *src, const char *dst) {
    FILE *fp_src = fopen(src, "rb");
    if (!fp_src) {
        perror("无法打开源文件");
        return;
    }

    FILE *fp_dst = fopen(dst, "wb");
    if (!fp_dst) {
        perror("无法打开目标文件");
        fclose(fp_src);
        return;
    }

    char buffer[4096];
    size_t bytes_read;
    while ((bytes_read = fread(buffer, 1, sizeof(buffer), fp_src)) > 0) {
        fwrite(buffer, 1, bytes_read, fp_dst);
    }

    fclose(fp_src);
    fclose(fp_dst);

    // 可选:同步文件系统缓存
    if (fsync(fileno(fp_dst)) == -1) {
        perror("fsync 失败");
    }
}

void remove_file(const char *path) {
    if (remove(path) != 0) {
        perror("无法删除文件");
    }
}

void remove_directory(const char *path) {
    if (rmdir(path) != 0) {
        perror("无法删除目录");
    }
}

代码说明

  1. 主函数

    • 检查命令行参数是否正确。
    • 调用 sync_directory 函数开始同步。
  2. sync_directory 函数

    • 打开源目录和目标目录。
    • 遍历源目录中的每个条目:
      • 如果是目录,检查目标目录是否存在该目录,不存在则创建,并递归调用 sync_directory
      • 如果是文件,检查目标目录是否存在该文件,不存在则复制。
    • 遍历目标目录中的每个条目,删除源目录中不存在的文件。
  3. 辅助函数

    • is_directory:判断路径是否为目录。
    • copy_file:复制文件内容。
    • remove_file:删除文件。
    • remove_directory:删除目录。

注意事项

扩展功能

根据需求,您还可以扩展以下功能:

总结

使用 opendir 实现目录同步功能需要仔细处理目录和文件的遍历、比较和操作。上述示例提供了一个基本的框架,您可以根据具体需求进行扩展和优化。

0
看了该问题的人还看了