Commit d0cebd67 authored by Ophélie Gagnard's avatar Ophélie Gagnard

src/main.{pyx,cpp}:

- Switch to SHA256 (instead of MD5).
- Add back the files stats.
parent 2e9768b1
This source diff could not be displayed because it is too large. You can view the blob instead.
...@@ -12,7 +12,7 @@ from runtime.unistd cimport pid_t, execlp, fork, sleep ...@@ -12,7 +12,7 @@ from runtime.unistd cimport pid_t, execlp, fork, sleep
from runtime.wait cimport wait, waitpid from runtime.wait cimport wait, waitpid
from stdlib.stat cimport Stat, dev_t from stdlib.stat cimport Stat, dev_t
from stdlib.digest cimport MessageDigest, md5sum, sha1sum, sha256sum, sha512sum from stdlib.digest cimport MessageDigest, sha256sum, sha512sum
from stdlib.fmt cimport sprintf from stdlib.fmt cimport sprintf
from stdlib.string cimport string from stdlib.string cimport string
from stdlib.dirent cimport DIR, struct_dirent, opendir, readdir, closedir from stdlib.dirent cimport DIR, struct_dirent, opendir, readdir, closedir
...@@ -43,12 +43,12 @@ cdef cypclass Node activable: ...@@ -43,12 +43,12 @@ cdef cypclass Node activable:
pass pass
void format_node(self): void format_node(self):
#''' light data version ''' light data version
self.formatted = sprintf("""{"path": "%s"}\n""", self.formatted = sprintf("""{"path": "%s"}\n""",
self.path, self.path,
) )
#''' #'''
''' full data version #''' full data version
self.formatted = sprintf("""{"path": "%s", "stat": %s}\n""", self.formatted = sprintf("""{"path": "%s", "stat": %s}\n""",
self.path, self.path,
self.st.to_json(), self.st.to_json(),
...@@ -124,12 +124,12 @@ cdef cypclass DirNode(Node): ...@@ -124,12 +124,12 @@ cdef cypclass DirNode(Node):
active_child.build_node(NULL, dev_whitelist, ignore_paths) active_child.build_node(NULL, dev_whitelist, ignore_paths)
void format_node(self): void format_node(self):
#''' light data version ''' light data version
self.formatted = sprintf("""{"path": "%s/"}\n""", self.formatted = sprintf("""{"path": "%s/"}\n""",
self.path, self.path,
) )
#''' #'''
''' full data version #''' full data version
self.formatted = sprintf("""{"path": "%s/", "stat": %s}\n""", self.formatted = sprintf("""{"path": "%s/", "stat": %s}\n""",
self.path, self.path,
self.st.to_json(), self.st.to_json(),
...@@ -149,8 +149,6 @@ cdef enum: ...@@ -149,8 +149,6 @@ cdef enum:
BUFSIZE = 64 * 1024 BUFSIZE = 64 * 1024
cdef cypclass FileNode(Node): cdef cypclass FileNode(Node):
string md5_data
string sha1_data
string sha256_data string sha256_data
string sha512_data string sha512_data
bint error bint error
...@@ -162,8 +160,6 @@ cdef cypclass FileNode(Node): ...@@ -162,8 +160,6 @@ cdef cypclass FileNode(Node):
void build_node(self, lock cyplist[dev_t] dev_whitelist, lock cyplist[string] ignore_paths): void build_node(self, lock cyplist[dev_t] dev_whitelist, lock cyplist[string] ignore_paths):
cdef unsigned char buffer[BUFSIZE] cdef unsigned char buffer[BUFSIZE]
cdef bint eof = False cdef bint eof = False
cdef bint md5_ok
cdef bint sha1_ok
cdef bint sha256_ok cdef bint sha256_ok
cdef bint sha512_ok cdef bint sha512_ok
...@@ -174,17 +170,13 @@ cdef cypclass FileNode(Node): ...@@ -174,17 +170,13 @@ cdef cypclass FileNode(Node):
self.format_node() self.format_node()
return return
md5 = MessageDigest(md5sum())
sha1 = MessageDigest(sha1sum())
sha256 = MessageDigest(sha256sum()) sha256 = MessageDigest(sha256sum())
sha512 = MessageDigest(sha512sum()) sha512 = MessageDigest(sha512sum())
md5_ok = md5 is not NULL
sha1_ok = sha1 is not NULL
sha256_ok = sha256 is not NULL sha256_ok = sha256 is not NULL
sha512_ok = sha512 is not NULL sha512_ok = sha512 is not NULL
while not eof and (md5_ok or sha1_ok or sha256_ok or sha512_ok): while not eof and (sha256_ok or sha512_ok):
size = fread(buffer, 1, BUFSIZE, file) size = fread(buffer, 1, BUFSIZE, file)
if size != BUFSIZE: if size != BUFSIZE:
self.error = ferror(file) self.error = ferror(file)
...@@ -192,16 +184,12 @@ cdef cypclass FileNode(Node): ...@@ -192,16 +184,12 @@ cdef cypclass FileNode(Node):
break break
eof = True eof = True
if md5_ok: md5_ok = md5.update(buffer, size) == 0
if sha1_ok: sha1_ok = sha1.update(buffer, size) == 0
if sha256_ok: sha256_ok = sha256.update(buffer, size) == 0 if sha256_ok: sha256_ok = sha256.update(buffer, size) == 0
if sha512_ok: sha512_ok = sha512.update(buffer, size) == 0 if sha512_ok: sha512_ok = sha512.update(buffer, size) == 0
fclose(file) fclose(file)
if not self.error: if not self.error:
if md5_ok: self.md5_data = md5.hexdigest()
if sha1_ok: self.sha1_data = sha1.hexdigest()
if sha256_ok: self.sha256_data = sha256.hexdigest() if sha256_ok: self.sha256_data = sha256.hexdigest()
if sha512_ok: self.sha512_data = sha512.hexdigest() if sha512_ok: self.sha512_data = sha512.hexdigest()
...@@ -211,18 +199,16 @@ cdef cypclass FileNode(Node): ...@@ -211,18 +199,16 @@ cdef cypclass FileNode(Node):
if self.error: if self.error:
Node.format_node(self) Node.format_node(self)
else: else:
#''' light data version ''' light data version
self.formatted = sprintf("""{"path": "%s", "hash": {"md5": "%s"}}\n""", self.formatted = sprintf("""{"path": "%s", "hash": {"sha256": "%s"}}\n""",
self.path, self.path,
self.md5_data, self.sha256_data,
) )
#''' #'''
''' full data version #''' full data version
self.formatted = sprintf("""{"path": "%s", "stat": %s, "hash": {"md5": "%s", "sha1": "%s", "sha256": "%s", "sha512": "%s"}}\n""", self.formatted = sprintf("""{"path": "%s", "stat": %s, "hash": {"sha256": "%s", "sha512": "%s"}}\n""",
self.path, self.path,
self.st.to_json(), self.st.to_json(),
self.md5_data,
self.sha1_data,
self.sha256_data, self.sha256_data,
self.sha512_data, self.sha512_data,
) )
...@@ -248,13 +234,13 @@ cdef cypclass SymlinkNode(Node): ...@@ -248,13 +234,13 @@ cdef cypclass SymlinkNode(Node):
if self.error: if self.error:
Node.format_node(self) Node.format_node(self)
else: else:
#''' light data version ''' light data version
self.formatted = sprintf("""{"path": "%s", "target": "%s"}\n""", self.formatted = sprintf("""{"path": "%s", "target": "%s"}\n""",
self.path, self.path,
self.target, self.target,
) )
#''' #'''
''' full data version #''' full data version
self.formatted = sprintf("""{"path": "%s", "stat": %s, "target": "%s"}\n""", self.formatted = sprintf("""{"path": "%s", "stat": %s, "target": "%s"}\n""",
self.path, self.path,
self.st.to_json(), self.st.to_json(),
......
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