-
Notifications
You must be signed in to change notification settings - Fork 3
/
diskstorage.c
202 lines (173 loc) · 4.22 KB
/
diskstorage.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
* Copyright (c) 2016 Sugizaki Yukimasa
* All rights reserved.
*
* This software is licensed under a Modified (3-Clause) BSD License.
* You should have received a copy of this license along with this
* software. If not, contact the copyright holder above.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
#include "diskstorage.h"
#include "error_fl.h"
/*
* +-------------------+---------+-------------------+---------+-----+
* | length of data #1 | data #1 | length of data #2 | data #2 | ... |
* +-------------------+---------+-------------------+---------+-----+
*/
void diskstorage_init(diskstorage_t *dstp)
{
int fd;
int umask_set, umask_orig;
char filename[] = "temp-ds-XXXXXXXXXXXXXXXXXXXXXXXX";
int ret;
umask_set = S_IXUSR | S_IRWXG | S_IRWXO;
umask_orig = umask(umask_set);
fd = mkstemp(filename);
umask(umask_orig);
if (fd == -1) {
error_fl(__FILE__, __LINE__, "mkstemp: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
ret = ftruncate(fd, 0);
if (ret == -1) {
error_fl(__FILE__, __LINE__, "ftruncate: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
ret = unlink(filename);
if (ret == -1) {
error_fl(__FILE__, __LINE__, "unlink: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
dstp->fd = fd;
dstp->len_max = 0;
}
void diskstorage_finalize(diskstorage_t *dstp)
{
int fd = dstp->fd;
int ret;
ret = close(fd);
if (ret == -1) {
error_fl(__FILE__, __LINE__, "close: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
dstp->fd = -1;
dstp->len_max = 0;
}
void diskstorage_append(const void *data, int len, diskstorage_t *dstp)
{
int fd = dstp->fd;
int len_max = dstp->len_max;
off_t reto;
ssize_t retss;
if (len == 0) {
error_fl(__FILE__, __LINE__, "len == 0\n");
exit(EXIT_FAILURE);
}
reto = lseek(fd, 0, SEEK_END);
if (reto == (off_t) -1) {
error_fl(__FILE__, __LINE__, "lseek: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
retss = write(fd, &len, sizeof(len));
if (retss == -1) {
error_fl(__FILE__, __LINE__, "write: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
if (retss != sizeof(len)) {
error_fl(__FILE__, __LINE__, "short write\n");
exit(EXIT_FAILURE);
}
retss = write(fd, data, len);
if (retss == -1) {
error_fl(__FILE__, __LINE__, "write: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
if (retss != len) {
error_fl(__FILE__, __LINE__, "short write\n");
exit(EXIT_FAILURE);
}
if (len > len_max) {
len_max = len;
dstp->len_max = len_max;
}
dstp->n ++;
}
void diskstorage_seek(const int n, diskstorage_t *dstp)
{
int i;
int fd = dstp->fd;
int len;
int len_max = dstp->len_max;
ssize_t retss;
off_t reto;
reto = lseek(fd, 0, SEEK_SET);
if (reto == (off_t) -1) {
error_fl(__FILE__, __LINE__, "lseek: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
for (i = 0; i < n; i ++) {
retss = read(fd, &len, sizeof(len));
if (retss == -1) {
error_fl(__FILE__, __LINE__, "read: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
if (retss != sizeof(len)) {
error_fl(__FILE__, __LINE__, "short read\n");
exit(EXIT_FAILURE);
}
if (len > len_max) {
error_fl(__FILE__, __LINE__, "len exceeds len_max\n");
exit(EXIT_FAILURE);
}
reto = lseek(fd, len, SEEK_CUR);
if (reto == -1) {
error_fl(__FILE__, __LINE__, "lseek: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
}
}
int diskstorage_read_next(void *data, int maxlen, diskstorage_t *dstp)
{
int fd = dstp->fd;
int len;
ssize_t retss;
retss = read(fd, &len, sizeof(len));
if (retss == -1) {
error_fl(__FILE__, __LINE__, "read: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
if (retss != sizeof(len)) {
error_fl(__FILE__, __LINE__, "short read\n");
exit(EXIT_FAILURE);
}
if (len > maxlen) {
error_fl(__FILE__, __LINE__, "len exceeds maxlen\n");
exit(EXIT_FAILURE);
}
retss = read(fd, data, len);
if (retss == -1) {
error_fl(__FILE__, __LINE__, "read: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
if (retss != len) {
error_fl(__FILE__, __LINE__, "short read\n");
exit(EXIT_FAILURE);
}
return len;
}
int diskstorage_n(diskstorage_t *dstp)
{
return dstp->n;
}
int diskstorage_len_max(diskstorage_t *dstp)
{
return dstp->len_max;
}