summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorPatrick McDermott <patrick.mcdermott@libiquity.com>2023-04-30 21:25:55 (EDT)
committer Patrick McDermott <patrick.mcdermott@libiquity.com>2023-04-30 21:25:55 (EDT)
commitd1383915089af484e68ae5ce03f5c20e78ad0726 (patch)
tree38d0f4ca5ee490df007e5d35856b22b269b91712 /src
parent4cc0d685f1d741e84f126ca1eb91f16b602d023d (diff)
ustar: Move SOURCE_DATE_EPOCH code to opk/write
This way it runs only once (without resorting to static variables) across all headers in all ustar archives. Also, opk/write now prints an informative error message.
Diffstat (limited to 'src')
-rw-r--r--src/opk/write.c28
-rw-r--r--src/ustar.c26
-rw-r--r--src/ustar.h2
3 files changed, 30 insertions, 26 deletions
diff --git a/src/opk/write.c b/src/opk/write.c
index 1b490ee..93ddc74 100644
--- a/src/opk/write.c
+++ b/src/opk/write.c
@@ -20,6 +20,7 @@
#include <grp.h>
#include <pwd.h>
#include <stdio.h>
+#include <stdlib.h>
#include <string.h>
#include "../defs.h"
#include "../dirent.h"
@@ -41,16 +42,41 @@ _opkg_opk_opk_write_file(void *user_data, size_t size)
}
}
+static int
+_opkg_opk_opk_source_date_epoch(int64_t *mtime)
+{
+ char *env;
+ char *end;
+
+ env = getenv("SOURCE_DATE_EPOCH");
+ if (env == NULL) {
+ return OPKG_OPK_ERROR;
+ }
+ *mtime = strtol(env, &end, 10);
+ if (*end != '\0') {
+ return OPKG_OPK_ERROR;
+ }
+ return OPKG_OPK_OK;
+}
+
int
opkg_opk_opk_write(struct opkg_opk_opk *opk, const char *file_name)
{
int ret;
+ int64_t mtime;
struct opkg_opk_dirent dirent;
char *buffer;
size_t size;
ret = OPKG_OPK_OK;
+ if (_opkg_opk_opk_source_date_epoch(&mtime) == OPKG_OPK_ERROR) {
+ fputs(_("Error: Missing or invalid SOURCE_DATE_EPOCH\n"),
+ stderr);
+ ret = OPKG_OPK_ERROR;
+ goto out0;
+ }
+
/* Open outer archive. */
opk->file = fopen(file_name, "wb");
if (opk->file == NULL) {
@@ -85,7 +111,7 @@ opkg_opk_opk_write(struct opkg_opk_opk *opk, const char *file_name)
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) {
+ 4, mtime, '-', NULL) != OPKG_OPK_OK) {
fputs(_("Error: Failed to write header\n"), stderr);
ret = OPKG_OPK_ERROR;
goto out3;
diff --git a/src/ustar.c b/src/ustar.c
index a2ab0f8..88ae7b6 100644
--- a/src/ustar.c
+++ b/src/ustar.c
@@ -265,31 +265,13 @@ opkg_opk_ustar_read(struct opkg_opk_ustar *ustar, char **buffer, size_t *size)
return OPKG_OPK_OK;
}
-static int
-_opkg_opk_ustar_source_date_epoch(int64_t *mtime)
-{
- char *env;
- char *end;
-
- env = getenv("SOURCE_DATE_EPOCH");
- if (env == NULL) {
- return OPKG_OPK_ERROR;
- }
- *mtime = strtol(env, &end, 10);
- if (*end != '\0') {
- return OPKG_OPK_ERROR;
- }
- return OPKG_OPK_OK;
-}
-
int
opkg_opk_ustar_write_header(struct opkg_opk_ustar *ustar,
struct opkg_opk_dirent *dirent, uint16_t mode,
uid_t uid, const char *uname, gid_t gid, const char *gname,
- uint64_t size, char type, const char *linkname)
+ uint64_t size, int64_t mtime, char type, const char *linkname)
{
size_t linkname_len;
- int64_t mtime;
uint32_t chksum;
size_t i;
char *header_uc;
@@ -307,6 +289,7 @@ opkg_opk_ustar_write_header(struct opkg_opk_ustar *ustar,
strncpy(ustar->header.uname, uname, sizeof(ustar->header.uname));
strncpy(ustar->header.gname, gname, sizeof(ustar->header.gname));
sprintf(ustar->header.size, "%o", size);
+ sprintf(ustar->header.mtime, "%o", mtime);
switch (type) {
case '-': /* Regular file */
*ustar->header.typeflag = '0';
@@ -338,11 +321,6 @@ opkg_opk_ustar_write_header(struct opkg_opk_ustar *ustar,
memset(ustar->header.linkname, 0,
sizeof(ustar->header.linkname) - linkname_len);
- if (_opkg_opk_ustar_source_date_epoch(&mtime) == OPKG_OPK_ERROR) {
- return OPKG_OPK_ERROR;
- }
- sprintf(ustar->header.mtime, "%o", mtime);
-
strcpy(ustar->header.magic, "ustar");
memcpy(ustar->header.version, "00", 2);
chksum = 0;
diff --git a/src/ustar.h b/src/ustar.h
index 679f8ae..4c51070 100644
--- a/src/ustar.h
+++ b/src/ustar.h
@@ -90,7 +90,7 @@ int
opkg_opk_ustar_write_header(struct opkg_opk_ustar *ustar,
struct opkg_opk_dirent *dirent, uint16_t mode,
uid_t uid, const char *uname, gid_t gid, const char *gname,
- uint64_t size, char type, const char *linkname);
+ uint64_t size, int64_t mtime, char type, const char *linkname);
int
opkg_opk_ustar_get_buffer(struct opkg_opk_ustar *ustar, char **buffer,