Skip to content

Commit

Permalink
Swap over to dirfd+basename based operation within the fsm
Browse files Browse the repository at this point in the history
Within fsm this is just a matter of adjusting error messages to include
the directory... if it only wasn't for the plugins requiring absolute
paths for outside users. For the plugins, we need to assemble absolute
paths as needed, both in ensureDir() and plugin file slots.
  • Loading branch information
pmatilai committed Feb 15, 2022
1 parent 90fa1bd commit 4e58299
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 14 deletions.
30 changes: 19 additions & 11 deletions lib/fsm.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,15 @@ static int fsmOpenat(int dirfd, const char *path, int flags, int dir);

/** \ingroup payload
* Build path to file from file info, optionally ornamented with suffix.
* "/" needs special handling to avoid appearing as empty path.
* @param fi file info iterator
* @param suffix suffix to use (NULL disables)
* @param[out] path to file (malloced)
*/
static char * fsmFsPath(rpmfi fi, const char * suffix)
{
return rstrscat(NULL, rpmfiDN(fi), rpmfiBN(fi), suffix ? suffix : "", NULL);
const char *bn = rpmfiBN(fi);
return rstrscat(NULL, *bn ? bn : "/", suffix ? suffix : "", NULL);
}

static int fsmLink(int odirfd, const char *opath, int dirfd, const char *path)
Expand Down Expand Up @@ -330,6 +332,7 @@ static int fsmOpenat(int dirfd, const char *path, int flags, int dir)
}

static int fsmDoMkDir(rpmPlugins plugins, int dirfd, const char *dn,
const char *apath,
int owned, mode_t mode, int *fdp)
{
int rc;
Expand All @@ -338,7 +341,7 @@ static int fsmDoMkDir(rpmPlugins plugins, int dirfd, const char *dn,
op |= FAF_UNOWNED;

/* Run fsm file pre hook for all plugins */
rc = rpmpluginsCallFsmFilePre(plugins, NULL, dn, mode, op);
rc = rpmpluginsCallFsmFilePre(plugins, NULL, apath, mode, op);

if (!rc)
rc = fsmMkdir(dirfd, dn, mode);
Expand All @@ -350,7 +353,7 @@ static int fsmDoMkDir(rpmPlugins plugins, int dirfd, const char *dn,
}

if (!rc) {
rc = rpmpluginsCallFsmFilePrepare(plugins, NULL, *fdp, dn, dn, mode, op);
rc = rpmpluginsCallFsmFilePrepare(plugins, NULL, *fdp, apath, apath, mode, op);
}

/* Run fsm file post hook for all plugins */
Expand All @@ -359,7 +362,7 @@ static int fsmDoMkDir(rpmPlugins plugins, int dirfd, const char *dn,
if (!rc) {
rpmlog(RPMLOG_DEBUG,
"%s directory created with perms %04o\n",
dn, (unsigned)(mode & 07777));
apath, (unsigned)(mode & 07777));
}

return rc;
Expand All @@ -371,6 +374,7 @@ static int ensureDir(rpmPlugins plugins, const char *p, int owned, int create,
char *path = xstrdup(p);
char *dp = path;
char *sp = NULL, *bn;
char *apath = NULL;
int oflags = O_RDONLY;
int rc = 0;

Expand All @@ -382,10 +386,12 @@ static int ensureDir(rpmPlugins plugins, const char *p, int owned, int create,

while ((bn = strtok_r(dp, "/", &sp)) != NULL) {
fd = fsmOpenat(dirfd, bn, oflags, 1);
/* assemble absolute path for plugins benefit, sigh */
apath = rstrscat(&apath, "/", bn, NULL);

if (fd < 0 && errno == ENOENT && create) {
mode_t mode = S_IFDIR | (_dirPerms & 07777);
rc = fsmDoMkDir(plugins, dirfd, bn, owned, mode, &fd);
rc = fsmDoMkDir(plugins, dirfd, bn, apath, owned, mode, &fd);
}

close(dirfd);
Expand Down Expand Up @@ -418,6 +424,7 @@ static int ensureDir(rpmPlugins plugins, const char *p, int owned, int create,
}

free(path);
free(apath);
return rc;
}

Expand Down Expand Up @@ -709,7 +716,8 @@ static int fsmBackup(int dirfd, rpmfi fi, rpmFileAction action)
char * path = fsmFsPath(fi, suffix);
rc = fsmRename(dirfd, opath, dirfd, path);
if (!rc) {
rpmlog(RPMLOG_WARNING, _("%s saved as %s\n"), opath, path);
rpmlog(RPMLOG_WARNING, _("%s%s saved as %s%s\n"),
rpmfiDN(fi), opath, rpmfiDN(fi), path);
}
free(path);
free(opath);
Expand Down Expand Up @@ -765,8 +773,8 @@ static int fsmCommit(int dirfd, char **path, rpmfi fi, rpmFileAction action, con
if (!rc) {
if (nsuffix) {
char * opath = fsmFsPath(fi, NULL);
rpmlog(RPMLOG_WARNING, _("%s created as %s\n"),
opath, dest);
rpmlog(RPMLOG_WARNING, _("%s%s created as %s%s\n"),
rpmfiDN(fi), opath, rpmfiDN(fi), dest);
free(opath);
}
free(*path);
Expand Down Expand Up @@ -1033,7 +1041,7 @@ int rpmPackageFilesInstall(rpmts ts, rpmte te, rpmfiles files,

/* Notify on success. */
if (rc)
*failedFile = xstrdup(fp->fpath);
*failedFile = rstrscat(NULL, rpmfiDN(fi), fp->fpath, NULL);
else
rpmpsmNotify(psm, RPMCALLBACK_INST_PROGRESS, rpmfiArchiveTell(fi));
fp->stage = FILE_UNPACK;
Expand Down Expand Up @@ -1062,7 +1070,7 @@ int rpmPackageFilesInstall(rpmts ts, rpmte te, rpmfiles files,
if (!rc)
fp->stage = FILE_COMMIT;
else
*failedFile = xstrdup(fp->fpath);
*failedFile = rstrscat(NULL, rpmfiDN(fi), fp->fpath, NULL);

/* Run fsm file post hook for all plugins for all processed files */
rpmpluginsCallFsmFilePost(plugins, fi, fp->fpath,
Expand Down Expand Up @@ -1178,7 +1186,7 @@ int rpmPackageFilesRemove(rpmts ts, rpmte te, rpmfiles files,
if (!strict_erasures) rc = 0;

if (rc)
*failedFile = xstrdup(fp->fpath);
*failedFile = rstrscat(NULL, rpmfiDN(fi), fp->fpath, NULL);

if (rc == 0) {
/* Notify on success. */
Expand Down
20 changes: 17 additions & 3 deletions lib/rpmplugins.c
Original file line number Diff line number Diff line change
Expand Up @@ -350,21 +350,31 @@ rpmRC rpmpluginsCallScriptletPost(rpmPlugins plugins, const char *s_name, int ty
return rc;
}

static char *abspath(rpmfi fi, const char *path)
{
if (*path == '/')
return xstrdup(path);
else
return rstrscat(NULL, rpmfiDN(fi), path, NULL);
}

rpmRC rpmpluginsCallFsmFilePre(rpmPlugins plugins, rpmfi fi, const char *path,
mode_t file_mode, rpmFsmOp op)
{
plugin_fsm_file_pre_func hookFunc;
int i;
rpmRC rc = RPMRC_OK;
char *apath = abspath(fi, path);

for (i = 0; i < plugins->count; i++) {
rpmPlugin plugin = plugins->plugins[i];
RPMPLUGINS_SET_HOOK_FUNC(fsm_file_pre);
if (hookFunc && hookFunc(plugin, fi, path, file_mode, op) == RPMRC_FAIL) {
if (hookFunc && hookFunc(plugin, fi, apath, file_mode, op) == RPMRC_FAIL) {
rpmlog(RPMLOG_ERR, "Plugin %s: hook fsm_file_pre failed\n", plugin->name);
rc = RPMRC_FAIL;
}
}
free(apath);

return rc;
}
Expand All @@ -375,14 +385,16 @@ rpmRC rpmpluginsCallFsmFilePost(rpmPlugins plugins, rpmfi fi, const char *path,
plugin_fsm_file_post_func hookFunc;
int i;
rpmRC rc = RPMRC_OK;
char *apath = abspath(fi, path);

for (i = 0; i < plugins->count; i++) {
rpmPlugin plugin = plugins->plugins[i];
RPMPLUGINS_SET_HOOK_FUNC(fsm_file_post);
if (hookFunc && hookFunc(plugin, fi, path, file_mode, op, res) == RPMRC_FAIL) {
if (hookFunc && hookFunc(plugin, fi, apath, file_mode, op, res) == RPMRC_FAIL) {
rpmlog(RPMLOG_WARNING, "Plugin %s: hook fsm_file_post failed\n", plugin->name);
}
}
free(apath);

return rc;
}
Expand All @@ -394,15 +406,17 @@ rpmRC rpmpluginsCallFsmFilePrepare(rpmPlugins plugins, rpmfi fi,
plugin_fsm_file_prepare_func hookFunc;
int i;
rpmRC rc = RPMRC_OK;
char *apath = abspath(fi, path);

for (i = 0; i < plugins->count; i++) {
rpmPlugin plugin = plugins->plugins[i];
RPMPLUGINS_SET_HOOK_FUNC(fsm_file_prepare);
if (hookFunc && hookFunc(plugin, fi, fd, path, dest, file_mode, op) == RPMRC_FAIL) {
if (hookFunc && hookFunc(plugin, fi, fd, apath, dest, file_mode, op) == RPMRC_FAIL) {
rpmlog(RPMLOG_ERR, "Plugin %s: hook fsm_file_prepare failed\n", plugin->name);
rc = RPMRC_FAIL;
}
}
free(apath);

return rc;
}

0 comments on commit 4e58299

Please sign in to comment.