Update to 4.11.0.1
[platform/upstream/rpm.git] / lib / legacy.c
index caed5d8..422c2b0 100644 (file)
 #include <rpm/rpmfi.h>
 #include <rpm/rpmds.h>
 
-#include "lib/legacy.h"
-
 #include "debug.h"
 
-int _noDirTokens = 0;
-
 static int dncmp(const void * a, const void * b)
 {
     const char *const * first = a;
@@ -23,14 +19,14 @@ static int dncmp(const void * a, const void * b)
     return strcmp(*first, *second);
 }
 
-void compressFilelist(Header h)
+static void compressFilelist(Header h)
 {
     struct rpmtd_s fileNames;
     char ** dirNames;
     const char ** baseNames;
     uint32_t * dirIndexes;
     rpm_count_t count;
-    int xx, i;
+    int i;
     int dirIndex = -1;
 
     /*
@@ -40,7 +36,7 @@ void compressFilelist(Header h)
      */
 
     if (headerIsEntry(h, RPMTAG_DIRNAMES)) {
-       xx = headerDel(h, RPMTAG_OLDFILENAMES);
+       headerDel(h, RPMTAG_OLDFILENAMES);
        return;         /* Already converted. */
     }
 
@@ -54,23 +50,31 @@ void compressFilelist(Header h)
     baseNames = xmalloc(sizeof(*dirNames) * count);
     dirIndexes = xmalloc(sizeof(*dirIndexes) * count);
 
-    if (headerIsSource(h)) {
-       /* HACK. Source RPM, so just do things differently */
-       dirIndex = 0;
-       dirNames[dirIndex] = xstrdup("");
-       while ((i = rpmtdNext(&fileNames)) >= 0) {
-           dirIndexes[i] = dirIndex;
-           baseNames[i] = rpmtdGetString(&fileNames);
+    /* HACK. Source RPM, so just do things differently */
+    {  const char *fn = rpmtdGetString(&fileNames);
+       if (fn && *fn != '/') {
+           dirIndex = 0;
+           dirNames[dirIndex] = xstrdup("");
+           while ((i = rpmtdNext(&fileNames)) >= 0) {
+               dirIndexes[i] = dirIndex;
+               baseNames[i] = rpmtdGetString(&fileNames);
+           }
+           goto exit;
        }
-       goto exit;
     }
 
+    /* 
+     * XXX EVIL HACK, FIXME:
+     * This modifies (and then restores) a const string from rpmtd
+     * through basename retrieved from strrchr() which silently 
+     * casts away const on return.
+     */
     while ((i = rpmtdNext(&fileNames)) >= 0) {
        char ** needle;
        char savechar;
        char * baseName;
        size_t len;
-       const char *filename = rpmtdGetString(&fileNames);
+       char *filename = (char *) rpmtdGetString(&fileNames); /* HACK HACK */
 
        if (filename == NULL)   /* XXX can't happen */
            continue;
@@ -80,7 +84,7 @@ void compressFilelist(Header h)
        savechar = *baseName;
        *baseName = '\0';
        if (dirIndex < 0 ||
-           (needle = bsearch(filename, dirNames, dirIndex + 1, sizeof(dirNames[0]), dncmp)) == NULL) {
+           (needle = bsearch(&filename, dirNames, dirIndex + 1, sizeof(dirNames[0]), dncmp)) == NULL) {
            char *s = xmalloc(len + 1);
            rstrlcpy(s, filename, len + 1);
            dirIndexes[i] = ++dirIndex;
@@ -108,10 +112,10 @@ exit:
     free(baseNames);
     free(dirIndexes);
 
-    xx = headerDel(h, RPMTAG_OLDFILENAMES);
+    headerDel(h, RPMTAG_OLDFILENAMES);
 }
 
-void expandFilelist(Header h)
+static void expandFilelist(Header h)
 {
     struct rpmtd_s filenames;
 
@@ -135,15 +139,14 @@ void expandFilelist(Header h)
  */
 static void providePackageNVR(Header h)
 {
-    const char *name;
-    char *pEVR;
+    const char *name = headerGetString(h, RPMTAG_NAME);
+    char *pEVR = headerGetAsString(h, RPMTAG_EVR);
     rpmsenseFlags pFlags = RPMSENSE_EQUAL;
     int bingo = 1;
     struct rpmtd_s pnames;
     rpmds hds, nvrds;
 
     /* Generate provides for this package name-version-release. */
-    pEVR = headerGetEVR(h, &name);
     if (!(name && pEVR))
        return;
 
@@ -169,7 +172,7 @@ static void providePackageNVR(Header h)
     }
 
     /* see if we already have this provide */
-    hds = rpmdsNew(h, RPMTAG_PROVIDENAME, 1);
+    hds = rpmdsNew(h, RPMTAG_PROVIDENAME, 0);
     nvrds = rpmdsSingle(RPMTAG_PROVIDENAME, name, pEVR, pFlags);
     if (rpmdsFind(hds, nvrds) >= 0) {
        bingo = 0;
@@ -180,44 +183,16 @@ static void providePackageNVR(Header h)
 
 exit:
     if (bingo) {
-       const char *evr = pEVR;
        headerPutString(h, RPMTAG_PROVIDENAME, name);
-       headerPutString(h, RPMTAG_PROVIDEVERSION, evr);
+       headerPutString(h, RPMTAG_PROVIDEVERSION, pEVR);
        headerPutUint32(h, RPMTAG_PROVIDEFLAGS, &pFlags, 1);
     }
     rpmtdFreeData(&pnames);
     free(pEVR);
 }
 
-void legacyRetrofit(Header h)
+static void legacyRetrofit(Header h)
 {
-    struct rpmtd_s dprefix;
-
-    /*
-     * We don't use these entries (and rpm >= 2 never has) and they are
-     * pretty misleading. Let's just get rid of them so they don't confuse
-     * anyone.
-     */
-    if (headerIsEntry(h, RPMTAG_FILEUSERNAME))
-       (void) headerDel(h, RPMTAG_FILEUIDS);
-    if (headerIsEntry(h, RPMTAG_FILEGROUPNAME))
-       (void) headerDel(h, RPMTAG_FILEGIDS);
-
-    /*
-     * We switched the way we do relocatable packages. We fix some of
-     * it up here, though the install code still has to be a bit 
-     * careful. This fixup makes queries give the new values though,
-     * which is quite handy.
-     */
-    if (headerGet(h, RPMTAG_DEFAULTPREFIX, &dprefix, HEADERGET_MINMEM)) {
-       const char *prefix = rpmtdGetString(&dprefix);
-       char * nprefix = stripTrailingChar(xstrdup(prefix), '/');
-       
-       headerPutString(h, RPMTAG_PREFIXES, nprefix);
-       free(nprefix);
-       rpmtdFreeData(&dprefix);
-    }
-
     /*
      * The file list was moved to a more compressed format which not
      * only saves memory (nice), but gives fingerprinting a nice, fat
@@ -226,14 +201,179 @@ void legacyRetrofit(Header h)
      */
      compressFilelist(h);
 
-    /* XXX binary rpms always have RPMTAG_SOURCERPM, source rpms do not */
-    if (headerIsSource(h)) {
-       uint32_t one = 1;
-
-       if (!headerIsEntry(h, RPMTAG_SOURCEPACKAGE))
-           headerPutUint32(h, RPMTAG_SOURCEPACKAGE, &one, 1);
-    } else {
-       /* Retrofit "Provide: name = EVR" for binary packages. */
+    /* Retrofit "Provide: name = EVR" for binary packages. */
+    if (!headerIsSource(h)) {
        providePackageNVR(h);
     }
 }
+
+int headerConvert(Header h, int op)
+{
+    int rc = 1;
+
+    if (h == NULL)
+       return 0;
+
+    switch (op) {
+    case HEADERCONV_EXPANDFILELIST:
+       expandFilelist(h);
+       break;
+    case HEADERCONV_COMPRESSFILELIST:
+       compressFilelist(h);
+       break;
+    case HEADERCONV_RETROFIT_V3:
+       legacyRetrofit(h);
+       break;
+    default:
+       rc = 0;
+       break;
+    }
+    return rc;
+};
+
+/*
+ * Backwards compatibility wrappers for legacy interfaces.
+ * Remove these some day...
+ */
+#define _RPM_4_4_COMPAT
+#include <rpm/rpmlegacy.h>
+
+/* dumb macro to avoid 50 copies of this code while converting... */
+#define TDWRAP() \
+    if (type) \
+       *type = td.type; \
+    if (p) \
+       *p = td.data; \
+    else \
+       rpmtdFreeData(&td); \
+    if (c) \
+       *c = td.count
+
+int headerRemoveEntry(Header h, rpm_tag_t tag)
+{
+    return headerDel(h, tag);
+}
+
+static void *_headerFreeData(rpm_data_t data, rpm_tagtype_t type)
+{
+    if (data) {
+       if (type == RPM_FORCEFREE_TYPE ||
+           type == RPM_STRING_ARRAY_TYPE ||
+           type == RPM_I18NSTRING_TYPE ||
+           type == RPM_BIN_TYPE)
+               free(data);
+    }
+    return NULL;
+}
+
+void * headerFreeData(rpm_data_t data, rpm_tagtype_t type)
+{
+    return _headerFreeData(data, type);
+}
+
+void * headerFreeTag(Header h, rpm_data_t data, rpm_tagtype_t type)
+{
+    return _headerFreeData(data, type);
+}
+
+static int headerGetWrap(Header h, rpm_tag_t tag,
+               rpm_tagtype_t * type,
+               rpm_data_t * p,
+               rpm_count_t * c,
+               headerGetFlags flags)
+{
+    struct rpmtd_s td;
+    int rc;
+
+    rc = headerGet(h, tag, &td, flags);
+    TDWRAP();
+    return rc;
+}
+
+int headerGetEntry(Header h, rpm_tag_t tag,
+                       rpm_tagtype_t * type,
+                       rpm_data_t * p,
+                       rpm_count_t * c)
+{
+    return headerGetWrap(h, tag, type, p, c, HEADERGET_DEFAULT);
+}
+
+int headerGetEntryMinMemory(Header h, rpm_tag_t tag,
+                       rpm_tagtype_t * type,
+                       rpm_data_t * p,
+                       rpm_count_t * c)
+{
+    return headerGetWrap(h, tag, type, (rpm_data_t) p, c, HEADERGET_MINMEM);
+}
+
+/* XXX shut up compiler warning from missing prototype */
+int headerGetRawEntry(Header h, rpm_tag_t tag, rpm_tagtype_t * type, rpm_data_t * p,
+               rpm_count_t * c);
+
+int headerGetRawEntry(Header h, rpm_tag_t tag, rpm_tagtype_t * type, rpm_data_t * p,
+               rpm_count_t * c)
+{
+    if (p == NULL) 
+       return headerIsEntry(h, tag);
+
+    return headerGetWrap(h, tag, type, p, c, HEADERGET_RAW);
+}
+
+int headerNextIterator(HeaderIterator hi,
+               rpm_tag_t * tag,
+               rpm_tagtype_t * type,
+               rpm_data_t * p,
+               rpm_count_t * c)
+{
+    struct rpmtd_s td;
+    int rc;
+
+    rc = headerNext(hi, &td);
+    if (tag)
+       *tag = td.tag;
+    TDWRAP();
+    return rc;
+}
+
+int headerModifyEntry(Header h, rpm_tag_t tag, rpm_tagtype_t type,
+                       rpm_constdata_t p, rpm_count_t c)
+{
+    struct rpmtd_s td = {
+       .tag = tag,
+       .type = type,
+       .data = (void *) p,
+       .count = c,
+    };
+    return headerMod(h, &td);
+}
+
+static int headerPutWrap(Header h, rpm_tag_t tag, rpm_tagtype_t type,
+               rpm_constdata_t p, rpm_count_t c, headerPutFlags flags)
+{
+    struct rpmtd_s td = {
+       .tag = tag,
+       .type = type,
+       .data = (void *) p,
+       .count = c,
+    };
+    return headerPut(h, &td, flags);
+}
+
+int headerAddOrAppendEntry(Header h, rpm_tag_t tag, rpm_tagtype_t type,
+               rpm_constdata_t p, rpm_count_t c)
+{
+    return headerPutWrap(h, tag, type, p, c, HEADERPUT_APPEND);
+}
+
+int headerAppendEntry(Header h, rpm_tag_t tag, rpm_tagtype_t type,
+               rpm_constdata_t p, rpm_count_t c)
+{
+    return headerPutWrap(h, tag, type, p, c, HEADERPUT_APPEND);
+}
+
+int headerAddEntry(Header h, rpm_tag_t tag, rpm_tagtype_t type,
+               rpm_constdata_t p, rpm_count_t c)
+{
+    return headerPutWrap(h, tag, type, p, c, HEADERPUT_DEFAULT);
+}
+#undef _RPM_4_4_COMPAT