bin/xbps-create: cleanup readlink related code

This commit is contained in:
Duncan Overbruck 2022-12-22 21:42:32 +01:00
parent 2deb156beb
commit 9a46051499
No known key found for this signature in database
GPG Key ID: 335C1D17EC3D6E35

View File

@ -311,9 +311,8 @@ ftw_cb(const char *fpath, const struct stat *sb, const struct dirent *dir UNUSED
struct xentry *xe = NULL; struct xentry *xe = NULL;
xbps_dictionary_t fileinfo = NULL; xbps_dictionary_t fileinfo = NULL;
const char *filep = NULL; const char *filep = NULL;
char *buf, *p, *p2, *dname; char *p, *p2, *dname;
char sha256[XBPS_SHA256_SIZE]; char sha256[XBPS_SHA256_SIZE];
ssize_t r;
/* Ignore metadata files generated by xbps-src and destdir */ /* Ignore metadata files generated by xbps-src and destdir */
if ((strcmp(fpath, ".") == 0) || if ((strcmp(fpath, ".") == 0) ||
@ -353,6 +352,9 @@ ftw_cb(const char *fpath, const struct stat *sb, const struct dirent *dir UNUSED
} }
if (S_ISLNK(sb->st_mode)) { if (S_ISLNK(sb->st_mode)) {
char buf[PATH_MAX];
ssize_t len;
/* /*
* Symlinks. * Symlinks.
* *
@ -362,13 +364,12 @@ ftw_cb(const char *fpath, const struct stat *sb, const struct dirent *dir UNUSED
xe->type = strdup("links"); xe->type = strdup("links");
assert(xe->type); assert(xe->type);
xbps_dictionary_set_cstring_nocopy(fileinfo, "type", "links"); xbps_dictionary_set_cstring_nocopy(fileinfo, "type", "links");
buf = malloc(sb->st_size+1);
assert(buf);
r = readlink(fpath, buf, sb->st_size+1);
if (r < 0 || r > sb->st_size)
die("failed to process symlink %s:", fpath);
buf[sb->st_size] = '\0'; len = readlink(fpath, buf, sizeof(buf));
if (len < 0 || len >= (int)sizeof(buf))
die("readlink: %s:", fpath);
buf[len] = '\0';
/* /*
* Check if symlink is absolute or relative; on the former * Check if symlink is absolute or relative; on the former
* make it absolute for the target object. * make it absolute for the target object.
@ -422,7 +423,6 @@ ftw_cb(const char *fpath, const struct stat *sb, const struct dirent *dir UNUSED
} }
assert(xe->target); assert(xe->target);
assert(xbps_dictionary_get(fileinfo, "target")); assert(xbps_dictionary_get(fileinfo, "target"));
free(buf);
} else if (S_ISREG(sb->st_mode)) { } else if (S_ISREG(sb->st_mode)) {
struct xentry *xep; struct xentry *xep;
bool hlink = false; bool hlink = false;
@ -671,8 +671,7 @@ process_entry_file(struct archive *ar,
{ {
struct archive_entry *entry, *sparse_entry; struct archive_entry *entry, *sparse_entry;
struct stat st; struct stat st;
char *buf = NULL, *p; char path[PATH_MAX];
ssize_t len;
assert(ar); assert(ar);
assert(xe); assert(xe);
@ -680,8 +679,9 @@ process_entry_file(struct archive *ar,
if (filematch && strcmp(xe->file, filematch)) if (filematch && strcmp(xe->file, filematch))
return; return;
p = xbps_xasprintf("%s/%s", destdir, xe->file); if (xbps_path_join(path, sizeof(path), destdir, xe->file, (char *)NULL) == -1)
if (lstat(p, &st) == -1) die("path too long %s:", xe->file);
if (lstat(path, &st) == -1)
die("failed to add entry (fstat) %s to archive:", xe->file); die("failed to add entry (fstat) %s to archive:", xe->file);
entry = archive_entry_new(); entry = archive_entry_new();
@ -695,24 +695,23 @@ process_entry_file(struct archive *ar,
st.st_gid = 0; st.st_gid = 0;
archive_entry_copy_stat(entry, &st); archive_entry_copy_stat(entry, &st);
archive_entry_copy_sourcepath(entry, p); archive_entry_copy_sourcepath(entry, path);
if (st.st_uid == geteuid()) if (st.st_uid == geteuid())
archive_entry_set_uname(entry, "root"); archive_entry_set_uname(entry, "root");
if (st.st_gid == getegid()) if (st.st_gid == getegid())
archive_entry_set_gname(entry, "root"); archive_entry_set_gname(entry, "root");
if (S_ISLNK(st.st_mode)) { if (S_ISLNK(st.st_mode)) {
buf = malloc(st.st_size+1); char buf[PATH_MAX];
if (buf == NULL) ssize_t len;
die("failed to allocate readlink buffer", xe->file);
len = readlink(p, buf, st.st_size+1); len = readlink(path, buf, sizeof(buf));
if (len < 0 || len > st.st_size) if (len < 0 || len >= (int)sizeof(buf))
die("failed to add entry %s (readlink) to archive:", die("readlink: %s:", xe->file);
xe->file);
buf[len] = '\0'; buf[len] = '\0';
archive_entry_set_symlink(entry, buf); archive_entry_set_symlink(entry, buf);
} }
free(p);
archive_entry_linkify(resolver, &entry, &sparse_entry); archive_entry_linkify(resolver, &entry, &sparse_entry);
@ -720,8 +719,6 @@ process_entry_file(struct archive *ar,
write_entry(ar, entry); write_entry(ar, entry);
if (sparse_entry != NULL) if (sparse_entry != NULL)
write_entry(ar, sparse_entry); write_entry(ar, sparse_entry);
if (buf)
free(buf);
} }
static void static void