Use ReadOrCreateFile in ReadBundleDatabase.

Using the function also removed code that had hardcoded globals for the
location of some files instead of using the dbpath parameter.

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>
This commit is contained in:
Érico Rolim 2021-01-11 00:47:03 -03:00
parent cc55d6e443
commit f94f185652
3 changed files with 27 additions and 17 deletions

View File

@ -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) {

View File

@ -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])

View File

@ -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)
}