Unixfs draft
This commit is contained in:
parent
63eddb0253
commit
b62fde8869
3
README.md
Normal file
3
README.md
Normal file
|
@ -0,0 +1,3 @@
|
|||
## WARNING
|
||||
|
||||
Contains my own contrived standards and formats that will break often.
|
|
@ -35,6 +35,7 @@ proc infoCmd(store: IpldStore; params: seq[TaintedString]) {.async.} =
|
|||
for cidStr in params[1..params.high]:
|
||||
let
|
||||
cid = parseCid cidStr
|
||||
stdout.writeLine cid
|
||||
stdout.writeLine cid.toHex
|
||||
if cid.isDagCbor:
|
||||
let dag = await store.getDag(cid)
|
||||
|
|
20
ipld.nim
20
ipld.nim
|
@ -1,4 +1,4 @@
|
|||
import nimSHA2, streams, multiformats, base58.bitcoin, cbor, hex
|
||||
import nimSHA2, streams, multiformats, base58.bitcoin, cbor, hex, hashes
|
||||
|
||||
type Cid* = object
|
||||
digest*: string
|
||||
|
@ -14,6 +14,11 @@ proc `==`*(x, y: Cid): bool =
|
|||
x.hash == y.hash and
|
||||
x.digest == y.digest
|
||||
|
||||
proc hash*(cid: Cid): Hash =
|
||||
result = hash cid.digest
|
||||
result = result !& cid.ver !& cid.codec.int !& cid.hash.int
|
||||
result = !$result
|
||||
|
||||
proc isRaw*(cid: Cid): bool =
|
||||
cid.codec == MulticodecTag.Raw
|
||||
|
||||
|
@ -126,19 +131,6 @@ proc merge*(dag, other: Dag) =
|
|||
result.add link
|
||||
# append
|
||||
|
||||
proc containsFile*(dag: Dag; name: string): bool =
|
||||
for link in dag["links"].items:
|
||||
if link["name"].getText == name:
|
||||
return true
|
||||
false
|
||||
|
||||
proc lookupFile*(dag: Dag; name: string): tuple[cid: Cid, size: int] =
|
||||
for link in dag["links"].items:
|
||||
if link["name"].getText == name:
|
||||
result.cid = parseCid link["cid"].getBytes()
|
||||
result.size = link["size"].getInt().int
|
||||
return
|
||||
raise newException(SystemError, "DAG file lookup failed")
|
||||
|
||||
#[
|
||||
proc unixFsContains*(dag: Dag; name: string): bool =
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
# Package
|
||||
|
||||
version = "0.1.0"
|
||||
version = "0.1.1"
|
||||
author = "Emery Hemingway"
|
||||
description = "IPLD library"
|
||||
license = "GPLv3"
|
||||
|
||||
# Dependencies
|
||||
|
||||
requires "nim >= 0.17.3", "nimSHA2", "base58"
|
||||
requires "nim >= 0.17.3", "nimSHA2", "base58", "cbor >= 0.2.0"
|
||||
|
||||
bin = @["ipfs_client"]
|
||||
|
|
|
@ -16,23 +16,41 @@ proc close*(s: IpldStore) =
|
|||
|
||||
proc putRaw*(s: IpldStore; blk: string): Future[Cid] {.async.} =
|
||||
## Place a raw block to the store.
|
||||
doAssert(not s.putRawImpl.isNil)
|
||||
result = await s.putRawImpl(s, blk)
|
||||
|
||||
proc getRaw*(s: IpldStore; cid: Cid): Future[string] {.async.} =
|
||||
## Retrieve a raw block from the store.
|
||||
doAssert(not s.getRawImpl.isNil)
|
||||
result = await s.getRawImpl(s, cid)
|
||||
|
||||
proc putDag*(s: IpldStore; dag: Dag): Future[Cid] {.async.} =
|
||||
## Place an IPLD node in the store.
|
||||
doAssert(not s.putDagImpl.isNil)
|
||||
result = await s.putDagImpl(s, dag)
|
||||
|
||||
proc getDag*(s: IpldStore; cid: Cid): Future[Dag] {.async.} =
|
||||
## Retrieve an IPLD node from the store.
|
||||
doAssert(not s.getDagImpl.isNil)
|
||||
result = await s.getDagImpl(s, cid)
|
||||
|
||||
proc fileStream*(s: IpldStore; cid: Cid; fut: FutureStream[string]): Future[void] {.async.} =
|
||||
## Asynchronously stream a file from a CID list.
|
||||
await s.fileStreamImpl(s, cid, fut)
|
||||
if not s.fileStreamImpl.isNil:
|
||||
# use an optimized implementation
|
||||
await s.fileStreamImpl(s, cid, fut)
|
||||
else:
|
||||
# use the simple implementation
|
||||
if cid.isRaw:
|
||||
let blk = await s.getRaw(cid)
|
||||
await fut.write(blk)
|
||||
elif cid.isDagCbor:
|
||||
let dag = await s.getDag(cid)
|
||||
for link in dag["links"].items:
|
||||
let subCid = link["cid"].getBytes.parseCid
|
||||
await fileStream(s, subCid, fut)
|
||||
else:
|
||||
discard
|
||||
|
||||
proc addFile*(store: IpldStore; path: string): (Cid, int) =
|
||||
## Add a file to the store and return the CID and file size.
|
||||
|
@ -79,7 +97,8 @@ type
|
|||
root: string
|
||||
|
||||
proc parentAndFile(fs: FileStore; cid: Cid): (string, string) =
|
||||
let h = cid.toHex
|
||||
let
|
||||
h = cid.toHex
|
||||
result[0] = fs.root / h[0..10]
|
||||
result[1] = result[0] / h[11..h.high]
|
||||
|
||||
|
|
13
unixfs.nim
13
unixfs.nim
|
@ -56,7 +56,6 @@ proc toCbor*(node: UnixFsNode): CborNode =
|
|||
if node.size != 0:
|
||||
result[sizeKey.int] = newCborInt node.size
|
||||
|
||||
#[
|
||||
proc fromCbor*(result: var UnixFsNode; cn: CborNode)=
|
||||
doAssert(cn.kind == cborMap)
|
||||
result = newUnixFsRoot()
|
||||
|
@ -73,7 +72,6 @@ proc fromCbor*(result: var UnixFsNode; cn: CborNode)=
|
|||
result.addFile(name, cid, size)
|
||||
else:
|
||||
discard
|
||||
]#
|
||||
|
||||
proc toStream*(dir: UnixFsNode; s: Stream) =
|
||||
doAssert(dir.kind == rootNode)
|
||||
|
@ -84,3 +82,14 @@ iterator walk*(node: UnixFsNode): string =
|
|||
if node.kind == rootNode:
|
||||
for k in node.entries.keys:
|
||||
yield k
|
||||
|
||||
proc containsFile*(dir: UnixFsNode; name: string): bool =
|
||||
doAssert(dir.kind == rootNode)
|
||||
dir.entries.contains name
|
||||
|
||||
proc lookupFile*(dir: UnixFsNode; name: string): tuple[cid: Cid, size: BiggestInt] =
|
||||
doAssert(dir.kind == rootNode)
|
||||
let f = dir.entries[name]
|
||||
if f.kind == fileNode:
|
||||
result.cid = f.fCid
|
||||
result.size = f.size
|
||||
|
|
Loading…
Reference in New Issue
Block a user