osutil.go 641 B

1234567891011121314151617181920212223
  1. // Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file).
  2. // All rights reserved. Use of this source code is governed by an MIT-style
  3. // license that can be found in the LICENSE file.
  4. // Package osutil implements utilities for native OS support.
  5. package osutil
  6. import (
  7. "os"
  8. "runtime"
  9. )
  10. func Rename(from, to string) error {
  11. if runtime.GOOS == "windows" {
  12. os.Chmod(to, 0666) // Make sure the file is user writeable
  13. err := os.Remove(to)
  14. if err != nil && !os.IsNotExist(err) {
  15. return err
  16. }
  17. }
  18. defer os.Remove(from) // Don't leave a dangling temp file in case of rename error
  19. return os.Rename(from, to)
  20. }