Skip to content

Commit

Permalink
zold-io#164 Add file extension
Browse files Browse the repository at this point in the history
  • Loading branch information
pbenety committed Jun 22, 2018
1 parent c0e8f49 commit aa70525
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 20 deletions.
4 changes: 2 additions & 2 deletions lib/zold/commands/diff.rb
Expand Up @@ -71,8 +71,8 @@ def diff(wallet, cps, _)
end
before = AtomicFile.new(wallet.path).read
after = ''
Tempfile.open do |f|
patch.save(f, overwrite: true)
Tempfile.open(['', Wallet::EXTENSION]) do |f|
patch.save(f.path, overwrite: true)
after = File.read(f)
end
diff = Diffy::Diff.new(before, after, context: 0).to_s(:color)
Expand Down
2 changes: 1 addition & 1 deletion lib/zold/commands/fetch.rb
Expand Up @@ -94,7 +94,7 @@ def fetch_one(id, r, cps, opts)
r.assert_valid_score(score)
r.assert_score_ownership(score)
r.assert_score_strength(score) unless opts['ignore-score-weakness']
Tempfile.open do |f|
Tempfile.open(['', Wallet::EXTENSION]) do |f|
body = json['body']
File.write(f, body)
wallet = Wallet.new(f.path)
Expand Down
15 changes: 8 additions & 7 deletions lib/zold/copies.rb
Expand Up @@ -21,6 +21,7 @@
require 'time'
require 'csv'
require_relative 'atomic_file'
require_relative 'wallet'

# The list of copies.
# Author:: Yegor Bugayenko (yegor256@gmail.com)
Expand All @@ -45,8 +46,8 @@ def clean
list = load
list.reject! { |s| s[:time] < Time.now - 24 * 60 * 60 }
save(list)
Dir.new(@dir).select { |f| f =~ /^[0-9]+$/ }.each do |f|
File.delete(File.join(@dir, f)) if list.find { |s| s[:name] == f }.nil?
Dir.new(@dir).select { |f| File.basename(f, Wallet::EXTENSION) =~ /^[0-9]+$/ }.each do |f|
File.delete(File.join(@dir, f)) if list.find { |s| s[:name] == File.basename(f, Wallet::EXTENSION) }.nil?
end
end

Expand All @@ -66,17 +67,17 @@ def add(content, host, port, score, time = Time.now)
FileUtils.mkdir_p(@dir)
list = load
target = list.find do |s|
f = File.join(@dir, s[:name])
f = File.join(@dir, "#{s[:name]}#{Wallet::EXTENSION}")
File.exist?(f) && AtomicFile.new(f).read == content
end
if target.nil?
max = Dir.new(@dir)
.select { |f| f =~ /^[0-9]+$/ }
.select { |f| File.basename(f, Wallet::EXTENSION) =~ /^[0-9]+$/ }
.map(&:to_i)
.max
max = 0 if max.nil?
name = (max + 1).to_s
AtomicFile.new(File.join(@dir, name)).write(content)
AtomicFile.new(File.join(@dir, "#{name}#{Wallet::EXTENSION}")).write(content)
else
name = target[:name]
end
Expand All @@ -98,7 +99,7 @@ def all
name: name,
host: scores[0][:host],
port: scores[0][:port],
path: File.join(@dir, name),
path: File.join(@dir, "#{name}#{Wallet::EXTENSION}"),
score: scores.select { |s| s[:time] > Time.now - 24 * 60 * 60 }
.map { |s| s[:score] }
.inject(&:+) || 0
Expand Down Expand Up @@ -135,7 +136,7 @@ def save(list)
end

def file
File.join(@dir, 'scores')
File.join(@dir, "scores#{Wallet::EXTENSION}")
end
end
end
6 changes: 3 additions & 3 deletions lib/zold/node/safe_entrance.rb
Expand Up @@ -56,9 +56,9 @@ def push(id, body)
raise 'Id can\'t be nil' if id.nil?
raise 'Id must be of type Id' unless id.is_a?(Id)
raise 'Body can\'t be nil' if body.nil?
Tempfile.open do |f|
File.write(f.path, body)
wallet = Wallet.new(f)
Tempfile.open(['', Wallet::EXTENSION]) do |f|
File.write(f, body)
wallet = Wallet.new(f.path)
unless wallet.network == @network
raise "The network name mismatch, the wallet is in '#{wallet.network}', we are in '#{@network}'"
end
Expand Down
4 changes: 4 additions & 0 deletions lib/zold/wallet.rb
Expand Up @@ -44,8 +44,12 @@ class Wallet
# must have different names.
MAIN_NETWORK = 'zold'.freeze

# The extension of the wallet files
EXTENSION = '.z'.freeze

def initialize(file)
@file = file
@file = "#{file}#{EXTENSION}" if File.extname(file).empty?
end

def ==(other)
Expand Down
8 changes: 5 additions & 3 deletions lib/zold/wallets.rb
Expand Up @@ -46,13 +46,15 @@ def path

# Returns the list of their IDs (as plain text)
def all
Dir.new(path).select do |f|
selected = Dir.new(path).select do |f|
file = File.join(@dir, f)
basename = File.basename(f, Wallet::EXTENSION)
File.file?(file) &&
!File.directory?(file) &&
f =~ /^[0-9a-fA-F]{16}$/ &&
Id.new(f).to_s == f
basename =~ /^[0-9a-fA-F]{16}$/ &&
Id.new(basename).to_s == basename
end
selected.map { |w| File.basename(w, Wallet::EXTENSION) }
end

def find(id)
Expand Down
1 change: 1 addition & 0 deletions test/commands/routines/test_spread.rb
Expand Up @@ -24,6 +24,7 @@
require_relative '../../fake_home'
require_relative '../../node/fake_node'
require_relative '../../../lib/zold/commands/routines/spread.rb'
require_relative '../../../lib/zold/node/entrance.rb'

# Spread test.
# Author:: Yegor Bugayenko (yegor256@gmail.com)
Expand Down
4 changes: 2 additions & 2 deletions test/commands/test_create.rb
Expand Up @@ -37,8 +37,8 @@ def test_creates_wallet
)
assert wallet.balance.zero?
assert(
File.exist?(File.join(dir, wallet.id.to_s)),
"Wallet file not found: #{wallet.id}"
File.exist?(File.join(dir, "#{wallet.id}#{Zold::Wallet::EXTENSION}")),
"Wallet file not found: #{wallet.id}#{Zold::Wallet::EXTENSION}"
)
end
end
Expand Down
5 changes: 3 additions & 2 deletions test/test_copies.rb
Expand Up @@ -22,6 +22,7 @@
require 'tmpdir'
require 'time'
require_relative '../lib/zold/copies'
require_relative '../lib/zold/wallet'

# Copies test.
# Author:: Yegor Bugayenko (yegor256@gmail.com)
Expand Down Expand Up @@ -66,10 +67,10 @@ def test_cleans_copies
copies = Zold::Copies.new(dir)
copies.add('h1', 'zold.io', 50, 80, Time.now - 25 * 60 * 60)
copies.add('h1', 'zold.io', 33, 80, Time.now - 26 * 60 * 60)
assert(File.exist?(File.join(dir, '1')))
assert(File.exist?(File.join(dir, "1#{Zold::Wallet::EXTENSION}")))
copies.clean
assert(copies.all.empty?, "#{copies.all.count} is not empty")
assert(!File.exist?(File.join(dir, '1')))
assert(!File.exist?(File.join(dir, "1#{Zold::Wallet::EXTENSION}")))
end
end

Expand Down

0 comments on commit aa70525

Please sign in to comment.