8 #if defined(HAVE_MACHINE_TYPES_H)
9 # include <machine/types.h>
12 #if defined(HAVE_SYS_SOCKET_H)
13 # include <sys/socket.h>
17 #define NI_MAXHOST 1025
20 #if defined(__LCLINT__)
28 struct sockaddr *ai_addr;
30 struct addrinfo *ai_next;
34 extern int getaddrinfo (
__const char *__restrict __name,
35 __const char *__restrict __service,
36 __const struct addrinfo *__restrict __req,
37 struct addrinfo **__restrict __pai)
40 extern int getnameinfo (
__const struct sockaddr *__restrict __sa,
41 socklen_t __salen,
char *__restrict __host,
42 socklen_t __hostlen,
char *__restrict __serv,
43 socklen_t __servlen,
unsigned int __flags)
46 extern void freeaddrinfo (
struct addrinfo *__ai)
53 #include <netinet/in.h>
54 #include <arpa/inet.h>
56 #if defined(HAVE_NETINET_IN_SYSTM_H)
57 # include <sys/types.h>
58 # include <netinet/in_systm.h>
72 #include <rpmpython.h>
76 #if defined(HAVE_LIBIO_H) && defined(_G_IO_IO_FILE_VERSION)
81 #if !defined(HAVE_HERRNO) && (defined(hpux) || defined(__hpux) || defined(__LCLINT__))
90 #define IPPORT_HTTP 80
93 #if !defined(HAVE_INET_ATON)
94 #define inet_aton(cp,inp) rpm_inet_aton(cp,inp)
100 addr = inet_addr(cp);
101 if (addr == ((
long) -1))
return 0;
103 memcpy(inp, &addr,
sizeof(addr));
108 #if defined(USE_ALT_DNS) && USE_ALT_DNS
115 #define fdOpen __fdOpen
117 #define fdRead __fdRead
119 #define fdWrite __fdWrite
121 #define fdClose __fdClose
135 #define FDTO(fd) (fd ? ((FD_t)fd)->rd_timeoutsecs : -99)
136 #define FDCPIOPOS(fd) (fd ? ((FD_t)fd)->fd_cpioPos : -99)
138 #define FDONLY(fd) assert(fdGetIo(fd) == fdio)
142 #define fdGetFILE(_fd) ((FILE *)fdGetFp(_fd))
153 #define TIMEOUT_SECS 60
184 static char buf[BUFSIZ];
193 sprintf(be,
"fd %p", fd); be += strlen(be);
204 strcpy(be,
" chunked");
208 for (i = fd->
nfps; i >= 0; i--) {
215 sprintf(be,
"FD %d fp %p", fps->
fdno, fps->
fp);
217 sprintf(be,
"UFD %d fp %p", fps->
fdno, fps->
fp);
218 #if defined(WITH_ZLIB)
220 sprintf(be,
"GZD %p fdno %d", fps->
fp, fps->
fdno);
222 #if defined(WITH_BZIP2)
224 sprintf(be,
"BZD %p fdno %d", fps->
fp, fps->
fdno);
228 sprintf(be,
"LZD %p fdno %d", fps->
fp, fps->
fdno);
230 sprintf(be,
"XZD %p fdno %d", fps->
fp, fps->
fdno);
232 }
else if (fps->
io ==
fpio) {
234 sprintf(be,
"%s %p(%d) fdno %d",
235 (fps->
fdno < 0 ?
"LIBIO" :
"FP"),
236 fps->
fp, fileno(((FILE *)fps->
fp)), fps->
fdno);
239 sprintf(be,
"??? io %p fp %p fdno %d ???",
254 if ((nfdno = dup(fdno)) < 0)
256 if (fcntl(nfdno, F_SETFD, FD_CLOEXEC)) {
260 fd =
fdNew(
"open (fdDup)");
263 DBGIO(fd, (stderr,
"==> fdDup(%d) fd %p %s\n", fdno, (fd ? fd : NULL),
fdbg(fd)));
290 for (i = fd->
ndigests - 1; i >= 0; i--) {
317 if (_fdPool == NULL) {
319 (
const char * (*)(
void *))
fdbg, NULL,
fdFini);
337 memset(fd->
fps, 0,
sizeof(fd->
fps));
340 fd->
fps[0].
fp = NULL;
347 #if defined(RPM_VENDOR_MANDRIVA)
373 static ssize_t
fdRead(
void * cookie,
char * buf,
size_t count)
385 if (fd->
req != NULL) {
387 if (fd->
req != (
void *)-1)
398 if (fd->
xar != NULL) {
410 DBGIO(fd, (stderr,
"==>\tfdRead(%p,%p,%ld) rc %ld %s\n", cookie, buf, (
long)count, (
long)rc,
fdbg(fd)));
415 static ssize_t
fdWrite(
void * cookie,
const char * buf,
size_t count)
427 if (count == 0)
return 0;
433 if (fd->
req != (
void *)-1)
444 DBGIO(fd, (stderr,
"==>\tfdWrite(%p,%p,%ld) rc %ld %s\n", cookie, buf, (
long)count, (
long)rc,
fdbg(fd)));
453 #ifdef USE_COOKIE_SEEK_POINTER
454 _IO_off64_t p = *pos;
463 rc = lseek(
fdFileno(fd), p, whence);
466 DBGIO(fd, (stderr,
"==>\tfdSeek(%p,%ld,%d) rc %lx %s\n", cookie, (
long)p, whence, (
unsigned long)rc,
fdbg(fd)));
479 if (cookie == NULL)
return -2;
494 rc = ((fdno >= 0) ? close(fdno) : -2);
497 DBGIO(fd, (stderr,
"==>\tfdClose(%p) rc %lx %s\n", (fd ? fd : NULL), (
unsigned long)rc,
fdbg(fd)));
499 fd =
fdFree(fd,
"open (fdClose)");
510 fdno = open(path, flags, mode);
511 if (fdno < 0)
return NULL;
512 if (fcntl(fdno, F_SETFD, FD_CLOEXEC)) {
516 fd =
fdNew(
"open (fdOpen)");
521 DBGIO(fd, (stderr,
"==>\tfdOpen(\"%s\",%x,0%o) %s\n", path, (
unsigned)flags, (
unsigned)mode,
fdbg(fd)));
526 FILE *fdFdopen(
void * cookie,
const char *fmode)
532 if (fmode == NULL)
return NULL;
534 if (fdno < 0)
return NULL;
535 fp = fdopen(fdno, fmode);
536 DBGIO(fd, (stderr,
"==> fdFdopen(%p,\"%s\") fdno %d -> fp %p fdno %d\n", cookie, fmode, fdno, fp, fileno(fp)));
537 fd =
fdFree(fd,
"open (fdFdopen)");
554 #if defined(HAVE_POLL_H)
555 int msecs = (secs >= 0 ? (1000 * secs) : -1);
558 struct timeval timeout, *tvp = (secs >= 0 ? &timeout : NULL);
565 return (fd->
req == (
void *)-1 ? -1 : 1);
571 #if defined(HAVE_POLL_H)
573 wrfds.events = POLLOUT;
575 rc = poll(&wrfds, 1, msecs);
581 FD_SET(fdno, &wrfds);
583 rc = select(fdno + 1, NULL, &wrfds, NULL, tvp);
589 fprintf(stderr,
"*** fdWritable fdno %d rc %d %s\n", fdno, rc, strerror(
errno));
609 #if defined(HAVE_POLL_H)
610 int msecs = (secs >= 0 ? (1000 * secs) : -1);
613 struct timeval timeout, *tvp = (secs >= 0 ? &timeout : NULL);
620 return (fd->
req == (
void *)-1 ? -1 : 1);
626 #if defined(HAVE_POLL_H)
628 rdfds.events = POLLIN;
630 rc = poll(&rdfds, 1, msecs);
636 FD_SET(fdno, &rdfds);
638 rc = select(fdno + 1, &rdfds, NULL, NULL, tvp);
663 char lastchar =
'\0';
689 rc =
fdRead(fd, buf + nb, 1);
691 rc = (int)read(
fdFileno(fd), buf + nb, 1);
703 fprintf(stderr,
"*** read: fd %p rc %d errno %d %s \"%s\"\n", fd, rc,
errno, strerror(
errno), buf);
706 }
else if (rc == 0) {
708 fprintf(stderr,
"*** read: fd %p rc %d EOF errno %d %s \"%s\"\n", fd, rc,
errno, strerror(
errno), buf);
713 lastchar = buf[nb - 1];
715 }
while (ec == 0 && nb < len && lastchar !=
'\n');
717 return (ec >= 0 ? (
int)nb : ec);
725 switch (errorNumber) {
731 return (
"NE_ERROR: Generic error.");
733 return (
"NE_LOOKUP: Hostname lookup failed.");
735 return (
"NE_AUTH: Server authentication failed.");
737 return (
"NE_PROXYAUTH: Proxy authentication failed.");
739 return (
"NE_CONNECT: Could not connect to server.");
741 return (
"NE_TIMEOUT: Connection timed out.");
743 return (
"NE_FAILED: The precondition failed.");
745 return (
"NE_RETRY: Retry request.");
747 return (
"NE_REDIRECT: Redirect received.");
750 return _(
"Bad server response");
752 return _(
"Server I/O error");
754 return _(
"Server timeout");
756 return _(
"Unable to lookup server host address");
758 return _(
"Unable to lookup server host name");
760 return _(
"Failed to connect to server");
762 return _(
"Failed to establish data connection to server");
764 return _(
"I/O error to local file");
766 return _(
"Error setting remote server to passive mode");
768 return _(
"File not found on server");
770 return _(
"Abort in progress");
774 return _(
"Unknown or unexpected error");
791 retstr =
_(
"Malformed URL");
794 retstr = strerror(
errno);
800 #if !defined(HAVE_GETADDRINFO)
801 #if !defined(USE_ALT_DNS) || !USE_ALT_DNS
803 struct in_addr * address)
807 struct hostent * hostinfo;
810 hostinfo = gethostbyname(host);
812 if (!hostinfo)
return 1;
814 memcpy(address, hostinfo->h_addr_list[0],
sizeof(*address));
825 if (!strcmp(host,
"localhost")) {
855 #ifdef HAVE_GETADDRINFO
857 struct addrinfo hints, *res, *res0;
858 char pbuf[NI_MAXSERV];
861 memset(&hints, 0,
sizeof(hints));
862 hints.ai_family = AF_UNSPEC;
863 hints.ai_socktype = SOCK_STREAM;
864 sprintf(pbuf,
"%d", port);
865 pbuf[
sizeof(pbuf)-1] =
'\0';
867 if (getaddrinfo(host, pbuf, &hints, &res0) == 0) {
868 for (res = res0; res != NULL; res = res->ai_next) {
869 if ((fdno = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) < 0)
871 if (connect(fdno, res->ai_addr, (
int)res->ai_addrlen) < 0) {
880 xx = getnameinfo(res->ai_addr, res->ai_addrlen, hbuf,
sizeof(hbuf),
881 NULL, 0, NI_NUMERICHOST);
882 fprintf(stderr,
"++ connect [%s]:%d on fdno %d\n",
893 struct sockaddr_in sin;
895 memset(&sin, 0,
sizeof(sin));
896 sin.sin_family = AF_INET;
897 sin.sin_port = htons(port);
898 sin.sin_addr.s_addr = INADDR_ANY;
904 if ((fdno = socket(sin.sin_family, SOCK_STREAM, IPPROTO_IP)) < 0) {
910 if (connect(fdno, (
struct sockaddr *) &sin,
sizeof(sin))) {
921 fprintf(stderr,
"++ connect %s:%d on fdno %d\n",
923 inet_ntoa(sin.sin_addr)
925 (
int)ntohs(sin.sin_port), fdno);
928 fdSetFdno(ctrl, (fdno >= 0 ? fdno : -1));
941 int *ecp,
char ** str)
972 se = buf + bufLength;
974 rc =
fdFgets(ctrl, se, (bufAlloced - bufLength));
978 }
else if (rc == 0 ||
fdWritable(ctrl, 0) < 1)
984 for (s = se; *s !=
'\0'; s = se) {
987 while (*se && *se !=
'\n') se++;
989 if (se > s && se[-1] ==
'\r')
995 fprintf(stderr,
"<- %s\n", s);
1005 if (!strncmp(s,
"HTTP",
sizeof(
"HTTP")-1)) {
1007 if ((e = strchr(s,
'.')) != NULL) {
1015 if ((e = strchr(s,
' ')) != NULL) {
1017 if (strchr(
"0123456789", *e))
1018 strncpy(errorCode, e, 3);
1019 errorCode[3] =
'\0';
1025 for (e = s; *e && !(*e ==
' ' || *e ==
':'); e++)
1027 if (e > s && *e++ ==
':') {
1028 size_t ne = (e - s);
1029 while (*e && *e ==
' ') e++;
1031 if (!strncmp(s,
"Date:", ne)) {
1033 if (!strncmp(s,
"Server:", ne)) {
1035 if (!strncmp(s,
"Last-Modified:", ne)) {
1037 if (!strncmp(s,
"ETag:", ne)) {
1040 if (!strncmp(s,
"Accept-Ranges:", ne)) {
1041 if (!strcmp(e,
"bytes"))
1043 if (!strcmp(e,
"none"))
1046 if (!strncmp(s,
"Content-Length:", ne)) {
1047 if (strchr(
"0123456789", *e))
1050 if (!strncmp(s,
"Connection:", ne)) {
1051 if (!strcmp(e,
"close"))
1056 if (!strncmp(s,
"Content-Type:", ne)) {
1058 if (!strncmp(s,
"Transfer-Encoding:", ne)) {
1059 if (!strcmp(e,
"chunked"))
1064 if (!strncmp(s,
"Allow:", ne)) {
1071 if (!strncmp(s,
"<TITLE>",
sizeof(
"<TITLE>")-1))
1072 s +=
sizeof(
"<TITLE>") - 1;
1075 if (strchr(
"0123456789", *s)) {
1076 if (errorCode[0] !=
'\0') {
1077 if (!strncmp(s, errorCode,
sizeof(
"123")-1) && s[3] ==
' ')
1080 strncpy(errorCode, s,
sizeof(
"123")-1);
1081 errorCode[3] =
'\0';
1088 if (moretodo && se > s) {
1089 bufLength = se - s - 1;
1091 memmove(buf, s, bufLength);
1095 }
while (moretodo && ec == 0);
1097 if (str) *str = buf;
1098 if (ecp) *ecp = atoi(errorCode);
1121 if (ec >= 400 && ec <= 599) {
1135 const char * s, * t;
1141 while ((s = va_arg(ap,
const char *)) != NULL) {
1145 len +=
sizeof(
"\r\n")-1;
1148 t = te =
alloca(len + 1);
1151 while ((s = va_arg(ap,
const char *)) != NULL) {
1152 if (te > t) *te++ =
' ';
1159 fprintf(stderr,
"-> %s", t);
1173 const char * password;
1190 if ((password = u->
password) == NULL) {
1191 uid_t uid = getuid();
1193 if (uid && (pw = getpwuid(uid)) != NULL) {
1194 char *myp =
alloca(strlen(pw->pw_name) +
sizeof(
"@"));
1195 strcpy(myp, pw->pw_name);
1216 if ((rc =
ftpCommand(u, NULL,
"USER", user, NULL)))
1219 if ((rc =
ftpCommand(u, NULL,
"PASS", password, NULL)))
1222 if ((rc =
ftpCommand(u, NULL,
"TYPE",
"I", NULL)))
1245 #if !defined(HAVE_GETADDRINFO)
1246 struct sockaddr_in dataAddress;
1262 cmdlen = strlen(ftpCmd) + (ftpArg ? 1+strlen(ftpArg) : 0) +
sizeof(
"\r\n");
1263 chptr = cmd =
alloca(cmdlen);
1264 chptr =
stpcpy(chptr, ftpCmd);
1267 chptr =
stpcpy(chptr, ftpArg);
1269 chptr =
stpcpy(chptr,
"\r\n");
1270 cmdlen = chptr - cmd;
1275 if (!strncmp(cmd,
"RETR", 4)) {
1279 rc =
ftpCommand(u, &passReply,
"SIZE", ftpArg, NULL);
1282 if (sscanf(passReply,
"%d %u", &rc, &cl) != 2) {
1292 #ifdef HAVE_GETNAMEINFO
1293 rc =
ftpCommand(u, &passReply,
"EPSV", NULL);
1295 #ifdef HAVE_GETADDRINFO
1296 struct sockaddr_storage ss;
1298 struct sockaddr_in ss;
1300 socklen_t sslen =
sizeof(ss);
1303 if ((getpeername(
fdFileno(
c2f(u->
ctrl)), (
struct sockaddr *)&ss, &sslen) == 0)
1304 && (getnameinfo((
struct sockaddr *)&ss, sslen,
1305 remoteIP,
sizeof(remoteIP),
1306 NULL, 0, NI_NUMERICHOST) == 0))
1311 rc =
ftpCommand(u, &passReply,
"ABOR", NULL);
1320 rc =
ftpCommand(u, &passReply,
"PASV", NULL);
1327 assert(chptr != NULL);
1328 while (*chptr && *chptr !=
'(') chptr++;
1332 while (*chptr && *chptr !=
')') chptr++;
1338 if(sscanf(passReply,
"%*c%*c%*c%d%*c",&i) != 1) {
1345 while (*chptr && *chptr !=
',') chptr--;
1348 while (*chptr && *chptr !=
',') chptr--;
1356 if (sscanf(chptr,
"%d,%d", &i, &j) != 2) {
1360 port = (((unsigned)i) << 8) + j;
1364 while (*chptr++ !=
'\0') {
1365 if (*chptr ==
',') *chptr =
'.';
1367 sprintf(remoteIP,
"%s", passReply);
1370 #ifdef HAVE_GETADDRINFO
1373 struct addrinfo hints, *res, *res0;
1374 char pbuf[NI_MAXSERV];
1377 memset(&hints, 0,
sizeof(hints));
1378 hints.ai_family = AF_UNSPEC;
1379 hints.ai_socktype = SOCK_STREAM;
1380 hints.ai_flags = AI_NUMERICHOST;
1382 hints.ai_flags |= AI_IDN;
1384 sprintf(pbuf,
"%d", port);
1385 pbuf[
sizeof(pbuf)-1] =
'\0';
1386 if (getaddrinfo(remoteIP, pbuf, &hints, &res0)) {
1391 for (res = res0; res != NULL; res = res->ai_next) {
1392 rc = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
1403 data =
fdLink(data,
"open data (ftpReq)");
1411 while (connect(
fdFileno(data), res->ai_addr, (
int)res->ai_addrlen) < 0) {
1437 memset(&dataAddress, 0,
sizeof(dataAddress));
1438 dataAddress.sin_family = AF_INET;
1439 dataAddress.sin_port = htons(port);
1442 if (!
inet_aton(remoteIP, &dataAddress.sin_addr)) {
1448 rc = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
1454 data =
fdLink(data,
"open data (ftpReq)");
1461 while (connect(
fdFileno(data), (
struct sockaddr *) &dataAddress,
1462 sizeof(dataAddress)) < 0)
1473 fprintf(stderr,
"-> %s", cmd);
1474 if ((
size_t)
fdWrite(u->
ctrl, cmd, cmdlen) != cmdlen) {
1502 static void * _urlNotifyData = NULL;
1505 static int _urlNotifyCount = -1;
1507 static void urlSetCallback(
rpmCallbackFunction notify,
void *notifyData,
int notifyCount) {
1508 _urlNotify = notify;
1509 _urlNotifyData = notifyData;
1510 _urlNotifyCount = (notifyCount >= 0) ? notifyCount : 4096;
1518 int itemsCopied = 0;
1526 0, 0, NULL, _urlNotifyData);
1532 rc = (int)
Fread(buf,
sizeof(buf[0]),
sizeof(buf), sfd);
1540 rc = (int)
Fwrite(buf,
sizeof(buf[0]), itemsRead, tfd);
1543 if (rc != itemsRead) {
1548 itemsCopied += itemsRead;
1550 if (_urlNotify && _urlNotifyCount > 0) {
1551 int n = itemsCopied/_urlNotifyCount;
1552 if (n != notifier) {
1555 itemsCopied, 0, NULL, _urlNotifyData);
1563 DBGIO(sfd, (stderr,
"++ copied %d bytes: %s\n", itemsCopied,
1570 itemsCopied, itemsCopied, NULL, _urlNotifyData);
1591 if ((fd = u->
ctrl) == NULL) {
1592 fd = u->
ctrl =
fdNew(
"persist ctrl (urlConnect FTP)");
1604 fd =
fdLink(fd,
"grab ctrl (urlConnect FTP)");
1613 u->
ctrl =
fdFree(fd,
"grab ctrl (urlConnect FTP)");
1620 *uret =
urlLink(u,
"urlConnect");
1621 u =
urlFree(u,
"urlSplit (urlConnect)");
1639 int ftpCmd(
const char * cmd,
const char * url,
const char * arg2)
1650 rc =
ftpCommand(u, NULL, cmd, path, arg2, NULL);
1657 #define IAC ((unsigned char)255)
1660 #define IP ((unsigned char)244)
1663 #define DM ((unsigned char)242)
1665 #if !defined(SHUT_RDWR)
1666 #define SHUT_RDWR 1+1
1673 static unsigned char ipbuf[3] = {
IAC,
IP,
IAC };
1688 DBGIO(0, (stderr,
"-> ABOR\n"));
1691 if (send(
fdFileno(ctrl), ipbuf,
sizeof(ipbuf), MSG_OOB) !=
sizeof(ipbuf)) {
1696 sprintf(u->
buf,
"%cABOR\r\n",(
char)
DM);
1720 assert(u->
ctrl != NULL);
1762 if (
_ftp_debug && !(rc == 0 && (ec == 200 || ec == 201)))
1763 fprintf(stderr,
"*** httpResp: rc %d ec %d\n", rc, ec);
1781 static int httpReq(
FD_t ctrl,
const char * httpCmd,
const char * httpArg)
1795 assert(ctrl != NULL);
1801 if (strchr(host,
':'))
1802 sprintf(hthost,
"[%s]", host);
1804 strcpy(hthost, host);
1808 if (path == NULL) path =
"";
1820 ctrl =
fdLink(ctrl,
"open ctrl (httpReq)");
1825 User-Agent: rpm/3.0.4\r\n\
1827 Accept: text/plain\r\n\
1828 Transfer-Encoding: chunked\r\n\
1830 ") + strlen(httpCmd) + strlen(path) +
sizeof(
VERSION) + strlen(hthost) + 20;
1835 if (!strcmp(httpCmd,
"PUT")) {
1837 %s %s HTTP/1.%d\r\n\
1838 User-Agent: rpm/%s\r\n\
1840 Accept: text/plain\r\n\
1841 Transfer-Encoding: chunked\r\n\
1846 %s %s HTTP/1.%d\r\n\
1847 User-Agent: rpm/%s\r\n\
1849 Accept: text/plain\r\n\
1855 fprintf(stderr,
"-> %s", req);
1858 if (
fdWrite(ctrl, req, len) != len) {
1863 if (!strcmp(httpCmd,
"PUT")) {
1879 ctrl =
fdLink(ctrl,
"open data (httpReq)");
1898 if (fd->
url == NULL)
1906 static ssize_t
ufdRead(
void * cookie,
char * buf,
size_t count)
1918 (void) fstat(fdno, &sb);
1919 if (S_ISREG(sb.st_mode))
1920 return fdRead(fd, buf, count);
1926 for (total = 0; total < count; total += bytesRead) {
1939 return (ssize_t) total;
1945 rc = (int)
fdRead(fd, buf + total, count - total);
1956 fprintf(stderr,
"*** read: rc %d errno %d %s \"%s\"\n", rc,
errno, strerror(
errno), buf);
1959 }
else if (rc == 0) {
1960 return (ssize_t) total;
1963 bytesRead = (size_t) rc;
1966 return (ssize_t) count;
1969 static ssize_t
ufdWrite(
void * cookie,
const char * buf,
size_t count)
1974 size_t bytesWritten;
1981 if (S_ISREG(sb.st_mode))
1982 return fdWrite(fd, buf, count);
1988 for (total = 0; total < count; total += bytesWritten) {
1996 fprintf(stderr,
"*** ufdWrite fd %p WRITE PAST END OF CONTENT\n", fd);
1997 return (ssize_t) total;
2004 return (ssize_t) total;
2010 rc = (int)
fdWrite(fd, buf + total, count - total);
2021 fprintf(stderr,
"*** write: rc %d errno %d %s \"%s\"\n", rc,
errno, strerror(
errno), buf);
2024 }
else if (rc == 0) {
2025 return (ssize_t) total;
2028 bytesWritten = (size_t) rc;
2031 return (ssize_t) count;
2053 return fdSeek(cookie, pos, whence);
2068 fd = u->
data =
fdFree(fd,
"grab data (ufdClose persist)");
2070 fd =
fdFree(fd,
"grab data (ufdClose)");
2130 && (!strncmp(u->
scheme,
"http",
sizeof(
"http")-1) || !strncmp(u->
scheme,
"hkp",
sizeof(
"hkp")-1)))
2143 fd = u->
ctrl =
fdFree(fd,
"open data (ufdClose HTTP persist ctrl)");
2144 else if (fd == u->
data)
2145 fd = u->
data =
fdFree(fd,
"open data (ufdClose HTTP persist data)");
2147 fd =
fdFree(fd,
"open data (ufdClose HTTP)");
2183 assert(!(flags & O_RDWR));
2188 if (u->
data == NULL)
2189 u->
data =
fdNew(
"persist data (ftpOpen)");
2191 assert(u->
data != NULL);
2194 fd = u->
data =
fdLink(u->
data,
"grab data (ftpOpen persist data)");
2196 fd =
fdNew(
"grab data (ftpOpen)");
2231 fprintf(stderr,
"*** ufdOpen(%s,0x%x,0%o)\n", url, (
unsigned)flags, (
unsigned)mode);
2236 fd =
ftpOpen(url, flags, mode, &u);
2237 if (fd == NULL || u == NULL)
2241 cmd = ((flags & O_WRONLY)
2242 ? ((flags & O_APPEND) ?
"APPE" :
2243 ((flags & O_CREAT) ?
"STOR" :
"STOR"))
2244 : ((flags & O_CREAT) ?
"STOR" :
"RETR"));
2248 fd =
fdLink(fd,
"error data (ufdOpen FTP)");
2259 fd =
davOpen(url, flags, mode, &u);
2261 fd =
httpOpen(url, flags, mode, &u);
2263 if (fd == NULL || u == NULL)
2266 cmd = ((flags & O_WRONLY)
2267 ? ((flags & O_APPEND) ?
"PUT" :
2268 ((flags & O_CREAT) ?
"PUT" :
"PUT"))
2277 fd =
fdLink(fd,
"error ctrl (ufdOpen HTTP)");
2278 fd =
fdLink(fd,
"error data (ufdOpen HTTP)");
2287 assert(!(flags & O_RDWR));
2288 fd =
fdDup( ((flags & O_WRONLY) ? STDOUT_FILENO : STDIN_FILENO) );
2299 fd =
fdOpen(path, flags, mode);
2302 #if defined(RPM_VENDOR_MANDRIVA)
2312 if (fd == NULL)
return NULL;
2319 DBGIO(fd, (stderr,
"==>\tufdOpen(\"%s\",%x,0%o) %s\n", url, (
unsigned)flags, (
unsigned)mode,
fdbg(fd)));
2336 const char *errstr = NULL;
2338 #if defined(WITH_ZLIB)
2344 #if defined(WITH_BZIP2)
2350 #if defined(WITH_XZ)
2376 #define FDIOVEC(_fd, _vec) \
2377 ((fdGetIo(_fd) && fdGetIo(_fd)->_vec) ? fdGetIo(_fd)->_vec : NULL)
2379 size_t Fread(
void *buf,
size_t size,
size_t nmemb,
FD_t fd) {
2384 DBGIO(fd, (stderr,
"==> Fread(%p,%u,%u,%p) %s\n", buf, (
unsigned)size, (
unsigned)nmemb, (fd ? fd : NULL),
fdbg(fd)));
2388 rc = (int) fread(buf, size, nmemb,
fdGetFILE(fd));
2397 rc = (int) (_read ? (*_read) (fd, buf, size * nmemb) : -2);
2401 size_t Fwrite(
const void *buf,
size_t size,
size_t nmemb,
FD_t fd)
2407 DBGIO(fd, (stderr,
"==> Fwrite(%p,%u,%u,%p) %s\n", buf, (
unsigned)size, (
unsigned)nmemb, (fd ? fd : NULL),
fdbg(fd)));
2411 rc = (int) fwrite(buf, size, nmemb,
fdGetFILE(fd));
2420 rc = (int) (_write ? _write(fd, buf, size * nmemb) : -2);
2426 #ifdef USE_COOKIE_SEEK_POINTER
2427 _IO_off64_t o64 = offset;
2436 DBGIO(fd, (stderr,
"==> Fseek(%p,%ld,%d) %s\n", fd, (
long)offset, whence,
fdbg(fd)));
2443 rc = fseek(fp, (
long)offset, whence);
2452 rc = (_seek ? _seek(fd, pos, whence) : -2);
2461 DBGIO(fd, (stderr,
"==> Fclose(%p) %s\n", (fd ? fd : NULL),
fdbg(fd)));
2464 fd =
fdLink(fd,
"Fclose");
2466 while (fd->
nfps >= 0) {
2478 if (fd->
nfps > 0 && fpno == -1 &&
2483 int hadreqpersist = (fd->
req != NULL);
2499 if (hadreqpersist) {
2527 fd =
fdFree(fd,
"fopencookie (Fclose)");
2536 if (fd == NULL || fd->
nfps == 0)
2542 fd =
fdFree(fd,
"Fclose");
2565 char *stdio,
size_t nstdio,
2566 char *other,
size_t nother,
2567 const char **end,
int * f)
2575 flags |= O_WRONLY | O_CREAT | O_APPEND;
2576 if (--nstdio > 0) *stdio++ = *m;
2579 flags |= O_WRONLY | O_CREAT | O_TRUNC;
2580 if (--nstdio > 0) *stdio++ = *m;
2584 if (--nstdio > 0) *stdio++ = *m;
2593 while ((c = *m++) !=
'\0') {
2598 flags &= ~(O_RDONLY|O_WRONLY);
2600 if (--nstdio > 0) *stdio++ = c;
2608 #if defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ >= 3
2609 if (--nstdio > 0) *stdio++ = c;
2614 if (--nstdio > 0) *stdio++ = c;
2618 if (--nother > 0) *other++ = c;
2626 *stdio = *other =
'\0';
2628 *end = (*m !=
'\0' ? m : NULL);
2634 #if defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ == 0
2636 typedef _IO_cookie_io_functions_t cookie_io_functions_t;
2642 char stdio[20], other[20], zstdio[40+1];
2643 const char *end = NULL;
2648 fprintf(stderr,
"*** Fdopen(%p,%s) %s\n", fd, fmode,
fdbg(fd));
2654 cvtfmode(fmode, stdio,
sizeof(stdio), other,
sizeof(other), &end, NULL);
2655 if (stdio[0] ==
'\0')
2660 if (end == NULL && other[0] ==
'\0')
2664 if (!strcmp(end,
"fdio")) {
2666 #if defined(WITH_ZLIB)
2667 }
else if (!strcmp(end,
"gzdio")) {
2670 fd = iof->
_fdopen(fd, zstdio);
2673 #if defined(WITH_BZIP2)
2674 }
else if (!strcmp(end,
"bzdio")) {
2677 fd = iof->
_fdopen(fd, zstdio);
2680 #if defined(WITH_XZ)
2681 }
else if (!strcmp(end,
"lzdio")) {
2683 fd = iof->
_fdopen(fd, zstdio);
2684 }
else if (!strcmp(end,
"xzdio")) {
2686 fd = iof->
_fdopen(fd, zstdio);
2688 }
else if (!strcmp(end,
"ufdio")) {
2690 }
else if (!strcmp(end,
"fpio")) {
2694 FILE * fp = fdopen(fdno, stdio);
2697 fprintf(stderr,
"*** Fdopen fpio fp %p\n", (
void *)fp);
2709 }
else if (other[0] !=
'\0') {
2710 for (end = other; *end && strchr(
"0123456789fh", *end); end++)
2713 #if defined(WITH_ZLIB)
2716 fd = iof->
_fdopen(fd, zstdio);
2728 { cookie_io_functions_t ciof;
2729 ciof.read = iof->
read;
2730 ciof.write = iof->
write;
2731 ciof.seek = iof->
seek;
2732 ciof.close = iof->
close;
2733 fp = fopencookie(fd, stdio, ciof);
2734 DBGIO(fd, (stderr,
"==> fopencookie(%p,\"%s\",*%p) returns fp %p\n", fd, stdio, iof, fp));
2745 fd =
fdLink(fd,
"fopencookie");
2750 DBGIO(fd, (stderr,
"==> Fdopen(%p,\"%s\") returns fd %p %s\n", ofd, fmode, (fd ? fd : NULL),
fdbg(fd)));
2757 const char * fmode = NULL;
2758 char stdio[20], other[20];
2759 const char *end = NULL;
2760 mode_t perms = 0666;
2764 if (path == NULL || _fmode == NULL)
2771 cvtfmode(fmode, stdio,
sizeof(stdio), other,
sizeof(other), &end, &flags);
2772 if (stdio[0] ==
'\0')
2775 if (end == NULL || !strcmp(end,
"fdio")) {
2777 fprintf(stderr,
"*** Fopen(%s, %s) fdio\n", path, fmode);
2778 fd =
fdOpen(path, flags, perms);
2802 fprintf(stderr,
"*** Fopen(%s, %s) ufdio\n", path, fmode);
2803 fd =
ufdOpen(path, flags, perms);
2804 if (fd == NULL || !(
fdFileno(fd) >= 0 || fd->
req != NULL)) {
2812 fprintf(stderr,
"*** Fopen(%s, %s) WTFO\n", path, fmode);
2820 if (isHTTP && ((fp =
fdGetFp(fd)) != NULL) && ((fdno =
fdGetFdno(fd)) >= 0 || fd->
req != NULL))
2832 fmode =
_free(fmode);
2839 if (fd == NULL)
return -1;
2846 #if defined(WITH_ZLIB)
2850 #if defined(WITH_BZIP2)
2854 #if defined(WITH_XZ)
2868 if (fd == NULL)
return -1;
2869 if (fd->
req != NULL) {
2873 for (i = fd->
nfps; rc == 0 && i >= 0; i--) {
2881 #if defined(WITH_ZLIB)
2882 }
else if (fps->
io ==
gzdio) {
2886 #if defined(WITH_BZIP2)
2887 }
else if (fps->
io ==
bzdio) {
2891 #if defined(WITH_XZ)
2892 }
else if (fps->
io ==
lzdio) {
2895 }
else if (fps->
io ==
xzdio) {
2907 DBGIO(fd, (stderr,
"==> Ferror(%p) rc %d %s\n", fd, rc,
fdbg(fd)));
2917 if (fd->
req != NULL)
2920 for (i = fd->
nfps ; rc == -1 && i >= 0; i--) {
2924 DBGIO(fd, (stderr,
"==> Fileno(%p) rc %d %s\n", (fd ? fd : NULL), rc,
fdbg(fd)));
2931 return fcntl(
Fileno(fd), op, lip);
2943 if (path == NULL || *path ==
'\0')
2945 d =
alloca(strlen(path)+2);
2948 for (de = d; *de !=
'\0'; de++) {
2952 while (*de && *de !=
'/') de++;
2965 rc =
Mkdir(d, mode);
2969 if (!(uid == (uid_t) -1 && gid == (gid_t) -1)) {
2970 rc =
Chown(d, uid, gid);
2974 }
else if (!S_ISDIR(st.st_mode)) {
2982 path, (
unsigned)mode);
2986 #define _PATH "/bin:/usr/bin:/sbin:/usr/sbin"
2990 #define alloca_strdup(_s) strcpy(alloca(strlen(_s)+1), (_s))
3002 if (FN == NULL || *FN ==
'\0')
3010 for (t = bn; t && *t; t++) {
3023 && strchr(
"Rr_", bn[0]) != NULL
3024 && strchr(
"Ww_", bn[1]) != NULL
3025 && strchr(
"Xx_", bn[2]) != NULL) {
3027 if (strchr(
"Rr", bn[0]) != NULL)
3029 if (strchr(
"Ww", bn[1]) != NULL)
3031 if (strchr(
"Xx", bn[2]) != NULL)
3035 }
else if (!strcmp(bn,
"exists"))
3037 else if (!strcmp(bn,
"executable"))
3039 else if (!strcmp(bn,
"readable"))
3041 else if (!strcmp(bn,
"writable"))
3045 te = bn + strlen(t) - 1;
3058 rc = (
Access(bn, mode) != 0 ? 1 : 0);
3060 fprintf(stderr,
"*** rpmioAccess(\"%s\", 0x%x) rc %d\n", bn, mode, rc);
3075 for (r =
alloca_strdup(path); r != NULL && *r !=
'\0'; r = re) {
3078 for (re = r; (re = strchr(re,
':')) != NULL; re++) {
3079 if (!(re[1] ==
'/' && re[2] ==
'/'))
3082 if (re && *re ==
':')
3091 if (r[0] ==
'~' && r[1] ==
'/') {
3092 const char * home =
getenv(
"HOME");
3095 if (strlen(home) > (
sizeof(fn) - strlen(r)))
3101 if (t[-1] !=
'/' && *bn !=
'/')
3109 rc = (
Access(t, mode) != 0 ? 1 : 0);
3111 fprintf(stderr,
"*** rpmioAccess(\"%s\", 0x%x) rc %d\n", t, mode, rc);
3124 #if defined(WITH_NSS) && !defined(__LCLINT__)
3126 extern void NSS_Shutdown(
void);
3130 int _rpmnss_init = 0;
3156 #if defined(WITH_LUA)
3159 #if defined(WITH_NEON)
3162 #if defined(WITH_NSS) && !defined(__LCLINT__)
3164 (void) NSS_Shutdown();
3170 _rpmtclI = rpmtclFree(_rpmtclI);
3172 _rpmrubyI = rpmrubyFree(_rpmrubyI);
3174 _rpmpythonI = rpmpythonFree(_rpmpythonI);
3176 _rpmperlI = rpmperlFree(_rpmperlI);
3178 _rpmjsI = rpmjsFree(_rpmjsI);
3180 _rpmficlI = rpmficlFree(_rpmficlI);