Eliminate nasty hackery with "special" doc + docdir format
authorPanu Matilainen <pmatilai@redhat.com>
Tue, 6 May 2008 09:48:09 +0000 (12:48 +0300)
committerPanu Matilainen <pmatilai@redhat.com>
Tue, 6 May 2008 09:48:09 +0000 (12:48 +0300)
- figure out docdir early on in spec parsing, store in package struct
  instead of abusing parse buffer
- fixes memleaks from docdir_fmt and headerSprintf too

build/files.c
build/parsePreamble.c
build/rpmspec.h
build/spec.c

index 18b71b3..e7074dd 100644 (file)
@@ -863,39 +863,16 @@ static rpmRC parseForSimple(rpmSpec spec, Package pkg, char * buf,
                     (*fileName ? *fileName : ""));
            res = RPMRC_FAIL;
        } else {
-       /* XXX WATCHOUT: buf is an arg */
-          {
-               static const char *_docdir_fmt = NULL;
-               static int oneshot = 0;
-               char *ddir, *fmt;
-               errmsg_t errstr;
-               if (!oneshot) {
-                   _docdir_fmt = rpmExpand("%{?_docdir_fmt}", NULL);
-                   if (!_docdir_fmt || !*_docdir_fmt)
-                       _docdir_fmt = "%{NAME}-%{VERSION}";
-                   oneshot = 1;
-               }
-               fmt = headerSprintf(pkg->header, _docdir_fmt, rpmTagTable, rpmHeaderFormats, &errstr);
-               if (!fmt) {
-                   rpmlog(RPMLOG_ERR, _("illegal _docdir_fmt: %s\n"), errstr);
-                   res = RPMRC_FAIL;
-               }
-               ddir = rpmGetPath("%{_docdir}/", fmt, NULL);
-               strcpy(buf, ddir);
-               ddir = _free(ddir);
-           }
-
-       /* XXX FIXME: this is easy to do as macro expansion */
-
+           /* XXX FIXME: this is easy to do as macro expansion */
            if (! fl->passedSpecialDoc) {
                pkg->specialDoc = newStringBuf();
                appendStringBuf(pkg->specialDoc, "DOCDIR=$RPM_BUILD_ROOT");
-               appendLineStringBuf(pkg->specialDoc, buf);
+               appendLineStringBuf(pkg->specialDoc, pkg->specialDocDir);
                appendLineStringBuf(pkg->specialDoc, "export DOCDIR");
                appendLineStringBuf(pkg->specialDoc, "rm -rf $DOCDIR");
                appendLineStringBuf(pkg->specialDoc, RPM_MKDIR_P " $DOCDIR");
 
-               *fileName = buf;
+               *fileName = pkg->specialDocDir;
                fl->passedSpecialDoc = 1;
                fl->isSpecialDoc = 1;
            }
index 9a23fcb..f606100 100644 (file)
@@ -327,6 +327,31 @@ static void fillOutMainPackage(Header h)
     }
 }
 
+static int getSpecialDocDir(Package pkg)
+{
+    const char *errstr, *docdir_fmt = "%{NAME}-%{VERSION}";
+    char *fmt_macro, *fmt; 
+    int rc = -1;
+
+    fmt_macro = rpmExpand("%{?_docdir_fmt}", NULL);
+    if (fmt_macro && strlen(fmt_macro) > 0) {
+       docdir_fmt = fmt_macro;
+    }
+    fmt = headerSprintf(pkg->header, docdir_fmt, 
+                       rpmTagTable, rpmHeaderFormats, &errstr);
+    if (!fmt) {
+       rpmlog(RPMLOG_ERR, _("illegal _docdir_fmt: %s\n"), errstr);
+       goto exit;
+    }
+    pkg->specialDocDir = rpmGetPath("%{_docdir}/", fmt, NULL);
+    rc = 0;
+
+exit:
+    free(fmt);
+    free(fmt_macro);
+    return rc;
+}
+
 /**
  */
 static rpmRC readIcon(Header h, const char * file)
@@ -900,6 +925,11 @@ int parsePreamble(rpmSpec spec, int initialPackage)
     if (checkForRequired(pkg->header, NVR)) {
        goto exit;
     }
+
+    if (getSpecialDocDir(pkg)) {
+       goto exit;
+    }
+       
     /* if we get down here nextPart has been set to non-error */
     res = nextPart;
 
index 77c7b30..31a6087 100644 (file)
@@ -167,6 +167,7 @@ struct Package_s {
     char * verifyFile; /*!< %verifyscript scriptlet. */
 
     StringBuf specialDoc;
+    char *specialDocDir;
 
     struct TriggerFileEntry * triggerFiles;
 
index 9f9c6f2..038839c 100644 (file)
@@ -131,6 +131,7 @@ Package newPackage(rpmSpec spec)
     p->verifyFile = NULL;
 
     p->specialDoc = NULL;
+    p->specialDocDir = NULL;
 
     if (spec->packages == NULL) {
        spec->packages = p;
@@ -166,6 +167,7 @@ Package freePackage(Package pkg)
     }
 
     pkg->specialDoc = freeStringBuf(pkg->specialDoc);
+    pkg->specialDocDir = _free(pkg->specialDocDir);
     pkg->icon = freeSources(pkg->icon);
     pkg->triggerFiles = freeTriggerFiles(pkg->triggerFiles);