8 #if HAVE_MACHINE_TYPES_H
9 # include <machine/types.h>
13 # include <sys/socket.h>
16 #if defined(__LCLINT__)
24 struct sockaddr *ai_addr;
26 struct addrinfo *ai_next;
30 extern int getaddrinfo (__const
char *__restrict __name,
31 __const
char *__restrict __service,
32 __const
struct addrinfo *__restrict __req,
33 struct addrinfo **__restrict __pai)
36 extern int getnameinfo (__const
struct sockaddr *__restrict __sa,
37 socklen_t __salen,
char *__restrict __host,
38 socklen_t __hostlen,
char *__restrict __serv,
39 socklen_t __servlen,
unsigned int __flags)
42 extern void freeaddrinfo (
struct addrinfo *__ai)
49 #include <netinet/in.h>
50 #include <arpa/inet.h>
52 #if HAVE_NETINET_IN_SYSTM_H
53 # include <sys/types.h>
54 # include <netinet/in_systm.h>
59 #if HAVE_LIBIO_H && defined(_G_IO_IO_FILE_VERSION)
64 #if !defined(HAVE_HERRNO) && (defined(hpux) || defined(__hpux) || defined(__LCLINT__))
73 #define IPPORT_HTTP 80
76 #if !defined(HAVE_INET_ATON)
77 static int inet_aton(
const char *cp,
struct in_addr *inp)
83 if (addr == ((
long) -1))
return 0;
85 memcpy(inp, &addr,
sizeof(addr));
90 #if defined(USE_ALT_DNS) && USE_ALT_DNS
97 #define fdOpen __fdOpen
99 #define fdRead __fdRead
101 #define fdWrite __fdWrite
103 #define fdClose __fdClose
115 #define FDNREFS(fd) (fd ? ((FD_t)fd)->nrefs : -9)
116 #define FDTO(fd) (fd ? ((FD_t)fd)->rd_timeoutsecs : -99)
117 #define FDCPIOPOS(fd) (fd ? ((FD_t)fd)->fd_cpioPos : -99)
119 #define FDONLY(fd) assert(fdGetIo(fd) == fdio)
120 #define GZDONLY(fd) assert(fdGetIo(fd) == gzdio)
121 #define BZDONLY(fd) assert(fdGetIo(fd) == bzdio)
125 #define fdGetFILE(_fd) ((FILE *)fdGetFp(_fd))
136 #define TIMEOUT_SECS 60
163 static char buf[BUFSIZ];
172 sprintf(be,
"fd %p", fd); be += strlen(be);
183 strcpy(be,
" chunked");
187 for (i = fd->
nfps; i >= 0; i--) {
194 sprintf(be,
"FD %d fp %p", fps->
fdno, fps->
fp);
196 sprintf(be,
"UFD %d fp %p", fps->
fdno, fps->
fp);
198 sprintf(be,
"GZD %p fdno %d", fps->
fp, fps->
fdno);
201 sprintf(be,
"BZD %p fdno %d", fps->
fp, fps->
fdno);
204 sprintf(be,
"LZD %p fdno %d", fps->
fp, fps->
fdno);
205 }
else if (fps->
io ==
fpio) {
207 sprintf(be,
"%s %p(%d) fdno %d",
208 (fps->
fdno < 0 ?
"LIBIO" :
"FP"),
209 fps->
fp, fileno(((FILE *)fps->
fp)), fps->
fdno);
212 sprintf(be,
"??? io %p fp %p fdno %d ???",
229 DBGIO(0, (stderr,
"==>\tfdSize(%p) rc %ld\n", fd, (
long)rc));
237 if (fstat(
Fileno(fd), &sb) == 0)
255 if ((nfdno = dup(fdno)) < 0)
257 fd =
fdNew(
"open (fdDup)");
260 DBGIO(fd, (stderr,
"==> fdDup(%d) fd %p %s\n", fdno, (fd ? fd : NULL),
fdbg(fd)));
277 const char *
file,
unsigned line)
283 DBGREFS(0, (stderr,
"--> fd %p ++ %d %s at %s:%u\n", cookie,
FDNREFS(cookie)+1, msg, file, line));
288 DBGREFS(fd, (stderr,
"--> fd %p ++ %d %s at %s:%u %s\n", fd, fd->
nrefs, msg, file, line,
fdbg(fd)));
296 const char *
file,
unsigned line)
302 DBGREFS(0, (stderr,
"--> fd %p -- %d %s at %s:%u\n", fd,
FDNREFS(fd), msg, file, line));
305 DBGREFS(fd, (stderr,
"--> fd %p -- %d %s at %s:%u %s\n", fd, fd->
nrefs, msg, file, line,
fdbg(fd)));
310 for (i = fd->
ndigests - 1; i >= 0; i--) {
337 memset(fd->
fps, 0,
sizeof(fd->
fps));
340 fd->
fps[0].
fp = NULL;
347 #if defined(RPM_VENDOR_MANDRIVA)
368 return XfdLink(fd, msg, file, line);
371 static ssize_t
fdRead(
void * cookie,
char * buf,
size_t count)
390 DBGIO(fd, (stderr,
"==>\tfdRead(%p,%p,%ld) rc %ld %s\n", cookie, buf, (
long)count, (
long)rc,
fdbg(fd)));
395 static ssize_t
fdWrite(
void * cookie,
const char * buf,
size_t count)
407 if (count == 0)
return 0;
415 DBGIO(fd, (stderr,
"==>\tfdWrite(%p,%p,%ld) rc %ld %s\n", cookie, buf, (
long)count, (
long)rc,
fdbg(fd)));
424 #ifdef USE_COOKIE_SEEK_POINTER
425 _IO_off64_t p = *pos;
434 rc = lseek(
fdFileno(fd), p, whence);
437 DBGIO(fd, (stderr,
"==>\tfdSeek(%p,%ld,%d) rc %lx %s\n", cookie, (
long)p, whence, (
unsigned long)rc,
fdbg(fd)));
450 if (cookie == NULL)
return -2;
457 rc = ((fdno >= 0) ? close(fdno) : -2);
461 DBGIO(fd, (stderr,
"==>\tfdClose(%p) rc %lx %s\n", (fd ? fd : NULL), (
unsigned long)rc,
fdbg(fd)));
463 fd =
fdFree(fd,
"open (fdClose)");
474 fdno = open(path, flags, mode);
475 if (fdno < 0)
return NULL;
476 if (fcntl(fdno, F_SETFD, FD_CLOEXEC)) {
480 fd =
fdNew(
"open (fdOpen)");
484 DBGIO(fd, (stderr,
"==>\tfdOpen(\"%s\",%x,0%o) %s\n", path, (
unsigned)flags, (
unsigned)mode,
fdbg(fd)));
489 FILE *fdFdopen(
void * cookie,
const char *fmode)
495 if (fmode == NULL)
return NULL;
497 if (fdno < 0)
return NULL;
498 fp = fdopen(fdno, fmode);
499 DBGIO(fd, (stderr,
"==> fdFdopen(%p,\"%s\") fdno %d -> fp %p fdno %d\n", cookie, fmode, fdno, fp, fileno(fp)));
500 fd =
fdFree(fd,
"open (fdFdopen)");
518 int msecs = (secs >= 0 ? (1000 * secs) : -1);
521 struct timeval timeout, *tvp = (secs >= 0 ? &timeout : NULL);
532 wrfds.events = POLLOUT;
534 rc = poll(&wrfds, 1, msecs);
540 FD_SET(fdno, &wrfds);
542 rc = select(fdno + 1, NULL, &wrfds, NULL, tvp);
548 fprintf(stderr,
"*** fdWritable fdno %d rc %d %s\n", fdno, rc, strerror(
errno));
569 int msecs = (secs >= 0 ? (1000 * secs) : -1);
572 struct timeval timeout, *tvp = (secs >= 0 ? &timeout : NULL);
583 rdfds.events = POLLIN;
585 rc = poll(&rdfds, 1, msecs);
591 FD_SET(fdno, &rdfds);
593 rc = select(fdno + 1, &rdfds, NULL, NULL, tvp);
619 char lastchar =
'\0';
645 rc =
fdRead(fd, buf + nb, 1);
647 rc = read(
fdFileno(fd), buf + nb, 1);
659 fprintf(stderr,
"*** read: fd %p rc %d errno %d %s \"%s\"\n", fd, rc,
errno, strerror(
errno), buf);
662 }
else if (rc == 0) {
664 fprintf(stderr,
"*** read: fd %p rc %d EOF errno %d %s \"%s\"\n", fd, rc,
errno, strerror(
errno), buf);
669 lastchar = buf[nb - 1];
671 }
while (ec == 0 && nb < len && lastchar !=
'\n');
673 return (ec >= 0 ? nb : ec);
682 switch (errorNumber) {
688 return (
"NE_ERROR: Generic error.");
690 return (
"NE_LOOKUP: Hostname lookup failed.");
692 return (
"NE_AUTH: Server authentication failed.");
694 return (
"NE_PROXYAUTH: Proxy authentication failed.");
696 return (
"NE_CONNECT: Could not connect to server.");
698 return (
"NE_TIMEOUT: Connection timed out.");
700 return (
"NE_FAILED: The precondition failed.");
702 return (
"NE_RETRY: Retry request.");
704 return (
"NE_REDIRECT: Redirect received.");
707 return _(
"Bad server response");
709 return _(
"Server I/O error");
711 return _(
"Server timeout");
713 return _(
"Unable to lookup server host address");
715 return _(
"Unable to lookup server host name");
717 return _(
"Failed to connect to server");
719 return _(
"Failed to establish data connection to server");
721 return _(
"I/O error to local file");
723 return _(
"Error setting remote server to passive mode");
725 return _(
"File not found on server");
727 return _(
"Abort in progress");
731 return _(
"Unknown or unexpected error");
749 retstr =
_(
"Malformed URL");
752 retstr = strerror(
errno);
759 #if !defined(HAVE_GETADDRINFO)
760 #if !defined(USE_ALT_DNS) || !USE_ALT_DNS
762 struct in_addr * address)
766 struct hostent * hostinfo;
769 hostinfo = gethostbyname(host);
771 if (!hostinfo)
return 1;
774 memcpy(address, hostinfo->h_addr_list[0],
sizeof(*address));
787 if (!strcmp(host,
"localhost")) {
818 #ifdef HAVE_GETADDRINFO
820 struct addrinfo hints, *res, *res0;
821 char pbuf[NI_MAXSERV];
824 memset(&hints, 0,
sizeof(hints));
825 hints.ai_family = AF_UNSPEC;
826 hints.ai_socktype = SOCK_STREAM;
827 sprintf(pbuf,
"%d", port);
828 pbuf[
sizeof(pbuf)-1] =
'\0';
830 if (getaddrinfo(host, pbuf, &hints, &res0) == 0) {
831 for (res = res0; res != NULL; res = res->ai_next) {
832 if ((fdno = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) < 0)
834 if (connect(fdno, res->ai_addr, res->ai_addrlen) < 0) {
841 char hbuf[NI_MAXHOST];
843 xx = getnameinfo(res->ai_addr, res->ai_addrlen, hbuf,
sizeof(hbuf),
844 NULL, 0, NI_NUMERICHOST);
845 fprintf(stderr,
"++ connect [%s]:%d on fdno %d\n",
856 struct sockaddr_in sin;
859 memset(&sin, 0,
sizeof(sin));
861 sin.sin_family = AF_INET;
862 sin.sin_port = htons(port);
863 sin.sin_addr.s_addr = INADDR_ANY;
869 if ((fdno = socket(sin.sin_family, SOCK_STREAM, IPPROTO_IP)) < 0) {
875 if (connect(fdno, (
struct sockaddr *) &sin,
sizeof(sin))) {
886 fprintf(stderr,
"++ connect %s:%d on fdno %d\n",
888 inet_ntoa(sin.sin_addr)
890 (
int)ntohs(sin.sin_port), fdno);
893 fdSetFdno(ctrl, (fdno >= 0 ? fdno : -1));
907 int *ecp,
char ** str)
938 se = buf + bufLength;
940 rc =
fdFgets(ctrl, se, (bufAlloced - bufLength));
944 }
else if (rc == 0 ||
fdWritable(ctrl, 0) < 1)
950 for (s = se; *s !=
'\0'; s = se) {
953 while (*se && *se !=
'\n') se++;
955 if (se > s && se[-1] ==
'\r')
961 fprintf(stderr,
"<- %s\n", s);
971 if (!strncmp(s,
"HTTP",
sizeof(
"HTTP")-1)) {
973 if ((e = strchr(s,
'.')) != NULL) {
981 if ((e = strchr(s,
' ')) != NULL) {
983 if (strchr(
"0123456789", *e))
984 strncpy(errorCode, e, 3);
991 for (e = s; *e && !(*e ==
' ' || *e ==
':'); e++)
993 if (e > s && *e++ ==
':') {
995 while (*e && *e ==
' ') e++;
997 if (!strncmp(s,
"Date:", ne)) {
999 if (!strncmp(s,
"Server:", ne)) {
1001 if (!strncmp(s,
"Last-Modified:", ne)) {
1003 if (!strncmp(s,
"ETag:", ne)) {
1006 if (!strncmp(s,
"Accept-Ranges:", ne)) {
1007 if (!strcmp(e,
"bytes"))
1009 if (!strcmp(e,
"none"))
1012 if (!strncmp(s,
"Content-Length:", ne)) {
1013 if (strchr(
"0123456789", *e))
1016 if (!strncmp(s,
"Connection:", ne)) {
1017 if (!strcmp(e,
"close"))
1022 if (!strncmp(s,
"Content-Type:", ne)) {
1024 if (!strncmp(s,
"Transfer-Encoding:", ne)) {
1025 if (!strcmp(e,
"chunked"))
1030 if (!strncmp(s,
"Allow:", ne)) {
1037 if (!strncmp(s,
"<TITLE>",
sizeof(
"<TITLE>")-1))
1038 s +=
sizeof(
"<TITLE>") - 1;
1041 if (strchr(
"0123456789", *s)) {
1042 if (errorCode[0] !=
'\0') {
1043 if (!strncmp(s, errorCode,
sizeof(
"123")-1) && s[3] ==
' ')
1046 strncpy(errorCode, s,
sizeof(
"123")-1);
1047 errorCode[3] =
'\0';
1054 if (moretodo && se > s) {
1055 bufLength = se - s - 1;
1057 memmove(buf, s, bufLength);
1061 }
while (moretodo && ec == 0);
1063 if (str) *str = buf;
1064 if (ecp) *ecp = atoi(errorCode);
1088 if (ec >= 400 && ec <= 599) {
1102 const char * s, * t;
1108 while ((s = va_arg(ap,
const char *)) != NULL) {
1112 len +=
sizeof(
"\r\n")-1;
1116 t = te =
alloca(len + 1);
1119 while ((s = va_arg(ap,
const char *)) != NULL) {
1120 if (te > t) *te++ =
' ';
1128 fprintf(stderr,
"-> %s", t);
1142 const char * password;
1162 if ((password = u->
password) == NULL) {
1163 uid_t uid = getuid();
1165 if (uid && (pw = getpwuid(uid)) != NULL) {
1167 char *myp =
alloca(strlen(pw->pw_name) +
sizeof(
"@"));
1168 strcpy(myp, pw->pw_name);
1193 if ((rc =
ftpCommand(u, NULL,
"USER", user, NULL)))
1196 if ((rc =
ftpCommand(u, NULL,
"PASS", password, NULL)))
1199 if ((rc =
ftpCommand(u, NULL,
"TYPE",
"I", NULL)))
1224 #if !defined(HAVE_GETADDRINFO)
1225 struct sockaddr_in dataAddress;
1227 char remoteIP[NI_MAXHOST];
1242 cmdlen = strlen(ftpCmd) + (ftpArg ? 1+strlen(ftpArg) : 0) +
sizeof(
"\r\n");
1243 chptr = cmd =
alloca(cmdlen);
1244 chptr =
stpcpy(chptr, ftpCmd);
1247 chptr =
stpcpy(chptr, ftpArg);
1249 chptr =
stpcpy(chptr,
"\r\n");
1250 cmdlen = chptr - cmd;
1255 if (!strncmp(cmd,
"RETR", 4)) {
1259 rc =
ftpCommand(u, &passReply,
"SIZE", ftpArg, NULL);
1262 if (sscanf(passReply,
"%d %u", &rc, &cl) != 2) {
1272 #ifdef HAVE_GETNAMEINFO
1273 rc =
ftpCommand(u, &passReply,
"EPSV", NULL);
1275 #ifdef HAVE_GETADDRINFO
1276 struct sockaddr_storage ss;
1278 struct sockaddr_in ss;
1280 socklen_t sslen =
sizeof(ss);
1283 if ((getpeername(
fdFileno(
c2f(u->
ctrl)), (
struct sockaddr *)&ss, &sslen) == 0)
1284 && (getnameinfo((
struct sockaddr *)&ss, sslen,
1285 remoteIP,
sizeof(remoteIP),
1286 NULL, 0, NI_NUMERICHOST) == 0))
1291 rc =
ftpCommand(u, &passReply,
"ABOR", NULL);
1300 rc =
ftpCommand(u, &passReply,
"PASV", NULL);
1307 while (*chptr && *chptr !=
'(') chptr++;
1311 while (*chptr && *chptr !=
')') chptr++;
1317 if(sscanf(passReply,
"%*c%*c%*c%d%*c",&i) != 1) {
1324 while (*chptr && *chptr !=
',') chptr--;
1327 while (*chptr && *chptr !=
',') chptr--;
1335 if (sscanf(chptr,
"%d,%d", &i, &j) != 2) {
1339 port = (((unsigned)i) << 8) + j;
1343 while (*chptr++ !=
'\0') {
1344 if (*chptr ==
',') *chptr =
'.';
1347 sprintf(remoteIP,
"%s", passReply);
1350 #ifdef HAVE_GETADDRINFO
1353 struct addrinfo hints, *res, *res0;
1354 char pbuf[NI_MAXSERV];
1357 memset(&hints, 0,
sizeof(hints));
1358 hints.ai_family = AF_UNSPEC;
1359 hints.ai_socktype = SOCK_STREAM;
1360 hints.ai_flags = AI_NUMERICHOST;
1362 hints.ai_flags |= AI_IDN;
1364 sprintf(pbuf,
"%d", port);
1365 pbuf[
sizeof(pbuf)-1] =
'\0';
1366 if (getaddrinfo(remoteIP, pbuf, &hints, &res0)) {
1371 for (res = res0; res != NULL; res = res->ai_next) {
1372 rc = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
1383 data =
fdLink(data,
"open data (ftpReq)");
1391 while (connect(
fdFileno(data), res->ai_addr, res->ai_addrlen) < 0) {
1417 memset(&dataAddress, 0,
sizeof(dataAddress));
1418 dataAddress.sin_family = AF_INET;
1419 dataAddress.sin_port = htons(port);
1422 if (!
inet_aton(remoteIP, &dataAddress.sin_addr)) {
1428 rc = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
1434 data =
fdLink(data,
"open data (ftpReq)");
1441 while (connect(
fdFileno(data), (
struct sockaddr *) &dataAddress,
1442 sizeof(dataAddress)) < 0)
1453 fprintf(stderr,
"-> %s", cmd);
1490 urlNotifyData = notifyData;
1491 urlNotifyCount = (notifyCount >= 0) ? notifyCount : 4096;
1498 int itemsCopied = 0;
1512 rc =
Fread(buf,
sizeof(buf[0]),
sizeof(buf), sfd);
1520 rc =
Fwrite(buf,
sizeof(buf[0]), itemsRead, tfd);
1523 if (rc != itemsRead) {
1528 itemsCopied += itemsRead;
1529 if (urlNotify && urlNotifyCount > 0) {
1531 if (n != notifier) {
1543 DBGIO(sfd, (stderr,
"++ copied %d bytes: %s\n", itemsCopied,
1571 if ((fd = u->
ctrl) == NULL) {
1572 fd = u->
ctrl =
fdNew(
"persist ctrl (urlConnect FTP)");
1581 fd =
fdLink(fd,
"grab ctrl (urlConnect FTP)");
1590 u->
ctrl =
fdFree(fd,
"grab ctrl (urlConnect FTP)");
1598 *uret =
urlLink(u,
"urlConnect");
1600 u =
urlFree(u,
"urlSplit (urlConnect)");
1618 int ftpCmd(
const char * cmd,
const char * url,
const char * arg2)
1629 rc =
ftpCommand(u, NULL, cmd, path, arg2, NULL);
1644 #if !defined(SHUT_RDWR)
1645 #define SHUT_RDWR 1+1
1652 static unsigned char ipbuf[3] = {
IAC,
IP,
IAC };
1667 DBGIO(0, (stderr,
"-> ABOR\n"));
1670 if (send(
fdFileno(ctrl), ipbuf,
sizeof(ipbuf), MSG_OOB) !=
sizeof(ipbuf)) {
1675 sprintf(u->
buf,
"%cABOR\r\n",(
char)
DM);
1730 static int httpResp(
urlinfo u,
FD_t ctrl,
char ** str)
1740 if (
_ftp_debug && !(rc == 0 && (ec == 200 || ec == 201)))
1741 fprintf(stderr,
"*** httpResp: rc %d ec %d\n", rc, ec);
1759 static int httpReq(
FD_t ctrl,
const char * httpCmd,
const char * httpArg)
1766 char hthost[NI_MAXHOST];
1773 assert(ctrl != NULL);
1779 if (strchr(host,
':'))
1780 sprintf(hthost,
"[%s]", host);
1782 strcpy(hthost, host);
1787 if (path == NULL) path =
"";
1802 ctrl =
fdLink(ctrl,
"open ctrl (httpReq)");
1807 User-Agent: rpm/3.0.4\r\n\
1809 Accept: text/plain\r\n\
1810 Transfer-Encoding: chunked\r\n\
1812 ") + strlen(httpCmd) + strlen(path) +
sizeof(
VERSION) + strlen(hthost) + 20;
1818 if (!strcmp(httpCmd,
"PUT")) {
1820 %s %s HTTP/1.%d\r\n\
1821 User-Agent: rpm/%s\r\n\
1823 Accept: text/plain\r\n\
1824 Transfer-Encoding: chunked\r\n\
1829 %s %s HTTP/1.%d\r\n\
1830 User-Agent: rpm/%s\r\n\
1832 Accept: text/plain\r\n\
1839 fprintf(stderr,
"-> %s", req);
1842 if (
fdWrite(ctrl, req, len) != len) {
1848 if (!strcmp(httpCmd,
"PUT")) {
1852 rc = httpResp(u, ctrl, NULL);
1865 ctrl =
fdLink(ctrl,
"open data (httpReq)");
1886 if (fd->
url == NULL)
1892 static ssize_t
ufdRead(
void * cookie,
char * buf,
size_t count)
1906 (void) fstat(fdno, &sb);
1907 if (S_ISREG(sb.st_mode))
1908 return fdRead(fd, buf, count);
1914 for (total = 0; total < count; total += bytesRead) {
1934 rc =
fdRead(fd, buf + total, count - total);
1946 fprintf(stderr,
"*** read: rc %d errno %d %s \"%s\"\n", rc,
errno, strerror(
errno), buf);
1949 }
else if (rc == 0) {
1959 static ssize_t
ufdWrite(
void * cookie,
const char * buf,
size_t count)
1971 if (S_ISREG(sb.st_mode))
1972 return fdWrite(fd, buf, count);
1978 for (total = 0; total < count; total += bytesWritten) {
1986 fprintf(stderr,
"*** ufdWrite fd %p WRITE PAST END OF CONTENT\n", fd);
2000 rc =
fdWrite(fd, buf + total, count - total);
2011 fprintf(stderr,
"*** write: rc %d errno %d %s \"%s\"\n", rc,
errno, strerror(
errno), buf);
2014 }
else if (rc == 0) {
2043 return fdSeek(cookie, pos, whence);
2059 fd = u->
data =
fdFree(fd,
"grab data (ufdClose persist)");
2061 fd =
fdFree(fd,
"grab data (ufdClose)");
2119 && (!strncmp(u->
scheme,
"http",
sizeof(
"http")-1) || !strncmp(u->
scheme,
"hkp",
sizeof(
"hkp")-1)))
2131 fd = u->
ctrl =
fdFree(fd,
"open data (ufdClose HTTP persist ctrl)");
2132 else if (fd == u->
data)
2133 fd = u->
data =
fdFree(fd,
"open data (ufdClose HTTP persist data)");
2135 fd =
fdFree(fd,
"open data (ufdClose HTTP)");
2170 assert(!(flags & O_RDWR));
2175 if (u->
data == NULL)
2176 u->
data =
fdNew(
"persist data (ftpOpen)");
2179 fd =
fdLink(u->
data,
"grab data (ftpOpen persist data)");
2181 fd =
fdNew(
"grab data (ftpOpen)");
2215 fprintf(stderr,
"*** ufdOpen(%s,0x%x,0%o)\n", url, (
unsigned)flags, (
unsigned)mode);
2220 fd =
ftpOpen(url, flags, mode, &u);
2221 if (fd == NULL || u == NULL)
2225 cmd = ((flags & O_WRONLY)
2226 ? ((flags & O_APPEND) ?
"APPE" :
2227 ((flags & O_CREAT) ?
"STOR" :
"STOR"))
2228 : ((flags & O_CREAT) ?
"STOR" :
"RETR"));
2232 fd =
fdLink(fd,
"error data (ufdOpen FTP)");
2240 assert(!(flags & O_RDWR));
2241 fd =
fdDup( ((flags & O_WRONLY) ? STDOUT_FILENO : STDIN_FILENO) );
2252 fd =
fdOpen(path, flags, mode);
2255 #if defined(RPM_VENDOR_MANDRIVA)
2270 if (fd == NULL)
return NULL;
2276 DBGIO(fd, (stderr,
"==>\tufdOpen(\"%s\",%x,0%o) %s\n", url, (
unsigned)flags, (
unsigned)mode,
fdbg(fd)));
2298 static inline void * gzdFileno(
FD_t fd)
2305 for (i = fd->
nfps; i >= 0; i--) {
2319 FD_t gzdOpen(
const char * path,
const char * fmode)
2325 mode_t mode = (fmode && fmode[0] ==
'w' ? O_WRONLY : O_RDONLY);
2327 if ((gzfile = gzopen(path, fmode)) == NULL)
2329 fd =
fdNew(
"open (gzdOpen)");
2333 DBGIO(fd, (stderr,
"==>\tgzdOpen(\"%s\", \"%s\") fd %p %s\n", path, fmode, (fd ? fd : NULL),
fdbg(fd)));
2334 return fdLink(fd,
"gzdOpen");
2337 static FD_t gzdFdopen(
void * cookie,
const char *fmode)
2345 if (fmode == NULL)
return NULL;
2348 if (fdno < 0)
return NULL;
2349 gzfile = gzdopen(fdno, fmode);
2350 if (gzfile == NULL)
return NULL;
2352 fdPush(fd, gzdio, gzfile, fdno);
2354 return fdLink(fd,
"gzdFdopen");
2357 static int gzdFlush(
FD_t fd)
2362 gzfile = gzdFileno(fd);
2363 if (gzfile == NULL)
return -2;
2364 return gzflush(gzfile, Z_SYNC_FLUSH);
2368 static ssize_t gzdRead(
void * cookie,
char * buf,
size_t count)
2378 gzfile = gzdFileno(fd);
2379 if (gzfile == NULL)
return -2;
2382 rc = gzread(gzfile, buf, count);
2383 DBGIO(fd, (stderr,
"==>\tgzdRead(%p,%p,%u) rc %lx %s\n", cookie, buf, (
unsigned)count, (
unsigned long)rc,
fdbg(fd)));
2386 fd->
errcookie = gzerror(gzfile, &zerror);
2387 if (zerror == Z_ERRNO) {
2391 }
else if (rc >= 0) {
2398 static ssize_t gzdWrite(
void * cookie,
const char * buf,
size_t count)
2410 gzfile = gzdFileno(fd);
2411 if (gzfile == NULL)
return -2;
2414 rc = gzwrite(gzfile, (
void *)buf, count);
2415 DBGIO(fd, (stderr,
"==>\tgzdWrite(%p,%p,%u) rc %lx %s\n", cookie, buf, (
unsigned)count, (
unsigned long)rc,
fdbg(fd)));
2418 fd->
errcookie = gzerror(gzfile, &zerror);
2419 if (zerror == Z_ERRNO) {
2423 }
else if (rc > 0) {
2430 static inline int gzdSeek(
void * cookie,
_libio_pos_t pos,
int whence)
2434 #ifdef USE_COOKIE_SEEK_POINTER
2435 _IO_off64_t p = *pos;
2444 if (fd == NULL)
return -2;
2447 gzfile = gzdFileno(fd);
2448 if (gzfile == NULL)
return -2;
2451 rc = gzseek(gzfile, p, whence);
2452 DBGIO(fd, (stderr,
"==>\tgzdSeek(%p,%ld,%d) rc %lx %s\n", cookie, (
long)p, whence, (
unsigned long)rc,
fdbg(fd)));
2455 fd->
errcookie = gzerror(gzfile, &zerror);
2456 if (zerror == Z_ERRNO) {
2460 }
else if (rc >= 0) {
2469 static int gzdClose(
void * cookie)
2477 gzfile = gzdFileno(fd);
2478 if (gzfile == NULL)
return -2;
2482 rc = gzclose(gzfile);
2488 DBGIO(fd, (stderr,
"==>\tgzdClose(%p) zerror %d %s\n", cookie, rc,
fdbg(fd)));
2491 if (rc == Z_ERRNO) {
2495 }
else if (rc >= 0) {
2500 DBGIO(fd, (stderr,
"==>\tgzdClose(%p) rc %lx %s\n", cookie, (
unsigned long)rc,
fdbg(fd)));
2505 fd =
fdFree(fd,
"open (gzdClose)");
2511 static struct FDIO_s gzdio_s = {
2512 gzdRead, gzdWrite, gzdSeek, gzdClose, gzdOpen, gzdFdopen,
2529 static inline void * bzdFileno(
FD_t fd)
2536 for (i = fd->
nfps; i >= 0; i--) {
2540 if (fps->
io != bzdio)
2550 static FD_t bzdOpen(
const char * path,
const char * fmode)
2556 mode_t mode = (fmode && fmode[0] ==
'w' ? O_WRONLY : O_RDONLY);
2558 if ((bzfile = BZ2_bzopen(path, fmode)) == NULL)
2560 fd =
fdNew(
"open (bzdOpen)");
2563 return fdLink(fd,
"bzdOpen");
2568 static FD_t bzdFdopen(
void * cookie,
const char * fmode)
2576 if (fmode == NULL)
return NULL;
2579 if (fdno < 0)
return NULL;
2580 bzfile = BZ2_bzdopen(fdno, fmode);
2581 if (bzfile == NULL)
return NULL;
2583 fdPush(fd, bzdio, bzfile, fdno);
2585 return fdLink(fd,
"bzdFdopen");
2590 static int bzdFlush(
FD_t fd)
2594 return BZ2_bzflush(bzdFileno(fd));
2601 static ssize_t bzdRead(
void * cookie,
char * buf,
size_t count)
2610 bzfile = bzdFileno(fd);
2614 rc = BZ2_bzread(bzfile, buf, count);
2619 fd->
errcookie = BZ2_bzerror(bzfile, &zerror);
2620 }
else if (rc >= 0) {
2632 static ssize_t bzdWrite(
void * cookie,
const char * buf,
size_t count)
2644 bzfile = bzdFileno(fd);
2646 rc = BZ2_bzwrite(bzfile, (
void *)buf, count);
2649 fd->
errcookie = BZ2_bzerror(bzfile, &zerror);
2650 }
else if (rc > 0) {
2657 static inline int bzdSeek(
void * cookie,
_libio_pos_t pos,
2667 static int bzdClose(
void * cookie)
2675 bzfile = bzdFileno(fd);
2677 if (bzfile == NULL)
return -2;
2680 BZ2_bzclose(bzfile);
2689 fd->
errcookie = BZ2_bzerror(bzfile, &zerror);
2690 }
else if (rc >= 0) {
2695 DBGIO(fd, (stderr,
"==>\tbzdClose(%p) rc %lx %s\n", cookie, (
unsigned long)rc,
fdbg(fd)));
2700 fd =
fdFree(fd,
"open (bzdClose)");
2706 static struct FDIO_s bzdio_s = {
2707 bzdRead, bzdWrite, bzdSeek, bzdClose, bzdOpen, bzdFdopen,
2721 const char *errstr = NULL;
2754 #define FDIOVEC(_fd, _vec) \
2755 ((fdGetIo(_fd) && fdGetIo(_fd)->_vec) ? fdGetIo(_fd)->_vec : NULL)
2757 size_t Fread(
void *buf,
size_t size,
size_t nmemb,
FD_t fd) {
2762 DBGIO(fd, (stderr,
"==> Fread(%p,%u,%u,%p) %s\n", buf, (
unsigned)size, (
unsigned)nmemb, (fd ? fd : NULL),
fdbg(fd)));
2766 rc = fread(buf, size, nmemb,
fdGetFILE(fd));
2775 rc = (_read ? (*_read) (fd, buf, size * nmemb) : -2);
2779 size_t Fwrite(
const void *buf,
size_t size,
size_t nmemb,
FD_t fd)
2785 DBGIO(fd, (stderr,
"==> Fwrite(%p,%u,%u,%p) %s\n", buf, (
unsigned)size, (
unsigned)nmemb, (fd ? fd : NULL),
fdbg(fd)));
2790 rc = fwrite(buf, size, nmemb,
fdGetFILE(fd));
2800 rc = (_write ? _write(fd, buf, size * nmemb) : -2);
2806 #ifdef USE_COOKIE_SEEK_POINTER
2807 _IO_off64_t o64 = offset;
2816 DBGIO(fd, (stderr,
"==> Fseek(%p,%ld,%d) %s\n", fd, (
long)offset, whence,
fdbg(fd)));
2823 rc = fseek(fp, offset, whence);
2832 rc = (_seek ? _seek(fd, pos, whence) : -2);
2841 DBGIO(fd, (stderr,
"==> Fclose(%p) %s\n", (fd ? fd : NULL),
fdbg(fd)));
2843 fd =
fdLink(fd,
"Fclose");
2845 while (fd->
nfps >= 0) {
2859 if (fd->
nfps > 0 && fpno == -1 &&
2885 fd =
fdFree(fd,
"fopencookie (Fclose)");
2902 fd =
fdFree(fd,
"Fclose");
2924 char *stdio,
size_t nstdio,
2925 char *other,
size_t nother,
2926 const char **end,
int * f)
2934 flags |= O_WRONLY | O_CREAT | O_APPEND;
2935 if (--nstdio > 0) *stdio++ = *m;
2938 flags |= O_WRONLY | O_CREAT | O_TRUNC;
2939 if (--nstdio > 0) *stdio++ = *m;
2943 if (--nstdio > 0) *stdio++ = *m;
2952 while ((c = *m++) !=
'\0') {
2957 flags &= ~(O_RDONLY|O_WRONLY);
2959 if (--nstdio > 0) *stdio++ = c;
2967 #if defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ >= 3
2968 if (--nstdio > 0) *stdio++ = c;
2973 if (--nstdio > 0) *stdio++ = c;
2977 if (--nother > 0) *other++ = c;
2984 *stdio = *other =
'\0';
2986 *end = (*m !=
'\0' ? m : NULL);
2993 #if defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ == 0
2995 typedef _IO_cookie_io_functions_t cookie_io_functions_t;
3002 char stdio[20], other[20], zstdio[20];
3003 const char *end = NULL;
3008 fprintf(stderr,
"*** Fdopen(%p,%s) %s\n", fd, fmode,
fdbg(fd));
3014 cvtfmode(fmode, stdio,
sizeof(stdio), other,
sizeof(other), &end, NULL);
3015 if (stdio[0] ==
'\0')
3018 strncat(zstdio, stdio,
sizeof(zstdio) - strlen(zstdio));
3019 strncat(zstdio, other,
sizeof(zstdio) - strlen(zstdio));
3021 if (end == NULL && other[0] ==
'\0')
3026 if (!strcmp(end,
"fdio")) {
3028 }
else if (!strcmp(end,
"gzdio")) {
3031 fd = iof->
_fdopen(fd, zstdio);
3034 }
else if (!strcmp(end,
"bzdio")) {
3037 fd = iof->
_fdopen(fd, zstdio);
3040 }
else if (!strcmp(end,
"lzdio")) {
3042 fd = iof->
_fdopen(fd, zstdio);
3043 }
else if (!strcmp(end,
"ufdio")) {
3045 }
else if (!strcmp(end,
"fpio")) {
3049 FILE * fp = fdopen(fdno, stdio);
3052 fprintf(stderr,
"*** Fdopen fpio fp %p\n", (
void *)fp);
3064 }
else if (other[0] !=
'\0') {
3065 for (end = other; *end && strchr(
"0123456789fh", *end); end++)
3070 fd = iof->
_fdopen(fd, zstdio);
3082 { cookie_io_functions_t ciof;
3083 ciof.read = iof->
read;
3084 ciof.write = iof->
write;
3085 ciof.seek = iof->
seek;
3086 ciof.close = iof->
close;
3087 fp = fopencookie(fd, stdio, ciof);
3088 DBGIO(fd, (stderr,
"==> fopencookie(%p,\"%s\",*%p) returns fp %p\n", fd, stdio, iof, fp));
3100 fd =
fdLink(fd,
"fopencookie");
3105 DBGIO(fd, (stderr,
"==> Fdopen(%p,\"%s\") returns fd %p %s\n", ofd, fmode, (fd ? fd : NULL),
fdbg(fd)));
3112 char stdio[20], other[20];
3113 const char *end = NULL;
3114 mode_t perms = 0666;
3118 if (path == NULL || fmode == NULL)
3122 cvtfmode(fmode, stdio,
sizeof(stdio), other,
sizeof(other), &end, &flags);
3123 if (stdio[0] ==
'\0')
3127 if (end == NULL || !strcmp(end,
"fdio")) {
3129 fprintf(stderr,
"*** Fopen fdio path %s fmode %s\n", path, fmode);
3130 fd =
fdOpen(path, flags, perms);
3153 fprintf(stderr,
"*** Fopen ufdio path %s fmode %s\n", path, fmode);
3154 fd =
ufdOpen(path, flags, perms);
3155 if (fd == NULL || !(
fdFileno(fd) >= 0))
3160 fprintf(stderr,
"*** Fopen WTFO path %s fmode %s\n", path, fmode);
3166 if (isHTTP && ((fp =
fdGetFp(fd)) != NULL) && ((fdno =
fdGetFdno(fd)) >= 0))
3186 if (fd == NULL)
return -1;
3193 if (vh &&
fdGetIo(fd) == gzdio)
3194 return gzdFlush(vh);
3196 if (vh &&
fdGetIo(fd) == bzdio)
3197 return bzdFlush(vh);
3207 if (fd == NULL)
return -1;
3208 for (i = fd->
nfps; rc == 0 && i >= 0; i--) {
3218 }
else if (fps->
io == gzdio) {
3222 }
else if (fps->
io == bzdio) {
3226 }
else if (fps->
io ==
lzdio) {
3237 DBGIO(fd, (stderr,
"==> Ferror(%p) rc %d %s\n", fd, rc,
fdbg(fd)));
3245 for (i = fd->
nfps ; rc == -1 && i >= 0; i--) {
3251 DBGIO(fd, (stderr,
"==> Fileno(%p) rc %d %s\n", (fd ? fd : NULL), rc,
fdbg(fd)));
3258 return fcntl(
Fileno(fd), op, lip);
3273 d =
alloca(strlen(path)+2);
3276 for (de = d; *de !=
'\0'; de++) {
3280 while (*de && *de !=
'/') de++;
3293 rc =
Mkdir(d, mode);
3297 if (!(uid == (uid_t) -1 && gid == (gid_t) -1)) {
3298 rc = chown(d, uid, gid);
3302 }
else if (!S_ISDIR(st.st_mode)) {
3316 #define _PATH "/usr/kerberos/sbin:/usr/kerberos/bin:/usr/lib/ccache/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:~/bin"
3320 #define alloca_strdup(_s) strcpy(alloca(strlen(_s)+1), (_s))
3332 if (FN == NULL || *FN ==
'\0')
3340 for (t = bn; t && *t; t++) {
3353 && strchr(
"Rr_", bn[0]) != NULL
3354 && strchr(
"Ww_", bn[1]) != NULL
3355 && strchr(
"Xx_", bn[2]) != NULL) {
3357 if (strchr(
"Rr", bn[0]) != NULL)
3359 if (strchr(
"Ww", bn[1]) != NULL)
3361 if (strchr(
"Xx", bn[2]) != NULL)
3365 }
else if (!strcmp(bn,
"exists"))
3367 else if (!strcmp(bn,
"executable"))
3369 else if (!strcmp(bn,
"readable"))
3371 else if (!strcmp(bn,
"writable"))
3375 te = bn + strlen(t) - 1;
3388 rc = (
Access(bn, mode) != 0 ? 1 : 0);
3390 fprintf(stderr,
"*** rpmioAccess(\"%s\", 0x%x) rc %d\n", bn, mode, rc);
3406 for (r =
alloca_strdup(path); r != NULL && *r !=
'\0'; r = re) {
3409 for (re = r; (re = strchr(re,
':')) != NULL; re++) {
3410 if (!(re[1] ==
'/' && re[2] ==
'/'))
3413 if (re && *re ==
':')
3422 if (r[0] ==
'~' && r[1] ==
'/') {
3423 const char * home =
getenv(
"HOME");
3426 if (strlen(home) > (
sizeof(fn) - strlen(r)))
3432 if (t[-1] !=
'/' && *bn !=
'/')
3440 rc = (
Access(t, mode) != 0 ? 1 : 0);
3442 fprintf(stderr,
"*** rpmioAccess(\"%s\", 0x%x) rc %d\n", t, mode, rc);
3459 static ssize_t blenmax = (32 * BUFSIZ);
3466 fd =
Fopen(fn,
"r.ufdio");
3467 if (fd == NULL ||
Ferror(fd)) {
3473 blen = (size >= 0 ? size : blenmax);
3479 nb =
Fread(b,
sizeof(*b), blen, fd);
3480 if (
Ferror(fd) || (size > 0 && nb != blen)) {
3484 if (blen == blenmax && nb < blen) {
3493 if (fd) (void)
Fclose(fd);
3502 else if (b) free(b);
3504 if (blenp) *blenp = blen;