file_posix.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // Copyright 2009 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris windows
  5. package os
  6. import (
  7. "syscall"
  8. "time"
  9. )
  10. func sigpipe() // implemented in package runtime
  11. // Readlink returns the destination of the named symbolic link.
  12. // If there is an error, it will be of type *PathError.
  13. func Readlink(name string) (string, error) {
  14. for len := 128; ; len *= 2 {
  15. b := make([]byte, len)
  16. n, e := fixCount(syscall.Readlink(name, b))
  17. if e != nil {
  18. return "", &PathError{"readlink", name, e}
  19. }
  20. if n < len {
  21. return string(b[0:n]), nil
  22. }
  23. }
  24. }
  25. func rename(oldname, newname string) error {
  26. e := syscall.Rename(oldname, newname)
  27. if e != nil {
  28. return &LinkError{"rename", oldname, newname, e}
  29. }
  30. return nil
  31. }
  32. // syscallMode returns the syscall-specific mode bits from Go's portable mode bits.
  33. func syscallMode(i FileMode) (o uint32) {
  34. o |= uint32(i.Perm())
  35. if i&ModeSetuid != 0 {
  36. o |= syscall.S_ISUID
  37. }
  38. if i&ModeSetgid != 0 {
  39. o |= syscall.S_ISGID
  40. }
  41. if i&ModeSticky != 0 {
  42. o |= syscall.S_ISVTX
  43. }
  44. // No mapping for Go's ModeTemporary (plan9 only).
  45. return
  46. }
  47. // Chmod changes the mode of the named file to mode.
  48. // If the file is a symbolic link, it changes the mode of the link's target.
  49. // If there is an error, it will be of type *PathError.
  50. func Chmod(name string, mode FileMode) error {
  51. if e := syscall.Chmod(name, syscallMode(mode)); e != nil {
  52. return &PathError{"chmod", name, e}
  53. }
  54. return nil
  55. }
  56. // Chmod changes the mode of the file to mode.
  57. // If there is an error, it will be of type *PathError.
  58. func (f *File) Chmod(mode FileMode) error {
  59. if f == nil {
  60. return ErrInvalid
  61. }
  62. if e := syscall.Fchmod(f.fd, syscallMode(mode)); e != nil {
  63. return &PathError{"chmod", f.name, e}
  64. }
  65. return nil
  66. }
  67. // Chown changes the numeric uid and gid of the named file.
  68. // If the file is a symbolic link, it changes the uid and gid of the link's target.
  69. // If there is an error, it will be of type *PathError.
  70. func Chown(name string, uid, gid int) error {
  71. if e := syscall.Chown(name, uid, gid); e != nil {
  72. return &PathError{"chown", name, e}
  73. }
  74. return nil
  75. }
  76. // Lchown changes the numeric uid and gid of the named file.
  77. // If the file is a symbolic link, it changes the uid and gid of the link itself.
  78. // If there is an error, it will be of type *PathError.
  79. func Lchown(name string, uid, gid int) error {
  80. if e := syscall.Lchown(name, uid, gid); e != nil {
  81. return &PathError{"lchown", name, e}
  82. }
  83. return nil
  84. }
  85. // Chown changes the numeric uid and gid of the named file.
  86. // If there is an error, it will be of type *PathError.
  87. func (f *File) Chown(uid, gid int) error {
  88. if f == nil {
  89. return ErrInvalid
  90. }
  91. if e := syscall.Fchown(f.fd, uid, gid); e != nil {
  92. return &PathError{"chown", f.name, e}
  93. }
  94. return nil
  95. }
  96. // Truncate changes the size of the file.
  97. // It does not change the I/O offset.
  98. // If there is an error, it will be of type *PathError.
  99. func (f *File) Truncate(size int64) error {
  100. if f == nil {
  101. return ErrInvalid
  102. }
  103. if e := syscall.Ftruncate(f.fd, size); e != nil {
  104. return &PathError{"truncate", f.name, e}
  105. }
  106. return nil
  107. }
  108. // Sync commits the current contents of the file to stable storage.
  109. // Typically, this means flushing the file system's in-memory copy
  110. // of recently written data to disk.
  111. func (f *File) Sync() (err error) {
  112. if f == nil {
  113. return ErrInvalid
  114. }
  115. if e := syscall.Fsync(f.fd); e != nil {
  116. return NewSyscallError("fsync", e)
  117. }
  118. return nil
  119. }
  120. // Chtimes changes the access and modification times of the named
  121. // file, similar to the Unix utime() or utimes() functions.
  122. //
  123. // The underlying filesystem may truncate or round the values to a
  124. // less precise time unit.
  125. // If there is an error, it will be of type *PathError.
  126. func Chtimes(name string, atime time.Time, mtime time.Time) error {
  127. var utimes [2]syscall.Timespec
  128. utimes[0] = syscall.NsecToTimespec(atime.UnixNano())
  129. utimes[1] = syscall.NsecToTimespec(mtime.UnixNano())
  130. if e := syscall.UtimesNano(name, utimes[0:]); e != nil {
  131. return &PathError{"chtimes", name, e}
  132. }
  133. return nil
  134. }