瀏覽代碼

pkg/pidfile: don't ignore all errors when reading file

It's ok to ignore if the file doesn't exist, or if the file doesn't
have a PID in it, but we should produce an error if the file exists,
but we're unable to read it for other reasons.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Sebastiaan van Stijn 2 年之前
父節點
當前提交
4917bcc039
共有 1 個文件被更改,包括 11 次插入6 次删除
  1. 11 6
      pkg/pidfile/pidfile.go

+ 11 - 6
pkg/pidfile/pidfile.go

@@ -19,12 +19,17 @@ type PIDFile struct {
 }
 
 func checkPIDFileAlreadyExists(path string) error {
-	if pidByte, err := os.ReadFile(path); err == nil {
-		pidString := strings.TrimSpace(string(pidByte))
-		if pid, err := strconv.Atoi(pidString); err == nil {
-			if processExists(pid) {
-				return fmt.Errorf("pid file found, ensure docker is not running or delete %s", path)
-			}
+	pidByte, err := os.ReadFile(path)
+	if err != nil {
+		if os.IsNotExist(err) {
+			return nil
+		}
+		return err
+	}
+	pidString := strings.TrimSpace(string(pidByte))
+	if pid, err := strconv.Atoi(pidString); err == nil {
+		if processExists(pid) {
+			return fmt.Errorf("pid file found, ensure docker is not running or delete %s", path)
 		}
 	}
 	return nil