forked from wesedens/fs-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sysdep.c
318 lines (243 loc) · 5.68 KB
/
sysdep.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*
This file contains some routines that are #ifdef'ed based on what
system you're on. Currently it supports the BeOS and Unix. It
could be extended to support Windows NT but their posix support
is such a joke that it would probably be a real pain in the arse.
THIS CODE COPYRIGHT DOMINIC GIAMPAOLO. NO WARRANTY IS EXPRESSED
OR IMPLIED. YOU MAY USE THIS CODE AND FREELY DISTRIBUTE IT FOR
NON-COMMERCIAL USE AS LONG AS THIS NOTICE REMAINS ATTACHED.
FOR COMMERCIAL USE, CONTACT DOMINIC GIAMPAOLO (dbg@be.com).
Dominic Giampaolo
dbg@be.com
*/
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include "compat.h"
int
device_is_read_only(const char *device)
{
#if defined(unix) || defined(__APPLE__)
return 0; /* XXXdbg should do an ioctl or something */
#else
int fd;
device_geometry dg;
fd = open(device, O_RDONLY);
if (ioctl(fd, B_GET_GEOMETRY, &dg) < 0)
return 0;
close(fd);
return dg.read_only;
#endif
}
int
get_device_block_size(int fd)
{
#if defined(unix) || defined(__APPLE__)
return 512; /* XXXdbg should do an ioctl or something */
#else
struct stat st;
device_geometry dg;
if (ioctl(fd, B_GET_GEOMETRY, &dg) < 0) {
if (fstat(fd, &st) < 0 || S_ISDIR(st.st_mode))
return 0;
return 512; /* just assume it's a plain old file or something */
}
return dg.bytes_per_sector;
#endif
}
fs_off_t
get_num_device_blocks(int fd)
{
#if defined(unix) || defined(__APPLE__)
struct stat st;
fstat(fd, &st); /* XXXdbg should be an ioctl or something */
return st.st_size / get_device_block_size(fd);
#else
struct stat st;
device_geometry dg;
if (ioctl(fd, B_GET_GEOMETRY, &dg) >= 0) {
return (fs_off_t)dg.cylinder_count *
(fs_off_t)dg.sectors_per_track *
(fs_off_t)dg.head_count;
}
/* if the ioctl fails, try just stat'ing in case it's a regular file */
if (fstat(fd, &st) < 0)
return 0;
return st.st_size / get_device_block_size(fd);
#endif
}
int
device_is_removeable(int fd)
{
#if defined(unix) || defined(__APPLE__)
return 0; /* XXXdbg should do an ioctl or something */
#else
struct stat st;
device_geometry dg;
if (ioctl(fd, B_GET_GEOMETRY, &dg) < 0) {
return 0;
}
return dg.removable;
#endif
}
int
lock_removeable_device(int fd, bool on_or_off)
{
#if defined(unix) || defined(__APPLE__)
return 0; /* XXXdbg should do an ioctl or something */
#else
return ioctl(fd, B_SCSI_PREVENT_ALLOW, &on_or_off);
#endif
}
ssize_t
read_pos(int fd, fs_off_t _pos, void *data, size_t nbytes)
{
off_t pos = (off_t)_pos;
size_t ret;
if (lseek(fd, pos, SEEK_SET) < 0) {
perror("read lseek");
return EINVAL;
}
ret = read(fd, data, nbytes);
if (ret != nbytes) {
printf("read_pos: wanted %d, got %d\n", nbytes, ret);
return -1;
}
return ret;
}
ssize_t
write_pos(int fd, fs_off_t _pos, const void *data, size_t nbytes)
{
off_t pos = (off_t)_pos;
size_t ret;
if (lseek(fd, pos, SEEK_SET) < 0) {
perror("read lseek");
return EINVAL;
}
ret = write(fd, data, nbytes);
if (ret != nbytes) {
printf("write_pos: wanted %d, got %d\n", nbytes, ret);
return -1;
}
return ret;
}
#define MAX_IOV 8192 /* something way bigger than we'll ever use */
ssize_t
readv_pos(int fd, fs_off_t _pos, struct iovec *iov, int count)
{
off_t pos = (off_t)_pos;
size_t amt = 0;
ssize_t ret;
struct iovec *tmpiov;
int i, n;
if (lseek(fd, pos, SEEK_SET) < 0) {
perror("read lseek");
return EINVAL;
}
i = 0;
tmpiov = iov;
while (i < count) {
if (i + MAX_IOV < count)
n = MAX_IOV;
else
n = (count - i);
ret = readv(fd, tmpiov, n);
amt += ret;
if (ret < 0)
break;
i += n;
tmpiov += n;
}
return amt;
}
ssize_t
writev_pos(int fd, fs_off_t _pos, struct iovec *iov, int count)
{
off_t pos = (off_t)_pos;
size_t amt = 0;
ssize_t ret;
struct iovec *tmpiov;
int i, n;
if (lseek(fd, pos, SEEK_SET) < 0) {
perror("read lseek");
return EINVAL;
}
i = 0;
tmpiov = iov;
while (i < count) {
if (i + MAX_IOV < count)
n = MAX_IOV;
else
n = (count - i);
ret = writev(fd, tmpiov, n);
amt += ret;
if (ret < 0)
break;
i += n;
tmpiov += n;
}
return amt;
}
#include <stdarg.h>
void
panic(const char *format, ...)
{
va_list ap;
va_start(ap, format);
vfprintf(stderr, format, ap);
va_end(ap);
while (TRUE)
;
}
#include "lock.h"
int
new_lock(lock *l, const char *name)
{
l->c = 1;
l->s = create_sem(0, (char *)name);
if (l->s <= 0)
return l->s;
return 0;
}
int
free_lock(lock *l)
{
delete_sem(l->s);
return 0;
}
int
new_mlock(mlock *l, long c, const char *name)
{
l->s = create_sem(c, (char *)name);
if (l->s <= 0)
return l->s;
return 0;
}
int
free_mlock(mlock *l)
{
delete_sem(l->s);
return 0;
}
#if defined(unix) || defined(__APPLE__)
#include <sys/time.h>
bigtime_t
system_time(void)
{
bigtime_t t;
struct timeval tv;
gettimeofday(&tv, NULL);
t = ((bigtime_t)tv.tv_sec * 1000000) + (bigtime_t)tv.tv_usec;
return t;
}
/*
If you're compiler/system can't deal with the version of system_time()
as defined above, use this one instead
bigtime_t
system_time(void)
{
return (bigtime_t)time(NULL);
}
*/
#endif /* unix */