From 4e20111ff66c4c303280d287dc1688d63976ae36 Mon Sep 17 00:00:00 2001 From: Mikkel Krautz Date: Mon, 16 May 2011 23:32:41 +0200 Subject: [PATCH] Windows support for blobstore --- pkg/blobstore/Makefile | 6 ++++++ pkg/blobstore/blobstore.go | 7 +++---- pkg/blobstore/pid_unix.go | 17 +++++++++++++++++ pkg/blobstore/pid_windows.go | 20 ++++++++++++++++++++ 4 files changed, 46 insertions(+), 4 deletions(-) create mode 100644 pkg/blobstore/pid_unix.go create mode 100644 pkg/blobstore/pid_windows.go diff --git a/pkg/blobstore/Makefile b/pkg/blobstore/Makefile index 5bf0f89..db07d2f 100644 --- a/pkg/blobstore/Makefile +++ b/pkg/blobstore/Makefile @@ -5,4 +5,10 @@ GOFILES = \ blobstore.go \ blobreader.go +ifeq ($(GOOS),windows) +GOFILES += pid_windows.go +else +GOFILES += pid_unix.go +endif + include $(GOROOT)/src/Make.pkg diff --git a/pkg/blobstore/blobstore.go b/pkg/blobstore/blobstore.go index 0b914cd..186c15d 100644 --- a/pkg/blobstore/blobstore.go +++ b/pkg/blobstore/blobstore.go @@ -186,10 +186,9 @@ func acquireLockfile(path string) os.Error { return err } - pid, err := strconv.Atoi(string(content)) + pid, err := strconv.Atoui64(string(content)) if err == nil { - errno := syscall.Kill(pid, 0) - if errno == 0 { + if pidRunning(pid) { return ErrLocked } } @@ -220,7 +219,7 @@ func acquireLockfile(path string) os.Error { } else if err != nil { return err } else { - _, err = lockfile.WriteString(strconv.Itoa(syscall.Getpid())) + _, err = lockfile.WriteString(strconv.Uitoa64(getPid())) if err != nil { return err } diff --git a/pkg/blobstore/pid_unix.go b/pkg/blobstore/pid_unix.go new file mode 100644 index 0000000..a4fcc53 --- /dev/null +++ b/pkg/blobstore/pid_unix.go @@ -0,0 +1,17 @@ +// Copyright (c) 2011 The Grumble Authors +// The use of this source code is goverened by a BSD-style +// license that can be found in the LICENSE-file. + +package blobstore + +import ( + "syscall" +) + +func getPid() uint64 { + return uint64(syscall.Getpid()) +} + +func pidRunning(pid uint64) bool { + return syscall.Kill(int(pid), 0) == 0 +} diff --git a/pkg/blobstore/pid_windows.go b/pkg/blobstore/pid_windows.go new file mode 100644 index 0000000..043010f --- /dev/null +++ b/pkg/blobstore/pid_windows.go @@ -0,0 +1,20 @@ +// Copyright (c) 2011 The Grumble Authors +// The use of this source code is goverened by a BSD-style +// license that can be found in the LICENSE-file. + +package blobstore + +import ( + "syscall" +) + +func getPid() uint64 { + handle, _ := syscall.GetCurrentProcess() + return uint64(handle) +} + +func pidRunning(pid uint64) bool { + var status uint32 + syscall.GetExitCodeProcess(uint32(pid), &status) + return status == 259 // STILL_ACTIVE +}