Skip to content

Commit

Permalink
Add optional callback on directory changes during rpmfi iteration
Browse files Browse the repository at this point in the history
Internal only for now in case we need to fiddle with the API some more,
but no reason this couldn't be made public later.
  • Loading branch information
pmatilai committed Feb 16, 2022
1 parent 0e9f6fd commit fb13f7f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
24 changes: 23 additions & 1 deletion lib/rpmfi.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ struct rpmfi_s {
int intervalStart; /*!< Start of iterating interval. */
int intervalEnd; /*!< End of iterating interval. */

rpmfiChdirCb onChdir; /*!< Callback for directory changes */
void *onChdirData; /*!< Caller private callback data */

rpmfiles files; /*!< File info set */
rpmcpio_t archive; /*!< Archive with payload */
unsigned char * found; /*!< Bit field of files found in the archive */
Expand Down Expand Up @@ -303,6 +306,17 @@ rpm_count_t rpmfiDC(rpmfi fi)
return (fi != NULL ? rpmfilesDC(fi->files) : 0);
}

int rpmfiSetOnChdir(rpmfi fi, rpmfiChdirCb cb, void *data)
{
int rc = -1;
if (fi != NULL) {
fi->onChdir = cb;
fi->onChdirData = data;
rc = 0;
}
return rc;
}

int rpmfiFX(rpmfi fi)
{
return (fi != NULL ? fi->i : -1);
Expand All @@ -313,9 +327,17 @@ int rpmfiSetFX(rpmfi fi, int fx)
int i = -1;

if (fi != NULL && fx >= 0 && fx < rpmfilesFC(fi->files)) {
int dx = fi->j;
i = fi->i;
fi->i = fx;
fi->j = rpmfilesDI(fi->files, fi->i);
i = fi->i;

if (fi->j != dx && fi->onChdir) {
int chrc = fi->onChdir(fi, fi->onChdirData);
if (chrc < 0)
i = chrc;
}
}
return i;
}
Expand Down Expand Up @@ -1780,9 +1802,9 @@ static rpmfi initIter(rpmfiles files, int itype, int link)
if (files && itype>=0 && itype<=RPMFILEITERMAX) {
fi = xcalloc(1, sizeof(*fi));
fi->i = -1;
fi->j = -1;
fi->files = link ? rpmfilesLink(files) : files;
fi->next = nextfuncs[itype];
fi->i = -1;
if (itype == RPMFI_ITER_BACK) {
fi->i = rpmfilesFC(fi->files);
} else if (itype >=RPMFI_ITER_READ_ARCHIVE
Expand Down
17 changes: 17 additions & 0 deletions lib/rpmfi_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,23 @@
extern "C" {
#endif

/** \ingroup rpmfi
* Callback on file iterator directory changes
* @param fi file info
* @param data caller private callback data
* @return 0 on success, < 0 on error (to stop iteration)
*/
typedef int (*rpmfiChdirCb)(rpmfi fi, void *data);

/** \ingroup rpmfi
* Set a callback for directory changes during iteration.
* @param fi file info
* @param cb callback function
* @param data caller private callback data
* @return string pool handle (weak reference)
*/
int rpmfiSetOnChdir(rpmfi fi, rpmfiChdirCb cb, void *data);

/** \ingroup rpmfi
* Return file info set string pool handle
* @param fi file info
Expand Down

0 comments on commit fb13f7f

Please sign in to comment.