imager.c 7.1 KB

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