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
|
/* $OpenBSD: file_media.c,v 1.22 2016/01/16 20:00:50 krw Exp $ */
/*
* file_media.c -
*
* Written by Eryk Vershen
*/
/*
* Copyright 1997,1998 by Apple Computer, Inc.
* All Rights Reserved
*
* Permission to use, copy, modify, and distribute this software and
* its documentation for any purpose and without fee is hereby granted,
* provided that the above copyright notice appears in all copies and
* that both the copyright notice and this permission notice appear in
* supporting documentation.
*
* APPLE COMPUTER DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE.
*
* IN NO EVENT SHALL APPLE COMPUTER BE LIABLE FOR ANY SPECIAL, INDIRECT, OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT,
* NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
* WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/param.h> /* DEV_BSIZE */
#include <sys/dkio.h>
#include <sys/disklabel.h>
#include <err.h>
// for printf()
#include <stdio.h>
// for malloc() & free()
#include <stdlib.h>
// for lseek(), read(), write(), close()
#include <unistd.h>
// for open()
#include <fcntl.h>
// for LONG_MAX
#include <limits.h>
// for errno
#include <errno.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <util.h>
#include "file_media.h"
/*
* Types
*/
typedef struct file_media *FILE_MEDIA;
struct file_media {
struct media m;
int fd;
int regular_file;
};
struct file_media_globals {
long exists;
long kind;
};
/*
* Global Constants
*/
/*
* Global Variables
*/
static long file_inited = 0;
static struct file_media_globals file_info;
/*
* Forward declarations
*/
int compute_block_size(int fd, char *name);
void file_init(void);
FILE_MEDIA new_file_media(void);
/*
* Routines
*/
void
file_init(void)
{
if (file_inited != 0) {
return;
}
file_inited = 1;
file_info.kind = allocate_media_kind();
}
FILE_MEDIA
new_file_media(void)
{
return (FILE_MEDIA) new_media(sizeof(struct file_media));
}
int
compute_block_size(int fd, char *name)
{
struct disklabel dl;
struct stat st;
if (fstat(fd, &st) == -1)
err(1, "can't fstat %s", name);
if (!S_ISCHR(st.st_mode) && !S_ISREG(st.st_mode))
errx(1, "%s is not a character device or a regular file", name);
if (ioctl(fd, DIOCGPDINFO, &dl) == -1)
err(1, "can't get disklabel for %s", name);
if (dl.d_secsize != DEV_BSIZE)
err(1, "%u-byte sector size not supported", dl.d_secsize);
return (dl.d_secsize);
}
MEDIA
open_file_as_media(char *file, int oflag)
{
FILE_MEDIA a;
int fd;
off_t off;
struct stat info;
if (file_inited == 0) {
file_init();
}
a = 0;
fd = opendev(file, oflag, OPENDEV_PART, NULL);
if (fd >= 0) {
a = new_file_media();
if (a != 0) {
a->m.kind = file_info.kind;
a->m.grain = compute_block_size(fd, file);
off = lseek(fd, 0, SEEK_END); /* seek to end of media */
//printf("file size = %Ld\n", off);
a->m.size_in_bytes = (long long) off;
a->fd = fd;
a->regular_file = 0;
if (fstat(fd, &info) < 0) {
warn("can't stat file '%s'", file);
} else {
a->regular_file = S_ISREG(info.st_mode);
}
} else {
close(fd);
}
}
return (MEDIA) a;
}
long
read_file_media(MEDIA m, long long offset, unsigned long count, void *address)
{
FILE_MEDIA a;
long rtn_value;
off_t off;
int t;
a = (FILE_MEDIA) m;
rtn_value = 0;
if (a == 0) {
/* no media */
fprintf(stderr,"no media\n");
} else if (a->m.kind != file_info.kind) {
/* wrong kind - XXX need to error here - this is an internal problem */
fprintf(stderr,"wrong kind\n");
} else if (count <= 0 || count % a->m.grain != 0) {
/* can't handle size */
fprintf(stderr,"bad size\n");
} else if (offset < 0 || offset % a->m.grain != 0) {
/* can't handle offset */
fprintf(stderr,"bad offset\n");
} else if (offset + count > a->m.size_in_bytes && a->m.size_in_bytes != (long long) 0) {
/* check for offset (and offset+count) too large */
fprintf(stderr,"offset+count too large\n");
} else if (count > LLONG_MAX - offset) {
/* check for offset (and offset+count) too large */
fprintf(stderr,"offset+count too large 2\n");
} else {
/* do the read */
off = offset;
if ((off = lseek(a->fd, off, SEEK_SET)) >= 0) {
if ((t = read(a->fd, address, count)) == count) {
rtn_value = 1;
} else {
fprintf(stderr,"read failed\n");
}
} else {
fprintf(stderr,"lseek failed\n");
}
}
return rtn_value;
}
long
write_file_media(MEDIA m, long long offset, unsigned long count, void *address)
{
FILE_MEDIA a;
long rtn_value;
off_t off;
int t;
a = (FILE_MEDIA) m;
rtn_value = 0;
if (a == 0) {
/* no media */
} else if (a->m.kind != file_info.kind) {
/* wrong kind - XXX need to error here - this is an internal problem */
} else if (count <= 0 || count % a->m.grain != 0) {
/* can't handle size */
} else if (offset < 0 || offset % a->m.grain != 0) {
/* can't handle offset */
} else if (count > LLONG_MAX - offset) {
/* check for offset (and offset+count) too large */
} else {
/* do the write */
off = offset;
if ((off = lseek(a->fd, off, SEEK_SET)) >= 0) {
if ((t = write(a->fd, address, count)) == count) {
if (off + count > a->m.size_in_bytes) {
a->m.size_in_bytes = off + count;
}
rtn_value = 1;
}
}
}
return rtn_value;
}
long
close_file_media(MEDIA m)
{
FILE_MEDIA a;
a = (FILE_MEDIA) m;
if (a == 0) {
return 0;
} else if (a->m.kind != file_info.kind) {
/* XXX need to error here - this is an internal problem */
return 0;
}
close(a->fd);
return 1;
}
long
os_reload_file_media(MEDIA m)
{
FILE_MEDIA a;
long rtn_value;
a = (FILE_MEDIA) m;
rtn_value = 0;
if (a == 0) {
/* no media */
} else if (a->m.kind != file_info.kind) {
/* wrong kind - XXX need to error here - this is an internal problem */
} else if (a->regular_file) {
/* okay - nothing to do */
rtn_value = 1;
} else {
rtn_value = 1;
}
return rtn_value;
}
|