#!/usr/bin/python # Eric's first python program :) import sys #for sys.argv import fcntl #for fcntl.ioctl import os #for os.access import struct # for struct.pack and struct.unpack print "WARNING: This script may not work for files with holes." if len(sys.argv) != 2: print "Usage: " + sys.argv[0] + " " print "Note: This program uses the FIBMAP ioctl, so you must be root." sys.exit(1) if os.access(sys.argv[1], os.F_OK) == False: print "File " + sys.argv[1] + " doesn't exist." sys.exit(1) f = open(sys.argv[1],'r') fsBlockSize = struct.unpack('i',fcntl.ioctl(f,2,' '))[0] numFileBlocks = os.stat(sys.argv[1])[6] / fsBlockSize if (os.stat(sys.argv[1])[6] % fsBlockSize) != 0: numFileBlocks += 1 blockIterator = 0 blockmap = [] for blockIterator in range(numFileBlocks): h=struct.pack('i',blockIterator) blockmap += struct.unpack('i',fcntl.ioctl(f,1,h)) print "Filesystem block size: " + str(fsBlockSize) print "Number of filesystem blocks in file: " + str(numFileBlocks) extentBegin = [] extentEnd = [] extentBegin += [blockmap[0]] for blockIterator in range(1,len(blockmap)): if blockmap[blockIterator]-blockmap[blockIterator-1] == 1: blockIterator += 1 continue else: extentEnd += [blockmap[blockIterator-1]] extentBegin += [blockmap[blockIterator]] blockIterator += 1 extentEnd += [blockmap[blockIterator-1]] for n in range(0,len(extentBegin)): print "Contiguous chunk " + str(n) + ": " + str(extentBegin[n]) + " - " + str(extentEnd[n]) + " (" + str(extentEnd[n] - extentBegin[n]+1) + " blocks)" sys.exit(0)