=== BEGIN FILESYSTEM REMOVAL ===

This commit is contained in:
Netscape Navigator 2019-11-26 19:46:50 -06:00
parent ed0ee3520f
commit 99ed146ee3
13 changed files with 109 additions and 140 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
coverage/
.pigeon/
pigeon.db

View File

@ -6,7 +6,6 @@ gem "ed25519"
gem "thor"
group :dev do
gem "fakefs"
gem "pry"
gem "rspec"
gem "simplecov"

View File

@ -5,7 +5,6 @@ GEM
diff-lcs (1.3)
docile (1.3.2)
ed25519 (1.2.4)
fakefs (0.20.1)
json (2.2.0)
method_source (0.9.2)
pry (0.12.2)
@ -36,7 +35,6 @@ PLATFORMS
DEPENDENCIES
ed25519
fakefs
pry
rspec
simplecov

BIN
db.pigeon Normal file

Binary file not shown.

121
dist/pigeon/storage.rb vendored
View File

@ -1,11 +1,15 @@
require "pstore"
module Pigeon
class Storage
ROOT_DIR = ".pigeon"
CONF_DIR = "conf"
BLOB_DIR = "blobs"
PEER_DIR = "peers"
USER_DIR = "user"
BLOCK_DIR = "blocked"
PIGEON_DB_PATH = File.join("db.pigeon")
ROOT_NS = ".pigeon"
CONF_NS = "conf"
BLOB_NS = "blobs"
PEER_NS = "peers"
USER_NS = "user"
BLCK_NS = "blocked"
BLOB_HEADER = "&"
BLOB_FOOTER = ".sha256"
@ -15,57 +19,55 @@ module Pigeon
end
def initialize
unless initialized?
create_root_dir
create_conf_dir
create_blob_dir
create_peer_dir
create_user_dir
end
create_pstore unless initialized?
end
def set_config(key, value)
path = conf_path_for(key)
maybe_touch(path)
File.write(path, value.to_s)
store.transaction { store[key] = value }
end
def delete_config(key)
File.delete(conf_path_for(key))
File.delete(key)
end
def get_config(key)
f = conf_path_for(key)
f = (key)
File.read(f) if File.file?(f)
end
def set_blob(data)
hex_digest = Digest::SHA256.hexdigest(data)
path = blob_path_for(hex_digest)
File.write(path, data)
store.transaction do
store[BLOB_NS] ||= {}
store[BLOB_NS][hex_digest] = data
end
[BLOB_HEADER, hex_digest, BLOB_FOOTER].join("")
end
def get_blob(hex_digest)
hd = hex_digest.gsub(BLOB_HEADER, "").gsub(BLOB_FOOTER, "")
# Allows user to pass first n chars of a
# hash instead of the whole hash.
f = Dir[blob_path_for(hd) + "*"].first
File.file?(f) ? File.read(f) : nil
store.transaction(true) do
store[BLOB_NS] ||= {}
store[BLOB_NS][hex_digest]
end
end
def add_peer(identity)
path = KeyPair.strip_headers(identity)
FileUtils.mkdir_p(File.join(peer_dir, path))
store.transaction do
store[PEER_NS] ||= Set.new
store[PEER_NS].add(identity)
end
identity
end
def remove_peer(identity)
path = KeyPair.strip_headers(identity)
FileUtils.rm_rf(File.join(peer_dir, path))
store.transaction do
store[PEER_NS] ||= Set.new
store[PEER_NS].remove(identity)
end
identity
end
@ -88,69 +90,18 @@ module Pigeon
.map { |x| KeyPair.add_headers(x) }
end
private # ====================================
private
def initialized?
File.directory?(root_dir)
File.file?(PIGEON_DB_PATH)
end
def root_dir
@root_dir ||= File.join(ROOT_DIR)
def create_pstore
FileUtils.rm(PIGEON_DB_PATH) if File.file?(PIGEON_DB_PATH)
end
def blob_dir
@blob_dir ||= File.join(ROOT_DIR, BLOB_DIR, "sha256")
end
def peer_dir
@peer_dir ||= File.join(ROOT_DIR, PEER_DIR)
end
def block_dir
File.join(ROOT_DIR, BLOCK_DIR)
end
def user_dir
File.join(ROOT_DIR, USER_DIR)
end
def blob_path_for(hex_hash_string)
first_part = File.join(blob_dir, hex_hash_string[0, 2])
FileUtils.mkdir_p(first_part)
File.join(first_part, hex_hash_string[2..-1])
end
def maybe_touch(path)
FileUtils.touch(path) unless File.file?(path)
end
def conf_path_for(key)
File.join(conf_dir, key.to_s)
end
def conf_dir
@conf_dir ||= File.join(ROOT_DIR, CONF_DIR)
end
def create_conf_dir
FileUtils.mkdir_p(conf_dir)
end
def create_blob_dir
FileUtils.mkdir_p(blob_dir)
end
def create_root_dir
FileUtils.mkdir_p(root_dir)
end
def create_peer_dir
FileUtils.mkdir_p(peer_dir)
FileUtils.mkdir_p(block_dir)
end
def create_user_dir
FileUtils.mkdir_p(user_dir)
def store
@store ||= PStore.new(PIGEON_DB_PATH)
end
end
end

View File

@ -11,8 +11,6 @@ module Pigeon
desc "new", "Creates a new identiy in `.pigeon` directory if none exists"
def new
# TODO: --force flag
# TODO: --seed flag
if Dir.exist?(Pigeon::Storage::ROOT_DIR)
puts "Pigeon has detected a `.pigeon` directory.
Refusing to overwrite existing Pigeon config.

View File

@ -1,3 +1,9 @@
WE need to know:
BEFORE DELETING CONSTANTS:
==========================
rspec ./spec/pigeon/kitchen_sink_spec.rb:4 # Kitch sink spec does everything
rspec ./spec/pigeon/storage_spec.rb:15 # Pigeon::Storage deletes a config
rspec ./spec/pigeon/storage_spec.rb:34 # Pigeon::Storage manages blobs
rspec ./spec/pigeon/storage_spec.rb:41 # Pigeon::Storage manages peers
rspec ./spec/pigeon/storage_spec.rb:26 # Pigeon::Storage manages configs
rspec ./spec/pigeon/key_pair_spec.rb:36 # Pigeon::KeyPair saves to disk
* What's the current `sequence` number?

1
scratchpad2.rb Normal file
View File

@ -0,0 +1 @@

View File

@ -38,13 +38,11 @@ RSpec.describe Pigeon::KeyPair do
Pigeon::KeyPair::SEED_CONFIG_KEY,
FAKE_SEED,
]
FakeFS.with_fresh do
lol = receive(:set_config).with(*argss).and_call_original
expect(Pigeon::Storage.current).to lol
kp.save!
new_kp = Pigeon::KeyPair.current
expect(new_kp.public_key).to eq(kp.public_key)
expect(new_kp.private_key).to eq(kp.private_key)
end
lol = receive(:set_config).with(*argss).and_call_original
expect(Pigeon::Storage.current).to lol
kp.save!
new_kp = Pigeon::KeyPair.current
expect(new_kp.public_key).to eq(kp.public_key)
expect(new_kp.private_key).to eq(kp.private_key)
end
end

View File

@ -0,0 +1,34 @@
require "spec_helper"
RSpec.describe "Kitch sink spec" do
it "does everything" do
$lol = true
kp = Pigeon::KeyPair.new()
$lol = false
kp.save!
Pigeon::KeyPair.current.public_key
# ./pigeon-cli identity new
# ./pigeon-cli identity show
# cat scratchpad.jpg | ./pigeon-cli blob set
# ./pigeon-cli peer add @_TlC2z3FT4fimecC4eytrBhOwhLUZsVBZEZriBO9cWs=.ed25519
# ./pigeon-cli peer add @28FyT7evjcYrrwngr8G2V1HZ0ODK0VPsFctDEZwfZJc=.ed25519
# ./pigeon-cli peer remove @mYWRsosFtoxvn3GURmmE0FVtOWPcYv4ovXIAqy49sH4=.ed25519
# ./pigeon-cli peer remove @Nf7ZU9fLwukgfRfCunDtfjXRlhitiR-DcTmlNhB8lwk=.ed25519
# ./pigeon-cli peer block @q-_9BTnTThvW2ZGkmy8D3j-hW9ON2PNa3nwbCQgRw-g=.ed25519
# ./pigeon-cli peer block @VIim19-PzaavRICicQg4c4z08SoWTa1tr2e-kfhmm0Y=.ed25519
# ./pigeon-cli peer all
# ./pigeon-cli message create scratch_pad
# echo "my_value" | ./pigeon-cli message append key1
# ./pigeon-cli message append key2 my_value2
# ./pigeon-cli message append key3 "my_value3"
# ./pigeon-cli message append key4 \%jvKh9yoiEJaePzoWCF1nnqpIlPgTk9FHEtqczQbvzGM=.sha256
# ./pigeon-cli message append key5 \&29f3933302c49c60841d7620886ce54afc68630242aee6ff683926d2465e6ca3.sha256
# ./pigeon-cli message append key6 \@galdahnB3L2DE2cTU0Me54IpIUKVEgKmBwvZVtWJccg=.ed25519
# ./pigeon-cli message show
# ./pigeon-cli message sign
# ./pigeon-cli message create second_test
# ./pigeon-cli message append hello "world"
# ./pigeon-cli message sign
# ./pigeon-cli status
end
end

View File

@ -1,7 +1,5 @@
require "spec_helper"
RSpec.describe Pigeon::Message do
include FakeFS::SpecHelpers
it "??"
end

View File

@ -1,58 +1,44 @@
require "spec_helper"
RSpec.describe Pigeon::Storage do
include FakeFS::SpecHelpers
LOGO_BLOB = File.read("./logo.png")
IDS = %w(@_TlC2z3FT4fimecC4eytrBhOwhLUZsVBZEZriBO9cWs=.ed25519
@28FyT7evjcYrrwngr8G2V1HZ0ODK0VPsFctDEZwfZJc=.ed25519)
def test_fs
FakeFS.with_fresh do
yield(Pigeon::Storage.new)
end
end
let(:s) { Pigeon::Storage.current }
it "deletes a config" do
test_fs do |s|
s.set_config("FOO", "BAR")
value = s.get_config("FOO")
expect(value).to eq("BAR")
s.delete_config("FOO")
value = s.get_config("FOO")
expect(value).to eq(nil)
end
s.set_config("FOO", "BAR")
value = s.get_config("FOO")
expect(value).to eq("BAR")
s.delete_config("FOO")
value = s.get_config("FOO")
expect(value).to eq(nil)
end
it "manages configs" do
test_fs do |s|
s.set_config("FOO", "BAR")
value = s.get_config("FOO")
expect(value).to eq("BAR")
end
s.set_config("FOO", "BAR")
value = s.get_config("FOO")
expect(value).to eq("BAR")
end
it "manages blobs" do
test_fs do |s|
logo_hash = s.set_blob(LOGO_BLOB)
expect(s.get_blob(logo_hash)).to eq(LOGO_BLOB)
end
logo_hash = s.set_blob(LOGO_BLOB)
expect(s.get_blob(logo_hash)).to eq(LOGO_BLOB)
end
it "manages peers" do
test_fs do |s|
s.add_peer(IDS[0])
s.add_peer(IDS[1])
expect(s.all_peers).to include(IDS[0])
expect(s.all_peers).to include(IDS[1])
s.add_peer(IDS[0])
s.add_peer(IDS[1])
expect(s.all_peers).to include(IDS[0])
expect(s.all_peers).to include(IDS[1])
s.remove_peer(IDS[0])
expect(s.all_peers).not_to include(IDS[0])
expect(s.all_blocks).not_to include(IDS[0])
s.remove_peer(IDS[0])
expect(s.all_peers).not_to include(IDS[0])
expect(s.all_blocks).not_to include(IDS[0])
s.block_peer(IDS[1])
expect(s.all_peers).not_to include(IDS[1])
expect(s.all_blocks).to include(IDS[1])
expect(s.all_blocks.count).to eq(1)
end
s.block_peer(IDS[1])
expect(s.all_peers).not_to include(IDS[1])
expect(s.all_blocks).to include(IDS[1])
expect(s.all_blocks.count).to eq(1)
end
end

View File

@ -1,6 +1,5 @@
require "pry"
require "simplecov"
require "fakefs/spec_helpers"
SimpleCov.start
require_relative File.join("..", "dist", "pigeon")