Refactor sparse file support into libsparse

Minimal refactoring of output_file.c into libsparse in preparation
for completely separating libsparse from ext4_utils.

Moves output_file.c, backed_block.c, and parts of ext4_utils.c
into libsparse.  The only changes to the remanining files in
ext4_utils are using the new sparse.h header, and moving the
wipe call out of output_file.c and into make_ext4fs.c.

Change-Id: I1f66f6c3e05230a350023c5b4ea4422f16a73c4b
diff --git a/ext4_utils/ext4_utils.c b/ext4_utils/ext4_utils.c
index 7f226c8..a210281 100644
--- a/ext4_utils/ext4_utils.c
+++ b/ext4_utils/ext4_utils.c
@@ -15,13 +15,13 @@
  */
 
 #include "ext4_utils.h"
-#include "output_file.h"
-#include "backed_block.h"
 #include "uuid.h"
 #include "allocate.h"
 #include "indirect.h"
 #include "extent.h"
 
+#include <sparse/sparse.h>
+
 #include <fcntl.h>
 #include <sys/stat.h>
 #include <sys/types.h>
@@ -80,83 +80,10 @@
 	return 0;
 }
 
-struct count_chunks {
-	u32 chunks;
-	u64 cur_ptr;
-};
-
-void count_data_block(void *priv, u64 off, u8 *data, int len)
-{
-	struct count_chunks *count_chunks = priv;
-	if (off > count_chunks->cur_ptr)
-		count_chunks->chunks++;
-	count_chunks->cur_ptr = off + ALIGN(len, info.block_size);
-	count_chunks->chunks++;
-}
-
-void count_fill_block(void *priv, u64 off, u32 fill_val, int len)
-{
-	struct count_chunks *count_chunks = priv;
-	if (off > count_chunks->cur_ptr)
-		count_chunks->chunks++;
-	count_chunks->cur_ptr = off + ALIGN(len, info.block_size);
-	count_chunks->chunks++;
-}
-
-void count_file_block(void *priv, u64 off, const char *file,
-		off64_t offset, int len)
-{
-	struct count_chunks *count_chunks = priv;
-	if (off > count_chunks->cur_ptr)
-		count_chunks->chunks++;
-	count_chunks->cur_ptr = off + ALIGN(len, info.block_size);
-	count_chunks->chunks++;
-}
-
-int count_sparse_chunks()
-{
-	struct count_chunks count_chunks = {0, 0};
-
-	for_each_data_block(count_data_block, count_file_block, count_fill_block, &count_chunks);
-
-	if (count_chunks.cur_ptr != (u64) info.len)
-		count_chunks.chunks++;
-
-	return count_chunks.chunks;
-}
-
-static void ext4_write_data_block(void *priv, u64 off, u8 *data, int len)
-{
-	write_data_block(priv, off, data, len);
-}
-
-static void ext4_write_fill_block(void *priv, u64 off, u32 fill_val, int len)
-{
-	write_fill_block(priv, off, fill_val, len);
-}
-
-static void ext4_write_data_file(void *priv, u64 off, const char *file,
-		off64_t offset, int len)
-{
-	write_data_file(priv, off, file, offset, len);
-}
-
 /* Write the filesystem image to a file */
-void write_ext4_image(int fd, int gz, int sparse, int crc, int wipe)
+void write_ext4_image(int fd, int gz, int sparse, int crc)
 {
-	int ret = 0;
-
-	struct output_file *out = open_output_fd(fd, gz, sparse,
-	        count_sparse_chunks(), crc, wipe);
-
-	if (!out)
-		return;
-
-	for_each_data_block(ext4_write_data_block, ext4_write_data_file, ext4_write_fill_block, out);
-
-	pad_output_file(out, info.len);
-
-	close_output_file(out);
+	write_sparse_image(fd, gz, sparse, crc, info.block_size, info.len);
 }
 
 /* Compute the rest of the parameters of the filesystem from the basic info */