<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/.

/// Test the functionality of stratis filesystems.
extern crate devicemapper;
extern crate uuid;
extern crate env_logger;
extern crate nix;
extern crate tempdir;

use std::fs::OpenOptions;
use std::io::Write;
use std::path::Path;

use self::nix::mount::{MsFlags, mount, umount};

use self::tempdir::TempDir;

use self::devicemapper::consts::IEC;
use self::devicemapper::{Bytes, DM};
use self::devicemapper::consts::SECTOR_SIZE;

use libstratis::engine::Pool;
use libstratis::engine::Filesystem;
use libstratis::engine::strat_engine::filesystem::{FILESYSTEM_LOWATER, fs_usage};
use libstratis::engine::strat_engine::pool::StratPool;
use libstratis::engine::types::Redundancy;

/// Verify that the logical space allocated to a filesystem is expanded when
/// the number of sectors written to the filesystem causes the free space to
/// dip below the FILESYSTEM_LOWATER mark. Verify that the space has been
/// expanded by calling filesystem.check() then looking at the total space
/// compared to the original size.
pub fn test_xfs_expand(paths: &[&Path]) -> () {
    let dm = DM::new().unwrap();
    // Create a filesytem as small as possible.  Allocate 1 MiB bigger than
    // the low water mark.
    let fs_size = FILESYSTEM_LOWATER + Bytes(IEC::Mi).sectors();

    let (mut pool, _) =
        StratPool::initialize("stratis_test_pool", &dm, paths, Redundancy::NONE, true).unwrap();
    let &(_, fs_uuid) = pool.create_filesystems(&[("stratis_test_filesystem", Some(fs_size))])
        .unwrap()
        .first()
        .unwrap();
    // Braces to ensure f is closed before destroy and the borrow of pool is complete
    {
        let filesystem = pool.get_mut_strat_filesystem(&fs_uuid).unwrap();
        let devnode = filesystem.devnode();
        // Write 2 MiB of data. The filesystem's free space is now 1 MiB below
        // FILESYSTEM_LOWATER.
        let write_size = Bytes(IEC::Mi * 2).sectors();
        let tmp_dir = TempDir::new("stratis_testing").unwrap();
        mount(Some(&devnode),
              tmp_dir.path(),
              Some("xfs"),
              MsFlags::empty(),
              None as Option<&str>)
                .unwrap();
        let buf = &[1u8; SECTOR_SIZE];
        for i in 0..*write_size {
            let file_path = tmp_dir.path().join(format!("stratis_test{}.txt", i));
            let mut f = OpenOptions::new()
                .create(true)<commit_msg>Substitute an expression for a name<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/.

/// Test the functionality of stratis filesystems.
extern crate devicemapper;
extern crate uuid;
extern crate env_logger;
extern crate nix;
extern crate tempdir;

use std::fs::OpenOptions;
use std::io::Write;
use std::path::Path;

use self::nix::mount::{MsFlags, mount, umount};

use self::tempdir::TempDir;

use self::devicemapper::consts::IEC;
use self::devicemapper::{Bytes, DM};
use self::devicemapper::consts::SECTOR_SIZE;

use libstratis::engine::Pool;
use libstratis::engine::Filesystem;
use libstratis::engine::strat_engine::filesystem::{FILESYSTEM_LOWATER, fs_usage};
use libstratis::engine::strat_engine::pool::StratPool;
use libstratis::engine::types::Redundancy;

/// Verify that the logical space allocated to a filesystem is expanded when
/// the number of sectors written to the filesystem causes the free space to
/// dip below the FILESYSTEM_LOWATER mark. Verify that the space has been
/// expanded by calling filesystem.check() then looking at the total space
/// compared to the original size.
pub fn test_xfs_expand(paths: &[&Path]) -> () {
    let dm = DM::new().unwrap();
    // Create a filesytem as small as possible.  Allocate 1 MiB bigger than
    // the low water mark.
    let fs_size = FILESYSTEM_LOWATER + Bytes(IEC::Mi).sectors();

    let (mut pool, _) =
        StratPool::initialize("stratis_test_pool", &dm, paths, Redundancy::NONE, true).unwrap();
    let &(_, fs_uuid) = pool.create_filesystems(&[("stratis_test_filesystem", Some(fs_size))])
        .unwrap()
        .first()
        .unwrap();
    // Braces to ensure f is closed before destroy and the borrow of pool is complete
    {
        let filesystem = pool.get_mut_strat_filesystem(&fs_uuid).unwrap();
        // Write 2 MiB of data. The filesystem's free space is now 1 MiB below
        // FILESYSTEM_LOWATER.
        let write_size = Bytes(IEC::Mi * 2).sectors();
        let tmp_dir = TempDir::new("stratis_testing").unwrap();
        mount(Some(&filesystem.devnode()),
              tmp_dir.path(),
              Some("xfs"),
              MsFlags::empty(),
              None as Option<&str>)
                .unwrap();
        let buf = &[1u8; SECTOR_SIZE];
        for i in 0..*write_size {
            let file_path = tmp_dir.path().join(format!("stratis_test{}.txt", i));
            let mut f = OpenOptions::new()
                .create(true)
{'fraction_non_alphanumeric': 0.1283770837325158, 'fraction_numerical': 0.00421536692853037, 'mean_word_length': 3.299835255354201, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '4203880', 'n_tokens_mistral': 1631, 'n_tokens_neox': 1613, 'n_words': 495}