2000-08-28 03:27:03 +08:00
|
|
|
/**
|
|
|
|
* \file lib/misc.c
|
|
|
|
*/
|
|
|
|
|
1998-07-26 05:00:26 +08:00
|
|
|
#include "system.h"
|
1997-05-07 22:56:06 +08:00
|
|
|
|
2002-02-08 00:55:01 +08:00
|
|
|
/* just to put a marker in librpm.a */
|
|
|
|
const char * RPMVERSION = VERSION;
|
|
|
|
|
2001-09-26 04:51:34 +08:00
|
|
|
#include "rpmio_internal.h"
|
1999-10-31 05:26:45 +08:00
|
|
|
#include <rpmurl.h>
|
1999-07-14 05:37:57 +08:00
|
|
|
#include <rpmmacro.h> /* XXX for rpmGetPath */
|
2001-09-26 04:51:34 +08:00
|
|
|
#include <rpmlib.h>
|
2002-12-19 06:54:00 +08:00
|
|
|
#include "legacy.h"
|
1995-11-28 06:31:21 +08:00
|
|
|
#include "misc.h"
|
2000-12-13 04:03:45 +08:00
|
|
|
#include "debug.h"
|
1995-11-28 06:31:21 +08:00
|
|
|
|
2002-07-05 05:08:05 +08:00
|
|
|
rpmRC rpmMkdirPath (const char * dpath, const char * dname)
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
if ((rc = Stat(dpath, &st)) < 0) {
|
|
|
|
int ut = urlPath(dpath, NULL);
|
|
|
|
switch (ut) {
|
|
|
|
case URL_IS_PATH:
|
|
|
|
case URL_IS_UNKNOWN:
|
|
|
|
if (errno != ENOENT)
|
|
|
|
break;
|
|
|
|
/*@fallthrough@*/
|
2004-11-04 21:29:11 +08:00
|
|
|
case URL_IS_HTTPS:
|
2002-07-05 05:08:05 +08:00
|
|
|
case URL_IS_HTTP:
|
2004-11-04 21:29:11 +08:00
|
|
|
case URL_IS_FTP:
|
2002-07-05 05:08:05 +08:00
|
|
|
rc = Mkdir(dpath, 0755);
|
|
|
|
break;
|
|
|
|
case URL_IS_DASH:
|
2004-11-29 05:14:41 +08:00
|
|
|
case URL_IS_HKP:
|
2002-07-05 05:08:05 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (rc < 0) {
|
|
|
|
rpmError(RPMERR_CREATE, _("cannot create %%%s %s\n"), dname, dpath);
|
|
|
|
return RPMRC_FAIL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ((rc = Access(dpath, W_OK))) {
|
|
|
|
rpmError(RPMERR_CREATE, _("cannot write to %%%s %s\n"), dname, dpath);
|
|
|
|
return RPMRC_FAIL;
|
|
|
|
}
|
|
|
|
return RPMRC_OK;
|
|
|
|
}
|
|
|
|
|
2002-06-20 02:52:46 +08:00
|
|
|
/*@-bounds@*/
|
1999-11-27 05:58:42 +08:00
|
|
|
char ** splitString(const char * str, int length, char sep)
|
|
|
|
{
|
1999-01-07 01:33:50 +08:00
|
|
|
const char * source;
|
|
|
|
char * s, * dest;
|
1995-11-28 06:31:21 +08:00
|
|
|
char ** list;
|
|
|
|
int i;
|
|
|
|
int fields;
|
1999-11-27 05:58:42 +08:00
|
|
|
|
1999-09-21 11:22:53 +08:00
|
|
|
s = xmalloc(length + 1);
|
1999-11-27 05:58:42 +08:00
|
|
|
|
1995-11-28 06:31:21 +08:00
|
|
|
fields = 1;
|
|
|
|
for (source = str, dest = s, i = 0; i < length; i++, source++, dest++) {
|
|
|
|
*dest = *source;
|
|
|
|
if (*dest == sep) fields++;
|
|
|
|
}
|
|
|
|
|
|
|
|
*dest = '\0';
|
|
|
|
|
2001-10-16 22:58:57 +08:00
|
|
|
list = xmalloc(sizeof(*list) * (fields + 1));
|
1995-11-28 06:31:21 +08:00
|
|
|
|
|
|
|
dest = s;
|
|
|
|
list[0] = dest;
|
|
|
|
i = 1;
|
|
|
|
while (i < fields) {
|
|
|
|
if (*dest == sep) {
|
|
|
|
list[i++] = dest + 1;
|
|
|
|
*dest = 0;
|
|
|
|
}
|
|
|
|
dest++;
|
|
|
|
}
|
|
|
|
|
|
|
|
list[i] = NULL;
|
|
|
|
|
2003-04-03 05:16:26 +08:00
|
|
|
/*@-nullret@*/ /* FIX: list[i] is NULL */
|
1995-11-28 06:31:21 +08:00
|
|
|
return list;
|
2003-04-03 05:16:26 +08:00
|
|
|
/*@=nullret@*/
|
1995-11-28 06:31:21 +08:00
|
|
|
}
|
2002-06-20 02:52:46 +08:00
|
|
|
/*@=bounds@*/
|
1995-11-28 06:31:21 +08:00
|
|
|
|
1999-11-27 05:58:42 +08:00
|
|
|
void freeSplitString(char ** list)
|
|
|
|
{
|
2001-04-30 06:43:01 +08:00
|
|
|
/*@-unqualifiedtrans@*/
|
2001-03-24 04:27:48 +08:00
|
|
|
list[0] = _free(list[0]);
|
2001-04-30 06:43:01 +08:00
|
|
|
/*@=unqualifiedtrans@*/
|
2001-03-24 04:27:48 +08:00
|
|
|
list = _free(list);
|
1995-11-28 06:31:21 +08:00
|
|
|
}
|
1995-12-14 04:11:31 +08:00
|
|
|
|
1999-11-27 05:58:42 +08:00
|
|
|
int doputenv(const char *str)
|
|
|
|
{
|
1996-12-24 22:01:56 +08:00
|
|
|
char * a;
|
1999-11-27 05:58:42 +08:00
|
|
|
|
1996-12-24 22:01:56 +08:00
|
|
|
/* FIXME: this leaks memory! */
|
1999-09-21 11:22:53 +08:00
|
|
|
a = xmalloc(strlen(str) + 1);
|
1996-12-24 22:01:56 +08:00
|
|
|
strcpy(a, str);
|
|
|
|
return putenv(a);
|
|
|
|
}
|
|
|
|
|
2001-06-18 06:18:03 +08:00
|
|
|
int dosetenv(const char * name, const char * value, int overwrite)
|
1999-11-27 05:58:42 +08:00
|
|
|
{
|
1996-12-24 22:01:56 +08:00
|
|
|
char * a;
|
|
|
|
|
|
|
|
if (!overwrite && getenv(name)) return 0;
|
|
|
|
|
2001-06-18 06:18:03 +08:00
|
|
|
/* FIXME: this leaks memory! */
|
|
|
|
a = xmalloc(strlen(name) + strlen(value) + sizeof("="));
|
|
|
|
(void) stpcpy( stpcpy( stpcpy( a, name), "="), value);
|
1996-12-24 22:01:56 +08:00
|
|
|
return putenv(a);
|
|
|
|
}
|
1997-02-14 10:05:13 +08:00
|
|
|
|
1999-11-27 05:58:42 +08:00
|
|
|
int makeTempFile(const char * prefix, const char ** fnptr, FD_t * fdptr)
|
|
|
|
{
|
2000-12-20 01:43:56 +08:00
|
|
|
const char * tpmacro = "%{?_tmppath:%{_tmppath}}%{!?_tmppath:/var/tmp}";
|
1999-11-20 02:19:41 +08:00
|
|
|
const char * tempfn = NULL;
|
|
|
|
const char * tfn = NULL;
|
2000-12-20 01:43:56 +08:00
|
|
|
static int _initialized = 0;
|
1999-11-19 02:07:46 +08:00
|
|
|
int temput;
|
1999-11-27 05:58:42 +08:00
|
|
|
FD_t fd = NULL;
|
1997-08-29 03:13:54 +08:00
|
|
|
int ran;
|
|
|
|
|
2001-10-16 01:53:34 +08:00
|
|
|
/*@-branchstate@*/
|
1997-08-29 03:13:54 +08:00
|
|
|
if (!prefix) prefix = "";
|
2001-10-16 01:53:34 +08:00
|
|
|
/*@=branchstate@*/
|
1997-08-29 03:13:54 +08:00
|
|
|
|
2000-12-20 01:43:56 +08:00
|
|
|
/* Create the temp directory if it doesn't already exist. */
|
2001-10-16 01:53:34 +08:00
|
|
|
/*@-branchstate@*/
|
2000-12-20 01:43:56 +08:00
|
|
|
if (!_initialized) {
|
|
|
|
_initialized = 1;
|
|
|
|
tempfn = rpmGenPath(prefix, tpmacro, NULL);
|
2002-08-14 04:40:32 +08:00
|
|
|
if (rpmioMkpath(tempfn, 0755, (uid_t) -1, (gid_t) -1))
|
2000-12-20 01:43:56 +08:00
|
|
|
goto errxit;
|
|
|
|
}
|
2001-10-16 01:53:34 +08:00
|
|
|
/*@=branchstate@*/
|
2000-12-20 01:43:56 +08:00
|
|
|
|
|
|
|
/* XXX should probably use mkstemp here */
|
1997-11-01 01:31:17 +08:00
|
|
|
srand(time(NULL));
|
|
|
|
ran = rand() % 100000;
|
1998-09-15 02:55:18 +08:00
|
|
|
|
1999-11-27 05:58:42 +08:00
|
|
|
/* maybe this should use link/stat? */
|
1998-09-15 02:55:18 +08:00
|
|
|
|
1997-08-29 03:13:54 +08:00
|
|
|
do {
|
1999-11-11 06:09:49 +08:00
|
|
|
char tfnbuf[64];
|
|
|
|
#ifndef NOTYET
|
|
|
|
sprintf(tfnbuf, "rpm-tmp.%d", ran++);
|
2001-03-24 04:27:48 +08:00
|
|
|
tempfn = _free(tempfn);
|
2000-12-20 01:43:56 +08:00
|
|
|
tempfn = rpmGenPath(prefix, tpmacro, tfnbuf);
|
1999-11-11 06:09:49 +08:00
|
|
|
#else
|
|
|
|
strcpy(tfnbuf, "rpm-tmp.XXXXXX");
|
2001-03-24 04:27:48 +08:00
|
|
|
tempfn = _free(tempfn);
|
2000-12-20 01:43:56 +08:00
|
|
|
tempfn = rpmGenPath(prefix, tpmacro, mktemp(tfnbuf));
|
1999-11-11 06:09:49 +08:00
|
|
|
#endif
|
|
|
|
|
1999-11-19 02:07:46 +08:00
|
|
|
temput = urlPath(tempfn, &tfn);
|
1999-11-20 02:19:41 +08:00
|
|
|
if (*tfn == '\0') goto errxit;
|
|
|
|
|
1999-11-19 02:07:46 +08:00
|
|
|
switch (temput) {
|
1999-11-11 06:09:49 +08:00
|
|
|
case URL_IS_DASH:
|
2004-11-29 05:14:41 +08:00
|
|
|
case URL_IS_HKP:
|
1999-11-19 02:07:46 +08:00
|
|
|
goto errxit;
|
2001-10-14 06:01:38 +08:00
|
|
|
/*@notreached@*/ /*@switchbreak@*/ break;
|
2004-11-04 21:29:11 +08:00
|
|
|
case URL_IS_HTTPS:
|
|
|
|
case URL_IS_HTTP:
|
|
|
|
case URL_IS_FTP:
|
1999-11-01 05:38:21 +08:00
|
|
|
default:
|
2001-10-14 06:01:38 +08:00
|
|
|
/*@switchbreak@*/ break;
|
1999-11-01 05:38:21 +08:00
|
|
|
}
|
1999-11-13 01:20:49 +08:00
|
|
|
|
1999-11-20 02:19:41 +08:00
|
|
|
fd = Fopen(tempfn, "w+x.ufdio");
|
1999-11-27 05:58:42 +08:00
|
|
|
/* XXX FIXME: errno may not be correct for ufdio */
|
1999-11-11 06:09:49 +08:00
|
|
|
} while ((fd == NULL || Ferror(fd)) && errno == EEXIST);
|
|
|
|
|
1999-11-24 08:03:54 +08:00
|
|
|
if (fd == NULL || Ferror(fd))
|
|
|
|
goto errxit;
|
|
|
|
|
1999-11-19 02:07:46 +08:00
|
|
|
switch(temput) {
|
|
|
|
case URL_IS_PATH:
|
|
|
|
case URL_IS_UNKNOWN:
|
2001-09-21 23:07:11 +08:00
|
|
|
{ struct stat sb, sb2;
|
1999-11-19 02:07:46 +08:00
|
|
|
if (!stat(tfn, &sb) && S_ISLNK(sb.st_mode)) {
|
2001-01-16 07:09:42 +08:00
|
|
|
rpmError(RPMERR_SCRIPT, _("error creating temporary file %s\n"), tfn);
|
1999-11-19 02:07:46 +08:00
|
|
|
goto errxit;
|
|
|
|
}
|
1998-03-04 05:45:38 +08:00
|
|
|
|
1999-11-19 02:07:46 +08:00
|
|
|
if (sb.st_nlink != 1) {
|
2001-01-16 07:09:42 +08:00
|
|
|
rpmError(RPMERR_SCRIPT, _("error creating temporary file %s\n"), tfn);
|
1999-11-19 02:07:46 +08:00
|
|
|
goto errxit;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fstat(Fileno(fd), &sb2) == 0) {
|
|
|
|
if (sb2.st_ino != sb.st_ino || sb2.st_dev != sb.st_dev) {
|
2001-01-16 07:09:42 +08:00
|
|
|
rpmError(RPMERR_SCRIPT, _("error creating temporary file %s\n"), tfn);
|
1999-11-19 02:07:46 +08:00
|
|
|
goto errxit;
|
|
|
|
}
|
1999-11-15 03:15:18 +08:00
|
|
|
}
|
2001-09-21 23:07:11 +08:00
|
|
|
} break;
|
1999-11-19 02:07:46 +08:00
|
|
|
default:
|
|
|
|
break;
|
1998-03-04 05:45:38 +08:00
|
|
|
}
|
|
|
|
|
2001-10-16 01:53:34 +08:00
|
|
|
/*@-branchstate@*/
|
1999-01-07 01:33:50 +08:00
|
|
|
if (fnptr)
|
1999-11-20 02:19:41 +08:00
|
|
|
*fnptr = tempfn;
|
2001-03-24 04:27:48 +08:00
|
|
|
else
|
|
|
|
tempfn = _free(tempfn);
|
2001-10-16 01:53:34 +08:00
|
|
|
/*@=branchstate@*/
|
1997-08-29 03:13:54 +08:00
|
|
|
*fdptr = fd;
|
|
|
|
|
|
|
|
return 0;
|
1999-11-19 02:07:46 +08:00
|
|
|
|
|
|
|
errxit:
|
2001-03-24 04:27:48 +08:00
|
|
|
tempfn = _free(tempfn);
|
2001-04-30 06:43:01 +08:00
|
|
|
/*@-usereleased@*/
|
2003-01-24 04:23:24 +08:00
|
|
|
if (fd != NULL) (void) Fclose(fd);
|
2001-04-30 06:43:01 +08:00
|
|
|
/*@=usereleased@*/
|
1999-11-19 02:07:46 +08:00
|
|
|
return 1;
|
1997-08-29 03:13:54 +08:00
|
|
|
}
|
1999-01-08 11:10:12 +08:00
|
|
|
|
1999-11-27 05:58:42 +08:00
|
|
|
char * currentDirectory(void)
|
|
|
|
{
|
2003-02-03 19:46:52 +08:00
|
|
|
int currDirLen = 0;
|
|
|
|
char * currDir = NULL;
|
1999-01-08 11:10:12 +08:00
|
|
|
|
2003-02-03 19:46:52 +08:00
|
|
|
do {
|
|
|
|
currDirLen += 128;
|
1999-09-21 11:22:53 +08:00
|
|
|
currDir = xrealloc(currDir, currDirLen);
|
2003-02-03 19:46:52 +08:00
|
|
|
memset(currDir, 0, currDirLen);
|
|
|
|
} while (getcwd(currDir, currDirLen) == NULL && errno == ERANGE);
|
1999-01-08 11:10:12 +08:00
|
|
|
|
|
|
|
return currDir;
|
|
|
|
}
|
1999-10-06 23:51:14 +08:00
|
|
|
|
2000-02-24 03:20:20 +08:00
|
|
|
/*
|
|
|
|
* XXX This is a "dressed" entry to headerGetEntry to do:
|
|
|
|
* 1) DIRNAME/BASENAME/DIRINDICES -> FILENAMES tag conversions.
|
|
|
|
* 2) i18n lookaside (if enabled).
|
|
|
|
*/
|
|
|
|
int rpmHeaderGetEntry(Header h, int_32 tag, int_32 *type,
|
|
|
|
void **p, int_32 *c)
|
|
|
|
{
|
|
|
|
switch (tag) {
|
|
|
|
case RPMTAG_OLDFILENAMES:
|
|
|
|
{ const char ** fl = NULL;
|
|
|
|
int count;
|
2002-12-19 06:54:00 +08:00
|
|
|
rpmfiBuildFNames(h, RPMTAG_BASENAMES, &fl, &count);
|
2000-02-24 03:20:20 +08:00
|
|
|
if (count > 0) {
|
|
|
|
*p = fl;
|
|
|
|
if (c) *c = count;
|
2000-03-23 23:49:50 +08:00
|
|
|
if (type) *type = RPM_STRING_ARRAY_TYPE;
|
2000-02-24 03:20:20 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (c) *c = 0;
|
|
|
|
return 0;
|
|
|
|
} /*@notreached@*/ break;
|
|
|
|
|
|
|
|
case RPMTAG_GROUP:
|
|
|
|
case RPMTAG_DESCRIPTION:
|
|
|
|
case RPMTAG_SUMMARY:
|
|
|
|
{ char fmt[128];
|
|
|
|
const char * msgstr;
|
|
|
|
const char * errstr;
|
|
|
|
|
|
|
|
fmt[0] = '\0';
|
|
|
|
(void) stpcpy( stpcpy( stpcpy( fmt, "%{"), tagName(tag)), "}\n");
|
|
|
|
|
|
|
|
/* XXX FIXME: memory leak. */
|
|
|
|
msgstr = headerSprintf(h, fmt, rpmTagTable, rpmHeaderFormats, &errstr);
|
|
|
|
if (msgstr) {
|
2000-02-25 06:28:31 +08:00
|
|
|
*p = (void *) msgstr;
|
2000-02-24 03:20:20 +08:00
|
|
|
if (type) *type = RPM_STRING_TYPE;
|
|
|
|
if (c) *c = 1;
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
if (c) *c = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} /*@notreached@*/ break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return headerGetEntry(h, tag, type, p, c);
|
|
|
|
/*@notreached@*/ break;
|
|
|
|
}
|
|
|
|
/*@notreached@*/
|
|
|
|
}
|