misc.Exists: bubble up any unexpected errors
Fixes https://gitlab.com/postmarketOS/postmarketos-mkinitfs/-/issues/6
This commit is contained in:
@@ -48,9 +48,12 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
deviceinfoFile := "/etc/deviceinfo"
|
deviceinfoFile := "/etc/deviceinfo"
|
||||||
if !misc.Exists(deviceinfoFile) {
|
if exists, err := misc.Exists(deviceinfoFile); !exists {
|
||||||
log.Printf("NOTE: %q not found, this file is required by mkinitfs.\n", deviceinfoFile)
|
log.Printf("NOTE: %q not found, this file is required by mkinitfs.\n", deviceinfoFile)
|
||||||
return
|
return
|
||||||
|
} else if err != nil {
|
||||||
|
retCode = 1
|
||||||
|
log.Printf("received unexpected error when getting status for %q: %s", deviceinfoFile, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
devinfo, err := deviceinfo.ReadDeviceinfo(deviceinfoFile)
|
devinfo, err := deviceinfo.ReadDeviceinfo(deviceinfoFile)
|
||||||
|
@@ -40,10 +40,12 @@ func (m *Modules) List() (*filelist.FileList, error) {
|
|||||||
files := filelist.NewFileList()
|
files := filelist.NewFileList()
|
||||||
|
|
||||||
modDir := filepath.Join("/lib/modules", kernVer)
|
modDir := filepath.Join("/lib/modules", kernVer)
|
||||||
if !misc.Exists(modDir) {
|
if exists, err := misc.Exists(modDir); !exists {
|
||||||
// dir /lib/modules/<kernel> if kernel built without module support, so just print a message
|
// dir /lib/modules/<kernel> if kernel built without module support, so just print a message
|
||||||
log.Printf("-- kernel module directory not found: %q, not including modules", modDir)
|
log.Printf("-- kernel module directory not found: %q, not including modules", modDir)
|
||||||
return files, nil
|
return files, nil
|
||||||
|
} else if err != nil {
|
||||||
|
return nil, fmt.Errorf("received unexpected error when getting status for %q: %w", modDir, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// modules.* required by modprobe
|
// modules.* required by modprobe
|
||||||
@@ -148,8 +150,10 @@ func getModulesInDir(modPath string) (files []string, err error) {
|
|||||||
func getModule(modName string, modDir string) (files []string, err error) {
|
func getModule(modName string, modDir string) (files []string, err error) {
|
||||||
|
|
||||||
modDep := filepath.Join(modDir, "modules.dep")
|
modDep := filepath.Join(modDir, "modules.dep")
|
||||||
if !misc.Exists(modDep) {
|
if exists, err := misc.Exists(modDep); !exists {
|
||||||
return nil, fmt.Errorf("kernel module.dep not found: %s", modDir)
|
return nil, fmt.Errorf("kernel module.dep not found: %s", modDir)
|
||||||
|
} else if err != nil {
|
||||||
|
return nil, fmt.Errorf("received unexpected error when getting module.dep status: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
fd, err := os.Open(modDep)
|
fd, err := os.Open(modDep)
|
||||||
@@ -165,9 +169,12 @@ func getModule(modName string, modDir string) (files []string, err error) {
|
|||||||
|
|
||||||
for _, dep := range deps {
|
for _, dep := range deps {
|
||||||
p := filepath.Join(modDir, dep)
|
p := filepath.Join(modDir, dep)
|
||||||
if !misc.Exists(p) {
|
if exists, err := misc.Exists(p); !exists {
|
||||||
return nil, fmt.Errorf("tried to include a module that doesn't exist in the modules directory (%s): %s", modDir, p)
|
return nil, fmt.Errorf("tried to include a module that doesn't exist in the modules directory (%s): %s", modDir, p)
|
||||||
|
} else if err != nil {
|
||||||
|
return nil, fmt.Errorf("received unexpected error when getting status for %q: %w", p, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
files = append(files, p)
|
files = append(files, p)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -28,8 +28,11 @@ func New(mesaDriverName string) *OskSdl {
|
|||||||
// disk (d)encryption
|
// disk (d)encryption
|
||||||
func (s *OskSdl) List() (*filelist.FileList, error) {
|
func (s *OskSdl) List() (*filelist.FileList, error) {
|
||||||
files := filelist.NewFileList()
|
files := filelist.NewFileList()
|
||||||
if !misc.Exists("/usr/bin/osk-sdl") {
|
|
||||||
|
if exists, err := misc.Exists("/usr/bin/osk-sdl"); !exists {
|
||||||
return files, nil
|
return files, nil
|
||||||
|
} else if err != nil {
|
||||||
|
return files, fmt.Errorf("received unexpected error when getting status for %q: %w", "/usr/bin/osk-sdl", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Println("- Including osk-sdl support")
|
log.Println("- Including osk-sdl support")
|
||||||
@@ -145,8 +148,10 @@ func getOskConfFontPath(oskConfPath string) (string, error) {
|
|||||||
path = fields[2]
|
path = fields[2]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !misc.Exists(path) {
|
if exists, err := misc.Exists(path); !exists {
|
||||||
return path, fmt.Errorf("unable to find font: %s", path)
|
return path, fmt.Errorf("unable to find font: %s", path)
|
||||||
|
} else if err != nil {
|
||||||
|
return path, fmt.Errorf("received unexpected error when getting status for %q: %w", path, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return path, nil
|
return path, nil
|
||||||
|
@@ -4,6 +4,7 @@
|
|||||||
package misc
|
package misc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
@@ -47,10 +48,18 @@ func TimeFunc(start time.Time, name string) {
|
|||||||
log.Printf("%s completed in: %s", name, elapsed)
|
log.Printf("%s completed in: %s", name, elapsed)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Exists tests if the given file/dir exists or not
|
// Exists tests if the given file/dir exists or not. Returns any errors related
|
||||||
func Exists(file string) bool {
|
// to os.Stat if the type is *not* ErrNotExist. If an error is returned, then
|
||||||
if _, err := os.Stat(file); err == nil {
|
// the value of the returned boolean cannot be trusted.
|
||||||
return true
|
func Exists(file string) (bool, error) {
|
||||||
|
_, err := os.Stat(file)
|
||||||
|
if err == nil {
|
||||||
|
return true, nil
|
||||||
|
} else if errors.Is(err, os.ErrNotExist) {
|
||||||
|
// Don't return the error, the file doesn't exist which is OK
|
||||||
|
return false, nil
|
||||||
}
|
}
|
||||||
return false
|
|
||||||
|
// Other errors from os.Stat returned here
|
||||||
|
return false, err
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user