mirror of
https://github.com/bootandy/dust.git
synced 2025-12-11 15:20:39 -08:00
Compare commits
41 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2fe3943ed5 | ||
|
|
b8ad44b7f0 | ||
|
|
1a34bc3198 | ||
|
|
385ddb75e1 | ||
|
|
5c6165da8a | ||
|
|
f39b09e79c | ||
|
|
778dbb44b3 | ||
|
|
69c79d5f95 | ||
|
|
61ab0e8f96 | ||
|
|
285fd62850 | ||
|
|
6a63cbe1bc | ||
|
|
8d98171b82 | ||
|
|
120b4e16e7 | ||
|
|
6198e3183f | ||
|
|
ecf6c8f0e5 | ||
|
|
f14a9789f4 | ||
|
|
4944d517f4 | ||
|
|
cce656ab4c | ||
|
|
03a517a310 | ||
|
|
3796c39ac9 | ||
|
|
0357fc5e71 | ||
|
|
aa3f411974 | ||
|
|
eb69ad19a0 | ||
|
|
c127580057 | ||
|
|
c978c6cf93 | ||
|
|
f72a67132c | ||
|
|
4d1f881c17 | ||
|
|
e75a666a4c | ||
|
|
617b0d2971 | ||
|
|
8fa83e3836 | ||
|
|
7fa2ce3434 | ||
|
|
b0e971891e | ||
|
|
cef95fa415 | ||
|
|
381d286847 | ||
|
|
aa963defda | ||
|
|
0faf795284 | ||
|
|
b7271b1da2 | ||
|
|
99f462f023 | ||
|
|
1c80cbf28b | ||
|
|
f802d7a6b4 | ||
|
|
b4c6c68527 |
@@ -19,17 +19,9 @@ matrix:
|
|||||||
# don't need
|
# don't need
|
||||||
include:
|
include:
|
||||||
# Linux
|
# Linux
|
||||||
- env: TARGET=aarch64-unknown-linux-gnu
|
|
||||||
- env: TARGET=arm-unknown-linux-gnueabi
|
|
||||||
- env: TARGET=armv7-unknown-linux-gnueabihf
|
|
||||||
- env: TARGET=i686-unknown-linux-gnu
|
|
||||||
- env: TARGET=i686-unknown-linux-musl
|
|
||||||
- env: TARGET=x86_64-unknown-linux-gnu
|
- env: TARGET=x86_64-unknown-linux-gnu
|
||||||
- env: TARGET=x86_64-unknown-linux-musl
|
|
||||||
|
|
||||||
# OSX
|
# OSX
|
||||||
- env: TARGET=i686-apple-darwin
|
|
||||||
os: osx
|
|
||||||
- env: TARGET=x86_64-apple-darwin
|
- env: TARGET=x86_64-apple-darwin
|
||||||
os: osx
|
os: osx
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,11 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "dust"
|
name = "dust"
|
||||||
version = "0.1.0"
|
version = "0.2.3"
|
||||||
authors = ["bootandy <bootandy@gmail.com>"]
|
authors = ["bootandy <bootandy@gmail.com>", "nebkor <code@ardent.nebcorp.com>"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
ansi_term = "0.11"
|
ansi_term = "0.11"
|
||||||
clap = "2.31"
|
clap = "2.31"
|
||||||
|
assert_cli = "0.5"
|
||||||
|
tempfile = "3"
|
||||||
|
|
||||||
|
|||||||
49
README.md
49
README.md
@@ -4,6 +4,12 @@
|
|||||||
# Dust
|
# Dust
|
||||||
du + rust = dust. A rust alternative to du
|
du + rust = dust. A rust alternative to du
|
||||||
|
|
||||||
|
[Releases](https://github.com/bootandy/dust/releases)
|
||||||
|
To install:
|
||||||
|
* Download linux / mac binary from [Releases](https://github.com/bootandy/dust/releases)
|
||||||
|
* unzip file: tar -xvf <file>
|
||||||
|
* copy file to search path: sudo mv dust /usr/local/bin/
|
||||||
|
|
||||||
Unlike du, dust is meant to give you an instant overview of which directories are using disk space without requiring sort or head. Dust does not count file system blocks; it uses file sizes instead. Dust will print a maximum of 1 'Did not have permissions message'.
|
Unlike du, dust is meant to give you an instant overview of which directories are using disk space without requiring sort or head. Dust does not count file system blocks; it uses file sizes instead. Dust will print a maximum of 1 'Did not have permissions message'.
|
||||||
|
|
||||||
|
|
||||||
@@ -11,32 +17,35 @@ Dust will list the 15 biggest sub directories and will smartly recurse down the
|
|||||||
|
|
||||||
```
|
```
|
||||||
Usage: dust <dir>
|
Usage: dust <dir>
|
||||||
|
Usage: dust <dir> <another_dir> <and_more>
|
||||||
|
Usage: dust -s <dir> (apparent-size - shows the length of the file as opposed to the amount of disk space it uses)
|
||||||
Usage: dust -n 30 <dir> (Shows 30 directories not 15)
|
Usage: dust -n 30 <dir> (Shows 30 directories not 15)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
```
|
```
|
||||||
dust .
|
djin:git/dust> dust
|
||||||
161M .
|
65M .
|
||||||
160M └── ./target
|
65M └─┬ ./target
|
||||||
123M ├── ./target/debug
|
49M ├─┬ ./target/debug
|
||||||
83M │ ├── ./target/debug/deps
|
26M │ ├─┬ ./target/debug/deps
|
||||||
16M │ │ ├── ./target/debug/deps/libclap-82e6176feef5d4b7.rlib
|
21M │ │ └── ./target/debug/deps/libclap-9e6625ac8ff074ad.rlib
|
||||||
8.6M │ │ └── ./target/debug/deps/dust-993f7d919d92f0f8.dSYM
|
13M │ ├── ./target/debug/dust
|
||||||
8.6M │ │ └── ./target/debug/deps/dust-993f7d919d92f0f8.dSYM/Contents
|
8.9M │ └─┬ ./target/debug/incremental
|
||||||
8.6M │ │ └── ./target/debug/deps/dust-993f7d919d92f0f8.dSYM/Contents/Resources
|
6.7M │ ├─┬ ./target/debug/incremental/dust-2748eiei2tcnp
|
||||||
27M │ ├── ./target/debug/incremental
|
6.7M │ │ └─┬ ./target/debug/incremental/dust-2748eiei2tcnp/s-ezd6jnik5u-163pyem-1aab9ncf5glum
|
||||||
12M │ └── ./target/debug/build
|
3.0M │ │ └── ./target/debug/incremental/dust-2748eiei2tcnp/s-ezd6jnik5u-163pyem-1aab9ncf5glum/dep-graph.bin
|
||||||
20M ├── ./target/x86_64-apple-darwin
|
2.2M │ └─┬ ./target/debug/incremental/dust-1dlon65p8m3vl
|
||||||
20M │ └── ./target/x86_64-apple-darwin/debug
|
2.2M │ └── ./target/debug/incremental/dust-1dlon65p8m3vl/s-ezd6jncecv-1xsnfd0-4dw9l1r2th2t
|
||||||
20M │ └── ./target/x86_64-apple-darwin/debug/deps
|
15M └─┬ ./target/release
|
||||||
16M │ └── ./target/x86_64-apple-darwin/debug/deps/libclap-7e3f8513c52cd558.rlib
|
9.2M ├─┬ ./target/release/deps
|
||||||
16M └── ./target/release
|
6.7M │ └── ./target/release/deps/libclap-87bc2534ea57f044.rlib
|
||||||
13M └── ./target/release/deps
|
5.9M └── ./target/release/dust
|
||||||
```
|
```
|
||||||
|
|
||||||
Performance: dust is currently about 4 times slower than du.
|
Performance: dust is currently about 4 times slower than du.
|
||||||
|
|
||||||
|
Alternatives:
|
||||||
|
* [NCDU](https://dev.yorhel.nl/ncdu)
|
||||||
|
* du -d 1 -h | sort -h
|
||||||
|
|
||||||
|
Note: Apparent-size is calculated slightly differently in dust to gdu. In dust each hard link is counted as using file_length space. In gdu only the first entry is counted.
|
||||||
|
|
||||||
|
|||||||
93
appveyor.yml
93
appveyor.yml
@@ -1,93 +0,0 @@
|
|||||||
# Based on the "trust" template v0.1.2
|
|
||||||
# https://github.com/japaric/trust/tree/v0.1.2
|
|
||||||
|
|
||||||
environment:
|
|
||||||
global:
|
|
||||||
# TODO This is the Rust channel that build jobs will use by default but can be
|
|
||||||
# overridden on a case by case basis down below
|
|
||||||
RUST_VERSION: stable
|
|
||||||
|
|
||||||
# TODO Update this to match the name of your project.
|
|
||||||
CRATE_NAME: dust
|
|
||||||
|
|
||||||
# TODO These are all the build jobs. Adjust as necessary. Comment out what you
|
|
||||||
# don't need
|
|
||||||
matrix:
|
|
||||||
# MinGW
|
|
||||||
- TARGET: i686-pc-windows-gnu
|
|
||||||
- TARGET: x86_64-pc-windows-gnu
|
|
||||||
|
|
||||||
# MSVC
|
|
||||||
- TARGET: i686-pc-windows-msvc
|
|
||||||
- TARGET: x86_64-pc-windows-msvc
|
|
||||||
|
|
||||||
# Testing other channels
|
|
||||||
- TARGET: x86_64-pc-windows-gnu
|
|
||||||
RUST_VERSION: nightly
|
|
||||||
- TARGET: x86_64-pc-windows-msvc
|
|
||||||
RUST_VERSION: nightly
|
|
||||||
|
|
||||||
install:
|
|
||||||
- ps: >-
|
|
||||||
If ($Env:TARGET -eq 'x86_64-pc-windows-gnu') {
|
|
||||||
$Env:PATH += ';C:\msys64\mingw64\bin'
|
|
||||||
} ElseIf ($Env:TARGET -eq 'i686-pc-windows-gnu') {
|
|
||||||
$Env:PATH += ';C:\msys64\mingw32\bin'
|
|
||||||
}
|
|
||||||
- curl -sSf -o rustup-init.exe https://win.rustup.rs/
|
|
||||||
- rustup-init.exe -y --default-host %TARGET% --default-toolchain %RUST_VERSION%
|
|
||||||
- set PATH=%PATH%;C:\Users\appveyor\.cargo\bin
|
|
||||||
- rustc -Vv
|
|
||||||
- cargo -V
|
|
||||||
|
|
||||||
# TODO This is the "test phase", tweak it as you see fit
|
|
||||||
test_script:
|
|
||||||
# we don't run the "test phase" when doing deploys
|
|
||||||
- if [%APPVEYOR_REPO_TAG%]==[false] (
|
|
||||||
cargo build --target %TARGET% &&
|
|
||||||
cargo build --target %TARGET% --release &&
|
|
||||||
cargo test --target %TARGET% &&
|
|
||||||
cargo test --target %TARGET% --release &&
|
|
||||||
cargo run --target %TARGET% &&
|
|
||||||
cargo run --target %TARGET% --release
|
|
||||||
)
|
|
||||||
|
|
||||||
before_deploy:
|
|
||||||
# TODO Update this to build the artifacts that matter to you
|
|
||||||
- cargo rustc --target %TARGET% --release --bin hello -- -C lto
|
|
||||||
- ps: ci\before_deploy.ps1
|
|
||||||
|
|
||||||
deploy:
|
|
||||||
artifact: /.*\.zip/
|
|
||||||
# TODO update `auth_token.secure`
|
|
||||||
# - Create a `public_repo` GitHub token. Go to: https://github.com/settings/tokens/new
|
|
||||||
# - Encrypt it. Go to https://ci.appveyor.com/tools/encrypt
|
|
||||||
# - Paste the output down here
|
|
||||||
auth_token:
|
|
||||||
secure: UlU73Td7Bkb2N88ws4YGLWR+4U0IMgiou9QQtMnmpouJFjeUNxtLSPMPODVXP7zq4sKt5HR5B3fX9MW4mKm351fvnQEoihETn06pKiXGnY//SlTPTt67MX9ZOYmd9ohJReMDOZDgqhnGLxfymycGtsLAmdjDZnAl+IMqgg0FMyVFj9Cl9aKxnn12lxQyX4zabHKk8TUKD3By8ZoEUnJMHt3gEtOmbDgS4brcTPeHCzqnYFw73LEnkqvz+JP0XwauJY7Cf8lminKm/klmjCkQji8T9SHI52v1g0Fxpx0ucp2o3vulQrLHXaHvZ6Fr7J0cSXXzaFF3rrGLt4t4jU/+9TZm1+n5k5XuPW4x4NTCC9NmIj/z0/z41t82E9qZhzhtm2Jdsg6H2tNk+C774TYqcmR6GCvfRadfjRp3cA5dh0UwDVjH2MJFxlHDVkl6la0mVVRsCGF3oBKZVk0BDl1womfnmI46o/uU+gLknHN6Ed6PHHPPYDViWd3VKdmHKT7XrkMMUF6HjZUtla689DWIOWZSiV++1dVPcl/1TV+6tTmN4bBtPcLuX7SHRuLp2PI2kATvRMECsa7gZRypW4jKpVn7b2yetX9TVI3i1zR5zkQJ3dPg8sATvYPL53aKH/WsqUg4rzoAlbk9so+++R4bQY69LhV3B511B7EAynoZFdM
|
|
||||||
description: ''
|
|
||||||
on:
|
|
||||||
# TODO Here you can pick which targets will generate binary releases
|
|
||||||
# In this example, there are some targets that are tested using the stable
|
|
||||||
# and nightly channels. This condition makes sure there is only one release
|
|
||||||
# for such targets and that's generated using the stable channel
|
|
||||||
RUST_VERSION: stable
|
|
||||||
appveyor_repo_tag: true
|
|
||||||
provider: GitHub
|
|
||||||
|
|
||||||
cache:
|
|
||||||
- C:\Users\appveyor\.cargo\registry
|
|
||||||
- target
|
|
||||||
|
|
||||||
branches:
|
|
||||||
only:
|
|
||||||
# Release tags
|
|
||||||
- /^v\d+\.\d+\.\d+.*$/
|
|
||||||
- master
|
|
||||||
|
|
||||||
notifications:
|
|
||||||
- provider: Email
|
|
||||||
on_build_success: false
|
|
||||||
|
|
||||||
# Building is done in the test phase, so we disable Appveyor's build phase.
|
|
||||||
build: false
|
|
||||||
131
src/display.rs
Normal file
131
src/display.rs
Normal file
@@ -0,0 +1,131 @@
|
|||||||
|
extern crate ansi_term;
|
||||||
|
|
||||||
|
use self::ansi_term::Colour::Fixed;
|
||||||
|
use dust::Node;
|
||||||
|
|
||||||
|
static UNITS: [char; 4] = ['T', 'G', 'M', 'K'];
|
||||||
|
|
||||||
|
pub fn draw_it(permissions: bool, heads: &Vec<Node>, to_display: &Vec<&Node>) -> () {
|
||||||
|
if !permissions {
|
||||||
|
eprintln!("Did not have permissions for all directories");
|
||||||
|
}
|
||||||
|
|
||||||
|
for d in to_display {
|
||||||
|
if heads.contains(d) {
|
||||||
|
display_node(d, &to_display, true, "")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn display_node<S: Into<String>>(
|
||||||
|
node_to_print: &Node,
|
||||||
|
to_display: &Vec<&Node>,
|
||||||
|
is_first: bool,
|
||||||
|
indentation_str: S,
|
||||||
|
) {
|
||||||
|
let mut is = indentation_str.into();
|
||||||
|
print_this_node(node_to_print, is_first, is.as_ref());
|
||||||
|
|
||||||
|
is = is.replace("└─┬", " ");
|
||||||
|
is = is.replace("└──", " ");
|
||||||
|
is = is.replace("├──", "│ ");
|
||||||
|
is = is.replace("├─┬", "│ ");
|
||||||
|
|
||||||
|
let printable_node_slashes = node_to_print.name().matches('/').count();
|
||||||
|
|
||||||
|
let mut num_siblings = to_display.iter().fold(0, |a, b| {
|
||||||
|
if node_to_print.children().contains(b)
|
||||||
|
&& b.name().matches('/').count() == printable_node_slashes + 1
|
||||||
|
{
|
||||||
|
a + 1
|
||||||
|
} else {
|
||||||
|
a
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
let mut is_biggest = true;
|
||||||
|
for node in to_display {
|
||||||
|
if node_to_print.children().contains(node) {
|
||||||
|
let has_display_children = node.children()
|
||||||
|
.iter()
|
||||||
|
.fold(false, |has_kids, n| has_kids || to_display.contains(&n));
|
||||||
|
|
||||||
|
let has_children = node.children().len() > 0 && has_display_children;
|
||||||
|
if node.name().matches('/').count() == printable_node_slashes + 1 {
|
||||||
|
num_siblings -= 1;
|
||||||
|
|
||||||
|
let tree_chars = {
|
||||||
|
if num_siblings == 0 {
|
||||||
|
if has_children {
|
||||||
|
"└─┬"
|
||||||
|
} else {
|
||||||
|
"└──"
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if has_children {
|
||||||
|
"├─┬"
|
||||||
|
} else {
|
||||||
|
"├──"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
display_node(&node, to_display, is_biggest, is.to_string() + tree_chars);
|
||||||
|
is_biggest = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn print_this_node(node: &Node, is_biggest: bool, indentation: &str) {
|
||||||
|
let pretty_size = format!("{:>5}", human_readable_number(node.size()),);
|
||||||
|
println!(
|
||||||
|
"{}",
|
||||||
|
format_string(node.name(), is_biggest, pretty_size.as_ref(), indentation)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn format_string(dir_name: &str, is_biggest: bool, size: &str, indentation: &str) -> String {
|
||||||
|
format!(
|
||||||
|
"{} {} {}",
|
||||||
|
if is_biggest {
|
||||||
|
Fixed(196).paint(size)
|
||||||
|
} else {
|
||||||
|
Fixed(7).paint(size)
|
||||||
|
},
|
||||||
|
indentation,
|
||||||
|
dir_name,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn human_readable_number(size: u64) -> String {
|
||||||
|
for (i, u) in UNITS.iter().enumerate() {
|
||||||
|
let marker = 1024u64.pow((UNITS.len() - i) as u32);
|
||||||
|
if size >= marker {
|
||||||
|
if size / marker < 10 {
|
||||||
|
return format!("{:.1}{}", (size as f32 / marker as f32), u);
|
||||||
|
} else {
|
||||||
|
return format!("{}{}", (size / marker), u);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return format!("{}B", size);
|
||||||
|
}
|
||||||
|
|
||||||
|
mod tests {
|
||||||
|
#[allow(unused_imports)]
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_human_readable_number() {
|
||||||
|
assert_eq!(human_readable_number(1), "1B");
|
||||||
|
assert_eq!(human_readable_number(956), "956B");
|
||||||
|
assert_eq!(human_readable_number(1004), "1004B");
|
||||||
|
assert_eq!(human_readable_number(1024), "1.0K");
|
||||||
|
assert_eq!(human_readable_number(1536), "1.5K");
|
||||||
|
assert_eq!(human_readable_number(1024 * 512), "512K");
|
||||||
|
assert_eq!(human_readable_number(1024 * 1024), "1.0M");
|
||||||
|
assert_eq!(human_readable_number(1024 * 1024 * 1024 - 1), "1023M");
|
||||||
|
assert_eq!(human_readable_number(1024 * 1024 * 1024 * 20), "20G");
|
||||||
|
assert_eq!(human_readable_number(1024 * 1024 * 1024 * 1024), "1.0T");
|
||||||
|
}
|
||||||
|
}
|
||||||
68
src/lib.rs
Normal file
68
src/lib.rs
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
use std::cmp::{Eq, Ord, Ordering, PartialEq, PartialOrd};
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
pub struct Node {
|
||||||
|
name: String,
|
||||||
|
size: u64,
|
||||||
|
children: Vec<Node>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Node {
|
||||||
|
pub fn new<S: Into<String>>(name: S, size: u64, children: Vec<Node>) -> Self {
|
||||||
|
Node {
|
||||||
|
children: children,
|
||||||
|
name: name.into(),
|
||||||
|
size: size,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn children(&self) -> &Vec<Node> {
|
||||||
|
&self.children
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn name(&self) -> &String {
|
||||||
|
&self.name
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn size(&self) -> u64 {
|
||||||
|
self.size
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Ord for Node {
|
||||||
|
fn cmp(&self, other: &Self) -> Ordering {
|
||||||
|
if self.size > other.size {
|
||||||
|
Ordering::Less
|
||||||
|
} else if self.size < other.size {
|
||||||
|
Ordering::Greater
|
||||||
|
} else {
|
||||||
|
let my_slashes = self.name.matches('/').count();
|
||||||
|
let other_slashes = other.name.matches('/').count();
|
||||||
|
|
||||||
|
if my_slashes > other_slashes {
|
||||||
|
Ordering::Greater
|
||||||
|
} else if my_slashes < other_slashes {
|
||||||
|
Ordering::Less
|
||||||
|
} else {
|
||||||
|
if self.name < other.name {
|
||||||
|
Ordering::Less
|
||||||
|
} else if self.name > other.name {
|
||||||
|
Ordering::Greater
|
||||||
|
} else {
|
||||||
|
Ordering::Equal
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl PartialOrd for Node {
|
||||||
|
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
||||||
|
Some(self.cmp(other))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl PartialEq for Node {
|
||||||
|
fn eq(&self, other: &Self) -> bool {
|
||||||
|
(&self.name, self.size) == (&other.name, other.size)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl Eq for Node {}
|
||||||
354
src/main.rs
354
src/main.rs
@@ -1,77 +1,19 @@
|
|||||||
// test:
|
|
||||||
// recursive dirs that link to each other.
|
|
||||||
// Pass in bad dir name
|
|
||||||
// num to search for is less than num available
|
|
||||||
// admin files.
|
|
||||||
//
|
|
||||||
extern crate ansi_term;
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate clap;
|
extern crate clap;
|
||||||
|
extern crate assert_cli;
|
||||||
|
extern crate dust;
|
||||||
|
|
||||||
use std::collections::HashSet;
|
use self::display::draw_it;
|
||||||
|
|
||||||
use ansi_term::Colour::Fixed;
|
|
||||||
use clap::{App, AppSettings, Arg};
|
use clap::{App, AppSettings, Arg};
|
||||||
|
use utils::{find_big_ones, get_dir_tree};
|
||||||
|
|
||||||
use std::cmp;
|
mod display;
|
||||||
use std::cmp::Ordering;
|
mod utils;
|
||||||
use std::fs;
|
|
||||||
use std::fs::ReadDir;
|
|
||||||
use std::io;
|
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
static DEFAULT_NUMBER_OF_LINES: &'static str = "15";
|
||||||
struct Node {
|
|
||||||
dir: Dir,
|
|
||||||
children: Vec<Node>,
|
|
||||||
}
|
|
||||||
impl Ord for Node {
|
|
||||||
fn cmp(&self, other: &Self) -> Ordering {
|
|
||||||
if self.dir.size > other.dir.size {
|
|
||||||
Ordering::Less
|
|
||||||
} else if self.dir.size < other.dir.size {
|
|
||||||
Ordering::Greater
|
|
||||||
} else {
|
|
||||||
let my_slashes = self.dir.name.matches("/").count();
|
|
||||||
let other_slashes = other.dir.name.matches("/").count();
|
|
||||||
|
|
||||||
if my_slashes > other_slashes {
|
|
||||||
Ordering::Greater
|
|
||||||
} else if my_slashes < other_slashes {
|
|
||||||
Ordering::Less
|
|
||||||
} else {
|
|
||||||
if self.dir.name < other.dir.name {
|
|
||||||
Ordering::Less
|
|
||||||
} else if self.dir.name > other.dir.name {
|
|
||||||
Ordering::Greater
|
|
||||||
} else {
|
|
||||||
Ordering::Equal
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl PartialOrd for Node {
|
|
||||||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
|
||||||
Some(self.cmp(other))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl PartialEq for Node {
|
|
||||||
fn eq(&self, other: &Self) -> bool {
|
|
||||||
(&self.dir.name, self.dir.size) == (&other.dir.name, other.dir.size)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl Eq for Node {}
|
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
|
||||||
struct Dir {
|
|
||||||
name: String,
|
|
||||||
size: u64,
|
|
||||||
}
|
|
||||||
|
|
||||||
static DEFAULT_NUMBER_OF_LINES: &'static str = &"15";
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let options = App::new("Trailing args example")
|
let options = App::new("Dust")
|
||||||
.setting(AppSettings::TrailingVarArg)
|
.setting(AppSettings::TrailingVarArg)
|
||||||
.arg(
|
.arg(
|
||||||
Arg::with_name("number_of_lines")
|
Arg::with_name("number_of_lines")
|
||||||
@@ -80,6 +22,11 @@ fn main() {
|
|||||||
.takes_value(true)
|
.takes_value(true)
|
||||||
.default_value(DEFAULT_NUMBER_OF_LINES),
|
.default_value(DEFAULT_NUMBER_OF_LINES),
|
||||||
)
|
)
|
||||||
|
.arg(
|
||||||
|
Arg::with_name("use_apparent_size")
|
||||||
|
.short("s")
|
||||||
|
.help("If set will use file length. Otherwise we use blocks"),
|
||||||
|
)
|
||||||
.arg(Arg::with_name("inputs").multiple(true))
|
.arg(Arg::with_name("inputs").multiple(true))
|
||||||
.get_matches();
|
.get_matches();
|
||||||
|
|
||||||
@@ -90,275 +37,12 @@ fn main() {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
let number_of_lines = value_t!(options.value_of("number_of_lines"), usize).unwrap();
|
let number_of_lines = value_t!(options.value_of("number_of_lines"), usize).unwrap();
|
||||||
|
let use_apparent_size = options.is_present("use_apparent_size");
|
||||||
|
|
||||||
let (permissions, results) = get_dir_tree(filenames);
|
let (permissions, node_per_top_level_dir) = get_dir_tree(&filenames, use_apparent_size);
|
||||||
let slice_it = find_big_ones(&results, number_of_lines);
|
let slice_it = find_big_ones(&node_per_top_level_dir, number_of_lines);
|
||||||
display(permissions, slice_it);
|
draw_it(permissions, &node_per_top_level_dir, &slice_it);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_dir_tree(filenames: Vec<&str>) -> (bool, Vec<Node>) {
|
#[cfg(test)]
|
||||||
let mut permissions = true;
|
mod tests;
|
||||||
let mut results = vec![];
|
|
||||||
for b in filenames {
|
|
||||||
let mut new_name = String::from(b);
|
|
||||||
while new_name.chars().last() == Some('/') && new_name.len() != 1 {
|
|
||||||
new_name.pop();
|
|
||||||
}
|
|
||||||
let (hp, data) = examine_dir_str(new_name);
|
|
||||||
permissions = permissions && hp;
|
|
||||||
results.push(data);
|
|
||||||
}
|
|
||||||
(permissions, results)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn examine_dir_str(loc: String) -> (bool, Node) {
|
|
||||||
let mut inodes: HashSet<u64> = HashSet::new();
|
|
||||||
let (hp, result) = examine_dir(fs::read_dir(&loc), &mut inodes);
|
|
||||||
|
|
||||||
// This needs to be folded into the below recursive call somehow
|
|
||||||
let new_size = result.iter().fold(0, |a, b| a + b.dir.size);
|
|
||||||
(
|
|
||||||
hp,
|
|
||||||
Node {
|
|
||||||
dir: Dir {
|
|
||||||
name: loc,
|
|
||||||
size: new_size,
|
|
||||||
},
|
|
||||||
children: result,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(target_os = "linux")]
|
|
||||||
fn get_metadata_blocks_and_inode(d: &std::fs::DirEntry) -> Option<(u64, u64)> {
|
|
||||||
use std::os::linux::fs::MetadataExt;
|
|
||||||
match d.metadata().ok() {
|
|
||||||
Some(md) => Some((md.len(), md.st_ino())),
|
|
||||||
None => None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(target_os = "unix")]
|
|
||||||
fn get_metadata_blocks_and_inode(d: &std::fs::DirEntry) -> Option<(u64, u64)> {
|
|
||||||
use std::os::unix::fs::MetadataExt;
|
|
||||||
match d.metadata().ok() {
|
|
||||||
Some(md) => Some((md.len(), md.ino())),
|
|
||||||
None => None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(target_os = "macos")]
|
|
||||||
fn get_metadata_blocks_and_inode(d: &std::fs::DirEntry) -> Option<(u64, u64)> {
|
|
||||||
use std::os::macos::fs::MetadataExt;
|
|
||||||
match d.metadata().ok() {
|
|
||||||
Some(md) => Some((md.len(), md.st_ino())),
|
|
||||||
None => None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(not(any(target_os = "linux", target_os = "unix", target_os = "macos")))]
|
|
||||||
fn get_metadata_blocks_and_inode(_d: &std::fs::DirEntry) -> Option<(u64, u64)> {
|
|
||||||
match _d.metadata().ok() {
|
|
||||||
Some(md) => Some((md.len(), 0)), //move to option not 0
|
|
||||||
None => None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn examine_dir(a_dir: io::Result<ReadDir>, inodes: &mut HashSet<u64>) -> (bool, Vec<Node>) {
|
|
||||||
let mut result = vec![];
|
|
||||||
let mut have_permission = true;
|
|
||||||
|
|
||||||
if a_dir.is_ok() {
|
|
||||||
let paths = a_dir.unwrap();
|
|
||||||
for dd in paths {
|
|
||||||
match dd {
|
|
||||||
Ok(d) => {
|
|
||||||
let file_type = d.file_type().ok();
|
|
||||||
let maybe_size_and_inode = get_metadata_blocks_and_inode(&d);
|
|
||||||
|
|
||||||
match (file_type, maybe_size_and_inode) {
|
|
||||||
(Some(file_type), Some((size, inode))) => {
|
|
||||||
let s = d.path().to_string_lossy().to_string();
|
|
||||||
if inodes.contains(&inode) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
inodes.insert(inode);
|
|
||||||
|
|
||||||
if d.path().is_dir() && !file_type.is_symlink() {
|
|
||||||
let (hp, recursive) = examine_dir(fs::read_dir(d.path()), inodes);
|
|
||||||
have_permission = have_permission && hp;
|
|
||||||
let new_size = recursive.iter().fold(size, |a, b| a + b.dir.size);
|
|
||||||
result.push(Node {
|
|
||||||
dir: Dir {
|
|
||||||
name: s,
|
|
||||||
size: new_size,
|
|
||||||
},
|
|
||||||
children: recursive,
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
result.push(Node {
|
|
||||||
dir: Dir {
|
|
||||||
name: s,
|
|
||||||
size: size,
|
|
||||||
},
|
|
||||||
children: vec![],
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
(_, None) => have_permission = false,
|
|
||||||
(_, _) => (),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(_) => (),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
have_permission = false;
|
|
||||||
}
|
|
||||||
(have_permission, result)
|
|
||||||
}
|
|
||||||
|
|
||||||
// We start with a list of root directories - these must be the biggest folders
|
|
||||||
// We then repeadedly merge in the children of the biggest directory - Each iteration
|
|
||||||
// the next biggest directory's children are merged in.
|
|
||||||
fn find_big_ones<'a>(l: &'a Vec<Node>, max_to_show: usize) -> Vec<&Node> {
|
|
||||||
let mut new_l: Vec<&Node> = l.iter().map(|a| a).collect();
|
|
||||||
new_l.sort();
|
|
||||||
|
|
||||||
for processed_pointer in 0..max_to_show {
|
|
||||||
if new_l.len() == processed_pointer {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
// Must be a list of pointers into new_l otherwise b_list will go out of scope
|
|
||||||
// when it is deallocated
|
|
||||||
let mut b_list: Vec<&Node> = new_l[processed_pointer]
|
|
||||||
.children
|
|
||||||
.iter()
|
|
||||||
.map(|a| a)
|
|
||||||
.collect();
|
|
||||||
new_l.extend(b_list);
|
|
||||||
new_l.sort();
|
|
||||||
/*println!(
|
|
||||||
"{:?} -------------------",
|
|
||||||
new_l
|
|
||||||
.iter()
|
|
||||||
.map(|a| a.dir.size.to_string() + ": " + &a.dir.name)
|
|
||||||
.collect::<Vec<String>>()
|
|
||||||
);*/
|
|
||||||
}
|
|
||||||
if new_l.len() > max_to_show {
|
|
||||||
new_l[0..max_to_show + 1].to_vec()
|
|
||||||
} else {
|
|
||||||
new_l
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn display(permissions: bool, to_display: Vec<&Node>) -> () {
|
|
||||||
if !permissions {
|
|
||||||
eprintln!("Did not have permissions for all directories");
|
|
||||||
}
|
|
||||||
|
|
||||||
display_node(to_display[0], &to_display, true, 1, "")
|
|
||||||
}
|
|
||||||
|
|
||||||
fn display_node<S: Into<String>>(
|
|
||||||
node_to_print: &Node,
|
|
||||||
to_display: &Vec<&Node>,
|
|
||||||
is_first: bool,
|
|
||||||
depth: u8,
|
|
||||||
indentation_str: S,
|
|
||||||
) {
|
|
||||||
let mut is = indentation_str.into();
|
|
||||||
print_this_node(node_to_print, is_first, depth, is.as_ref());
|
|
||||||
|
|
||||||
is = is.replace("└──", " ");
|
|
||||||
is = is.replace("├──", "│ ");
|
|
||||||
|
|
||||||
let printable_node_slashes = node_to_print.dir.name.matches("/").count();
|
|
||||||
|
|
||||||
let mut num_sibblings = to_display.iter().fold(0, |a, b| {
|
|
||||||
if node_to_print.children.contains(b)
|
|
||||||
&& b.dir.name.matches("/").count() == printable_node_slashes + 1
|
|
||||||
{
|
|
||||||
a + 1
|
|
||||||
} else {
|
|
||||||
a
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
let mut is_biggest = true;
|
|
||||||
for node in to_display {
|
|
||||||
if node_to_print.children.contains(node) {
|
|
||||||
if node.dir.name.matches("/").count() == printable_node_slashes + 1 {
|
|
||||||
num_sibblings -= 1;
|
|
||||||
let tree_chars = {
|
|
||||||
if num_sibblings == 0 {
|
|
||||||
"└──"
|
|
||||||
} else {
|
|
||||||
"├──"
|
|
||||||
}
|
|
||||||
};
|
|
||||||
display_node(
|
|
||||||
&node,
|
|
||||||
to_display,
|
|
||||||
is_biggest,
|
|
||||||
depth + 1,
|
|
||||||
is.to_string() + tree_chars,
|
|
||||||
);
|
|
||||||
is_biggest = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn print_this_node(node_to_print: &Node, is_biggest: bool, depth: u8, indentation_str: &str) {
|
|
||||||
let padded_size = format!("{:>5}", human_readable_number(node_to_print.dir.size),);
|
|
||||||
println!(
|
|
||||||
"{} {} {}",
|
|
||||||
if is_biggest {
|
|
||||||
Fixed(196).paint(padded_size)
|
|
||||||
} else {
|
|
||||||
Fixed(7).paint(padded_size)
|
|
||||||
},
|
|
||||||
indentation_str,
|
|
||||||
Fixed(7)
|
|
||||||
.on(Fixed(cmp::min(8, (depth) as u8) + 231))
|
|
||||||
.paint(node_to_print.dir.name.to_string())
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn human_readable_number(size: u64) -> (String) {
|
|
||||||
let units = vec!["T", "G", "M", "K"]; //make static
|
|
||||||
|
|
||||||
//return format!("{}B", size);
|
|
||||||
|
|
||||||
for (i, u) in units.iter().enumerate() {
|
|
||||||
let marker = 1024u64.pow((units.len() - i) as u32);
|
|
||||||
if size >= marker {
|
|
||||||
if size / marker < 10 {
|
|
||||||
return format!("{:.1}{}", (size as f32 / marker as f32), u);
|
|
||||||
} else {
|
|
||||||
return format!("{}{}", (size / marker), u);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return format!("{}B", size);
|
|
||||||
}
|
|
||||||
|
|
||||||
mod tests {
|
|
||||||
use super::*;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_human_readable_number() {
|
|
||||||
assert_eq!(human_readable_number(1), "1B");
|
|
||||||
assert_eq!(human_readable_number(956), "956B");
|
|
||||||
assert_eq!(human_readable_number(1004), "1004B");
|
|
||||||
assert_eq!(human_readable_number(1024), "1.0K");
|
|
||||||
assert_eq!(human_readable_number(1536), "1.5K");
|
|
||||||
assert_eq!(human_readable_number(1024 * 512), "512K");
|
|
||||||
assert_eq!(human_readable_number(1024 * 1024), "1.0M");
|
|
||||||
assert_eq!(human_readable_number(1024 * 1024 * 1024 - 1), "1023M");
|
|
||||||
assert_eq!(human_readable_number(1024 * 1024 * 1024 * 20), "20G");
|
|
||||||
assert_eq!(human_readable_number(1024 * 1024 * 1024 * 1024), "1.0T");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
0
src/test_dir/many/a_file
Normal file
0
src/test_dir/many/a_file
Normal file
1
src/test_dir/many/hello_file
Normal file
1
src/test_dir/many/hello_file
Normal file
@@ -0,0 +1 @@
|
|||||||
|
hello
|
||||||
140
src/tests.rs
Normal file
140
src/tests.rs
Normal file
@@ -0,0 +1,140 @@
|
|||||||
|
extern crate ansi_term;
|
||||||
|
extern crate tempfile;
|
||||||
|
use self::tempfile::Builder;
|
||||||
|
use self::tempfile::TempDir;
|
||||||
|
use super::*;
|
||||||
|
use display::format_string;
|
||||||
|
use std::fs::File;
|
||||||
|
use std::io::Write;
|
||||||
|
use std::path::PathBuf;
|
||||||
|
use std::process::Command;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
pub fn test_main() {
|
||||||
|
let r = format!(
|
||||||
|
"{}
|
||||||
|
{}
|
||||||
|
{}
|
||||||
|
{}",
|
||||||
|
format_string("src/test_dir", true, " 4.0K", ""),
|
||||||
|
format_string("src/test_dir/many", true, " 4.0K", "└─┬",),
|
||||||
|
format_string("src/test_dir/many/hello_file", true, " 4.0K", " ├──",),
|
||||||
|
format_string("src/test_dir/many/a_file", false, " 0B", " └──",),
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_cli::Assert::main_binary()
|
||||||
|
.with_args(&["src/test_dir"])
|
||||||
|
.stdout()
|
||||||
|
.is(r)
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
pub fn test_apparent_size() {
|
||||||
|
let r = format!(
|
||||||
|
"{}",
|
||||||
|
format_string("src/test_dir/many/hello_file", true, " 6B", " ├──",),
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_cli::Assert::main_binary()
|
||||||
|
.with_args(&["-s", "src/test_dir"])
|
||||||
|
.stdout()
|
||||||
|
.contains(r)
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn build_temp_file(dir: &TempDir) -> (PathBuf) {
|
||||||
|
let file_path = dir.path().join("notes.txt");
|
||||||
|
let mut file = File::create(&file_path).unwrap();
|
||||||
|
writeln!(file, "I am a temp file").unwrap();
|
||||||
|
file_path
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
pub fn test_soft_sym_link() {
|
||||||
|
let dir = Builder::new().tempdir().unwrap();
|
||||||
|
let file = build_temp_file(&dir);
|
||||||
|
let dir_s = dir.path().to_str().unwrap();
|
||||||
|
let file_path_s = file.to_str().unwrap();
|
||||||
|
|
||||||
|
let link_name = dir.path().join("the_link");
|
||||||
|
let link_name_s = link_name.to_str().unwrap();
|
||||||
|
let c = Command::new("ln")
|
||||||
|
.arg("-s")
|
||||||
|
.arg(file_path_s)
|
||||||
|
.arg(link_name_s)
|
||||||
|
.output();
|
||||||
|
assert!(c.is_ok());
|
||||||
|
|
||||||
|
let r = format!(
|
||||||
|
"{}
|
||||||
|
{}
|
||||||
|
{}",
|
||||||
|
format_string(dir_s, true, " 8.0K", ""),
|
||||||
|
format_string(file_path_s, true, " 4.0K", "├──",),
|
||||||
|
format_string(link_name_s, false, " 4.0K", "└──",),
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_cli::Assert::main_binary()
|
||||||
|
.with_args(&[dir_s])
|
||||||
|
.stdout()
|
||||||
|
.contains(r)
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Hard links are ignored as the inode is the same as the file
|
||||||
|
#[test]
|
||||||
|
pub fn test_hard_sym_link() {
|
||||||
|
let dir = Builder::new().tempdir().unwrap();
|
||||||
|
let file = build_temp_file(&dir);
|
||||||
|
let dir_s = dir.path().to_str().unwrap();
|
||||||
|
let file_path_s = file.to_str().unwrap();
|
||||||
|
|
||||||
|
let link_name = dir.path().join("the_link");
|
||||||
|
let link_name_s = link_name.to_str().unwrap();
|
||||||
|
let c = Command::new("ln")
|
||||||
|
.arg(file_path_s)
|
||||||
|
.arg(link_name_s)
|
||||||
|
.output();
|
||||||
|
assert!(c.is_ok());
|
||||||
|
|
||||||
|
let r = format!(
|
||||||
|
"{}
|
||||||
|
{}",
|
||||||
|
format_string(dir_s, true, " 4.0K", ""),
|
||||||
|
format_string(file_path_s, true, " 4.0K", "└──")
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_cli::Assert::main_binary()
|
||||||
|
.with_args(&[dir_s])
|
||||||
|
.stdout()
|
||||||
|
.contains(r)
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
//Check we don't recurse down an infinite symlink tree
|
||||||
|
#[test]
|
||||||
|
pub fn test_recursive_sym_link() {
|
||||||
|
let dir = Builder::new().tempdir().unwrap();
|
||||||
|
let dir_s = dir.path().to_str().unwrap();
|
||||||
|
|
||||||
|
let link_name = dir.path().join("the_link");
|
||||||
|
let link_name_s = link_name.to_str().unwrap();
|
||||||
|
|
||||||
|
let c = Command::new("cd").arg(dir_s).output();
|
||||||
|
assert!(c.is_ok());
|
||||||
|
let c = Command::new("ln")
|
||||||
|
.arg("-s")
|
||||||
|
.arg(".")
|
||||||
|
.arg(link_name_s)
|
||||||
|
.output();
|
||||||
|
assert!(c.is_ok());
|
||||||
|
|
||||||
|
let r = format!("{}", format_string(dir_s, true, " 4.0K", ""));
|
||||||
|
|
||||||
|
assert_cli::Assert::main_binary()
|
||||||
|
.with_args(&[dir_s])
|
||||||
|
.stdout()
|
||||||
|
.contains(r)
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
113
src/utils/mod.rs
Normal file
113
src/utils/mod.rs
Normal file
@@ -0,0 +1,113 @@
|
|||||||
|
use std::collections::HashSet;
|
||||||
|
|
||||||
|
use std::fs::{self, ReadDir};
|
||||||
|
use std::io;
|
||||||
|
|
||||||
|
use dust::Node;
|
||||||
|
|
||||||
|
mod platform;
|
||||||
|
use self::platform::*;
|
||||||
|
|
||||||
|
pub fn get_dir_tree(filenames: &Vec<&str>, apparent_size: bool) -> (bool, Vec<Node>) {
|
||||||
|
let mut permissions = true;
|
||||||
|
let mut results = vec![];
|
||||||
|
for &b in filenames {
|
||||||
|
let mut new_name = String::from(b);
|
||||||
|
while new_name.chars().last() == Some('/') && new_name.len() != 1 {
|
||||||
|
new_name.pop();
|
||||||
|
}
|
||||||
|
let (hp, data) = examine_dir_str(&new_name, apparent_size);
|
||||||
|
permissions = permissions && hp;
|
||||||
|
results.push(data);
|
||||||
|
}
|
||||||
|
(permissions, results)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn examine_dir_str(loc: &str, apparent_size: bool) -> (bool, Node) {
|
||||||
|
let mut inodes: HashSet<(u64, u64)> = HashSet::new();
|
||||||
|
let (hp, result) = examine_dir(fs::read_dir(loc), apparent_size, &mut inodes);
|
||||||
|
|
||||||
|
// This needs to be folded into the below recursive call somehow
|
||||||
|
let new_size = result.iter().fold(0, |a, b| a + b.size());
|
||||||
|
(hp, Node::new(loc, new_size, result))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn examine_dir(
|
||||||
|
a_dir: io::Result<ReadDir>,
|
||||||
|
apparent_size: bool,
|
||||||
|
inodes: &mut HashSet<(u64, u64)>,
|
||||||
|
) -> (bool, Vec<Node>) {
|
||||||
|
let mut result = vec![];
|
||||||
|
let mut have_permission = true;
|
||||||
|
|
||||||
|
if a_dir.is_ok() {
|
||||||
|
let paths = a_dir.unwrap();
|
||||||
|
for dd in paths {
|
||||||
|
match dd {
|
||||||
|
Ok(d) => {
|
||||||
|
let file_type = d.file_type().ok();
|
||||||
|
let maybe_size_and_inode = get_metadata(&d, apparent_size);
|
||||||
|
|
||||||
|
match (file_type, maybe_size_and_inode) {
|
||||||
|
(Some(file_type), Some((size, inode))) => {
|
||||||
|
let s = d.path().to_string_lossy().to_string();
|
||||||
|
if !apparent_size {
|
||||||
|
if let Some(inode_dev_pair) = inode {
|
||||||
|
if inodes.contains(&inode_dev_pair) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
inodes.insert(inode_dev_pair);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if d.path().is_dir() && !file_type.is_symlink() {
|
||||||
|
let (hp, recursive) =
|
||||||
|
examine_dir(fs::read_dir(d.path()), apparent_size, inodes);
|
||||||
|
have_permission = have_permission && hp;
|
||||||
|
let new_size = recursive.iter().fold(size, |a, b| a + b.size());
|
||||||
|
result.push(Node::new(s, new_size, recursive))
|
||||||
|
} else {
|
||||||
|
result.push(Node::new(s, size, vec![]))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
(_, None) => have_permission = false,
|
||||||
|
(_, _) => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(_) => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
have_permission = false;
|
||||||
|
}
|
||||||
|
(have_permission, result)
|
||||||
|
}
|
||||||
|
|
||||||
|
// We start with a list of root directories - these must be the biggest folders
|
||||||
|
// We then repeadedly merge in the children of the biggest directory - Each iteration
|
||||||
|
// the next biggest directory's children are merged in.
|
||||||
|
pub fn find_big_ones<'a>(l: &'a Vec<Node>, max_to_show: usize) -> Vec<&Node> {
|
||||||
|
let mut new_l: Vec<&Node> = l.iter().map(|a| a).collect();
|
||||||
|
new_l.sort();
|
||||||
|
|
||||||
|
for processed_pointer in 0..max_to_show {
|
||||||
|
if new_l.len() == processed_pointer {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
// Must be a list of pointers into new_l otherwise b_list will go out of scope
|
||||||
|
// when it is deallocated
|
||||||
|
let mut b_list: Vec<&Node> = new_l[processed_pointer]
|
||||||
|
.children()
|
||||||
|
.iter()
|
||||||
|
.map(|a| a)
|
||||||
|
.collect();
|
||||||
|
new_l.extend(b_list);
|
||||||
|
new_l.sort();
|
||||||
|
}
|
||||||
|
|
||||||
|
if new_l.len() > max_to_show {
|
||||||
|
new_l[0..max_to_show + 1].to_vec()
|
||||||
|
} else {
|
||||||
|
new_l
|
||||||
|
}
|
||||||
|
}
|
||||||
72
src/utils/platform.rs
Normal file
72
src/utils/platform.rs
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
use std;
|
||||||
|
|
||||||
|
fn get_block_size() -> u64 {
|
||||||
|
// All os specific implementations of MetatdataExt seem to define a block as 512 bytes
|
||||||
|
// https://doc.rust-lang.org/std/os/linux/fs/trait.MetadataExt.html#tymethod.st_blocks
|
||||||
|
512
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(target_os = "linux")]
|
||||||
|
pub fn get_metadata(
|
||||||
|
d: &std::fs::DirEntry,
|
||||||
|
use_apparent_size: bool,
|
||||||
|
) -> Option<(u64, Option<(u64, u64)>)> {
|
||||||
|
use std::os::linux::fs::MetadataExt;
|
||||||
|
match d.metadata().ok() {
|
||||||
|
Some(md) => {
|
||||||
|
let inode = Some((md.st_ino(), md.st_dev()));
|
||||||
|
if use_apparent_size {
|
||||||
|
Some((md.len(), inode))
|
||||||
|
} else {
|
||||||
|
Some((md.st_blocks() * get_block_size(), inode))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(target_os = "unix")]
|
||||||
|
pub fn get_metadata(
|
||||||
|
d: &std::fs::DirEntry,
|
||||||
|
use_apparent_size: bool,
|
||||||
|
) -> Option<(u64, Option<(u64, u64)>)> {
|
||||||
|
use std::os::unix::fs::MetadataExt;
|
||||||
|
match d.metadata().ok() {
|
||||||
|
Some(md) => {
|
||||||
|
let inode = Some((md.ino(), md.dev()));
|
||||||
|
if use_apparent_size {
|
||||||
|
Some((md.len(), inode))
|
||||||
|
} else {
|
||||||
|
Some((md.blocks() * get_block_size(), inode))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(target_os = "macos")]
|
||||||
|
pub fn get_metadata(
|
||||||
|
d: &std::fs::DirEntry,
|
||||||
|
use_apparent_size: bool,
|
||||||
|
) -> Option<(u64, Option<(u64, u64)>)> {
|
||||||
|
use std::os::macos::fs::MetadataExt;
|
||||||
|
match d.metadata().ok() {
|
||||||
|
Some(md) => {
|
||||||
|
let inode = Some((md.st_ino(), md.st_dev()));
|
||||||
|
if use_apparent_size {
|
||||||
|
Some((md.len(), inode))
|
||||||
|
} else {
|
||||||
|
Some((md.st_blocks() * get_block_size(), inode))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(not(any(target_os = "linux", target_os = "unix", target_os = "macos")))]
|
||||||
|
pub fn get_metadata(d: &std::fs::DirEntry, _apparent: bool) -> Option<(u64, Option<(u64, u64)>)> {
|
||||||
|
match d.metadata().ok() {
|
||||||
|
Some(md) => Some((md.len(), None)),
|
||||||
|
None => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user