diff --git a/CHANGELOG.rst b/CHANGELOG.rst index c7476f60..c107f773 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -10,6 +10,11 @@ - Add the global ``-V, --version`` option for printing the program version. The ``version`` command is deprecated in favor of this option and prints a warning when used. +- Preserve the relative paths of input files when creating a package, instead + of stripping their leading directories. For example, ``partup package + test.partup layout.yaml foo/bar.txt`` now stores ``foo/bar.txt`` in the + package. Input files must be located inside the current directory and the + layout configuration file must be located at the root of the package. .. _release-4.0.0: diff --git a/doc/usage.rst b/doc/usage.rst index 7d24f51a..82dbddfa 100644 --- a/doc/usage.rst +++ b/doc/usage.rst @@ -117,6 +117,12 @@ its input files and the layout configuration file as the only ``.yaml`` file:: The first filename provided after the ``package`` command is always the output filename of the package. +The relative paths of the input files are preserved in the package. For example, +``foo/bar.txt`` is stored as ``foo/bar.txt`` and not as ``bar.txt``. Input files +must be located inside the current directory, which can be changed with the +``-C, --directory`` option. The layout configuration file must always be +specified without a directory, so that it ends up at the root of the package. + .. note:: The extension of a partup package should be named ``.partup``, although this diff --git a/src/pu-package.c b/src/pu-package.c index 305fc86b..e251f9d3 100644 --- a/src/pu-package.c +++ b/src/pu-package.c @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include "pu-error.h" @@ -81,26 +82,64 @@ pu_package_create(gchar **files, GError **error) { g_autofree gchar *cmd = NULL; - g_autofree gchar *input = NULL; + g_autofree gchar *cwd = NULL; + g_autofree gchar *cwd_prefix = NULL; + g_autofree gchar *output_abs = NULL; + g_autofree gchar *quoted_output = NULL; + g_autoptr(GString) input = NULL; guint layout_yaml_count = 0; g_return_val_if_fail(files != NULL, FALSE); g_return_val_if_fail(g_strcmp0(output, "") > 0, FALSE); g_return_val_if_fail(error == NULL || *error == NULL, FALSE); - /* Check if specified input files exist and search for required layout - * configuration file */ - for (guint i = 0; i < g_strv_length(files); i++) { - g_autofree gchar *file = g_strdup(files[i]); - - if (!g_file_test(file, G_FILE_TEST_EXISTS)) { + cwd = g_get_current_dir(); + cwd_prefix = g_str_has_suffix(cwd, "/") ? g_strdup(cwd) + : g_strconcat(cwd, "/", NULL); + input = g_string_new(NULL); + + /* Check if specified input files exist and are located inside the current + * directory. Search for the required layout configuration file, which must + * be placed at the root of the package. The relative paths of the input + * files are preserved in the package. */ + for (gchar **file = files; *file != NULL; file++) { + g_autofree gchar *abs_path = NULL; + g_autofree gchar *rel_path_dot = NULL; + g_autofree gchar *quoted = NULL; + const gchar *rel_path; + + if (!g_file_test(*file, G_FILE_TEST_EXISTS)) { g_set_error(error, PU_PACKAGE_ERROR, PU_PACKAGE_ERROR_NOT_FOUND, - "Input file '%s' does not exist", file); + "Input file '%s' does not exist", *file); return FALSE; } - if (g_str_has_suffix(file, ".yaml")) + abs_path = g_canonicalize_filename(*file, cwd); + if (!g_str_has_prefix(abs_path, cwd_prefix) || + *(abs_path + strlen(cwd_prefix)) == '\0') { + g_set_error(error, PU_PACKAGE_ERROR, PU_PACKAGE_ERROR_INVALID_PATH, + "Input file '%s' must be located inside the current directory", + *file); + return FALSE; + } + rel_path = abs_path + strlen(cwd_prefix); + + if (g_str_has_suffix(rel_path, ".yaml")) { + if (strchr(rel_path, '/')) { + g_set_error(error, PU_PACKAGE_ERROR, + PU_PACKAGE_ERROR_INVALID_PATH, + "Layout configuration '%s' must be placed at the root of the package", + *file); + return FALSE; + } layout_yaml_count++; + } + + /* Prefix with "./" so that names starting with "-" are not + * interpreted as options by mksquashfs. The prefix is not stored. */ + rel_path_dot = g_strconcat("./", rel_path, NULL); + quoted = g_shell_quote(rel_path_dot); + g_string_append_printf(input, "%s ", quoted); } if (!layout_yaml_count) { @@ -127,8 +166,12 @@ pu_package_create(gchar **files, g_remove(output); } - input = g_strjoinv(" ", files); - cmd = g_strdup_printf("mksquashfs %s %s", input, output); + /* Do not strip leading directories from the input files, so that their + * relative paths are preserved in the package */ + output_abs = g_canonicalize_filename(output, cwd); + quoted_output = g_shell_quote(output_abs); + cmd = g_strdup_printf("mksquashfs %s%s -no-strip -noappend", input->str, + quoted_output); if (!pu_spawn_command_line_sync(cmd, error)) { g_prefix_error(error, "Failed creating package '%s': ", output); diff --git a/src/pu-package.h b/src/pu-package.h index 5b247687..111bc598 100644 --- a/src/pu-package.h +++ b/src/pu-package.h @@ -16,6 +16,7 @@ GQuark pu_package_error_quark(void); typedef enum { PU_PACKAGE_ERROR_CREATION_FAILED, PU_PACKAGE_ERROR_EXISTS, + PU_PACKAGE_ERROR_INVALID_PATH, PU_PACKAGE_ERROR_ITER_FAILED, PU_PACKAGE_ERROR_MISSING_LAYOUT, PU_PACKAGE_ERROR_MULTIPLE_LAYOUT, diff --git a/tests/package.c b/tests/package.c index 873f247b..8bbedc28 100644 --- a/tests/package.c +++ b/tests/package.c @@ -28,6 +28,152 @@ package_create(PackageFilesFixture *fixture, g_assert(g_chdir(fixture->path_test) == 0); } +/* Create a temporary directory containing the given files. Parent directories + * are created as needed. */ +static gchar * +tmp_tree_new(const gchar * const *names) +{ + GError *error = NULL; + gchar *path = g_dir_make_tmp("partup-XXXXXX", &error); + + g_assert_no_error(error); + for (; *names != NULL; names++) { + g_autofree gchar *file = g_build_filename(path, *names, NULL); + g_autofree gchar *dir = g_path_get_dirname(file); + + g_assert_cmpint(g_mkdir_with_parents(dir, 0755), ==, 0); + g_assert_true(g_file_set_contents(file, "", 0, &error)); + g_assert_no_error(error); + } + + return path; +} + +/* Remove the temporary directory and everything in it */ +static void +tmp_tree_free(gchar *path) +{ + g_autofree gchar *cmd = g_strdup_printf("rm -rf '%s'", path); + + g_assert_true(g_spawn_command_line_sync(cmd, NULL, NULL, NULL, NULL)); + g_free(path); +} + +static gchar * +package_list(const gchar *package) +{ + GError *error = NULL; + gchar *listing = NULL; + gint wait_status; + g_autofree gchar *cmd = g_strdup_printf("unsquashfs -ll -d squashfs-root './%s'", + package); + + g_assert_true(g_spawn_command_line_sync(cmd, &listing, NULL, &wait_status, + &error)); + g_assert_no_error(error); + g_assert_true(g_spawn_check_wait_status(wait_status, &error)); + g_assert_no_error(error); + + return listing; +} + +static void +package_create_preserve_path(G_GNUC_UNUSED gconstpointer user_data) +{ + g_autoptr(GError) error = NULL; + g_autofree gchar *path_test = g_get_current_dir(); + g_autofree gchar *listing = NULL; + const gchar *names[] = { "layout.yaml", "foo/bar.txt", "foo/sibling.txt", + NULL }; + gchar *files[] = { "layout.yaml", "foo/bar.txt", NULL }; + gchar *path_tmp = tmp_tree_new(names); + + g_assert(g_chdir(path_tmp) == 0); + g_assert_true(pu_package_create(files, PACKAGE_FILENAME, FALSE, &error)); + g_assert_no_error(error); + + listing = package_list(PACKAGE_FILENAME); + g_assert_nonnull(g_strstr_len(listing, -1, "squashfs-root/foo/bar.txt")); + g_assert_nonnull(g_strstr_len(listing, -1, "squashfs-root/layout.yaml")); + g_assert_null(g_strstr_len(listing, -1, "sibling.txt")); + + g_assert(g_chdir(path_test) == 0); + tmp_tree_free(path_tmp); +} + +static void +package_create_dash_name(G_GNUC_UNUSED gconstpointer user_data) +{ + g_autoptr(GError) error = NULL; + g_autofree gchar *path_test = g_get_current_dir(); + g_autofree gchar *listing = NULL; + g_autofree gchar *output_abs = NULL; + const gchar *names[] = { "layout.yaml", "-v", NULL }; + gchar *files[] = { "layout.yaml", "-v", NULL }; + gchar *path_tmp = tmp_tree_new(names); + + /* Names starting with a dash must not be interpreted as options */ + g_assert(g_chdir(path_tmp) == 0); + g_assert_true(pu_package_create(files, "-package.partup", FALSE, &error)); + g_assert_no_error(error); + + listing = package_list("-package.partup"); + g_assert_nonnull(g_strstr_len(listing, -1, "squashfs-root/-v")); + g_assert_nonnull(g_strstr_len(listing, -1, "squashfs-root/layout.yaml")); + + /* Absolute output path with a leading dash in the filename */ + output_abs = g_build_filename(path_tmp, "-abs.partup", NULL); + g_assert_true(pu_package_create(files, output_abs, FALSE, &error)); + g_assert_no_error(error); + g_assert_true(g_file_test(output_abs, G_FILE_TEST_IS_REGULAR)); + + g_assert(g_chdir(path_test) == 0); + tmp_tree_free(path_tmp); +} + +static void +package_create_invalid(G_GNUC_UNUSED gconstpointer user_data) +{ + g_autoptr(GError) error = NULL; + g_autofree gchar *path_test = g_get_current_dir(); + g_autofree gchar *abs_file = NULL; + const gchar *names[] = { "layout.yaml", "foo/layout.yaml", NULL }; + gchar *files_subdir_layout[] = { "foo/layout.yaml", NULL }; + gchar *files_parent[] = { "layout.yaml", "..", NULL }; + gchar *files_cwd[] = { "layout.yaml", ".", NULL }; + gchar *files_abs[] = { "layout.yaml", NULL, NULL }; + gchar *path_tmp = tmp_tree_new(names); + + g_assert(g_chdir(path_tmp) == 0); + + /* Layout configuration must be placed at the package root */ + g_assert_false(pu_package_create(files_subdir_layout, PACKAGE_FILENAME, + FALSE, &error)); + g_assert_error(error, PU_PACKAGE_ERROR, PU_PACKAGE_ERROR_INVALID_PATH); + g_clear_error(&error); + + /* Input files must be located inside the current directory */ + g_assert_false(pu_package_create(files_parent, PACKAGE_FILENAME, FALSE, + &error)); + g_assert_error(error, PU_PACKAGE_ERROR, PU_PACKAGE_ERROR_INVALID_PATH); + g_clear_error(&error); + + g_assert_false(pu_package_create(files_cwd, PACKAGE_FILENAME, FALSE, + &error)); + g_assert_error(error, PU_PACKAGE_ERROR, PU_PACKAGE_ERROR_INVALID_PATH); + g_clear_error(&error); + + abs_file = g_build_filename(path_test, "config", "simple.yaml", NULL); + files_abs[1] = abs_file; + g_assert_false(pu_package_create(files_abs, PACKAGE_FILENAME, FALSE, + &error)); + g_assert_error(error, PU_PACKAGE_ERROR, PU_PACKAGE_ERROR_INVALID_PATH); + g_clear_error(&error); + + g_assert(g_chdir(path_test) == 0); + tmp_tree_free(path_tmp); +} + int main(int argc, char *argv[]) @@ -40,6 +186,12 @@ main(int argc, g_test_add("/package/create", PackageFilesFixture, NULL, package_files_setup, package_create, package_files_teardown); + g_test_add_data_func("/package/create/preserve-path", + NULL, package_create_preserve_path); + g_test_add_data_func("/package/create/dash-name", + NULL, package_create_dash_name); + g_test_add_data_func("/package/create/invalid", + NULL, package_create_invalid); return g_test_run(); }