1 /* Copyright 1995-2007,2009,2011 Alain Knaff.
2 * This file is part of mtools.
3 *
4 * Mtools is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * Mtools is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with Mtools. If not, see <http://www.gnu.org/licenses/>.
16 *
17 * Io to a plain file or device
18 *
19 * written by:
20 *
21 * Alain L. Knaff
22 * [email protected]
23 *
24 */
25
26 #include "sysincludes.h"
27 #include "stream.h"
28 #include "mtools.h"
29 #include "msdos.h"
30 #include "open_image.h"
31 #include "devices.h"
32 #include "plain_io.h"
33 #include "llong.h"
34
35 #ifdef HAVE_LINUX_FS_H
36 # include <linux/fs.h>
37 #endif
38
39 typedef struct SimpleFile_t {
40 struct Stream_t head;
41
42 struct MT_STAT statbuf;
43 int fd;
44 mt_off_t lastwhere;
45 int seekable;
46 int privileged;
47 #ifdef OS_hpux
48 int size_limited;
49 #endif
50 } SimpleFile_t;
51
52
53 #include "lockdev.h"
54
55 typedef ssize_t (*iofn) (int, void *, size_t);
56
file_io(SimpleFile_t * This,char * buf,mt_off_t where,size_t len,iofn io)57 static ssize_t file_io(SimpleFile_t *This, char *buf,
58 mt_off_t where, size_t len,
59 iofn io)
60 {
61 ssize_t ret;
62
63 if (This->seekable && where != This->lastwhere ){
64 if(mt_lseek( This->fd, where, SEEK_SET) < 0 ){
65 perror("seek");
66 return -1; /* If seek failed, lastwhere did
67 not change */
68 }
69 This->lastwhere = where;
70 }
71
72 #ifdef OS_hpux
73 /*
74 * On HP/UX, we can not write more than MAX_LEN bytes in one go.
75 * If more are written, the write fails with EINVAL
76 */
77 #define MAX_SCSI_LEN (127*1024)
78 if(This->size_limited && len > MAX_SCSI_LEN)
79 len = MAX_SCSI_LEN;
80 #endif
81 ret = io(This->fd, buf, len);
82
83 #ifdef OS_hpux
84 if (ret == -1 &&
85 errno == EINVAL && /* if we got EINVAL */
86 len > MAX_SCSI_LEN) {
87 This->size_limited = 1;
88 len = MAX_SCSI_LEN;
89 ret = io(This->fd, buf, len);
90 }
91 #endif
92
93 if ( ret == -1 ){
94 perror("plain_io");
95 return -1;
96 }
97 This->lastwhere = where + ret;
98 return ret;
99 }
100
file_read(Stream_t * Stream,char * buf,size_t len)101 static ssize_t file_read(Stream_t *Stream, char *buf, size_t len)
102 {
103 DeclareThis(SimpleFile_t);
104 return file_io(This, buf, This->lastwhere, len, read);
105 }
106
file_write(Stream_t * Stream,char * buf,size_t len)107 static ssize_t file_write(Stream_t *Stream, char *buf, size_t len)
108 {
109 DeclareThis(SimpleFile_t);
110 return file_io(This, buf, This->lastwhere, len, (iofn) write);
111 }
112
file_pread(Stream_t * Stream,char * buf,mt_off_t where,size_t len)113 static ssize_t file_pread(Stream_t *Stream, char *buf,
114 mt_off_t where, size_t len)
115 {
116 DeclareThis(SimpleFile_t);
117 return file_io(This, buf, where, len, read);
118 }
119
file_pwrite(Stream_t * Stream,char * buf,mt_off_t where,size_t len)120 static ssize_t file_pwrite(Stream_t *Stream, char *buf,
121 mt_off_t where, size_t len)
122 {
123 DeclareThis(SimpleFile_t);
124 return file_io(This, buf, where, len, (iofn) write);
125 }
126
file_flush(Stream_t * Stream UNUSEDP)127 static int file_flush(Stream_t *Stream UNUSEDP)
128 {
129 #if 0
130 DeclareThis(SimpleFile_t);
131
132 return fsync(This->fd);
133 #endif
134 return 0;
135 }
136
file_free(Stream_t * Stream)137 static int file_free(Stream_t *Stream)
138 {
139 DeclareThis(SimpleFile_t);
140
141 if (This->fd > 2)
142 return close(This->fd);
143 else
144 return 0;
145 }
146
init_geom_with_reg(int fd,struct device * dev,struct device * orig_dev,struct MT_STAT * statbuf)147 static int init_geom_with_reg(int fd, struct device *dev,
148 struct device *orig_dev,
149 struct MT_STAT *statbuf) {
150 if(S_ISREG(statbuf->st_mode)) {
151 /* Regular file (image file) */
152 mt_off_t sectors;
153 if(statbuf->st_size == 0) {
154 /* zero sized image => newly created.
155 Size not actually known...
156 */
157 return 0;
158 }
159 sectors = statbuf->st_size /
160 (mt_off_t)(dev->sector_size ? dev->sector_size : 512);
161 dev->tot_sectors =
162 ((smt_off_t) sectors > UINT32_MAX)
163 ? UINT32_MAX
164 : (uint32_t) sectors;
165 return 0;
166 } else {
167 /* All the rest (devices, etc.) */
168 return init_geom(fd, dev, orig_dev, statbuf);
169 }
170 }
171
file_geom(Stream_t * Stream,struct device * dev,struct device * orig_dev)172 static int file_geom(Stream_t *Stream, struct device *dev,
173 struct device *orig_dev)
174 {
175 int ret;
176 DeclareThis(SimpleFile_t);
177
178 if(dev->sector_size && dev->sector_size != 512) {
179 dev->sectors =
180 (uint16_t) (dev->sectors * dev->sector_size / 512);
181 }
182
183 #ifdef JPD
184 printf("file_geom:media=%0X=>cyl=%d,heads=%d,sects=%d,ssize=%d,use2m=%X\n",
185 media, dev->tracks, dev->heads, dev->sectors, dev->ssize,
186 dev->use_2m);
187 #endif
188 ret = init_geom_with_reg(This->fd,dev, orig_dev, &This->statbuf);
189 if(dev->sector_size && dev->sector_size != 512) {
190 dev->sectors =
191 (uint16_t) (dev->sectors * 512 / dev->sector_size);
192 }
193 #ifdef JPD
194 printf("f_geom: after init_geom(), sects=%d\n", dev->sectors);
195 #endif
196 return ret;
197 }
198
199
file_data(Stream_t * Stream,time_t * date,mt_off_t * size,int * type,uint32_t * address)200 static int file_data(Stream_t *Stream, time_t *date, mt_off_t *size,
201 int *type, uint32_t *address)
202 {
203 DeclareThis(SimpleFile_t);
204
205 if(date)
206 *date = This->statbuf.st_mtime;
207 if(size)
208 *size = This->statbuf.st_size;
209 if(type)
210 *type = S_ISDIR(This->statbuf.st_mode);
211 if(address)
212 *address = 0;
213 return 0;
214 }
215
file_discard(Stream_t * Stream UNUSEDP)216 static int file_discard(Stream_t *Stream UNUSEDP)
217 {
218 #ifdef BLKFLSBUF
219 int ret;
220 DeclareThis(SimpleFile_t);
221 ret= ioctl(This->fd, BLKFLSBUF);
222 if(ret < 0)
223 perror("BLKFLSBUF");
224 return ret;
225 #else
226 return 0;
227 #endif
228 }
229
230 static Class_t SimpleFileClass = {
231 file_read,
232 file_write,
233 file_pread,
234 file_pwrite,
235 file_flush,
236 file_free,
237 file_geom,
238 file_data,
239 0, /* pre_allocate */
240 0, /* dos-convert */
241 file_discard
242 };
243
244
LockDevice(int fd,struct device * dev,int locked,int lockMode,char * errmsg)245 int LockDevice(int fd, struct device *dev,
246 int locked, int lockMode,
247 char *errmsg)
248 {
249 #ifndef __EMX__
250 #ifndef __CYGWIN__
251 #ifndef OS_mingw32msvc
252 /* lock the device on writes */
253 if (locked && lock_dev(fd, (lockMode&O_ACCMODE) == O_RDWR, dev)) {
254 if(errmsg)
255 #ifdef HAVE_SNPRINTF
256 snprintf(errmsg,199,
257 "plain floppy: device \"%s\" busy (%s):",
258 dev ? dev->name : "unknown", strerror(errno));
259 #else
260 sprintf(errmsg,
261 "plain floppy: device \"%s\" busy (%s):",
262 (dev && strlen(dev->name) < 50) ?
263 dev->name : "unknown", strerror(errno));
264 #endif
265
266 if(errno != EOPNOTSUPP || (lockMode&O_ACCMODE) == O_RDWR) {
267 /* If error is "not supported", and we're only
268 * reading from the device anyways, then ignore. Some
269 * OS'es don't support locks on read-only devices, even
270 * if they are shared (read-only) locks */
271 return -1;
272 }
273 }
274 #endif
275 #endif
276 #endif
277 return 0;
278 }
279
SimpleFileOpen(struct device * dev,struct device * orig_dev,const char * name,int mode,char * errmsg,int mode2,int locked,mt_off_t * maxSize)280 Stream_t *SimpleFileOpen(struct device *dev, struct device *orig_dev,
281 const char *name, int mode, char *errmsg,
282 int mode2, int locked, mt_off_t *maxSize) {
283 return SimpleFileOpenWithLm(dev, orig_dev, name, mode,
284 errmsg, mode2, locked, mode, maxSize,
285 NULL);
286 }
287
SimpleFileOpenWithLm(struct device * dev,struct device * orig_dev,const char * name,int mode,char * errmsg,int mode2,int locked,int lockMode,mt_off_t * maxSize,int * geomFailure)288 Stream_t *SimpleFileOpenWithLm(struct device *dev, struct device *orig_dev,
289 const char *name, int mode, char *errmsg,
290 int mode2, int locked, int lockMode,
291 mt_off_t *maxSize, int *geomFailure)
292 {
293 SimpleFile_t *This;
294 #ifdef __EMX__
295 HFILE FileHandle;
296 ULONG Action;
297 APIRET rc;
298 #endif
299 if (IS_SCSI(dev))
300 return NULL;
301 This = New(SimpleFile_t);
302 if (!This){
303 printOom();
304 return 0;
305 }
306 memset((void*)This, 0, sizeof(SimpleFile_t));
307 This->seekable = 1;
308 #ifdef OS_hpux
309 This->size_limited = 0;
310 #endif
311 init_head(&This->head, &SimpleFileClass, NULL);
312 if (!name || strcmp(name,"-") == 0 ){
313 if (mode == O_RDONLY)
314 This->fd = 0;
315 else
316 This->fd = 1;
317 This->seekable = 0;
318 if (MT_FSTAT(This->fd, &This->statbuf) < 0) {
319 Free(This);
320 if(errmsg)
321 #ifdef HAVE_SNPRINTF
322 snprintf(errmsg,199,"Can't stat -: %s",
323 strerror(errno));
324 #else
325 sprintf(errmsg,"Can't stat -: %s",
326 strerror(errno));
327 #endif
328 return NULL;
329 }
330
331 return &This->head;
332 }
333
334
335 if(dev) {
336 if(!(mode2 & NO_PRIV))
337 This->privileged = IS_PRIVILEGED(dev);
338 mode |= dev->mode;
339 }
340
341 precmd(dev);
342 if(IS_PRIVILEGED(dev) && !(mode2 & NO_PRIV))
343 reclaim_privs();
344
345 #ifdef __EMX__
346 #define DOSOPEN_FLAGS (OPEN_FLAGS_DASD | OPEN_FLAGS_WRITE_THROUGH | \
347 OPEN_FLAGS_NOINHERIT | OPEN_FLAGS_RANDOM | \
348 OPEN_FLAGS_NO_CACHE)
349 #define DOSOPEN_FD_ACCESS (OPEN_SHARE_DENYREADWRITE | OPEN_ACCESS_READWRITE)
350 #define DOSOPEN_HD_ACCESS (OPEN_SHARE_DENYNONE | OPEN_ACCESS_READONLY)
351
352 if (isalpha(*name) && (*(name+1) == ':')) {
353 rc = DosOpen(
354 name, &FileHandle, &Action, 0L, FILE_NORMAL,
355 OPEN_ACTION_OPEN_IF_EXISTS, DOSOPEN_FLAGS |
356 (IS_NOLOCK(dev)?DOSOPEN_HD_ACCESS:DOSOPEN_FD_ACCESS),
357 0L);
358 #if DEBUG
359 if (rc != NO_ERROR) fprintf (stderr, "DosOpen() returned %d\n", rc);
360 #endif
361 if (!IS_NOLOCK(dev)) {
362 rc = DosDevIOCtl(
363 FileHandle, 0x08L, DSK_LOCKDRIVE, 0, 0, 0, 0, 0, 0);
364 #if DEBUG
365 if (rc != NO_ERROR) fprintf (stderr, "DosDevIOCtl() returned %d\n", rc);
366 #endif
367 }
368 if (rc == NO_ERROR)
369 This->fd = _imphandle(FileHandle); else This->fd = -1;
370 } else
371 #endif
372 {
373 This->fd = open(name, mode | O_LARGEFILE | O_BINARY,
374 IS_NOLOCK(dev)?0444:0666);
375 }
376
377 if(IS_PRIVILEGED(dev) && !(mode2 & NO_PRIV))
378 drop_privs();
379
380 if (This->fd < 0) {
381 if(errmsg) {
382 #ifdef HAVE_SNPRINTF
383 snprintf(errmsg, 199, "Can't open %s: %s",
384 name, strerror(errno));
385 #else
386 sprintf(errmsg, "Can't open %s: %s",
387 name, strerror(errno));
388 #endif
389 }
390 goto exit_1;
391 }
392
393 if(IS_PRIVILEGED(dev) && !(mode2 & NO_PRIV))
394 closeExec(This->fd);
395
396 #ifdef __EMX__
397 if (*(name+1) != ':')
398 #endif
399 if (MT_FSTAT(This->fd, &This->statbuf) < 0
400 #ifdef OS_mingw32msvc
401 && strncmp(name, "\\\\.\\", 4) != 0
402 #endif
403 ) {
404 if(errmsg) {
405 #ifdef HAVE_SNPRINTF
406 snprintf(errmsg,199,"Can't stat %s: %s",
407 name, strerror(errno));
408 #else
409 if(strlen(name) > 50) {
410 sprintf(errmsg,"Can't stat file: %s",
411 strerror(errno));
412 } else {
413 sprintf(errmsg,"Can't stat %s: %s",
414 name, strerror(errno));
415 }
416 #endif
417 }
418 goto exit_0;
419 }
420
421 if(LockDevice(This->fd, dev, locked, lockMode, errmsg) < 0)
422 goto exit_0;
423
424 /* set default parameters, if needed */
425 if (dev){
426 errno=0;
427 if (((!IS_MFORMAT_ONLY(dev) && dev->tracks) ||
428 mode2 & ALWAYS_GET_GEOMETRY) &&
429 init_geom_with_reg(This->fd, dev, orig_dev,
430 &This->statbuf)){
431 if(geomFailure && (errno==EBADF || errno==EPERM)) {
432 *geomFailure=1;
433 return NULL;
434 } else if(errmsg)
435 sprintf(errmsg,"init: set default params");
436 goto exit_0;
437 }
438 }
439
440 if(maxSize)
441 *maxSize = max_off_t_seek;
442
443 This->lastwhere = 0;
444
445 return &This->head;
446 exit_0:
447 close(This->fd);
448 exit_1:
449 Free(This);
450 return NULL;
451 }
452
get_fd(Stream_t * Stream)453 int get_fd(Stream_t *Stream)
454 {
455 Class_t *clazz;
456 DeclareThis(SimpleFile_t);
457 clazz = This->head.Class;
458 if(clazz != &SimpleFileClass)
459 return -1;
460 else
461 return This->fd;
462 }
463