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>
84 #define _RPMHKP_INTERNAL
91 #if defined(HAVE_LIBIO_H) && defined(_G_IO_IO_FILE_VERSION)
96 #if !defined(HAVE_HERRNO) && (defined(hpux) || defined(__hpux) || defined(__LCLINT__))
102 #define IPPORT_FTP 21
105 #define IPPORT_HTTP 80
108 #if !defined(HAVE_INET_ATON)
109 #define inet_aton(cp,inp) rpm_inet_aton(cp,inp)
115 addr = inet_addr(cp);
116 if (addr == ((
long) -1))
return 0;
118 memcpy(inp, &addr,
sizeof(addr));
123 #if defined(USE_ALT_DNS) && USE_ALT_DNS
130 #define fdOpen __fdOpen
132 #define fdRead __fdRead
134 #define fdWrite __fdWrite
136 #define fdClose __fdClose
150 #define FDTO(fd) (fd ? ((FD_t)fd)->rd_timeoutsecs : -99)
151 #define FDCPIOPOS(fd) (fd ? ((FD_t)fd)->fd_cpioPos : -99)
153 #define FDONLY(fd) assert(fdGetIo(fd) == fdio)
157 #define fdGetFILE(_fd) ((FILE *)fdGetFp(_fd))
168 #define TIMEOUT_SECS 60
199 static char buf[BUFSIZ];
208 sprintf(be,
"fd %p", fd); be += strlen(be);
219 strcpy(be,
" chunked");
223 for (i = fd->
nfps; i >= 0; i--) {
230 sprintf(be,
"FD %d fp %p", fps->
fdno, fps->
fp);
232 sprintf(be,
"UFD %d fp %p", fps->
fdno, fps->
fp);
233 #if defined(WITH_ZLIB)
235 sprintf(be,
"GZD %p fdno %d", fps->
fp, fps->
fdno);
237 #if defined(WITH_BZIP2)
239 sprintf(be,
"BZD %p fdno %d", fps->
fp, fps->
fdno);
243 sprintf(be,
"LZD %p fdno %d", fps->
fp, fps->
fdno);
245 sprintf(be,
"XZD %p fdno %d", fps->
fp, fps->
fdno);
247 }
else if (fps->
io ==
fpio) {
249 sprintf(be,
"%s %p(%d) fdno %d",
250 (fps->
fdno < 0 ?
"LIBIO" :
"FP"),
251 fps->
fp, fileno(((FILE *)fps->
fp)), fps->
fdno);
254 sprintf(be,
"??? io %p fp %p fdno %d ???",
269 if ((nfdno = dup(fdno)) < 0)
271 if (fcntl(nfdno, F_SETFD, FD_CLOEXEC)) {
275 fd =
fdNew(
"open (fdDup)");
278 DBGIO(fd, (stderr,
"<-- fdDup(%d) fd %p %s\n", fdno, (fd ? fd : NULL),
fdbg(fd)));
306 for (i = fd->
ndigests - 1; i >= 0; i--) {
324 fprintf(stderr,
"*** %s: fd->req %p\n", __FUNCTION__, fd->
req);
327 assert(fd->
req == NULL);
343 if (_fdPool == NULL) {
349 memset(((
char *)fd)+
sizeof(fd->
_item), 0,
sizeof(*fd)-
sizeof(fd->
_item));
364 memset(fd->
fps, 0,
sizeof(fd->
fps));
367 fd->
fps[0].
fp = NULL;
401 static ssize_t
fdRead(
void * cookie,
char * buf,
size_t count)
413 if (fd->
req != NULL) {
415 if (fd->
req != (
void *)-1)
426 if (fd->
xar != NULL) {
438 DBGIO(fd, (stderr,
"<--\tfdRead(%p,%p,%ld) rc %ld %s\n", cookie, buf, (
long)count, (
long)rc,
fdbg(fd)));
443 static ssize_t
fdWrite(
void * cookie,
const char * buf,
size_t count)
455 if (count == 0)
return 0;
461 if (fd->
req != (
void *)-1)
472 DBGIO(fd, (stderr,
"<--\tfdWrite(%p,%p,%ld) rc %ld %s\n", cookie, buf, (
long)count, (
long)rc,
fdbg(fd)));
481 #ifdef USE_COOKIE_SEEK_POINTER
482 _IO_off64_t p = *pos;
491 rc = lseek(
fdFileno(fd), p, whence);
494 DBGIO(fd, (stderr,
"<--\tfdSeek(%p,%ld,%d) rc %lx %s\n", cookie, (
long)p, whence, (
unsigned long)rc,
fdbg(fd)));
507 if (cookie == NULL)
return -2;
522 rc = ((fdno >= 0) ? close(fdno) : -2);
525 DBGIO(fd, (stderr,
"<--\tfdClose(%p) rc %lx %s\n", (fd ? fd : NULL), (
unsigned long)rc,
fdbg(fd)));
527 fd =
fdFree(fd,
"open (fdClose)");
538 fdno = open(path, flags, mode);
539 if (fdno < 0)
return NULL;
540 if (fcntl(fdno, F_SETFD, FD_CLOEXEC)) {
544 fd =
fdNew(
"open (fdOpen)");
549 DBGIO(fd, (stderr,
"<--\tfdOpen(\"%s\",%x,0%o) %s\n", path, (
unsigned)flags, (
unsigned)mode,
fdbg(fd)));
554 FILE *fdFdopen(
void * cookie,
const char *fmode)
560 if (fmode == NULL)
return NULL;
562 if (fdno < 0)
return NULL;
563 fp = fdopen(fdno, fmode);
564 DBGIO(fd, (stderr,
"<-- fdFdopen(%p,\"%s\") fdno %d -> fp %p fdno %d\n", cookie, fmode, fdno, fp, fileno(fp)));
565 fd =
fdFree(fd,
"open (fdFdopen)");
582 #if defined(HAVE_POLL_H)
583 int msecs = (secs >= 0 ? (1000 * secs) : -1);
586 struct timeval timeout, *tvp = (secs >= 0 ? &timeout : NULL);
593 return (fd->
req == (
void *)-1 ? -1 : 1);
599 #if defined(HAVE_POLL_H)
601 wrfds.events = POLLOUT;
603 rc = poll(&wrfds, 1, msecs);
609 FD_SET(fdno, &wrfds);
611 rc = select(fdno + 1, NULL, &wrfds, NULL, tvp);
617 fprintf(stderr,
"*** fdWritable fdno %d rc %d %s\n", fdno, rc, strerror(
errno));
637 #if defined(HAVE_POLL_H)
638 int msecs = (secs >= 0 ? (1000 * secs) : -1);
641 struct timeval timeout, *tvp = (secs >= 0 ? &timeout : NULL);
648 return (fd->
req == (
void *)-1 ? -1 : 1);
654 #if defined(HAVE_POLL_H)
656 rdfds.events = POLLIN;
658 rc = poll(&rdfds, 1, msecs);
664 FD_SET(fdno, &rdfds);
666 rc = select(fdno + 1, &rdfds, NULL, NULL, tvp);
691 char lastchar =
'\0';
717 rc =
fdRead(fd, buf + nb, 1);
719 rc = (int)read(
fdFileno(fd), buf + nb, 1);
731 fprintf(stderr,
"*** read: fd %p rc %d errno %d %s \"%s\"\n", fd, rc,
errno, strerror(
errno), buf);
734 }
else if (rc == 0) {
736 fprintf(stderr,
"*** read: fd %p rc %d EOF errno %d %s \"%s\"\n", fd, rc,
errno, strerror(
errno), buf);
741 lastchar = buf[nb - 1];
743 }
while (ec == 0 && nb < len && lastchar !=
'\n');
745 return (ec >= 0 ? (
int)nb : ec);
753 switch (errorNumber) {
759 return (
"NE_ERROR: Generic error.");
761 return (
"NE_LOOKUP: Hostname lookup failed.");
763 return (
"NE_AUTH: Server authentication failed.");
765 return (
"NE_PROXYAUTH: Proxy authentication failed.");
767 return (
"NE_CONNECT: Could not connect to server.");
769 return (
"NE_TIMEOUT: Connection timed out.");
771 return (
"NE_FAILED: The precondition failed.");
773 return (
"NE_RETRY: Retry request.");
775 return (
"NE_REDIRECT: Redirect received.");
778 return _(
"Bad server response");
780 return _(
"Server I/O error");
782 return _(
"Server timeout");
784 return _(
"Unable to lookup server host address");
786 return _(
"Unable to lookup server host name");
788 return _(
"Failed to connect to server");
790 return _(
"Failed to establish data connection to server");
792 return _(
"I/O error to local file");
794 return _(
"Error setting remote server to passive mode");
796 return _(
"File not found on server");
798 return _(
"Abort in progress");
802 return _(
"Unknown or unexpected error");
819 retstr =
_(
"Malformed URL");
823 retstr = strerror(
errno);
829 #if !defined(HAVE_GETADDRINFO)
830 #if !defined(USE_ALT_DNS) || !USE_ALT_DNS
832 struct in_addr * address)
836 struct hostent * hostinfo;
839 hostinfo = gethostbyname(host);
841 if (!hostinfo)
return 1;
843 memcpy(address, hostinfo->h_addr_list[0],
sizeof(*address));
854 if (!strcmp(host,
"localhost")) {
884 #ifdef HAVE_GETADDRINFO
886 struct addrinfo hints, *res, *res0;
888 #define NI_MAXSERV 32
890 char pbuf[NI_MAXSERV];
893 memset(&hints, 0,
sizeof(hints));
894 hints.ai_family = AF_UNSPEC;
895 hints.ai_socktype = SOCK_STREAM;
896 sprintf(pbuf,
"%d", port);
897 pbuf[
sizeof(pbuf)-1] =
'\0';
899 if (getaddrinfo(host, pbuf, &hints, &res0) == 0) {
900 for (res = res0; res != NULL; res = res->ai_next) {
901 if ((fdno = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) < 0)
903 if (connect(fdno, res->ai_addr, (
int)res->ai_addrlen) < 0) {
912 xx = getnameinfo(res->ai_addr, res->ai_addrlen, hbuf,
sizeof(hbuf),
913 NULL, 0, NI_NUMERICHOST);
914 fprintf(stderr,
"++ connect [%s]:%d on fdno %d\n",
925 struct sockaddr_in sin;
927 memset(&sin, 0,
sizeof(sin));
928 sin.sin_family = AF_INET;
929 sin.sin_port = htons(port);
930 sin.sin_addr.s_addr = INADDR_ANY;
936 if ((fdno = socket(sin.sin_family, SOCK_STREAM, IPPROTO_IP)) < 0) {
942 if (connect(fdno, (
struct sockaddr *) &sin,
sizeof(sin))) {
953 fprintf(stderr,
"++ connect %s:%d on fdno %d\n",
955 inet_ntoa(sin.sin_addr)
957 (
int)ntohs(sin.sin_port), fdno);
960 fdSetFdno(ctrl, (fdno >= 0 ? fdno : -1));
973 int *ecp,
char ** str)
1004 se = buf + bufLength;
1006 rc =
fdFgets(ctrl, se, (bufAlloced - bufLength));
1010 }
else if (rc == 0 ||
fdWritable(ctrl, 0) < 1)
1016 for (s = se; *s !=
'\0'; s = se) {
1019 while (*se && *se !=
'\n') se++;
1021 if (se > s && se[-1] ==
'\r')
1027 fprintf(stderr,
"<- %s\n", s);
1037 if (!strncmp(s,
"HTTP",
sizeof(
"HTTP")-1)) {
1039 if ((e = strchr(s,
'.')) != NULL) {
1047 if ((e = strchr(s,
' ')) != NULL) {
1049 if (strchr(
"0123456789", *e))
1050 strncpy(errorCode, e, 3);
1051 errorCode[3] =
'\0';
1057 for (e = s; *e && !(*e ==
' ' || *e ==
':'); e++)
1059 if (e > s && *e++ ==
':') {
1060 size_t ne = (e - s);
1061 while (*e && *e ==
' ') e++;
1063 if (!strncmp(s,
"Date:", ne)) {
1065 if (!strncmp(s,
"Server:", ne)) {
1067 if (!strncmp(s,
"Last-Modified:", ne)) {
1069 if (!strncmp(s,
"ETag:", ne)) {
1072 if (!strncmp(s,
"Accept-Ranges:", ne)) {
1073 if (!strcmp(e,
"bytes"))
1075 if (!strcmp(e,
"none"))
1078 if (!strncmp(s,
"Content-Length:", ne)) {
1079 if (strchr(
"0123456789", *e))
1082 if (!strncmp(s,
"Connection:", ne)) {
1083 if (!strcmp(e,
"close"))
1088 if (!strncmp(s,
"Content-Type:", ne)) {
1090 if (!strncmp(s,
"Transfer-Encoding:", ne)) {
1091 if (!strcmp(e,
"chunked"))
1096 if (!strncmp(s,
"Allow:", ne)) {
1103 if (!strncmp(s,
"<TITLE>",
sizeof(
"<TITLE>")-1))
1104 s +=
sizeof(
"<TITLE>") - 1;
1107 if (strchr(
"0123456789", *s)) {
1108 if (errorCode[0] !=
'\0') {
1109 if (!strncmp(s, errorCode,
sizeof(
"123")-1) && s[3] ==
' ')
1112 strncpy(errorCode, s,
sizeof(
"123")-1);
1113 errorCode[3] =
'\0';
1120 if (moretodo && se > s) {
1121 bufLength = se - s - 1;
1123 memmove(buf, s, bufLength);
1127 }
while (moretodo && ec == 0);
1129 if (str) *str = buf;
1130 if (ecp) *ecp = atoi(errorCode);
1153 if (ec >= 400 && ec <= 599) {
1167 const char * s, * t;
1173 while ((s = va_arg(ap,
const char *)) != NULL) {
1177 len +=
sizeof(
"\r\n")-1;
1180 t = te = (
char *)
alloca(len + 1);
1183 while ((s = va_arg(ap,
const char *)) != NULL) {
1184 if (te > t) *te++ =
' ';
1191 fprintf(stderr,
"-> %s", t);
1205 const char * password;
1222 if ((password = u->
password) == NULL) {
1223 uid_t uid = getuid();
1225 if (uid && (pw = getpwuid(uid)) != NULL) {
1226 char *myp = (
char *)
alloca(strlen(pw->pw_name) +
sizeof(
"@"));
1227 strcpy(myp, pw->pw_name);
1248 if ((rc =
ftpCommand(u, NULL,
"USER", user, NULL)))
1251 if ((rc =
ftpCommand(u, NULL,
"PASS", password, NULL)))
1254 if ((rc =
ftpCommand(u, NULL,
"TYPE",
"I", NULL)))
1278 struct sockaddr_in dataAddress;
1294 cmdlen = strlen(ftpCmd) + (ftpArg ? 1+strlen(ftpArg) : 0) +
sizeof(
"\r\n");
1295 chptr = cmd = (
char *)
alloca(cmdlen);
1296 chptr =
stpcpy(chptr, ftpCmd);
1299 chptr =
stpcpy(chptr, ftpArg);
1301 chptr =
stpcpy(chptr,
"\r\n");
1302 cmdlen = chptr - cmd;
1307 if (!strncmp(cmd,
"RETR", 4)) {
1311 rc =
ftpCommand(u, &passReply,
"SIZE", ftpArg, NULL);
1314 if (sscanf(passReply,
"%d %u", &rc, &cl) != 2) {
1324 #ifdef HAVE_GETNAMEINFO
1325 rc =
ftpCommand(u, &passReply,
"EPSV", NULL);
1327 #ifdef HAVE_GETADDRINFO
1328 struct sockaddr_storage ss;
1330 struct sockaddr_in ss;
1332 socklen_t sslen =
sizeof(ss);
1335 if ((getpeername(
fdFileno(
c2f(u->
ctrl)), (
struct sockaddr *)&ss, &sslen) == 0)
1336 && (getnameinfo((
struct sockaddr *)&ss, sslen,
1337 remoteIP,
sizeof(remoteIP),
1338 NULL, 0, NI_NUMERICHOST) == 0))
1343 rc =
ftpCommand(u, &passReply,
"ABOR", NULL);
1352 rc =
ftpCommand(u, &passReply,
"PASV", NULL);
1359 assert(chptr != NULL);
1360 while (*chptr && *chptr !=
'(') chptr++;
1364 while (*chptr && *chptr !=
')') chptr++;
1370 if(sscanf(passReply,
"%*c%*c%*c%d%*c",&i) != 1) {
1377 while (*chptr && *chptr !=
',') chptr--;
1380 while (*chptr && *chptr !=
',') chptr--;
1388 if (sscanf(chptr,
"%d,%d", &i, &j) != 2) {
1392 port = (((unsigned)i) << 8) + j;
1396 while (*chptr++ !=
'\0') {
1397 if (*chptr ==
',') *chptr =
'.';
1399 sprintf(remoteIP,
"%s", passReply);
1402 #ifdef HAVE_GETADDRINFO
1405 struct addrinfo hints, *res, *res0;
1406 char pbuf[NI_MAXSERV];
1409 memset(&hints, 0,
sizeof(hints));
1410 hints.ai_family = AF_UNSPEC;
1411 hints.ai_socktype = SOCK_STREAM;
1412 hints.ai_flags = AI_NUMERICHOST;
1414 hints.ai_flags |= AI_IDN;
1416 sprintf(pbuf,
"%d", port);
1417 pbuf[
sizeof(pbuf)-1] =
'\0';
1418 if (getaddrinfo(remoteIP, pbuf, &hints, &res0)) {
1423 for (res = res0; res != NULL; res = res->ai_next) {
1424 rc = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
1435 data =
fdLink(data,
"open data (ftpReq)");
1443 while (connect(
fdFileno(data), res->ai_addr, (
int)res->ai_addrlen) < 0) {
1469 memset(&dataAddress, 0,
sizeof(dataAddress));
1470 dataAddress.sin_family = AF_INET;
1471 dataAddress.sin_port = htons(port);
1474 if (!
inet_aton(remoteIP, &dataAddress.sin_addr)) {
1480 rc = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
1486 data =
fdLink(data,
"open data (ftpReq)");
1493 while (connect(
fdFileno(data), (
struct sockaddr *) &dataAddress,
1494 sizeof(dataAddress)) < 0)
1505 fprintf(stderr,
"-> %s", cmd);
1506 if ((
size_t)
fdWrite(u->
ctrl, cmd, cmdlen) != cmdlen) {
1534 static void * _urlNotifyData = NULL;
1537 static int _urlNotifyCount = -1;
1539 static void urlSetCallback(
rpmCallbackFunction notify,
void *notifyData,
int notifyCount) {
1540 _urlNotify = notify;
1541 _urlNotifyData = notifyData;
1542 _urlNotifyCount = (notifyCount >= 0) ? notifyCount : 4096;
1550 int itemsCopied = 0;
1558 0, 0, NULL, _urlNotifyData);
1564 rc = (int)
Fread(buf,
sizeof(buf[0]),
sizeof(buf), sfd);
1572 rc = (int)
Fwrite(buf,
sizeof(buf[0]), itemsRead, tfd);
1575 if (rc != itemsRead) {
1580 itemsCopied += itemsRead;
1582 if (_urlNotify && _urlNotifyCount > 0) {
1583 int n = itemsCopied/_urlNotifyCount;
1584 if (n != notifier) {
1587 itemsCopied, 0, NULL, _urlNotifyData);
1595 DBGIO(sfd, (stderr,
"++ copied %d bytes: %s\n", itemsCopied,
1602 itemsCopied, itemsCopied, NULL, _urlNotifyData);
1623 if ((fd = u->
ctrl) == NULL) {
1624 fd = u->
ctrl =
fdNew(
"persist ctrl (urlConnect FTP)");
1636 fd =
fdLink(fd,
"grab ctrl (urlConnect FTP)");
1645 u->
ctrl =
fdFree(fd,
"grab ctrl (urlConnect FTP)");
1652 *uret =
urlLink(u,
"urlConnect");
1653 u =
urlFree(u,
"urlSplit (urlConnect)");
1671 int ftpCmd(
const char * cmd,
const char * url,
const char * arg2)
1682 rc =
ftpCommand(u, NULL, cmd, path, arg2, NULL);
1689 #define IAC ((unsigned char)255)
1692 #define IP ((unsigned char)244)
1695 #define DM ((unsigned char)242)
1697 #if !defined(SHUT_RDWR)
1698 #define SHUT_RDWR 1+1
1705 static unsigned char ipbuf[3] = {
IAC,
IP,
IAC };
1720 DBGIO(0, (stderr,
"-> ABOR\n"));
1723 if (send(
fdFileno(ctrl), ipbuf,
sizeof(ipbuf), MSG_OOB) !=
sizeof(ipbuf)) {
1728 sprintf(u->
buf,
"%cABOR\r\n",(
char)
DM);
1752 assert(u->
ctrl != NULL);
1794 if (
_ftp_debug && !(rc == 0 && (ec == 200 || ec == 201)))
1795 fprintf(stderr,
"*** httpResp: rc %d ec %d\n", rc, ec);
1813 static int httpReq(
FD_t ctrl,
const char * httpCmd,
const char * httpArg)
1827 assert(ctrl != NULL);
1833 if (strchr(host,
':'))
1834 sprintf(hthost,
"[%s]", host);
1836 strcpy(hthost, host);
1840 if (path == NULL) path =
"";
1852 ctrl =
fdLink(ctrl,
"open ctrl (httpReq)");
1857 User-Agent: rpm/3.0.4\r\n\
1859 Accept: text/plain\r\n\
1860 Transfer-Encoding: chunked\r\n\
1862 ") + strlen(httpCmd) + strlen(path) +
sizeof(
VERSION) + strlen(hthost) + 20;
1867 if (!strcmp(httpCmd,
"PUT")) {
1869 %s %s HTTP/1.%d\r\n\
1870 User-Agent: rpm/%s\r\n\
1872 Accept: text/plain\r\n\
1873 Transfer-Encoding: chunked\r\n\
1878 %s %s HTTP/1.%d\r\n\
1879 User-Agent: rpm/%s\r\n\
1881 Accept: text/plain\r\n\
1887 fprintf(stderr,
"-> %s", req);
1890 if (
fdWrite(ctrl, req, len) != (ssize_t)len) {
1895 if (!strcmp(httpCmd,
"PUT")) {
1911 ctrl =
fdLink(ctrl,
"open data (httpReq)");
1933 return urlLink(fd->
u,
"ufdGetUrlinfo");
1938 static ssize_t
ufdRead(
void * cookie,
char * buf,
size_t count)
1950 (void) fstat(fdno, &sb);
1951 if (S_ISREG(sb.st_mode))
1952 return fdRead(fd, buf, count);
1958 for (total = 0; total < count; total += bytesRead) {
1971 return (ssize_t) total;
1977 rc = (int)
fdRead(fd, buf + total, count - total);
1988 fprintf(stderr,
"*** read: rc %d errno %d %s \"%s\"\n", rc,
errno, strerror(
errno), buf);
1991 }
else if (rc == 0) {
1992 return (ssize_t) total;
1995 bytesRead = (size_t) rc;
1998 return (ssize_t) count;
2001 static ssize_t
ufdWrite(
void * cookie,
const char * buf,
size_t count)
2006 size_t bytesWritten;
2013 if (S_ISREG(sb.st_mode))
2014 return fdWrite(fd, buf, count);
2020 for (total = 0; total < count; total += bytesWritten) {
2028 fprintf(stderr,
"*** ufdWrite fd %p WRITE PAST END OF CONTENT\n", fd);
2029 return (ssize_t) total;
2036 return (ssize_t) total;
2042 rc = (int)
fdWrite(fd, buf + total, count - total);
2053 fprintf(stderr,
"*** write: rc %d errno %d %s \"%s\"\n", rc,
errno, strerror(
errno), buf);
2056 }
else if (rc == 0) {
2057 return (ssize_t) total;
2060 bytesWritten = (size_t) rc;
2063 return (ssize_t) count;
2086 return fdSeek(cookie, pos, whence);
2101 fd = u->
data =
fdFree(fd,
"grab data (ufdClose persist)");
2103 fd =
fdFree(fd,
"grab data (ufdClose)");
2105 (void)
urlFree(fd->
u,
"url (ufdClose)");
2163 && (!strncmp(u->
scheme,
"http",
sizeof(
"http")-1) || !strncmp(u->
scheme,
"hkp",
sizeof(
"hkp")-1)))
2176 fd = u->
ctrl =
fdFree(fd,
"open data (ufdClose HTTP persist ctrl)");
2177 else if (fd == u->
data)
2178 fd = u->
data =
fdFree(fd,
"open data (ufdClose HTTP persist data)");
2180 fd =
fdFree(fd,
"open data (ufdClose HTTP)");
2217 assert(!(flags & O_RDWR));
2222 if (u->
data == NULL)
2223 u->
data =
fdNew(
"persist data (ftpOpen)");
2225 assert(u->
data != NULL);
2227 if (u->
data->
u == NULL)
2228 fd = u->
data =
fdLink(u->
data,
"grab data (ftpOpen persist data)");
2230 fd =
fdNew(
"grab data (ftpOpen)");
2240 fd->
u =
urlLink(u,
"url (ufdOpen FTP)");
2264 fprintf(stderr,
"*** ufdOpen(%s,0x%x,0%o)\n", url, (
unsigned)flags, (
unsigned)mode);
2269 fd =
ftpOpen(url, flags, mode, &u);
2270 if (fd == NULL || u == NULL)
2274 cmd = ((flags & O_WRONLY)
2275 ? ((flags & O_APPEND) ?
"APPE" :
2276 ((flags & O_CREAT) ?
"STOR" :
"STOR"))
2277 : ((flags & O_CREAT) ?
"STOR" :
"RETR"));
2281 fd =
fdLink(fd,
"error data (ufdOpen FTP)");
2292 fd =
davOpen(url, flags, mode, &u);
2294 fd =
httpOpen(url, flags, mode, &u);
2296 if (fd == NULL || u == NULL)
2299 cmd = ((flags & O_WRONLY)
2300 ? ((flags & O_APPEND) ?
"PUT" :
2301 ((flags & O_CREAT) ?
"PUT" :
"PUT"))
2312 fd = u->
ctrl =
fdLink(fd,
"error ctrl (ufdOpen HTTP)");
2313 fd = u->
data fdLink(fd,
"error data (ufdOpen HTTP)");
2325 assert(!(flags & O_RDWR));
2326 fd =
fdDup( ((flags & O_WRONLY) ? STDOUT_FILENO : STDIN_FILENO) );
2337 fd =
fdOpen(path, flags, mode);
2346 if (fd == NULL)
return NULL;
2352 DBGIO(fd, (stderr,
"<--\tufdOpen(\"%s\",%x,0%o) %s\n", url, (
unsigned)flags, (
unsigned)mode,
fdbg(fd)));
2369 const char *errstr = NULL;
2371 #if defined(WITH_ZLIB)
2377 #if defined(WITH_BZIP2)
2383 #if defined(WITH_XZ)
2409 #define FDIOVEC(_fd, _vec) \
2410 ((fdGetIo(_fd) && fdGetIo(_fd)->_vec) ? fdGetIo(_fd)->_vec : NULL)
2412 size_t Fread(
void *buf,
size_t size,
size_t nmemb,
FD_t fd) {
2417 DBGIO(fd, (stderr,
"==> Fread(%p,%u,%u,%p) %s\n", buf, (
unsigned)size, (
unsigned)nmemb, (fd ? fd : NULL),
fdbg(fd)));
2421 rc = (int) fread(buf, size, nmemb,
fdGetFILE(fd));
2430 rc = (int) (_read ? (*_read) (fd, (
char *)buf, size * nmemb) : -2);
2434 size_t Fwrite(
const void *buf,
size_t size,
size_t nmemb,
FD_t fd)
2440 DBGIO(fd, (stderr,
"==> Fwrite(%p,%u,%u,%p) %s\n", buf, (
unsigned)size, (
unsigned)nmemb, (fd ? fd : NULL),
fdbg(fd)));
2444 rc = (int) fwrite(buf, size, nmemb,
fdGetFILE(fd));
2453 rc = (int) (_write ? _write(fd, (
const char *)buf, size * nmemb) : -2);
2460 #ifdef USE_COOKIE_SEEK_POINTER
2461 _IO_off64_t o64 = offset;
2470 DBGIO(fd, (stderr,
"==> Fseek(%p,%ld,%d) %s\n", fd, (
long)offset, whence,
fdbg(fd)));
2473 return fseek(
fdGetFILE(fd), (
long)offset, whence);
2479 rc = (_seek ? _seek(fd, pos, whence) : -2);
2493 DBGIO(fd, (stderr,
"<== Ftell(%p) rc %ld %s\n", fd, rc,
fdbg(fd)));
2500 DBGIO(fd, (stderr,
"==> Rewind(%p) %s\n", fd,
fdbg(fd)));
2516 DBGIO(fd, (stderr,
"<== Fgetpos(%p,%p) rc %d %s\n", fd, pos, rc,
fdbg(fd)));
2530 DBGIO(fd, (stderr,
"<== Fsetpos(%p,%p) rc %d %s\n", fd, pos, rc,
fdbg(fd)));
2539 DBGIO(fd, (stderr,
"==> Fclose(%p) %s\n", (fd ? fd : NULL),
fdbg(fd)));
2542 fd =
fdLink(fd,
"Fclose");
2544 while (fd->
nfps >= 0) {
2556 if (fd->
nfps > 0 && fpno == -1 &&
2561 int hadreqpersist = (fd->
req != NULL);
2577 if (hadreqpersist) {
2604 fd =
fdFree(fd,
"fopencookie (Fclose)");
2614 if (fd == NULL || fd->
nfps == 0)
2620 fd =
fdFree(fd,
"Fclose");
2643 char *stdio,
size_t nstdio,
2644 char *other,
size_t nother,
2645 const char **end,
int * f)
2653 flags |= O_WRONLY | O_CREAT | O_APPEND;
2654 if (--nstdio > 0) *stdio++ = *m;
2657 flags |= O_WRONLY | O_CREAT | O_TRUNC;
2658 if (--nstdio > 0) *stdio++ = *m;
2662 if (--nstdio > 0) *stdio++ = *m;
2671 while ((c = *m++) !=
'\0') {
2676 flags &= ~(O_RDONLY|O_WRONLY);
2678 if (--nstdio > 0) *stdio++ = c;
2686 #if defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ >= 3
2687 if (--nstdio > 0) *stdio++ = c;
2692 if (--nstdio > 0) *stdio++ = c;
2696 if (--nother > 0) *other++ = c;
2704 *stdio = *other =
'\0';
2706 *end = (*m !=
'\0' ? m : NULL);
2712 #if defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ == 0
2714 typedef _IO_cookie_io_functions_t cookie_io_functions_t;
2720 char stdio[20], other[20], zstdio[40+1];
2721 const char *end = NULL;
2726 fprintf(stderr,
"*** Fdopen(%p,%s) %s\n", fd, fmode,
fdbg(fd));
2732 cvtfmode(fmode, stdio,
sizeof(stdio), other,
sizeof(other), &end, NULL);
2733 if (stdio[0] ==
'\0')
2738 if (end == NULL && other[0] ==
'\0')
2742 if (!strcmp(end,
"fdio")) {
2744 #if defined(WITH_ZLIB)
2745 }
else if (!strcmp(end,
"gzdio")) {
2748 fd = iof->
_fdopen(fd, zstdio);
2751 #if defined(WITH_BZIP2)
2752 }
else if (!strcmp(end,
"bzdio")) {
2755 fd = iof->
_fdopen(fd, zstdio);
2758 #if defined(WITH_XZ)
2759 }
else if (!strcmp(end,
"lzdio")) {
2761 fd = iof->
_fdopen(fd, zstdio);
2762 }
else if (!strcmp(end,
"xzdio")) {
2764 fd = iof->
_fdopen(fd, zstdio);
2766 }
else if (!strcmp(end,
"ufdio")) {
2768 }
else if (!strcmp(end,
"fpio")) {
2772 FILE * fp = fdopen(fdno, stdio);
2775 fprintf(stderr,
"*** Fdopen fpio fp %p\n", (
void *)fp);
2787 }
else if (other[0] !=
'\0') {
2788 for (end = other; *end && strchr(
"0123456789fh", *end); end++)
2791 #if defined(WITH_ZLIB)
2794 fd = iof->
_fdopen(fd, zstdio);
2806 { cookie_io_functions_t ciof;
2807 ciof.read = iof->
read;
2808 ciof.write = iof->
write;
2809 ciof.seek = iof->
seek;
2810 ciof.close = iof->
close;
2811 fp = fopencookie(fd, stdio, ciof);
2812 DBGIO(fd, (stderr,
"<-- fopencookie(%p,\"%s\",*%p) returns fp %p\n", fd, stdio, iof, fp));
2823 fd =
fdLink(fd,
"fopencookie");
2828 DBGIO(fd, (stderr,
"<== Fdopen(%p,\"%s\") returns fd %p %s\n", ofd, fmode, (fd ? fd : NULL),
fdbg(fd)));
2835 const char * fmode = NULL;
2836 char stdio[20], other[20];
2837 const char *end = NULL;
2838 mode_t perms = 0666;
2842 if (path == NULL || _fmode == NULL)
2849 fprintf(stderr,
"==> Fopen(%s, %s)\n", path, fmode);
2852 cvtfmode(fmode, stdio,
sizeof(stdio), other,
sizeof(other), &end, &flags);
2853 if (stdio[0] ==
'\0')
2856 if (end == NULL || !strcmp(end,
"fdio")) {
2857 fd =
fdOpen(path, flags, perms);
2880 fd =
ufdOpen(path, flags, perms);
2881 if (fd == NULL || !(
fdFileno(fd) >= 0 || fd->
req != NULL)) {
2896 if (isHTTP && ((fp = (FILE *)
fdGetFp(fd)) != NULL)
2911 fprintf(stderr,
"<== Fopen(%s, %s) fd %p\n", path, fmode, fd);
2912 fmode =
_free(fmode);
2919 if (fd == NULL)
return -1;
2926 #if defined(WITH_ZLIB)
2930 #if defined(WITH_BZIP2)
2934 #if defined(WITH_XZ)
2948 if (fd == NULL)
return -1;
2949 if (fd->
req != NULL) {
2953 for (i = fd->
nfps; rc == 0 && i >= 0; i--) {
2961 #if defined(WITH_ZLIB)
2962 }
else if (fps->
io ==
gzdio) {
2966 #if defined(WITH_BZIP2)
2967 }
else if (fps->
io ==
bzdio) {
2971 #if defined(WITH_XZ)
2972 }
else if (fps->
io ==
lzdio) {
2975 }
else if (fps->
io ==
xzdio) {
2987 DBGIO(fd, (stderr,
"<== Ferror(%p) rc %d %s\n", fd, rc,
fdbg(fd)));
2997 if (fd->
req != NULL)
3000 for (i = fd->
nfps ; rc == -1 && i >= 0; i--) {
3004 DBGIO(fd, (stderr,
"<== Fileno(%p) rc %d %s\n", (fd ? fd : NULL), rc,
fdbg(fd)));
3011 return fcntl(
Fileno(fd), op, lip);
3023 if (path == NULL || *path ==
'\0')
3025 d = (
char *)
alloca(strlen(path)+2);
3028 for (de = d; *de !=
'\0'; de++) {
3032 while (*de && *de !=
'/') de++;
3045 rc =
Mkdir(d, mode);
3049 if (!(uid == (uid_t) -1 && gid == (gid_t) -1)) {
3050 rc =
Chown(d, uid, gid);
3054 }
else if (!S_ISDIR(st.st_mode)) {
3062 path, (
unsigned)mode);
3066 #define _PATH "/bin:/usr/bin:/sbin:/usr/sbin"
3070 #define alloca_strdup(_s) strcpy((char *)alloca(strlen(_s)+1), (_s))
3082 if (FN == NULL || *FN ==
'\0')
3090 for (t = bn; t && *t; t++) {
3103 && strchr(
"Rr_", bn[0]) != NULL
3104 && strchr(
"Ww_", bn[1]) != NULL
3105 && strchr(
"Xx_", bn[2]) != NULL) {
3107 if (strchr(
"Rr", bn[0]) != NULL)
3109 if (strchr(
"Ww", bn[1]) != NULL)
3111 if (strchr(
"Xx", bn[2]) != NULL)
3115 }
else if (!strcmp(bn,
"exists"))
3117 else if (!strcmp(bn,
"executable"))
3119 else if (!strcmp(bn,
"readable"))
3121 else if (!strcmp(bn,
"writable"))
3125 te = bn + strlen(t) - 1;
3138 rc = (
Access(bn, mode) != 0 ? 1 : 0);
3140 fprintf(stderr,
"*** rpmioAccess(\"%s\", 0x%x) rc %d\n", bn, mode, rc);
3155 for (r =
alloca_strdup(path); r != NULL && *r !=
'\0'; r = re) {
3158 for (re = r; (re = strchr(re,
':')) != NULL; re++) {
3159 if (!(re[1] ==
'/' && re[2] ==
'/'))
3162 if (re && *re ==
':')
3171 if (r[0] ==
'~' && r[1] ==
'/') {
3172 const char * home =
getenv(
"HOME");
3175 if (strlen(home) > (
sizeof(fn) - strlen(r)))
3181 if (t[-1] !=
'/' && *bn !=
'/')
3189 rc = (
Access(t, mode) != 0 ? 1 : 0);
3191 fprintf(stderr,
"*** rpmioAccess(\"%s\", 0x%x) rc %d\n", t, mode, rc);
3204 #if defined(WITH_NSS) && !defined(__LCLINT__)
3209 extern void NSS_Shutdown(
void);
3216 int _rpmnss_init = 0;
3270 #if defined(WITH_LUA)
3273 #if defined(WITH_NEON)
3276 #if defined(WITH_NSS) && !defined(__LCLINT__)
3278 (void) NSS_Shutdown();
3298 _rpmficlI = rpmficlFree(_rpmficlI);
3301 _rpmgitI = rpmgitFree(_rpmgitI);
3304 _rpmaugI = rpmaugFree(_rpmaugI);
3306 _rpmmgoI = rpmmgoFree(_rpmmgoI);
3318 _rpmnixI = rpmnixFree(_rpmnixI);
3326 _rpmhkpI = rpmhkpFree(_rpmhkpI);
3328 _rpmhkp_awol.bf = rpmbfFree(_rpmhkp_awol.bf);
3329 _rpmhkp_crl.bf = rpmbfFree(_rpmhkp_crl.bf);
3334 _rpmsmI = rpmsmFree(_rpmsmI);