diff options
author | joerg <joerg@pkgsrc.org> | 2008-04-02 15:33:14 +0000 |
---|---|---|
committer | joerg <joerg@pkgsrc.org> | 2008-04-02 15:33:14 +0000 |
commit | 586f71a7c1cac1420c1c8907dcc9ce49e967334a (patch) | |
tree | e46e301164ebf52c61e36f756381d08430abbb91 | |
parent | 38a70df40141965befabeec5d475c5d79693eb2c (diff) | |
download | pkgsrc-586f71a7c1cac1420c1c8907dcc9ce49e967334a.tar.gz |
libfetch-2.1:
- remove most of the debug junk
- fix a buffer overflow in the config parser
- replace stdio usage with a simple abstract IO framework. currently
without explicit buffering, but that might be added later
-rw-r--r-- | net/libfetch/Makefile | 8 | ||||
-rw-r--r-- | net/libfetch/PLIST | 3 | ||||
-rw-r--r-- | net/libfetch/buildlink3.mk | 4 | ||||
-rw-r--r-- | net/libfetch/files/common.c | 64 | ||||
-rw-r--r-- | net/libfetch/files/common.h | 14 | ||||
-rw-r--r-- | net/libfetch/files/fetch.3 | 32 | ||||
-rw-r--r-- | net/libfetch/files/fetch.c | 28 | ||||
-rw-r--r-- | net/libfetch/files/fetch.cat3 | 30 | ||||
-rw-r--r-- | net/libfetch/files/fetch.h | 40 | ||||
-rw-r--r-- | net/libfetch/files/file.c | 124 | ||||
-rw-r--r-- | net/libfetch/files/ftp.c | 83 | ||||
-rw-r--r-- | net/libfetch/files/http.c | 126 |
12 files changed, 283 insertions, 273 deletions
diff --git a/net/libfetch/Makefile b/net/libfetch/Makefile index 3e1220ca633..fb4dc6663aa 100644 --- a/net/libfetch/Makefile +++ b/net/libfetch/Makefile @@ -1,7 +1,7 @@ -# $NetBSD: Makefile,v 1.1.1.1 2008/02/07 01:48:22 joerg Exp $ +# $NetBSD: Makefile,v 1.2 2008/04/02 15:33:14 joerg Exp $ # -DISTNAME= libfetch-2.0 +DISTNAME= libfetch-2.1 CATEGORIES= net MASTER_SITES= # empty DISTFILES= # empty @@ -17,7 +17,9 @@ PKG_DESTDIR_SUPPORT= user-destdir USE_BSD_MAKEFILE= yes -INSTALLATION_DIRS= lib ${PKGMANDIR}/cat3 ${PKGMANDIR}/man3 +INSTALLATION_DIRS= include lib ${PKGMANDIR}/cat3 ${PKGMANDIR}/man3 + +INSTALL_TARGET= install includes do-extract: ${CP} -r ${FILESDIR} ${WRKSRC} diff --git a/net/libfetch/PLIST b/net/libfetch/PLIST index 2fe982147f4..cd180083f8f 100644 --- a/net/libfetch/PLIST +++ b/net/libfetch/PLIST @@ -1,4 +1,5 @@ -@comment $NetBSD: PLIST,v 1.1.1.1 2008/02/07 01:48:22 joerg Exp $ +@comment $NetBSD: PLIST,v 1.2 2008/04/02 15:33:14 joerg Exp $ +include/fetch.h lib/libfetch.a man/cat3/fetch.0 man/man3/fetch.3 diff --git a/net/libfetch/buildlink3.mk b/net/libfetch/buildlink3.mk index 05527697010..d3c764935e7 100644 --- a/net/libfetch/buildlink3.mk +++ b/net/libfetch/buildlink3.mk @@ -1,4 +1,4 @@ -# $NetBSD: buildlink3.mk,v 1.1.1.1 2008/02/07 01:48:22 joerg Exp $ +# $NetBSD: buildlink3.mk,v 1.2 2008/04/02 15:33:14 joerg Exp $ BUILDLINK_DEPMETHOD.libfetch?= build @@ -14,7 +14,7 @@ BUILDLINK_PACKAGES+= libfetch BUILDLINK_ORDER:= ${BUILDLINK_ORDER} ${BUILDLINK_DEPTH}libfetch .if ${LIBFETCH_BUILDLINK3_MK} == "+" -BUILDLINK_API_DEPENDS.libfetch+= libfetch>=2.0 +BUILDLINK_API_DEPENDS.libfetch+= libfetch>=2.1 BUILDLINK_PKGSRCDIR.libfetch?= ../../net/libfetch .endif # LIBFETCH_BUILDLINK3_MK diff --git a/net/libfetch/files/common.c b/net/libfetch/files/common.c index 1461ea933b9..1e224c668ed 100644 --- a/net/libfetch/files/common.c +++ b/net/libfetch/files/common.c @@ -1,4 +1,4 @@ -/* $NetBSD: common.c,v 1.4 2008/02/07 16:30:49 joerg Exp $ */ +/* $NetBSD: common.c,v 1.5 2008/04/02 15:33:14 joerg Exp $ */ /*- * Copyright (c) 1998-2004 Dag-Erling Coïdan Smørgrav * All rights reserved. @@ -271,8 +271,6 @@ fetch_connect(const char *host, int port, int af, int verbose) struct addrinfo hints, *res, *res0; int sd, err; - DEBUG(fprintf(stderr, "---> %s:%d\n", host, port)); - if (verbose) fetch_info("looking up %s", host); @@ -485,7 +483,6 @@ fetch_getln(conn_t *conn) } while (c != '\n'); conn->buf[conn->buflen] = '\0'; - DEBUG(fprintf(stderr, "<<< %s", conn->buf)); return (0); } @@ -588,7 +585,6 @@ fetch_putln(conn_t *conn, const char *str, size_t len) struct iovec iov[2]; int ret; - DEBUG(fprintf(stderr, ">>> %s\n", str)); iov[0].iov_base = DECONST(char *, str); iov[0].iov_len = len; iov[1].iov_base = DECONST(char *, ENDL); @@ -662,7 +658,7 @@ fetch_read_word(FILE *f) { static char word[1024]; - if (fscanf(f, " %1024s ", word) != 1) + if (fscanf(f, " %1023s ", word) != 1) return (NULL); return (word); } @@ -699,14 +695,11 @@ fetch_netrc_auth(struct url *url) if ((f = fopen(fn, "r")) == NULL) return (-1); while ((word = fetch_read_word(f)) != NULL) { - if (strcmp(word, "default") == 0) { - DEBUG(fetch_info("Using default .netrc settings")); + if (strcmp(word, "default") == 0) break; - } if (strcmp(word, "machine") == 0 && (word = fetch_read_word(f)) != NULL && strcasecmp(word, url->host) == 0) { - DEBUG(fetch_info("Using .netrc settings for %s", word)); break; } } @@ -791,3 +784,54 @@ fetch_no_proxy_match(const char *host) return (0); } + +struct fetchIO { + void *io_cookie; + ssize_t (*io_read)(void *, void *, size_t); + ssize_t (*io_write)(void *, const void *, size_t); + void (*io_close)(void *); +}; + +void +fetchIO_close(fetchIO *f) +{ + if (f->io_close != NULL) + (*f->io_close)(f->io_cookie); + + free(f); +} + +fetchIO * +fetchIO_unopen(void *io_cookie, ssize_t (*io_read)(void *, void *, size_t), + ssize_t (*io_write)(void *, const void *, size_t), + void (*io_close)(void *)) +{ + fetchIO *f; + + f = malloc(sizeof(*f)); + if (f == NULL) + return f; + + f->io_cookie = io_cookie; + f->io_read = io_read; + f->io_write = io_write; + f->io_close = io_close; + + return f; +} + +ssize_t +fetchIO_read(fetchIO *f, void *buf, size_t len) +{ + if (f->io_read == NULL) + return EBADF; + return (*f->io_read)(f->io_cookie, buf, len); +} + +ssize_t +fetchIO_write(fetchIO *f, const void *buf, size_t len) +{ + if (f->io_read == NULL) + return EBADF; + return (*f->io_write)(f->io_cookie, buf, len); +} diff --git a/net/libfetch/files/common.h b/net/libfetch/files/common.h index d8fb9bc1462..fbdfbbb4483 100644 --- a/net/libfetch/files/common.h +++ b/net/libfetch/files/common.h @@ -1,4 +1,4 @@ -/* $NetBSD: common.h,v 1.3 2008/02/21 14:40:43 tnn Exp $ */ +/* $NetBSD: common.h,v 1.4 2008/04/02 15:33:14 joerg Exp $ */ /*- * Copyright (c) 1998-2004 Dag-Erling Coïdan Smørgrav * All rights reserved. @@ -103,11 +103,8 @@ int fetch_no_proxy_match(const char *); #define netdb_seterr(n) fetch_seterr(netdb_errlist, n) #define url_seterr(n) fetch_seterr(url_errlist, n) -#ifndef NDEBUG -#define DEBUG(x) do { if (fetchDebug) { x; } } while (0) -#else -#define DEBUG(x) do { } while (0) -#endif +fetchIO *fetchIO_unopen(void *, ssize_t (*)(void *, void *, size_t), + ssize_t (*)(void *, const void *, size_t), void (*)(void *)); /* * I don't really like exporting http_request() and ftp_request(), @@ -118,11 +115,12 @@ int fetch_no_proxy_match(const char *); * Note that _*_request() free purl, which is way ugly but saves us a * whole lot of trouble. */ -FILE *http_request(struct url *, const char *, +fetchIO *http_request(struct url *, const char *, struct url_stat *, struct url *, const char *); -FILE *ftp_request(struct url *, const char *, +fetchIO *ftp_request(struct url *, const char *, struct url_stat *, struct url *, const char *); + /* * Check whether a particular flag is set */ diff --git a/net/libfetch/files/fetch.3 b/net/libfetch/files/fetch.3 index cae66f9e70a..7637eb781c0 100644 --- a/net/libfetch/files/fetch.3 +++ b/net/libfetch/files/fetch.3 @@ -24,7 +24,7 @@ .\" SUCH DAMAGE. .\" .\" $FreeBSD: fetch.3,v 1.64 2007/12/18 11:03:26 des Exp $ -.\" $NetBSD: fetch.3,v 1.2 2008/02/07 16:14:32 joerg Exp $ +.\" $NetBSD: fetch.3,v 1.3 2008/04/02 15:33:14 joerg Exp $ .\" .Dd December 18, 2007 .Dt FETCH 3 @@ -70,51 +70,51 @@ .Fn fetchParseURL "const char *URL" .Ft void .Fn fetchFreeURL "struct url *u" -.Ft FILE * +.Ft fetchIO * .Fn fetchXGetURL "const char *URL" "struct url_stat *us" "const char *flags" -.Ft FILE * +.Ft fetchIO * .Fn fetchGetURL "const char *URL" "const char *flags" -.Ft FILE * +.Ft fetchIO * .Fn fetchPutURL "const char *URL" "const char *flags" .Ft int .Fn fetchStatURL "const char *URL" "struct url_stat *us" "const char *flags" .Ft struct url_ent * .Fn fetchListURL "const char *URL" "const char *flags" -.Ft FILE * +.Ft fetchIO * .Fn fetchXGet "struct url *u" "struct url_stat *us" "const char *flags" -.Ft FILE * +.Ft fetchIO * .Fn fetchGet "struct url *u" "const char *flags" -.Ft FILE * +.Ft fetchIO * .Fn fetchPut "struct url *u" "const char *flags" .Ft int .Fn fetchStat "struct url *u" "struct url_stat *us" "const char *flags" .Ft struct url_ent * .Fn fetchList "struct url *u" "const char *flags" -.Ft FILE * +.Ft fetchIO * .Fn fetchXGetFile "struct url *u" "struct url_stat *us" "const char *flags" -.Ft FILE * +.Ft fetchIO * .Fn fetchGetFile "struct url *u" "const char *flags" -.Ft FILE * +.Ft fetchIO * .Fn fetchPutFile "struct url *u" "const char *flags" .Ft int .Fn fetchStatFile "struct url *u" "struct url_stat *us" "const char *flags" .Ft struct url_ent * .Fn fetchListFile "struct url *u" "const char *flags" -.Ft FILE * +.Ft fetchIO * .Fn fetchXGetHTTP "struct url *u" "struct url_stat *us" "const char *flags" -.Ft FILE * +.Ft fetchIO * .Fn fetchGetHTTP "struct url *u" "const char *flags" -.Ft FILE * +.Ft fetchIO * .Fn fetchPutHTTP "struct url *u" "const char *flags" .Ft int .Fn fetchStatHTTP "struct url *u" "struct url_stat *us" "const char *flags" .Ft struct url_ent * .Fn fetchListHTTP "struct url *u" "const char *flags" -.Ft FILE * +.Ft fetchIO * .Fn fetchXGetFTP "struct url *u" "struct url_stat *us" "const char *flags" -.Ft FILE * +.Ft fetchIO * .Fn fetchGetFTP "struct url *u" "const char *flags" -.Ft FILE * +.Ft fetchIO * .Fn fetchPutFTP "struct url *u" "const char *flags" .Ft int .Fn fetchStatFTP "struct url *u" "struct url_stat *us" "const char *flags" diff --git a/net/libfetch/files/fetch.c b/net/libfetch/files/fetch.c index 95f4cadc529..0c7fcc0d214 100644 --- a/net/libfetch/files/fetch.c +++ b/net/libfetch/files/fetch.c @@ -1,4 +1,4 @@ -/* $NetBSD: fetch.c,v 1.1.1.1 2008/02/07 01:48:22 joerg Exp $ */ +/* $NetBSD: fetch.c,v 1.2 2008/04/02 15:33:14 joerg Exp $ */ /*- * Copyright (c) 1998-2004 Dag-Erling Coïdan Smørgrav * All rights reserved. @@ -69,7 +69,7 @@ static struct fetcherr url_errlist[] = { * read-only stream connected to the document referenced by the URL. * Also fill out the struct url_stat. */ -FILE * +fetchIO * fetchXGet(struct url *URL, struct url_stat *us, const char *flags) { int direct; @@ -95,7 +95,7 @@ fetchXGet(struct url *URL, struct url_stat *us, const char *flags) * Select the appropriate protocol for the URL scheme, and return a * read-only stream connected to the document referenced by the URL. */ -FILE * +fetchIO * fetchGet(struct url *URL, const char *flags) { return (fetchXGet(URL, NULL, flags)); @@ -105,7 +105,7 @@ fetchGet(struct url *URL, const char *flags) * Select the appropriate protocol for the URL scheme, and return a * write-only stream connected to the document referenced by the URL. */ -FILE * +fetchIO * fetchPut(struct url *URL, const char *flags) { int direct; @@ -174,11 +174,11 @@ fetchList(struct url *URL, const char *flags) /* * Attempt to parse the given URL; if successful, call fetchXGet(). */ -FILE * +fetchIO * fetchXGetURL(const char *URL, struct url_stat *us, const char *flags) { struct url *u; - FILE *f; + fetchIO *f; if ((u = fetchParseURL(URL)) == NULL) return (NULL); @@ -192,7 +192,7 @@ fetchXGetURL(const char *URL, struct url_stat *us, const char *flags) /* * Attempt to parse the given URL; if successful, call fetchGet(). */ -FILE * +fetchIO * fetchGetURL(const char *URL, const char *flags) { return (fetchXGetURL(URL, NULL, flags)); @@ -201,11 +201,11 @@ fetchGetURL(const char *URL, const char *flags) /* * Attempt to parse the given URL; if successful, call fetchPut(). */ -FILE * +fetchIO * fetchPutURL(const char *URL, const char *flags) { struct url *u; - FILE *f; + fetchIO *f; if ((u = fetchParseURL(URL)) == NULL) return (NULL); @@ -408,16 +408,6 @@ nohost: goto ouch; } - DEBUG(fprintf(stderr, - "scheme: [%s]\n" - "user: [%s]\n" - "password: [%s]\n" - "host: [%s]\n" - "port: [%d]\n" - "document: [%s]\n", - u->scheme, u->user, u->pwd, - u->host, u->port, u->doc)); - return (u); ouch: diff --git a/net/libfetch/files/fetch.cat3 b/net/libfetch/files/fetch.cat3 index a0128855d52..e99f922fc30 100644 --- a/net/libfetch/files/fetch.cat3 +++ b/net/libfetch/files/fetch.cat3 @@ -25,13 +25,13 @@ SSYYNNOOPPSSIISS _v_o_i_d ffeettcchhFFrreeeeUURRLL(_s_t_r_u_c_t _u_r_l _*_u); - _F_I_L_E _* + _f_e_t_c_h_I_O _* ffeettcchhXXGGeettUURRLL(_c_o_n_s_t _c_h_a_r _*_U_R_L, _s_t_r_u_c_t _u_r_l___s_t_a_t _*_u_s, _c_o_n_s_t _c_h_a_r _*_f_l_a_g_s); - _F_I_L_E _* + _f_e_t_c_h_I_O _* ffeettcchhGGeettUURRLL(_c_o_n_s_t _c_h_a_r _*_U_R_L, _c_o_n_s_t _c_h_a_r _*_f_l_a_g_s); - _F_I_L_E _* + _f_e_t_c_h_I_O _* ffeettcchhPPuuttUURRLL(_c_o_n_s_t _c_h_a_r _*_U_R_L, _c_o_n_s_t _c_h_a_r _*_f_l_a_g_s); _i_n_t @@ -40,13 +40,13 @@ SSYYNNOOPPSSIISS _s_t_r_u_c_t _u_r_l___e_n_t _* ffeettcchhLLiissttUURRLL(_c_o_n_s_t _c_h_a_r _*_U_R_L, _c_o_n_s_t _c_h_a_r _*_f_l_a_g_s); - _F_I_L_E _* + _f_e_t_c_h_I_O _* ffeettcchhXXGGeett(_s_t_r_u_c_t _u_r_l _*_u, _s_t_r_u_c_t _u_r_l___s_t_a_t _*_u_s, _c_o_n_s_t _c_h_a_r _*_f_l_a_g_s); - _F_I_L_E _* + _f_e_t_c_h_I_O _* ffeettcchhGGeett(_s_t_r_u_c_t _u_r_l _*_u, _c_o_n_s_t _c_h_a_r _*_f_l_a_g_s); - _F_I_L_E _* + _f_e_t_c_h_I_O _* ffeettcchhPPuutt(_s_t_r_u_c_t _u_r_l _*_u, _c_o_n_s_t _c_h_a_r _*_f_l_a_g_s); _i_n_t @@ -55,13 +55,13 @@ SSYYNNOOPPSSIISS _s_t_r_u_c_t _u_r_l___e_n_t _* ffeettcchhLLiisstt(_s_t_r_u_c_t _u_r_l _*_u, _c_o_n_s_t _c_h_a_r _*_f_l_a_g_s); - _F_I_L_E _* + _f_e_t_c_h_I_O _* ffeettcchhXXGGeettFFiillee(_s_t_r_u_c_t _u_r_l _*_u, _s_t_r_u_c_t _u_r_l___s_t_a_t _*_u_s, _c_o_n_s_t _c_h_a_r _*_f_l_a_g_s); - _F_I_L_E _* + _f_e_t_c_h_I_O _* ffeettcchhGGeettFFiillee(_s_t_r_u_c_t _u_r_l _*_u, _c_o_n_s_t _c_h_a_r _*_f_l_a_g_s); - _F_I_L_E _* + _f_e_t_c_h_I_O _* ffeettcchhPPuuttFFiillee(_s_t_r_u_c_t _u_r_l _*_u, _c_o_n_s_t _c_h_a_r _*_f_l_a_g_s); _i_n_t @@ -70,13 +70,13 @@ SSYYNNOOPPSSIISS _s_t_r_u_c_t _u_r_l___e_n_t _* ffeettcchhLLiissttFFiillee(_s_t_r_u_c_t _u_r_l _*_u, _c_o_n_s_t _c_h_a_r _*_f_l_a_g_s); - _F_I_L_E _* + _f_e_t_c_h_I_O _* ffeettcchhXXGGeettHHTTTTPP(_s_t_r_u_c_t _u_r_l _*_u, _s_t_r_u_c_t _u_r_l___s_t_a_t _*_u_s, _c_o_n_s_t _c_h_a_r _*_f_l_a_g_s); - _F_I_L_E _* + _f_e_t_c_h_I_O _* ffeettcchhGGeettHHTTTTPP(_s_t_r_u_c_t _u_r_l _*_u, _c_o_n_s_t _c_h_a_r _*_f_l_a_g_s); - _F_I_L_E _* + _f_e_t_c_h_I_O _* ffeettcchhPPuuttHHTTTTPP(_s_t_r_u_c_t _u_r_l _*_u, _c_o_n_s_t _c_h_a_r _*_f_l_a_g_s); _i_n_t @@ -85,13 +85,13 @@ SSYYNNOOPPSSIISS _s_t_r_u_c_t _u_r_l___e_n_t _* ffeettcchhLLiissttHHTTTTPP(_s_t_r_u_c_t _u_r_l _*_u, _c_o_n_s_t _c_h_a_r _*_f_l_a_g_s); - _F_I_L_E _* + _f_e_t_c_h_I_O _* ffeettcchhXXGGeettFFTTPP(_s_t_r_u_c_t _u_r_l _*_u, _s_t_r_u_c_t _u_r_l___s_t_a_t _*_u_s, _c_o_n_s_t _c_h_a_r _*_f_l_a_g_s); - _F_I_L_E _* + _f_e_t_c_h_I_O _* ffeettcchhGGeettFFTTPP(_s_t_r_u_c_t _u_r_l _*_u, _c_o_n_s_t _c_h_a_r _*_f_l_a_g_s); - _F_I_L_E _* + _f_e_t_c_h_I_O _* ffeettcchhPPuuttFFTTPP(_s_t_r_u_c_t _u_r_l _*_u, _c_o_n_s_t _c_h_a_r _*_f_l_a_g_s); _i_n_t diff --git a/net/libfetch/files/fetch.h b/net/libfetch/files/fetch.h index be4f1c93c36..e6c94ae0db8 100644 --- a/net/libfetch/files/fetch.h +++ b/net/libfetch/files/fetch.h @@ -1,4 +1,4 @@ -/* $NetBSD: fetch.h,v 1.3 2008/02/07 17:10:35 joerg Exp $ */ +/* $NetBSD: fetch.h,v 1.4 2008/04/02 15:33:14 joerg Exp $ */ /*- * Copyright (c) 1998-2004 Dag-Erling Coïdan Smørgrav * All rights reserved. @@ -43,6 +43,8 @@ #define URL_USERLEN 256 #define URL_PWDLEN 256 +typedef struct fetchIO fetchIO; + struct url { char scheme[URL_SCHEMELEN + 1]; char user[URL_USERLEN + 1]; @@ -96,36 +98,40 @@ struct url_ent { extern "C" { #endif -/* FILE-specific functions */ -FILE *fetchXGetFile(struct url *, struct url_stat *, const char *); -FILE *fetchGetFile(struct url *, const char *); -FILE *fetchPutFile(struct url *, const char *); +void fetchIO_close(fetchIO *); +ssize_t fetchIO_read(fetchIO *, void *, size_t); +ssize_t fetchIO_write(fetchIO *, const void *, size_t); + +/* fetchIO-specific functions */ +fetchIO *fetchXGetFile(struct url *, struct url_stat *, const char *); +fetchIO *fetchGetFile(struct url *, const char *); +fetchIO *fetchPutFile(struct url *, const char *); int fetchStatFile(struct url *, struct url_stat *, const char *); struct url_ent *fetchListFile(struct url *, const char *); /* HTTP-specific functions */ -FILE *fetchXGetHTTP(struct url *, struct url_stat *, const char *); -FILE *fetchGetHTTP(struct url *, const char *); -FILE *fetchPutHTTP(struct url *, const char *); +fetchIO *fetchXGetHTTP(struct url *, struct url_stat *, const char *); +fetchIO *fetchGetHTTP(struct url *, const char *); +fetchIO *fetchPutHTTP(struct url *, const char *); int fetchStatHTTP(struct url *, struct url_stat *, const char *); struct url_ent *fetchListHTTP(struct url *, const char *); /* FTP-specific functions */ -FILE *fetchXGetFTP(struct url *, struct url_stat *, const char *); -FILE *fetchGetFTP(struct url *, const char *); -FILE *fetchPutFTP(struct url *, const char *); +fetchIO *fetchXGetFTP(struct url *, struct url_stat *, const char *); +fetchIO *fetchGetFTP(struct url *, const char *); +fetchIO *fetchPutFTP(struct url *, const char *); int fetchStatFTP(struct url *, struct url_stat *, const char *); struct url_ent *fetchListFTP(struct url *, const char *); /* Generic functions */ -FILE *fetchXGetURL(const char *, struct url_stat *, const char *); -FILE *fetchGetURL(const char *, const char *); -FILE *fetchPutURL(const char *, const char *); +fetchIO *fetchXGetURL(const char *, struct url_stat *, const char *); +fetchIO *fetchGetURL(const char *, const char *); +fetchIO *fetchPutURL(const char *, const char *); int fetchStatURL(const char *, struct url_stat *, const char *); struct url_ent *fetchListURL(const char *, const char *); -FILE *fetchXGet(struct url *, struct url_stat *, const char *); -FILE *fetchGet(struct url *, const char *); -FILE *fetchPut(struct url *, const char *); +fetchIO *fetchXGet(struct url *, struct url_stat *, const char *); +fetchIO *fetchGet(struct url *, const char *); +fetchIO *fetchPut(struct url *, const char *); int fetchStat(struct url *, struct url_stat *, const char *); struct url_ent *fetchList(struct url *, const char *); diff --git a/net/libfetch/files/file.c b/net/libfetch/files/file.c index 95d93c9d2d0..153a4ed56ce 100644 --- a/net/libfetch/files/file.c +++ b/net/libfetch/files/file.c @@ -1,4 +1,4 @@ -/* $NetBSD: file.c,v 1.2 2008/02/07 17:12:12 joerg Exp $ */ +/* $NetBSD: file.c,v 1.3 2008/04/02 15:33:14 joerg Exp $ */ /*- * Copyright (c) 1998-2004 Dag-Erling Coïdan Smørgrav * All rights reserved. @@ -32,68 +32,130 @@ #include <sys/stat.h> #include <dirent.h> +#include <fcntl.h> #include <stdio.h> +#include <stdlib.h> #include <string.h> +#include <unistd.h> #include "fetch.h" #include "common.h" -FILE * +static int fetch_stat_file(int, struct url_stat *); + +static ssize_t +fetchFile_read(void *cookie, void *buf, size_t len) +{ + return read(*(int *)cookie, buf, len); +} + +static ssize_t +fetchFile_write(void *cookie, const void *buf, size_t len) +{ + return write(*(int *)cookie, buf, len); +} + +static void +fetchFile_close(void *cookie) +{ + int fd = *(int *)cookie; + + free(cookie); + + close(fd); +} + +fetchIO * fetchXGetFile(struct url *u, struct url_stat *us, const char *flags) { - FILE *f; + fetchIO *f; + int fd, *cookie; - if (us && fetchStatFile(u, us, flags) == -1) - return (NULL); + fd = open(u->doc, O_RDONLY); + if (fd == -1) { + fetch_syserr(); + return NULL; + } - f = fopen(u->doc, "r"); + if (us && fetch_stat_file(fd, us) == -1) { + close(fd); + return NULL; + } - if (f == NULL) + if (u->offset && lseek(fd, u->offset, SEEK_SET) == -1) { + close(fd); fetch_syserr(); + return NULL; + } - if (u->offset && fseeko(f, u->offset, SEEK_SET) == -1) { - fclose(f); + cookie = malloc(sizeof(int)); + if (cookie == NULL) { + close(fd); fetch_syserr(); + return NULL; } - return (f); + *cookie = fd; + f = fetchIO_unopen(cookie, fetchFile_read, fetchFile_write, fetchFile_close); + if (f == NULL) { + close(fd); + free(cookie); + } + return f; } -FILE * +fetchIO * fetchGetFile(struct url *u, const char *flags) { return (fetchXGetFile(u, NULL, flags)); } -FILE * +fetchIO * fetchPutFile(struct url *u, const char *flags) { - FILE *f; + fetchIO *f; + int fd, *cookie; if (CHECK_FLAG('a')) - f = fopen(u->doc, "a"); + fd = open(u->doc, O_WRONLY | O_APPEND); else - f = fopen(u->doc, "w+"); + fd = open(u->doc, O_WRONLY); + + if (fd == -1) { + fetch_syserr(); + return NULL; + } - if (f == NULL) + if (u->offset && lseek(fd, u->offset, SEEK_SET) == -1) { + close(fd); fetch_syserr(); + return NULL; + } - if (u->offset && fseeko(f, u->offset, SEEK_SET) == -1) { - fclose(f); + cookie = malloc(sizeof(int)); + if (cookie == NULL) { + close(fd); fetch_syserr(); + return NULL; } - return (f); + *cookie = fd; + f = fetchIO_unopen(cookie, fetchFile_read, fetchFile_write, fetchFile_close); + if (f == NULL) { + close(fd); + free(cookie); + } + return f; } static int -fetch_stat_file(const char *fn, struct url_stat *us) +fetch_stat_file(int fd, struct url_stat *us) { struct stat sb; us->size = -1; us->atime = us->mtime = 0; - if (stat(fn, &sb) == -1) { + if (fstat(fd, &sb) == -1) { fetch_syserr(); return (-1); } @@ -103,10 +165,25 @@ fetch_stat_file(const char *fn, struct url_stat *us) return (0); } +static int +fetch_stat_file2(const char *fn, struct url_stat *us) +{ + int fd, rv; + + fd = open(fn, O_RDONLY); + if (fd == -1) { + fetch_syserr(); + return -1; + } + rv = fetch_stat_file(fd, us); + close(fd); + return rv; +} + int fetchStatFile(struct url *u, struct url_stat *us, const char *flags) { - return (fetch_stat_file(u->doc, us)); + return (fetch_stat_file2(u->doc, us)); } struct url_ent * @@ -135,9 +212,10 @@ fetchListFile(struct url *u, const char *flags) while ((de = readdir(dir)) != NULL) { strncpy(p, de->d_name, l - 1); p[l - 1] = 0; - if (fetch_stat_file(fn, &us) == -1) + if (fetch_stat_file2(fn, &us) == -1) { /* should I return a partial result, or abort? */ break; + } fetch_add_entry(&ue, &size, &len, de->d_name, &us); } diff --git a/net/libfetch/files/ftp.c b/net/libfetch/files/ftp.c index 6fc054c63c0..f6e6f1b4672 100644 --- a/net/libfetch/files/ftp.c +++ b/net/libfetch/files/ftp.c @@ -1,4 +1,4 @@ -/* $NetBSD: ftp.c,v 1.9 2008/02/07 17:57:28 joerg Exp $ */ +/* $NetBSD: ftp.c,v 1.10 2008/04/02 15:33:14 joerg Exp $ */ /*- * Copyright (c) 1998-2004 Dag-Erling Coïdan Smørgrav * All rights reserved. @@ -258,9 +258,6 @@ ftp_pwd(conn_t *conn, char *pwd, size_t pwdlen) if (!pwdlen) return (FTP_PROTOCOL_ERROR); *dst = '\0'; -#if 0 - DEBUG(fprintf(stderr, "pwd: [%s]\n", pwd)); -#endif return (FTP_OK); } @@ -290,10 +287,6 @@ ftp_cwd(conn_t *conn, const char *file) for (i = 0; i <= len && i <= end - file; ++i) if (pwd[i] != file[i]) break; -#if 0 - DEBUG(fprintf(stderr, "have: [%.*s|%s]\n", i, pwd, pwd + i)); - DEBUG(fprintf(stderr, "want: [%.*s|%s]\n", i, file, file + i)); -#endif /* Keep going up a dir until we have a matching prefix. */ if (pwd[i] == '\0' && (file[i - 1] == '/' || file[i] == '/')) break; @@ -433,7 +426,6 @@ ftp_stat(conn_t *conn, const char *file, struct url_stat *us) } if (us->size == 0) us->size = -1; - DEBUG(fprintf(stderr, "size: [%lld]\n", (long long)us->size)); e = ftp_cmd(conn, "MDTM %.*s", filenamelen, filename); if (e != FTP_FILE_STATUS) { @@ -468,10 +460,7 @@ ftp_stat(conn_t *conn, const char *file, struct url_stat *us) t = time(NULL); us->mtime = t; us->atime = t; - DEBUG(fprintf(stderr, - "last modified: [%04d-%02d-%02d %02d:%02d:%02d]\n", - tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, - tm.tm_hour, tm.tm_min, tm.tm_sec)); + return (0); } @@ -486,13 +475,12 @@ struct ftpio { int err; /* Error code */ }; -static int ftp_readfn(void *, char *, int); -static int ftp_writefn(void *, const char *, int); -static fpos_t ftp_seekfn(void *, fpos_t, int); -static int ftp_closefn(void *); +static ssize_t ftp_readfn(void *, void *, size_t); +static ssize_t ftp_writefn(void *, const void *, size_t); +static void ftp_closefn(void *); -static int -ftp_readfn(void *v, char *buf, int len) +static ssize_t +ftp_readfn(void *v, void *buf, size_t len) { struct ftpio *io; int r; @@ -524,8 +512,8 @@ ftp_readfn(void *v, char *buf, int len) return (-1); } -static int -ftp_writefn(void *v, const char *buf, int len) +static ssize_t +ftp_writefn(void *v, const void *buf, size_t len) { struct ftpio *io; int w; @@ -551,21 +539,7 @@ ftp_writefn(void *v, const char *buf, int len) return (-1); } -static fpos_t -ftp_seekfn(void *v, fpos_t pos, int whence) -{ - struct ftpio *io; - - io = (struct ftpio *)v; - if (io == NULL) { - errno = EBADF; - return (-1); - } - errno = ESPIPE; - return (-1); -} - -static int +static void ftp_closefn(void *v) { struct ftpio *io; @@ -574,31 +548,30 @@ ftp_closefn(void *v) io = (struct ftpio *)v; if (io == NULL) { errno = EBADF; - return (-1); + return; } if (io->dir == -1) - return (0); + return; if (io->cconn == NULL || io->dconn == NULL) { errno = EBADF; - return (-1); + return; } fetch_close(io->dconn); io->dir = -1; io->dconn = NULL; - DEBUG(fprintf(stderr, "Waiting for final status\n")); r = ftp_chkerr(io->cconn); if (io->cconn == cached_connection && io->cconn->ref == 1) cached_connection = NULL; fetch_close(io->cconn); free(io); - return (r == FTP_TRANSFER_COMPLETE) ? 0 : -1; + return; } -static FILE * +static fetchIO * ftp_setup(conn_t *cconn, conn_t *dconn, int mode) { struct ftpio *io; - FILE *f; + fetchIO *f; if (cconn == NULL || dconn == NULL) return (NULL); @@ -608,7 +581,7 @@ ftp_setup(conn_t *cconn, conn_t *dconn, int mode) io->dconn = dconn; io->dir = mode; io->eof = io->err = 0; - f = funopen(io, ftp_readfn, ftp_writefn, ftp_seekfn, ftp_closefn); + f = fetchIO_unopen(io, ftp_readfn, ftp_writefn, ftp_closefn); if (f == NULL) free(io); return (f); @@ -617,7 +590,7 @@ ftp_setup(conn_t *cconn, conn_t *dconn, int mode) /* * Transfer file */ -static FILE * +static fetchIO * ftp_transfer(conn_t *conn, const char *oper, const char *file, int mode, off_t offset, const char *flags) { @@ -631,7 +604,7 @@ ftp_transfer(conn_t *conn, const char *oper, const char *file, int e, sd = -1; socklen_t l; char *s; - FILE *df; + fetchIO *df; /* check flags */ low = CHECK_FLAG('l'); @@ -1103,7 +1076,7 @@ ftp_get_proxy(struct url * url, const char *flags) /* * Process an FTP request */ -FILE * +fetchIO * ftp_request(struct url *url, const char *op, struct url_stat *us, struct url *purl, const char *flags) { @@ -1141,7 +1114,7 @@ ftp_request(struct url *url, const char *op, struct url_stat *us, /* just a stat */ if (strcmp(op, "STAT") == 0) - return (FILE *)1; /* bogus return value */ + return fetchIO_unopen(NULL, NULL, NULL, NULL); if (strcmp(op, "STOR") == 0 || strcmp(op, "APPE") == 0) oflag = O_WRONLY; else @@ -1154,7 +1127,7 @@ ftp_request(struct url *url, const char *op, struct url_stat *us, /* * Get and stat file */ -FILE * +fetchIO * fetchXGetFTP(struct url *url, struct url_stat *us, const char *flags) { return (ftp_request(url, "RETR", us, ftp_get_proxy(url, flags), flags)); @@ -1163,7 +1136,7 @@ fetchXGetFTP(struct url *url, struct url_stat *us, const char *flags) /* * Get file */ -FILE * +fetchIO * fetchGetFTP(struct url *url, const char *flags) { return (fetchXGetFTP(url, NULL, flags)); @@ -1172,7 +1145,7 @@ fetchGetFTP(struct url *url, const char *flags) /* * Put file */ -FILE * +fetchIO * fetchPutFTP(struct url *url, const char *flags) { return (ftp_request(url, CHECK_FLAG('a') ? "APPE" : "STOR", NULL, @@ -1185,16 +1158,12 @@ fetchPutFTP(struct url *url, const char *flags) int fetchStatFTP(struct url *url, struct url_stat *us, const char *flags) { - FILE *f; + fetchIO *f; f = ftp_request(url, "STAT", us, ftp_get_proxy(url, flags), flags); if (f == NULL) return (-1); - /* - * When op is "STAT", ftp_request() will return either NULL or - * (FILE *)1, never a valid FILE *, so we mustn't fclose(f) before - * returning, as it would cause a segfault. - */ + fetchIO_close(f); return (0); } diff --git a/net/libfetch/files/http.c b/net/libfetch/files/http.c index 6d98dcaac48..e5ba72a7199 100644 --- a/net/libfetch/files/http.c +++ b/net/libfetch/files/http.c @@ -1,4 +1,4 @@ -/* $NetBSD: http.c,v 1.8 2008/02/07 18:02:01 joerg Exp $ */ +/* $NetBSD: http.c,v 1.9 2008/04/02 15:33:15 joerg Exp $ */ /*- * Copyright (c) 2000-2004 Dag-Erling Coïdan Smørgrav * All rights reserved. @@ -120,9 +120,6 @@ struct httpio int eof; /* end-of-file flag */ int error; /* error flag */ size_t chunksize; /* remaining size of current chunk */ -#ifndef NDEBUG - size_t total; -#endif }; /* @@ -153,18 +150,6 @@ http_new_chunk(struct httpio *io) } } -#ifndef NDEBUG - if (fetchDebug) { - io->total += io->chunksize; - if (io->chunksize == 0) - fprintf(stderr, "http_new_chunk(): end of last chunk\n"); - else - fprintf(stderr, "http_new_chunk(): new chunk: %lu (%lu)\n", - (unsigned long)io->chunksize, - (unsigned long)io->total); - } -#endif - return (io->chunksize); } @@ -245,11 +230,11 @@ http_fillbuf(struct httpio *io, size_t len) /* * Read function */ -static int -http_readfn(void *v, char *buf, int len) +static ssize_t +http_readfn(void *v, void *buf, size_t len) { struct httpio *io = (struct httpio *)v; - int l, pos; + size_t l, pos; if (io->error) return (-1); @@ -264,7 +249,7 @@ http_readfn(void *v, char *buf, int len) l = io->buflen - io->bufpos; if (len < l) l = len; - memcpy(buf + pos, io->buf + io->bufpos, l); + memcpy((char *)buf + pos, io->buf + io->bufpos, l); io->bufpos += l; } @@ -276,8 +261,8 @@ http_readfn(void *v, char *buf, int len) /* * Write function */ -static int -http_writefn(void *v, const char *buf, int len) +static ssize_t +http_writefn(void *v, const void *buf, size_t len) { struct httpio *io = (struct httpio *)v; @@ -287,27 +272,25 @@ http_writefn(void *v, const char *buf, int len) /* * Close function */ -static int +static void http_closefn(void *v) { struct httpio *io = (struct httpio *)v; - int r; - r = fetch_close(io->conn); + fetch_close(io->conn); if (io->buf) free(io->buf); free(io); - return (r); } /* * Wrap a file descriptor up */ -static FILE * +static fetchIO * http_funopen(conn_t *conn, int chunked) { struct httpio *io; - FILE *f; + fetchIO *f; if ((io = calloc(1, sizeof(*io))) == NULL) { fetch_syserr(); @@ -315,7 +298,7 @@ http_funopen(conn_t *conn, int chunked) } io->conn = conn; io->chunked = chunked; - f = funopen(io, http_readfn, http_writefn, NULL, http_closefn); + f = fetchIO_unopen(io, http_readfn, http_writefn, http_closefn); if (f == NULL) { fetch_syserr(); free(io); @@ -486,10 +469,6 @@ http_parse_mtime(const char *p, time_t *mtime) setlocale(LC_TIME, locale); if (r == NULL) return (-1); - DEBUG(fprintf(stderr, "last modified: [%04d-%02d-%02d " - "%02d:%02d:%02d]\n", - tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, - tm.tm_hour, tm.tm_min, tm.tm_sec)); *mtime = timegm(&tm); return (0); } @@ -506,8 +485,6 @@ http_parse_length(const char *p, off_t *length) len = len * 10 + (*p - '0'); if (*p) return (-1); - DEBUG(fprintf(stderr, "content length: [%lld]\n", - (long long)len)); *length = len; return (0); } @@ -540,15 +517,10 @@ http_parse_range(const char *p, off_t *offset, off_t *length, off_t *size) len = len * 10 + *p - '0'; if (*p || len < last - first + 1) return (-1); - if (first == -1) { - DEBUG(fprintf(stderr, "content range: [*/%lld]\n", - (long long)len)); + if (first == -1) *length = 0; - } else { - DEBUG(fprintf(stderr, "content range: [%lld-%lld/%lld]\n", - (long long)first, (long long)last, (long long)len)); + else *length = last - first + 1; - } *offset = first; *size = len; return (0); @@ -624,8 +596,6 @@ http_basic_auth(conn_t *conn, const char *hdr, const char *usr, const char *pwd) char *upw, *auth; int r; - DEBUG(fprintf(stderr, "usr: [%s]\n", usr)); - DEBUG(fprintf(stderr, "pwd: [%s]\n", pwd)); if (asprintf(&upw, "%s:%s", usr, pwd) == -1) return (-1); auth = http_base64(upw); @@ -751,45 +721,6 @@ http_get_proxy(struct url * url, const char *flags) return (NULL); } -static void -http_print_html(FILE *out, FILE *in) -{ - size_t len; - char *line, *p, *q; - int comment, tag; - - comment = tag = 0; - while ((line = fgetln(in, &len)) != NULL) { - while (len && isspace((unsigned char)line[len - 1])) - --len; - for (p = q = line; q < line + len; ++q) { - if (comment && *q == '-') { - if (q + 2 < line + len && - strcmp(q, "-->") == 0) { - tag = comment = 0; - q += 2; - } - } else if (tag && !comment && *q == '>') { - p = q + 1; - tag = 0; - } else if (!tag && *q == '<') { - if (q > p) - fwrite(p, q - p, 1, out); - tag = 1; - if (q + 3 < line + len && - strcmp(q, "<!--") == 0) { - comment = 1; - q += 3; - } - } - } - if (!tag && q > p) - fwrite(p, q - p, 1, out); - fputc('\n', out); - } -} - - /***************************************************************************** * Core */ @@ -800,7 +731,7 @@ http_print_html(FILE *out, FILE *in) * XXX This function is way too long, the do..while loop should be split * XXX off into a separate function. */ -FILE * +fetchIO * http_request(struct url *URL, const char *op, struct url_stat *us, struct url *purl, const char *flags) { @@ -811,7 +742,7 @@ http_request(struct url *URL, const char *op, struct url_stat *us, off_t offset, clength, length, size; time_t mtime; const char *p; - FILE *f; + fetchIO *f; hdr_t h; char hbuf[URL_HOSTLEN + 7], *host; @@ -1032,7 +963,6 @@ http_request(struct url *URL, const char *op, struct url_stat *us, new = fetchParseURL(p); if (new == NULL) { /* XXX should set an error code */ - DEBUG(fprintf(stderr, "failed to parse new URL\n")); goto ouch; } if (!*new->user && !*new->pwd) { @@ -1090,10 +1020,8 @@ http_request(struct url *URL, const char *op, struct url_stat *us, need_auth = 0; fetch_close(conn); conn = NULL; - if (!new) { - DEBUG(fprintf(stderr, "redirect with no new location\n")); + if (!new) break; - } if (url != URL) fetchFreeURL(url); url = new; @@ -1105,11 +1033,6 @@ http_request(struct url *URL, const char *op, struct url_stat *us, goto ouch; } - DEBUG(fprintf(stderr, "offset %lld, length %lld," - " size %lld, clength %lld\n", - (long long)offset, (long long)length, - (long long)size, (long long)clength)); - /* check for inconsistencies */ if (clength != -1 && length != -1 && clength != length) { http_seterr(HTTP_PROTOCOL_ERROR); @@ -1142,7 +1065,7 @@ http_request(struct url *URL, const char *op, struct url_stat *us, URL->offset = offset; URL->length = clength; - /* wrap it up in a FILE */ + /* wrap it up in a fetchIO */ if ((f = http_funopen(conn, chunked)) == NULL) { fetch_syserr(); goto ouch; @@ -1154,8 +1077,7 @@ http_request(struct url *URL, const char *op, struct url_stat *us, fetchFreeURL(purl); if (HTTP_ERROR(conn->err)) { - http_print_html(stderr, f); - fclose(f); + fetchIO_close(f); f = NULL; } @@ -1179,7 +1101,7 @@ ouch: /* * Retrieve and stat a file by HTTP */ -FILE * +fetchIO * fetchXGetHTTP(struct url *URL, struct url_stat *us, const char *flags) { return (http_request(URL, "GET", us, http_get_proxy(URL, flags), flags)); @@ -1188,7 +1110,7 @@ fetchXGetHTTP(struct url *URL, struct url_stat *us, const char *flags) /* * Retrieve a file by HTTP */ -FILE * +fetchIO * fetchGetHTTP(struct url *URL, const char *flags) { return (fetchXGetHTTP(URL, NULL, flags)); @@ -1197,7 +1119,7 @@ fetchGetHTTP(struct url *URL, const char *flags) /* * Store a file by HTTP */ -FILE * +fetchIO * fetchPutHTTP(struct url *URL, const char *flags) { fprintf(stderr, "fetchPutHTTP(): not implemented\n"); @@ -1210,12 +1132,12 @@ fetchPutHTTP(struct url *URL, const char *flags) int fetchStatHTTP(struct url *URL, struct url_stat *us, const char *flags) { - FILE *f; + fetchIO *f; f = http_request(URL, "HEAD", us, http_get_proxy(URL, flags), flags); if (f == NULL) return (-1); - fclose(f); + fetchIO_close(f); return (0); } |