os_unix.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // Copyright (c) 2012, Suryandaru Triandana <syndtr@gmail.com>
  2. // All rights reserved.
  3. //
  4. // Use of this source code is governed by a BSD-style license.
  5. //
  6. // +build darwin dragonfly freebsd linux netbsd openbsd solaris
  7. package main
  8. import (
  9. "os"
  10. "syscall"
  11. )
  12. func rename(oldpath, newpath string) error {
  13. return os.Rename(oldpath, newpath)
  14. }
  15. func isErrInvalid(err error) bool {
  16. if err == os.ErrInvalid {
  17. return true
  18. }
  19. // Go < 1.8
  20. if syserr, ok := err.(*os.SyscallError); ok && syserr.Err == syscall.EINVAL {
  21. return true
  22. }
  23. // Go >= 1.8 returns *os.PathError instead
  24. if patherr, ok := err.(*os.PathError); ok && patherr.Err == syscall.EINVAL {
  25. return true
  26. }
  27. return false
  28. }
  29. func syncDir(name string) error {
  30. // As per fsync manpage, Linux seems to expect fsync on directory, however
  31. // some system don't support this, so we will ignore syscall.EINVAL.
  32. //
  33. // From fsync(2):
  34. // Calling fsync() does not necessarily ensure that the entry in the
  35. // directory containing the file has also reached disk. For that an
  36. // explicit fsync() on a file descriptor for the directory is also needed.
  37. f, err := os.Open(name)
  38. if err != nil {
  39. return err
  40. }
  41. defer f.Close()
  42. if err := f.Sync(); err != nil && !isErrInvalid(err) {
  43. return err
  44. }
  45. return nil
  46. }