sparse.sh 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #!/bin/sh
  2. # Copyright (C) 2012-2018 Free Software Foundation, Inc.
  3. # This program is free software: you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation, either version 3 of the License, or
  6. # (at your option) any later version.
  7. # This program is distributed in the hope that it will be useful,
  8. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. # GNU General Public License for more details.
  11. # You should have received a copy of the GNU General Public License
  12. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  13. . "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
  14. print_ver_ dd
  15. is_local_dir_ . || very_expensive_
  16. require_sparse_support_
  17. # Ensure basic sparse generation works
  18. truncate -s1M sparse
  19. dd bs=32K if=sparse of=sparse.dd conv=sparse
  20. test $(stat -c %s sparse) = $(stat -c %s sparse.dd) || fail=1
  21. # Demonstrate that conv=sparse with oflag=append,
  22. # will do ineffective seeks in the output
  23. printf 'a\000\000b' > file.in
  24. printf 'ab' > exp
  25. dd if=file.in bs=1 conv=sparse oflag=append > out
  26. compare exp out || fail=1
  27. # Demonstrate conv=sparse with conv=notrunc,
  28. # where data in file.out is not overwritten with NULs
  29. printf '____' > out
  30. printf 'a__b' > exp
  31. dd if=file.in bs=1 conv=sparse,notrunc of=out
  32. compare exp out || fail=1
  33. # Ensure we fall back to write if seek fails
  34. dd if=file.in bs=1 conv=sparse | cat > file.out
  35. cmp file.in file.out || fail=1
  36. # Setup for block size tests: create a 3MiB file with a 1MiB
  37. # stretch of NUL bytes in the middle.
  38. rm -f file.in
  39. dd if=/dev/urandom of=file.in bs=1M count=3 iflag=fullblock || fail=1
  40. dd if=/dev/zero of=file.in bs=1M count=1 seek=1 conv=notrunc || fail=1
  41. kb_alloc() { du -k "$1"|cut -f1; }
  42. # sync out data for async allocators like NFS/BTRFS
  43. # sync file.in || fail=1
  44. # If our just-created input file appears to be too small,
  45. # skip the remaining tests. On at least Solaris 10 with NFS,
  46. # file.in is reported to occupy <= 1KiB for about 50 seconds
  47. # after its creation.
  48. if test $(kb_alloc file.in) -gt 3000; then
  49. # Ensure NUL blocks smaller than the block size are not made sparse.
  50. # Here, with a 2MiB block size, dd's conv=sparse must *not* introduce a hole.
  51. dd if=file.in of=file.out bs=2M conv=sparse || fail=1
  52. # Intermittently BTRFS returns 0 allocation for file.out unless synced
  53. sync file.out || framework_failure_
  54. test 2500 -lt $(kb_alloc file.out) || fail=1
  55. # Note we recreate a sparse file first to avoid
  56. # speculative preallocation seen in XFS, where a write() that
  57. # extends a file can preallocate some extra space that
  58. # a subsequent seek will not convert to a hole.
  59. rm -f file.out
  60. truncate --size=3M file.out
  61. # Ensure that this 1MiB string of NULs *is* converted to a hole.
  62. dd if=file.in of=file.out bs=1M conv=sparse,notrunc
  63. test $(kb_alloc file.out) -lt 2500 || fail=1
  64. fi
  65. Exit $fail