aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/osutil/osutil_unix.go
diff options
context:
space:
mode:
authorDmitry Vyukov <dvyukov@google.com>2020-06-04 21:47:34 +0200
committerDmitry Vyukov <dvyukov@google.com>2020-06-05 12:23:19 +0200
commit6c22a3d2ddd599dab260b2d36d91b0292254893c (patch)
tree78ff13181c320dd71f3e76b1e745f0f9f7539d82 /pkg/osutil/osutil_unix.go
parent503fd5b2f7dbac79970770f0fc6754163dce1588 (diff)
.golangci.yml: enable nestif checker
Prevents functions with too many nested if's.
Diffstat (limited to 'pkg/osutil/osutil_unix.go')
-rw-r--r--pkg/osutil/osutil_unix.go32
1 files changed, 18 insertions, 14 deletions
diff --git a/pkg/osutil/osutil_unix.go b/pkg/osutil/osutil_unix.go
index 8f31a43e0..914398c79 100644
--- a/pkg/osutil/osutil_unix.go
+++ b/pkg/osutil/osutil_unix.go
@@ -36,21 +36,9 @@ func ProcessTempDir(where string) (string, error) {
err := os.Mkdir(path, DefaultDirPerm)
if os.IsExist(err) {
// Try to clean up.
- data, err := ioutil.ReadFile(pidfile)
- if err == nil && len(data) > 0 {
- pid, err := strconv.Atoi(string(data))
- if err == nil && pid > 1 {
- if err := syscall.Kill(pid, 0); err == syscall.ESRCH {
- if os.Remove(pidfile) == nil {
- if os.RemoveAll(path) == nil {
- i--
- continue
- }
- }
- }
- }
+ if cleanupTempDir(path, pidfile) {
+ i--
}
- // If err != nil, assume that the pid file is not created yet.
continue
}
if err != nil {
@@ -64,6 +52,22 @@ func ProcessTempDir(where string) (string, error) {
return "", fmt.Errorf("too many live instances")
}
+func cleanupTempDir(path, pidfile string) bool {
+ data, err := ioutil.ReadFile(pidfile)
+ if err == nil && len(data) > 0 {
+ pid, err := strconv.Atoi(string(data))
+ if err == nil && pid > 1 {
+ if err := syscall.Kill(pid, 0); err == syscall.ESRCH {
+ if os.Remove(pidfile) == nil {
+ return os.RemoveAll(path) == nil
+ }
+ }
+ }
+ }
+ // If err != nil, assume that the pid file is not created yet.
+ return false
+}
+
// HandleInterrupts closes shutdown chan on first SIGINT
// (expecting that the program will gracefully shutdown and exit)
// and terminates the process on third SIGINT.