2007-11-22 22:06:11 +08:00
|
|
|
#include "system.h"
|
|
|
|
|
|
|
|
#if HAVE_GELF_H
|
|
|
|
|
|
|
|
#include <gelf.h>
|
|
|
|
|
|
|
|
#if !defined(DT_GNU_PRELINKED)
|
|
|
|
#define DT_GNU_PRELINKED 0x6ffffdf5
|
|
|
|
#endif
|
|
|
|
#if !defined(DT_GNU_LIBLIST)
|
|
|
|
#define DT_GNU_LIBLIST 0x6ffffef9
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2011-09-30 18:04:44 +08:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2010-01-05 18:35:54 +08:00
|
|
|
#include <sys/wait.h>
|
2010-01-05 21:25:31 +08:00
|
|
|
#include <errno.h>
|
2008-03-17 21:48:25 +08:00
|
|
|
#include <popt.h>
|
2010-01-05 21:33:47 +08:00
|
|
|
#include <ctype.h>
|
2013-02-15 21:20:09 +08:00
|
|
|
#include <pthread.h>
|
2008-03-17 21:48:25 +08:00
|
|
|
|
2007-12-08 20:02:32 +08:00
|
|
|
#include <rpm/rpmfileutil.h>
|
|
|
|
#include <rpm/rpmurl.h>
|
|
|
|
#include <rpm/rpmmacro.h>
|
|
|
|
#include <rpm/rpmlog.h>
|
|
|
|
#include <rpm/argv.h>
|
2007-11-22 22:06:11 +08:00
|
|
|
|
2008-01-30 23:05:29 +08:00
|
|
|
#include "rpmio/rpmio_internal.h"
|
|
|
|
|
|
|
|
#include "debug.h"
|
|
|
|
|
2008-11-23 00:06:31 +08:00
|
|
|
static const char *rpm_config_dir = NULL;
|
2013-02-15 21:20:09 +08:00
|
|
|
static pthread_once_t configDirSet = PTHREAD_ONCE_INIT;
|
2008-11-23 00:06:31 +08:00
|
|
|
|
2012-05-21 19:52:19 +08:00
|
|
|
static int is_prelinked(int fdno)
|
|
|
|
{
|
|
|
|
int prelinked = 0;
|
|
|
|
#if HAVE_GELF_H && HAVE_LIBELF
|
|
|
|
Elf *elf = NULL;
|
|
|
|
Elf_Scn *scn = NULL;
|
|
|
|
Elf_Data *data = NULL;
|
|
|
|
GElf_Ehdr ehdr;
|
|
|
|
GElf_Shdr shdr;
|
|
|
|
GElf_Dyn dyn;
|
|
|
|
|
|
|
|
(void) elf_version(EV_CURRENT);
|
|
|
|
|
|
|
|
if ((elf = elf_begin (fdno, ELF_C_READ, NULL)) == NULL ||
|
|
|
|
elf_kind(elf) != ELF_K_ELF || gelf_getehdr(elf, &ehdr) == NULL ||
|
|
|
|
!(ehdr.e_type == ET_DYN || ehdr.e_type == ET_EXEC))
|
|
|
|
goto exit;
|
|
|
|
|
|
|
|
while (!prelinked && (scn = elf_nextscn(elf, scn)) != NULL) {
|
|
|
|
(void) gelf_getshdr(scn, &shdr);
|
2018-03-27 20:15:51 +08:00
|
|
|
if (shdr.sh_type != SHT_DYNAMIC || shdr.sh_entsize == 0)
|
2012-05-21 19:52:19 +08:00
|
|
|
continue;
|
|
|
|
while (!prelinked && (data = elf_getdata (scn, data)) != NULL) {
|
|
|
|
int maxndx = data->d_size / shdr.sh_entsize;
|
|
|
|
|
|
|
|
for (int ndx = 0; ndx < maxndx; ++ndx) {
|
|
|
|
(void) gelf_getdyn (data, ndx, &dyn);
|
|
|
|
if (!(dyn.d_tag == DT_GNU_PRELINKED || dyn.d_tag == DT_GNU_LIBLIST))
|
|
|
|
continue;
|
|
|
|
prelinked = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
|
|
|
if (elf) (void) elf_end(elf);
|
|
|
|
#endif
|
|
|
|
return prelinked;
|
|
|
|
}
|
|
|
|
|
2008-06-11 14:25:02 +08:00
|
|
|
static int open_dso(const char * path, pid_t * pidp, rpm_loff_t *fsizep)
|
2007-11-22 22:06:11 +08:00
|
|
|
{
|
|
|
|
static const char * cmd = NULL;
|
|
|
|
static int initted = 0;
|
|
|
|
int fdno;
|
|
|
|
|
|
|
|
if (!initted) {
|
|
|
|
cmd = rpmExpand("%{?__prelink_undo_cmd}", NULL);
|
|
|
|
initted++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pidp) *pidp = 0;
|
|
|
|
|
|
|
|
if (fsizep) {
|
|
|
|
struct stat sb, * st = &sb;
|
|
|
|
if (stat(path, st) < 0)
|
|
|
|
return -1;
|
|
|
|
*fsizep = st->st_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
fdno = open(path, O_RDONLY);
|
|
|
|
if (fdno < 0)
|
|
|
|
return fdno;
|
|
|
|
|
|
|
|
if (!(cmd && *cmd))
|
|
|
|
return fdno;
|
|
|
|
|
2012-05-21 19:52:19 +08:00
|
|
|
if (pidp != NULL && is_prelinked(fdno)) {
|
2007-11-22 22:06:11 +08:00
|
|
|
int pipes[2];
|
|
|
|
pid_t pid;
|
|
|
|
|
2013-04-12 14:19:35 +08:00
|
|
|
close(fdno);
|
2007-11-22 22:06:11 +08:00
|
|
|
pipes[0] = pipes[1] = -1;
|
2013-04-12 14:19:35 +08:00
|
|
|
if (pipe(pipes) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
pid = fork();
|
|
|
|
if (pid < 0) {
|
|
|
|
close(pipes[0]);
|
|
|
|
close(pipes[1]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pid == 0) {
|
2007-11-22 22:06:11 +08:00
|
|
|
ARGV_t av, lib;
|
2013-04-12 14:19:35 +08:00
|
|
|
int dfd;
|
2007-11-22 22:06:11 +08:00
|
|
|
argvSplit(&av, cmd, " ");
|
|
|
|
|
2013-04-12 14:19:35 +08:00
|
|
|
close(pipes[0]);
|
|
|
|
dfd = dup2(pipes[1], STDOUT_FILENO);
|
|
|
|
close(pipes[1]);
|
|
|
|
if (dfd >= 0 && (lib = argvSearch(av, "library", NULL)) != NULL) {
|
2008-04-07 17:11:16 +08:00
|
|
|
*lib = (char *) path;
|
2007-11-22 22:06:11 +08:00
|
|
|
unsetenv("MALLOC_CHECK_");
|
2013-04-12 14:19:35 +08:00
|
|
|
execve(av[0], av+1, environ);
|
2007-11-22 22:06:11 +08:00
|
|
|
}
|
2013-04-12 14:19:35 +08:00
|
|
|
_exit(127); /* not normally reached */
|
|
|
|
} else {
|
|
|
|
*pidp = pid;
|
|
|
|
fdno = pipes[0];
|
|
|
|
close(pipes[1]);
|
2007-11-22 22:06:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fdno;
|
|
|
|
}
|
|
|
|
|
2010-10-22 17:22:44 +08:00
|
|
|
int rpmDoDigest(int algo, const char * fn,int asAscii,
|
2008-06-11 14:25:02 +08:00
|
|
|
unsigned char * digest, rpm_loff_t * fsizep)
|
2007-11-22 22:06:11 +08:00
|
|
|
{
|
|
|
|
const char * path;
|
|
|
|
urltype ut = urlPath(fn, &path);
|
2007-12-01 18:32:30 +08:00
|
|
|
unsigned char * dig = NULL;
|
2014-06-11 18:11:48 +08:00
|
|
|
size_t diglen, buflen = 32 * BUFSIZ;
|
|
|
|
unsigned char *buf = xmalloc(buflen);
|
2007-11-22 22:06:11 +08:00
|
|
|
FD_t fd;
|
2008-06-11 14:25:02 +08:00
|
|
|
rpm_loff_t fsize = 0;
|
2007-11-22 22:06:11 +08:00
|
|
|
pid_t pid = 0;
|
|
|
|
int rc = 0;
|
|
|
|
int fdno;
|
|
|
|
|
|
|
|
fdno = open_dso(path, &pid, &fsize);
|
|
|
|
if (fdno < 0) {
|
|
|
|
rc = 1;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2017-02-27 23:37:18 +08:00
|
|
|
switch (ut) {
|
2007-11-22 22:06:11 +08:00
|
|
|
case URL_IS_PATH:
|
|
|
|
case URL_IS_UNKNOWN:
|
|
|
|
case URL_IS_HTTPS:
|
|
|
|
case URL_IS_HTTP:
|
|
|
|
case URL_IS_FTP:
|
|
|
|
case URL_IS_HKP:
|
|
|
|
case URL_IS_DASH:
|
|
|
|
default:
|
|
|
|
/* Either use the pipe to prelink -y or open the URL. */
|
|
|
|
fd = (pid != 0) ? fdDup(fdno) : Fopen(fn, "r.ufdio");
|
|
|
|
(void) close(fdno);
|
|
|
|
if (fd == NULL || Ferror(fd)) {
|
|
|
|
rc = 1;
|
|
|
|
if (fd != NULL)
|
|
|
|
(void) Fclose(fd);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2007-12-01 18:32:30 +08:00
|
|
|
fdInitDigest(fd, algo, 0);
|
2007-11-22 22:06:11 +08:00
|
|
|
fsize = 0;
|
2014-06-11 18:11:48 +08:00
|
|
|
while ((rc = Fread(buf, sizeof(*buf), buflen, fd)) > 0)
|
2007-11-22 22:06:11 +08:00
|
|
|
fsize += rc;
|
2007-12-01 18:32:30 +08:00
|
|
|
fdFiniDigest(fd, algo, (void **)&dig, &diglen, asAscii);
|
2013-02-12 11:11:16 +08:00
|
|
|
if (dig == NULL || Ferror(fd))
|
2007-11-22 22:06:11 +08:00
|
|
|
rc = 1;
|
|
|
|
|
|
|
|
(void) Fclose(fd);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Reap the prelink -y helper. */
|
|
|
|
if (pid) {
|
|
|
|
int status;
|
|
|
|
(void) waitpid(pid, &status, 0);
|
|
|
|
if (!WIFEXITED(status) || WEXITSTATUS(status))
|
|
|
|
rc = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
|
|
|
if (fsizep)
|
|
|
|
*fsizep = fsize;
|
|
|
|
if (!rc)
|
2007-12-01 18:32:30 +08:00
|
|
|
memcpy(digest, dig, diglen);
|
|
|
|
dig = _free(dig);
|
2014-06-11 18:11:48 +08:00
|
|
|
free(buf);
|
2007-11-22 22:06:11 +08:00
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2008-12-06 03:17:16 +08:00
|
|
|
FD_t rpmMkTemp(char *templ)
|
2008-05-03 17:34:19 +08:00
|
|
|
{
|
2011-09-30 18:04:44 +08:00
|
|
|
mode_t mode;
|
2008-05-03 17:34:19 +08:00
|
|
|
int sfd;
|
|
|
|
FD_t tfd = NULL;
|
|
|
|
|
2011-09-30 18:04:44 +08:00
|
|
|
mode = umask(0077);
|
2008-12-06 03:17:16 +08:00
|
|
|
sfd = mkstemp(templ);
|
2011-09-30 18:04:44 +08:00
|
|
|
umask(mode);
|
|
|
|
|
2008-05-03 17:34:19 +08:00
|
|
|
if (sfd < 0) {
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
tfd = fdDup(sfd);
|
|
|
|
close(sfd);
|
|
|
|
|
|
|
|
exit:
|
|
|
|
return tfd;
|
|
|
|
}
|
|
|
|
|
|
|
|
FD_t rpmMkTempFile(const char * prefix, char **fn)
|
2007-11-22 22:28:30 +08:00
|
|
|
{
|
2008-04-11 14:05:05 +08:00
|
|
|
const char *tpmacro = "%{_tmppath}"; /* always set from rpmrc */
|
|
|
|
char *tempfn;
|
2007-11-22 22:28:30 +08:00
|
|
|
static int _initialized = 0;
|
2008-04-11 14:05:05 +08:00
|
|
|
FD_t tfd = NULL;
|
2007-11-22 22:28:30 +08:00
|
|
|
|
|
|
|
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))
|
2008-04-11 14:05:05 +08:00
|
|
|
goto exit;
|
|
|
|
free(tempfn);
|
2007-11-22 22:28:30 +08:00
|
|
|
}
|
|
|
|
|
2008-04-11 14:05:05 +08:00
|
|
|
tempfn = rpmGetPath(prefix, tpmacro, "/rpm-tmp.XXXXXX", NULL);
|
2008-05-03 17:34:19 +08:00
|
|
|
tfd = rpmMkTemp(tempfn);
|
2007-11-22 22:28:30 +08:00
|
|
|
|
2008-04-11 14:05:05 +08:00
|
|
|
if (tfd == NULL || Ferror(tfd)) {
|
2008-05-03 17:34:19 +08:00
|
|
|
rpmlog(RPMLOG_ERR, _("error creating temporary file %s: %m\n"), tempfn);
|
2008-04-11 14:05:05 +08:00
|
|
|
goto exit;
|
|
|
|
}
|
2007-11-22 22:28:30 +08:00
|
|
|
|
2008-04-11 14:05:05 +08:00
|
|
|
exit:
|
|
|
|
if (tfd != NULL && fn)
|
|
|
|
*fn = tempfn;
|
|
|
|
else
|
|
|
|
free(tempfn);
|
2007-11-22 22:28:30 +08:00
|
|
|
|
2008-04-11 14:05:05 +08:00
|
|
|
return tfd;
|
2007-11-22 22:28:30 +08:00
|
|
|
}
|
|
|
|
|
2007-11-23 15:58:02 +08:00
|
|
|
int rpmioMkpath(const char * path, mode_t mode, uid_t uid, gid_t gid)
|
|
|
|
{
|
2008-04-28 23:30:25 +08:00
|
|
|
char *d, *de;
|
2007-11-23 15:58:02 +08:00
|
|
|
int rc;
|
|
|
|
|
2009-01-09 21:05:40 +08:00
|
|
|
if (path == NULL || *path == '\0')
|
2007-11-23 15:58:02 +08:00
|
|
|
return -1;
|
2008-04-28 23:30:25 +08:00
|
|
|
d = rstrcat(NULL, path);
|
|
|
|
if (d[strlen(d)-1] != '/') {
|
|
|
|
rstrcat(&d,"/");
|
|
|
|
}
|
|
|
|
de = d;
|
|
|
|
for (;(de=strchr(de+1,'/'));) {
|
2007-11-23 15:58:02 +08:00
|
|
|
struct stat st;
|
2008-04-28 23:30:25 +08:00
|
|
|
*de = '\0';
|
2007-12-03 22:33:18 +08:00
|
|
|
rc = stat(d, &st);
|
2007-11-23 15:58:02 +08:00
|
|
|
if (rc) {
|
2008-04-28 23:30:25 +08:00
|
|
|
if (errno != ENOENT)
|
|
|
|
goto exit;
|
2007-12-03 22:33:18 +08:00
|
|
|
rc = mkdir(d, mode);
|
2007-11-23 15:58:02 +08:00
|
|
|
if (rc)
|
2008-04-28 23:30:25 +08:00
|
|
|
goto exit;
|
|
|
|
rpmlog(RPMLOG_DEBUG, "created directory(s) %s mode 0%o\n", path, mode);
|
2007-11-23 15:58:02 +08:00
|
|
|
if (!(uid == (uid_t) -1 && gid == (gid_t) -1)) {
|
|
|
|
rc = chown(d, uid, gid);
|
|
|
|
if (rc)
|
2008-04-28 23:30:25 +08:00
|
|
|
goto exit;
|
2007-11-23 15:58:02 +08:00
|
|
|
}
|
|
|
|
} else if (!S_ISDIR(st.st_mode)) {
|
2008-04-28 23:30:25 +08:00
|
|
|
rc = ENOTDIR;
|
|
|
|
goto exit;
|
2007-11-23 15:58:02 +08:00
|
|
|
}
|
2008-04-28 23:30:25 +08:00
|
|
|
*de = '/';
|
2007-11-23 15:58:02 +08:00
|
|
|
}
|
|
|
|
rc = 0;
|
2008-04-28 23:30:25 +08:00
|
|
|
exit:
|
|
|
|
free(d);
|
2007-11-23 15:58:02 +08:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2008-01-27 22:39:40 +08:00
|
|
|
int rpmFileIsCompressed(const char * file, rpmCompressedMagic * compressed)
|
2007-11-23 16:05:49 +08:00
|
|
|
{
|
|
|
|
FD_t fd;
|
|
|
|
ssize_t nb;
|
|
|
|
int rc = -1;
|
|
|
|
unsigned char magic[13];
|
|
|
|
|
|
|
|
*compressed = COMPRESSED_NOT;
|
|
|
|
|
|
|
|
fd = Fopen(file, "r.ufdio");
|
|
|
|
if (fd == NULL || Ferror(fd)) {
|
|
|
|
/* XXX Fstrerror */
|
|
|
|
rpmlog(RPMLOG_ERR, _("File %s: %s\n"), file, Fstrerror(fd));
|
|
|
|
if (fd) (void) Fclose(fd);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
nb = Fread(magic, sizeof(magic[0]), sizeof(magic), fd);
|
|
|
|
if (nb < 0) {
|
|
|
|
rpmlog(RPMLOG_ERR, _("File %s: %s\n"), file, Fstrerror(fd));
|
|
|
|
rc = 1;
|
|
|
|
} else if (nb < sizeof(magic)) {
|
|
|
|
rpmlog(RPMLOG_ERR, _("File %s is smaller than %u bytes\n"),
|
|
|
|
file, (unsigned)sizeof(magic));
|
|
|
|
rc = 0;
|
|
|
|
}
|
|
|
|
(void) Fclose(fd);
|
|
|
|
if (rc >= 0)
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
rc = 0;
|
|
|
|
|
2015-02-18 00:30:00 +08:00
|
|
|
if ((magic[0] == 'B') && (magic[1] == 'Z') &&
|
|
|
|
(magic[2] == 'h')) {
|
2007-11-23 16:05:49 +08:00
|
|
|
*compressed = COMPRESSED_BZIP2;
|
2011-04-26 12:33:19 +08:00
|
|
|
} else if ((magic[0] == 'P') && (magic[1] == 'K') &&
|
|
|
|
(((magic[2] == 3) && (magic[3] == 4)) ||
|
|
|
|
((magic[2] == '0') && (magic[3] == '0')))) { /* pkzip */
|
2007-11-23 16:05:49 +08:00
|
|
|
*compressed = COMPRESSED_ZIP;
|
2008-11-25 21:11:08 +08:00
|
|
|
} else if ((magic[0] == 0xfd) && (magic[1] == 0x37) &&
|
|
|
|
(magic[2] == 0x7a) && (magic[3] == 0x58) &&
|
|
|
|
(magic[4] == 0x5a) && (magic[5] == 0x00)) {
|
2009-03-18 15:42:23 +08:00
|
|
|
/* new style xz (lzma) with magic */
|
|
|
|
*compressed = COMPRESSED_XZ;
|
2017-08-09 22:42:56 +08:00
|
|
|
} else if ((magic[0] == 0x28) && (magic[1] == 0x85) &&
|
|
|
|
(magic[2] == 0x2f) ) {
|
|
|
|
*compressed = COMPRESSED_ZSTD;
|
2011-04-24 20:56:11 +08:00
|
|
|
} else if ((magic[0] == 'L') && (magic[1] == 'Z') &&
|
|
|
|
(magic[2] == 'I') && (magic[3] == 'P')) {
|
|
|
|
*compressed = COMPRESSED_LZIP;
|
2011-04-24 20:57:23 +08:00
|
|
|
} else if ((magic[0] == 'L') && (magic[1] == 'R') &&
|
|
|
|
(magic[2] == 'Z') && (magic[3] == 'I')) {
|
|
|
|
*compressed = COMPRESSED_LRZIP;
|
2007-11-23 16:05:49 +08:00
|
|
|
} else if (((magic[0] == 0037) && (magic[1] == 0213)) || /* gzip */
|
|
|
|
((magic[0] == 0037) && (magic[1] == 0236)) || /* old gzip */
|
|
|
|
((magic[0] == 0037) && (magic[1] == 0036)) || /* pack */
|
|
|
|
((magic[0] == 0037) && (magic[1] == 0240)) || /* SCO lzh */
|
|
|
|
((magic[0] == 0037) && (magic[1] == 0235)) /* compress */
|
|
|
|
) {
|
|
|
|
*compressed = COMPRESSED_OTHER;
|
2012-05-11 05:27:56 +08:00
|
|
|
} else if ((magic[0] == '7') && (magic[1] == 'z') &&
|
|
|
|
(magic[2] == 0xbc) && (magic[3] == 0xaf) &&
|
|
|
|
(magic[4] == 0x27) && (magic[5] == 0x1c)) {
|
|
|
|
*compressed = COMPRESSED_7ZIP;
|
2008-01-27 22:11:46 +08:00
|
|
|
} else if (rpmFileHasSuffix(file, ".lzma")) {
|
|
|
|
*compressed = COMPRESSED_LZMA;
|
2015-11-05 21:29:21 +08:00
|
|
|
} else if (rpmFileHasSuffix(file, ".gem")) {
|
|
|
|
*compressed = COMPRESSED_GEM;
|
2007-11-23 16:05:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
2007-11-23 16:10:19 +08:00
|
|
|
|
|
|
|
/* @todo "../sbin/./../bin/" not correct. */
|
|
|
|
char *rpmCleanPath(char * path)
|
|
|
|
{
|
|
|
|
const char *s;
|
|
|
|
char *se, *t, *te;
|
|
|
|
int begin = 1;
|
|
|
|
|
|
|
|
if (path == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/*fprintf(stderr, "*** RCP %s ->\n", path); */
|
|
|
|
s = t = te = path;
|
|
|
|
while (*s != '\0') {
|
|
|
|
/*fprintf(stderr, "*** got \"%.*s\"\trest \"%s\"\n", (t-path), path, s); */
|
2017-02-27 23:37:18 +08:00
|
|
|
switch (*s) {
|
2007-11-23 16:10:19 +08:00
|
|
|
case ':': /* handle url's */
|
|
|
|
if (s[1] == '/' && s[2] == '/') {
|
|
|
|
*t++ = *s++;
|
|
|
|
*t++ = *s++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
begin=1;
|
|
|
|
break;
|
|
|
|
case '/':
|
|
|
|
/* Move parent dir forward */
|
|
|
|
for (se = te + 1; se < t && *se != '/'; se++)
|
|
|
|
{};
|
|
|
|
if (se < t && *se == '/') {
|
|
|
|
te = se;
|
|
|
|
/*fprintf(stderr, "*** next pdir \"%.*s\"\n", (te-path), path); */
|
|
|
|
}
|
|
|
|
while (s[1] == '/')
|
|
|
|
s++;
|
|
|
|
while (t > path && t[-1] == '/')
|
|
|
|
t--;
|
|
|
|
break;
|
|
|
|
case '.':
|
|
|
|
/* Leading .. is special */
|
|
|
|
/* Check that it is ../, so that we don't interpret */
|
|
|
|
/* ..?(i.e. "...") or ..* (i.e. "..bogus") as "..". */
|
|
|
|
/* in the case of "...", this ends up being processed*/
|
|
|
|
/* as "../.", and the last '.' is stripped. This */
|
|
|
|
/* would not be correct processing. */
|
|
|
|
if (begin && s[1] == '.' && (s[2] == '/' || s[2] == '\0')) {
|
|
|
|
/*fprintf(stderr, " leading \"..\"\n"); */
|
|
|
|
*t++ = *s++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* Single . is special */
|
|
|
|
if (begin && s[1] == '\0') {
|
|
|
|
break;
|
|
|
|
}
|
2009-03-26 00:28:24 +08:00
|
|
|
/* Handle the ./ cases */
|
|
|
|
if (t > path && t[-1] == '/') {
|
|
|
|
/* Trim embedded ./ */
|
|
|
|
if (s[1] == '/') {
|
|
|
|
s+=2;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* Trim trailing /. */
|
|
|
|
if (s[1] == '\0') {
|
|
|
|
s++;
|
|
|
|
continue;
|
|
|
|
}
|
2007-11-23 16:10:19 +08:00
|
|
|
}
|
|
|
|
/* Trim embedded /../ and trailing /.. */
|
|
|
|
if (!begin && t > path && t[-1] == '/' && s[1] == '.' && (s[2] == '/' || s[2] == '\0')) {
|
|
|
|
t = te;
|
|
|
|
/* Move parent dir forward */
|
|
|
|
if (te > path)
|
|
|
|
for (--te; te > path && *te != '/'; te--)
|
|
|
|
{};
|
|
|
|
/*fprintf(stderr, "*** prev pdir \"%.*s\"\n", (te-path), path); */
|
|
|
|
s++;
|
|
|
|
s++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
begin = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
*t++ = *s++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Trim trailing / (but leave single / alone) */
|
|
|
|
if (t > &path[1] && t[-1] == '/')
|
|
|
|
t--;
|
|
|
|
*t = '\0';
|
|
|
|
|
|
|
|
/*fprintf(stderr, "\t%s\n", path); */
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2007-11-23 16:20:19 +08:00
|
|
|
/* Merge 3 args into path, any or all of which may be a url. */
|
|
|
|
|
2007-12-15 15:39:32 +08:00
|
|
|
char * rpmGenPath(const char * urlroot, const char * urlmdir,
|
2007-11-23 16:20:19 +08:00
|
|
|
const char *urlfile)
|
|
|
|
{
|
2007-12-15 23:15:39 +08:00
|
|
|
char * xroot = rpmGetPath(urlroot, NULL);
|
|
|
|
const char * root = xroot;
|
|
|
|
char * xmdir = rpmGetPath(urlmdir, NULL);
|
|
|
|
const char * mdir = xmdir;
|
|
|
|
char * xfile = rpmGetPath(urlfile, NULL);
|
|
|
|
const char * file = xfile;
|
2007-12-15 16:39:15 +08:00
|
|
|
char * result;
|
2009-06-25 22:17:16 +08:00
|
|
|
char * url = NULL;
|
2007-11-23 16:20:19 +08:00
|
|
|
int nurl = 0;
|
|
|
|
int ut;
|
|
|
|
|
|
|
|
ut = urlPath(xroot, &root);
|
|
|
|
if (url == NULL && ut > URL_IS_DASH) {
|
|
|
|
url = xroot;
|
|
|
|
nurl = root - xroot;
|
|
|
|
}
|
|
|
|
if (root == NULL || *root == '\0') root = "/";
|
|
|
|
|
|
|
|
ut = urlPath(xmdir, &mdir);
|
|
|
|
if (url == NULL && ut > URL_IS_DASH) {
|
|
|
|
url = xmdir;
|
|
|
|
nurl = mdir - xmdir;
|
|
|
|
}
|
|
|
|
if (mdir == NULL || *mdir == '\0') mdir = "/";
|
|
|
|
|
|
|
|
ut = urlPath(xfile, &file);
|
|
|
|
if (url == NULL && ut > URL_IS_DASH) {
|
|
|
|
url = xfile;
|
|
|
|
nurl = file - xfile;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (url && nurl > 0) {
|
2008-04-29 22:08:19 +08:00
|
|
|
char *t = rstrcat(NULL, url);
|
2007-11-23 16:20:19 +08:00
|
|
|
t[nurl] = '\0';
|
|
|
|
url = t;
|
|
|
|
} else
|
2008-04-29 22:08:19 +08:00
|
|
|
url = xstrdup("");
|
2007-11-23 16:20:19 +08:00
|
|
|
|
|
|
|
result = rpmGetPath(url, root, "/", mdir, "/", file, NULL);
|
|
|
|
|
2011-05-29 00:43:52 +08:00
|
|
|
free(xroot);
|
|
|
|
free(xmdir);
|
|
|
|
free(xfile);
|
2009-06-25 22:17:16 +08:00
|
|
|
free(url);
|
2007-11-23 16:20:19 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return concatenated and expanded canonical path. */
|
|
|
|
|
2007-12-15 15:39:32 +08:00
|
|
|
char * rpmGetPath(const char *path, ...)
|
2007-11-23 16:20:19 +08:00
|
|
|
{
|
|
|
|
va_list ap;
|
2008-04-19 18:42:04 +08:00
|
|
|
char *dest = NULL, *res;
|
|
|
|
const char *s;
|
2007-11-23 16:20:19 +08:00
|
|
|
|
|
|
|
if (path == NULL)
|
|
|
|
return xstrdup("");
|
|
|
|
|
|
|
|
va_start(ap, path);
|
2008-04-19 18:42:04 +08:00
|
|
|
for (s = path; s; s = va_arg(ap, const char *)) {
|
|
|
|
rstrcat(&dest, s);
|
2007-11-23 16:20:19 +08:00
|
|
|
}
|
|
|
|
va_end(ap);
|
|
|
|
|
2008-04-19 18:42:04 +08:00
|
|
|
res = rpmExpand(dest, NULL);
|
|
|
|
free(dest);
|
|
|
|
|
|
|
|
return rpmCleanPath(res);
|
2007-11-23 16:20:19 +08:00
|
|
|
}
|
|
|
|
|
2008-01-09 16:21:01 +08:00
|
|
|
char * rpmEscapeSpaces(const char * s)
|
|
|
|
{
|
|
|
|
const char * se;
|
|
|
|
char * t;
|
|
|
|
char * te;
|
|
|
|
size_t nb = 0;
|
|
|
|
|
|
|
|
for (se = s; *se; se++) {
|
|
|
|
if (isspace(*se))
|
|
|
|
nb++;
|
|
|
|
nb++;
|
|
|
|
}
|
|
|
|
nb++;
|
|
|
|
|
|
|
|
t = te = xmalloc(nb);
|
|
|
|
for (se = s; *se; se++) {
|
|
|
|
if (isspace(*se))
|
|
|
|
*te++ = '\\';
|
|
|
|
*te++ = *se;
|
|
|
|
}
|
|
|
|
*te = '\0';
|
|
|
|
return t;
|
|
|
|
}
|
2008-01-27 22:00:06 +08:00
|
|
|
|
|
|
|
int rpmFileHasSuffix(const char *path, const char *suffix)
|
|
|
|
{
|
|
|
|
size_t plen = strlen(path);
|
|
|
|
size_t slen = strlen(suffix);
|
2009-08-31 16:15:16 +08:00
|
|
|
return (plen >= slen && rstreq(path+plen-slen, suffix));
|
2008-01-27 22:00:06 +08:00
|
|
|
}
|
2008-04-04 14:53:17 +08:00
|
|
|
|
|
|
|
char * rpmGetCwd(void)
|
|
|
|
{
|
|
|
|
int currDirLen = 0;
|
|
|
|
char * currDir = NULL;
|
|
|
|
|
|
|
|
do {
|
|
|
|
currDirLen += 128;
|
|
|
|
currDir = xrealloc(currDir, currDirLen);
|
|
|
|
memset(currDir, 0, currDirLen);
|
|
|
|
} while (getcwd(currDir, currDirLen) == NULL && errno == ERANGE);
|
|
|
|
|
|
|
|
return currDir;
|
|
|
|
}
|
|
|
|
|
2008-10-10 19:28:41 +08:00
|
|
|
int rpmMkdirs(const char *root, const char *pathstr)
|
|
|
|
{
|
|
|
|
ARGV_t dirs = NULL;
|
|
|
|
int rc = 0;
|
|
|
|
argvSplit(&dirs, pathstr, ":");
|
|
|
|
|
|
|
|
for (char **d = dirs; *d; d++) {
|
2008-11-21 15:50:53 +08:00
|
|
|
char *path = rpmGetPath(root ? root : "", *d, NULL);
|
2008-10-10 19:28:41 +08:00
|
|
|
if ((rc = rpmioMkpath(path, 0755, -1, -1)) != 0) {
|
|
|
|
const char *msg = _("failed to create directory");
|
|
|
|
/* try to be more informative if the failing part was a macro */
|
|
|
|
if (**d == '%') {
|
|
|
|
rpmlog(RPMLOG_ERR, "%s %s: %s: %m\n", msg, *d, path);
|
|
|
|
} else {
|
|
|
|
rpmlog(RPMLOG_ERR, "%s %s: %m\n", msg, path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(path);
|
|
|
|
if (rc) break;
|
|
|
|
}
|
|
|
|
argvFree(dirs);
|
|
|
|
return rc;
|
|
|
|
}
|
2008-11-23 00:06:31 +08:00
|
|
|
|
2013-02-15 21:20:09 +08:00
|
|
|
static void setConfigDir(void)
|
|
|
|
{
|
|
|
|
char *rpmenv = getenv("RPM_CONFIGDIR");
|
|
|
|
rpm_config_dir = rpmenv ? xstrdup(rpmenv) : RPMCONFIGDIR;
|
|
|
|
}
|
|
|
|
|
2008-11-23 00:06:31 +08:00
|
|
|
const char *rpmConfigDir(void)
|
|
|
|
{
|
2013-02-15 21:20:09 +08:00
|
|
|
pthread_once(&configDirSet, setConfigDir);
|
2008-11-23 00:06:31 +08:00
|
|
|
return rpm_config_dir;
|
|
|
|
}
|