Class: Bdsync::Core

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

Direct Known Subclasses

Lfs, Sftp

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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_pathObject (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

.optionsObject



19
20
21
# File 'lib/bdsync/core.rb', line 19

def self.options
    ["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



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
# File 'lib/bdsync/core.rb', line 194

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
            handle_remote_conflict remote_path
            remote = upload_file local_path, remote_path
            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



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
191
192
# File 'lib/bdsync/core.rb', line 165

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
            handle_local_conflict local_path
            download_file local_path, remote_path
            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



302
303
304
305
306
307
308
309
310
311
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
# File 'lib/bdsync/core.rb', line 302

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
                    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
                    download_file local_path, remote_path
                    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



223
224
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
# File 'lib/bdsync/core.rb', line 223

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
                    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
                    download_file local_path, remote_path
                    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



397
398
399
400
401
402
403
# File 'lib/bdsync/core.rb', line 397

def handle_local_conflict local_path
    ts = Utils.timestamp
    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



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/bdsync/core.rb', line 145

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



405
406
407
408
409
410
411
# File 'lib/bdsync/core.rb', line 405

def handle_remote_conflict remote_path
    ts = Utils.timestamp
    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



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/bdsync/core.rb', line 127

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

#load_dataObject



59
60
61
62
63
64
# File 'lib/bdsync/core.rb', line 59

def load_data
    puts "\nload #{@data_path}"
    YAML.load_file @data_path
rescue Errno::ENOENT
    {}
end

#local_ensure_dir(path) ⇒ Object



433
434
435
# File 'lib/bdsync/core.rb', line 433

def local_ensure_dir path
    FileUtils.mkdir_p path if !File.directory? path
end

#local_ensure_parent(path) ⇒ Object



437
438
439
# File 'lib/bdsync/core.rb', line 437

def local_ensure_parent path
    local_ensure_dir File.dirname path
end

#local_mkdir(local_path) ⇒ Object



413
414
415
416
# File 'lib/bdsync/core.rb', line 413

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



423
424
425
426
# File 'lib/bdsync/core.rb', line 423

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



418
419
420
421
# File 'lib/bdsync/core.rb', line 418

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



428
429
430
431
# File 'lib/bdsync/core.rb', line 428

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



66
67
68
69
70
# File 'lib/bdsync/core.rb', line 66

def save_data data
    local_ensure_parent @data_path
    File.write @data_path, data.to_yaml
    puts "\nsaved to #{@data_path}"
end

#start_session(&block) ⇒ Object



55
56
57
# File 'lib/bdsync/core.rb', line 55

def start_session &block
    fail NotImplementedError, "A subclass class must be able to #{__method__}!"
end

#synchronizeObject



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
# 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 = {}

            start_session {
                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



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/bdsync/core.rb', line 101

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



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/bdsync/core.rb', line 72

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



389
390
391
392
393
394
395
# File 'lib/bdsync/core.rb', line 389

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



381
382
383
384
385
386
387
# File 'lib/bdsync/core.rb', line 381

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