ext4_utils: allow passing flash block sizes to make_ext4fs_sparse functions

Add versions of make_ext4fs_sparse_fd() and
make_ext4fs_sparse_fd_directory() that accept flash erase & logical
block size as arguments. Refactor to avoid copied code in the new
aligned versions of each function.

Test: Pass block sizes to new functions using fastboot -w and check
that the new userdata has the correct stride and stripe-width values.
Bug: 33243520
Signed-off-by: Connor O'Brien <connoro@google.com>

Change-Id: I16da6e0ce699439c53d1a722c6d56eb3f9cd9bc5
diff --git a/ext4_utils/include/ext4_utils/make_ext4fs.h b/ext4_utils/include/ext4_utils/make_ext4fs.h
index c558b87..44e9481 100644
--- a/ext4_utils/include/ext4_utils/make_ext4fs.h
+++ b/ext4_utils/include/ext4_utils/make_ext4fs.h
@@ -37,6 +37,12 @@
 int make_ext4fs_sparse_fd_directory(int fd, long long len,
                 const char *mountpoint, struct selabel_handle *sehnd,
                 const char *directory);
+int make_ext4fs_sparse_fd_align(int fd, long long len,
+                const char *mountpoint, struct selabel_handle *sehnd,
+                unsigned eraseblk, unsigned logicalblk);
+int make_ext4fs_sparse_fd_directory_align(int fd, long long len,
+                const char *mountpoint, struct selabel_handle *sehnd,
+                const char *directory, unsigned eraseblk, unsigned logicalblk);
 
 #ifdef __cplusplus
 }
diff --git a/ext4_utils/make_ext4fs.c b/ext4_utils/make_ext4fs.c
index ec093f8..b84db9b 100644
--- a/ext4_utils/make_ext4fs.c
+++ b/ext4_utils/make_ext4fs.c
@@ -426,15 +426,32 @@
 int make_ext4fs_sparse_fd(int fd, long long len,
 				const char *mountpoint, struct selabel_handle *sehnd)
 {
-	return make_ext4fs_sparse_fd_directory(fd, len, mountpoint, sehnd, NULL);
+	return make_ext4fs_sparse_fd_align(fd, len, mountpoint, sehnd, 0, 0);
+}
+
+int make_ext4fs_sparse_fd_align(int fd, long long len,
+				const char *mountpoint, struct selabel_handle *sehnd,
+				unsigned eraseblk, unsigned logicalblk)
+{
+	return make_ext4fs_sparse_fd_directory_align(fd, len, mountpoint, sehnd, NULL,
+								eraseblk, logicalblk);
 }
 
 int make_ext4fs_sparse_fd_directory(int fd, long long len,
 				const char *mountpoint, struct selabel_handle *sehnd,
 				const char *directory)
 {
+	return make_ext4fs_sparse_fd_directory_align(fd, len, mountpoint, sehnd, directory, 0, 0);
+}
+
+int make_ext4fs_sparse_fd_directory_align(int fd, long long len,
+				const char *mountpoint, struct selabel_handle *sehnd,
+				const char *directory, unsigned eraseblk, unsigned logicalblk)
+{
 	reset_ext4fs_info();
 	info.len = len;
+	info.flash_erase_block_size = eraseblk;
+	info.flash_logical_block_size = logicalblk;
 
 	return make_ext4fs_internal(fd, directory, NULL, mountpoint, NULL,
 								0, 1, 0, 0, 0,
@@ -447,6 +464,13 @@
 	return make_ext4fs_directory(filename, len, mountpoint, sehnd, NULL);
 }
 
+int make_ext4fs_directory(const char *filename, long long len,
+						  const char *mountpoint, struct selabel_handle *sehnd,
+						  const char *directory)
+{
+	return make_ext4fs_directory_align(filename, len, mountpoint, sehnd, directory, 0, 0);
+}
+
 int make_ext4fs_directory_align(const char *filename, long long len,
 						  const char *mountpoint, struct selabel_handle *sehnd,
 						  const char *directory, unsigned eraseblk,
@@ -474,30 +498,6 @@
 	return status;
 }
 
-int make_ext4fs_directory(const char *filename, long long len,
-						  const char *mountpoint, struct selabel_handle *sehnd,
-						  const char *directory)
-{
-	int fd;
-	int status;
-
-	reset_ext4fs_info();
-	info.len = len;
-
-	fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
-	if (fd < 0) {
-		error_errno("open");
-		return EXIT_FAILURE;
-	}
-
-	status = make_ext4fs_internal(fd, directory, NULL, mountpoint, NULL,
-								  0, 0, 0, 1, 0,
-								  sehnd, 0, -1, NULL, NULL, NULL);
-	close(fd);
-
-	return status;
-}
-
 /* return a newly-malloc'd string that is a copy of str.  The new string
    is guaranteed to have a trailing slash.  If absolute is true, the new string
    is also guaranteed to have a leading slash.