Skip to content

Commit

Permalink
Use ReadOrCreateFile in ReadBundleDatabase.
Browse files Browse the repository at this point in the history
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 <erico.erc@gmail.com>
  • Loading branch information
ericonr committed Jan 11, 2021
1 parent ecad902 commit 4ef8f5b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 17 deletions.
18 changes: 5 additions & 13 deletions bundles.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
16 changes: 14 additions & 2 deletions cmd/sbctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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])
Expand Down
10 changes: 8 additions & 2 deletions sbctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit 4ef8f5b

Please sign in to comment.