diff options
author | Patrick McDermott <patrick.mcdermott@libiquity.com> | 2023-04-30 03:44:20 (EDT) |
---|---|---|
committer | Patrick McDermott <patrick.mcdermott@libiquity.com> | 2023-04-30 03:44:20 (EDT) |
commit | dc25a788d874d9ee95738b9fa56d2a06f6a31141 (patch) | |
tree | c387f2cac34c32e001409f85dfe1281ede74cc6c /src/opk | |
parent | c28ea1e0a30ad036a30288c8cda68c80c08ab0ec (diff) |
opk/write: Build mode (WIP)
Diffstat (limited to 'src/opk')
-rw-r--r-- | src/opk/local.mk | 3 | ||||
-rw-r--r-- | src/opk/opk.h | 1 | ||||
-rw-r--r-- | src/opk/write.c | 119 |
3 files changed, 122 insertions, 1 deletions
diff --git a/src/opk/local.mk b/src/opk/local.mk index a535a4f..a4722ba 100644 --- a/src/opk/local.mk +++ b/src/opk/local.mk @@ -1,3 +1,4 @@ opkg_opk_SOURCES += \ %reldir%/opk.h \ - %reldir%/read.c + %reldir%/read.c \ + %reldir%/write.c diff --git a/src/opk/opk.h b/src/opk/opk.h index e5fc4d2..afd39b6 100644 --- a/src/opk/opk.h +++ b/src/opk/opk.h @@ -39,6 +39,7 @@ struct opkg_opk_opk { int list_control; int list_data; const char *control_dir; + const char *data_dir; FILE *file; char file_buffer[8192]; struct opkg_opk_gzip *outer_gzip; diff --git a/src/opk/write.c b/src/opk/write.c new file mode 100644 index 0000000..12a36c6 --- /dev/null +++ b/src/opk/write.c @@ -0,0 +1,119 @@ +/* + * Copyright (C) 2023 Patrick McDermott + * + * This file is part of opkg-opkg. + * + * opkg-opkg is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * opkg-opkg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with opkg-opkg. If not, see <http://www.gnu.org/licenses/>. + */ + +#include <grp.h> +#include <pwd.h> +#include <stdio.h> +#include <string.h> +#include "../defs.h" +#include "../dirent.h" +#include "../gzip.h" +#include "../i18n.h" +#include "../opk.h" +#include "../ustar.h" +#include "opk.h" + +static int +_opkg_opk_opk_write_file(void *user_data, size_t size) +{ + struct opkg_opk_opk *opk = user_data; + + if (fwrite(opk->file_buffer, 1, sizeof(opk->file_buffer), opk->file) == + size) { + return OPKG_OPK_OK; + } else { + return OPKG_OPK_ERROR; + } +} + +int +opkg_opk_opk_write(struct opkg_opk_opk *opk, const char *file_name) +{ + int ret; + struct opkg_opk_dirent dirent; + char *buffer; + size_t size; + + ret = OPKG_OPK_OK; + + /* Open outer archive. */ + opk->file = fopen(file_name, "wb"); + if (opk->file == NULL) { + fprintf(stderr, _("Error: Failed to open file \"%s\"\n"), + file_name); + ret = OPKG_OPK_ERROR; + goto out0; + } + + /* Initialize outer gzip decompressor. */ + opk->outer_gzip = opkg_opk_gzip_init_write(&_opkg_opk_opk_write_file, + opk); + if (opk->outer_gzip == NULL) { + fputs(_("Error: Failed to initialize\n"), stderr); + ret = OPKG_OPK_ERROR; + goto out1; + } + opkg_opk_gzip_set_write_buffer(opk->outer_gzip, opk->file_buffer, + sizeof(opk->file_buffer)); + + /* Initialize outer ustar unarchiver. */ + opk->outer_ustar = opkg_opk_ustar_init(opk->outer_gzip); + if (opk->outer_ustar == NULL) { + fputs(_("Error: Failed to initialize\n"), stderr); + ret = OPKG_OPK_ERROR; + goto out2; + } + + /* Write version file. */ + dirent.name = "debian-binary"; + dirent.parent = NULL; + if (opkg_opk_ustar_write_header(opk->outer_ustar, &dirent, 0644, + 0, getpwuid(0)->pw_name, + 0, getgrgid(0)->gr_name, + 4, '-', NULL) != OPKG_OPK_OK) { + fputs(_("Error: Failed to write header\n"), stderr); + ret = OPKG_OPK_ERROR; + goto out3; + } + opkg_opk_ustar_get_buffer(opk->outer_ustar, &buffer, &size); + memcpy(buffer, "2.0\n", 4); + if (opkg_opk_ustar_write_data(opk->outer_ustar, 4) != OPKG_OPK_OK) { + fputs(_("Error: Failed to write data\n"), stderr); + ret = OPKG_OPK_ERROR; + goto out3; + } + + /* TODO: control.tar.gz and data.tar.gz */ + + /* Write trailer. */ + if (opkg_opk_ustar_write_trailer(opk->outer_ustar) != OPKG_OPK_OK) { + fputs(_("Error: Failed to write trailer\n"), stderr); + ret = OPKG_OPK_ERROR; + goto out3; + } + + out3: + opkg_opk_ustar_free(opk->outer_ustar); + out2: + opkg_opk_gzip_free(opk->outer_gzip); + out1: + fclose(opk->file); + out0: + return ret; +} |