Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No safe hardlinking for Windows #668

Merged
merged 6 commits into from
Apr 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions czkawka_core/src/duplicate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ use crate::common_traits::*;
use crate::flc;
use crate::localizer_core::generate_translation_hashmap;

const TEMP_HARDLINK_FILE: &str = "rzeczek.rxrxrxl";

#[derive(PartialEq, Eq, Clone, Debug, Copy)]
pub enum HashType {
Blake3,
Expand Down Expand Up @@ -1249,12 +1251,13 @@ fn filter_hard_links(vec_file_entry: &[FileEntry]) -> Vec<FileEntry> {

pub fn make_hard_link(src: &Path, dst: &Path) -> io::Result<()> {
let dst_dir = dst.parent().ok_or_else(|| Error::new(ErrorKind::Other, "No parent"))?;
let temp = tempfile::Builder::new().tempfile_in(dst_dir)?;
fs::rename(dst, temp.path())?;
let temp = dst_dir.join(TEMP_HARDLINK_FILE);
fs::rename(dst, temp.as_path())?;
let result = fs::hard_link(src, dst);
if result.is_err() {
fs::rename(temp.path(), dst)?;
fs::rename(temp.as_path(), dst)?;
}
fs::remove_file(temp)?;
result
}

Expand Down
24 changes: 18 additions & 6 deletions czkawka_gui/src/connect_things/connect_button_hardlink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ use crate::help_functions::*;
use crate::localizer_core::generate_translation_hashmap;
use crate::notebook_enums::*;

#[derive(PartialEq, Eq)]
enum TypeOfTool {
Hardlinking,
Symlinking,
}

pub fn connect_button_hardlink_symlink(gui_data: &GuiData) {
// Hardlinking
{
Expand All @@ -20,7 +26,7 @@ pub fn connect_button_hardlink_symlink(gui_data: &GuiData) {
let gui_data = gui_data.clone();

buttons_hardlink.connect_clicked(move |_| {
glib::MainContext::default().spawn_local(sym_hard_link_things(gui_data.clone(), true));
glib::MainContext::default().spawn_local(sym_hard_link_things(gui_data.clone(), TypeOfTool::Hardlinking));
});
}

Expand All @@ -31,12 +37,12 @@ pub fn connect_button_hardlink_symlink(gui_data: &GuiData) {
let gui_data = gui_data.clone();

buttons_symlink.connect_clicked(move |_| {
glib::MainContext::default().spawn_local(sym_hard_link_things(gui_data.clone(), false));
glib::MainContext::default().spawn_local(sym_hard_link_things(gui_data.clone(), TypeOfTool::Symlinking));
});
}
}

pub async fn sym_hard_link_things(gui_data: GuiData, hardlinking: bool) {
async fn sym_hard_link_things(gui_data: GuiData, hardlinking: TypeOfTool) {
let notebook_main = gui_data.main_notebook.notebook_main.clone();
let main_tree_views = gui_data.main_notebook.get_main_tree_views();

Expand Down Expand Up @@ -90,13 +96,13 @@ pub async fn sym_hard_link_things(gui_data: GuiData, hardlinking: bool) {
}
}

pub fn hardlink_symlink(
fn hardlink_symlink(
tree_view: &gtk::TreeView,
column_file_name: i32,
column_path: i32,
column_color: i32,
column_selection: i32,
hardlinking: bool,
hardlinking: TypeOfTool,
text_view_errors: &TextView,
) {
reset_text_view(text_view_errors);
Expand Down Expand Up @@ -192,13 +198,19 @@ pub fn hardlink_symlink(
break;
}
}
if hardlinking {
if hardlinking == TypeOfTool::Hardlinking {
for symhardlink_data in vec_symhardlink_data {
for file_to_hardlink in symhardlink_data.files_to_symhardlink {
// #[cfg(target_family = "unix")]
if let Err(e) = make_hard_link(&PathBuf::from(&symhardlink_data.original_data), &PathBuf::from(&file_to_hardlink)) {
add_text_to_text_view(text_view_errors, format!("{} {}, reason {}", flg!("hardlink_failed"), file_to_hardlink, e).as_str());
continue;
}
// #[cfg(target_family = "windows")]
// if let Err(e) = fs::hard_link(&PathBuf::from(&symhardlink_data.original_data), &PathBuf::from(&file_to_hardlink)) {
// add_text_to_text_view(text_view_errors, format!("{} {}, reason {}", flg!("hardlink_failed"), file_to_hardlink, e).as_str());
// continue;
// }
}
}
} else {
Expand Down