1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

use std::{
    ffi::{CStr, CString},
    fs::File,
    os::unix::io::AsRawFd,
    ptr,
};

use crate::{err::Result, tag::BlkidTagIter};

/// Size of a device as reported by libblkid
pub struct BlkidSize(libblkid_rs_sys::blkid_loff_t);

impl Into<i64> for BlkidSize {
    fn into(self) -> i64 {
        self.0
    }
}

/// Block device found by blkid
pub struct BlkidDev(libblkid_rs_sys::blkid_dev);

impl BlkidDev {
    pub(crate) fn new(inner: libblkid_rs_sys::blkid_dev) -> Self {
        BlkidDev(inner)
    }

    /// Get the device name for a blkid device
    pub fn devname(&self) -> Result<&str> {
        let ret = errno_ptr!(unsafe { libblkid_rs_sys::blkid_dev_devname(self.0) })?;
        let cstr_ret = unsafe { CStr::from_ptr(ret) };
        Ok(cstr_ret.to_str()?)
    }

    /// Get the size of a device as reported by the cache
    pub fn devsize(&self) -> Result<BlkidSize> {
        let f = File::open(self.devname()?)?;
        Ok(BlkidSize(unsafe {
            libblkid_rs_sys::blkid_get_dev_size(f.as_raw_fd())
        }))
    }

    /// Iterate through tags in associated with the given block device
    pub fn tag_iter(&self) -> BlkidTagIter {
        BlkidTagIter::new(unsafe { libblkid_rs_sys::blkid_tag_iterate_begin(self.0) })
    }

    /// Return `true` if the given device has a specified tag
    pub fn has_tag(&self, type_: &str, value: &str) -> Result<bool> {
        let type_cstring = CString::new(type_)?;
        let value_cstring = CString::new(value)?;
        Ok(unsafe {
            libblkid_rs_sys::blkid_dev_has_tag(
                self.0,
                type_cstring.as_ptr(),
                value_cstring.as_ptr(),
            )
        } != 0)
    }
}

/// Iterator for blkid-discovered block devices
pub struct BlkidDevIter(libblkid_rs_sys::blkid_dev_iterate);

impl BlkidDevIter {
    pub(crate) fn new(iter: libblkid_rs_sys::blkid_dev_iterate) -> Self {
        BlkidDevIter(iter)
    }

    /// Set search parameters for iteration
    pub fn search(self, search_type: &str, search_value: &str) -> Result<Self> {
        let search_type_cstring = CString::new(search_type)?;
        let search_value_cstring = CString::new(search_value)?;
        errno!(unsafe {
            libblkid_rs_sys::blkid_dev_set_search(
                self.0,
                search_type_cstring.as_ptr(),
                search_value_cstring.as_ptr(),
            )
        })?;
        Ok(self)
    }
}

impl Iterator for BlkidDevIter {
    type Item = BlkidDev;

    fn next(&mut self) -> Option<Self::Item> {
        let mut dev: libblkid_rs_sys::blkid_dev = ptr::null_mut();
        if unsafe { libblkid_rs_sys::blkid_dev_next(self.0, &mut dev as *mut _) } < 0 {
            None
        } else {
            assert!(!dev.is_null());
            Some(BlkidDev(dev))
        }
    }
}

impl Drop for BlkidDevIter {
    fn drop(&mut self) {
        unsafe { libblkid_rs_sys::blkid_dev_iterate_end(self.0) }
    }
}