溫馨提示×

Linux readdir如何支持自定義屬性

小樊
44
2025-07-04 09:42:21
欄目: 智能運維

在Linux中,readdir函數用于讀取目錄中的條目。默認情況下,readdir只能訪問標準的文件屬性,如文件名、大小、類型等。如果你希望在使用readdir時訪問自定義屬性,可以通過以下幾種方法實現:

1. 使用擴展屬性(Extended Attributes)

Linux支持擴展屬性,允許你在文件或目錄上存儲額外的元數據。你可以使用getfattrsetfattr命令來操作這些屬性。

設置擴展屬性:

setfattr -n user.my_custom_attr -v "value" filename

獲取擴展屬性:

getfattr -n user.my_custom_attr filename

在C代碼中使用擴展屬性:

要使用擴展屬性,你需要在代碼中包含相應的頭文件,并使用相關的系統調用。

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <attr/xattr.h>

int main() {
    DIR *dir = opendir(".");
    struct dirent *entry;

    if (dir == NULL) {
        perror("opendir");
        return 1;
    }

    while ((entry = readdir(dir)) != NULL) {
        char path[PATH_MAX];
        snprintf(path, sizeof(path), "./%s", entry->d_name);

        // 獲取擴展屬性
        char attr_value[1024];
        ssize_t len = getfattr(path, "user.my_custom_attr", attr_value, sizeof(attr_value));
        if (len > 0) {
            attr_value[len] = '\0'; // 確保字符串終止
            printf("File: %s, Custom Attr: %s\n", entry->d_name, attr_value);
        }
    }

    closedir(dir);
    return 0;
}

2. 使用readdir結合stat獲取更多信息

雖然readdir本身不直接支持自定義屬性,但你可以結合使用stat系統調用來獲取文件的更多信息。

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

int main() {
    DIR *dir = opendir(".");
    struct dirent *entry;

    if (dir == NULL) {
        perror("opendir");
        return 1;
    }

    while ((entry = readdir(dir)) != NULL) {
        char path[PATH_MAX];
        snprintf(path, sizeof(path), "./%s", entry->d_name);

        struct stat st;
        if (stat(path, &st) == 0) {
            printf("File: %s, Size: %ld\n", entry->d_name, st.st_size);
            // 你可以在這里檢查更多的文件屬性
        }
    }

    closedir(dir);
    return 0;
}

3. 使用inotify監控文件變化

如果你需要在文件屬性發生變化時實時響應,可以使用inotify接口。

#include <sys/inotify.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

int main() {
    int fd = inotify_init();
    if (fd < 0) {
        perror("inotify_init");
        return 1;
    }

    int wd = inotify_add_watch(fd, ".", IN_MODIFY | IN_CREATE | IN_DELETE);
    if (wd < 0) {
        perror("inotify_add_watch");
        close(fd);
        return 1;
    }

    char buffer[4096];
    while (read(fd, buffer, sizeof(buffer)) > 0) {
        int i = 0;
        while (i < sizeof(buffer)) {
            struct inotify_event *event = (struct inotify_event *)&buffer[i];
            if (event->len) {
                printf("Event: %s, Name: %s\n", event->mask & IN_MODIFY ? "MODIFY" : "CREATE", event->name);
            }
            i += sizeof(struct inotify_event) + event->len;
        }
    }

    inotify_rm_watch(fd, wd);
    close(fd);
    return 0;
}

總結

要在Linux中使用readdir支持自定義屬性,最常用的方法是使用擴展屬性(Extended Attributes)。通過getfattrsetfattr命令,以及相應的系統調用,你可以在文件或目錄上存儲和讀取自定義數據。結合stat系統調用,你可以獲取更多的文件信息。如果需要實時監控文件變化,可以使用inotify接口。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女