Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use libelf for determining file colors #1095

Merged
merged 1 commit into from
Mar 6, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 29 additions & 6 deletions build/rpmfc.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <signal.h>
#include <magic.h>
#include <regex.h>
#include <gelf.h>

#include <rpm/header.h>
#include <rpm/argv.h>
Expand Down Expand Up @@ -622,7 +623,7 @@ static int rpmfcHelper(rpmfc fc, int ix, const struct exclreg_s *excl,
return rc;
}

/* Only used for elf coloring and controlling RPMTAG_FILECLASS inclusion now */
/* Only used for controlling RPMTAG_FILECLASS inclusion now */
static const struct rpmfcTokens_s rpmfcTokens[] = {
{ "directory", RPMFC_INCLUDE },

Expand Down Expand Up @@ -1115,6 +1116,29 @@ static int initAttrs(rpmfc fc)
return nattrs;
}

static uint32_t getElfColor(const char *fn)
{
uint32_t color = 0;
int fd = open(fn, O_RDONLY);
if (fd >= 0) {
Elf *elf = elf_begin (fd, ELF_C_READ, NULL);
GElf_Ehdr ehdr;
if (elf && gelf_getehdr(elf, &ehdr)) {
switch (ehdr.e_ident[EI_CLASS]) {
case ELFCLASS64:
color = RPMFC_ELF64;
break;
case ELFCLASS32:
color = RPMFC_ELF32;
break;
}
elf_end(elf);
}
close(fd);
}
return color;
}

rpmRC rpmfcClassify(rpmfc fc, ARGV_t argv, rpm_mode_t * fmode)
{
int msflags = MAGIC_CHECK | MAGIC_COMPRESS | MAGIC_NO_CHECK_TOKENS;
Expand Down Expand Up @@ -1226,10 +1250,12 @@ rpmRC rpmfcClassify(rpmfc fc, ARGV_t argv, rpm_mode_t * fmode)
/* Add attributes based on file type and/or path */
rpmfcAttributes(fc, ix, ftype, s);

fc->fcolor[ix] = fcolor;

if (fcolor != RPMFC_WHITE && (fcolor & RPMFC_INCLUDE))
fc->ftype[ix] = xstrdup(ftype);

/* Add ELF colors */
if (S_ISREG(mode) && is_executable)
fc->fcolor[ix] = getElfColor(s);
}

if (ms != NULL)
Expand Down Expand Up @@ -1533,9 +1559,6 @@ rpmRC rpmfcGenerateDepends(const rpmSpec spec, Package pkg)
goto exit;

/* Add per-file colors(#files) */
/* XXX Make sure only primary (i.e. Elf32/Elf64) colors are added. */
for (int i = 0; i < fc->nfiles; i++)
fc->fcolor[i] &= 0x0f;
headerPutUint32(pkg->header, RPMTAG_FILECOLORS, fc->fcolor, fc->nfiles);

/* Add classes(#classes) */
Expand Down