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

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


Merge installation of ldlinux.sys from ext_common_install_file and
xfs_install_file into one function ext_install_ldlinux_sys.

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

diff --git a/extlinux/main.c b/extlinux/main.c
index c446c75..43c1a31 100644
--- a/extlinux/main.c
+++ b/extlinux/main.c
@@ -595,32 +595,24 @@ static int ext_install_ldlinux_c32(const char *path)
     return rv;
 }
 
-int ext_common_install_file(const char *path, int devfd, struct stat *rst)
+static int ext_install_ldlinux_sys(int devfd, const char *path, struct stat *rst)
 {
-    char *file, *oldfile;
-    int fd = -1, dirfd = -1;
-    int r1, r2;
+    char *file;
+    int fd, rv;
 
-    r1 = asprintf(&file, "%s%sldlinux.sys",
-		  path, path[0] && path[strlen(path) - 1] == '/' ? "" : "/");
-    r2 = asprintf(&oldfile, "%s%sextlinux.sys",
+    rv = asprintf(&file, "%s%sldlinux.sys",
 		  path, path[0] && path[strlen(path) - 1] == '/' ? "" : "/");
-    if (r1 < 0 || !file || r2 < 0 || !oldfile) {
+    if (rv < 0 || !file) {
 	perror(program);
 	return 1;
     }
 
-    dirfd = open(path, O_RDONLY | O_DIRECTORY);
-    if (dirfd < 0) {
-	perror(path);
-	goto bail;
-    }
-
     fd = open(file, O_RDONLY);
     if (fd < 0) {
 	if (errno != ENOENT) {
 	    perror(file);
-	    goto bail;
+	    free(file);
+	    return 1;
 	}
     } else {
 	clear_attributes(fd);
@@ -628,8 +620,10 @@ int ext_common_install_file(const char *path, int devfd, struct stat *rst)
     close(fd);
 
     fd = rewrite_boot_image(devfd, path, file);
-    if (fd < 0)
-	goto bail;
+    if (fd < 0) {
+	free(file);
+	return 1;
+    }
 
     /* Attempt to set immutable flag and remove all write access */
     /* Only set immutable flag if file is owned by root */
@@ -637,11 +631,39 @@ int ext_common_install_file(const char *path, int devfd, struct stat *rst)
 
     if (fstat(fd, rst)) {
 	perror(file);
+	free(file);
+	close(fd);
+	return 1;
+    }
+
+    close(fd);
+
+    return 0;
+}
+
+int ext_common_install_file(const char *path, int devfd, struct stat *rst)
+{
+    char *oldfile;
+    int fd = -1, dirfd = -1;
+    int r1;
+
+    r1 = asprintf(&oldfile, "%s%sextlinux.sys",
+		  path, path[0] && path[strlen(path) - 1] == '/' ? "" : "/");
+    if (r1 < 0 || !oldfile) {
+	perror(program);
+	return 1;
+    }
+
+    dirfd = open(path, O_RDONLY | O_DIRECTORY);
+    if (dirfd < 0) {
+	perror(path);
 	goto bail;
     }
 
+    if (ext_install_ldlinux_sys(devfd, path, rst))
+	goto bail;
+
     close(dirfd);
-    close(fd);
 
     /* Look if we have the old filename */
     fd = open(oldfile, O_RDONLY);
@@ -654,7 +676,6 @@ int ext_common_install_file(const char *path, int devfd, struct stat *rst)
     if (ext_install_ldlinux_c32(path))
 	goto bail;
 
-    free(file);
     free(oldfile);
     return 0;
 
@@ -664,7 +685,6 @@ bail:
     if (fd >= 0)
 	close(fd);
 
-    free(file);
     free(oldfile);
     return 1;
 }
@@ -710,12 +730,7 @@ 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];
     int dirfd = -1;
-    int fd = -1;
-
-    snprintf(file, PATH_MAX + 1, "%s%sldlinux.sys", path,
-	     path[0] && path[strlen(path) - 1] == '/' ? "" : "/");
 
     dirfd = open(path, O_RDONLY | O_DIRECTORY);
     if (dirfd < 0) {
@@ -723,36 +738,12 @@ static int xfs_install_file(const char *path, int devfd, struct stat *rst)
 	goto bail;
     }
 
-    fd = open(file, O_RDONLY);
-    if (fd < 0) {
-	if (errno != ENOENT) {
-	    perror(file);
-	    goto bail;
-	}
-    } else {
-	clear_attributes(fd);
-    }
-
-    close(fd);
-
-    fd = rewrite_boot_image(devfd, path, file);
-    if (fd < 0)
+    if (ext_install_ldlinux_sys(devfd, path, rst))
 	goto bail;
 
-    /* Attempt to set immutable flag and remove all write access */
-    /* Only set immutable flag if file is owned by root */
-    set_attributes(fd);
-
-    if (fstat(fd, rst)) {
-	perror(file);
-	goto bail;
-    }
-
     close(dirfd);
-    close(fd);
 
     dirfd = -1;
-    fd = -1;
 
     if (ext_install_ldlinux_c32(path))
 	goto bail;
@@ -765,9 +756,6 @@ bail:
     if (dirfd >= 0)
 	close(dirfd);
 
-    if (fd >= 0)
-	close(fd);
-
     return 1;
 }
 
-- 
2.6.2



More information about the Syslinux mailing list