diff options
author | Panu Matilainen <pmatilai@redhat.com> | 2007-11-22 16:28:30 +0200 |
---|---|---|
committer | Panu Matilainen <pmatilai@redhat.com> | 2007-11-22 16:28:30 +0200 |
commit | d10acc7c2a280bd8e0aea3610d9a8767521a5f40 (patch) | |
tree | f7aeffa417332f6d81021aef3b2db26ff05174d2 | |
parent | d8cd2b6b1139a81b819e66817e22d814fe25a9e7 (diff) | |
download | librpm-tizen-d10acc7c2a280bd8e0aea3610d9a8767521a5f40.tar.gz librpm-tizen-d10acc7c2a280bd8e0aea3610d9a8767521a5f40.tar.bz2 librpm-tizen-d10acc7c2a280bd8e0aea3610d9a8767521a5f40.zip |
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
-rw-r--r-- | build/build.c | 4 | ||||
-rw-r--r-- | build/pack.c | 4 | ||||
-rw-r--r-- | lib/misc.c | 99 | ||||
-rw-r--r-- | lib/misc.h | 17 | ||||
-rw-r--r-- | lib/psm.c | 5 | ||||
-rw-r--r-- | lib/rpmchecksig.c | 6 | ||||
-rw-r--r-- | lib/signature.c | 8 | ||||
-rw-r--r-- | rpmio/Makefile.am | 1 | ||||
-rw-r--r-- | rpmio/rpmfileutil.c | 100 | ||||
-rw-r--r-- | rpmio/rpmfileutil.h | 16 |
10 files changed, 131 insertions, 129 deletions
diff --git a/build/build.c b/build/build.c index 25c3d444f..0072dc663 100644 --- a/build/build.c +++ b/build/build.c @@ -7,7 +7,7 @@ #include <rpmbuild.h> #include <rpmlog.h> -#include "misc.h" /* XXX for makeTempFile */ +#include <rpmfileutil.h> #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 17bc66018..c823f286e 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 <rpmfileutil.h> #include <rpmlog.h> #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 d0151e2c8..4a578fc91 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 0730045f0..c860ab892 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) */ @@ -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 <rpmfileutil.h> /* rpmMkTempFile() */ +#include "misc.h" /* XXX rpmMkdirPath, doputenv */ #include <rpmdb.h> /* XXX for db_chrootDone */ #include <rpmlog.h> #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 9629b73e2..04de0371b 100644 --- a/lib/rpmchecksig.c +++ b/lib/rpmchecksig.c @@ -16,7 +16,7 @@ #include <rpmlog.h> #include "rpmlead.h" #include "signature.h" -#include "misc.h" /* XXX for makeTempFile() */ +#include <rpmfileutil.h> /* 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 7d58c1ed5..156fc242f 100644 --- a/lib/signature.c +++ b/lib/signature.c @@ -13,8 +13,8 @@ #include <rpmts.h> #include <rpmlog.h> -#include "misc.h" /* XXX for dosetenv() and makeTempFile() */ -#include "rpmfileutil.h" +#include "misc.h" /* XXX for dosetenv() */ +#include <rpmfileutil.h> #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 0510be0e6..9b4b17a80 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 e79a65ad3..30047dadc 100644 --- a/rpmio/rpmfileutil.c +++ b/rpmio/rpmfileutil.c @@ -17,6 +17,7 @@ #include <rpmfileutil.h> #include <rpmurl.h> #include <rpmmacro.h> +#include <rpmlog.h> #include <argv.h> 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 b2f72f518..0022e5d03 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 */ |