2017-09-30 23:13:50 +00:00
|
|
|
//
|
|
|
|
// imag - the personal information management suite for the commandline
|
2019-01-03 01:32:07 +00:00
|
|
|
// Copyright (C) 2015-2019 Matthias Beyer <mail@beyermatthias.de> and contributors
|
2017-09-30 23:13:50 +00:00
|
|
|
//
|
|
|
|
// This library is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
|
|
|
// License as published by the Free Software Foundation; version
|
|
|
|
// 2.1 of the License.
|
|
|
|
//
|
|
|
|
// This library 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
|
|
|
|
// Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public
|
|
|
|
// License along with this library; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
//
|
|
|
|
|
2018-09-27 06:13:58 +00:00
|
|
|
#![forbid(unsafe_code)]
|
|
|
|
|
2017-09-30 23:13:50 +00:00
|
|
|
#![deny(
|
|
|
|
non_camel_case_types,
|
|
|
|
non_snake_case,
|
|
|
|
path_statements,
|
|
|
|
trivial_numeric_casts,
|
|
|
|
unstable_features,
|
|
|
|
unused_allocation,
|
|
|
|
unused_import_braces,
|
|
|
|
unused_imports,
|
|
|
|
unused_must_use,
|
|
|
|
unused_mut,
|
|
|
|
unused_qualifications,
|
|
|
|
while_true,
|
|
|
|
)]
|
|
|
|
|
|
|
|
#[macro_use] extern crate log;
|
|
|
|
extern crate clap;
|
|
|
|
|
2018-01-31 10:09:00 +00:00
|
|
|
#[macro_use] extern crate libimagrt;
|
2017-09-30 23:13:50 +00:00
|
|
|
extern crate libimagstore;
|
|
|
|
extern crate libimagerror;
|
2018-02-06 20:34:33 +00:00
|
|
|
extern crate libimagentrylink;
|
|
|
|
|
|
|
|
use std::process::exit;
|
2017-09-30 23:13:50 +00:00
|
|
|
|
|
|
|
mod ui;
|
2019-02-28 16:28:32 +00:00
|
|
|
use crate::ui::build_ui;
|
2017-09-30 23:13:50 +00:00
|
|
|
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
|
|
|
use libimagrt::setup::generate_runtime_setup;
|
|
|
|
use libimagerror::trace::MapErrTrace;
|
2018-04-30 17:24:05 +00:00
|
|
|
use libimagerror::iter::TraceIterator;
|
2019-02-03 18:53:50 +00:00
|
|
|
use libimagerror::exit::ExitUnwrap;
|
2017-09-30 23:13:50 +00:00
|
|
|
use libimagstore::storeid::StoreId;
|
2018-02-06 20:34:33 +00:00
|
|
|
use libimagstore::store::Store;
|
|
|
|
use libimagstore::store::FileLockEntry;
|
2019-06-21 19:38:50 +00:00
|
|
|
use libimagentrylink::linkable::Linkable;
|
2018-02-06 20:34:33 +00:00
|
|
|
use libimagstore::iter::get::StoreIdGetIteratorExtension;
|
2017-09-30 23:13:50 +00:00
|
|
|
|
|
|
|
fn main() {
|
2018-01-31 10:09:00 +00:00
|
|
|
let version = make_imag_version!();
|
2017-09-30 23:13:50 +00:00
|
|
|
let rt = generate_runtime_setup("imag-mv",
|
2018-01-31 10:09:00 +00:00
|
|
|
&version,
|
2017-09-30 23:13:50 +00:00
|
|
|
"Move things around in the store",
|
|
|
|
build_ui);
|
|
|
|
|
|
|
|
debug!("mv");
|
|
|
|
|
|
|
|
let sourcename = rt
|
|
|
|
.cli()
|
|
|
|
.value_of("source")
|
|
|
|
.map(PathBuf::from)
|
2018-12-05 18:01:42 +00:00
|
|
|
.map(StoreId::new)
|
2017-09-30 23:13:50 +00:00
|
|
|
.unwrap() // unwrap safe by clap
|
2019-02-05 00:37:32 +00:00
|
|
|
.map_err_trace_exit_unwrap();
|
2017-09-30 23:13:50 +00:00
|
|
|
|
|
|
|
let destname = rt
|
|
|
|
.cli()
|
|
|
|
.value_of("dest")
|
|
|
|
.map(PathBuf::from)
|
2018-12-05 18:01:42 +00:00
|
|
|
.map(StoreId::new)
|
2017-09-30 23:13:50 +00:00
|
|
|
.unwrap() // unwrap safe by clap
|
2019-02-05 00:37:32 +00:00
|
|
|
.map_err_trace_exit_unwrap();
|
2017-09-30 23:13:50 +00:00
|
|
|
|
2018-02-06 20:34:33 +00:00
|
|
|
// remove links to entry, and re-add them later
|
|
|
|
let mut linked_entries = {
|
|
|
|
rt.store()
|
|
|
|
.get(sourcename.clone())
|
2019-02-05 00:37:32 +00:00
|
|
|
.map_err_trace_exit_unwrap()
|
2018-02-06 20:34:33 +00:00
|
|
|
.unwrap_or_else(|| {
|
|
|
|
error!("Funny things happened: Entry moved to destination did not fail, but entry does not exist");
|
|
|
|
exit(1)
|
|
|
|
})
|
2019-06-21 19:33:22 +00:00
|
|
|
.links()
|
2019-02-05 00:37:32 +00:00
|
|
|
.map_err_trace_exit_unwrap()
|
2018-10-30 17:40:52 +00:00
|
|
|
.map(|link| Ok(link.get_store_id().clone()) as Result<_, _>)
|
2018-02-06 20:34:33 +00:00
|
|
|
.into_get_iter(rt.store())
|
2019-02-05 00:37:32 +00:00
|
|
|
.trace_unwrap_exit()
|
2018-02-06 20:34:33 +00:00
|
|
|
.map(|e| {
|
2018-04-30 17:24:05 +00:00
|
|
|
e.unwrap_or_else(|| {
|
2018-02-06 20:34:33 +00:00
|
|
|
error!("Linked entry does not exist");
|
|
|
|
exit(1)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
};
|
|
|
|
|
|
|
|
{ // remove links to linked entries from source
|
|
|
|
let mut entry = rt
|
|
|
|
.store()
|
|
|
|
.get(sourcename.clone())
|
2019-02-05 00:37:32 +00:00
|
|
|
.map_err_trace_exit_unwrap()
|
2018-02-06 20:34:33 +00:00
|
|
|
.unwrap_or_else(|| {
|
|
|
|
error!("Source Entry does not exist");
|
|
|
|
exit(1)
|
|
|
|
});
|
|
|
|
|
|
|
|
for link in linked_entries.iter_mut() {
|
2019-08-27 08:41:14 +00:00
|
|
|
entry.remove_link(link).map_err_trace_exit_unwrap();
|
2018-02-06 20:34:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-27 08:41:14 +00:00
|
|
|
rt
|
2017-09-30 23:13:50 +00:00
|
|
|
.store()
|
2018-02-06 20:34:33 +00:00
|
|
|
.move_by_id(sourcename.clone(), destname.clone())
|
|
|
|
.map_err(|e| { // on error, re-add links
|
|
|
|
debug!("Re-adding links to source entry because moving failed");
|
|
|
|
relink(rt.store(), sourcename.clone(), &mut linked_entries);
|
|
|
|
e
|
|
|
|
})
|
2019-02-05 00:37:32 +00:00
|
|
|
.map_err_trace_exit_unwrap();
|
2017-09-30 23:13:50 +00:00
|
|
|
|
2019-08-27 08:41:14 +00:00
|
|
|
rt.report_touched(&destname).unwrap_or_exit();
|
2018-11-01 16:57:22 +00:00
|
|
|
|
2018-02-06 20:34:33 +00:00
|
|
|
// re-add links to moved entry
|
|
|
|
relink(rt.store(), destname, &mut linked_entries);
|
|
|
|
|
2017-09-30 23:13:50 +00:00
|
|
|
info!("Ok.");
|
|
|
|
}
|
2018-02-06 20:34:33 +00:00
|
|
|
|
|
|
|
fn relink<'a>(store: &'a Store, target: StoreId, linked_entries: &mut Vec<FileLockEntry<'a>>) {
|
|
|
|
let mut entry = store
|
|
|
|
.get(target)
|
2019-02-05 00:37:32 +00:00
|
|
|
.map_err_trace_exit_unwrap()
|
2018-02-06 20:34:33 +00:00
|
|
|
.unwrap_or_else(|| {
|
|
|
|
error!("Funny things happened: Entry moved to destination did not fail, but entry does not exist");
|
|
|
|
exit(1)
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
for mut link in linked_entries {
|
2019-08-27 08:41:14 +00:00
|
|
|
entry.add_link(&mut link).map_err_trace_exit_unwrap();
|
2018-02-06 20:34:33 +00:00
|
|
|
}
|
|
|
|
}
|