bbcobj.c
5.32 KB
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#include "bbclocal.h"
int
__BBC_ObjectSize(BBCHandle h, const char *name)
{
bbc_hand *hp;
int rv;
OSBbStatBuf sb;
BBC_LOG(MSG_DIAG, "__BBC_ObjectSize %s\n", name);
if ((rv = __BBC_CheckHandle(h)) != BBC_OK)
return rv;
hp = handles[h];
if ((rv = __BBC_FStat(hp, name, &sb, NULL, 0)) < 0)
return rv;
return sb.size;
}
int
__BBC_GetObject(BBCHandle h, const char *name, void *buf, int len)
{
bbc_hand *hp;
int rv;
int fd;
OSBbStatBuf sb;
BBC_LOG(MSG_DIAG, "__BBC_GetObject %s\n", name);
if ((rv = __BBC_CheckHandle(h)) != BBC_OK)
return rv;
hp = handles[h];
if ((fd = __BBC_FStat(hp, name, &sb, NULL, 0)) < 0)
return fd;
rv = __BBC_FRead(hp, fd, 0, buf, len);
return rv;
}
#if BBC_DEBUG
#ifndef WIN32
#define BBC_TIME 1
#endif
#endif
int
__BBC_StoreObject(BBCHandle h, const char *name, const char *tmpname, const void *buf, int len)
{
bbc_hand *hp;
int rv;
BBC_LOG(MSG_DIAG, "__BBC_StoreObject %s\n", name);
if ((rv = __BBC_CheckHandle(h)) != BBC_OK)
return rv;
hp = handles[h];
rv = __bbc_write_file(hp, name, tmpname, buf, len);
return rv;
}
int __BBC_RemoveObject(BBCHandle h, const char *name)
{
bbc_hand *hp;
int rv;
BBC_LOG(MSG_DIAG, "__BBC_RemoveObject %s\n", name);
if ((rv = __BBC_CheckHandle(h)) != BBC_OK) {
return rv;
}
hp = handles[h];
rv = __BBC_FDelete(hp, name);
return rv;
}
int __BBC_RenameObject(BBCHandle h, const char *old, const char *new)
{
bbc_hand *hp;
int rv;
BBC_LOG(MSG_DIAG, "__BBC_RenameObject %s to %s\n", old, new);
if ((rv = __BBC_CheckHandle(h)) != BBC_OK) {
return rv;
}
hp = handles[h];
rv = __BBC_FRename(hp, old, new);
return rv;
}
int __BBC_ListObjects(BBCHandle h)
{
bbc_hand *hp;
OSBbDirEnt *dirbuf, *dir;
int rv, count;
BBC_LOG(MSG_DIAG, "__BBC_ListObjects\n");
if ((rv = __BBC_CheckHandle(h)) != BBC_OK) {
BBC_LOG_BBCERROR("__BBC_ListObjects: __BBC_CheckHandle failed", rv);
return rv;
}
hp = handles[h];
if ((dirbuf = malloc(MAX_DIR * sizeof(OSBbDirEnt))) == NULL) {
BBC_LOG_SYSERROR("__BBC_ListObjects: malloc failed");
return BBC_NOMEM;
}
count = __BBC_FReadDir(hp, dirbuf, MAX_DIR);
if ((rv = count) < 0) {
BBC_LOG_BBCERROR("__BBC_ListObjects: __BBC_FReadDir failed", rv);
goto out;
}
BBC_LOG(MSG_INFO, "BBCard listing:\n");
for (dir = dirbuf; count > 0; count--, dir++) {
BBC_LOG(MSG_INFO, "%s %u %d\n", dir->name, dir->size, dir->type);
}
out:
return rv;
}
/*
* Writes a file by:
* 1. Removing the temp file
* 2. Creating and writing the content to the temp file
* 3. Verifying the data was written correctly to the temp file
* 4. Renaming the temp file the the desired file name
* 5. Removing the temp file
*/
int __bbc_write_file(bbc_hand *hp, const char *name, const char *tmpname,
const void *buf, int len)
{
int rv;
int fd;
const char *newfile;
#if BBC_TIME
struct timeval tv1, tv2;
float t;
#endif
/*
* Check whether to use a tempname
*
* The content object case used to pass NULL here, but doesn't
* anymore.
*/
newfile = (tmpname != NULL) ? tmpname : name;
/*
* If file already exists, delete it
*/
rv = __BBC_FDelete(hp, newfile);
if (rv != BBC_OK && rv != BBC_NOFILE) {
// Abort if file not removed successfully
BBC_LOG(MSG_ERR, "__bbc_write_file delete %s fails %d\n", newfile, rv);
return rv;
}
#if BBC_TIME
gettimeofday(&tv1, NULL);
#endif
/*
* Create the file and write the data
*/
if ((fd = __BBC_FCreate(hp, newfile, 1, len)) < 0) {
BBC_LOG(MSG_ERR, "__bbc_write_file create %s fails %d\n", newfile, fd);
return fd;
}
if ((rv = __BBC_FWrite(hp, fd, 0, buf, len)) < len) {
BBC_LOG(MSG_ERR, "__bbc_write_file write %s fails %d\n", newfile, rv);
if (rv >= 0) /* Didn't write as much as expected */
rv = BBC_ERROR;
goto err;
}
#if BBC_TIME
gettimeofday(&tv2, NULL);
t = (float)(tv2.tv_sec-tv1.tv_sec) + (tv2.tv_usec-tv1.tv_usec)/1000000.f;
BBC_LOG(MSG_DEBUG, "Write %.1f KB %.1f sec %.1f KB/s\n", len/1024.f, t, len/1000.f/t);
#endif
if ((rv = __BBC_VerifyFile(hp, newfile, buf, len)) != BBC_OK) {
BBC_LOG(MSG_ERR, "__bbc_write_file verify %s fails %d\n", newfile, rv);
goto err;
}
#if BBC_TIME
gettimeofday(&tv1, NULL);
t = (float)(tv1.tv_sec-tv2.tv_sec) + (tv1.tv_usec-tv2.tv_usec)/1000000.f;
BBC_LOG(MSG_DEBUG, "Verify %.1f KB %.1f sec %.1f KB/s\n", len/1024.f, t, len/1000.f/t);
#endif
/*
* Everything is successful up to this point, do atomic rename of the temp file to
* its proper name. Rename will delete the target if it exists.
*/
if (tmpname != NULL) {
if ((rv = __BBC_FRename(hp, newfile, name)) != BBC_OK) {
BBC_LOG(MSG_ERR, "__bbc_write_file rename %s to %s fails %d\n", newfile, name, rv);
goto err;
}
}
return rv;
err:
if (tmpname != NULL) {
if (!BBC_IO_ERR(rv) && !BBC_CARD_ERR(rv)) {
// Only try to delete tempfile if we didn't get an IO or card error
int res = __BBC_FDelete(hp, tmpname);
if (res != BBC_OK)
BBC_LOG(MSG_ERR, "__bbc_write_file delete %s fails %d\n", TEMPFILE, rv);
}
}
return rv;
}