launchpad.net/~rogpeppe/juju-core/500-errgo-fix@v0.0.0-20140213181702-000000002356/utils/file_windows.go (about) 1 // Copyright 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package utils 5 6 import ( 7 "os" 8 "syscall" 9 ) 10 11 const ( 12 movefile_replace_existing = 0x1 13 movefile_write_through = 0x8 14 ) 15 16 //sys moveFileEx(lpExistingFileName *uint16, lpNewFileName *uint16, dwFlags uint32) (err error) = MoveFileExW 17 18 // ReplaceFile atomically replaces the destination file or directory with the source. 19 // The errors that are returned are identical to those returned by os.Rename. 20 func ReplaceFile(source, destination string) error { 21 src, err := syscall.UTF16PtrFromString(source) 22 if err != nil { 23 return &os.LinkError{"replace", source, destination, err} 24 } 25 dest, err := syscall.UTF16PtrFromString(destination) 26 if err != nil { 27 return &os.LinkError{"replace", source, destination, err} 28 } 29 30 // see http://msdn.microsoft.com/en-us/library/windows/desktop/aa365240(v=vs.85).aspx 31 if err := moveFileEx(src, dest, movefile_replace_existing|movefile_write_through); err != nil { 32 return &os.LinkError{"replace", source, destination, err} 33 } 34 return nil 35 }