diff mbox series

[3/4] Factorize copy handler

Message ID 20221011111706.455706-3-sbabic@denx.de
State Accepted
Headers show
Series [1/4] raw_handler: drop duplicate #include | expand

Commit Message

Stefano Babic Oct. 11, 2022, 11:17 a.m. UTC
The "rawcopy" handler is just a use case of the copy handler with the
chain handler set to "raw". Drop it.

The change is backward compatible because a "rawcopy" handler is linked
to the new one, and "raw" is the default chained handler.

Signed-off-by: Stefano Babic <sbabic@denx.de>
---
 handlers/copy_handler.c |  8 ++++
 handlers/raw_handler.c  | 97 -----------------------------------------
 2 files changed, 8 insertions(+), 97 deletions(-)
diff mbox series

Patch

diff --git a/handlers/copy_handler.c b/handlers/copy_handler.c
index 6d1c577..27f6caf 100644
--- a/handlers/copy_handler.c
+++ b/handlers/copy_handler.c
@@ -34,6 +34,7 @@ 
 #define PIPE_WRITE 1
 
 static void copy_handler(void);
+static void raw_copyimage_handler(void);
 
 static int copy_image_file(struct img_type *img, void *data)
 {
@@ -202,3 +203,10 @@  void copy_handler(void)
 	register_handler("copy", copy_image_file,
 				SCRIPT_HANDLER | NO_DATA_HANDLER, NULL);
 }
+
+__attribute__((constructor))
+void raw_copyimage_handler(void)
+{
+	register_handler("rawcopy", copy_image_file,
+				SCRIPT_HANDLER | NO_DATA_HANDLER, NULL);
+}
diff --git a/handlers/raw_handler.c b/handlers/raw_handler.c
index 9266da7..cdd81d3 100644
--- a/handlers/raw_handler.c
+++ b/handlers/raw_handler.c
@@ -30,7 +30,6 @@ 
 
 void raw_image_handler(void);
 void raw_file_handler(void);
-void raw_copyimage_handler(void);
 
 /**
  * Handle write protection for block devices
@@ -148,95 +147,6 @@  static int install_raw_image(struct img_type *img,
 	return ret;
 }
 
-static int copy_raw_image(struct img_type *img, void *data)
-{
-	int ret;
-	int fdout, fdin;
-	struct dict_list *proplist;
-	struct dict_list_elem *entry;
-	uint32_t checksum;
-	unsigned long offset = 0;
-	size_t size;
-	struct stat statbuf;
-	struct script_handler_data *script_data;
-
-	if (!data)
-		return -1;
-
-	script_data = data;
-
-	proplist = dict_get_list(&img->properties, "type");
-	if (proplist) {
-		entry = LIST_FIRST(proplist);
-		/* check if this should just run as pre or post install */
-		if (entry) {
-			if (strcmp(entry->value, "preinstall") && strcmp(entry->value, "postinstall")) {
-				ERROR("Type can be just preinstall or postinstall");
-				return -EINVAL;
-			}
-			script_fn type = !strcmp(entry->value, "preinstall") ? PREINSTALL : POSTINSTALL;
-			if (type != script_data->scriptfn) {
-				TRACE("Script set to %s, skipping", entry->value);
-				return 0;
-			}
-		}
-	}
-
-	proplist = dict_get_list(&img->properties, "copyfrom");
-
-	if (!proplist || !(entry = LIST_FIRST(proplist))) {
-		ERROR("Missing source device, no copyfrom property");
-		return -EINVAL;
-	}
-	fdin = open(entry->value, O_RDONLY);
-	if (fdin < 0) {
-		ERROR("Device %s cannot be opened: %s",
-			entry->value, strerror(errno));
-		return -ENODEV;
-	}
-
-	ret = fstat(fdin, &statbuf);
-	if (ret < 0) {
-		ERROR("Cannot be retrieved information on %s", entry->value);
-		close(fdin);
-		return -ENODEV;
-	}
-
-	if ((statbuf.st_mode & S_IFMT) == S_IFREG)
-		size = statbuf.st_size;
-	else if (ioctl(fdin, BLKGETSIZE64, &size) < 0) {
-		ERROR("Cannot get size of %s", entry->value);
-		close(fdin);
-		return -ENODEV;
-	}
-
-	fdout = open(img->device, O_RDWR);
-	if (fdout < 0) {
-		TRACE("Device %s cannot be opened: %s",
-			img->device, strerror(errno));
-		close(fdin);
-		return -ENODEV;
-	}
-
-	TRACE("Copying %s to %s", entry->value, img->device);
-	ret = copyfile(fdin,
-			&fdout,
-			size,
-			&offset,
-			0,
-			0, /* no skip */
-			0, /* no compressed */
-			&checksum,
-			0, /* no sha256 */
-			false, /* no encrypted */
-			NULL, /* no IVT */
-			NULL);
-
-	close(fdin);
-	close(fdout);
-	return ret;
-}
-
 static int install_raw_file(struct img_type *img,
 	void __attribute__ ((__unused__)) *data)
 {
@@ -340,10 +250,3 @@  void raw_file_handler(void)
 	register_handler("rawfile", install_raw_file,
 				FILE_HANDLER, NULL);
 }
-
-__attribute__((constructor))
-void raw_copyimage_handler(void)
-{
-	register_handler("rawcopy", copy_raw_image,
-				SCRIPT_HANDLER | NO_DATA_HANDLER, NULL);
-}