io.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. #define _LARGEFILE64_SOURCE
  25. #include <sys/types.h>
  26. #include <stdlib.h>
  27. #include <stdio.h>
  28. #include <string.h>
  29. #include <unistd.h>
  30. #include <sys/stat.h>
  31. #include <sys/ioctl.h>
  32. #include <errno.h>
  33. #include <fcntl.h>
  34. #include <linux/fd.h>
  35. #include "dosfsck.h"
  36. #include "common.h"
  37. #include "io.h"
  38. typedef struct _change {
  39. void *data;
  40. loff_t pos;
  41. int size;
  42. struct _change *next;
  43. } CHANGE;
  44. static CHANGE *changes,*last;
  45. static int fd,did_change = 0;
  46. unsigned device_no;
  47. #ifdef __DJGPP__
  48. #include "volume.h" /* DOS lowlevel disk access functions */
  49. loff_t llseek(int fd, loff_t offset, int whence)
  50. {
  51. if ((whence != SEEK_SET) || (fd == 4711)) return -1; /* only those supported */
  52. return VolumeSeek(offset);
  53. }
  54. #define open OpenVolume
  55. #define close CloseVolume
  56. #define read(a,b,c) ReadVolume(b,c)
  57. #define write(a,b,c) WriteVolume(b,c)
  58. #else
  59. loff_t llseek(int fd, loff_t offset, int whence)
  60. {
  61. return (loff_t) lseek64(fd, (off64_t)offset, whence);
  62. }
  63. #endif
  64. void fs_open(char *path,int rw)
  65. {
  66. struct stat stbuf;
  67. if ((fd = open(path,rw ? O_RDWR : O_RDONLY)) < 0) {
  68. perror("open");
  69. exit(6);
  70. }
  71. changes = last = NULL;
  72. did_change = 0;
  73. #ifndef _DJGPP_
  74. if (fstat(fd,&stbuf) < 0)
  75. pdie("fstat %s",path);
  76. device_no = S_ISBLK(stbuf.st_mode) ? (stbuf.st_rdev >> 8) & 0xff : 0;
  77. #else
  78. if (IsWorkingOnImageFile()) {
  79. if (fstat(GetVolumeHandle(),&stbuf) < 0)
  80. pdie("fstat image %s",path);
  81. device_no = 0;
  82. }
  83. else {
  84. /* return 2 for floppy, 1 for ramdisk, 7 for loopback */
  85. /* used by boot.c in Atari mode: floppy always FAT12, */
  86. /* loopback / ramdisk only FAT12 if usual floppy size, */
  87. /* harddisk always FAT16 on Atari... */
  88. device_no = (GetVolumeHandle() < 2) ? 2 : 1;
  89. /* telling "floppy" for A:/B:, "ramdisk" for the rest */
  90. }
  91. #endif
  92. }
  93. /**
  94. * Read data from the partition, accounting for any pending updates that are
  95. * queued for writing.
  96. *
  97. * @param[in] pos Byte offset, relative to the beginning of the partition,
  98. * at which to read
  99. * @param[in] size Number of bytes to read
  100. * @param[out] data Where to put the data read
  101. */
  102. void fs_read(loff_t pos,int size,void *data)
  103. {
  104. CHANGE *walk;
  105. int got;
  106. if (llseek(fd,pos,0) != pos) pdie("Seek to %lld",pos);
  107. if ((got = read(fd,data,size)) < 0) pdie("Read %d bytes at %lld",size,pos);
  108. if (got != size) die("Got %d bytes instead of %d at %lld",got,size,pos);
  109. for (walk = changes; walk; walk = walk->next) {
  110. if (walk->pos < pos+size && walk->pos+walk->size > pos) {
  111. if (walk->pos < pos)
  112. memcpy(data,(char *) walk->data+pos-walk->pos,min(size,
  113. walk->size-pos+walk->pos));
  114. else memcpy((char *) data+walk->pos-pos,walk->data,min(walk->size,
  115. size+pos-walk->pos));
  116. }
  117. }
  118. }
  119. int fs_test(loff_t pos,int size)
  120. {
  121. void *scratch;
  122. int okay;
  123. if (llseek(fd,pos,0) != pos) pdie("Seek to %lld",pos);
  124. scratch = alloc(size);
  125. okay = read(fd,scratch,size) == size;
  126. free(scratch);
  127. return okay;
  128. }
  129. void fs_write(loff_t pos,int size,void *data)
  130. {
  131. CHANGE *new;
  132. int did;
  133. if (write_immed) {
  134. did_change = 1;
  135. if (llseek(fd,pos,0) != pos) pdie("Seek to %lld",pos);
  136. if ((did = write(fd,data,size)) == size) return;
  137. if (did < 0) pdie("Write %d bytes at %lld",size,pos);
  138. die("Wrote %d bytes instead of %d at %lld",did,size,pos);
  139. }
  140. new = alloc(sizeof(CHANGE));
  141. new->pos = pos;
  142. memcpy(new->data = alloc(new->size = size),data,size);
  143. new->next = NULL;
  144. if (last) last->next = new;
  145. else changes = new;
  146. last = new;
  147. }
  148. static void fs_flush(void)
  149. {
  150. CHANGE *this;
  151. int size;
  152. while (changes) {
  153. this = changes;
  154. changes = changes->next;
  155. if (llseek(fd,this->pos,0) != this->pos)
  156. fprintf(stderr,"Seek to %lld failed: %s\n Did not write %d bytes.\n",
  157. (long long)this->pos,strerror(errno),this->size);
  158. else if ((size = write(fd,this->data,this->size)) < 0)
  159. fprintf(stderr,"Writing %d bytes at %lld failed: %s\n",this->size,
  160. (long long)this->pos,strerror(errno));
  161. else if (size != this->size)
  162. fprintf(stderr,"Wrote %d bytes instead of %d bytes at %lld."
  163. "\n",size,this->size,(long long)this->pos);
  164. free(this->data);
  165. free(this);
  166. }
  167. }
  168. int fs_close(int write)
  169. {
  170. CHANGE *next;
  171. int changed;
  172. changed = !!changes;
  173. if (write) fs_flush();
  174. else while (changes) {
  175. next = changes->next;
  176. free(changes->data);
  177. free(changes);
  178. changes = next;
  179. }
  180. if (close(fd) < 0) pdie("closing file system");
  181. return changed || did_change;
  182. }
  183. int fs_changed(void)
  184. {
  185. return !!changes || did_change;
  186. }