all kinds of changes (I really should commit more often...)

This commit is contained in:
Dominik Moritz Roth 2020-06-18 20:32:06 +02:00
parent 085e503dec
commit 9f18e4a774

88
next.py
View File

@ -1,6 +1,5 @@
#import pyfuse3
# TODO: Milestones always written into new block
# TODO: Implement File COW (except for append) (+ version / token updates caused by this)
# TODO: ? Stop using tokens for dirs, use hashed name + parent token
# TODO: ? Stop using tokens for files, use hashed name + short ?version-number + parent token instead
@ -32,7 +31,6 @@ import gzip
import secrets
CHUNKSIZE = 2187
def log(txt):
print("[-] "+str(txt))
@ -122,7 +120,9 @@ class TangleBlob():
def append(self, data: bytes, newBlock: bool = False) -> None:
self._requireFetched()
if len(self.chunks) and not newBlock:
if newBlock:
self.chunks[-1].seal()
elif len(self.chunks):
bytesLeft = self.chunks[-1].getBytesLeft()
if bytesLeft:
leftChunk = data[:bytesLeft]
@ -190,6 +190,20 @@ class TangleBlob():
def sealLastChunk(self) -> None:
self.chunks[-1].seal()
def chunkLayout(self, width=50):
fac = width / CHUNKSIZE
lines = []
for c,chunk in enumerate(self.chunks):
bytesWritten = len(chunk.data)
bytesEmpty = CHUNKSIZE - bytesWritten
if chunk.isSealed():
lines.append("["+"#"*int(bytesWritten*fac)+"="*int(bytesEmpty*fac)+"] (SEALED)")
else:
lines.append("["+"#"*int(bytesWritten*fac)+" "*int(bytesEmpty*fac)+"] ("+str(bytesWritten)+"/"+str(CHUNKSIZE)+")")
if self.pushedNum < c+1:
lines[-1]+=" {+}"
return "\n".join(lines)
class TangleFileTreeElement(TangleBlob):
def __init__(self, name: str, lastMilestoneIndex: int, parent, iotaApi: Iota) -> None:
if isinstance(parent, bytes):
@ -251,6 +265,36 @@ class TangleFileTreeElement(TangleBlob):
self._requireFetched()
return list(self.inodes.keys())
def _tree(self):
dirs = {}
files = []
for inode in self.inodes:
if self.inodes[inode].type=="file":
files.append(inode)
elif self.inodes[inode].type=="dir":
dirs[inode] = self.inodes[inode].getRef()._tree()
else:
files.append("["+str(self.inodes[inode].type)+"]/"+inode)
lines = ["{"+self.name+"}"]
for i,d in enumerate(dirs):
dir = dirs[d]
if len(files)==0 and i==len(dirs)-1:
lines.append(" └──"+dir[0]+"")
for l in range(len(dir)-1):
lines.append("   "+dir[l+1])
else:
lines.append(" ├──"+dir[0]+"")
for l in range(len(dir)-1):
lines.append(" │  "+dir[l+1])
if len(files):
for f in range(len(files)-1):
lines.append(" ├──"+files[f])
lines.append(" └──"+files[-1])
return lines
def tree(self):
return "\n".join(self._tree())
def getInode(self, name: str) -> Atom:
self._requireFetched()
return self.inodes[name]
@ -269,10 +313,10 @@ class TangleFileTreeElement(TangleBlob):
self._requireFetched()
if name in self.getNameList():
return False
file = Inode(name, self.iotaApi, self, "file")
atom = file.change(size=0, milestoneIndex=0, hash=b'NULL')
inode = Inode(name, self.iotaApi, self, "file")
atom = inode.change(size=0, milestoneIndex=0, hash=b'NULL')
self._newAtom(atom)
self._applyAtom(atom)
self.inodes[name] = inode
return True
def _updateFileSize(self, name: str, size: int) -> None:
@ -288,6 +332,8 @@ class TangleFileTreeElement(TangleBlob):
self._newAtom(self.inodes[name])
def performMilestone(self) -> None:
if isinstance(self.parent, bytes):
raise Exception("You cant create a milestone of the genesis block, you idiot!")
stones = {}
for a in self.inodes:
stones[a] = self.inodes[a].toStone()
@ -300,11 +346,14 @@ class TangleFileTreeElement(TangleBlob):
if self.parent!=None:
self.parent._updateChildMilestone(self.name, self.milestoneIndex)
self.append(data, True)
# inform parent about milestone (when merged)
def _updateChildMilestone(self, name: str, milestoneIndex: int):
self.inodes[name].milestoneIndex = milestoneIndex
self._newAtom(self.inodes[name])
if isinstance(self.parent, bytes):
# We are the genesis-block
self.append(milestoneIndex, True)
else:
atom = self.inodes[name].change(milestoneIndex = milestoneIndex)
self._newAtom(atom)
class TangleFile():
def __init__(self, name: str, parent: TangleFileTreeElement, iotaApi: Iota) -> None:
@ -367,6 +416,8 @@ class Inode():
delta = atom.cont
if b's' in delta:
self.size = delta[b's']
if b'h' in delta:
self.hash = delta[b'h']
if b'm' in delta:
self.milestoneIndex = delta[b'm']
@ -416,3 +467,22 @@ token = b'testToken'
genesis = TangleFileTreeElement("*", 0, token, api)
genesis.mkdir("/")
root = genesis.inodes["/"].getRef()
root.mkdir("dir1")
root.mkdir("dir2")
root.mkdir("dir3")
root.mkdir("dir4")
root.mkfile("file.txt")
d2 = root.inodes["dir2"].getRef()
d2.mkfile("a.txt")
d3 = root.inodes["dir3"].getRef()
d3.mkfile("b.txt")
d3.mkfile("c.txt")
d3.mkfile("d.txt")
d3.mkdir("subDir")
d4 = root.inodes["dir4"].getRef()
d4.mkdir("sub")
sub = d4.inodes["sub"].getRef()
sub.mkdir("subsub")
print(genesis.tree())