import os
import sys

import fiemap

def main():
    
    if len(sys.argv) < 2:
        print 'ERROR: not enough arguments provided'
        return -1
        
    base_path = sys.argv[1]
        
    # Iterate over all of the "file" directories
    directories = [d for d in os.listdir(base_path) 
                   if os.path.isdir(os.path.join(base_path, d))]
    dir_total = len(directories)
    
    for directory in directories:
        path = os.path.join(base_path, directory)
                
        # Iterate over all of the chunk files
        for file_ in os.listdir(path):
            file_path = os.path.join(path, file_)
            
            # Open a chunk file to perform the fiemap call
            with open(file_path, 'r') as fd:
                map_ = fiemap.get_all_mappings(fd)
                
                # Output the chunk file name and each extent size
                for extent in map_.extents:
                    print '%s, %d' % (file_, extent.length)
                    
                    
if __name__ == '__main__':
    sys.exit(main())
