[syslinux] [PATCH 1/4] extlinux: simplification

Nicolas Cornu nicolac76 at yahoo.fr
Fri Nov 13 12:19:50 PST 2015


Merge installation of ldlinux.c32 from ext2_fat_install_file,
btrfs_install_file and xfs_install_file into one function
ext_install_ldlinux_c32

Signed-off-by: Nicolas Cornu <nicolac76 at yahoo.fr>
---
 extlinux/main.c | 106 +++++++++++++++++++++-----------------------------------
 1 file changed, 40 insertions(+), 66 deletions(-)

diff --git a/extlinux/main.c b/extlinux/main.c
index a2a396a..5bab712 100644
--- a/extlinux/main.c
+++ b/extlinux/main.c
@@ -561,19 +561,51 @@ error:
     return -1;
 }
 
+static int ext_install_ldlinux_c32(const char *path)
+{
+    char *file;
+    int fd, rv;
+
+    rv = asprintf(&file, "%s%sldlinux.c32",
+		  path, path[0] && path[strlen(path) - 1] == '/' ? "" : "/");
+    if (rv < 0 || !file) {
+	perror(program);
+	return 1;
+    }
+
+    fd = open(file, O_WRONLY | O_TRUNC | O_CREAT | O_SYNC,
+	      S_IRUSR | S_IRGRP | S_IROTH);
+    if (fd < 0) {
+	perror(file);
+	free(file);
+	return 1;
+    }
+
+    rv = xpwrite(fd, (const char _force *)syslinux_ldlinuxc32,
+		 syslinux_ldlinuxc32_len, 0);
+    if (rv != (int)syslinux_ldlinuxc32_len) {
+	fprintf(stderr, "%s: write failure on %s\n", program, file);
+	rv = 1;
+    } else
+	rv = 0;
+
+    close(fd);
+    free(file);
+
+    return rv;
+}
+
 int ext2_fat_install_file(const char *path, int devfd, struct stat *rst)
 {
-    char *file, *oldfile, *c32file;
+    char *file, *oldfile;
     int fd = -1, dirfd = -1;
-    int r1, r2, r3;
+    int r1, r2;
 
     r1 = asprintf(&file, "%s%sldlinux.sys",
 		  path, path[0] && path[strlen(path) - 1] == '/' ? "" : "/");
     r2 = asprintf(&oldfile, "%s%sextlinux.sys",
 		  path, path[0] && path[strlen(path) - 1] == '/' ? "" : "/");
-    r3 = asprintf(&c32file, "%s%sldlinux.c32",
-		  path, path[0] && path[strlen(path) - 1] == '/' ? "" : "/");
-    if (r1 < 0 || !file || r2 < 0 || !oldfile || r3 < 0 || !c32file) {
+    if (r1 < 0 || !file || r2 < 0 || !oldfile) {
 	perror(program);
 	return 1;
     }
@@ -619,23 +651,11 @@ int ext2_fat_install_file(const char *path, int devfd, struct stat *rst)
 	unlink(oldfile);
     }
 
-    fd = open(c32file, O_WRONLY | O_TRUNC | O_CREAT | O_SYNC,
-	      S_IRUSR | S_IRGRP | S_IROTH);
-    if (fd < 0) {
-	perror(c32file);
-	goto bail;
-    }
-
-    r3 = xpwrite(fd, (const char _force *)syslinux_ldlinuxc32,
-		 syslinux_ldlinuxc32_len, 0);
-    if (r3 != syslinux_ldlinuxc32_len) {
-	fprintf(stderr, "%s: write failure on %s\n", program, c32file);
+    if (ext_install_ldlinux_c32(path))
 	goto bail;
-    }
 
     free(file);
     free(oldfile);
-    free(c32file);
     return 0;
 
 bail:
@@ -646,7 +666,6 @@ bail:
 
     free(file);
     free(oldfile);
-    free(c32file);
     return 1;
 }
 
@@ -655,9 +674,6 @@ bail:
    since the cow feature of btrfs will move the ldlinux.sys every where */
 int btrfs_install_file(const char *path, int devfd, struct stat *rst)
 {
-    char *file;
-    int fd, rv;
-
     patch_file_and_bootblock(-1, path, devfd);
     if (xpwrite(devfd, (const char _force *)boot_image,
 		boot_image_len, BTRFS_EXTLINUX_OFFSET)
@@ -680,32 +696,7 @@ int btrfs_install_file(const char *path, int devfd, struct stat *rst)
      * it doesn't need to be within the first 64K. The Syslinux core
      * has enough smarts to search the btrfs dirs and find this file.
      */
-    rv = asprintf(&file, "%s%sldlinux.c32",
-		  path, path[0] && path[strlen(path) - 1] == '/' ? "" : "/");
-    if (rv < 0 || !file) {
-	perror(program);
-	return 1;
-    }
-
-    fd = open(file, O_WRONLY | O_TRUNC | O_CREAT | O_SYNC,
-	      S_IRUSR | S_IRGRP | S_IROTH);
-    if (fd < 0) {
-	perror(file);
-	free(file);
-	return 1;
-    }
-
-    rv = xpwrite(fd, (const char _force *)syslinux_ldlinuxc32,
-		 syslinux_ldlinuxc32_len, 0);
-    if (rv != (int)syslinux_ldlinuxc32_len) {
-	fprintf(stderr, "%s: write failure on %s\n", program, file);
-	rv = 1;
-    } else
-	rv = 0;
-
-    close(fd);
-    free(file);
-    return rv;
+    return ext_install_ldlinux_c32(path);
 }
 
 /*
@@ -720,15 +711,11 @@ int btrfs_install_file(const char *path, int devfd, struct stat *rst)
 static int xfs_install_file(const char *path, int devfd, struct stat *rst)
 {
     static char file[PATH_MAX + 1];
-    static char c32file[PATH_MAX + 1];
     int dirfd = -1;
     int fd = -1;
-    int retval;
 
     snprintf(file, PATH_MAX + 1, "%s%sldlinux.sys", path,
 	     path[0] && path[strlen(path) - 1] == '/' ? "" : "/");
-    snprintf(c32file, PATH_MAX + 1, "%s%sldlinux.c32", path,
-	     path[0] && path[strlen(path) - 1] == '/' ? "" : "/");
 
     dirfd = open(path, O_RDONLY | O_DIRECTORY);
     if (dirfd < 0) {
@@ -767,21 +754,8 @@ static int xfs_install_file(const char *path, int devfd, struct stat *rst)
     dirfd = -1;
     fd = -1;
 
-    fd = open(c32file, O_WRONLY | O_TRUNC | O_CREAT | O_SYNC,
-	      S_IRUSR | S_IRGRP | S_IROTH);
-    if (fd < 0) {
-	perror(c32file);
-	goto bail;
-    }
-
-    retval = xpwrite(fd, (const char _force *)syslinux_ldlinuxc32,
-		     syslinux_ldlinuxc32_len, 0);
-    if (retval != (int)syslinux_ldlinuxc32_len) {
-	fprintf(stderr, "%s: write failure on %s\n", program, file);
+    if (ext_install_ldlinux_c32(path))
 	goto bail;
-    }
-
-    close(fd);
 
     sync();
 
-- 
2.6.2



More information about the Syslinux mailing list