Advertisement
DeaD_EyE

find holes in sparse files

Jun 6th, 2025
475
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.54 KB | None | 0 0
  1. import os
  2.  
  3.  
  4. def find_holes(file):
  5.     with open(file, "rb") as fd:
  6.         fileno = fd.fileno()
  7.         size = os.stat(fileno).st_size
  8.         pos = 0
  9.         holes = []
  10.  
  11.         while True:
  12.             hole_start = os.lseek(fileno, pos, os.SEEK_HOLE)
  13.             data_start = os.lseek(fileno, pos, os.SEEK_DATA)
  14.  
  15.             if hole_start == size:
  16.                 break
  17.  
  18.             holes.append((hole_start, data_start))
  19.  
  20.             if pos == data_start:
  21.                 break
  22.  
  23.             pos = data_start
  24.  
  25.         return holes
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement