Skip to content

Commit

Permalink
wip counts hard links only once for each dir
Browse files Browse the repository at this point in the history
  • Loading branch information
wookietreiber committed Aug 14, 2024
1 parent 83e0ac0 commit f0259f2
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 97 deletions.
63 changes: 0 additions & 63 deletions src/mmdu.list.size

This file was deleted.

6 changes: 3 additions & 3 deletions src/policy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ fn policy_group(group: uid_t, attribute: &str) -> String {
RULE 'TOTAL'
LIST 'size'
DIRECTORIES_PLUS
SHOW(VARCHAR({attribute}))
SHOW(VARCHAR({attribute}) || ' ' || VARCHAR(NLINK))
WHERE GROUP_ID = {group}
"
)
Expand All @@ -71,7 +71,7 @@ fn policy_user(user: uid_t, attribute: &str) -> String {
RULE 'TOTAL'
LIST 'size'
DIRECTORIES_PLUS
SHOW(VARCHAR({attribute}))
SHOW(VARCHAR({attribute}) || ' ' || VARCHAR(NLINK))
WHERE USER_ID = {user}
"
)
Expand All @@ -86,7 +86,7 @@ fn policy_default(attribute: &str) -> String {
RULE 'TOTAL'
LIST 'size'
DIRECTORIES_PLUS
SHOW(VARCHAR({attribute}))
SHOW(VARCHAR({attribute}) || ' ' || VARCHAR(NLINK))
"
)
}
98 changes: 67 additions & 31 deletions src/usage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

use std::collections::BTreeMap;
use std::collections::{BTreeMap, HashMap};
use std::fs::File;
use std::io::BufReader;
use std::io::{BufReader, Read};
use std::ops::AddAssign;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
Expand Down Expand Up @@ -107,6 +107,14 @@ pub fn run(dir: &Path, config: &Config) -> Result<()> {
}

fn sum(dir: &Path, report: &Path, config: &Config) -> Result<()> {
let report = File::open(report).with_context(|| {
format!(
"opening report {} (this is likely because applying a \
filter didn't return any results)",
report.display()
)
})?;

if let Some(depth) = config.max_depth {
let sizes = sum_depth(dir, depth, report, config)?;

Expand All @@ -128,17 +136,9 @@ fn sum(dir: &Path, report: &Path, config: &Config) -> Result<()> {
fn sum_depth(
dir: &Path,
depth: usize,
report: &Path,
report: impl Read,
config: &Config,
) -> Result<BTreeMap<PathBuf, Acc>> {
let report = File::open(report).with_context(|| {
format!(
"opening report {} (this is likely because applying a \
filter didn't return any results)",
report.display()
)
})?;

let report = BufReader::new(report);

let mut dir_sums = BTreeMap::new();
Expand Down Expand Up @@ -177,27 +177,37 @@ fn sum_depth(
Ok(dir_sums)
}

fn sum_total(report: &Path) -> Result<Acc> {
let mut sum = Acc::default();

let report = File::open(report).with_context(|| {
format!(
"opening report {} (this is likely because applying a \
filter didn't return any results)",
report.display()
)
})?;

fn sum_total(report: impl Read) -> Result<Acc> {
let report = BufReader::new(report);

let mut sum = Acc::default();
let mut hard_links = HashMap::new();

for line in report.byte_lines() {
let line = line?;

let bytes = line.splitn_str(6, " ").nth(4).unwrap();
let bytes = bytes.to_str().unwrap();
let bytes: u64 = bytes.parse().unwrap();
// inode generation snapid X Y Z -- path
let fields = line.splitn_str(7, " ").take(6).collect::<Vec<_>>();

sum += bytes;
let bytes = fields[4].to_str().unwrap();
let bytes = bytes.parse::<u64>().unwrap();

let nlink = fields[5].to_str().unwrap();

if nlink == "1" {
sum += bytes;
} else {
let inode = fields[0].to_str().unwrap();

let inode = hard_links
.entry(inode.to_owned())
.and_modify(|c| *c += 1)
.or_insert(0);

if *inode == 1 {
sum += bytes;
}
}
}

Ok(sum)
Expand All @@ -207,7 +217,7 @@ fn sum_total(report: &Path) -> Result<Acc> {
// accumulator
// ----------------------------------------------------------------------------

#[derive(Clone, Copy, Default)]
#[derive(Clone, Copy, Debug, Default, PartialEq)]
struct Acc {
inodes: u64,
bytes: u64,
Expand All @@ -228,13 +238,39 @@ impl AddAssign<u64> for Acc {

#[cfg(test)]
mod test {
use std::path::Path;
use super::*;

#[test]
fn total() {
let acc = super::sum_total(Path::new("src/mmdu.list.size")).unwrap();
let source = "1 1 0 1024 1 -- /data/test/foo\n\
2 1 0 1024 1 -- /data/test/bar\n";

let acc = sum_total(source.as_bytes()).unwrap();

assert_eq!(
Acc {
inodes: 2,
bytes: 2048
},
acc
);
}

assert_eq!(acc.inodes, 63);
assert_eq!(acc.bytes, 1_415_269);
#[test]
fn hardlinks() {
let source = "1 1 0 1024 4 -- /data/test/foo\n\
1 1 0 1024 4 -- /data/test/bar\n\
1 1 0 1024 4 -- /data/test/baz\n\
1 1 0 1024 4 -- /data/test/bippy\n";

let acc = sum_total(source.as_bytes()).unwrap();

assert_eq!(
Acc {
inodes: 1,
bytes: 1024
},
acc
);
}
}

0 comments on commit f0259f2

Please sign in to comment.