<commit_before>#!/usr/bin/python

import btrfs
import os
import sys


def devices(fd):
    devices = btrfs.search(fd,
                           tree=btrfs.CHUNK_TREE_OBJECTID,
                           objid=btrfs.DEV_ITEMS_OBJECTID,
                           key_type=btrfs.DEV_ITEM_KEY,
                           structure=btrfs.dev_item)
    for header, buf, device in devices:
        print("dev item devid %s total bytes %s bytes used %s" % (device[0], device[1], device[2]))


def chunks(fd):
    offset = (0, btrfs.MINUS_ONE)
    while True:
        print("; searching chunks, offset %s" % offset[0])
        chunks = btrfs.search(fd,
                              tree=btrfs.CHUNK_TREE_OBJECTID,
                              objid=btrfs.FIRST_CHUNK_TREE_OBJECTID,
                              key_type=btrfs.CHUNK_ITEM_KEY,
                              offset=offset,
                              structure=btrfs.chunk)
        for header, buf, chunk in chunks:
            num_stripes = chunk[7]
            pos = btrfs.chunk.size
            vaddr = header[2]
            offset = (vaddr + 1, btrfs.MINUS_ONE)
            used = block_group_used_for_chunk(fd, vaddr)
            for i in xrange(num_stripes):
                stripe = btrfs.stripe.unpack_from(buf, pos)
                pos += btrfs.stripe.size
                print("chunk vaddr %s type %s stripe %s devid %s offset %s length %s used %s" %
                      (vaddr, chunk[3], i, stripe[0], stripe[1], chunk[0], used))

        if len(chunks) == 0:
            break


def block_group_used_for_chunk(fd, vaddr):
    print("; searching extent tree for block group %s" % vaddr)
    block_groups = btrfs.search(fd,
                                tree=btrfs.EXTENT_TREE_OBJECTID,
                                objid=vaddr,
                                key_type=btrfs.BLOCK_GROUP_ITEM_KEY,
                                structure=btrfs.block_group_item)
    if len(block_groups) == 0:
        return 0
    return block_groups[0][2][0]


def main():
    fd = os.open(sys.argv[1], os.O_RDONLY)
    devices(fd)
    chunks(fd)
    os.close(fd)


if __name__ == '__main__':<commit_msg>poc: make search for the block group more specific<commit_after>#!/usr/bin/python

import btrfs
import os
import sys


def devices(fd):
    devices = btrfs.search(fd,
                           tree=btrfs.CHUNK_TREE_OBJECTID,
                           objid=btrfs.DEV_ITEMS_OBJECTID,
                           key_type=btrfs.DEV_ITEM_KEY,
                           structure=btrfs.dev_item)
    for header, buf, device in devices:
        print("dev item devid %s total bytes %s bytes used %s" % (device[0], device[1], device[2]))


def chunks(fd):
    offset = (0, btrfs.MINUS_ONE)
    while True:
        print("; searching chunks, offset %s" % offset[0])
        chunks = btrfs.search(fd,
                              tree=btrfs.CHUNK_TREE_OBJECTID,
                              objid=btrfs.FIRST_CHUNK_TREE_OBJECTID,
                              key_type=btrfs.CHUNK_ITEM_KEY,
                              offset=offset,
                              structure=btrfs.chunk)
        for header, buf, chunk in chunks:
            num_stripes = chunk[7]
            pos = btrfs.chunk.size
            vaddr = header[2]
            length = chunk[0]
            offset = (vaddr + 1, btrfs.MINUS_ONE)
            used = block_group_used_for_chunk(fd, vaddr, length)
            for i in xrange(num_stripes):
                stripe = btrfs.stripe.unpack_from(buf, pos)
                pos += btrfs.stripe.size
                print("chunk vaddr %s type %s stripe %s devid %s offset %s length %s used %s" %
                      (vaddr, chunk[3], i, stripe[0], stripe[1], length, used))

        if len(chunks) == 0:
            break


def block_group_used_for_chunk(fd, vaddr, length):
    print("; searching extent tree for block group (%s BLOCK_GROUP_ITEM %s)" % (vaddr, length))
    block_groups = btrfs.search(fd,
                                tree=btrfs.EXTENT_TREE_OBJECTID,
                                objid=vaddr,
                                key_type=btrfs.BLOCK_GROUP_ITEM_KEY,
                                offset=length,
                                structure=btrfs.block_group_item)
    if len(block_groups) == 0:
        return 0
    return block_groups[0][2][0]


def main():
    fd = os.open(sys.argv[1], os.O_RDONLY)
    devices(fd)
    chunks(fd)
    os.close(fd)


if __name__ == '__main__':
{'fraction_non_alphanumeric': 0.09777777777777778, 'fraction_numerical': 0.008444444444444444, 'mean_word_length': 1.5838117106773824, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '25092', 'n_tokens_mistral': 1402, 'n_tokens_neox': 1413, 'n_words': 303}