<commit_before><?php
class Libvirt {
    private $conn;
    private $last_error;
    private $allow_cached = true;
    private $dominfos = array();

    function Libvirt($debug = false) {
        if ($debug)
            $this->set_logfile($debug);
    }

    function _set_last_error() {
        $this->last_error = libvirt_get_last_error();
        return false;
    }

    function set_logfile($filename) {
        if (!libvirt_logfile_set($filename))
            return $this->_set_last_error();

        return true;
    }

    function print_resources() {
        return libvirt_print_binding_resources();
    }

    function connect($uri = 'null') {
        $this->conn=libvirt_connect($uri, false);
        if ($this->conn==false)
            return $this->_set_last_error();
        return true;
    }

    function domain_disk_add($domain, $img, $dev, $type='scsi', $driver='raw') {
        $dom = $this->get_domain_object($domain);

        $tmp = libvirt_domain_disk_add($dom, $img, $dev, $type, $driver);
        return ($tmp) ? $tmp : $this->_set_last_error();
    }

    function domain_change_numVCpus($domain, $num) {
        $dom = $this->get_domain_object($domain);

        $tmp = libvirt_domain_change_vcpus($dom, $num);
        return ($tmp) ? $tmp : $this->_set_last_error();
    }

    function domain_change_memory_allocation($domain, $memory, $maxmem) {
        $dom = $this->get_domain_object($domain);

        $tmp = libvirt_domain_change_memory($dom, $memory, $maxmem);
        return ($tmp) ? $tmp : $this->_set_last_error();
    }

    function domain_change_boot_devices($domain, $first, $second) {
        $dom = $this->get_domain_object($domain);

        $tmp = libvirt_domain_change_boot_devices($dom, $first, $second);
        return ($tmp) ? $tmp : $this->_set_last_error();
    }

    function domain_get_screenshot($domain) {
        $dom = $this->get_domain_object($domain);

        $tmp = libvirt_domain_get_screenshot($dom, $this->get_hostname() );
        return ($tmp) ? $tmp : $this->_set_last_error();
    }

    function domain_get_screenshot_thumbnail($domain, $w=120) {
        $screen = $this->domain_get_screenshot($domain);
        $imgFile = tempnam("/tmp", "libvirt-php-tmp-resize-XXXXXX");;

        if ($screen) {
            $fp = fopen($imgFile, "wb");
            fwrite($fp, $screen);
            fclose($fp);
        }

        if (file_exists($imgFile) && $screen) {
            list($width, $height) = getimagesize($imgFile);
            $h = ($height / $width) * $w;
        } else {
            $w = $h = 1;
            //$h = $w * (3 / 4.5);
        }

        $new = imagecreatetruecolor($w, $h);
        if ($screen) {
            $img = imagecreatefrompng($imgFile);
            imagecopyresampled($new,$img,0,0,0,0, $w,$h,$width,$height);
            imagedestroy($img);
        } else {
            $c = imagecolorallocate($new, 255, 255, 255);
            imagefill($new, 0, 0, $c);
        }

        imagepng($new, $imgFile);
        imagedestroy($new);

        $fp = fopen($imgFile, "rb");
        $data = fread($fp, filesize($imgFile));
        fclose($fp);

        unlink($imgFile);
        return $data;
    }

    function domain_get_screen_dimensions($domain) {
        $screen = $this->domain_get_screenshot($domain);
        $imgFile = tempnam("/tmp", "libvirt-php-tmp-resize-XXXXXX");;

        $width = false;
        $height = false;

        if ($screen) {
            $fp = fopen($imgFile, "wb");
            fwrite($fp, $screen);
            fclose($fp);
        }
        if (file_exists($imgFile) && $screen)
            list($width, $height) = getimagesize($imgFile);

        unlink($imgFile);

        return array('height' => $height, 'width' => $width);
    }

    function domain_send_keys($domain, $keys) {
        $dom = $this->get_domain_object($domain);

        $tmp = libvirt_domain_send_keys($dom, $this->get_hostname(), $keys);
        return ($tmp) ? $tmp : $this->_set_last_error();
    }

    function domain_send_pointer_event($domain, $x, $y, $clicked = 1) {
        $dom = $this->get_domain_object($domain);

        $tmp = libvirt_domain_send_pointer_event($dom, $this->get_hostname(), $x, $y, $clicked, true);
        return ($tmp) ? $tmp : $this->_set_last_error();
    }

    function domain_disk_remove($domain, $dev) {
        $dom = $this->get_domain_object($domain);

        $tmp = libvirt_domain_disk_remove($dom, $dev);
        return ($tmp) ? $tmp : $this->_set_last_error();
    }

    function supports($name) {
        return libvirt_has_feature($name);
    }

    function macbyte($val) {
        if ($val < 16)
            return '0'.dechex($val);

        return dechex($val);
    }

    function generate_random_mac_addr($seed=false) {
        if (!$seed)
            $seed = 1;

        if ($this->get_hypervisor_name() == 'qemu') {
            $prefix = '52:54:00';
        } else {
            if ($this->get_hypervisor_name() == 'xen') {
                $prefix = '00:16:3e';
            } else {
                $prefix = $this->macbyte(($seed * rand()) % 256).':'.
                    $this->macbyte(($seed * rand()) % 256).':'.
                    $this->macbyte(($seed * rand()) % 256);
            }
        }

        return $prefix.':'.
            $this->macbyte(($seed * rand()) % 256).':'.
            $this->macbyte(($seed * rand()) % 256).':'.
            $this->macbyte(($seed * rand()) % 256);
    }

    function domain_nic_add($domain, $mac, $network, $model=false) {
        $dom = $this->get_domain_object($domain);

        if ($model == 'default')
            $model = false;

        $tmp = libvirt_domain_nic_add($dom, $mac, $network, $model);
        return ($tmp) ? $tmp : $this->_set_last_error();
    }

    function domain_nic_remove($domain, $mac) {
        $dom = $this->get_domain_object($domain);

        $tmp = libvirt_domain_nic_remove($dom, $mac);
        return ($tmp) ? $tmp : $this->_set_last_error();
    }

    function get_connection() {
        return $this->conn;
    }

    function get_hostname() {
        return libvirt_connect_get_hostname($this->conn);
    }

    function get_domain_object($nameRes) {
        if (is_resource($nameRes))
            return $nameRes;

        $dom=libvirt_domain_lookup_by_name($this->conn, $nameRes);
        if (!$dom) {
            $dom=libvirt_domain_lookup_by_uuid_string($this->conn, $nameRes);
            if (!$dom)
                return $this->_set_last_error();
        }

        return $dom;<commit_msg>examples: Don't report errors when looking up a domain<commit_after><?php
class Libvirt {
    private $conn;
    private $last_error;
    private $allow_cached = true;
    private $dominfos = array();

    function Libvirt($debug = false) {
        if ($debug)
            $this->set_logfile($debug);
    }

    function _set_last_error() {
        $this->last_error = libvirt_get_last_error();
        return false;
    }

    function set_logfile($filename) {
        if (!libvirt_logfile_set($filename))
            return $this->_set_last_error();

        return true;
    }

    function print_resources() {
        return libvirt_print_binding_resources();
    }

    function connect($uri = 'null') {
        $this->conn=libvirt_connect($uri, false);
        if ($this->conn==false)
            return $this->_set_last_error();
        return true;
    }

    function domain_disk_add($domain, $img, $dev, $type='scsi', $driver='raw') {
        $dom = $this->get_domain_object($domain);

        $tmp = libvirt_domain_disk_add($dom, $img, $dev, $type, $driver);
        return ($tmp) ? $tmp : $this->_set_last_error();
    }

    function domain_change_numVCpus($domain, $num) {
        $dom = $this->get_domain_object($domain);

        $tmp = libvirt_domain_change_vcpus($dom, $num);
        return ($tmp) ? $tmp : $this->_set_last_error();
    }

    function domain_change_memory_allocation($domain, $memory, $maxmem) {
        $dom = $this->get_domain_object($domain);

        $tmp = libvirt_domain_change_memory($dom, $memory, $maxmem);
        return ($tmp) ? $tmp : $this->_set_last_error();
    }

    function domain_change_boot_devices($domain, $first, $second) {
        $dom = $this->get_domain_object($domain);

        $tmp = libvirt_domain_change_boot_devices($dom, $first, $second);
        return ($tmp) ? $tmp : $this->_set_last_error();
    }

    function domain_get_screenshot($domain) {
        $dom = $this->get_domain_object($domain);

        $tmp = libvirt_domain_get_screenshot($dom, $this->get_hostname() );
        return ($tmp) ? $tmp : $this->_set_last_error();
    }

    function domain_get_screenshot_thumbnail($domain, $w=120) {
        $screen = $this->domain_get_screenshot($domain);
        $imgFile = tempnam("/tmp", "libvirt-php-tmp-resize-XXXXXX");;

        if ($screen) {
            $fp = fopen($imgFile, "wb");
            fwrite($fp, $screen);
            fclose($fp);
        }

        if (file_exists($imgFile) && $screen) {
            list($width, $height) = getimagesize($imgFile);
            $h = ($height / $width) * $w;
        } else {
            $w = $h = 1;
            //$h = $w * (3 / 4.5);
        }

        $new = imagecreatetruecolor($w, $h);
        if ($screen) {
            $img = imagecreatefrompng($imgFile);
            imagecopyresampled($new,$img,0,0,0,0, $w,$h,$width,$height);
            imagedestroy($img);
        } else {
            $c = imagecolorallocate($new, 255, 255, 255);
            imagefill($new, 0, 0, $c);
        }

        imagepng($new, $imgFile);
        imagedestroy($new);

        $fp = fopen($imgFile, "rb");
        $data = fread($fp, filesize($imgFile));
        fclose($fp);

        unlink($imgFile);
        return $data;
    }

    function domain_get_screen_dimensions($domain) {
        $screen = $this->domain_get_screenshot($domain);
        $imgFile = tempnam("/tmp", "libvirt-php-tmp-resize-XXXXXX");;

        $width = false;
        $height = false;

        if ($screen) {
            $fp = fopen($imgFile, "wb");
            fwrite($fp, $screen);
            fclose($fp);
        }
        if (file_exists($imgFile) && $screen)
            list($width, $height) = getimagesize($imgFile);

        unlink($imgFile);

        return array('height' => $height, 'width' => $width);
    }

    function domain_send_keys($domain, $keys) {
        $dom = $this->get_domain_object($domain);

        $tmp = libvirt_domain_send_keys($dom, $this->get_hostname(), $keys);
        return ($tmp) ? $tmp : $this->_set_last_error();
    }

    function domain_send_pointer_event($domain, $x, $y, $clicked = 1) {
        $dom = $this->get_domain_object($domain);

        $tmp = libvirt_domain_send_pointer_event($dom, $this->get_hostname(), $x, $y, $clicked, true);
        return ($tmp) ? $tmp : $this->_set_last_error();
    }

    function domain_disk_remove($domain, $dev) {
        $dom = $this->get_domain_object($domain);

        $tmp = libvirt_domain_disk_remove($dom, $dev);
        return ($tmp) ? $tmp : $this->_set_last_error();
    }

    function supports($name) {
        return libvirt_has_feature($name);
    }

    function macbyte($val) {
        if ($val < 16)
            return '0'.dechex($val);

        return dechex($val);
    }

    function generate_random_mac_addr($seed=false) {
        if (!$seed)
            $seed = 1;

        if ($this->get_hypervisor_name() == 'qemu') {
            $prefix = '52:54:00';
        } else {
            if ($this->get_hypervisor_name() == 'xen') {
                $prefix = '00:16:3e';
            } else {
                $prefix = $this->macbyte(($seed * rand()) % 256).':'.
                    $this->macbyte(($seed * rand()) % 256).':'.
                    $this->macbyte(($seed * rand()) % 256);
            }
        }

        return $prefix.':'.
            $this->macbyte(($seed * rand()) % 256).':'.
            $this->macbyte(($seed * rand()) % 256).':'.
            $this->macbyte(($seed * rand()) % 256);
    }

    function domain_nic_add($domain, $mac, $network, $model=false) {
        $dom = $this->get_domain_object($domain);

        if ($model == 'default')
            $model = false;

        $tmp = libvirt_domain_nic_add($dom, $mac, $network, $model);
        return ($tmp) ? $tmp : $this->_set_last_error();
    }

    function domain_nic_remove($domain, $mac) {
        $dom = $this->get_domain_object($domain);

        $tmp = libvirt_domain_nic_remove($dom, $mac);
        return ($tmp) ? $tmp : $this->_set_last_error();
    }

    function get_connection() {
        return $this->conn;
    }

    function get_hostname() {
        return libvirt_connect_get_hostname($this->conn);
    }

    function get_domain_object($nameRes) {
        if (is_resource($nameRes))
            return $nameRes;

        $dom=@libvirt_domain_lookup_by_name($this->conn, $nameRes);
        if (!$dom) {
            $dom=@libvirt_domain_lookup_by_uuid_string($this->conn, $nameRes);
            if (!$dom)
                return $this->_set_last_error();
        }

        return $dom;
{'fraction_non_alphanumeric': 0.1659421947685503, 'fraction_numerical': 0.00854114237779303, 'mean_word_length': 2.31078010603383, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 95, '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': '4496272', 'n_tokens_mistral': 4375, 'n_tokens_neox': 4193, 'n_words': 747}