Class: GDSync::Option

Inherits:
Object
  • Object
show all
Defined in:
lib/option.rb

Constant Summary collapse

SUPPORTED_OPTIONS =
[
  '--checksum',
  '--recursive',
  '--times',
  '--dry-run',
  '--existing',
  '--ignore-existing',
  '--delete',
  '--ignore-times',
  '--size-only',
  '--update',
  '--dirs',
  '--remove-source-files',
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Option


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
# File 'lib/option.rb', line 21

def initialize(options)
  @verbose = options.include?('--verbose')
  @delete = options.include?('--delete')
  @checksum = options.include?('--checksum')
  @dry_run = options.include?('--dry-run')
  @size_only = options.include?('--size-only')
  @recursive = options.include?('--recursive')
  @preserve_time = options.include?('--times')
  @ignore_times = options.include?('--ignore-times')
  @existing = options.include?('--existing')
  @ignore_existing = options.include?('--ignore-existing')
  @update = options.include?('--update')
  @dirs = options.include?('--dirs')
  @remove_source_files = options.include?('--remove-source-files')
  @max_size = _parse_size(options, '--max-size', Float::INFINITY)
  raise "--max-size value is invalid: #{@max_size}" if @max_size <= 0
  @min_size = _parse_size(options, '--min-size', 0)

  archive = options.include?('--archive')
  if archive
    @recursive = true
    @preserve_time = true
  end

  _validate
end

Instance Method Details

#delete?Boolean


48
49
50
# File 'lib/option.rb', line 48

def delete?
  @delete
end

#dirs?Boolean


76
77
78
79
80
81
82
# File 'lib/option.rb', line 76

def dirs?
  if @recursive
    false
  else
    @dirs
  end
end

#dry_run?Boolean


56
57
58
# File 'lib/option.rb', line 56

def dry_run?
  @dry_run
end

#error(msg) ⇒ Object


112
113
114
# File 'lib/option.rb', line 112

def error(msg)
  puts "Error: #{msg}"
end

#existing?Boolean


68
69
70
# File 'lib/option.rb', line 68

def existing?
  @existing
end

#ignore_existing?Boolean


72
73
74
# File 'lib/option.rb', line 72

def ignore_existing?
  @ignore_existing
end

#log_created(file) ⇒ Object


120
121
122
# File 'lib/option.rb', line 120

def log_created(file)
  puts "#{file.path}#{file.is_dir? ? '/' : ''} (created)" if @verbose
end

#log_deleted(file) ⇒ Object


124
125
126
# File 'lib/option.rb', line 124

def log_deleted(file)
  puts "#{file.path}#{file.is_dir? ? '/' : ''} (deleted)" if @verbose
end

#log_extraneous(file) ⇒ Object


128
129
130
# File 'lib/option.rb', line 128

def log_extraneous(file)
  puts "#{file.path}#{file.is_dir? ? '/' : ''} (extraneous)" if @verbose
end

#log_skip(file) ⇒ Object


132
133
134
# File 'lib/option.rb', line 132

def log_skip(file)
  puts "#{file.path}#{file.is_dir? ? '/' : ''} (skip)" if @verbose
end

#log_updated(file) ⇒ Object


116
117
118
# File 'lib/option.rb', line 116

def log_updated(file)
  puts "#{file.path}#{file.is_dir? ? '/' : ''} (updated)" if @verbose
end

#max_sizeObject


88
89
90
# File 'lib/option.rb', line 88

def max_size
  @max_size
end

#min_sizeObject


92
93
94
# File 'lib/option.rb', line 92

def min_size
  @min_size
end

#preserve_time?Boolean


64
65
66
# File 'lib/option.rb', line 64

def preserve_time?
  @preserve_time
end

#recursive?Boolean


60
61
62
# File 'lib/option.rb', line 60

def recursive?
  @recursive
end

#remove_source_files?Boolean


84
85
86
# File 'lib/option.rb', line 84

def remove_source_files?
  @remove_source_files
end

#should_update?(src_file, dest_file) ⇒ Boolean


96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/option.rb', line 96

def should_update?(src_file, dest_file)
  # Ignore sub-second part of mtime.
  # This is same behavior when rsync(1) had been compiled without 'HAVE_UTIMENSAT' macro.
  return false if @update && dest_file.mtime.to_time.to_i > src_file.mtime.to_time.to_i

  if @checksum
    src_file.md5 != dest_file.md5
  elsif @size_only
    src_file.size != dest_file.size
  elsif @ignore_times
    true
  else
    src_file.size != dest_file.size or dest_file.mtime.to_time.to_i < src_file.mtime.to_time.to_i
  end
end

#verbose?Boolean


52
53
54
# File 'lib/option.rb', line 52

def verbose?
  @verbose
end