<commit_before>// 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::path::PathBuf;

use devicemapper::{DM, DevId, DmFlags, DmResult};

use mnt::get_submounts;
use nix::mount::{MntFlags, umount2};


mod cleanup_errors {
    use mnt;
    use nix;

    error_chain!{
        foreign_links {
            Mnt(mnt::ParseError);
            Nix(nix::Error);
        }
    }
}

use self::cleanup_errors::{Error, Result, ResultExt};


/// Attempt to remove all device mapper devices which match the stratis naming convention.
/// FIXME: Current implementation complicated by https://bugzilla.redhat.com/show_bug.cgi?id=1506287
fn dm_stratis_devices_remove() -> Result<()> {

    /// One iteration of removing devicemapper devices
    fn one_iteration(dm: &DM) -> DmResult<(bool, Vec<String>)> {
        let mut progress_made = false;
        let mut remain = Vec::new();

        for d in dm.list_devices()?
                .iter()
                .filter(|d| format!("{}", d.0.as_ref()).starts_with("stratis-1")) {

            match dm.device_remove(&DevId::Name(&d.0), DmFlags::empty()) {
                Ok(_) => progress_made = true,
                Err(_) => remain.push(format!("{}", d.0.as_ref())),
            }
        }
        Ok((progress_made, remain))
    }

    let dm = DM::new().chain_err(|| "Unable to initialize DM")?;

    loop {
        let (progress_made, remain) = one_iteration(&dm)
            .map_err(|e| {
                Error::with_chain(e,
                                  "Error while attempting to remove stratis device mapper devices")}
                )?;

        if !progress_made {
            if remain.len() != 0 {
                bail!("We were unable to remove all stratis device mapper devices {:?}",
                      remain);
            }
            break;
        }
    }

    Ok(())
}<commit_msg>Use get_dm() in code for test cleanup<commit_after>// 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::panic::catch_unwind;
use std::path::PathBuf;

use mnt::get_submounts;
use nix::mount::{MntFlags, umount2};

use devicemapper::{DevId, DmFlags, DmResult};

use super::super::dm::get_dm;


mod cleanup_errors {
    use mnt;
    use nix;

    error_chain!{
        foreign_links {
            Mnt(mnt::ParseError);
            Nix(nix::Error);
        }
    }
}

use self::cleanup_errors::{Error, Result};


/// Attempt to remove all device mapper devices which match the stratis naming convention.
/// FIXME: Current implementation complicated by https://bugzilla.redhat.com/show_bug.cgi?id=1506287
fn dm_stratis_devices_remove() -> Result<()> {

    /// One iteration of removing devicemapper devices
    fn one_iteration() -> DmResult<(bool, Vec<String>)> {
        let mut progress_made = false;
        let mut remain = Vec::new();

        for d in get_dm()
                .list_devices()?
                .iter()
                .filter(|d| format!("{}", d.0.as_ref()).starts_with("stratis-1")) {

            match get_dm().device_remove(&DevId::Name(&d.0), DmFlags::empty()) {
                Ok(_) => progress_made = true,
                Err(_) => remain.push(format!("{}", d.0.as_ref())),
            }
        }
        Ok((progress_made, remain))
    }

    if catch_unwind(get_dm).is_err() {
        return Err("Unable to initialize DM".into());
    }

    loop {
        let (progress_made, remain) = one_iteration()
            .map_err(|e| {
                Error::with_chain(e,
                                  "Error while attempting to remove stratis device mapper devices")}
                )?;

        if !progress_made {
            if remain.len() != 0 {
                bail!("We were unable to remove all stratis device mapper devices {:?}",
                      remain);
            }
            break;
        }
    }

    Ok(())
}
{'fraction_non_alphanumeric': 0.13575757575757577, 'fraction_numerical': 0.008242424242424242, 'mean_word_length': 2.0699404761904763, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 17, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4876339', 'n_tokens_mistral': 1306, 'n_tokens_neox': 1249, 'n_words': 332}