From f1a17cab22816bbfa1f0f9ab3f3fe883f8e3a9f7 Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Thu, 20 Jan 2022 19:39:38 +0100 Subject: [PATCH] Failed attempt at fixing memory bug --- src/package.v | 14 +++++++--- src/repo.v | 75 ++++++++++++++++++++++++++------------------------- src/routes.v | 2 +- 3 files changed, 51 insertions(+), 40 deletions(-) diff --git a/src/package.v b/src/package.v index 9e8e503..c229e3f 100644 --- a/src/package.v +++ b/src/package.v @@ -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{ diff --git a/src/repo.v b/src/repo.v index 6ddde2a..cca6b84 100644 --- a/src/repo.v +++ b/src/repo.v @@ -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) } diff --git a/src/routes.v b/src/routes.v index c7e38ba..8ad4a4a 100644 --- a/src/routes.v +++ b/src/routes.v @@ -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.') }