If we need a FILE stream then get one...
authorPanu Matilainen <pmatilai@redhat.com>
Thu, 7 Jul 2011 19:51:29 +0000 (22:51 +0300)
committerPanu Matilainen <Panu Matilainen pmatilai@redhat.com>
Fri, 8 Jul 2011 07:15:58 +0000 (10:15 +0300)
- Replace the hysterical "lets see if the temp creation gave an fpio
  fd (it didn't), if not open another fd and then get its private
  FILE pointer" fiddling: since we need a FILE stream then open one
  with fdopen(), duh. Grabbing a rpmio fd to begin with is stupid enough
  when all we want is a stream, but wanting to use rpmMkTempFile()
  functionality...
- Also fixes an fd+mem leak in the unlikely but possible case that
  rpmMkTempFile() succeeds but fdopen() fails.

build/build.c

index 043760b..04b039c 100644 (file)
@@ -65,8 +65,7 @@ rpmRC doScript(rpmSpec spec, rpmBuildFlags what, const char *name,
     const char **argv = NULL;
     FILE * fp = NULL;
 
-    FD_t fd;
-    FD_t xfd;
+    FD_t fd = NULL;
     pid_t pid;
     pid_t child;
     int status;
@@ -117,18 +116,14 @@ rpmRC doScript(rpmSpec spec, rpmBuildFlags what, const char *name,
     }
     
     fd = rpmMkTempFile(spec->rootDir, &scriptName);
-    if (fd == NULL || Ferror(fd)) {
-       rpmlog(RPMLOG_ERR, _("Unable to open temp file.\n"));
+    if (Ferror(fd)) {
+       rpmlog(RPMLOG_ERR, _("Unable to open temp file: %s\n"), Fstrerror(fd));
        rc = RPMRC_FAIL;
        goto exit;
     }
 
-    if (fdGetFILE(fd) == NULL)
-       xfd = Fdopen(fd, "w.fpio");
-    else
-       xfd = fd;
-
-    if ((fp = fdGetFILE(xfd)) == NULL) {
+    if ((fp = fdopen(Fileno(fd), "w")) == NULL) {
+       rpmlog(RPMLOG_ERR, _("Unable to open stream: %s\n"), strerror(errno));
        rc = RPMRC_FAIL;
        goto exit;
     }
@@ -148,8 +143,7 @@ rpmRC doScript(rpmSpec spec, rpmBuildFlags what, const char *name,
        fprintf(fp, "%s", sb);
 
     (void) fputs(buildPost, fp);
-    
-    (void) Fclose(xfd);
+    (void) fclose(fp);
 
     if (test) {
        rc = RPMRC_OK;
@@ -194,6 +188,7 @@ rpmRC doScript(rpmSpec spec, rpmBuildFlags what, const char *name,
        rc = RPMRC_OK;
     
 exit:
+    Fclose(fd);
     if (scriptName) {
        if (rc == RPMRC_OK)
            (void) unlink(scriptName);