Skip to content

Commit

Permalink
feat: refactor command-line processor, add '--tebako-run' support
Browse files Browse the repository at this point in the history
  • Loading branch information
maxirmx committed Dec 16, 2024
1 parent 06b1f1e commit bf953ce
Show file tree
Hide file tree
Showing 13 changed files with 825 additions and 445 deletions.
1 change: 0 additions & 1 deletion .github/workflows/coverity.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ on:
- '.github/workflows/*.yml'
- '!.github/workflows/coverity.yml'
pull_request:
# The branches below must be a subset of the branches above
paths-ignore:
- 'docs/**'
- '**.adoc'
Expand Down
16 changes: 8 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,8 @@ if(WITH_TESTS)
# DATA_BIN_FILE is packaged filesystem itself
set(DATA_BIN_FILE ${DATA_BIN_DIR}/fs.bin)
set(DATA_BIN_FILE_2 ${DATA_BIN_DIR}/fs2.bin)
set(TEST_PACKAGE_DESCRIPTOR "${CMAKE_CURRENT_SOURCE_DIR}/tests/package_descriptor/pd")


endif(WITH_TESTS)

Expand Down Expand Up @@ -794,7 +796,7 @@ add_library(dwarfs-wr STATIC
"src/dir-io.cpp"
"src/file-io.cpp"
"src/dl-ctl.cpp"
"src/tebako-cmdline-helpers.cpp"
"src/tebako-cmdline.cpp"
"src/tebako-io-helpers.cpp"
"src/tebako-io-root.cpp"
"src/tebako-kfd.cpp"
Expand All @@ -805,7 +807,7 @@ add_library(dwarfs-wr STATIC
"src/tebako-fd.cpp"
"src/tebako-dirent.cpp"
"src/tebako-package-descriptor.cpp"
"include/tebako-cmdline-helpers.h"
"include/tebako-cmdline.h"
"include/tebako-common.h"
"include/tebako-config.h"
"include/tebako-defines.h"
Expand Down Expand Up @@ -919,8 +921,6 @@ if(WITH_TESTS)

set(TESTS_OUTSIDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/tests/test_files")

set(TESTS_THE_OTHER_MEMFS_IMAGE "${CMAKE_CURRENT_BINARY_DIR}/fs2.bin")

add_custom_target(PACKAGED_FILESYSTEM_STEP_1 ALL
COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_CURRENT_SOURCE_DIR}/tests/tebako-fs.cpp
COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_CURRENT_SOURCE_DIR}/tests/tebako-fs2.cpp
Expand Down Expand Up @@ -960,7 +960,7 @@ if(WITH_TESTS)
add_custom_target(PACKAGED_FILESYSTEM_STEP_3 ALL
COMMAND ${GNU_BASH} -c "chmod -w ${DATA_TEST_DIR}/directory-2/file-in-directory-2.txt"
COMMAND ${MKDWARFS} --force --progress simple -o ${DATA_BIN_FILE} -i ${DATA_TEST_DIR}
COMMAND ${MKDWARFS} --force --progress simple -o ${DATA_BIN_FILE_2} -i ${DATA_TEST_DIR_2}
COMMAND ${MKDWARFS} --force --progress simple -o ${DATA_BIN_FILE_2} -i ${DATA_TEST_DIR_2} --header ${TEST_PACKAGE_DESCRIPTOR}
COMMAND ${CMAKE_COMMAND} -E rm -rf ${DATA_TEST_DIR}/*-link-to-* ${DATA_TEST_DIR}/directory-1/*-link-to-*
${DATA_TEST_DIR}/directory-1/*empty.* ${DATA_TEST_DIR}/*-outside-of-memfs
COMMAND ${CMAKE_COMMAND} -E rm -rf ${DATA_TEST_DIR_2}/*-link-to-* ${DATA_TEST_DIR_2}/directory-1/*-link-to-*
Expand All @@ -972,7 +972,7 @@ if(WITH_TESTS)
else(WITH_LINK_TESTS)
add_custom_target(PACKAGED_FILESYSTEM_STEP_3 ALL
COMMAND ${MKDWARFS} --force --progress simple -o ${DATA_BIN_FILE} -i ${DATA_TEST_DIR}
COMMAND ${MKDWARFS} --force --progress simple -o ${DATA_BIN_FILE_2} -i ${DATA_TEST_DIR_2}
COMMAND ${MKDWARFS} --force --progress simple -o ${DATA_BIN_FILE_2} -i ${DATA_TEST_DIR_2} --header ${TEST_PACKAGE_DESCRIPTOR}
COMMAND ${CMAKE_COMMAND} -E rm -rf ${DATA_TEST_DIR}/directory-1/*empty.*
BYPRODUCTS ${DATA_BIN_FILE} ${DATA_BIN_FILE_2}
)
Expand All @@ -987,7 +987,7 @@ if(WITH_TESTS)
@ONLY
)

# feat: exectable application stub
# feat: executable application stub
# configure_file(
# ${CMAKE_CURRENT_SOURCE_DIR}/tests/resources/tebako-fs2.cpp.in
# ${CMAKE_CURRENT_SOURCE_DIR}/tests/tebako-fs2.cpp
Expand Down Expand Up @@ -1317,7 +1317,7 @@ install(FILES
"include/tebako-config.h"
"include/tebako-defines.h"
"include/tebako-io.h"
"include/tebako-cmdline-helpers.h"
"include/tebako-cmdline.h"
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/tebako
)

Expand Down
63 changes: 57 additions & 6 deletions include/tebako-cmdline-helpers.h → include/tebako-cmdline.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,62 @@

#pragma once

#include <tebako-package-descriptor.h>

namespace tebako {
int build_arguments_for_extract(int* argc, char*** argv, const char* fs_mount_point);
std::pair<int, char**> build_arguments(const std::vector<std::string>& new_argv,
const char* fs_mount_point,
const char* fs_entry_point);
std::pair<std::vector<std::string>, std::vector<std::string>> parse_arguments(int argc, char** argv);
void process_mountpoints(const std::vector<std::string>& mountpoints);

class cmdline_args {
int argc;
const char** argv;

std::vector<std::string> mountpoints;
std::vector<std::string> other_args;

std::string extract_folder;
std::string app_image;

std::optional<package_descriptor> package;

int new_argc;
char** new_argv;
char* new_argv_memory;

bool extract;
bool run;

public:
cmdline_args(int argc, const char** argv)
: argc(argc), argv(argv), extract(false), run(false), new_argc(0), new_argv(nullptr), new_argv_memory(nullptr)
{
}
~cmdline_args()
{
if (new_argv_memory) {
delete[] new_argv_memory;
}
if (new_argv) {
delete[] new_argv;
}
}

void parse_arguments(void);
void process_mountpoints();
void process_package();

void build_arguments(const char* fs_mount_point, const char* fs_entry_point);
void build_arguments_for_extract(const char* fs_mount_point);
void build_arguments_for_run(const char* fs_mount_point, const char* fs_entry_point);

std::vector<std::string> const& get_mountpoints() { return mountpoints; }
std::vector<std::string> const& get_args() { return other_args; }

int get_argc() { return new_argc; }
char** get_argv() { return new_argv; }

bool with_application() { return run; }
std::string const& get_application_image() { return app_image; }

std::optional<package_descriptor> const& get_package() { return package; }
};

} // namespace tebako
8 changes: 4 additions & 4 deletions include/tebako-package-descriptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@ class package_descriptor {
package_descriptor() = delete;

// Constructor for deserialization
explicit package_descriptor(const std::vector<uint8_t>& buffer);
explicit package_descriptor(const std::vector<char>& buffer);
// Constructor from version strings and other parameters
package_descriptor(const std::string& ruby_version,
const std::string& tebako_version,
const std::string& mount_point,
const std::string& entry_point,
const std::optional<std::string>& cwd);
// Serialize the object to a binary format
std::vector<uint8_t> serialize() const;
std::vector<char> serialize() const;

uint16_t get_ruby_version_major() const { return ruby_version_major; }
uint16_t get_ruby_version_minor() const { return ruby_version_minor; }
Expand All @@ -75,10 +75,10 @@ class package_descriptor {
const std::string& get_entry_point() const { return entry_point; }
const std::optional<std::string>& get_cwd() const { return cwd; }

static constexpr bool is_little_endian()
static bool is_little_endian()
{
uint16_t number = 1;
return *(reinterpret_cast<uint8_t*>(&number)) == 1;
return *(reinterpret_cast<char*>(&number)) == 1;
}

static uint16_t to_little_endian(uint16_t value)
Expand Down
Loading

0 comments on commit bf953ce

Please sign in to comment.