io.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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))
  52. return -1; /* only those supported */
  53. return VolumeSeek(offset);
  54. }
  55. #define open OpenVolume
  56. #define close CloseVolume
  57. #define read(a,b,c) ReadVolume(b,c)
  58. #define write(a,b,c) WriteVolume(b,c)
  59. #else
  60. loff_t llseek(int fd, loff_t offset, int whence)
  61. {
  62. return (loff_t) lseek64(fd, (off64_t) offset, whence);
  63. }
  64. #endif
  65. void fs_open(char *path, int rw)
  66. {
  67. struct stat stbuf;
  68. if ((fd = open(path, rw ? O_RDWR : O_RDONLY)) < 0) {
  69. perror("open");
  70. exit(6);
  71. }
  72. changes = last = NULL;
  73. did_change = 0;
  74. #ifndef _DJGPP_
  75. if (fstat(fd, &stbuf) < 0)
  76. pdie("fstat %s", path);
  77. device_no = S_ISBLK(stbuf.st_mode) ? (stbuf.st_rdev >> 8) & 0xff : 0;
  78. #else
  79. if (IsWorkingOnImageFile()) {
  80. if (fstat(GetVolumeHandle(), &stbuf) < 0)
  81. pdie("fstat image %s", path);
  82. device_no = 0;
  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)
  107. pdie("Seek to %lld", pos);
  108. if ((got = read(fd, data, size)) < 0)
  109. pdie("Read %d bytes at %lld", size, pos);
  110. if (got != size)
  111. die("Got %d bytes instead of %d at %lld", got, size, pos);
  112. for (walk = changes; walk; walk = walk->next) {
  113. if (walk->pos < pos + size && walk->pos + walk->size > pos) {
  114. if (walk->pos < pos)
  115. memcpy(data, (char *)walk->data + pos - walk->pos, min(size,
  116. walk->
  117. size -
  118. pos +
  119. walk->
  120. pos));
  121. else
  122. memcpy((char *)data + walk->pos - pos, walk->data,
  123. min(walk->size, size + pos - walk->pos));
  124. }
  125. }
  126. }
  127. int fs_test(loff_t pos, int size)
  128. {
  129. void *scratch;
  130. int okay;
  131. if (llseek(fd, pos, 0) != pos)
  132. pdie("Seek to %lld", pos);
  133. scratch = alloc(size);
  134. okay = read(fd, scratch, size) == size;
  135. free(scratch);
  136. return okay;
  137. }
  138. void fs_write(loff_t pos, int size, void *data)
  139. {
  140. CHANGE *new;
  141. int did;
  142. if (write_immed) {
  143. did_change = 1;
  144. if (llseek(fd, pos, 0) != pos)
  145. pdie("Seek to %lld", pos);
  146. if ((did = write(fd, data, size)) == size)
  147. return;
  148. if (did < 0)
  149. pdie("Write %d bytes at %lld", size, pos);
  150. die("Wrote %d bytes instead of %d at %lld", did, size, pos);
  151. }
  152. new = alloc(sizeof(CHANGE));
  153. new->pos = pos;
  154. memcpy(new->data = alloc(new->size = size), data, size);
  155. new->next = NULL;
  156. if (last)
  157. last->next = new;
  158. else
  159. changes = new;
  160. last = new;
  161. }
  162. static void fs_flush(void)
  163. {
  164. CHANGE *this;
  165. int size;
  166. while (changes) {
  167. this = changes;
  168. changes = changes->next;
  169. if (llseek(fd, this->pos, 0) != this->pos)
  170. fprintf(stderr,
  171. "Seek to %lld failed: %s\n Did not write %d bytes.\n",
  172. (long long)this->pos, strerror(errno), this->size);
  173. else if ((size = write(fd, this->data, this->size)) < 0)
  174. fprintf(stderr, "Writing %d bytes at %lld failed: %s\n", this->size,
  175. (long long)this->pos, strerror(errno));
  176. else if (size != this->size)
  177. fprintf(stderr, "Wrote %d bytes instead of %d bytes at %lld."
  178. "\n", size, this->size, (long long)this->pos);
  179. free(this->data);
  180. free(this);
  181. }
  182. }
  183. int fs_close(int write)
  184. {
  185. CHANGE *next;
  186. int changed;
  187. changed = ! !changes;
  188. if (write)
  189. fs_flush();
  190. else
  191. while (changes) {
  192. next = changes->next;
  193. free(changes->data);
  194. free(changes);
  195. changes = next;
  196. }
  197. if (close(fd) < 0)
  198. pdie("closing file system");
  199. return changed || did_change;
  200. }
  201. int fs_changed(void)
  202. {
  203. return ! !changes || did_change;
  204. }