imager.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /*
  2. * image.c --- writes out the critical parts of the filesystem as a
  3. * flat file.
  4. *
  5. * Copyright (C) 2000 Theodore Ts'o.
  6. *
  7. * Note: this uses the POSIX IO interfaces, unlike most of the other
  8. * functions in this library. So sue me.
  9. *
  10. * %Begin-Header%
  11. * This file may be redistributed under the terms of the GNU Public
  12. * License.
  13. * %End-Header%
  14. */
  15. #include <stdio.h>
  16. #include <string.h>
  17. #if HAVE_UNISTD_H
  18. #include <unistd.h>
  19. #endif
  20. #if HAVE_ERRNO_H
  21. #include <errno.h>
  22. #endif
  23. #include <fcntl.h>
  24. #include <time.h>
  25. #if HAVE_SYS_STAT_H
  26. #include <sys/stat.h>
  27. #endif
  28. #if HAVE_SYS_TYPES_H
  29. #include <sys/types.h>
  30. #endif
  31. #include "ext2_fs.h"
  32. #include "ext2fs.h"
  33. #ifndef HAVE_TYPE_SSIZE_T
  34. typedef int ssize_t;
  35. #endif
  36. /*
  37. * This function returns 1 if the specified block is all zeros
  38. */
  39. static int check_zero_block(char *buf, int blocksize)
  40. {
  41. char *cp = buf;
  42. int left = blocksize;
  43. while (left > 0) {
  44. if (*cp++)
  45. return 0;
  46. left--;
  47. }
  48. return 1;
  49. }
  50. /*
  51. * Write the inode table out as a single block.
  52. */
  53. #define BUF_BLOCKS 32
  54. errcode_t ext2fs_image_inode_write(ext2_filsys fs, int fd, int flags)
  55. {
  56. unsigned int group, left, c, d;
  57. char *buf, *cp;
  58. blk_t blk;
  59. ssize_t actual;
  60. errcode_t retval;
  61. buf = xmalloc(fs->blocksize * BUF_BLOCKS);
  62. for (group = 0; group < fs->group_desc_count; group++) {
  63. blk = fs->group_desc[(unsigned)group].bg_inode_table;
  64. if (!blk)
  65. return EXT2_ET_MISSING_INODE_TABLE;
  66. left = fs->inode_blocks_per_group;
  67. while (left) {
  68. c = BUF_BLOCKS;
  69. if (c > left)
  70. c = left;
  71. retval = io_channel_read_blk(fs->io, blk, c, buf);
  72. if (retval)
  73. goto errout;
  74. cp = buf;
  75. while (c) {
  76. if (!(flags & IMAGER_FLAG_SPARSEWRITE)) {
  77. d = c;
  78. goto skip_sparse;
  79. }
  80. /* Skip zero blocks */
  81. if (check_zero_block(cp, fs->blocksize)) {
  82. c--;
  83. blk++;
  84. left--;
  85. cp += fs->blocksize;
  86. lseek(fd, fs->blocksize, SEEK_CUR);
  87. continue;
  88. }
  89. /* Find non-zero blocks */
  90. for (d=1; d < c; d++) {
  91. if (check_zero_block(cp + d*fs->blocksize, fs->blocksize))
  92. break;
  93. }
  94. skip_sparse:
  95. actual = write(fd, cp, fs->blocksize * d);
  96. if (actual == -1) {
  97. retval = errno;
  98. goto errout;
  99. }
  100. if (actual != (ssize_t) (fs->blocksize * d)) {
  101. retval = EXT2_ET_SHORT_WRITE;
  102. goto errout;
  103. }
  104. blk += d;
  105. left -= d;
  106. cp += fs->blocksize * d;
  107. c -= d;
  108. }
  109. }
  110. }
  111. retval = 0;
  112. errout:
  113. free(buf);
  114. return retval;
  115. }
  116. /*
  117. * Read in the inode table and stuff it into place
  118. */
  119. errcode_t ext2fs_image_inode_read(ext2_filsys fs, int fd,
  120. int flags EXT2FS_ATTR((unused)))
  121. {
  122. unsigned int group, c, left;
  123. char *buf;
  124. blk_t blk;
  125. ssize_t actual;
  126. errcode_t retval;
  127. buf = xmalloc(fs->blocksize * BUF_BLOCKS);
  128. for (group = 0; group < fs->group_desc_count; group++) {
  129. blk = fs->group_desc[(unsigned)group].bg_inode_table;
  130. if (!blk) {
  131. retval = EXT2_ET_MISSING_INODE_TABLE;
  132. goto errout;
  133. }
  134. left = fs->inode_blocks_per_group;
  135. while (left) {
  136. c = BUF_BLOCKS;
  137. if (c > left)
  138. c = left;
  139. actual = read(fd, buf, fs->blocksize * c);
  140. if (actual == -1) {
  141. retval = errno;
  142. goto errout;
  143. }
  144. if (actual != (ssize_t) (fs->blocksize * c)) {
  145. retval = EXT2_ET_SHORT_READ;
  146. goto errout;
  147. }
  148. retval = io_channel_write_blk(fs->io, blk, c, buf);
  149. if (retval)
  150. goto errout;
  151. blk += c;
  152. left -= c;
  153. }
  154. }
  155. retval = ext2fs_flush_icache(fs);
  156. errout:
  157. free(buf);
  158. return retval;
  159. }
  160. /*
  161. * Write out superblock and group descriptors
  162. */
  163. errcode_t ext2fs_image_super_write(ext2_filsys fs, int fd,
  164. int flags EXT2FS_ATTR((unused)))
  165. {
  166. char *buf, *cp;
  167. ssize_t actual;
  168. errcode_t retval;
  169. buf = xmalloc(fs->blocksize);
  170. /*
  171. * Write out the superblock
  172. */
  173. memset(buf, 0, fs->blocksize);
  174. memcpy(buf, fs->super, SUPERBLOCK_SIZE);
  175. actual = write(fd, buf, fs->blocksize);
  176. if (actual == -1) {
  177. retval = errno;
  178. goto errout;
  179. }
  180. if (actual != (ssize_t) fs->blocksize) {
  181. retval = EXT2_ET_SHORT_WRITE;
  182. goto errout;
  183. }
  184. /*
  185. * Now write out the block group descriptors
  186. */
  187. cp = (char *) fs->group_desc;
  188. actual = write(fd, cp, fs->blocksize * fs->desc_blocks);
  189. if (actual == -1) {
  190. retval = errno;
  191. goto errout;
  192. }
  193. if (actual != (ssize_t) (fs->blocksize * fs->desc_blocks)) {
  194. retval = EXT2_ET_SHORT_WRITE;
  195. goto errout;
  196. }
  197. retval = 0;
  198. errout:
  199. free(buf);
  200. return retval;
  201. }
  202. /*
  203. * Read the superblock and group descriptors and overwrite them.
  204. */
  205. errcode_t ext2fs_image_super_read(ext2_filsys fs, int fd,
  206. int flags EXT2FS_ATTR((unused)))
  207. {
  208. char *buf;
  209. ssize_t actual, size;
  210. errcode_t retval;
  211. size = fs->blocksize * (fs->group_desc_count + 1);
  212. buf = xmalloc(size);
  213. /*
  214. * Read it all in.
  215. */
  216. actual = read(fd, buf, size);
  217. if (actual == -1) {
  218. retval = errno;
  219. goto errout;
  220. }
  221. if (actual != size) {
  222. retval = EXT2_ET_SHORT_READ;
  223. goto errout;
  224. }
  225. /*
  226. * Now copy in the superblock and group descriptors
  227. */
  228. memcpy(fs->super, buf, SUPERBLOCK_SIZE);
  229. memcpy(fs->group_desc, buf + fs->blocksize,
  230. fs->blocksize * fs->group_desc_count);
  231. retval = 0;
  232. errout:
  233. free(buf);
  234. return retval;
  235. }
  236. /*
  237. * Write the block/inode bitmaps.
  238. */
  239. errcode_t ext2fs_image_bitmap_write(ext2_filsys fs, int fd, int flags)
  240. {
  241. char *ptr;
  242. int c, size;
  243. char zero_buf[1024];
  244. ssize_t actual;
  245. errcode_t retval;
  246. if (flags & IMAGER_FLAG_INODEMAP) {
  247. if (!fs->inode_map) {
  248. retval = ext2fs_read_inode_bitmap(fs);
  249. if (retval)
  250. return retval;
  251. }
  252. ptr = fs->inode_map->bitmap;
  253. size = (EXT2_INODES_PER_GROUP(fs->super) / 8);
  254. } else {
  255. if (!fs->block_map) {
  256. retval = ext2fs_read_block_bitmap(fs);
  257. if (retval)
  258. return retval;
  259. }
  260. ptr = fs->block_map->bitmap;
  261. size = EXT2_BLOCKS_PER_GROUP(fs->super) / 8;
  262. }
  263. size = size * fs->group_desc_count;
  264. actual = write(fd, ptr, size);
  265. if (actual == -1) {
  266. retval = errno;
  267. goto errout;
  268. }
  269. if (actual != size) {
  270. retval = EXT2_ET_SHORT_WRITE;
  271. goto errout;
  272. }
  273. size = size % fs->blocksize;
  274. memset(zero_buf, 0, sizeof(zero_buf));
  275. if (size) {
  276. size = fs->blocksize - size;
  277. while (size) {
  278. c = size;
  279. if (c > (int) sizeof(zero_buf))
  280. c = sizeof(zero_buf);
  281. actual = write(fd, zero_buf, c);
  282. if (actual == -1) {
  283. retval = errno;
  284. goto errout;
  285. }
  286. if (actual != c) {
  287. retval = EXT2_ET_SHORT_WRITE;
  288. goto errout;
  289. }
  290. size -= c;
  291. }
  292. }
  293. retval = 0;
  294. errout:
  295. return (retval);
  296. }
  297. /*
  298. * Read the block/inode bitmaps.
  299. */
  300. errcode_t ext2fs_image_bitmap_read(ext2_filsys fs, int fd, int flags)
  301. {
  302. char *ptr, *buf = 0;
  303. int size;
  304. ssize_t actual;
  305. errcode_t retval;
  306. if (flags & IMAGER_FLAG_INODEMAP) {
  307. if (!fs->inode_map) {
  308. retval = ext2fs_read_inode_bitmap(fs);
  309. if (retval)
  310. return retval;
  311. }
  312. ptr = fs->inode_map->bitmap;
  313. size = (EXT2_INODES_PER_GROUP(fs->super) / 8);
  314. } else {
  315. if (!fs->block_map) {
  316. retval = ext2fs_read_block_bitmap(fs);
  317. if (retval)
  318. return retval;
  319. }
  320. ptr = fs->block_map->bitmap;
  321. size = EXT2_BLOCKS_PER_GROUP(fs->super) / 8;
  322. }
  323. size = size * fs->group_desc_count;
  324. buf = xmalloc(size);
  325. actual = read(fd, buf, size);
  326. if (actual == -1) {
  327. retval = errno;
  328. goto errout;
  329. }
  330. if (actual != size) {
  331. retval = EXT2_ET_SHORT_WRITE;
  332. goto errout;
  333. }
  334. memcpy(ptr, buf, size);
  335. retval = 0;
  336. errout:
  337. if (buf)
  338. free(buf);
  339. return (retval);
  340. }