Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add windows support #5

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions file_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// +build !windows

package write

import (
"os"
)

// replaceFile atomically replaces the destination file or directory with the
// source. It is guaranteed to either replace the target file entirely, or not
// change either file.
func replaceFile(source, destination string) error {
return os.Rename(source, destination)
}
33 changes: 33 additions & 0 deletions file_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package write

import (
"os"
"syscall"
)

const (
movefile_replace_existing = 0x1
movefile_write_through = 0x8
)

//sys moveFileEx(lpExistingFileName *uint16, lpNewFileName *uint16, dwFlags uint32) (err error) = MoveFileExW

// replaceFile atomically replaces the destination file or directory with the
// source. It is guaranteed to either replace the target file entirely, or not
// change either file.
func replaceFile(source, destination string) error {
src, err := syscall.UTF16PtrFromString(source)
if err != nil {
return &os.LinkError{"replace", source, destination, err}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here and below: could you please change the literal construction to use field names to be robust when os.LinkError changes? E.g. &os.LinkError{Op: "replace", Old: source, New: destination, Err: err}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh yeah, definitely.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me know once you pushed the change :)

}
dest, err := syscall.UTF16PtrFromString(destination)
if err != nil {
return &os.LinkError{"replace", source, destination, err}
}

// see http://msdn.microsoft.com/en-us/library/windows/desktop/aa365240(v=vs.85).aspx
if err := moveFileEx(src, dest, movefile_replace_existing|movefile_write_through); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, golang.org/x/sys/windows has a wrapper for MoveFileExW, so it could be used instead of generating one here.

return &os.LinkError{"replace", source, destination, err}
}
return nil
}
6 changes: 3 additions & 3 deletions tempfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func tempDir(dir, dest string) string {
defer os.Remove(testdest.Name())
testdest.Close()

if err := os.Rename(testsrc.Name(), testdest.Name()); err != nil {
if err := replaceFile(testsrc.Name(), testdest.Name()); err != nil {
return fallback
}
cleanup = false // testsrc no longer exists
Expand Down Expand Up @@ -119,7 +119,7 @@ func (t *PendingFile) CloseAtomicallyReplace() error {
if err := t.Close(); err != nil {
return err
}
if err := os.Rename(t.Name(), t.path); err != nil {
if err := replaceFile(t.Name(), t.path); err != nil {
return err
}
t.done = true
Expand Down Expand Up @@ -164,7 +164,7 @@ func Symlink(oldname, newname string) error {
return err
}

if err := os.Rename(symlink, newname); err != nil {
if err := replaceFile(symlink, newname); err != nil {
return err
}

Expand Down
27 changes: 27 additions & 0 deletions zfile_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// mksyscall_windows -l32 file_windows.go
// MACHINE GENERATED BY THE COMMAND ABOVE; DO NOT EDIT
stapelberg marked this conversation as resolved.
Show resolved Hide resolved

package write

import (
"syscall"
"unsafe"
)

var (
modkernel32 = syscall.NewLazyDLL("kernel32.dll")

procMoveFileExW = modkernel32.NewProc("MoveFileExW")
)

func moveFileEx(lpExistingFileName *uint16, lpNewFileName *uint16, dwFlags uint32) (err error) {
r1, _, e1 := syscall.Syscall(procMoveFileExW.Addr(), 3, uintptr(unsafe.Pointer(lpExistingFileName)), uintptr(unsafe.Pointer(lpNewFileName)), uintptr(dwFlags))
if r1 == 0 {
if e1 != 0 {
err = error(e1)
} else {
err = syscall.EINVAL
}
}
return
}