fileutil_linux.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright (c) 2014 The fileutil 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 !arm
  5. package fileutil
  6. import (
  7. "bytes"
  8. "io"
  9. "io/ioutil"
  10. "os"
  11. "strconv"
  12. "syscall"
  13. )
  14. const hasPunchHole = true
  15. func n(s []byte) byte {
  16. for i, c := range s {
  17. if c < '0' || c > '9' {
  18. s = s[:i]
  19. break
  20. }
  21. }
  22. v, _ := strconv.Atoi(string(s))
  23. return byte(v)
  24. }
  25. func init() {
  26. b, err := ioutil.ReadFile("/proc/sys/kernel/osrelease")
  27. if err != nil {
  28. panic(err)
  29. }
  30. tokens := bytes.Split(b, []byte("."))
  31. if len(tokens) > 3 {
  32. tokens = tokens[:3]
  33. }
  34. switch len(tokens) {
  35. case 3:
  36. // Supported since kernel 2.6.38
  37. if bytes.Compare([]byte{n(tokens[0]), n(tokens[1]), n(tokens[2])}, []byte{2, 6, 38}) < 0 {
  38. puncher = func(*os.File, int64, int64) error { return nil }
  39. }
  40. case 2:
  41. if bytes.Compare([]byte{n(tokens[0]), n(tokens[1])}, []byte{2, 7}) < 0 {
  42. puncher = func(*os.File, int64, int64) error { return nil }
  43. }
  44. default:
  45. puncher = func(*os.File, int64, int64) error { return nil }
  46. }
  47. }
  48. var puncher = func(f *os.File, off, len int64) error {
  49. const (
  50. /*
  51. /usr/include/linux$ grep FL_ falloc.h
  52. */
  53. _FALLOC_FL_KEEP_SIZE = 0x01 // default is extend size
  54. _FALLOC_FL_PUNCH_HOLE = 0x02 // de-allocates range
  55. )
  56. _, _, errno := syscall.Syscall6(
  57. syscall.SYS_FALLOCATE,
  58. uintptr(f.Fd()),
  59. uintptr(_FALLOC_FL_KEEP_SIZE|_FALLOC_FL_PUNCH_HOLE),
  60. uintptr(off),
  61. uintptr(len),
  62. 0, 0)
  63. if errno != 0 {
  64. return os.NewSyscallError("SYS_FALLOCATE", errno)
  65. }
  66. return nil
  67. }
  68. // PunchHole deallocates space inside a file in the byte range starting at
  69. // offset and continuing for len bytes. No-op for kernels < 2.6.38 (or < 2.7).
  70. func PunchHole(f *os.File, off, len int64) error {
  71. return puncher(f, off, len)
  72. }
  73. // Fadvise predeclares an access pattern for file data. See also 'man 2
  74. // posix_fadvise'.
  75. func Fadvise(f *os.File, off, len int64, advice FadviseAdvice) error {
  76. _, _, errno := syscall.Syscall6(
  77. syscall.SYS_FADVISE64,
  78. uintptr(f.Fd()),
  79. uintptr(off),
  80. uintptr(len),
  81. uintptr(advice),
  82. 0, 0)
  83. return os.NewSyscallError("SYS_FADVISE64", errno)
  84. }
  85. // IsEOF reports whether err is an EOF condition.
  86. func IsEOF(err error) bool { return err == io.EOF }