From cf5dfc452e45df897ced480c695c202dc2f6f3ee Mon Sep 17 00:00:00 2001 From: Jean SIMARD Date: Wed, 22 Apr 2020 15:51:59 +0200 Subject: [PATCH] [feature] Add 'ntfs2ntfs' binary --- Cargo.toml | 1 + ntfs2ntfs/Cargo.toml | 24 +++++++++++++ ntfs2ntfs/README.md | 24 +++++++++++++ ntfs2ntfs/src/main.rs | 84 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 133 insertions(+) create mode 100644 ntfs2ntfs/Cargo.toml create mode 100644 ntfs2ntfs/README.md create mode 100644 ntfs2ntfs/src/main.rs diff --git a/Cargo.toml b/Cargo.toml index efc954205..23458ac16 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,6 +28,7 @@ members = [ "gtfs2ntfs", "ntfs2gtfs", "ntfs2netexfr", + "ntfs2ntfs", ] [features] diff --git a/ntfs2ntfs/Cargo.toml b/ntfs2ntfs/Cargo.toml new file mode 100644 index 000000000..7d093d6ce --- /dev/null +++ b/ntfs2ntfs/Cargo.toml @@ -0,0 +1,24 @@ +[package] +name = "ntfs2ntfs" +version = "1.0.0" +authors = ["Kisio Digital "] +license = "AGPL-3.0-only" +description = "Binary to check and clean a NTFS" +edition = "2018" +homepage = "https://github.com/CanalTP/transit_model" +readme = "README.md" +categories = ["command-line-utilities", "data-structures", "encoding", "parser-implementations"] +keywords = ["ntfs", "transit"] + +[dependencies] +chrono = "0.4" +failure = "0.1" +log = "0.4" +slog = "2.5" +slog-async = "2.3" +slog-envlogger = "2.1" +slog-scope = "4.1" +slog-stdlog = "4.0" +slog-term = "2.4" +structopt = "0.3" +transit_model = { version = "0.17", path = "../" } diff --git a/ntfs2ntfs/README.md b/ntfs2ntfs/README.md new file mode 100644 index 000000000..57d52369e --- /dev/null +++ b/ntfs2ntfs/README.md @@ -0,0 +1,24 @@ +`ntfs2ntfs` +===== + +Command-Line Interface to check and clean a [NTFS] data format into data format. + +[NTFS]: https://github.com/CanalTP/ntfs-specification/blob/master/ntfs_fr.md + +# Installation +To install, use + +```bash +cargo install ntfs2ntfs +``` + +# Usage + +```bash +ntfs2ntfs --input /path/to/ntfs/folder/ --output /path/to/ntfs/ +``` + +- `--input` is the path to a folder containing NTFS data format +- `--output` is the path to a folder where the NTFS will be exported + +Get more information about the available options with `--help`. diff --git a/ntfs2ntfs/src/main.rs b/ntfs2ntfs/src/main.rs new file mode 100644 index 000000000..ecb31b8c2 --- /dev/null +++ b/ntfs2ntfs/src/main.rs @@ -0,0 +1,84 @@ +// Copyright 2017 Kisio Digital and/or its affiliates. +// +// This program is free software: you can redistribute it and/or +// modify it under the terms of the GNU General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see +// . + +use chrono::{DateTime, FixedOffset}; +use log::info; +use slog::{slog_o, Drain}; +use slog_async::OverflowStrategy; +use std::path::PathBuf; +use structopt::StructOpt; +use transit_model::Result; + +#[derive(Debug, StructOpt)] +#[structopt(name = "ntfs2ntfs", about = "Convert an NTFS to an NTFS.")] +struct Opt { + /// input directory. + #[structopt(short = "i", long = "input", parse(from_os_str), default_value = ".")] + input: PathBuf, + + /// output directory + #[structopt(short = "o", long = "output", parse(from_os_str))] + output: Option, + + /// current datetime + #[structopt( + short = "x", + long, + parse(try_from_str), + default_value = &transit_model::CURRENT_DATETIME + )] + current_datetime: DateTime, +} + +fn init_logger() -> slog_scope::GlobalLoggerGuard { + let decorator = slog_term::TermDecorator::new().stdout().build(); + let drain = slog_term::CompactFormat::new(decorator).build().fuse(); + let mut builder = slog_envlogger::LogBuilder::new(drain).filter(None, slog::FilterLevel::Info); + if let Ok(s) = std::env::var("RUST_LOG") { + builder = builder.parse(&s); + } + let drain = slog_async::Async::new(builder.build()) + .chan_size(256) // Double the default size + .overflow_strategy(OverflowStrategy::Block) + .build() + .fuse(); + let logger = slog::Logger::root(drain, slog_o!()); + + let scope_guard = slog_scope::set_global_logger(logger); + slog_stdlog::init().unwrap(); + scope_guard +} + +fn run(opt: Opt) -> Result<()> { + info!("Launching ntfs2ntfs..."); + + let objects = transit_model::ntfs::read(opt.input)?; + + if let Some(output) = opt.output { + transit_model::ntfs::write(&objects, output, opt.current_datetime)?; + } + Ok(()) +} + +fn main() { + let _log_guard = init_logger(); + if let Err(err) = run(Opt::from_args()) { + for cause in err.iter_chain() { + eprintln!("{}", cause); + } + std::process::exit(1); + } +}