Class: Dotsync::FileTransfer

Inherits:
Object
  • Object
show all
Defined in:
lib/dotsync/utils/file_transfer.rb

Instance Method Summary collapse

Constructor Details

#initialize(mapping, removals: nil) ⇒ FileTransfer

Initializes a new FileTransfer instance

Parameters:

  • the mapping object containing source, destination, force, and ignore details

  • (defaults to: nil)

    pre-computed absolute paths to remove in force mode. When provided (from cached diff), skips the destination tree traversal in cleanup_folder. When nil, falls back to scanning the destination tree (used by backup transfers).



11
12
13
14
15
16
17
18
19
# File 'lib/dotsync/utils/file_transfer.rb', line 11

def initialize(mapping, removals: nil)
  @mapping = mapping
  @src = mapping.src
  @dest = mapping.dest
  @force = mapping.force?
  @inclusions = mapping.inclusions || []
  @ignores = mapping.ignores || []
  @removals = removals
end

Instance Method Details

#transferObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/dotsync/utils/file_transfer.rb', line 21

def transfer
  if File.file?(@src)
    # Check if we're trying to overwrite a directory with a file
    if File.exist?(@dest) && File.directory?(@dest) && !File.symlink?(@dest)
      # If @dest is a directory and NOT just a parent directory for the file,
      # this is a conflict. The check is: if @dest path exactly matches where
      # we want the file to be (not a parent dir), then it's a conflict.
      # We determine this by checking if File.basename(@src) already appears
      # to be accounted for in @dest path.
      dest_basename = File.basename(@dest)
      src_basename = File.basename(@src)

      if dest_basename == src_basename
        raise Dotsync::TypeConflictError, "Cannot overwrite directory '#{@dest}' with file '#{@src}'"
      end
    end

    # If dest is a directory, compute the target file path
    target_dest = if File.directory?(@dest)
      File.join(@dest, File.basename(@src))
    else
      @dest
    end
    transfer_file(@src, target_dest)
  else
    if @force
      if @removals
        remove_precomputed_files
      else
        cleanup_folder(@dest)
      end
    end
    transfer_folder(@src, @dest)
  end
end