glusterfs

Форк
0
181 строка · 3.4 Кб
1

2
#define _GNU_SOURCE
3

4
#include <stdio.h>
5
#include <stdlib.h>
6
#include <unistd.h>
7
#include <sys/types.h>
8
#include <sys/stat.h>
9
#include <fcntl.h>
10
#include <string.h>
11
#include <errno.h>
12

13
static char buffer[65536];
14

15
static int
16
parse_int(const char *text, size_t *value)
17
{
18
    char *ptr;
19
    size_t val;
20

21
    val = strtoul(text, &ptr, 0);
22
    if (*ptr != 0) {
23
        return 0;
24
    }
25

26
    *value = val;
27

28
    return 1;
29
}
30

31
static int
32
fill_area(int fd, off_t offset, size_t size)
33
{
34
    size_t len;
35
    ssize_t res;
36

37
    while (size > 0) {
38
        len = sizeof(buffer);
39
        if (len > size) {
40
            len = size;
41
        }
42
        res = pwrite(fd, buffer, len, offset);
43
        if (res < 0) {
44
            fprintf(stderr, "pwrite(%d, %p, %lu, %lu) failed: %d\n", fd, buffer,
45
                    size, offset, errno);
46
            return 0;
47
        }
48
        if (res != len) {
49
            fprintf(stderr,
50
                    "pwrite(%d, %p, %lu, %lu) didn't wrote all "
51
                    "data: %lu/%lu\n",
52
                    fd, buffer, size, offset, res, len);
53
            return 0;
54
        }
55
        offset += len;
56
        size -= len;
57
    }
58

59
    return 1;
60
}
61

62
static void
63
syntax(void)
64
{
65
    fprintf(stderr, "Syntax: seek create <path> <offset> <size> [...]\n");
66
    fprintf(stderr, "        seek scan <path> data|hole <offset>\n");
67
}
68

69
static int
70
seek_create(const char *path, int argc, char *argv[])
71
{
72
    size_t off, size;
73
    int fd;
74
    int ret = 1;
75

76
    fd = open(path, O_CREAT | O_TRUNC | O_RDWR, 0644);
77
    if (fd < 0) {
78
        fprintf(stderr, "Failed to create the file\n");
79
        goto out;
80
    }
81

82
    while (argc > 0) {
83
        if (!parse_int(argv[0], &off) || !parse_int(argv[1], &size)) {
84
            syntax();
85
            goto out_close;
86
        }
87
        if (!fill_area(fd, off, size)) {
88
            goto out_close;
89
        }
90
        argv += 2;
91
        argc -= 2;
92
    }
93

94
    ret = 0;
95

96
out_close:
97
    close(fd);
98
out:
99
    return ret;
100
}
101

102
static int
103
seek_scan(const char *path, const char *type, const char *pos)
104
{
105
    size_t off, res;
106
    int fd, whence;
107
    int ret = 1;
108

109
    if (strcmp(type, "data") == 0) {
110
        whence = SEEK_DATA;
111
    } else if (strcmp(type, "hole") == 0) {
112
        whence = SEEK_HOLE;
113
    } else {
114
        syntax();
115
        goto out;
116
    }
117

118
    if (!parse_int(pos, &off)) {
119
        syntax();
120
        goto out;
121
    }
122

123
    fd = open(path, O_RDWR);
124
    if (fd < 0) {
125
        fprintf(stderr, "Failed to open the file\n");
126
        goto out;
127
    }
128

129
    res = lseek(fd, off, whence);
130
    if (res == (off_t)-1) {
131
        if (errno != ENXIO) {
132
            fprintf(stderr, "seek(%d, %lu, %d) failed: %d\n", fd, off, whence,
133
                    errno);
134
            goto out_close;
135
        }
136
        fprintf(stdout, "ENXIO\n");
137
    } else {
138
        fprintf(stdout, "%lu\n", res);
139
    }
140

141
    ret = 0;
142

143
out_close:
144
    close(fd);
145
out:
146
    return ret;
147
}
148

149
int
150
main(int argc, char *argv[])
151
{
152
    int ret = 1;
153

154
    memset(buffer, 0x55, sizeof(buffer));
155

156
    if (argc < 3) {
157
        syntax();
158
        goto out;
159
    }
160

161
    if (strcmp(argv[1], "create") == 0) {
162
        if (((argc - 3) & 1) != 0) {
163
            syntax();
164
            goto out;
165
        }
166
        ret = seek_create(argv[2], argc - 3, argv + 3);
167
    } else if (strcmp(argv[1], "scan") == 0) {
168
        if (argc != 5) {
169
            syntax();
170
            goto out;
171
        }
172
        ret = seek_scan(argv[2], argv[3], argv[4]);
173
    } else {
174
        syntax();
175
        goto out;
176
    }
177

178
    ret = 0;
179

180
out:
181
    return ret;
182
}
183

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

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

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

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