Failed attempt at fixing memory bug

main
Jef Roosens 2022-01-20 19:39:38 +01:00
parent 01d961f68e
commit f1a17cab22
Signed by: Jef Roosens
GPG Key ID: 955C0660072F691F
3 changed files with 51 additions and 40 deletions

View File

@ -126,6 +126,7 @@ pub fn read_pkg(pkg_path string) ?Pkg {
mut buf := voidptr(0)
mut files := []string{}
mut pkg_info := PkgInfo{}
for C.archive_read_next_header(a, &entry) == C.ARCHIVE_OK {
pathname := C.archive_entry_pathname(entry)
@ -142,14 +143,21 @@ pub fn read_pkg(pkg_path string) ?Pkg {
// TODO can this unsafe block be avoided?
buf = unsafe { malloc(size) }
C.archive_read_data(a, voidptr(buf), size)
C.archive_read_data(a, buf, size)
unsafe {
println(cstring_to_vstring(buf))
}
pkg_info = parse_pkg_info_string(unsafe { cstring_to_vstring(buf) }) ?
unsafe {
free(buf)
}
} else {
C.archive_read_data_skip(a)
}
}
mut pkg_info := parse_pkg_info_string(unsafe { cstring_to_vstring(&char(buf)) }) ?
pkg_info.csize = i64(os.file_size(pkg_path))
return Pkg{

View File

@ -45,7 +45,7 @@ pub fn new(repo_dir string, pkg_dir string) ?Repo {
// add_from_path adds a package from an arbitrary path & moves it into the pkgs
// directory if necessary.
pub fn (r &Repo) add_from_path(pkg_path string) ?bool {
pkg := package.read_pkg(pkg_path) or { return error('Failed to read package file.') }
pkg := package.read_pkg(pkg_path) or { return error('Failed to read package file: $err.msg') }
added := r.add(pkg) ?
@ -96,47 +96,50 @@ fn (r &Repo) pkg_path(pkg &package.Pkg) string {
// Re-generate the repo archive files
fn (r &Repo) sync() ? {
a := C.archive_write_new()
entry := C.archive_entry_new()
st := C.stat{}
buf := [8192]byte{}
lock r.mutex {
a := C.archive_write_new()
entry := C.archive_entry_new()
st := C.stat{}
buf := [8192]byte{}
// This makes the archive a gzip-compressed tarball
C.archive_write_add_filter_gzip(a)
C.archive_write_set_format_pax_restricted(a)
// This makes the archive a gzip-compressed tarball
C.archive_write_add_filter_gzip(a)
C.archive_write_set_format_pax_restricted(a)
repo_path := os.join_path_single(r.repo_dir, 'repo.db')
repo_path := os.join_path_single(r.repo_dir, 'repo.db')
C.archive_write_open_filename(a, &char(repo_path.str))
C.archive_write_open_filename(a, &char(repo_path.str))
// Iterate over each directory
for d in os.ls(r.repo_dir) ?.filter(os.is_dir(os.join_path_single(r.repo_dir, it))) {
inner_path := os.join_path_single(d, 'desc')
actual_path := os.join_path_single(r.repo_dir, inner_path)
// Iterate over each directory
for d in os.ls(r.repo_dir) ?.filter(os.is_dir(os.join_path_single(r.repo_dir,
it))) {
inner_path := os.join_path_single(d, 'desc')
actual_path := os.join_path_single(r.repo_dir, inner_path)
unsafe {
C.stat(&char(actual_path.str), &st)
unsafe {
C.stat(&char(actual_path.str), &st)
}
C.archive_entry_set_pathname(entry, &char(inner_path.str))
C.archive_entry_copy_stat(entry, &st)
// C.archive_entry_set_size(entry, st.st_size)
// C.archive_entry_set_filetype(entry, C.AE_IFREG)
// C.archive_entry_set_perm(entry, 0o644)
C.archive_write_header(a, entry)
fd := C.open(&char(actual_path.str), C.O_RDONLY)
mut len := C.read(fd, &buf, sizeof(buf))
for len > 0 {
C.archive_write_data(a, &buf, len)
len = C.read(fd, &buf, sizeof(buf))
}
C.close(fd)
C.archive_entry_clear(entry)
}
C.archive_entry_set_pathname(entry, &char(inner_path.str))
C.archive_entry_copy_stat(entry, &st)
// C.archive_entry_set_size(entry, st.st_size)
// C.archive_entry_set_filetype(entry, C.AE_IFREG)
// C.archive_entry_set_perm(entry, 0o644)
C.archive_write_header(a, entry)
fd := C.open(&char(actual_path.str), C.O_RDONLY)
mut len := C.read(fd, &buf, sizeof(buf))
for len > 0 {
C.archive_write_data(a, &buf, len)
len = C.read(fd, &buf, sizeof(buf))
}
C.close(fd)
C.archive_entry_clear(entry)
C.archive_write_close(a)
C.archive_write_free(a)
}
C.archive_write_close(a)
C.archive_write_free(a)
}

View File

@ -74,7 +74,7 @@ fn (mut app App) put_package() web.Result {
}
added := app.repo.add_from_path(pkg_path) or {
app.lerror('Error while adding package.')
app.lerror('Error while adding package: $err.msg')
return app.text('Failed to add package.')
}