From 4ef8f5b630540fcb4eedf8c8d382560bfc34f382 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89rico=20Rolim?= Date: Mon, 11 Jan 2021 00:47:03 -0300 Subject: [PATCH] Use ReadOrCreateFile in ReadBundleDatabase. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add error checking around the function where appropriate. Also fail early when creating a new bundle if it isn't possible to access the bundle database. Signed-off-by: Érico Rolim --- bundles.go | 18 +++++------------- cmd/sbctl/main.go | 16 ++++++++++++++-- sbctl.go | 10 ++++++++-- 3 files changed, 27 insertions(+), 17 deletions(-) diff --git a/bundles.go b/bundles.go index 48eeacf..75774f4 100644 --- a/bundles.go +++ b/bundles.go @@ -28,22 +28,14 @@ type Bundles map[string]*Bundle var BundleDBPath = filepath.Join(DatabasePath, "bundles.db") -func ReadBundleDatabase(dbpath string) Bundles { - bundles := make(Bundles) - os.MkdirAll(DatabasePath, os.ModePerm) - if _, err := os.Stat(BundleDBPath); os.IsNotExist(err) { - file, err := os.Create(BundleDBPath) - if err != nil { - log.Fatal(err) - } - file.Close() - } - f, err := ioutil.ReadFile(dbpath) +func ReadBundleDatabase(dbpath string) (Bundles, error) { + f, err := ReadOrCreateFile(dbpath) if err != nil { - log.Fatal(err) + return nil, err } + bundles := make(Bundles) json.Unmarshal(f, &bundles) - return bundles + return bundles, nil } func WriteBundleDatabase(dbpath string, bundles Bundles) { diff --git a/cmd/sbctl/main.go b/cmd/sbctl/main.go index a916a5f..5a2a044 100755 --- a/cmd/sbctl/main.go +++ b/cmd/sbctl/main.go @@ -200,6 +200,16 @@ func bundleCmd() *cobra.Command { if err != nil { log.Fatal(err) } + // Fail early if user wants to save bundle but doesn't have permissions + var bundles sbctl.Bundles + if save { + // "err" needs to have been declared before this, otherwise it's necessary + // to use ":=", which shadows the "bundles" variable + bundles, err = sbctl.ReadBundleDatabase(sbctl.BundleDBPath) + if err != nil { + log.Fatalln(err) + } + } bundle.Output = output bundle.IntelMicrocode = intelucode bundle.AMDMicrocode = amducode @@ -212,7 +222,6 @@ func bundleCmd() *cobra.Command { bundle.ESP = espPath sbctl.CreateBundle(*bundle) if save { - bundles := sbctl.ReadBundleDatabase(sbctl.BundleDBPath) bundles[bundle.Output] = bundle sbctl.WriteBundleDatabase(sbctl.BundleDBPath, bundles) sbctl.FormatBundle(bundle.Output, bundle) @@ -266,7 +275,10 @@ func removeBundleCmd() *cobra.Command { if len(args) < 1 { log.Fatal("Need to specify file") } - bundles := sbctl.ReadBundleDatabase(sbctl.BundleDBPath) + bundles, err := sbctl.ReadBundleDatabase(sbctl.BundleDBPath) + if err != nil { + log.Fatalln(err) + } if _, ok := bundles[args[0]]; !ok { log.Printf("Bundle %s doesn't exist in database!\n", args[0]) diff --git a/sbctl.go b/sbctl.go index a7eecb1..3f102a3 100644 --- a/sbctl.go +++ b/sbctl.go @@ -260,7 +260,10 @@ func CreateBundle(bundle Bundle) error { func GenerateAllBundles(sign bool) error { msg.Println("Generating EFI bundles....") - bundles := ReadBundleDatabase(BundleDBPath) + bundles, err := ReadBundleDatabase(BundleDBPath) + if err != nil { + return err + } out_create := true out_sign := true for _, bundle := range bundles { @@ -291,7 +294,10 @@ func GenerateAllBundles(sign bool) error { } func ListBundles() { - bundles := ReadBundleDatabase(BundleDBPath) + bundles, err := ReadBundleDatabase(BundleDBPath) + if err != nil { + log.Fatalln(err) + } for key, bundle := range bundles { FormatBundle(key, bundle) }