io.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /* io.c - Virtual disk input/output */
  2. /* Written 1993 by Werner Almesberger */
  3. /*
  4. * Thu Feb 26 01:15:36 CET 1998: Martin Schulze <joey@infodrom.north.de>
  5. * Fixed nasty bug that caused every file with a name like
  6. * xxxxxxxx.xxx to be treated as bad name that needed to be fixed.
  7. */
  8. /* FAT32, VFAT, Atari format support, and various fixes additions May 1998
  9. * by Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de> */
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <unistd.h>
  14. #include <sys/stat.h>
  15. #include <sys/ioctl.h>
  16. #include <errno.h>
  17. #include <fcntl.h>
  18. #include <linux/fd.h>
  19. #include "dosfsck.h"
  20. #include "common.h"
  21. #include "io.h"
  22. typedef struct _change {
  23. void *data;
  24. loff_t pos;
  25. int size;
  26. struct _change *next;
  27. } CHANGE;
  28. static CHANGE *changes,*last;
  29. static int fd,did_change = 0;
  30. unsigned device_no;
  31. #ifdef __DJGPP__
  32. #include "volume.h" /* DOS lowlevel disk access functions */
  33. #undef llseek
  34. static loff_t llseek( int fd, loff_t offset, int whence )
  35. {
  36. if ((whence != SEEK_SET) || (fd == 4711)) return -1; /* only those supported */
  37. return VolumeSeek(offset);
  38. }
  39. #define open OpenVolume
  40. #define close CloseVolume
  41. #define read(a,b,c) ReadVolume(b,c)
  42. #define write(a,b,c) WriteVolume(b,c)
  43. #endif
  44. void fs_open(char *path,int rw)
  45. {
  46. struct stat stbuf;
  47. if ((fd = open(path,rw ? O_RDWR : O_RDONLY)) < 0)
  48. pdie("open %s",path);
  49. changes = last = NULL;
  50. did_change = 0;
  51. #ifndef _DJGPP_
  52. if (fstat(fd,&stbuf) < 0)
  53. pdie("fstat %s",path);
  54. device_no = S_ISBLK(stbuf.st_mode) ? (stbuf.st_rdev >> 8) & 0xff : 0;
  55. #else
  56. if (IsWorkingOnImageFile()) {
  57. if (fstat(GetVolumeHandle(),&stbuf) < 0)
  58. pdie("fstat image %s",path);
  59. device_no = 0;
  60. }
  61. else {
  62. /* return 2 for floppy, 1 for ramdisk, 7 for loopback */
  63. /* used by boot.c in Atari mode: floppy always FAT12, */
  64. /* loopback / ramdisk only FAT12 if usual floppy size, */
  65. /* harddisk always FAT16 on Atari... */
  66. device_no = (GetVolumeHandle() < 2) ? 2 : 1;
  67. /* telling "floppy" for A:/B:, "ramdisk" for the rest */
  68. }
  69. #endif
  70. }
  71. void fs_read(loff_t pos,int size,void *data)
  72. {
  73. CHANGE *walk;
  74. int got;
  75. if (llseek(fd,pos,0) != pos) pdie("Seek to %lld",pos);
  76. if ((got = read(fd,data,size)) < 0) pdie("Read %d bytes at %lld",size,pos);
  77. if (got != size) die("Got %d bytes instead of %d at %lld",got,size,pos);
  78. for (walk = changes; walk; walk = walk->next) {
  79. if (walk->pos < pos+size && walk->pos+walk->size > pos) {
  80. if (walk->pos < pos)
  81. memcpy(data,(char *) walk->data+pos-walk->pos,min(size,
  82. walk->size-pos+walk->pos));
  83. else memcpy((char *) data+walk->pos-pos,walk->data,min(walk->size,
  84. size+pos-walk->pos));
  85. }
  86. }
  87. }
  88. int fs_test(loff_t pos,int size)
  89. {
  90. void *scratch;
  91. int okay;
  92. if (llseek(fd,pos,0) != pos) pdie("Seek to %lld",pos);
  93. scratch = alloc(size);
  94. okay = read(fd,scratch,size) == size;
  95. free(scratch);
  96. return okay;
  97. }
  98. void fs_write(loff_t pos,int size,void *data)
  99. {
  100. CHANGE *new;
  101. int did;
  102. if (write_immed) {
  103. did_change = 1;
  104. if (llseek(fd,pos,0) != pos) pdie("Seek to %lld",pos);
  105. if ((did = write(fd,data,size)) == size) return;
  106. if (did < 0) pdie("Write %d bytes at %lld",size,pos);
  107. die("Wrote %d bytes instead of %d at %lld",did,size,pos);
  108. }
  109. new = alloc(sizeof(CHANGE));
  110. new->pos = pos;
  111. memcpy(new->data = alloc(new->size = size),data,size);
  112. new->next = NULL;
  113. if (last) last->next = new;
  114. else changes = new;
  115. last = new;
  116. }
  117. static void fs_flush(void)
  118. {
  119. CHANGE *this;
  120. int size;
  121. while (changes) {
  122. this = changes;
  123. changes = changes->next;
  124. if (llseek(fd,this->pos,0) != this->pos)
  125. fprintf(stderr,"Seek to %lld failed: %s\n Did not write %d bytes.\n",
  126. (long long)this->pos,strerror(errno),this->size);
  127. else if ((size = write(fd,this->data,this->size)) < 0)
  128. fprintf(stderr,"Writing %d bytes at %lld failed: %s\n",this->size,
  129. (long long)this->pos,strerror(errno));
  130. else if (size != this->size)
  131. fprintf(stderr,"Wrote %d bytes instead of %d bytes at %lld."
  132. "\n",size,this->size,(long long)this->pos);
  133. free(this->data);
  134. free(this);
  135. }
  136. }
  137. int fs_close(int write)
  138. {
  139. CHANGE *next;
  140. int changed;
  141. changed = !!changes;
  142. if (write) fs_flush();
  143. else while (changes) {
  144. next = changes->next;
  145. free(changes->data);
  146. free(changes);
  147. changes = next;
  148. }
  149. if (close(fd) < 0) pdie("closing file system");
  150. return changed || did_change;
  151. }
  152. int fs_changed(void)
  153. {
  154. return !!changes || did_change;
  155. }
  156. /* Local Variables: */
  157. /* tab-width: 8 */
  158. /* End: */