io.c 5.4 KB

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