From 41eae92d0ca04458dfee6632b2ccb5aca0ff88d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20S=C3=A1enz?= Date: Sun, 22 Mar 2026 13:13:01 +0100 Subject: [PATCH] Fix backup crash on pre-existing symlinks during backup (#35) Co-Authored-By: Claude Opus 4.6 (1M context) --- CHANGELOG.md | 7 +++++++ lib/dotsync/actions/pull_action.rb | 1 + lib/dotsync/version.rb | 2 +- spec/dotsync/actions/pull_action_spec.rb | 22 ++++++++++++++++++++++ 4 files changed, 31 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cdc99ae..9c3707f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +## [0.4.2] - 2026-03-22 + +### Fixed + +- Fix `cp_r_regular_files` crash on pre-existing symlinks during backup (#35) + - Add `FileUtils.rm_f` before `FileUtils.ln_s` to handle leftover symlinks from partial backup runs + ## [0.4.1] - 2026-03-17 ### Fixed diff --git a/lib/dotsync/actions/pull_action.rb b/lib/dotsync/actions/pull_action.rb index 75db7a5..b2ca92c 100644 --- a/lib/dotsync/actions/pull_action.rb +++ b/lib/dotsync/actions/pull_action.rb @@ -76,6 +76,7 @@ def cp_r_regular_files(src, dest) src_entry = File.join(src, entry) dest_entry = File.join(dest, entry) if File.symlink?(src_entry) + FileUtils.rm_f(dest_entry) FileUtils.ln_s(File.readlink(src_entry), dest_entry) elsif File.directory?(src_entry) cp_r_regular_files(src_entry, dest_entry) diff --git a/lib/dotsync/version.rb b/lib/dotsync/version.rb index 7a090e7..d2bce22 100644 --- a/lib/dotsync/version.rb +++ b/lib/dotsync/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module Dotsync - VERSION = "0.4.1" + VERSION = "0.4.2" end diff --git a/spec/dotsync/actions/pull_action_spec.rb b/spec/dotsync/actions/pull_action_spec.rb index 021d4d1..0c01e53 100644 --- a/spec/dotsync/actions/pull_action_spec.rb +++ b/spec/dotsync/actions/pull_action_spec.rb @@ -330,6 +330,28 @@ end end + context "when backup directory has pre-existing symlinks" do + it "overwrites the existing symlinks without crashing" do + timestamp = Time.now.strftime("%Y%m%d%H%M%S") + backup_dir = File.join(backups_root, timestamp) + + # Simulate a partial backup from a previous failed run + FileUtils.mkdir_p(File.join(backup_dir, "folder_dest")) + FileUtils.ln_s("old_target", File.join(backup_dir, "folder_dest", "file1")) + + # Create a symlink in the source to back up + symlink_src = File.join(mapping1.dest, "link1") + FileUtils.ln_s("some_target", symlink_src) + + subject + + expect(Dir.exist?(backup_dir)).to eq(true) + backed_up_link = File.join(backup_dir, "folder_dest", "link1") + expect(File.symlink?(backed_up_link)).to eq(true) + expect(File.readlink(backed_up_link)).to eq("some_target") + end + end + context "when there are more than 10 backups" do before do 1.upto(12) do |day|