Commit 22d61940 authored by Stefan Schindler's avatar Stefan Schindler
Browse files

Initialer Commit

parents
[root]
name = "whipe_buddy"
version = "0.1.0"
[package]
name = "whipe_buddy"
version = "0.1.0"
authors = ["stefschi"]
[dependencies]
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
int
main(int argc, char *argv[])
{
struct stat sb;
if (argc != 2) {
fprintf(stderr, "Usage: %s <pathname>\n", argv[0]);
exit(EXIT_FAILURE);
}
if (stat(argv[1], &sb) == -1) {
perror("stat");
exit(EXIT_FAILURE);
}
printf("File type: ");
switch (sb.st_mode & S_IFMT) {
case S_IFBLK: printf("block device\n"); break;
case S_IFCHR: printf("character device\n"); break;
case S_IFDIR: printf("directory\n"); break;
case S_IFIFO: printf("FIFO/pipe\n"); break;
case S_IFLNK: printf("symlink\n"); break;
case S_IFREG: printf("regular file\n"); break;
case S_IFSOCK: printf("socket\n"); break;
default: printf("unknown?\n"); break;
}
printf("I-node number: %ld\n", (long) sb.st_ino);
printf("Mode: %lo (octal)\n",
(unsigned long) sb.st_mode);
printf("Link count: %ld\n", (long) sb.st_nlink);
printf("Ownership: UID=%ld GID=%ld\n",
(long) sb.st_uid, (long) sb.st_gid);
printf("Preferred I/O block size: %ld bytes\n",
(long) sb.st_blksize);
printf("File size: %lld bytes\n",
(long long) sb.st_size);
printf("Blocks allocated: %lld\n",
(long long) sb.st_blocks);
printf("Last status change: %s", ctime(&sb.st_ctime));
printf("Last file access: %s", ctime(&sb.st_atime));
printf("Last file modification: %s", ctime(&sb.st_mtime));
exit(EXIT_SUCCESS);
}
use std::io;
use std::io::prelude::*;
use std::io::BufReader;
use std::fs::{self, File};
use std::fmt::Display;
fn main() {
let model = get_file_contents("/sys/devices/virtual/dmi/id/product_version");
println!("\n\n whipe buddy running on {}\n", model);
let disks = parse_disks();
list_disks(&disks);
// TODO interactive
return;
loop {
println!("\nPress q to exit");
let input = {
let mut input = String::new();
io::stdin().read_line(&mut input).unwrap();
input
};
match input.trim() {
"q" => return,
"l" => list_disks(&disks),
com => println!("unknown command: {}", com),
}
}
}
fn list_disks(disks: &Vec<DiskInformation>) {
println!("Found following disks:");
for disk in disks {
println!(" {}: {}", disk.name, disk.size);
}
}
fn parse_disks() -> Vec<DiskInformation> {
let partitions = get_file_contents("/proc/partitions");
//println!("partitions: {:?}", partitions);
let lines = partitions.split("\n").skip(2);
//println!("lines: {:?}", lines);
let mut disks = Vec::new();
for line in lines {
let line = line.trim();
//println!("line: {:?}", line);
let mut parts = line.split_whitespace();
let (_major, minor, no_blocks, name) = (parts.next(), parts.next(), parts.next(), parts.next());
if let Some(name) = name {
if minor == Some("0") && (name.starts_with("sd") || name.starts_with("nvme") || name.starts_with("xvd")) {
/// Contains the major and minor numbers of each partition as well as the number of 1024-byte blocks and the partition name.
let no_blocks: u64 = no_blocks.unwrap_or("0").parse().unwrap_or(0);
disks.push(DiskInformation {
name: name.into(),
size: KiloByte(no_blocks),
});
}
}
}
disks
}
fn get_file_contents(path: &str) -> String {
let f = match File::open(path) {
Ok(f) => f,
Err(_) => return format!("<unable to open {:?}", path),
};
let reader = BufReader::new(f);
let mut buffer = String::new();
let mut i = 0;
for line in reader.lines() {
i += 1;
buffer += &* line.unwrap_or( format!("<read error on line {} - {}>", i, path) );
buffer += "\n";
}
buffer
}
#[derive(Debug)]
struct DiskInformation {
size: ByteUnit,
name: String,
}
#[derive(Debug)]
enum ByteUnit {
//Byte(u64),
KiloByte(u64),
}
use ByteUnit::*;
impl Display for ByteUnit {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
&KiloByte(size) => {
if size < 1600 {
return write!(f, "{:>7.2} KB", size);
}
let size = size as f64;
let size: f64 = size / 1024.0;
if size < 1600.0 {
return write!(f, "{:>7.2} MB", size);
}
let size: f64 = size / 1024.0;
if size < 1600.0 {
return write!(f, "{:>7.2} GB", size);
}
let size: f64 = size / 1024.0;
write!(f, "{:>7.2} TB", size)
}
}
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment