io.c 6.0 KB

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