Class: Bdsync::Core
- Inherits:
-
Object
- Object
- Bdsync::Core
- Defined in:
- lib/bdsync/core.rb
Instance Attribute Summary collapse
-
#data_path ⇒ Object
readonly
Returns the value of attribute data_path.
Class Method Summary collapse
Instance Method Summary collapse
- #do_first_time_sync_from_local(relative_path, local_path, remote_path, remote, type) ⇒ Object
- #do_first_time_sync_from_remote(relative_path, local_path, remote_path, remote, type) ⇒ Object
- #do_sync_from_local(relative_path, local_path, remote_path, remote, type, old) ⇒ Object
- #do_sync_from_remote(relative_path, local_path, remote_path, remote, type, old) ⇒ Object
- #handle_local_conflict(local_path) ⇒ Object
- #handle_local_entry(path, type) ⇒ Object
- #handle_remote_conflict(remote_path) ⇒ Object
- #handle_remote_entry(remote, path, type) ⇒ Object
-
#initialize(params, sync_type) ⇒ Core
constructor
A new instance of Core.
- #is_same_contents(local_path, remote_path) ⇒ Object
- #load_data ⇒ Object
- #local_ensure_dir(path) ⇒ Object
- #local_ensure_parent(path) ⇒ Object
- #local_mkdir(local_path) ⇒ Object
- #local_remove_directory(local_path) ⇒ Object
- #local_remove_file(local_path) ⇒ Object
- #local_rename(local_path, new_local_path) ⇒ Object
- #save_data(data) ⇒ Object
- #synchronize ⇒ Object
- #traverse_local_path(local_path) ⇒ Object
- #traverse_remote_path(remote_path) ⇒ Object
- #update_directory_data(relative_path, local_path, remote_mtime) ⇒ Object
- #update_file_data(relative_path, local_path, remote_mtime) ⇒ Object
Constructor Details
#initialize(params, sync_type) ⇒ Core
Returns a new instance of Core.
9 10 11 12 13 14 15 16 17 |
# File 'lib/bdsync/core.rb', line 9 def initialize params, sync_type raise "local path not specified" if !params["local_root_path"] raise "remote path not specified" if !params["remote_root_path"] @local_root_path = params["local_root_path"] @remote_root_path = params["remote_root_path"] @infinite_loop = params["infinite_loop"] @data_path = "#{Dir.home}/.bdsync/#{sync_type}_#{Utils.md5 params.to_s}.yaml" end |
Instance Attribute Details
#data_path ⇒ Object (readonly)
Returns the value of attribute data_path.
7 8 9 |
# File 'lib/bdsync/core.rb', line 7 def data_path @data_path end |
Class Method Details
.options ⇒ Object
19 20 21 |
# File 'lib/bdsync/core.rb', line 19 def self. ["local_root_path:", "remote_root_path:", "infinite_loop"] end |
Instance Method Details
#do_first_time_sync_from_local(relative_path, local_path, remote_path, remote, type) ⇒ Object
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/bdsync/core.rb', line 192 def do_first_time_sync_from_local relative_path, local_path, remote_path, remote, type case type when :file if !remote remote = upload_file local_path, remote_path update_file_data relative_path, local_path, remote.mtime elsif remote.directory? handle_remote_conflict remote_path remote = upload_file local_path, remote_path update_file_data relative_path, local_path, remote.mtime else # compare file contents, conflict if not equal if !is_same_contents local_path, remote_path handle_remote_conflict remote_path remote = upload_file local_path, remote_path end update_file_data relative_path, local_path, remote.mtime end when :directory if !remote remote = remote_mkdir remote_path update_directory_data relative_path, local_path, remote.mtime elsif remote.directory? update_directory_data relative_path, local_path, remote.mtime else handle_remote_conflict remote_path remote = remote_mkdir remote_path update_directory_data relative_path, local_path, remote.mtime end end end |
#do_first_time_sync_from_remote(relative_path, local_path, remote_path, remote, type) ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/bdsync/core.rb', line 159 def do_first_time_sync_from_remote relative_path, local_path, remote_path, remote, type case type when :file if !File.exist? local_path download_file local_path, remote_path update_file_data relative_path, local_path, remote.mtime elsif File.directory? local_path handle_local_conflict local_path download_file local_path, remote_path update_file_data relative_path, local_path, remote.mtime else # compare file contents, conflict if not equal if !is_same_contents local_path, remote_path handle_local_conflict local_path download_file local_path, remote_path end update_file_data relative_path, local_path, remote.mtime end when :directory if !File.exist? local_path local_mkdir local_path update_directory_data relative_path, local_path, remote.mtime elsif File.directory? local_path update_directory_data relative_path, local_path, remote.mtime else handle_local_conflict local_path local_mkdir local_path update_directory_data relative_path, local_path, remote.mtime end end end |
#do_sync_from_local(relative_path, local_path, remote_path, remote, type, old) ⇒ Object
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 |
# File 'lib/bdsync/core.rb', line 312 def do_sync_from_local relative_path, local_path, remote_path, remote, type, old local_changed = (File.mtime(local_path).to_i - old[:local_mtime]) != 0 case type when :file if !remote if !local_changed local_remove_file local_path else handle_local_conflict local_path end elsif remote.directory? if !local_changed local_remove_file local_path local_mkdir local_path update_directory_data relative_path, local_path, remote.mtime else if File.mtime(local_path).to_i > remote.mtime handle_remote_conflict remote_path remote = upload_file local_path, remote_path update_file_data relative_path, local_path, remote.mtime else handle_local_conflict local_path local_mkdir local_path update_directory_data relative_path, local_path, remote.mtime end end else remote_changed = (remote.mtime - old[:remote_mtime]) != 0 if !local_changed && !remote_changed @data[relative_path] = old elsif local_changed && !remote_changed remote = upload_file local_path, remote_path update_file_data relative_path, local_path, remote.mtime elsif !local_changed && remote_changed download_file local_path, remote_path update_file_data relative_path, local_path, remote.mtime else if File.mtime(local_path).to_i > remote.mtime # compare file contents, conflict if not equal if !is_same_contents local_path, remote_path handle_remote_conflict remote_path remote = upload_file local_path, remote_path end update_file_data relative_path, local_path, remote.mtime else # compare file contents, conflict if not equal if !is_same_contents local_path, remote_path handle_local_conflict local_path download_file local_path, remote_path end update_file_data relative_path, local_path, remote.mtime end end end when :directory if !remote if !local_changed local_remove_directory local_path else handle_local_conflict local_path end elsif remote.directory? update_directory_data relative_path, local_path, remote.mtime else if !local_changed local_remove_directory local_path download_file local_path, remote_path update_file_data relative_path, local_path, remote.mtime else if File.mtime(local_path).to_i > remote.mtime handle_remote_conflict remote_path remote = remote_mkdir remote_path update_directory_data relative_path, local_path, remote.mtime else handle_local_conflict local_path download_file local_path, remote_path update_file_data relative_path, local_path, remote.mtime end end end end end |
#do_sync_from_remote(relative_path, local_path, remote_path, remote, type, old) ⇒ Object
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 |
# File 'lib/bdsync/core.rb', line 225 def do_sync_from_remote relative_path, local_path, remote_path, remote, type, old remote_changed = (remote.mtime - old[:remote_mtime]) != 0 case type when :file if !File.exist? local_path if !remote_changed remote_remove_file remote_path else handle_remote_conflict remote_path end elsif File.directory? local_path if !remote_changed remote_remove_file remote_path remote = remote_mkdir remote_path update_directory_data relative_path, local_path, remote.mtime else if File.mtime(local_path).to_i > remote.mtime handle_remote_conflict remote_path remote = remote_mkdir remote_path update_directory_data relative_path, local_path, remote.mtime else handle_local_conflict local_path download_file local_path, remote_path update_file_data relative_path, local_path, remote.mtime end end else local_changed = (File.mtime(local_path).to_i - old[:local_mtime]) != 0 if !local_changed && !remote_changed @data[relative_path] = old elsif local_changed && !remote_changed remote = upload_file local_path, remote_path update_file_data relative_path, local_path, remote.mtime elsif !local_changed && remote_changed download_file local_path, remote_path update_file_data relative_path, local_path, remote.mtime else if File.mtime(local_path).to_i > remote.mtime # compare file contents, conflict if not equal if !is_same_contents local_path, remote_path handle_remote_conflict remote_path remote = upload_file local_path, remote_path end update_file_data relative_path, local_path, remote.mtime else # compare file contents, conflict if not equal if !is_same_contents local_path, remote_path handle_local_conflict local_path download_file local_path, remote_path end update_file_data relative_path, local_path, remote.mtime end end end when :directory if !File.exist? local_path if !remote_changed remote_remove_dir remote_path else handle_remote_conflict remote_path end elsif File.directory? local_path update_directory_data relative_path, local_path, remote.mtime else if !remote_changed remote_remove_dir remote_path remote = upload_file local_path, remote_path update_file_data relative_path, local_path, remote.mtime else if File.mtime(local_path).to_i > remote.mtime handle_remote_conflict remote_path remote = upload_file local_path, remote_path update_file_data relative_path, local_path, remote.mtime else handle_local_conflict local_path local_mkdir local_path update_directory_data relative_path, local_path, remote.mtime end end end end end |
#handle_local_conflict(local_path) ⇒ Object
415 416 417 418 419 420 421 |
# File 'lib/bdsync/core.rb', line 415 def handle_local_conflict local_path ts = Utils. local_conflict_path = local_path.sub(@local_root_path, "#{@local_root_path}/.conflict") + "." + ts local_ensure_parent local_conflict_path local_rename local_path, local_conflict_path end |
#handle_local_entry(path, type) ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/bdsync/core.rb', line 139 def handle_local_entry path, type puts "#{'%9s' % type}: #{path}".green # NOTE: force_encoding to output readable japanese text relative_path = path.sub(@local_root_path + "/", "").force_encoding("UTF-8") local_path = "#{@local_root_path}/#{relative_path}" remote_path = "#{@remote_root_path}/#{relative_path}" remote = remote_get_object remote_path old = @old_data[relative_path] if !old # no old sync record do_first_time_sync_from_local relative_path, local_path, remote_path, remote, type else # old sync record found do_sync_from_local relative_path, local_path, remote_path, remote, type, old end end |
#handle_remote_conflict(remote_path) ⇒ Object
423 424 425 426 427 428 429 |
# File 'lib/bdsync/core.rb', line 423 def handle_remote_conflict remote_path ts = Utils. remote_conflict_path = remote_path.sub(@remote_root_path, "#{@remote_root_path}/.conflict") + "." + ts remote_ensure_parent remote_conflict_path remote_rename remote_path, remote_conflict_path end |
#handle_remote_entry(remote, path, type) ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/bdsync/core.rb', line 121 def handle_remote_entry remote, path, type puts "#{'%9s' % type}: #{path}".green # NOTE: force_encoding to output readable japanese text relative_path = path.sub(@remote_root_path + "/", "").force_encoding("UTF-8") local_path = "#{@local_root_path}/#{relative_path}" remote_path = "#{@remote_root_path}/#{relative_path}" old = @old_data[relative_path] if !old # no old sync record do_first_time_sync_from_remote relative_path, local_path, remote_path, remote, type else # old sync record found do_sync_from_remote relative_path, local_path, remote_path, remote, type, old end end |
#is_same_contents(local_path, remote_path) ⇒ Object
459 460 461 462 463 |
# File 'lib/bdsync/core.rb', line 459 def is_same_contents local_path, remote_path local_file_md5 = Utils.file_md5 local_path remote_file_md5 = get_remote_file_md5 remote_path local_file_md5 == remote_file_md5 end |
#load_data ⇒ Object
53 54 55 56 57 58 |
# File 'lib/bdsync/core.rb', line 53 def load_data puts "\nload #{@data_path}" YAML.load_file @data_path rescue Errno::ENOENT {} end |
#local_ensure_dir(path) ⇒ Object
451 452 453 |
# File 'lib/bdsync/core.rb', line 451 def local_ensure_dir path FileUtils.mkdir_p path if !File.directory? path end |
#local_ensure_parent(path) ⇒ Object
455 456 457 |
# File 'lib/bdsync/core.rb', line 455 def local_ensure_parent path local_ensure_dir File.dirname path end |
#local_mkdir(local_path) ⇒ Object
431 432 433 434 |
# File 'lib/bdsync/core.rb', line 431 def local_mkdir local_path puts "#{Utils.caller_info 1} mkdir_p #{local_path}".white FileUtils.mkdir_p local_path end |
#local_remove_directory(local_path) ⇒ Object
441 442 443 444 |
# File 'lib/bdsync/core.rb', line 441 def local_remove_directory local_path puts "#{Utils.caller_info 1} rm_rf #{local_path}".white FileUtils.rm_rf local_path end |
#local_remove_file(local_path) ⇒ Object
436 437 438 439 |
# File 'lib/bdsync/core.rb', line 436 def local_remove_file local_path puts "#{Utils.caller_info 1} rm #{local_path}".white FileUtils.rm local_path end |
#local_rename(local_path, new_local_path) ⇒ Object
446 447 448 449 |
# File 'lib/bdsync/core.rb', line 446 def local_rename local_path, new_local_path puts "#{Utils.caller_info 1} mv #{local_path} #{new_local_path}".yellow FileUtils.mv local_path, new_local_path end |
#save_data(data) ⇒ Object
60 61 62 63 64 |
# File 'lib/bdsync/core.rb', line 60 def save_data data local_ensure_parent @data_path File.write @data_path, data.to_yaml puts "\nsaved to #{@data_path}" end |
#synchronize ⇒ Object
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 |
# File 'lib/bdsync/core.rb', line 23 def synchronize # Run only one instance of a Ruby program at the same time - self locking # SEE: https://code-maven.com/run-only-one-instance-of-a-script Utils.try_lock { loop { @old_data = load_data @data = {} remote_ensure_dir @remote_root_path local_ensure_dir @local_root_path puts "\n==== traverse_remote_path ====" traverse_remote_path @remote_root_path # merge @data to @old_data, and clear @data @old_data.merge! @data @data = {} puts "\n==== traverse_local_path ====" traverse_local_path @local_root_path save_data @data break if !@infinite_loop sleep 1 } } end |
#traverse_local_path(local_path) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/bdsync/core.rb', line 95 def traverse_local_path local_path next_level_dirs = [] Dir.foreach(local_path) { |entry_name| next if [".", "..", ".conflict"].include? entry_name path = "#{local_path}/#{entry_name}" if File.directory? path next_level_dirs << path else handle_local_entry path, :file end } next_level_dirs.sort.each { |path| handle_local_entry path, :directory begin traverse_local_path path rescue Errno::ENOENT # OK: the local directory may be deleted with synchronization! end } end |
#traverse_remote_path(remote_path) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/bdsync/core.rb', line 66 def traverse_remote_path remote_path next_level_dirs = [] remote_dir_foreach(remote_path) { |entry| next if [".", "..", ".conflict"].include? entry.name path = "#{remote_path}/#{entry.name}" remote = entry.attributes if remote.directory? next_level_dirs << [path, remote] else handle_remote_entry remote, path, :file end } next_level_dirs.sort.each { |path, remote| handle_remote_entry remote, path, :directory begin traverse_remote_path path rescue Net::SFTP::StatusException # OK: the remote directory may be deleted with synchronization! rescue Errno::ENOENT # OK: the remote directory may be deleted with synchronization! end } end |
#update_directory_data(relative_path, local_path, remote_mtime) ⇒ Object
407 408 409 410 411 412 413 |
# File 'lib/bdsync/core.rb', line 407 def update_directory_data relative_path, local_path, remote_mtime @data[relative_path] = { type: :directory, remote_mtime: remote_mtime, local_mtime: File.mtime(local_path).to_i } end |
#update_file_data(relative_path, local_path, remote_mtime) ⇒ Object
399 400 401 402 403 404 405 |
# File 'lib/bdsync/core.rb', line 399 def update_file_data relative_path, local_path, remote_mtime @data[relative_path] = { type: :file, remote_mtime: remote_mtime, local_mtime: File.mtime(local_path).to_i } end |