glusterfs

Форк
0
/
get-mdata-xattr.c 
156 строк · 3.7 Кб
1
/*
2
   Copyright (c) 2019 Red Hat, Inc. <http://www.redhat.com>
3
   This file is part of GlusterFS.
4

5
   This file is licensed to you under your choice of the GNU Lesser
6
   General Public License, version 3 or any later version (LGPLv3 or
7
   later), or the GNU General Public License, version 2 (GPLv2), in all
8
   cases as published by the Free Software Foundation.
9
*/
10

11
#include <stdlib.h>
12
#ifdef __FreeBSD__
13
#include <sys/endian.h>
14
#else
15
#include <endian.h>
16
#endif
17
#include <stdio.h>
18
#include <time.h>
19
#include <string.h>
20
#include <inttypes.h>
21
#include <sys/types.h>
22
#include <sys/xattr.h>
23
#include <errno.h>
24

25
typedef struct gf_timespec_disk {
26
    uint64_t tv_sec;
27
    uint64_t tv_nsec;
28
} gf_timespec_disk_t;
29

30
/* posix_mdata_t on disk structure */
31
typedef struct __attribute__((__packed__)) posix_mdata_disk {
32
    /* version of structure, bumped up if any new member is added */
33
    uint8_t version;
34
    /* flags indicates valid fields in the structure */
35
    uint64_t flags;
36
    gf_timespec_disk_t ctime;
37
    gf_timespec_disk_t mtime;
38
    gf_timespec_disk_t atime;
39
} posix_mdata_disk_t;
40

41
/* In memory representation posix metadata xattr */
42
typedef struct {
43
    /* version of structure, bumped up if any new member is added */
44
    uint8_t version;
45
    /* flags indicates valid fields in the structure */
46
    uint64_t flags;
47
    struct timespec ctime;
48
    struct timespec mtime;
49
    struct timespec atime;
50
} posix_mdata_t;
51

52
#define GF_XATTR_MDATA_KEY "trusted.glusterfs.mdata"
53

54
/* posix_mdata_from_disk converts posix_mdata_disk_t into host byte order
55
 */
56
static inline void
57
posix_mdata_from_disk(posix_mdata_t *out, posix_mdata_disk_t *in)
58
{
59
    out->version = in->version;
60
    out->flags = be64toh(in->flags);
61

62
    out->ctime.tv_sec = be64toh(in->ctime.tv_sec);
63
    out->ctime.tv_nsec = be64toh(in->ctime.tv_nsec);
64

65
    out->mtime.tv_sec = be64toh(in->mtime.tv_sec);
66
    out->mtime.tv_nsec = be64toh(in->mtime.tv_nsec);
67

68
    out->atime.tv_sec = be64toh(in->atime.tv_sec);
69
    out->atime.tv_nsec = be64toh(in->atime.tv_nsec);
70
}
71

72
/* posix_fetch_mdata_xattr fetches the posix_mdata_t from disk */
73
static int
74
posix_fetch_mdata_xattr(const char *real_path, posix_mdata_t *metadata)
75
{
76
    size_t size = -1;
77
    char *value = NULL;
78
    char gfid_str[64] = {0};
79

80
    char *key = GF_XATTR_MDATA_KEY;
81

82
    if (!metadata || !real_path) {
83
        goto err;
84
    }
85

86
    /* Get size */
87
    size = lgetxattr(real_path, key, NULL, 0);
88
    if (size == -1) {
89
        goto err;
90
    }
91

92
    value = calloc(size + 1, sizeof(char));
93
    if (!value) {
94
        goto err;
95
    }
96

97
    /* Get xattr value */
98
    size = lgetxattr(real_path, key, value, size);
99
    if (size == -1) {
100
        goto err;
101
    }
102
    posix_mdata_from_disk(metadata, (posix_mdata_disk_t *)value);
103

104
out:
105
    if (value)
106
        free(value);
107
    return 0;
108
err:
109
    if (value)
110
        free(value);
111
    return -1;
112
}
113

114
int
115
main(int argc, char *argv[])
116
{
117
    posix_mdata_t metadata;
118
    uint64_t result;
119

120
    if (argc != 3) {
121
        /*
122
        Usage: get_mdata_xattr -c|-m|-a <file-name>
123
                       where -c --> ctime
124
                             -m --> mtime
125
                             -a --> atime
126
        */
127
        printf("-1");
128
        goto err;
129
    }
130

131
    if (posix_fetch_mdata_xattr(argv[2], &metadata)) {
132
        printf("-1");
133
        goto err;
134
    }
135

136
    switch (argv[1][1]) {
137
        case 'c':
138
            result = metadata.ctime.tv_sec;
139
            break;
140
        case 'm':
141
            result = metadata.mtime.tv_sec;
142
            break;
143
        case 'a':
144
            result = metadata.atime.tv_sec;
145
            break;
146
        default:
147
            printf("-1");
148
            goto err;
149
    }
150
    printf("%" PRIu64, result);
151
    fflush(stdout);
152
    return 0;
153
err:
154
    fflush(stdout);
155
    return -1;
156
}
157

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.