温馨提示×

如何通过readdir实现Debian文件同步

小樊
45
2025-11-10 21:30:11
栏目: 智能运维

在Debian系统中,readdir函数是用于读取目录内容的系统调用。要使用readdir实现文件同步,你需要编写一个程序来比较源目录和目标目录的内容,并根据需要复制或删除文件以使它们保持同步。

以下是一个简单的示例,展示了如何使用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, const char *dest);

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

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

    sync_directories(src_dir, dest_dir);

    return EXIT_SUCCESS;
}

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

    struct dirent *entry;
    while ((entry = readdir(src_dir)) != NULL) {
        // Skip current and parent directory entries
        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, entry->d_name);

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

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

        if (S_ISDIR(src_stat.st_mode)) {
            // Directory exists in source, check if it exists in destination
            if (access(dest_path, F_OK) == -1) {
                // Destination directory does not exist, create it
                mkdir(dest_path, src_stat.st_mode);
            } else {
                // Directories exist, sync them recursively
                sync_directories(src_path, dest_path);
            }
        } else {
            // File exists in source, check if it exists in destination
            if (access(dest_path, F_OK) == -1) {
                // Destination file does not exist, copy it
                FILE *src_file = fopen(src_path, "rb");
                FILE *dest_file = fopen(dest_path, "wb");
                if (src_file && dest_file) {
                    char buffer[4096];
                    size_t bytes_read;
                    while ((bytes_read = fread(buffer, 1, sizeof(buffer), src_file)) > 0) {
                        fwrite(buffer, 1, bytes_read, dest_file);
                    }
                    fclose(src_file);
                    fclose(dest_file);
                } else {
                    perror("copy file");
                }
            } else {
                // Files exist, check if they are the same
                FILE *src_file = fopen(src_path, "rb");
                FILE *dest_file = fopen(dest_path, "rb");
                if (src_file && dest_file) {
                    fseek(src_file, 0, SEEK_END);
                    fseek(dest_file, 0, SEEK_END);
                    if (ftell(src_file) == ftell(dest_file)) {
                        fseek(src_file, 0, SEEK_SET);
                        fseek(dest_file, 0, SEEK_SET);
                        char buffer[4096];
                        size_t bytes_read;
                        while ((bytes_read = fread(buffer, 1, sizeof(buffer), src_file)) > 0) {
                            if (memcmp(buffer, buffer + bytes_read, bytes_read) != 0) {
                                // Files are different, copy the updated file
                                fclose(dest_file);
                                FILE *temp_dest_file = fopen(dest_path, "wb");
                                if (temp_dest_file) {
                                    fwrite(buffer, 1, bytes_read, temp_dest_file);
                                    fclose(temp_dest_file);
                                } else {
                                    perror("copy file");
                                }
                                break;
                            }
                        }
                    }
                    fclose(src_file);
                    fclose(dest_file);
                } else {
                    perror("compare files");
                }
            }
        }
    }

    closedir(src_dir);

    // Check for files in the destination directory that are not in the source directory
    src_dir = opendir(dest);
    if (!src_dir) {
        perror("opendir");
        return;
    }

    while ((entry = readdir(src_dir)) != NULL) {
        // Skip current and parent directory entries
        if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
            continue;
        }

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

        if (access(dest_path, F_OK) == -1) {
            // File does not exist in source, delete it
            remove(dest_path);
        }
    }

    closedir(src_dir);
}

这个程序接受两个命令行参数:源目录和目标目录。它会递归地比较这两个目录的内容,并确保它们保持同步。如果目标目录中存在源目录中没有的文件或目录,程序会删除它们。

请注意,这个示例程序是一个简单的实现,它没有处理所有的边缘情况和错误检查。在实际使用中,你可能需要添加更多的错误处理和日志记录功能。此外,这个程序不会处理符号链接、设备文件或其他特殊类型的文件。如果你需要处理这些情况,你可能需要扩展程序的功能。

0