WIP: get_file_size() - extrack file to Lua

parent 0de7226f
......@@ -36,6 +36,11 @@ local stat = stat
local events_wait = events_wait
local run_util = run_util
local print = print
module "utils"
-- luacheck: globals lines2set map set2arr arr2set cleanup_dirs dir_ensure mkdirp read_file write_file clone shallow_copy table_merge arr_append exception multi_index private filter_best strip table_overlay randstr arr_prune arr_inv file_exists
......@@ -106,6 +111,9 @@ end
-- Run rm -rf on all dirs in the provided table
function cleanup_dirs(dirs)
print("cleanup " .. type(dirs))
if next(dirs) then
events_wait(run_util(function (ecode, _, _, stderr)
if ecode ~= 0 then
......
......@@ -50,14 +50,17 @@ local sha256_file = sha256_file
-- local test_extract = test_extract
local extract_inner_archive = extract_inner_archive
local get_file_size = get_file_size
local get_file_content = get_file_content
local DBG = DBG
local INFO = INFO
local WARN = WARN
local ERROR = ERROR
local syscnf = require "syscnf"
local utils = require "utils"
local locks = require "locks"
local print = print
module "backend"
-- Variables that we want to access from outside (ex. for testing purposes)
......@@ -448,7 +451,8 @@ TODO:
-- First we duplicate old functionality, to keep the rewrite simple
function pkg_unpack(package_path)
INFO("***a_08/pkg_unpack: "..package_path)
-- We do not need temp directory, so let's just use s2dir (renamed to just dir)
utils.mkdirp(syscnf.pkg_unpacked_dir)
local dir = mkdtemp(syscnf.pkg_unpacked_dir)
......@@ -458,6 +462,11 @@ function pkg_unpack(package_path)
get_file_size(package_path, "control", "conffiles")
local data = get_file_content(package_path, "control", "conffiles")
print("(((in Lua, data)))\n" .. data .. "---")
return dir
end
......@@ -878,6 +887,9 @@ function pkg_merge_files(dir, dirs, files, configs)
end
end
-- Remove the original directory
print("cleanup from merge_files")
utils.cleanup_dirs({dir})
return true
end
......
......@@ -24,6 +24,7 @@ interrupted and the dangerous parts already started.
This is a fairly high-level module, connecting many things together.
]]
local tostring = tostring
local ipairs = ipairs
local next = next
......@@ -53,6 +54,9 @@ local sync = sync
local log_event = log_event
local system_reboot = system_reboot
local print = print
local type = type
module "transaction"
-- luacheck: globals perform recover perform_queue recover_pretty queue_remove queue_install queue_install_downloaded cleanup_actions
......@@ -296,6 +300,13 @@ local function perform_internal(operations, journal_status, run_state)
-- Look at what the current status looks like.
local to_remove, to_install, plan
to_remove, to_install, plan, dir_cleanups, cleanup_actions = step(journal.UNPACKED, pkg_unpack, true, operations, status)
print(syscnf.pkg_download_dir)
print("cleanup from perrfom_internals1" .. type({syscnf.pkg_download_dir}))
utils.cleanup_dirs({syscnf.pkg_download_dir})
cleanup_actions = cleanup_actions or {} -- just to handle if journal contains no cleanup actions (journal from previous version)
-- Drop the operations. This way, if we are tail-called, then the package buffers may be garbage-collected
......@@ -307,6 +318,11 @@ local function perform_internal(operations, journal_status, run_state)
status, errors_collected = step(journal.SCRIPTS, pkg_scripts, true, status, plan, removes, to_install, errors_collected, all_configs)
end)
-- Make sure the temporary dirs are removed even if it fails. This will probably be slightly different with working journal.
print("cleanup from perrfom_internals2 " .. tostring(dir_cleanups))
utils.cleanup_dirs(dir_cleanups)
if not ok then
--[[
......
......@@ -641,20 +641,34 @@ static int lua_extract_inner_archive(lua_State *L) {
/* TODO: error handling */
/* TODO: return something sensible to lua? */
return r;
return 0;
}
static int lua_get_file_size(lua_State *L) {
/* Returns -1 if file is not found in archive */
const char *arc_name = luaL_checkstring(L, 1);
const char *subarc_name = luaL_checkstring(L, 2);
const char *path = luaL_checkstring(L, 3);
int r = get_file_size(arc_name, subarc_name, path);
int size = get_file_size(arc_name, subarc_name, path);
printf("File size of %s in %d.\n", path, r);
printf("File size of %s in %d.\n", path, size);
lua_pushinteger(L, size);
return 1;
}
/* TODO: error handling */
/* TODO: return something sensible to lua? */
return r;
static int lua_get_file_content(lua_State *L) {
const char *arc_name = luaL_checkstring(L, 1);
const char *subarc_name = luaL_checkstring(L, 2);
const char *path = luaL_checkstring(L, 3);
int size = get_file_size(arc_name, subarc_name, path);
if (size > 0) {
char buffer[size];
extract_file_to_memory(buffer, arc_name, subarc_name, path, size);
lua_pushlstring(L, buffer, size);
} else {
lua_pushlstring(L, "", 0);
}
return 1;
}
static const char *stat2str(const struct stat *buf) {
......@@ -941,6 +955,7 @@ static const struct injected_func injected_funcs[] = {
{ lua_copy, "copy" },
{ lua_extract_inner_archive, "extract_inner_archive" },
{ lua_get_file_size, "get_file_size" },
{ lua_get_file_content, "get_file_content" },
{ lua_ls, "ls" },
{ lua_stat, "stat" },
{ lua_lstat, "lstat" },
......
......@@ -83,7 +83,7 @@ static int get_inner_archive(struct archive *arc, const char* arcname, const cha
return -1;
}
int extract_file(struct archive *a, const char *filename) {
int _extract_file(struct archive *a, const char *filename) {
struct archive *ext;
struct archive_entry *entry;
char name[PATH_MAX];
......@@ -255,14 +255,12 @@ int extract_to_disk(const char *arc_name, const char *subarc_name, char *files[]
return 0;
}
/* do some action to file in subarchive */
int process_file(const char *arcname, const char *subarcname, const char *filename) {
}
int get_file_size(const char *arcname, const char *subarcname, const char *filename) {
/*
* do some action to file in subarchive
* pass action as function that takes archive_entry
* filename is sanitized automatically
*/
int process_file(const char *arcname, const char *subarcname, const char *filename, int (*action)(struct archive *, struct archive_entry *)){
struct archive *a; /* main archive */
struct archive *sa; /* sub archive */
struct archive_entry *entry;
......@@ -293,7 +291,6 @@ int get_file_size(const char *arcname, const char *subarcname, const char *filen
const char *entry_name = archive_entry_pathname(entry);
sanitize_filename(name, entry_name);
if (strcmp(name, subarc_name) == 0) {
printf("%s matched\n", name);
/* prepare subarchive */
size = archive_entry_size(entry);
char *buff = malloc(size);
......@@ -314,15 +311,14 @@ int get_file_size(const char *arcname, const char *subarcname, const char *filen
while (archive_read_next_header(sa, &subentry) == ARCHIVE_OK) {
/* check if we have right file */
const char *subentry_name = archive_entry_pathname(subentry);
printf("Comparing:\n%s\n%s\n", file_name, subentry_name);
if (strcmp(file_name, subentry_name) == 0) {
r = archive_entry_size(subentry);
r = action(sa, subentry);
archive_read_free(sa);
return r;
}
archive_read_data_skip(sa);
}
printf("Found nothing\n");
/* Found nothing */
archive_read_free(sa);
return -1;
}
......@@ -332,6 +328,59 @@ int get_file_size(const char *arcname, const char *subarcname, const char *filen
return -1;
}
int get_file_size(const char *arcname, const char *subarcname, const char *filename) {
int get_size(struct archive *a, struct archive_entry *entry) {
return archive_entry_size(entry);
}
return process_file(arcname, subarcname, filename, get_size);
}
/* extract file into current directory (TODO: add path?) */
int extract_file_to_disk(const char *arcname, const char *subarcname, const char *filename) {
int unpack(struct archive *a, struct archive_entry *entry) {
int r;
struct archive *ext;
int flags;
/* Select which attributes we want to restore. */
flags = ARCHIVE_EXTRACT_TIME;
flags |= ARCHIVE_EXTRACT_PERM;
flags |= ARCHIVE_EXTRACT_ACL;
flags |= ARCHIVE_EXTRACT_FFLAGS;
ext = archive_write_disk_new();
archive_write_disk_set_options(ext, flags);
archive_write_disk_set_standard_lookup(ext);
r = archive_write_header(ext, entry);
if (r < ARCHIVE_OK) {
fprintf(stderr, "%s\n", archive_error_string(ext));
return -1;
} else if (archive_entry_size(entry) > 0) {
r = copy_data(a, ext);
if (r < ARCHIVE_OK) {
fprintf(stderr, "%s\n", archive_error_string(ext));
return -1;
}
}
r = archive_write_finish_entry(ext);
if (r < ARCHIVE_OK) {
fprintf(stderr, "%s\n", archive_error_string(ext));
return -1;
}
archive_write_close(ext);
archive_write_free(ext);
return 0;
}
return process_file(arcname, subarcname, filename, unpack);
}
int extract_file_to_memory(char *buff, const char *arcname, const char *subarcname, const char *filename, int size) {
int unpack(struct archive *a, struct archive_entry *entry) {
archive_read_data(a, buff, size);
/* TODO: error checking */
return 0;
}
return process_file(arcname, subarcname, filename, unpack);
}
int extract_inner_archive(const char *arcname, const char *subarcname, const char *path) {
/*
......
......@@ -5,7 +5,7 @@ int unpacker_test();
/*
*
*/
int extract_file(struct archive *a, const char *filename);
/*int extract_file(struct archive *a, const char *filename);*/
/*
*
......@@ -22,5 +22,7 @@ int extract_inner_archive(const char* arcname, const char* subarcname, const cha
int get_file_size(const char *arcname, const char *subarcname, const char *filename);
int extract_file_to_memory(char *buff, const char *arcname, const char *subarcname, const char *filename, int size);
int test_extract(const char *arc_name, const char *subarc_name, char *files[], int count);
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