Commit f9c8da8c authored by Xavier Thompson's avatar Xavier Thompson

Improve scan.pyx

parent 6f79ca87
......@@ -21,6 +21,7 @@ cdef Str sep = Str("/")
cdef char * file_fmt = """\
[{}]
type = file
sha256 = {}
sha512 = {}
{}
......@@ -29,12 +30,14 @@ sha512 = {}
cdef char * dir_fmt = """\
[{}]
type = dir
{}
"""
cdef char * symlink_fmt = """\
[{}]
type = symlink
target = {}
{}
......@@ -66,9 +69,9 @@ cdef cypclass Node activable:
void build_node(self): pass
void format_node(self): pass
void write_node(self, FILE * stream): pass
Str format_stat(self):
Str to_toml(self):
return format(stat_fmt,
self.stat.st_dev,
self.stat.st_ino,
......@@ -88,15 +91,13 @@ cdef cypclass Node activable:
self.stat.st_ctim.tv_nsec,
)
void write_node(self, FILE * stream): pass
@staticmethod
iso Node create(iso Str path):
iso Node node(iso Str path):
cdef Node node
p = <Str> consume path
s = os.stat(p)
if s is NULL:
node = NULL
return NULL
elif s.is_symlink():
node = SymlinkNode(p, s)
elif s.is_dir():
......@@ -104,7 +105,7 @@ cdef cypclass Node activable:
elif s.is_regular():
node = FileNode(p, s)
else:
node = NULL
return NULL
del p, s
return consume node
......@@ -125,23 +126,21 @@ cdef cypclass DirNode(Node):
continue
path = self.path
if Str(path[-1]) != sep:
path = path + sep
path = path + sep
path = path + name
child = Node.create(consume path)
child = Node.node(consume path)
if child is not NULL:
self.children.append(activate(consume child))
self.format_node()
for active_child in self.children:
active_child.build_node(NULL)
void format_node(self):
self.text = format(dir_fmt,
self.path,
self.format_stat(),
self.to_toml(),
)
for active_child in self.children:
active_child.build_node(NULL)
pass
void write_node(self, FILE * stream):
os.write(self.text, stream)
while self.children.__len__() > 0:
......@@ -150,9 +149,7 @@ cdef cypclass DirNode(Node):
child.write_node(stream)
cdef enum:
CHUNK = 64 * 1024
cdef enum: CHUNK = 64 * 1024
cdef cypclass FileNode(Node):
Str sha256
......@@ -162,89 +159,71 @@ cdef cypclass FileNode(Node):
Node.__init__(self, path, stat)
void build_node(self):
cdef bint sha256_ok
cdef bint sha512_ok
cdef bint error = False
cdef bint sha256_ok = False
cdef bint sha512_ok = False
cdef FILE * file = os.open(self.path, 'rb')
if file is NULL:
self.format_node()
return
file = os.open(self.path, 'rb')
if file is not NULL:
sha256 = hashlib.Hash(hashlib.sha256())
sha512 = hashlib.Hash(hashlib.sha512())
sha256 = hashlib.Hash(hashlib.sha256())
sha512 = hashlib.Hash(hashlib.sha512())
sha256_ok = sha256 is not NULL
sha512_ok = sha512 is not NULL
sha256_ok = sha256 is not NULL
sha512_ok = sha512 is not NULL
while (sha256_ok or sha512_ok):
s = os.read(file, CHUNK)
if s is NULL:
error = True
break
while (sha256_ok or sha512_ok):
s = os.read(file, CHUNK)
if s is NULL:
sha256_ok = sha512_ok = False
break
if sha256_ok:
sha256_ok = sha256.update(s) == 0
if sha512_ok:
sha512_ok = sha512.update(s) == 0
if sha256_ok:
sha256_ok = sha256.update(s) == 0
if sha512_ok:
sha512_ok = sha512.update(s) == 0
if s.__len__() != CHUNK:
break
if s.__len__() != CHUNK:
break
os.close(file)
os.close(file)
if not error:
self.sha256 = sha256.hexdigest() if sha256_ok else NULL
self.sha512 = sha512.hexdigest() if sha512_ok else NULL
self.format_node()
void format_node(self):
sha256 = self.sha256 if self.sha256 else Str("<error>")
sha512 = self.sha512 if self.sha512 else Str("<error>")
self.text = format(file_fmt,
self.path,
sha256,
sha512,
self.format_stat(),
self.sha256 if self.sha256 else Str("<error>"),
self.sha512 if self.sha512 else Str("<error>"),
self.to_toml(),
)
void write_node(self, FILE * stream):
os.write(self.text, stream)
cdef cypclass SymlinkNode(Node):
Str target
void build_node(self):
self.target = os.readlink(self.path, self.stat.st_size)
self.format_node()
target = os.readlink(self.path, self.stat.st_size)
if target is NULL:
target = Str("<error>")
void format_node(self):
target = self.target if self.target is not NULL else Str("<error>")
self.target = target
self.text = format(symlink_fmt,
self.path,
target,
self.format_stat(),
self.to_toml(),
)
void write_node(self, FILE * stream):
os.write(self.text, stream)
cdef int scan(iso Str root) nogil:
node = Node.create(consume root)
if node is NULL:
cdef int scan(iso Str path) nogil:
root = Node.node(consume path)
if root is NULL:
return -1
active_node = activate(consume node)
active_node.build_node(NULL)
root.build_node()
scheduler.join()
node = consume active_node
node.write_node(os.stdout)
root.write_node(os.stdout)
return 0
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment