Module: Dotsync::MappingsTransfer

Extended by:
Forwardable
Includes:
PathUtils
Included in:
PullAction, PushAction, WatchAction
Defined in:
lib/dotsync/actions/concerns/mappings_transfer.rb

Overview

MappingsTransfer provides shared functionality for push/pull actions.

Performance Optimizations

This module uses parallel execution for two key operations:

  1. Parallel diff computation (see #differs) Each mapping's diff is computed in a separate thread. Since mappings are independent (different src/dest paths), this overlaps I/O and CPU work.

  2. Parallel file transfers (see #transfer_mappings) File transfers for each mapping run concurrently. This is especially beneficial for many small files where I/O latency dominates.

Error handling is thread-safe: errors are collected in a mutex-protected array and reported after all parallel operations complete.

Constant Summary collapse

MAPPINGS_LEGEND =
[
  [Icons.force, "The source will overwrite the destination"],
  [Icons.only, "Filtered by 'only' whitelist"],
  [Icons.ignore, "Filtered by 'ignore' blacklist"],
  [Icons.hook, "Post-sync hooks configured"],
  [Icons.invalid, "Invalid paths detected in the source or destination"]
]
DIFFERENCES_LEGEND =
[
  [Icons.diff_created, "Created/added file"],
  [Icons.diff_updated, "Updated/modified file"],
  [Icons.diff_removed, "Removed/deleted file"]
]

Constants included from PathUtils

PathUtils::ENV_VARS_COLOR

Instance Method Summary collapse

Methods included from PathUtils

#colorize_env_vars, #expand_env_vars, #extract_env_vars, #path_is_parent_or_same?, #relative_to_absolute, #sanitize_path, #translate_tmp_path

Instance Method Details

#cleanup_orphansObject



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/dotsync/actions/concerns/mappings_transfer.rb', line 145

def cleanup_orphans
  valid_mappings.each do |mapping|
    next unless mapping.has_inclusions? && !mapping.force? && mapping.manifest_key

    current_files = dest_files_matching_inclusions(mapping)
    manifest = Dotsync::Manifest.new(
      dest_dir: mapping.dest,
      key: mapping.manifest_key,
      xdg_data_home: manifests_xdg_data_home
    )

    manifest.orphans(current_files).each do |orphan_path|
      next unless File.exist?(orphan_path)

      FileUtils.rm(orphan_path)
      logger.log("#{Icons.diff_removed}#{orphan_path}", color: Colors.diff_removals)
    end

    manifest.write(current_files)
  end
end

#execute_hooks(force: false) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/dotsync/actions/concerns/mappings_transfer.rb', line 167

def execute_hooks(force: false)
  valid_mappings.each_with_index do |mapping, idx|
    next unless mapping.has_hooks?

    differ = differs[idx]
    changed_files = differ.additions + differ.modifications
    if changed_files.empty?
      next unless force

      changed_files = all_dest_files(mapping)
      next if changed_files.empty?
    end

    runner = Dotsync::HookRunner.new(mapping: mapping, changed_files: changed_files, logger: logger)
    runner.execute
  end
end

#show_content_diffsObject



100
101
102
103
104
105
# File 'lib/dotsync/actions/concerns/mappings_transfer.rb', line 100

def show_content_diffs
  info("Content Differences:", icon: :diff)
  modification_pairs.each do |pair|
    Dotsync::ContentDiff.new(pair[:src], pair[:dest], logger).display
  end
end

#show_differences(diff_content: false) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/dotsync/actions/concerns/mappings_transfer.rb', line 81

def show_differences(diff_content: false)
  info("Differences:", icon: :diff)
  differs.flat_map(&:additions).sort.each do |path|
    logger.log("#{Icons.diff_created}#{path}", color: Colors.diff_additions)
  end
  differs.flat_map(&:modifications).sort.each do |path|
    logger.log("#{Icons.diff_updated}#{path}", color: Colors.diff_modifications)
  end
  differs.flat_map(&:removals).sort.each do |path|
    logger.log("#{Icons.diff_removed}#{path}", color: Colors.diff_removals)
  end
  logger.log("  No differences") unless has_differences?
  logger.log("")

  show_content_diffs if diff_content && has_modifications?

  show_orphan_preview if has_differences? && respond_to?(:manifests_xdg_data_home, true)
end

#show_differences_legendObject



75
76
77
78
79
# File 'lib/dotsync/actions/concerns/mappings_transfer.rb', line 75

def show_differences_legend
  info("Differences Legend:", icon: :legend)
  logger.log(Dotsync::TableRenderer.new(rows: DIFFERENCES_LEGEND).render)
  logger.log("")
end

#show_env_varsObject



44
45
46
47
48
49
50
51
52
53
# File 'lib/dotsync/actions/concerns/mappings_transfer.rb', line 44

def show_env_vars
  env_vars = mappings_env_vars
  return unless env_vars.any?

  info("Environment variables:", icon: :env_vars)

  rows = env_vars.map { |env_var| [env_var, ENV[env_var]] }.sort_by(&:first)
  logger.log(Dotsync::TableRenderer.new(rows: rows).render)
  logger.log("")
end

#show_hooks_preview(force: false) ⇒ Object



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/dotsync/actions/concerns/mappings_transfer.rb', line 185

def show_hooks_preview(force: false)
  hooks_to_run = []

  valid_mappings.each_with_index do |mapping, idx|
    next unless mapping.has_hooks?

    differ = differs[idx]
    changed_files = differ.additions + differ.modifications
    if changed_files.empty?
      next unless force

      changed_files = all_dest_files(mapping)
      next if changed_files.empty?
    end

    runner = Dotsync::HookRunner.new(mapping: mapping, changed_files: changed_files, logger: logger)
    hooks_to_run.concat(runner.preview)
  end

  return if hooks_to_run.empty?

  info("Hooks to run:", icon: :hook)
  hooks_to_run.each do |command|
    logger.log("  #{command}")
  end
  logger.log("")
end

#show_mappingsObject



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/dotsync/actions/concerns/mappings_transfer.rb', line 61

def show_mappings
  info("Mappings:", icon: :config)

  rows = mappings.map do |mapping|
    [
      mapping.icons,
      colorize_env_vars(mapping.original_src),
      colorize_env_vars(mapping.original_dest)
    ]
  end
  logger.log(Dotsync::TableRenderer.new(headings: ["Flags", "Source", "Destination"], rows: rows).render)
  logger.log("")
end

#show_mappings_legendObject



55
56
57
58
59
# File 'lib/dotsync/actions/concerns/mappings_transfer.rb', line 55

def show_mappings_legend
  info("Mappings Legend:", icon: :legend)
  logger.log(Dotsync::TableRenderer.new(rows: MAPPINGS_LEGEND).render)
  logger.log("")
end

#transfer_mappingsObject

Transfers all valid mappings from source to destination.

OPTIMIZATION: Parallel execution Mappings are transferred concurrently using Dotsync::Parallel. Each mapping operates on independent paths, so parallel execution is safe. Errors are collected thread-safely and reported after all transfers complete.



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/dotsync/actions/concerns/mappings_transfer.rb', line 113

def transfer_mappings
  errors = []
  mutex = Mutex.new

  # Only transfer mappings that have actual differences (uses cached diffs)
  changed_pairs = valid_mappings.each_with_index.filter_map do |mapping, idx|
    [mapping, differs[idx]] if differs[idx].any?
  end

  # Process mappings in parallel - each mapping is independent
  Dotsync::Parallel.each(changed_pairs) do |mapping, diff|
    removals = diff.removal_rel_paths.map { |rel| File.join(mapping.dest, rel) }
    Dotsync::FileTransfer.new(mapping, removals: removals).transfer
  rescue Dotsync::PermissionError => e
    mutex.synchronize { errors << ["Permission denied: #{e.message}", "Try: chmod +w <path> or check file permissions"] }
  rescue Dotsync::DiskFullError => e
    mutex.synchronize { errors << ["Disk full: #{e.message}", "Free up disk space and try again"] }
  rescue Dotsync::SymlinkError => e
    mutex.synchronize { errors << ["Symlink error: #{e.message}", "Check that symlink target exists and is accessible"] }
  rescue Dotsync::TypeConflictError => e
    mutex.synchronize { errors << ["Type conflict: #{e.message}", "Cannot overwrite directory with file or vice versa"] }
  rescue Dotsync::FileTransferError => e
    mutex.synchronize { errors << ["File transfer failed: #{e.message}", nil] }
  end

  # Report all errors after parallel execution
  errors.each do |error_msg, info_msg|
    logger.error(error_msg)
    logger.info(info_msg) if info_msg
  end
end