[9] | 1 | /*
|
---|
| 2 | * Io to a plain file or device
|
---|
| 3 | *
|
---|
| 4 | * written by:
|
---|
| 5 | *
|
---|
| 6 | * Alain L. Knaff
|
---|
| 7 | * alain@linux.lu
|
---|
| 8 | *
|
---|
| 9 | */
|
---|
| 10 |
|
---|
| 11 | #include "sysincludes.h"
|
---|
| 12 | #include "stream.h"
|
---|
| 13 | #include "mtools.h"
|
---|
| 14 | #include "msdos.h"
|
---|
| 15 | #include "plain_io.h"
|
---|
| 16 | #include "scsi.h"
|
---|
| 17 | #include "partition.h"
|
---|
| 18 | #include "llong.h"
|
---|
| 19 |
|
---|
| 20 | typedef struct SimpleFile_t {
|
---|
| 21 | Class_t *Class;
|
---|
| 22 | int refs;
|
---|
| 23 | Stream_t *Next;
|
---|
| 24 | Stream_t *Buffer;
|
---|
| 25 | struct stat stat;
|
---|
| 26 | int fd;
|
---|
| 27 | mt_off_t offset;
|
---|
| 28 | mt_off_t lastwhere;
|
---|
| 29 | int seekable;
|
---|
| 30 | int privileged;
|
---|
| 31 | #ifdef OS_hpux
|
---|
| 32 | int size_limited;
|
---|
| 33 | #endif
|
---|
| 34 | int scsi_sector_size;
|
---|
| 35 | void *extra_data; /* extra system dependant information for scsi */
|
---|
| 36 | } SimpleFile_t;
|
---|
| 37 |
|
---|
| 38 |
|
---|
| 39 | /*
|
---|
| 40 | * Create an advisory lock on the device to prevent concurrent writes.
|
---|
| 41 | * Uses either lockf, flock, or fcntl locking methods. See the Makefile
|
---|
| 42 | * and the Configure files for how to specify the proper method.
|
---|
| 43 | */
|
---|
| 44 |
|
---|
| 45 | int lock_dev(int fd, int mode, struct device *dev)
|
---|
| 46 | {
|
---|
| 47 | #if (defined(HAVE_FLOCK) && defined (LOCK_EX) && defined(LOCK_NB))
|
---|
| 48 | /**/
|
---|
| 49 | #else /* FLOCK */
|
---|
| 50 |
|
---|
| 51 | #if (defined(HAVE_LOCKF) && defined(F_TLOCK))
|
---|
| 52 | /**/
|
---|
| 53 | #else /* LOCKF */
|
---|
| 54 |
|
---|
| 55 | #if (defined(F_SETLK) && defined(F_WRLCK))
|
---|
| 56 | struct flock flk;
|
---|
| 57 |
|
---|
| 58 | #endif /* FCNTL */
|
---|
| 59 | #endif /* LOCKF */
|
---|
| 60 | #endif /* FLOCK */
|
---|
| 61 |
|
---|
| 62 | if(IS_NOLOCK(dev))
|
---|
| 63 | return 0;
|
---|
| 64 |
|
---|
| 65 | #if (defined(HAVE_FLOCK) && defined (LOCK_EX) && defined(LOCK_NB))
|
---|
| 66 | if (flock(fd, (mode ? LOCK_EX : LOCK_SH)|LOCK_NB) < 0)
|
---|
| 67 | #else /* FLOCK */
|
---|
| 68 |
|
---|
| 69 | #if (defined(HAVE_LOCKF) && defined(F_TLOCK))
|
---|
| 70 | if (mode && lockf(fd, F_TLOCK, 0) < 0)
|
---|
| 71 | #else /* LOCKF */
|
---|
| 72 |
|
---|
| 73 | #if (defined(F_SETLK) && defined(F_WRLCK))
|
---|
| 74 | flk.l_type = mode ? F_WRLCK : F_RDLCK;
|
---|
| 75 | flk.l_whence = 0;
|
---|
| 76 | flk.l_start = 0L;
|
---|
| 77 | flk.l_len = 0L;
|
---|
| 78 |
|
---|
| 79 | if (fcntl(fd, F_SETLK, &flk) < 0)
|
---|
| 80 | #endif /* FCNTL */
|
---|
| 81 | #endif /* LOCKF */
|
---|
| 82 | #endif /* FLOCK */
|
---|
| 83 | {
|
---|
| 84 | if(errno == EINVAL
|
---|
| 85 | #ifdef EOPNOTSUPP
|
---|
| 86 | || errno == EOPNOTSUPP
|
---|
| 87 | #endif
|
---|
| 88 | )
|
---|
| 89 | return 0;
|
---|
| 90 | else
|
---|
| 91 | return 1;
|
---|
| 92 | }
|
---|
| 93 | return 0;
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | typedef int (*iofn) (int, char *, int);
|
---|
| 97 |
|
---|
| 98 |
|
---|
| 99 |
|
---|
| 100 | static int file_io(Stream_t *Stream, char *buf, mt_off_t where, int len,
|
---|
| 101 | iofn io)
|
---|
| 102 | {
|
---|
| 103 | DeclareThis(SimpleFile_t);
|
---|
| 104 | int ret;
|
---|
| 105 |
|
---|
| 106 | where += This->offset;
|
---|
| 107 |
|
---|
| 108 | if (This->seekable && where != This->lastwhere ){
|
---|
| 109 | if(mt_lseek( This->fd, where, SEEK_SET) < 0 ){
|
---|
| 110 | perror("seek");
|
---|
| 111 | This->lastwhere = (mt_off_t) -1;
|
---|
| 112 | return -1;
|
---|
| 113 | }
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | #ifdef OS_hpux
|
---|
| 117 | /*
|
---|
| 118 | * On HP/UX, we can not write more than MAX_LEN bytes in one go.
|
---|
| 119 | * If more are written, the write fails with EINVAL
|
---|
| 120 | */
|
---|
| 121 | #define MAX_SCSI_LEN (127*1024)
|
---|
| 122 | if(This->size_limited && len > MAX_SCSI_LEN)
|
---|
| 123 | len = MAX_SCSI_LEN;
|
---|
| 124 | #endif
|
---|
| 125 | ret = io(This->fd, buf, len);
|
---|
| 126 |
|
---|
| 127 | #ifdef OS_hpux
|
---|
| 128 | if (ret == -1 &&
|
---|
| 129 | errno == EINVAL && /* if we got EINVAL */
|
---|
| 130 | len > MAX_SCSI_LEN) {
|
---|
| 131 | This->size_limited = 1;
|
---|
| 132 | len = MAX_SCSI_LEN;
|
---|
| 133 | ret = io(This->fd, buf, len);
|
---|
| 134 | }
|
---|
| 135 | #endif
|
---|
| 136 |
|
---|
| 137 | if ( ret == -1 ){
|
---|
| 138 | perror("plain_io");
|
---|
| 139 | This->lastwhere = (mt_off_t) -1;
|
---|
| 140 | return -1;
|
---|
| 141 | }
|
---|
| 142 | This->lastwhere = where + ret;
|
---|
| 143 | return ret;
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 |
|
---|
| 147 |
|
---|
| 148 | static int file_read(Stream_t *Stream, char *buf, mt_off_t where, size_t len)
|
---|
| 149 | {
|
---|
| 150 | return file_io(Stream, buf, where, len, (iofn) read);
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | static int file_write(Stream_t *Stream, char *buf, mt_off_t where, size_t len)
|
---|
| 154 | {
|
---|
| 155 | return file_io(Stream, buf, where, len, (iofn) write);
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | static int file_flush(Stream_t *Stream)
|
---|
| 159 | {
|
---|
| 160 | #if 0
|
---|
| 161 | DeclareThis(SimpleFile_t);
|
---|
| 162 |
|
---|
| 163 | return fsync(This->fd);
|
---|
| 164 | #endif
|
---|
| 165 | return 0;
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | static int file_free(Stream_t *Stream)
|
---|
| 169 | {
|
---|
| 170 | DeclareThis(SimpleFile_t);
|
---|
| 171 |
|
---|
| 172 | if (This->fd > 2)
|
---|
| 173 | return close(This->fd);
|
---|
| 174 | else
|
---|
| 175 | return 0;
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | static int file_geom(Stream_t *Stream, struct device *dev,
|
---|
| 179 | struct device *orig_dev,
|
---|
| 180 | int media, struct bootsector *boot)
|
---|
| 181 | {
|
---|
| 182 | int ret;
|
---|
| 183 | DeclareThis(SimpleFile_t);
|
---|
| 184 | size_t tot_sectors;
|
---|
| 185 | int BootP, Infp0, InfpX, InfTm;
|
---|
| 186 | int sectors, j;
|
---|
| 187 | unsigned char sum;
|
---|
| 188 | int sect_per_track;
|
---|
| 189 | struct label_blk_t *labelBlock;
|
---|
| 190 |
|
---|
| 191 | dev->ssize = 2; /* allow for init_geom to change it */
|
---|
| 192 | dev->use_2m = 0x80; /* disable 2m mode to begin */
|
---|
| 193 |
|
---|
| 194 | if(media == 0xf0 || media >= 0x100){
|
---|
| 195 | dev->heads = WORD(nheads);
|
---|
| 196 | dev->sectors = WORD(nsect);
|
---|
| 197 | tot_sectors = DWORD(bigsect);
|
---|
| 198 | SET_INT(tot_sectors, WORD(psect));
|
---|
| 199 | sect_per_track = dev->heads * dev->sectors;
|
---|
| 200 | tot_sectors += sect_per_track - 1; /* round size up */
|
---|
| 201 | dev->tracks = tot_sectors / sect_per_track;
|
---|
| 202 |
|
---|
| 203 | BootP = WORD(ext.old.BootP);
|
---|
| 204 | Infp0 = WORD(ext.old.Infp0);
|
---|
| 205 | InfpX = WORD(ext.old.InfpX);
|
---|
| 206 | InfTm = WORD(ext.old.InfTm);
|
---|
| 207 |
|
---|
| 208 | if(WORD(fatlen)) {
|
---|
| 209 | labelBlock = &boot->ext.old.labelBlock;
|
---|
| 210 | } else {
|
---|
| 211 | labelBlock = &boot->ext.fat32.labelBlock;
|
---|
| 212 | }
|
---|
| 213 |
|
---|
| 214 | if (boot->descr >= 0xf0 &&
|
---|
| 215 | labelBlock->dos4 == 0x29 &&
|
---|
| 216 | strncmp( boot->banner,"2M", 2 ) == 0 &&
|
---|
| 217 | BootP < 512 && Infp0 < 512 && InfpX < 512 && InfTm < 512 &&
|
---|
| 218 | BootP >= InfTm + 2 && InfTm >= InfpX && InfpX >= Infp0 &&
|
---|
| 219 | Infp0 >= 76 ){
|
---|
| 220 | for (sum=0, j=63; j < BootP; j++)
|
---|
| 221 | sum += boot->jump[j];/* checksum */
|
---|
| 222 | dev->ssize = boot->jump[InfTm];
|
---|
| 223 | if (!sum && dev->ssize <= 7){
|
---|
| 224 | dev->use_2m = 0xff;
|
---|
| 225 | dev->ssize |= 0x80; /* is set */
|
---|
| 226 | }
|
---|
| 227 | }
|
---|
| 228 | } else if (media >= 0xf8){
|
---|
| 229 | media &= 3;
|
---|
| 230 | dev->heads = old_dos[media].heads;
|
---|
| 231 | dev->tracks = old_dos[media].tracks;
|
---|
| 232 | dev->sectors = old_dos[media].sectors;
|
---|
| 233 | dev->ssize = 0x80;
|
---|
| 234 | dev->use_2m = ~1;
|
---|
| 235 | } else {
|
---|
| 236 | fprintf(stderr,"Unknown media type\n");
|
---|
| 237 | exit(1);
|
---|
| 238 | }
|
---|
| 239 |
|
---|
| 240 | sectors = dev->sectors;
|
---|
| 241 | dev->sectors = dev->sectors * WORD(secsiz) / 512;
|
---|
| 242 |
|
---|
| 243 | #ifdef JPD
|
---|
| 244 | printf("file_geom:media=%0X=>cyl=%d,heads=%d,sects=%d,ssize=%d,use2m=%X\n",
|
---|
| 245 | media, dev->tracks, dev->heads, dev->sectors, dev->ssize,
|
---|
| 246 | dev->use_2m);
|
---|
| 247 | #endif
|
---|
| 248 | ret = init_geom(This->fd,dev, orig_dev, &This->stat);
|
---|
| 249 | dev->sectors = sectors;
|
---|
| 250 | #ifdef JPD
|
---|
| 251 | printf("f_geom: after init_geom(), sects=%d\n", dev->sectors);
|
---|
| 252 | #endif
|
---|
| 253 | return ret;
|
---|
| 254 | }
|
---|
| 255 |
|
---|
| 256 |
|
---|
| 257 | static int file_data(Stream_t *Stream, time_t *date, mt_size_t *size,
|
---|
| 258 | int *type, int *address)
|
---|
| 259 | {
|
---|
| 260 | DeclareThis(SimpleFile_t);
|
---|
| 261 |
|
---|
| 262 | if(date)
|
---|
| 263 | *date = This->stat.st_mtime;
|
---|
| 264 | if(size)
|
---|
| 265 | *size = This->stat.st_size;
|
---|
| 266 | if(type)
|
---|
| 267 | *type = S_ISDIR(This->stat.st_mode);
|
---|
| 268 | if(address)
|
---|
| 269 | *address = 0;
|
---|
| 270 | return 0;
|
---|
| 271 | }
|
---|
| 272 |
|
---|
| 273 | /* ZIP or other scsi device on Solaris or SunOS system.
|
---|
| 274 | Since Sun won't accept a non-Sun label on a scsi disk, we must
|
---|
| 275 | bypass Sun's disk interface and use low-level SCSI commands to read
|
---|
| 276 | or write the ZIP drive. We thus replace the file_read and file_write
|
---|
| 277 | routines with our own scsi_read and scsi_write routines, that use the
|
---|
| 278 | uscsi ioctl interface. By James Dugal, jpd@usl.edu, 11-96. Tested
|
---|
| 279 | under Solaris 2.5 and SunOS 4.3.1_u1 using GCC.
|
---|
| 280 |
|
---|
| 281 | Note: the mtools.conf entry for a ZIP drive would look like this:
|
---|
| 282 | (solaris) drive C: file="/dev/rdsk/c0t5d0s2" partition=4 FAT=16 nodelay exclusive scsi=&
|
---|
| 283 | (sunos) drive C: file="/dev/rsd5c" partition=4 FAT=16 nodelay exclusive scsi=1
|
---|
| 284 |
|
---|
| 285 | Note 2: Sol 2.5 wants mtools to be suid-root, to use the ioctl. SunOS is
|
---|
| 286 | happy if we just have access to the device, so making mtools sgid to a
|
---|
| 287 | group called, say, "ziprw" which has rw permission on /dev/rsd5c, is fine.
|
---|
| 288 | */
|
---|
| 289 |
|
---|
| 290 | #define MAXBLKSPERCMD 255
|
---|
| 291 |
|
---|
| 292 | static void scsi_init(SimpleFile_t *This)
|
---|
| 293 | {
|
---|
| 294 | int fd = This->fd;
|
---|
| 295 | unsigned char cdb[10],buf[8];
|
---|
| 296 |
|
---|
| 297 | memset(cdb, 0, sizeof cdb);
|
---|
| 298 | memset(buf,0, sizeof(buf));
|
---|
| 299 | cdb[0]=SCSI_READ_CAPACITY;
|
---|
| 300 | if (scsi_cmd(fd, (unsigned char *)cdb,
|
---|
| 301 | sizeof(cdb), SCSI_IO_READ, buf, sizeof(buf), This->extra_data)==0)
|
---|
| 302 | {
|
---|
| 303 | This->scsi_sector_size=
|
---|
| 304 | ((unsigned)buf[5]<<16)|((unsigned)buf[6]<<8)|(unsigned)buf[7];
|
---|
| 305 | if (This->scsi_sector_size != 512)
|
---|
| 306 | fprintf(stderr," (scsi_sector_size=%d)\n",This->scsi_sector_size);
|
---|
| 307 | }
|
---|
| 308 | }
|
---|
| 309 |
|
---|
| 310 | int scsi_io(Stream_t *Stream, char *buf, mt_off_t where, size_t len, int rwcmd)
|
---|
| 311 | {
|
---|
| 312 | unsigned int firstblock, nsect;
|
---|
| 313 | int clen,r,max;
|
---|
| 314 | off_t offset;
|
---|
| 315 | unsigned char cdb[10];
|
---|
| 316 | DeclareThis(SimpleFile_t);
|
---|
| 317 |
|
---|
| 318 | firstblock=truncBytes32((where + This->offset)/This->scsi_sector_size);
|
---|
| 319 | /* 512,1024,2048,... bytes/sector supported */
|
---|
| 320 | offset=truncBytes32(where + This->offset -
|
---|
| 321 | firstblock*This->scsi_sector_size);
|
---|
| 322 | nsect=(offset+len+This->scsi_sector_size-1)/ This->scsi_sector_size;
|
---|
| 323 | #if defined(OS_sun) && defined(OS_i386)
|
---|
| 324 | if (This->scsi_sector_size>512)
|
---|
| 325 | firstblock*=This->scsi_sector_size/512; /* work around a uscsi bug */
|
---|
| 326 | #endif /* sun && i386 */
|
---|
| 327 |
|
---|
| 328 | if (len>512) {
|
---|
| 329 | /* avoid buffer overruns. The transfer MUST be smaller or
|
---|
| 330 | * equal to the requested size! */
|
---|
| 331 | while (nsect*This->scsi_sector_size>len)
|
---|
| 332 | --nsect;
|
---|
| 333 | if(!nsect) {
|
---|
| 334 | fprintf(stderr,"Scsi buffer too small\n");
|
---|
| 335 | exit(1);
|
---|
| 336 | }
|
---|
| 337 | if(rwcmd == SCSI_IO_WRITE && offset) {
|
---|
| 338 | /* there seems to be no memmove before a write */
|
---|
| 339 | fprintf(stderr,"Unaligned write\n");
|
---|
| 340 | exit(1);
|
---|
| 341 | }
|
---|
| 342 | /* a better implementation should use bounce buffers.
|
---|
| 343 | * However, in normal operation no buffer overruns or
|
---|
| 344 | * unaligned writes should happen anyways, as the logical
|
---|
| 345 | * sector size is (hopefully!) equal to the physical one
|
---|
| 346 | */
|
---|
| 347 | }
|
---|
| 348 |
|
---|
| 349 |
|
---|
| 350 | max = scsi_max_length();
|
---|
| 351 |
|
---|
| 352 | if (nsect > max)
|
---|
| 353 | nsect=max;
|
---|
| 354 |
|
---|
| 355 | /* set up SCSI READ/WRITE command */
|
---|
| 356 | memset(cdb, 0, sizeof cdb);
|
---|
| 357 |
|
---|
| 358 | switch(rwcmd) {
|
---|
| 359 | case SCSI_IO_READ:
|
---|
| 360 | cdb[0] = SCSI_READ;
|
---|
| 361 | break;
|
---|
| 362 | case SCSI_IO_WRITE:
|
---|
| 363 | cdb[0] = SCSI_WRITE;
|
---|
| 364 | break;
|
---|
| 365 | }
|
---|
| 366 |
|
---|
| 367 | cdb[1] = 0;
|
---|
| 368 |
|
---|
| 369 | if (firstblock > 0x1fffff || nsect > 0xff) {
|
---|
| 370 | /* I suspect that the ZIP drive also understands Group 1
|
---|
| 371 | * commands. If that is indeed true, we may chose Group 1
|
---|
| 372 | * more agressively in the future */
|
---|
| 373 |
|
---|
| 374 | cdb[0] |= SCSI_GROUP1;
|
---|
| 375 | clen=10; /* SCSI Group 1 cmd */
|
---|
| 376 |
|
---|
| 377 | /* this is one of the rare case where explicit coding is
|
---|
| 378 | * more portable than macros... The meaning of scsi command
|
---|
| 379 | * bytes is standardised, whereas the preprocessor macros
|
---|
| 380 | * handling it might be not... */
|
---|
| 381 |
|
---|
| 382 | cdb[2] = (unsigned char) (firstblock >> 24) & 0xff;
|
---|
| 383 | cdb[3] = (unsigned char) (firstblock >> 16) & 0xff;
|
---|
| 384 | cdb[4] = (unsigned char) (firstblock >> 8) & 0xff;
|
---|
| 385 | cdb[5] = (unsigned char) firstblock & 0xff;
|
---|
| 386 | cdb[6] = 0;
|
---|
| 387 | cdb[7] = (unsigned char) (nsect >> 8) & 0xff;
|
---|
| 388 | cdb[8] = (unsigned char) nsect & 0xff;
|
---|
| 389 | cdb[9] = 0;
|
---|
| 390 | } else {
|
---|
| 391 | clen = 6; /* SCSI Group 0 cmd */
|
---|
| 392 | cdb[1] |= (unsigned char) ((firstblock >> 16) & 0x1f);
|
---|
| 393 | cdb[2] = (unsigned char) ((firstblock >> 8) & 0xff);
|
---|
| 394 | cdb[3] = (unsigned char) firstblock & 0xff;
|
---|
| 395 | cdb[4] = (unsigned char) nsect;
|
---|
| 396 | cdb[5] = 0;
|
---|
| 397 | }
|
---|
| 398 |
|
---|
| 399 | if(This->privileged)
|
---|
| 400 | reclaim_privs();
|
---|
| 401 |
|
---|
| 402 | r=scsi_cmd(This->fd, (unsigned char *)cdb, clen, rwcmd, buf,
|
---|
| 403 | nsect*This->scsi_sector_size, This->extra_data);
|
---|
| 404 |
|
---|
| 405 | if(This->privileged)
|
---|
| 406 | drop_privs();
|
---|
| 407 |
|
---|
| 408 | if(r) {
|
---|
| 409 | perror(rwcmd == SCSI_IO_READ ? "SCMD_READ" : "SCMD_WRITE");
|
---|
| 410 | return -1;
|
---|
| 411 | }
|
---|
| 412 | #ifdef JPD
|
---|
| 413 | printf("finished %u for %u\n", firstblock, nsect);
|
---|
| 414 | #endif
|
---|
| 415 |
|
---|
| 416 | #ifdef JPD
|
---|
| 417 | printf("zip: read or write OK\n");
|
---|
| 418 | #endif
|
---|
| 419 | if (offset>0) memmove(buf,buf+offset,nsect*This->scsi_sector_size-offset);
|
---|
| 420 | if (len==256) return 256;
|
---|
| 421 | else if (len==512) return 512;
|
---|
| 422 | else return nsect*This->scsi_sector_size-offset;
|
---|
| 423 | }
|
---|
| 424 |
|
---|
| 425 | int scsi_read(Stream_t *Stream, char *buf, mt_off_t where, size_t len)
|
---|
| 426 | {
|
---|
| 427 |
|
---|
| 428 | #ifdef JPD
|
---|
| 429 | printf("zip: to read %d bytes at %d\n", len, where);
|
---|
| 430 | #endif
|
---|
| 431 | return scsi_io(Stream, buf, where, len, SCSI_IO_READ);
|
---|
| 432 | }
|
---|
| 433 |
|
---|
| 434 | int scsi_write(Stream_t *Stream, char *buf, mt_off_t where, size_t len)
|
---|
| 435 | {
|
---|
| 436 | #ifdef JPD
|
---|
| 437 | Printf("zip: to write %d bytes at %d\n", len, where);
|
---|
| 438 | #endif
|
---|
| 439 | return scsi_io(Stream, buf, where, len, SCSI_IO_WRITE);
|
---|
| 440 | }
|
---|
| 441 |
|
---|
| 442 | static Class_t ScsiClass = {
|
---|
| 443 | scsi_read,
|
---|
| 444 | scsi_write,
|
---|
| 445 | file_flush,
|
---|
| 446 | file_free,
|
---|
| 447 | file_geom,
|
---|
| 448 | file_data,
|
---|
| 449 | 0 /* pre-allocate */
|
---|
| 450 | };
|
---|
| 451 |
|
---|
| 452 |
|
---|
| 453 | static Class_t SimpleFileClass = {
|
---|
| 454 | file_read,
|
---|
| 455 | file_write,
|
---|
| 456 | file_flush,
|
---|
| 457 | file_free,
|
---|
| 458 | file_geom,
|
---|
| 459 | file_data,
|
---|
| 460 | 0 /* pre_allocate */
|
---|
| 461 | };
|
---|
| 462 |
|
---|
| 463 |
|
---|
| 464 | Stream_t *SimpleFileOpen(struct device *dev, struct device *orig_dev,
|
---|
| 465 | const char *name, int mode, char *errmsg,
|
---|
| 466 | int mode2, int locked, mt_size_t *maxSize)
|
---|
| 467 | {
|
---|
| 468 | SimpleFile_t *This;
|
---|
| 469 | #ifdef __EMX__
|
---|
| 470 | HFILE FileHandle;
|
---|
| 471 | ULONG Action;
|
---|
| 472 | APIRET rc;
|
---|
| 473 | #endif
|
---|
| 474 | This = New(SimpleFile_t);
|
---|
| 475 | if (!This){
|
---|
| 476 | printOom();
|
---|
| 477 | return 0;
|
---|
| 478 | }
|
---|
| 479 | This->scsi_sector_size = 512;
|
---|
| 480 | This->seekable = 1;
|
---|
| 481 | #ifdef OS_hpux
|
---|
| 482 | This->size_limited = 0;
|
---|
| 483 | #endif
|
---|
| 484 | This->Class = &SimpleFileClass;
|
---|
| 485 | if (!name || strcmp(name,"-") == 0 ){
|
---|
| 486 | if (mode == O_RDONLY)
|
---|
| 487 | This->fd = 0;
|
---|
| 488 | else
|
---|
| 489 | This->fd = 1;
|
---|
| 490 | This->seekable = 0;
|
---|
| 491 | This->refs = 1;
|
---|
| 492 | This->Next = 0;
|
---|
| 493 | This->Buffer = 0;
|
---|
| 494 | if (fstat(This->fd, &This->stat) < 0) {
|
---|
| 495 | Free(This);
|
---|
| 496 | if(errmsg)
|
---|
| 497 | #ifdef HAVE_SNPRINTF
|
---|
| 498 | snprintf(errmsg,199,"Can't stat -: %s",
|
---|
| 499 | strerror(errno));
|
---|
| 500 | #else
|
---|
| 501 | sprintf(errmsg,"Can't stat -: %s",
|
---|
| 502 | strerror(errno));
|
---|
| 503 | #endif
|
---|
| 504 | return NULL;
|
---|
| 505 | }
|
---|
| 506 |
|
---|
| 507 | return (Stream_t *) This;
|
---|
| 508 | }
|
---|
| 509 |
|
---|
| 510 |
|
---|
| 511 | if(dev) {
|
---|
| 512 | if(!(mode2 & NO_PRIV))
|
---|
| 513 | This->privileged = IS_PRIVILEGED(dev);
|
---|
| 514 | mode |= dev->mode;
|
---|
| 515 | }
|
---|
| 516 |
|
---|
| 517 | precmd(dev);
|
---|
| 518 | if(IS_PRIVILEGED(dev) && !(mode2 & NO_PRIV))
|
---|
| 519 | reclaim_privs();
|
---|
| 520 |
|
---|
| 521 | #ifdef __EMX__
|
---|
| 522 | #define DOSOPEN_FLAGS (OPEN_FLAGS_DASD | OPEN_FLAGS_WRITE_THROUGH | \
|
---|
| 523 | OPEN_FLAGS_NOINHERIT | OPEN_FLAGS_RANDOM | \
|
---|
| 524 | OPEN_FLAGS_NO_CACHE)
|
---|
| 525 | #define DOSOPEN_FD_ACCESS (OPEN_SHARE_DENYREADWRITE | OPEN_ACCESS_READWRITE)
|
---|
| 526 | #define DOSOPEN_HD_ACCESS (OPEN_SHARE_DENYNONE | OPEN_ACCESS_READONLY)
|
---|
| 527 |
|
---|
| 528 | if (skip_drive(name) > name) {
|
---|
| 529 | rc = DosOpen(
|
---|
| 530 | name, &FileHandle, &Action, 0L, FILE_NORMAL,
|
---|
| 531 | OPEN_ACTION_OPEN_IF_EXISTS, DOSOPEN_FLAGS |
|
---|
| 532 | (IS_NOLOCK(dev)?DOSOPEN_HD_ACCESS:DOSOPEN_FD_ACCESS),
|
---|
| 533 | 0L);
|
---|
| 534 | #ifdef DEBUG
|
---|
| 535 | if (rc != NO_ERROR) fprintf (stderr, "DosOpen() returned %d\n", rc);
|
---|
| 536 | #endif
|
---|
| 537 | if (!IS_NOLOCK(dev)) {
|
---|
| 538 | rc = DosDevIOCtl(
|
---|
| 539 | FileHandle, 0x08L, DSK_LOCKDRIVE, 0, 0, 0, 0, 0, 0);
|
---|
| 540 | #ifdef DEBUG
|
---|
| 541 | if (rc != NO_ERROR) fprintf (stderr, "DosDevIOCtl() returned %d\n", rc);
|
---|
| 542 | #endif
|
---|
| 543 | }
|
---|
| 544 | if (rc == NO_ERROR)
|
---|
| 545 | This->fd = _imphandle(FileHandle); else This->fd = -1;
|
---|
| 546 | } else
|
---|
| 547 | #endif
|
---|
| 548 | {
|
---|
| 549 | if (IS_SCSI(dev))
|
---|
| 550 | This->fd = scsi_open(name, mode, IS_NOLOCK(dev)?0444:0666,
|
---|
| 551 | &This->extra_data);
|
---|
| 552 | else
|
---|
| 553 | This->fd = open(name, mode, IS_NOLOCK(dev)?0444:0666);
|
---|
| 554 | }
|
---|
| 555 |
|
---|
| 556 | if(IS_PRIVILEGED(dev) && !(mode2 & NO_PRIV))
|
---|
| 557 | drop_privs();
|
---|
| 558 |
|
---|
| 559 | if (This->fd < 0) {
|
---|
| 560 | Free(This);
|
---|
| 561 | if(errmsg)
|
---|
| 562 | #ifdef HAVE_SNPRINTF
|
---|
| 563 | snprintf(errmsg, 199, "Can't open %s: %s",
|
---|
| 564 | name, strerror(errno));
|
---|
| 565 | #else
|
---|
| 566 | sprintf(errmsg, "Can't open %s: %s",
|
---|
| 567 | name, strerror(errno));
|
---|
| 568 | #endif
|
---|
| 569 | return NULL;
|
---|
| 570 | }
|
---|
| 571 |
|
---|
| 572 | if(IS_PRIVILEGED(dev) && !(mode2 & NO_PRIV))
|
---|
| 573 | closeExec(This->fd);
|
---|
| 574 |
|
---|
| 575 | #ifdef __EMX__
|
---|
| 576 | if (*(name+1) != ':')
|
---|
| 577 | #endif
|
---|
| 578 | if (fstat(This->fd, &This->stat) < 0){
|
---|
| 579 | Free(This);
|
---|
| 580 | if(errmsg) {
|
---|
| 581 | #ifdef HAVE_SNPRINTF
|
---|
| 582 | snprintf(errmsg,199,"Can't stat %s: %s",
|
---|
| 583 | name, strerror(errno));
|
---|
| 584 | #else
|
---|
| 585 | if(strlen(name) > 50) {
|
---|
| 586 | sprintf(errmsg,"Can't stat file: %s",
|
---|
| 587 | strerror(errno));
|
---|
| 588 | } else {
|
---|
| 589 | sprintf(errmsg,"Can't stat %s: %s",
|
---|
| 590 | name, strerror(errno));
|
---|
| 591 | }
|
---|
| 592 | #endif
|
---|
| 593 | }
|
---|
| 594 | return NULL;
|
---|
| 595 | }
|
---|
| 596 | #ifndef __EMX__
|
---|
| 597 | /* lock the device on writes */
|
---|
| 598 | if (locked && lock_dev(This->fd, mode == O_RDWR, dev)) {
|
---|
| 599 | if(errmsg)
|
---|
| 600 | #ifdef HAVE_SNPRINTF
|
---|
| 601 | snprintf(errmsg,199,
|
---|
| 602 | "plain floppy: device \"%s\" busy (%s):",
|
---|
| 603 | dev ? dev->name : "unknown", strerror(errno));
|
---|
| 604 | #else
|
---|
| 605 | sprintf(errmsg,
|
---|
| 606 | "plain floppy: device \"%s\" busy (%s):",
|
---|
| 607 | (dev && strlen(dev->name) < 50) ?
|
---|
| 608 | dev->name : "unknown", strerror(errno));
|
---|
| 609 | #endif
|
---|
| 610 |
|
---|
| 611 | close(This->fd);
|
---|
| 612 | Free(This);
|
---|
| 613 | return NULL;
|
---|
| 614 | }
|
---|
| 615 | #endif
|
---|
| 616 | /* set default parameters, if needed */
|
---|
| 617 | if (dev){
|
---|
| 618 | if ((IS_MFORMAT_ONLY(dev) || !dev->tracks) &&
|
---|
| 619 | init_geom(This->fd, dev, orig_dev, &This->stat)){
|
---|
| 620 | close(This->fd);
|
---|
| 621 | Free(This);
|
---|
| 622 | if(errmsg)
|
---|
| 623 | sprintf(errmsg,"init: set default params");
|
---|
| 624 | return NULL;
|
---|
| 625 | }
|
---|
| 626 | This->offset = (mt_off_t) dev->offset;
|
---|
| 627 | } else
|
---|
| 628 | This->offset = 0;
|
---|
| 629 |
|
---|
| 630 | This->refs = 1;
|
---|
| 631 | This->Next = 0;
|
---|
| 632 | This->Buffer = 0;
|
---|
| 633 |
|
---|
| 634 | if(maxSize) {
|
---|
| 635 | if (IS_SCSI(dev)) {
|
---|
| 636 | *maxSize = MAX_OFF_T_B(31+log_2(This->scsi_sector_size));
|
---|
| 637 | } else {
|
---|
| 638 | *maxSize = max_off_t_seek;
|
---|
| 639 | }
|
---|
| 640 | if(This->offset > *maxSize) {
|
---|
| 641 | close(This->fd);
|
---|
| 642 | Free(This);
|
---|
| 643 | if(errmsg)
|
---|
| 644 | sprintf(errmsg,"init: Big disks not supported");
|
---|
| 645 | return NULL;
|
---|
| 646 | }
|
---|
| 647 |
|
---|
| 648 | *maxSize -= This->offset;
|
---|
| 649 | }
|
---|
| 650 | /* partitioned drive */
|
---|
| 651 |
|
---|
| 652 | /* jpd@usl.edu: assume a partitioned drive on these 2 systems is a ZIP*/
|
---|
| 653 | /* or similar drive that must be accessed by low-level scsi commands */
|
---|
| 654 | /* AK: introduce new "scsi=1" statement to specifically set
|
---|
| 655 | * this option. Indeed, there could conceivably be partitioned
|
---|
| 656 | * devices where low level scsi commands will not be needed */
|
---|
| 657 | if(IS_SCSI(dev)) {
|
---|
| 658 | This->Class = &ScsiClass;
|
---|
| 659 | if(This->privileged)
|
---|
| 660 | reclaim_privs();
|
---|
| 661 | scsi_init(This);
|
---|
| 662 | if(This->privileged)
|
---|
| 663 | drop_privs();
|
---|
| 664 | }
|
---|
| 665 | while(!(mode2 & NO_OFFSET) &&
|
---|
| 666 | dev && dev->partition && dev->partition <= 4) {
|
---|
| 667 | int has_activated, last_end, j;
|
---|
| 668 | unsigned char buf[2048];
|
---|
| 669 | struct partition *partTable=(struct partition *)(buf+ 0x1ae);
|
---|
| 670 | size_t partOff;
|
---|
| 671 |
|
---|
| 672 | /* read the first sector, or part of it */
|
---|
| 673 | if (force_read((Stream_t *)This, (char*) buf, 0, 512) != 512)
|
---|
| 674 | break;
|
---|
| 675 | if( _WORD(buf+510) != 0xaa55)
|
---|
| 676 | break;
|
---|
| 677 |
|
---|
| 678 | partOff = BEGIN(partTable[dev->partition]);
|
---|
| 679 | if (maxSize) {
|
---|
| 680 | if (partOff > *maxSize >> 9) {
|
---|
| 681 | close(This->fd);
|
---|
| 682 | Free(This);
|
---|
| 683 | if(errmsg)
|
---|
| 684 | sprintf(errmsg,"init: Big disks not supported");
|
---|
| 685 | return NULL;
|
---|
| 686 | }
|
---|
| 687 | *maxSize -= (mt_off_t) partOff << 9;
|
---|
| 688 | }
|
---|
| 689 |
|
---|
| 690 | This->offset += (mt_off_t) partOff << 9;
|
---|
| 691 | if(!partTable[dev->partition].sys_ind) {
|
---|
| 692 | if(errmsg)
|
---|
| 693 | sprintf(errmsg,
|
---|
| 694 | "init: non-existant partition");
|
---|
| 695 | close(This->fd);
|
---|
| 696 | Free(This);
|
---|
| 697 | return NULL;
|
---|
| 698 | }
|
---|
| 699 |
|
---|
| 700 | if(!dev->tracks) {
|
---|
| 701 | dev->heads = head(partTable[dev->partition].end)+1;
|
---|
| 702 | dev->sectors = sector(partTable[dev->partition].end);
|
---|
| 703 | dev->tracks = cyl(partTable[dev->partition].end) -
|
---|
| 704 | cyl(partTable[dev->partition].start)+1;
|
---|
| 705 | }
|
---|
| 706 | dev->hidden=dev->sectors*head(partTable[dev->partition].start);
|
---|
| 707 | if(!mtools_skip_check &&
|
---|
| 708 | consistencyCheck((struct partition *)(buf+0x1ae), 0, 0,
|
---|
| 709 | &has_activated, &last_end, &j, dev, 0)) {
|
---|
| 710 | fprintf(stderr,
|
---|
| 711 | "Warning: inconsistent partition table\n");
|
---|
| 712 | fprintf(stderr,
|
---|
| 713 | "Possibly unpartitioned device\n");
|
---|
| 714 | fprintf(stderr,
|
---|
| 715 | "\n*** Maybe try without partition=%d in "
|
---|
| 716 | "device definition ***\n\n",
|
---|
| 717 | dev->partition);
|
---|
| 718 | fprintf(stderr,
|
---|
| 719 | "If this is a PCMCIA card, or a disk "
|
---|
| 720 | "partitioned on another computer, this "
|
---|
| 721 | "message may be in error: add "
|
---|
| 722 | "mtools_skip_check=1 to your .mtoolsrc "
|
---|
| 723 | "file to suppress this warning\n");
|
---|
| 724 |
|
---|
| 725 | }
|
---|
| 726 | break;
|
---|
| 727 | /* NOTREACHED */
|
---|
| 728 | }
|
---|
| 729 |
|
---|
| 730 | This->lastwhere = -This->offset;
|
---|
| 731 | /* provoke a seek on those devices that don't start on a partition
|
---|
| 732 | * boundary */
|
---|
| 733 |
|
---|
| 734 | return (Stream_t *) This;
|
---|
| 735 | }
|
---|
| 736 |
|
---|
| 737 | int get_fd(Stream_t *Stream)
|
---|
| 738 | {
|
---|
| 739 | DeclareThis(SimpleFile_t);
|
---|
| 740 |
|
---|
| 741 | return This->fd;
|
---|
| 742 | }
|
---|
| 743 |
|
---|
| 744 | void *get_extra_data(Stream_t *Stream)
|
---|
| 745 | {
|
---|
| 746 | DeclareThis(SimpleFile_t);
|
---|
| 747 |
|
---|
| 748 | return This->extra_data;
|
---|
| 749 | }
|
---|