hole_unix.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //go:build linux
  15. // +build linux
  16. package sparsefile
  17. import (
  18. "io"
  19. "os"
  20. "syscall"
  21. "golang.org/x/sys/unix"
  22. )
  23. const (
  24. SEEK_DATA = 3
  25. SEEK_HOLE = 4
  26. )
  27. func detectHoles(file *os.File) ([]sSparseHole, error) {
  28. holes := []sSparseHole{}
  29. offset := int64(0)
  30. for {
  31. start, err := unix.Seek(int(file.Fd()), offset, SEEK_HOLE)
  32. if err != nil {
  33. if e, ok := err.(syscall.Errno); ok && e == syscall.ENXIO {
  34. break
  35. }
  36. return nil, err
  37. }
  38. end, err := unix.Seek(int(file.Fd()), start, SEEK_DATA)
  39. if err != nil {
  40. if e, ok := err.(syscall.Errno); ok && e == syscall.ENXIO {
  41. end, _ = file.Seek(0, io.SeekEnd)
  42. if end > start {
  43. holes = append(holes, sSparseHole{Offset: start, Length: end - start})
  44. }
  45. break
  46. }
  47. return nil, err
  48. }
  49. offset = end
  50. holes = append(holes, sSparseHole{Offset: start, Length: end - start})
  51. }
  52. return holes, nil
  53. }