From f713884b1766396aab4f0fb75a7cc7b8cb8319bb Mon Sep 17 00:00:00 2001 From: Mario Limonciello Date: Tue, 11 Jul 2017 10:35:37 -0500 Subject: [PATCH] create the 'fw' directory on the ESP if it doesn't exist --- linux/libfwup.c | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/linux/libfwup.c b/linux/libfwup.c index 4aa2bf3..d37f644 100644 --- a/linux/libfwup.c +++ b/linux/libfwup.c @@ -1177,6 +1177,8 @@ get_existing_media_path(update_info *info) static int get_fd_and_media_path(update_info *info, char **path) { + struct stat st; + char *directory = NULL; char *fullpath = NULL; int fd = -1; int rc; @@ -1193,13 +1195,28 @@ get_fd_and_media_path(update_info *info, char **path) } } else { /* fall back to creating a new file from scratch */ - rc = asprintf(&fullpath, - "/boot/efi/EFI/%s/fw/fwupdate-XXXXXX.cap", + rc = asprintf(&directory, + "/boot/efi/EFI/%s/fw", FWUP_EFI_DIR_NAME); if (rc < 0) { - efi_error("asprintf failed"); + efi_error("asprintf directory failed"); return fd; } + if (stat(directory, &st) != 0 || + (st.st_mode & S_IFDIR) == 0) { + rc = mkdir(directory, 0775); + if (rc < 0) { + efi_error("failed to make %s", directory); + goto out; + } + } + rc = asprintf(&fullpath, + "%s/fwupdate-XXXXXX.cap", + directory); + if (rc < 0) { + efi_error("asprintf fullpath failed"); + goto out; + } fd = mkostemps(fullpath, 4, O_CREAT|O_TRUNC|O_CLOEXEC); if (fd < 0) { efi_error("mkostemps(%s) failed", fullpath); @@ -1213,6 +1230,7 @@ get_fd_and_media_path(update_info *info, char **path) fullpath = NULL; } out: + free(directory); free(fullpath); return fd; }