2021-02-14 13:58:46 +00:00
|
|
|
use std::io::{self, Write};
|
|
|
|
#[cfg(unix)]
|
|
|
|
use std::os::unix::ffi::OsStrExt;
|
2021-02-14 14:25:32 +00:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
|
|
|
use snailquote::escape;
|
|
|
|
|
|
|
|
use crate::scanerror::ScanError;
|
2021-02-18 09:50:22 +00:00
|
|
|
use crate::Findings;
|
2021-02-14 14:25:32 +00:00
|
|
|
|
|
|
|
const VERSION: Option<&'static str> = option_env!("CARGO_PKG_VERSION");
|
2021-02-14 13:58:46 +00:00
|
|
|
|
2021-02-10 09:20:22 +00:00
|
|
|
type Entries = [Result<Findings, (ScanError, PathBuf)>];
|
|
|
|
|
2021-02-21 14:46:51 +00:00
|
|
|
enum Writable<'a> {
|
|
|
|
String(&'a str),
|
|
|
|
Path(&'a PathBuf),
|
|
|
|
Space,
|
2021-02-21 15:55:27 +00:00
|
|
|
Newline,
|
2021-02-21 14:46:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// the lifetime of a lifetime
|
|
|
|
impl<'a> From<&'a str> for Writable<'a> {
|
|
|
|
fn from(s: &'a str) -> Writable<'a> {
|
|
|
|
Writable::String(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> From<&'a PathBuf> for Writable<'a> {
|
|
|
|
fn from(p: &'a PathBuf) -> Writable<'a> {
|
|
|
|
Writable::Path(p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn smart_write<W: Write>(f: &mut W, writeables: &[Writable]) -> io::Result<()> {
|
|
|
|
// ehhhh
|
|
|
|
for writeable in writeables {
|
|
|
|
match writeable {
|
|
|
|
Writable::Space => write!(f, " ")?,
|
|
|
|
Writable::Newline => writeln!(f, )?,
|
|
|
|
Writable::String(s) => write!(f, "{}", s)?,
|
|
|
|
Writable::Path(path) => {
|
2021-02-21 15:55:27 +00:00
|
|
|
if let Some(string) = path.to_str() {
|
|
|
|
write!(f, "{}", escape(string))?
|
|
|
|
} else {
|
|
|
|
write!(f, "'''")?;
|
|
|
|
#[cfg(unix)]
|
2021-02-21 14:46:51 +00:00
|
|
|
f.write_all(&*path.as_os_str().as_bytes())?;
|
2021-02-21 15:55:27 +00:00
|
|
|
#[cfg(windows)]
|
|
|
|
write!(f, "{}", path.as_os_str().to_string_lossy())?; // TODO: implement bonked strings for windows
|
|
|
|
// f.write_all(&*path.as_os_str().encode_wide().collect::<Vec<u16>>())?;
|
|
|
|
write!(f, "'''")?
|
2021-02-21 14:46:51 +00:00
|
|
|
}
|
|
|
|
}
|
2021-02-14 13:58:46 +00:00
|
|
|
}
|
|
|
|
}
|
2021-02-21 14:46:51 +00:00
|
|
|
Ok(())
|
2021-02-14 13:58:46 +00:00
|
|
|
}
|
|
|
|
|
2021-02-10 09:20:22 +00:00
|
|
|
pub trait Format {
|
|
|
|
fn new() -> Self;
|
2021-02-14 13:58:46 +00:00
|
|
|
fn rename<W: Write>(&self, f: &mut W, from: &PathBuf, to: &PathBuf) -> io::Result<()>;
|
|
|
|
fn no_known_extension<W: Write>(&self, f: &mut W, path: &PathBuf) -> io::Result<()>;
|
|
|
|
fn unreadable<W: Write>(&self, f: &mut W, path: &PathBuf) -> io::Result<()>;
|
|
|
|
fn unknown_type<W: Write>(&self, f: &mut W, path: &PathBuf) -> io::Result<()>;
|
2021-02-14 14:25:32 +00:00
|
|
|
fn header<W: Write>(&self, entries: &Entries, f: &mut W) -> io::Result<()>;
|
|
|
|
fn footer<W: Write>(&self, entries: &Entries, f: &mut W) -> io::Result<()>;
|
2021-02-10 09:20:22 +00:00
|
|
|
|
2021-02-14 13:58:46 +00:00
|
|
|
fn write_all<W: Write>(&self, entries: &Entries, f: &mut W) -> io::Result<()> {
|
2021-02-21 14:46:51 +00:00
|
|
|
// TODO: clean this up - it's kinda messy
|
2021-02-14 14:25:32 +00:00
|
|
|
self.header(entries, f)?;
|
|
|
|
|
2021-02-10 09:20:22 +00:00
|
|
|
for entry in entries {
|
|
|
|
match entry {
|
|
|
|
Ok(finding) => {
|
2021-02-21 14:46:51 +00:00
|
|
|
if let Some(ext) = finding.recommended_extension() {
|
|
|
|
self.rename(f, &finding.file, &finding.file.with_extension(ext.as_str()))?
|
|
|
|
} else {
|
|
|
|
self.no_known_extension(f, &finding.file)?
|
2021-02-10 09:20:22 +00:00
|
|
|
}
|
|
|
|
}
|
2021-02-21 14:46:51 +00:00
|
|
|
|
2021-02-10 09:20:22 +00:00
|
|
|
Err(error) => {
|
|
|
|
// something went wrong 0uo
|
|
|
|
match error.0 {
|
|
|
|
// failed to read the file
|
2021-02-14 13:58:46 +00:00
|
|
|
ScanError::File => self.unreadable(f, &error.1)?,
|
2021-02-10 09:20:22 +00:00
|
|
|
// file was read successfully, but we couldn't determine a mimetype
|
2021-02-18 09:48:38 +00:00
|
|
|
ScanError::Mime => self.unknown_type(f, &error.1)?,
|
2021-02-10 09:20:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-02-14 14:25:32 +00:00
|
|
|
|
|
|
|
self.footer(entries, f)
|
2021-02-10 09:20:22 +00:00
|
|
|
}
|
2021-02-06 11:51:20 +00:00
|
|
|
}
|
|
|
|
|
2021-02-18 11:43:24 +00:00
|
|
|
// TODO: maybe make a batch script version for windows
|
2021-02-10 09:20:22 +00:00
|
|
|
pub struct Script {}
|
2021-02-14 17:12:27 +00:00
|
|
|
|
2021-02-06 11:51:20 +00:00
|
|
|
impl Format for Script {
|
2021-02-18 09:48:38 +00:00
|
|
|
fn new() -> Self {
|
|
|
|
Self {}
|
|
|
|
}
|
2021-02-10 09:20:22 +00:00
|
|
|
|
2021-02-14 13:58:46 +00:00
|
|
|
fn rename<W: Write>(&self, f: &mut W, from: &PathBuf, to: &PathBuf) -> io::Result<()> {
|
2021-02-21 14:46:51 +00:00
|
|
|
smart_write(f, &[
|
|
|
|
"mv -v -i -- ".into(),
|
|
|
|
from.into(),
|
|
|
|
Writable::Space,
|
|
|
|
to.into(),
|
|
|
|
Writable::Newline
|
|
|
|
])
|
2021-02-10 09:20:22 +00:00
|
|
|
}
|
|
|
|
|
2021-02-14 13:58:46 +00:00
|
|
|
fn no_known_extension<W: Write>(&self, f: &mut W, path: &PathBuf) -> io::Result<()> {
|
2021-02-21 14:46:51 +00:00
|
|
|
smart_write(f, &[
|
|
|
|
"echo No known extension for ".into(),
|
|
|
|
path.into(),
|
|
|
|
Writable::Newline
|
|
|
|
])
|
2021-02-06 11:51:20 +00:00
|
|
|
}
|
|
|
|
|
2021-02-14 13:58:46 +00:00
|
|
|
fn unreadable<W: Write>(&self, f: &mut W, path: &PathBuf) -> io::Result<()> {
|
2021-02-21 14:46:51 +00:00
|
|
|
smart_write(f, &[
|
|
|
|
"# Failed to read ".into(),
|
|
|
|
path.into(),
|
|
|
|
Writable::Newline
|
|
|
|
])
|
2021-02-06 11:51:20 +00:00
|
|
|
}
|
|
|
|
|
2021-02-14 13:58:46 +00:00
|
|
|
fn unknown_type<W: Write>(&self, f: &mut W, path: &PathBuf) -> io::Result<()> {
|
2021-02-21 14:46:51 +00:00
|
|
|
smart_write(f, &[
|
|
|
|
"# Failed to detect mime type for ".into(),
|
|
|
|
path.into(),
|
|
|
|
Writable::Newline
|
|
|
|
])
|
2021-02-06 11:51:20 +00:00
|
|
|
}
|
2021-02-14 14:25:32 +00:00
|
|
|
|
|
|
|
fn header<W: Write>(&self, _: &Entries, f: &mut W) -> io::Result<()> {
|
2021-02-18 09:48:38 +00:00
|
|
|
write!(
|
|
|
|
f,
|
2021-02-18 11:43:24 +00:00
|
|
|
"#!/usr/bin/env sh\n# Generated by fif {}.\n\nset -e\n\n",
|
2021-02-18 09:48:38 +00:00
|
|
|
VERSION.unwrap_or("???")
|
|
|
|
)
|
2021-02-14 14:25:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn footer<W: Write>(&self, _: &Entries, f: &mut W) -> io::Result<()> {
|
2021-02-21 11:30:58 +00:00
|
|
|
writeln!(f, "\necho 'Done.'")
|
2021-02-14 14:25:32 +00:00
|
|
|
}
|
2021-02-18 09:48:38 +00:00
|
|
|
}
|