<commit_before>#!/usr/bin/env python

import xapi
import commands
from common import log, run

# Use Linux "losetup" to create block devices from files

class Loop:
    """An active loop device"""
    def __init__(self, path, loop):
        self.path = path
        self.loop = loop
    def destroy(self, dbg):
        run(dbg, "losetup -d %s" % self.loop)
    def block_device(self):
        return self.loop

def find(dbg, path):
    """Return the active loop device associated with the given path"""
    for line in run(dbg, "losetup -a").split("\n"):
        line = line.strip()
        if line <> "":
            bits = line.split()
            loop = bits[0][0:-1]
            this_path = bits[2][1:-1]
            if this_path == path:
                return Loop(path, loop)
    return None

def create(dbg, path):
    """Creates a new loop device backed by the given file"""
    run(dbg, "losetup -f \"%s\"" % path)
    return find(dbg, path)
<commit_msg>losetup: resolve symlinks when creating loop devices<commit_after>#!/usr/bin/env python

import os.path
import xapi
import commands
from common import log, run

# Use Linux "losetup" to create block devices from files

class Loop:
    """An active loop device"""
    def __init__(self, path, loop):
        self.path = path
        self.loop = loop
    def destroy(self, dbg):
        run(dbg, "losetup -d %s" % self.loop)
    def block_device(self):
        return self.loop

def find(dbg, path):
    """Return the active loop device associated with the given path"""
    for line in run(dbg, "losetup -a").split("\n"):
        line = line.strip()
        if line <> "":
            bits = line.split()
            loop = bits[0][0:-1]
            this_path = bits[2][1:-1]
            if this_path == path:
                return Loop(path, loop)
    return None

def create(dbg, path):
    """Creates a new loop device backed by the given file"""
    # losetup will resolve paths and 'find' needs to use string equality
    path = os.path.realpath(path)

    run(dbg, "losetup -f \"%s\"" % path)
    return find(dbg, path)
{'fraction_non_alphanumeric': 0.125418860698899, 'fraction_numerical': 0.0057443752991862135, 'mean_word_length': 2.387358184764992, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 5, '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': '6763623', 'n_tokens_mistral': 655, 'n_tokens_neox': 646, 'n_words': 229}