From d10acc7c2a280bd8e0aea3610d9a8767521a5f40 Mon Sep 17 00:00:00 2001 From: Panu Matilainen Date: Thu, 22 Nov 2007 16:28:30 +0200 Subject: [PATCH] Move makeTempFile() from misc.h to rpmfileutil.h - probably not very useful outside rpm but used all over the tree, - rename to rpmMkTempFile() for namespacing since we're exporting it now --- build/build.c | 4 +-- build/pack.c | 4 +-- lib/misc.c | 99 --------------------------------------------------- lib/misc.h | 17 --------- lib/psm.c | 5 +-- lib/rpmchecksig.c | 6 ++-- lib/signature.c | 8 ++--- rpmio/Makefile.am | 1 + rpmio/rpmfileutil.c | 100 ++++++++++++++++++++++++++++++++++++++++++++++++++++ rpmio/rpmfileutil.h | 16 +++++++++ 10 files changed, 131 insertions(+), 129 deletions(-) diff --git a/build/build.c b/build/build.c index 25c3d44..0072dc6 100644 --- a/build/build.c +++ b/build/build.c @@ -7,7 +7,7 @@ #include #include -#include "misc.h" /* XXX for makeTempFile */ +#include #include "debug.h" @@ -128,7 +128,7 @@ int doScript(rpmSpec spec, rpmBuildFlags what, const char *name, StringBuf sb, i goto exit; } - if (makeTempFile(rootURL, &scriptName, &fd) || fd == NULL || Ferror(fd)) { + if (rpmMkTempFile(rootURL, &scriptName, &fd) || fd == NULL || Ferror(fd)) { rpmlog(RPMLOG_ERR, _("Unable to open temp file.\n")); rc = RPMLOG_ERR; goto exit; diff --git a/build/pack.c b/build/pack.c index 17bc660..c823f28 100644 --- a/build/pack.c +++ b/build/pack.c @@ -23,7 +23,7 @@ #include "legacy.h" /* XXX providePackageNVR */ #include "signature.h" #include "rpmlead.h" -#include "misc.h" /* XXX for makeTempFile */ +#include #include #include "debug.h" @@ -456,7 +456,7 @@ int writeRPM(Header *hdrp, unsigned char ** pkgidp, const char *fileName, * Write the header+archive into a temp file so that the size of * archive (after compression) can be added to the header. */ - if (makeTempFile(NULL, &sigtarget, &fd)) { + if (rpmMkTempFile(NULL, &sigtarget, &fd)) { rc = RPMLOG_ERR; rpmlog(RPMLOG_ERR, _("Unable to open temp file.\n")); goto exit; diff --git a/lib/misc.c b/lib/misc.c index d0151e2..4a578fc 100644 --- a/lib/misc.c +++ b/lib/misc.c @@ -69,105 +69,6 @@ int dosetenv(const char * name, const char * value, int overwrite) return putenv(a); } -int makeTempFile(const char * prefix, const char ** fnptr, FD_t * fdptr) -{ - const char * tpmacro = "%{?_tmppath:%{_tmppath}}%{!?_tmppath:" LOCALSTATEDIR "/tmp}"; - const char * tempfn = NULL; - const char * tfn = NULL; - static int _initialized = 0; - int temput; - FD_t fd = NULL; - int ran; - - if (!prefix) prefix = ""; - - /* Create the temp directory if it doesn't already exist. */ - if (!_initialized) { - _initialized = 1; - tempfn = rpmGenPath(prefix, tpmacro, NULL); - if (rpmioMkpath(tempfn, 0755, (uid_t) -1, (gid_t) -1)) - goto errxit; - } - - /* XXX should probably use mkstemp here */ - srand(time(NULL)); - ran = rand() % 100000; - - /* maybe this should use link/stat? */ - - do { - char tfnbuf[64]; -#ifndef NOTYET - sprintf(tfnbuf, "rpm-tmp.%d", ran++); - tempfn = _free(tempfn); - tempfn = rpmGenPath(prefix, tpmacro, tfnbuf); -#else - strcpy(tfnbuf, "rpm-tmp.XXXXXX"); - tempfn = _free(tempfn); - tempfn = rpmGenPath(prefix, tpmacro, mktemp(tfnbuf)); -#endif - - temput = urlPath(tempfn, &tfn); - if (*tfn == '\0') goto errxit; - - switch (temput) { - case URL_IS_DASH: - case URL_IS_HKP: - goto errxit; - break; - case URL_IS_HTTPS: - case URL_IS_HTTP: - case URL_IS_FTP: - default: - break; - } - - fd = Fopen(tempfn, "w+x.ufdio"); - /* XXX FIXME: errno may not be correct for ufdio */ - } while ((fd == NULL || Ferror(fd)) && errno == EEXIST); - - if (fd == NULL || Ferror(fd)) - goto errxit; - - switch(temput) { - case URL_IS_PATH: - case URL_IS_UNKNOWN: - { struct stat sb, sb2; - if (!stat(tfn, &sb) && S_ISLNK(sb.st_mode)) { - rpmlog(RPMLOG_ERR, _("error creating temporary file %s\n"), tfn); - goto errxit; - } - - if (sb.st_nlink != 1) { - rpmlog(RPMLOG_ERR, _("error creating temporary file %s\n"), tfn); - goto errxit; - } - - if (fstat(Fileno(fd), &sb2) == 0) { - if (sb2.st_ino != sb.st_ino || sb2.st_dev != sb.st_dev) { - rpmlog(RPMLOG_ERR, _("error creating temporary file %s\n"), tfn); - goto errxit; - } - } - } break; - default: - break; - } - - if (fnptr) - *fnptr = tempfn; - else - tempfn = _free(tempfn); - *fdptr = fd; - - return 0; - -errxit: - tempfn = _free(tempfn); - if (fd != NULL) (void) Fclose(fd); - return 1; -} - char * currentDirectory(void) { int currDirLen = 0; diff --git a/lib/misc.h b/lib/misc.h index 0730045..c860ab8 100644 --- a/lib/misc.h +++ b/lib/misc.h @@ -38,23 +38,6 @@ int dosetenv(const char * name, const char * value, int overwrite); int doputenv(const char * str); /** - * Return file handle for a temporaray file. - * A unique temporaray file path will be generated using - * rpmGenPath(prefix, "%{_tmppath}/", "rpm-tmp.XXXXX") - * where "XXXXXX" is filled in using rand(3). The file is opened, and - * the link count and (dev,ino) location are verified after opening. - * The file name and the open file handle are returned. - * - * @param prefix leading part of temp file path - * @retval fnptr temp file name (or NULL) - * @retval fdptr temp file handle - * @return 0 on success - */ -int makeTempFile(const char * prefix, - const char ** fnptr, - FD_t * fdptr); - -/** * Return (malloc'd) current working directory. * @return current working directory (malloc'ed) */ diff --git a/lib/psm.c b/lib/psm.c index 0107327..4743325 100644 --- a/lib/psm.c +++ b/lib/psm.c @@ -29,7 +29,8 @@ #include "rpmlead.h" /* writeLead proto */ #include "signature.h" /* signature constants */ #include "legacy.h" /* XXX rpmfiBuildFNames() */ -#include "misc.h" /* XXX rpmMkdirPath, makeTempFile, doputenv */ +#include /* rpmMkTempFile() */ +#include "misc.h" /* XXX rpmMkdirPath, doputenv */ #include /* XXX for db_chrootDone */ #include #include "debug.h" @@ -680,7 +681,7 @@ static rpmRC runScript(rpmpsm psm, Header h, const char * sln, const char * rootDir = rpmtsRootDir(ts); FD_t fd; - if (makeTempFile((!rpmtsChrootDone(ts) ? rootDir : "/"), &fn, &fd)) { + if (rpmMkTempFile((!rpmtsChrootDone(ts) ? rootDir : "/"), &fn, &fd)) { if (prefixes != NULL && freePrefixes) free(prefixes); return RPMRC_FAIL; } diff --git a/lib/rpmchecksig.c b/lib/rpmchecksig.c index 9629b73..04de037 100644 --- a/lib/rpmchecksig.c +++ b/lib/rpmchecksig.c @@ -16,7 +16,7 @@ #include #include "rpmlead.h" #include "signature.h" -#include "misc.h" /* XXX for makeTempFile() */ +#include /* rpmMkTempFile() */ #include "debug.h" int _print_pkts = 0; @@ -55,8 +55,8 @@ static int manageFile(FD_t *fdp, /* open a temp file */ if (*fdp == NULL && (fnp == NULL || *fnp == NULL)) { fn = NULL; - if (makeTempFile(NULL, (fnp ? &fn : NULL), &fd)) { - rpmlog(RPMLOG_ERR, _("makeTempFile failed\n")); + if (rpmMkTempFile(NULL, (fnp ? &fn : NULL), &fd)) { + rpmlog(RPMLOG_ERR, _("rpmMkTempFile failed\n")); return 1; } if (fnp != NULL) diff --git a/lib/signature.c b/lib/signature.c index 7d58c1e..156fc24 100644 --- a/lib/signature.c +++ b/lib/signature.c @@ -13,8 +13,8 @@ #include #include -#include "misc.h" /* XXX for dosetenv() and makeTempFile() */ -#include "rpmfileutil.h" +#include "misc.h" /* XXX for dosetenv() */ +#include #include "rpmlead.h" #include "signature.h" #include "header_internal.h" @@ -672,7 +672,7 @@ static int makeHDRSignature(Header sigh, const char * file, int32_t sigTag, if (h == NULL) goto exit; (void) Fclose(fd); fd = NULL; - if (makeTempFile(NULL, &fn, &fd)) + if (rpmMkTempFile(NULL, &fn, &fd)) goto exit; if (headerWrite(fd, h, HEADER_MAGIC_YES)) goto exit; @@ -690,7 +690,7 @@ static int makeHDRSignature(Header sigh, const char * file, int32_t sigTag, if (h == NULL) goto exit; (void) Fclose(fd); fd = NULL; - if (makeTempFile(NULL, &fn, &fd)) + if (rpmMkTempFile(NULL, &fn, &fd)) goto exit; if (headerWrite(fd, h, HEADER_MAGIC_YES)) goto exit; diff --git a/rpmio/Makefile.am b/rpmio/Makefile.am index 0510be0..9b4b17a 100644 --- a/rpmio/Makefile.am +++ b/rpmio/Makefile.am @@ -7,6 +7,7 @@ AM_CPPFLAGS += @WITH_LUA_INCLUDE@ AM_CPPFLAGS += @WITH_POPT_INCLUDE@ AM_CPPFLAGS += -I$(top_srcdir)/misc AM_CPPFLAGS += -DRPMCONFIGDIR="\"@RPMCONFIGDIR@\"" +AM_CPPFLAGS += -DLOCALSTATEDIR="\"$(localstatedir)\"" usrlibdir = $(libdir) usrlib_LTLIBRARIES = librpmio.la diff --git a/rpmio/rpmfileutil.c b/rpmio/rpmfileutil.c index e79a65a..30047da 100644 --- a/rpmio/rpmfileutil.c +++ b/rpmio/rpmfileutil.c @@ -17,6 +17,7 @@ #include #include #include +#include #include static int open_dso(const char * path, pid_t * pidp, size_t *fsizep) @@ -219,3 +220,102 @@ exit: return rc; } +int rpmMkTempFile(const char * prefix, const char ** fnptr, FD_t * fdptr) +{ + const char * tpmacro = "%{?_tmppath:%{_tmppath}}%{!?_tmppath:" LOCALSTATEDIR "/tmp}"; + const char * tempfn = NULL; + const char * tfn = NULL; + static int _initialized = 0; + int temput; + FD_t fd = NULL; + int ran; + + if (!prefix) prefix = ""; + + /* Create the temp directory if it doesn't already exist. */ + if (!_initialized) { + _initialized = 1; + tempfn = rpmGenPath(prefix, tpmacro, NULL); + if (rpmioMkpath(tempfn, 0755, (uid_t) -1, (gid_t) -1)) + goto errxit; + } + + /* XXX should probably use mkstemp here */ + srand(time(NULL)); + ran = rand() % 100000; + + /* maybe this should use link/stat? */ + + do { + char tfnbuf[64]; +#ifndef NOTYET + sprintf(tfnbuf, "rpm-tmp.%d", ran++); + tempfn = _free(tempfn); + tempfn = rpmGenPath(prefix, tpmacro, tfnbuf); +#else + strcpy(tfnbuf, "rpm-tmp.XXXXXX"); + tempfn = _free(tempfn); + tempfn = rpmGenPath(prefix, tpmacro, mktemp(tfnbuf)); +#endif + + temput = urlPath(tempfn, &tfn); + if (*tfn == '\0') goto errxit; + + switch (temput) { + case URL_IS_DASH: + case URL_IS_HKP: + goto errxit; + break; + case URL_IS_HTTPS: + case URL_IS_HTTP: + case URL_IS_FTP: + default: + break; + } + + fd = Fopen(tempfn, "w+x.ufdio"); + /* XXX FIXME: errno may not be correct for ufdio */ + } while ((fd == NULL || Ferror(fd)) && errno == EEXIST); + + if (fd == NULL || Ferror(fd)) + goto errxit; + + switch(temput) { + case URL_IS_PATH: + case URL_IS_UNKNOWN: + { struct stat sb, sb2; + if (!stat(tfn, &sb) && S_ISLNK(sb.st_mode)) { + rpmlog(RPMLOG_ERR, _("error creating temporary file %s\n"), tfn); + goto errxit; + } + + if (sb.st_nlink != 1) { + rpmlog(RPMLOG_ERR, _("error creating temporary file %s\n"), tfn); + goto errxit; + } + + if (fstat(Fileno(fd), &sb2) == 0) { + if (sb2.st_ino != sb.st_ino || sb2.st_dev != sb.st_dev) { + rpmlog(RPMLOG_ERR, _("error creating temporary file %s\n"), tfn); + goto errxit; + } + } + } break; + default: + break; + } + + if (fnptr) + *fnptr = tempfn; + else + tempfn = _free(tempfn); + *fdptr = fd; + + return 0; + +errxit: + tempfn = _free(tempfn); + if (fd != NULL) (void) Fclose(fd); + return 1; +} + diff --git a/rpmio/rpmfileutil.h b/rpmio/rpmfileutil.h index b2f72f5..0022e5d 100644 --- a/rpmio/rpmfileutil.h +++ b/rpmio/rpmfileutil.h @@ -15,4 +15,20 @@ int rpmDoDigest(pgpHashAlgo algo, const char * fn,int asAscii, unsigned char * digest, size_t * fsizep); + +/** + * Return file handle for a temporaray file. + * A unique temporaray file path will be generated using + * rpmGenPath(prefix, "%{_tmppath}/", "rpm-tmp.XXXXX") + * where "XXXXXX" is filled in using rand(3). The file is opened, and + * the link count and (dev,ino) location are verified after opening. + * The file name and the open file handle are returned. + * + * @param prefix leading part of temp file path + * @retval fnptr temp file name (or NULL) + * @retval fdptr temp file handle + * @return 0 on success + */ +int rpmMkTempFile(const char * prefix, const char ** fnptr, FD_t * fdptr); + #endif /* _RPMFILEUTIL_H */ -- 2.7.4