diff options
author | jbj <devnull@localhost> | 1999-11-02 14:33:14 +0000 |
---|---|---|
committer | jbj <devnull@localhost> | 1999-11-02 14:33:14 +0000 |
commit | 7fc3a97733080a03e8bb3872bd37019db483b93c (patch) | |
tree | 3a61e2602283c9adf7441ed5acd1068cbf16fd98 /lib | |
parent | 65a08c9e60c0bff0e09cfdcc98ca97910ab24ac5 (diff) | |
download | librpm-tizen-7fc3a97733080a03e8bb3872bd37019db483b93c.tar.gz librpm-tizen-7fc3a97733080a03e8bb3872bd37019db483b93c.tar.bz2 librpm-tizen-7fc3a97733080a03e8bb3872bd37019db483b93c.zip |
Add ref/deref/new/fileno/open vectors to FDIO_t.
Hide fdOpen/ufdOpen, use {fdio,ufdio}->open throughout.
url.c: Create ref counted abstract urlinfo type with debugging.
url.c: Rename functions to urlNew/urlFree/urlFreeCache for consistency.
rpmio.c: Create ref counted abstract FD_t type with debugging.
rpmio.c: Create private struct _FD_s type.
cpio.c: Create fd[GS]etCpioPos to preserve FD_t abstraction.
CVS patchset: 3414
CVS date: 1999/11/02 14:33:14
Diffstat (limited to 'lib')
-rw-r--r-- | lib/cpio.c | 17 | ||||
-rw-r--r-- | lib/falloc.c | 4 | ||||
-rw-r--r-- | lib/falloc.h | 2 | ||||
-rw-r--r-- | lib/ftp.c | 75 | ||||
-rw-r--r-- | lib/header.c | 3 | ||||
-rw-r--r-- | lib/macro.c | 8 | ||||
-rw-r--r-- | lib/misc.c | 2 | ||||
-rw-r--r-- | lib/oldheader.c | 2 | ||||
-rw-r--r-- | lib/query.c | 2 | ||||
-rw-r--r-- | lib/rpmchecksig.c | 4 | ||||
-rw-r--r-- | lib/rpminstall.c | 8 | ||||
-rw-r--r-- | lib/rpmio.h | 93 | ||||
-rw-r--r-- | lib/rpmrc.c | 6 | ||||
-rw-r--r-- | lib/rpmurl.h | 35 | ||||
-rw-r--r-- | lib/signature.c | 8 | ||||
-rw-r--r-- | lib/uninstall.c | 2 | ||||
-rw-r--r-- | lib/url.c | 108 |
17 files changed, 239 insertions, 140 deletions
diff --git a/lib/cpio.c b/lib/cpio.c index 86258d6cd..f97954673 100644 --- a/lib/cpio.c +++ b/lib/cpio.c @@ -1,9 +1,8 @@ #include "system.h" +#include <rpmio.h> #include "cpio.h" -/*@access FD_t@*/ - #define CPIO_NEWC_MAGIC "070701" #define CPIO_CRC_MAGIC "070702" #define TRAILER "TRAILER!!!" @@ -79,7 +78,7 @@ static inline off_t ourread(FD_t cfd, /*@out@*/void * buf, size_t size) { off_t i = saferead(cfd, buf, size); if (i > 0) - cfd->fd_cpioPos += i; + fdSetCpioPos(cfd, fdGetCpioPos(cfd) + i); return i; } @@ -88,7 +87,7 @@ static inline void padinfd(FD_t cfd, int modulo) int buf[10]; int amount; - amount = (modulo - cfd->fd_cpioPos % modulo) % modulo; + amount = (modulo - fdGetCpioPos(cfd) % modulo) % modulo; (void)ourread(cfd, buf, amount); } @@ -342,7 +341,7 @@ static int expandRegular(FD_t cfd, struct cpioHeader * hdr, } } - ofd = fdOpen(hdr->path, O_CREAT | O_WRONLY, 0); + ofd = fdio->open(hdr->path, O_CREAT | O_WRONLY, 0); if (Ferror(ofd)) return CPIOERR_OPEN_FAILED; @@ -366,7 +365,7 @@ static int expandRegular(FD_t cfd, struct cpioHeader * hdr, /* don't call this with fileSize == fileComplete */ if (!rc && cb && left) { cbInfo.fileComplete = hdr->size - left; - cbInfo.bytesProcessed = cfd->fd_cpioPos; + cbInfo.bytesProcessed = fdGetCpioPos(cfd); cb(&cbInfo, cbData); } } @@ -517,7 +516,7 @@ int cpioInstallArchive(FD_t cfd, struct cpioFileMapping * mappings, struct hardLink * links = NULL; struct hardLink * li = NULL; - cfd->fd_cpioPos = 0; + fdSetCpioPos(cfd, 0); if (failedFile) *failedFile = NULL; @@ -645,7 +644,7 @@ int cpioInstallArchive(FD_t cfd, struct cpioFileMapping * mappings, cbInfo.file = ch.path; cbInfo.fileSize = ch.size; cbInfo.fileComplete = ch.size; - cbInfo.bytesProcessed = cfd->fd_cpioPos; + cbInfo.bytesProcessed = fdGetCpioPos(cfd); cb(&cbInfo, cbData); } @@ -747,7 +746,7 @@ static int writeFile(FD_t cfd, struct stat sb, struct cpioFileMapping * map, size_t nmapped; #endif - datafd = fdOpen(map->fsPath, O_RDONLY, 0); + datafd = fdio->open(map->fsPath, O_RDONLY, 0); if (Ferror(datafd)) return CPIOERR_OPEN_FAILED; diff --git a/lib/falloc.c b/lib/falloc.c index 7156e6017..334edb0d9 100644 --- a/lib/falloc.c +++ b/lib/falloc.c @@ -43,12 +43,12 @@ FD_t fadOpen(const char * path, int flags, int perms) if (flags & O_WRONLY) return NULL; - fd = ufdOpen(path, flags, perms); + fd = ufdio->open(path, flags, perms); if (Ferror(fd)) /* XXX Fstrerror */ return NULL; - fdSetIoCookie(fd, &fadio); + fdSetIoCookie(fd, fadio); fadSetFirstFree(fd, 0); fadSetFileSize(fd, Fseek(fd, 0, SEEK_END)); diff --git a/lib/falloc.h b/lib/falloc.h index 1e1f0baf9..fb1d569b8 100644 --- a/lib/falloc.h +++ b/lib/falloc.h @@ -9,7 +9,7 @@ extern "C" { #endif -/*@only@*/ FD_t fadOpen (const char * path, int flags, int perms); +FD_t fadOpen (const char * path, int flags, int perms); unsigned int fadAlloc (FD_t fd, unsigned int size); /* 0 on failure */ void fadFree (FD_t fd, unsigned int offset); @@ -58,6 +58,7 @@ int inet_aton(const char *cp, struct in_addr *inp); #endif #include <rpmurl.h> +/*@access urlinfo@*/ #ifdef __MINT__ # ifndef EAGAIN @@ -84,7 +85,7 @@ void urlSetCallback(rpmCallbackFunction notify, void *notifyData, int notifyCoun urlNotifyCount = (notifyCount >= 0) ? notifyCount : 4096; } -static int checkResponse(int fd, int secs, int *ecp, /*@out@*/char ** str) { +static int checkResponse(int fd, int secs, int *ecp, /*@out@*/ char ** str) { static char buf[BUFFER_SIZE + 1]; int bufLength = 0; fd_set emptySet, readSet; @@ -190,7 +191,7 @@ static int checkResponse(int fd, int secs, int *ecp, /*@out@*/char ** str) { return rc; } -static int ftpCheckResponse(urlinfo *u, /*@out@*/char ** str) { +static int ftpCheckResponse(urlinfo u, /*@out@*/ char ** str) { int ec = 0; int rc = checkResponse(u->ftpControl, ftpTimeoutSecs, &ec, str); @@ -209,7 +210,7 @@ static int ftpCheckResponse(urlinfo *u, /*@out@*/char ** str) { return rc; } -static int ftpCommand(urlinfo *u, char * command, ...) { +static int ftpCommand(urlinfo u, char * command, ...) { va_list ap; int len; char * s; @@ -312,7 +313,7 @@ static int tcpConnect(const char *host, int port) return sock; } -int httpOpen(urlinfo *u) +int httpOpen(urlinfo u) { int sock; const char *host; @@ -364,7 +365,7 @@ int httpOpen(urlinfo *u) return sock; } -int ftpOpen(urlinfo *u) +int ftpOpen(urlinfo u) { const char * host; const char * user; @@ -422,7 +423,7 @@ errxit: return rc; } -static int copyData( /*@only@*/ FD_t sfd, FD_t tfd) { +static int copyData( /*@killref@*/ FD_t sfd, FD_t tfd) { char buf[BUFFER_SIZE]; fd_set emptySet, readSet; struct timeval timeout; @@ -487,11 +488,14 @@ static int copyData( /*@only@*/ FD_t sfd, FD_t tfd) { } int ftpAbort(FD_t fd) { - urlinfo *u = ufdGetUrlinfo(fd); + urlinfo u = ufdGetUrlinfo(fd); char buf[BUFFER_SIZE]; int rc; int tosecs = ftpTimeoutSecs; + if (u == NULL) + return FTPERR_UNKNOWN; /* XXX W2DO? */ + DBG(fdDebug(fd), (stderr, "-> ABOR\n")); sprintf(buf, "%c%c%c", (char)IAC, (char)IP, (char)IAC); @@ -500,6 +504,7 @@ int ftpAbort(FD_t fd) { if (write(u->ftpControl, buf, 7) != 7) { close(u->ftpControl); u->ftpControl = -1; + urlFree(u, "ufdGetUrlinfo (from ftpAbort #1)"); return FTPERR_SERVER_IO_ERROR; } if (Fileno(fd) >= 0) { @@ -515,11 +520,15 @@ int ftpAbort(FD_t fd) { ftpTimeoutSecs = tosecs; if (Fileno(fd) >= 0) - Fclose(fd); + fdio->close(fd); + urlFree(u, "ufdGetUrlinfo (from ftpAbort)"); return 0; } -static int ftpGetFileDone(urlinfo *u) { +static int ftpGetFileDone(urlinfo u) { + if (u == NULL) + return FTPERR_UNKNOWN; /* XXX W2DO? */ + if (u->ftpGetFileDoneNeeded) { u->ftpGetFileDoneNeeded = 0; if (ftpCheckResponse(u, NULL)) @@ -530,7 +539,7 @@ static int ftpGetFileDone(urlinfo *u) { int ftpGetFileDesc(FD_t fd) { - urlinfo * u = ufdGetUrlinfo(fd); + urlinfo u = ufdGetUrlinfo(fd); const char *remotename; struct sockaddr_in dataAddress; int i, j; @@ -539,6 +548,9 @@ int ftpGetFileDesc(FD_t fd) char * retrCommand; int rc; + if (u == NULL) + return FTPERR_UNKNOWN; /* XXX W2DO? */ + remotename = u->path; /* @@ -551,11 +563,15 @@ int ftpGetFileDesc(FD_t fd) rc = ftpGetFileDone(u); DBG(fdDebug(fd), (stderr, "-> PASV\n")); - if (write(u->ftpControl, "PASV\r\n", 6) != 6) + if (write(u->ftpControl, "PASV\r\n", 6) != 6) { + urlFree(u, "ufdGetUrlinfo (from ftpGetFileDesc #1)"); return FTPERR_SERVER_IO_ERROR; + } - if ((rc = ftpCheckResponse(u, &passReply))) + if ((rc = ftpCheckResponse(u, &passReply))) { + urlFree(u, "ufdGetUrlinfo (from ftpGetFileDesc #2)"); return FTPERR_PASSIVE_ERROR; + } chptr = passReply; while (*chptr && *chptr != '(') chptr++; @@ -578,6 +594,7 @@ int ftpGetFileDesc(FD_t fd) dataAddress.sin_family = AF_INET; if (sscanf(chptr, "%d,%d", &i, &j) != 2) { + urlFree(u, "ufdGetUrlinfo (from ftpGetFileDesc #3)"); return FTPERR_PASSIVE_ERROR; } dataAddress.sin_port = htons((((unsigned)i) << 8) + j); @@ -587,12 +604,16 @@ int ftpGetFileDesc(FD_t fd) if (*chptr == ',') *chptr = '.'; } - if (!inet_aton(passReply, &dataAddress.sin_addr)) + if (!inet_aton(passReply, &dataAddress.sin_addr)) { + urlFree(u, "ufdGetUrlinfo (from ftpGetFileDesc #4)"); return FTPERR_PASSIVE_ERROR; + } ufdSetFd(fd, socket(AF_INET, SOCK_STREAM, IPPROTO_IP)); - if (Fileno(fd) < 0) + if (Fileno(fd) < 0) { + urlFree(u, "ufdGetUrlinfo (from ftpGetFileDesc #5)"); return FTPERR_FAILED_CONNECT; + } retrCommand = alloca(strlen(remotename) + 20); sprintf(retrCommand, "RETR %s\r\n", remotename); @@ -603,20 +624,24 @@ int ftpGetFileDesc(FD_t fd) if (errno == EINTR) continue; Fclose(fd); + urlFree(u, "ufdGetUrlinfo (from ftpGetFileDesc #6)"); return FTPERR_FAILED_DATA_CONNECT; } DBG(fdDebug(fd), (stderr, "-> %s", retrCommand)); if (write(u->ftpControl, retrCommand, i) != i) { + urlFree(u, "ufdGetUrlinfo (from ftpGetFileDesc #7)"); return FTPERR_SERVER_IO_ERROR; } if ((rc = ftpCheckResponse(u, NULL))) { Fclose(fd); + urlFree(u, "ufdGetUrlinfo (from ftpGetFileDesc #8)"); return rc; } u->ftpGetFileDoneNeeded = 1; + urlFree(u, "ufdGetUrlinfo (from ftpGetFileDesc)"); return 0; } @@ -627,27 +652,37 @@ int httpGetFile(FD_t sfd, FD_t tfd) { int ftpGetFile(FD_t sfd, FD_t tfd) { /* XXX sfd will be freed by copyData -- grab sfd->fd_url now */ - urlinfo * sfu = ufdGetUrlinfo(sfd); + urlinfo sfu = ufdGetUrlinfo(sfd); int rc; /* XXX normally sfd = ufdOpen(...) and this code does not execute */ if (Fileno(sfd) < 0 && (rc = ftpGetFileDesc(sfd)) < 0) { Fclose(sfd); + urlFree(sfu, "ufdGetUrlinfo (from ftpGetFile #1)"); return rc; } rc = copyData(sfd, tfd); - if (rc < 0) + if (rc < 0) { + urlFree(sfu, "ufdGetUrlinfo (from ftpGetFile #2)"); return rc; + } - return ftpGetFileDone(sfu); + rc = ftpGetFileDone(sfu); + urlFree(sfu, "ufdGetUrlinfo (from ftpGetFile)"); + return rc; } int ftpClose(FD_t fd) { - urlinfo * u = ufdGetUrlinfo(fd); - int fdno = u->ftpControl; - if (fdno >= 0) + urlinfo u = ufdGetUrlinfo(fd); + int fdno; + + if (u == NULL) + return FTPERR_UNKNOWN; /* XXX W2DO? */ + + if ((fdno = u->ftpControl) >= 0) close(fdno); + urlFree(u, "ufdGetUrlinfo (from ftpClose)"); return 0; } diff --git a/lib/header.c b/lib/header.c index adfb148ef..a7dae6255 100644 --- a/lib/header.c +++ b/lib/header.c @@ -14,6 +14,7 @@ #define ntohs(_x) (_x) #define htonl(_x) (_x) #define htons(_x) (_x) +/*@-observertrans@*/ /* XXX FIXME */ #else #include <netinet/in.h> #endif /* __LCLINT__ */ @@ -1397,7 +1398,7 @@ static void freeFormat( /*@only@*/ struct sprintfToken * format, int num) freeFormat(format[i].u.cond.elseFormat, format[i].u.cond.numElseTokens); break; - case PTOK_NONE: + case PTOK_NONE: case PTOK_TAG: case PTOK_STRING: default: diff --git a/lib/macro.c b/lib/macro.c index 10e329e6b..5b9320ff2 100644 --- a/lib/macro.c +++ b/lib/macro.c @@ -23,14 +23,16 @@ #define RPMERR_BADSPEC stderr #undef _ #define _(x) x -#define xfree(_p) free((void *)_p) +#define xfree(_p) free((void *)_p) typedef int FD_t; -#define Ferror(_x) (_x) -#define fdOpen open +#define Ferror(_x) (_x) +#define fdOpen open #define Fread(_b, _s, _n, _fd) read(_fd, _b, _s) #define Fclose(_fd) close(_fd) #else #include <rpmlib.h> +#include <rpmio.h> +#define fdOpen fdio->open #endif #include <rpmmacro.h> diff --git a/lib/misc.c b/lib/misc.c index dd42f9a76..39a7a4482 100644 --- a/lib/misc.c +++ b/lib/misc.c @@ -387,7 +387,7 @@ int makeTempFile(const char * prefix, const char ** fnptr, FD_t * fdptr) { default: return 1; } - fd = fdOpen(fn, O_CREAT | O_RDWR | O_EXCL, 0700); + fd = fdio->open(fn, O_CREAT | O_RDWR | O_EXCL, 0700); } while (Ferror(fd) && errno == EEXIST); if (!stat(fn, &sb) && S_ISLNK(sb.st_mode)) { diff --git a/lib/oldheader.c b/lib/oldheader.c index 7f69634bd..56de86915 100644 --- a/lib/oldheader.c +++ b/lib/oldheader.c @@ -145,7 +145,7 @@ char * oldhdrReadFromFile(char * filename, struct oldrpmHeader * header) { char * rc; FD_t fd; - fd = fdOpen(filename, O_RDONLY, 0); + fd = fdio->open(filename, O_RDONLY, 0); if (Ferror(fd)) /* XXX Fstrerror */ return strerror(errno); diff --git a/lib/query.c b/lib/query.c index 2242db9c8..9dd7bd049 100644 --- a/lib/query.c +++ b/lib/query.c @@ -458,7 +458,7 @@ int rpmQueryVerify(QVA_t *qva, enum rpmQVSources source, const char * arg, case RPMQV_RPM: { FD_t fd; - fd = ufdOpen(arg, O_RDONLY, 0); + fd = ufdio->open(arg, O_RDONLY, 0); if (Ferror(fd)) { /* XXX Fstrerror */ fprintf(stderr, _("open of %s failed: %s\n"), arg,urlStrerror(arg)); diff --git a/lib/rpmchecksig.c b/lib/rpmchecksig.c index 06ef62e19..c4e39c9b3 100644 --- a/lib/rpmchecksig.c +++ b/lib/rpmchecksig.c @@ -27,10 +27,10 @@ static int manageFile(FD_t *fdp, const char **fnp, int flags, int rc) /* open a file and set *fdp */ if (*fdp == NULL && fnp && *fnp) { mode_t mode = (flags & O_CREAT) ? 0644 : 0; - fd = fdOpen(*fnp, flags, mode); + fd = fdio->open(*fnp, flags, mode); if (Ferror(fd)) { /* XXX Fstrerror */ - fprintf(stderr, _("%s: fdOpen failed: %s\n"), *fnp, + fprintf(stderr, _("%s: open failed: %s\n"), *fnp, strerror(errno)); return 1; } diff --git a/lib/rpminstall.c b/lib/rpminstall.c index 64319303f..dc018ff6f 100644 --- a/lib/rpminstall.c +++ b/lib/rpminstall.c @@ -46,7 +46,7 @@ static void * showProgress(const Header h, const rpmCallbackType what, switch (what) { case RPMCALLBACK_INST_OPEN_FILE: - fd = fdOpen(filename, O_RDONLY, 0); + fd = fdio->open(filename, O_RDONLY, 0); return fd; case RPMCALLBACK_INST_CLOSE_FILE: @@ -186,7 +186,7 @@ int rpmInstall(const char * rootdir, const char ** argv, int transFlags, would create all sorts of confusion later. */ for (filename = packages; *filename; filename++) { - fd = fdOpen(*filename, O_RDONLY, 0); + fd = fdio->open(*filename, O_RDONLY, 0); if (Ferror(fd)) { /* XXX Fstrerror */ rpmMessage(RPMMESS_ERROR, _("cannot open file %s\n"), *filename); @@ -328,7 +328,7 @@ int rpmInstall(const char * rootdir, const char ** argv, int transFlags, if (numSourcePackages && !stopInstall) { for (i = 0; i < numSourcePackages; i++) { - fd = fdOpen(sourcePackages[i], O_RDONLY, 0); + fd = fdio->open(sourcePackages[i], O_RDONLY, 0); if (Ferror(fd)) { /* XXX Fstrerror */ rpmMessage(RPMMESS_ERROR, _("cannot open file %s\n"), @@ -456,7 +456,7 @@ int rpmInstallSource(const char * rootdir, const char * arg, const char ** specF FD_t fd; int rc; - fd = ufdOpen(arg, O_RDONLY, 0); + fd = ufdio->open(arg, O_RDONLY, 0); if (Ferror(fd)) { /* XXX Fstrerror */ rpmMessage(RPMMESS_ERROR, _("cannot open %s\n"), arg); diff --git a/lib/rpmio.h b/lib/rpmio.h index 12b2c321e..3297cb1ac 100644 --- a/lib/rpmio.h +++ b/lib/rpmio.h @@ -6,28 +6,51 @@ #include <unistd.h> #include <stdio.h> -typedef /*@abstract@*/ struct _FD { - int fd_fd; /* file descriptor */ -/*@owned@*/ void * fd_bzd; /* bzdio: I/O cookie */ -/*@owned@*/ void * fd_gzd; /* gzdio: I/O cookie */ -/*@dependent@*/ void * fd_url; /* ufdio: URL info */ -/*@observer@*/ const void *fd_errcookie;/* opaque error token */ - int fd_errno; /* last system errno encountered */ -/*@observer@*/ const char *fd_errstr; - int fd_flags; - unsigned int firstFree; /* fadio: */ - long int fileSize; /* fadio: */ - long int fd_cpioPos; /* cfdio: */ - long int fd_pos; -/*@dependent@*/ cookie_io_functions_t * fd_io; -} *FD_t; +typedef /*@abstract@*/ /*@refcounted@*/ struct _FD_s * FD_t; +typedef /*@observer@*/ struct FDIO_s * FDIO_t; + +typedef /*@null@*/ FD_t fdio_ref_function_t ( /*@only@*/ void * cookie, const char *msg, const char *file, unsigned line); +typedef /*@null@*/ FD_t fdio_deref_function_t ( /*@only@*/ FD_t fd, const char *msg, const char *file, unsigned line); + +typedef /*@null@*/ FD_t fdio_new_function_t (FDIO_t iop, const char *msg, const char *file, unsigned line); + +typedef int fdio_fileno_function_t (void * cookie); + +typedef FD_t fdio_open_function_t (const char *path, int flags, mode_t mode); + +typedef int fdio_mkdir_function_t (const char *path, mode_t mode); +typedef int fdio_chdir_function_t (const char *path); +typedef int fdio_rmdir_function_t (const char *path); +typedef int fdio_rename_function_t (const char *oldpath, const char *newpath); +typedef int fdio_unlink_function_t (const char *path); + +struct FDIO_s { + cookie_read_function_t *read; + cookie_write_function_t *write; + cookie_seek_function_t *seek; + cookie_close_function_t *close; + + fdio_ref_function_t *ref; + fdio_deref_function_t *deref; + fdio_new_function_t *new; + fdio_fileno_function_t *fileno; + + fdio_open_function_t *open; +#ifdef NOTYET + fdio_mkdir_function_t *mkdir; + fdio_chdir_function_t *chdir; + fdio_rmdir_function_t *rmdir; + fdio_rename_function_t *rename; + fdio_unlink_function_t *unlink; +#endif +}; /*@observer@*/ const char * Fstrerror(FD_t fd); size_t Fread (/*@out@*/ void * buf, size_t size, size_t nmemb, FD_t fd); size_t Fwrite (const void *buf, size_t size, size_t nmemb, FD_t fd); int Fseek (FD_t fd, long int offset, int whence); -int Fclose ( /*@only@*/ FD_t fd); +int Fclose ( /*@killref@*/ FD_t fd); FILE * Fopen (const char * path, const char * fmode); int Ferror (FD_t fd); @@ -54,31 +77,31 @@ extern "C" { int timedRead(FD_t fd, /*@out@*/void * bufptr, int length); -extern /*@only@*/ /*@null@*/ FD_t fdNew(cookie_io_functions_t * iop); -extern int fdValid(FD_t fd); - -extern /*@only@*/ /*@null@*/ FD_t fdOpen(const char * pathname, int flags, mode_t mode); -extern /*@only@*/ /*@null@*/ FD_t fdDup(int fdno); -extern /*@dependent@*/ /*@null@*/ FILE *fdFdopen( /*@only@*/ FD_t fd, const char * mode); +/*@null@*/ const FDIO_t fdGetIoCookie(FD_t fd); +void fdSetIoCookie(FD_t fd, FDIO_t iop); +#define fdLink(_fd, _msg) fdio->ref(_fd, _msg, __FILE__, __LINE__) +#define fdFree(_fd, _msg) fdio->deref(_fd, _msg, __FILE__, __LINE__) +#define fdNew(_iop, _msg) fdio->new(_iop, _msg, __FILE__, __LINE__) -/*@observer@*/ /*@null@*/ const cookie_io_functions_t * fdGetIoCookie(FD_t fd); -void fdSetIoCookie(FD_t fd, cookie_io_functions_t * io); +extern /*@null@*/ FD_t fdDup(int fdno); +extern /*@null@*/ FILE *fdFdopen( /*@only@*/ void * cookie, const char * mode); +long int fdGetCpioPos(FD_t fd); +void fdSetCpioPos(FD_t fd, long int cpioPos); int fdDebug(FD_t fd); void fdDebugOn(FD_t fd); void fdDebugOff(FD_t fd); -extern cookie_io_functions_t fdio; +/*@observer@*/ extern FDIO_t fdio; /* * Support for FTP/HTTP I/O. */ -/*@only@*/ FD_t ufdOpen(const char * pathname, int flags, mode_t mode); -/*@dependent@*/ void * ufdGetUrlinfo(FD_t fd); +/*@dependent@*/ /*@null@*/ void * ufdGetUrlinfo(FD_t fd); void ufdSetFd(FD_t fd, int fdno); /*@observer@*/ const char * urlStrerror(const char * url); -extern cookie_io_functions_t ufdio; +/*@observer@*/ extern FDIO_t ufdio; /* * Support for first fit File Allocation I/O. @@ -89,7 +112,7 @@ void fadSetFileSize(FD_t fd, long int fileSize); unsigned int fadGetFirstFree(FD_t fd); void fadSetFirstFree(FD_t fd, unsigned int firstFree); -extern cookie_io_functions_t fadio; +/*@observer@*/ extern FDIO_t fadio; #ifdef HAVE_ZLIB_H /* @@ -100,13 +123,13 @@ extern cookie_io_functions_t fadio; extern /*@dependent@*/ /*@null@*/ gzFile * gzdFileno(FD_t fd); -extern /*@only@*/ /*@null@*/ FD_t gzdOpen(const char * pathname, const char * mode); +extern /*@null@*/ FD_t gzdOpen(const char * pathname, const char * mode); -extern /*@only@*/ /*@null@*/ FD_t gzdFdopen( /*@only@*/ FD_t fd, const char * mode); +extern /*@null@*/ FD_t gzdFdopen( /*@only@*/ void * cookie, const char * mode); extern int gzdFlush(FD_t fd); -extern cookie_io_functions_t gzdio; +/*@observer@*/ extern FDIO_t gzdio; #endif /* HAVE_ZLIB_H */ @@ -119,13 +142,13 @@ extern cookie_io_functions_t gzdio; extern /*@dependent@*/ /*@null@*/ BZFILE * bzdFileno(FD_t fd); -extern /*@only@*/ /*@null@*/ FD_t bzdOpen(const char * pathname, const char * mode); +extern /*@null@*/ FD_t bzdOpen(const char * pathname, const char * mode); -extern /*@only@*/ /*@null@*/ FD_t bzdFdopen( /*@only@*/ FD_t fd, const char * mode); +extern /*@null@*/ FD_t bzdFdopen( /*@only@*/ void * cookie, const char * mode); extern int bzdFlush(FD_t fd); -extern cookie_io_functions_t bzdio; +/*@observer@*/ extern FDIO_t bzdio; #endif /* HAVE_BZLIB_H */ diff --git a/lib/rpmrc.c b/lib/rpmrc.c index e4a6b1edf..47585d4d7 100644 --- a/lib/rpmrc.c +++ b/lib/rpmrc.c @@ -543,7 +543,7 @@ int rpmReadRC(const char * rcfiles) strcat(fn, r); /* Read another rcfile */ - fd = ufdOpen(fn, O_RDONLY, 0); + fd = ufdio->open(fn, O_RDONLY, 0); if (Ferror(fd)) { /* XXX Only /usr/lib/rpm/rpmrc must exist in default rcfiles list */ if (rcfiles == defrcfiles && myrcfiles != r) @@ -658,7 +658,7 @@ static int doReadRC(FD_t fd, const char * filename) /*@notreached@*/ } - fdinc = fdOpen(fn, O_RDONLY, 0); + fdinc = fdio->open(fn, O_RDONLY, 0); if (Ferror(fdinc)) { /* XXX Fstrerror */ rpmError(RPMERR_RPMRC, _("cannot open %s at %s:%d"), @@ -830,7 +830,7 @@ static void defaultMachine(const char ** arch, const char ** os) { !strncmp(un.release, "4.0", 3)) { /* we are on ncr-sysv4 */ char *prelid = NULL; - FD_t fd = fdOpen("/etc/.relid", O_RDONLY, 0700); + FD_t fd = fdio->open("/etc/.relid", O_RDONLY, 0700); if (!Ferror(fd)) { chptr = (char *) xcalloc(1, 256); if (chptr != NULL) { diff --git a/lib/rpmurl.h b/lib/rpmurl.h index 445e41ae5..27c805222 100644 --- a/lib/rpmurl.h +++ b/lib/rpmurl.h @@ -29,7 +29,7 @@ typedef enum { URL_IS_HTTP = 4 } urltype; -typedef struct urlinfo { +typedef /*@abstract@*/ /*@refcounted@*/ struct urlinfo { const char *url; /* copy of original url */ const char *service; const char *user; @@ -44,7 +44,8 @@ typedef struct urlinfo { int ftpControl; int ftpGetFileDoneNeeded; int openError; /* Type of open failure */ -} urlinfo; +/*@refs@*/ int nrefs; +} *urlinfo; #ifdef __cplusplus extern "C" { @@ -53,21 +54,31 @@ extern "C" { /*@observer@*/ const char *const ftpStrerror(int ftpErrno); void urlSetCallback(rpmCallbackFunction notify, void *notifyData, int notifyCount); -int httpOpen(urlinfo *u); -int ftpOpen(urlinfo *u); +int httpOpen(urlinfo u); +int ftpOpen(urlinfo u); -int httpGetFile( /*@only@*/ FD_t sfd, FD_t tfd); -int ftpGetFile( /*@only@*/ FD_t sfd, FD_t tfd); +int httpGetFile( /*@killref@*/ FD_t sfd, FD_t tfd); +int ftpGetFile( /*@killref@*/ FD_t sfd, FD_t tfd); int ftpGetFileDesc(FD_t); -int ftpAbort( /*@only@*/ FD_t fd); -int ftpClose( /*@only@*/ FD_t fd); +int ftpAbort( /*@killref@*/ FD_t fd); +int ftpClose( /*@killref@*/ FD_t fd); -/*@only@*/ urlinfo *newUrlinfo(void); -void freeUrlinfo( /*@only@*/ urlinfo *u); -void freeUrlinfoCache(void); +urlinfo urlLink(urlinfo u, const char *msg); +urlinfo XurlLink(urlinfo u, const char *msg, const char *file, unsigned line); +#define urlLink(_u, _msg) XurlLink(_u, _msg, __FILE__, __LINE__) + +urlinfo urlNew(const char *msg); +urlinfo XurlNew(const char *msg, const char *file, unsigned line); +#define urlNew(_msg) XurlNew(_msg, __FILE__, __LINE__) + +void urlFree( /*@killref@*/ urlinfo u, const char *msg); +void XurlFree( /*@killref@*/ urlinfo u, const char *msg, const char *file, unsigned line); +#define urlFree(_u, _msg) XurlFree(_u, _msg, __FILE__, __LINE__) + +void urlFreeCache(void); urltype urlIsURL(const char * url); -int urlSplit(const char *url, /*@out@*/ urlinfo **u); +int urlSplit(const char *url, /*@out@*/ urlinfo *u); int urlGetFile(const char * url, const char * dest); diff --git a/lib/signature.c b/lib/signature.c index 2271cc724..afdad05a4 100644 --- a/lib/signature.c +++ b/lib/signature.c @@ -292,7 +292,7 @@ static int makePGPSignature(const char *file, /*@out@*/void **sig, /*@out@*/int_ { FD_t fd; int rc; - fd = fdOpen(sigfile, O_RDONLY, 0); + fd = fdio->open(sigfile, O_RDONLY, 0); rc = timedRead(fd, *sig, *size); unlink(sigfile); Fclose(fd); @@ -369,7 +369,7 @@ static int makeGPGSignature(const char *file, /*@out@*/void **sig, /*@out@*/int_ { FD_t fd; int rc; - fd = fdOpen(sigfile, O_RDONLY, 0); + fd = fdio->open(sigfile, O_RDONLY, 0); rc = timedRead(fd, *sig, *size); unlink(sigfile); Fclose(fd); @@ -506,7 +506,7 @@ static int verifyPGPSignature(const char *datafile, void *sig, sigfile = tempnam(tmppath, "rpmsig"); xfree(tmppath); } - sfd = fdOpen(sigfile, O_WRONLY|O_CREAT|O_TRUNC, 0644); + sfd = fdio->open(sigfile, O_WRONLY|O_CREAT|O_TRUNC, 0644); (void)Fwrite(sig, count, 1, sfd); Fclose(sfd); @@ -601,7 +601,7 @@ static int verifyGPGSignature(const char *datafile, void *sig, sigfile = tempnam(tmppath, "rpmsig"); xfree(tmppath); } - sfd = fdOpen(sigfile, O_WRONLY|O_CREAT|O_TRUNC, 0644); + sfd = fdio->open(sigfile, O_WRONLY|O_CREAT|O_TRUNC, 0644); (void)Fwrite(sig, count, 1, sfd); Fclose(sfd); diff --git a/lib/uninstall.c b/lib/uninstall.c index dec6de481..267d19529 100644 --- a/lib/uninstall.c +++ b/lib/uninstall.c @@ -313,7 +313,7 @@ static int runScript(Header h, const char * root, int progArgc, const char ** pr if (rpmIsVerbose()) { out = fdDup(Fileno(errfd)); } else { - out = fdOpen("/dev/null", O_WRONLY, 0); + out = fdio->open("/dev/null", O_WRONLY, 0); if (Ferror(out)) out = fdDup(Fileno(errfd)); } @@ -9,17 +9,34 @@ #include <netinet/in.h> #endif /* __LCLINT__ */ -#include "rpmbuild.h" - +#include <rpmbuild.h> +#include <rpmio.h> #include <rpmurl.h> -typedef /*@owned@*/ urlinfo * urlinfop; -/*@only@*/ /*@null@*/ static urlinfop *uCache = NULL; +/*@access urlinfo@*/ + +#define RPMURL_DEBUG_IO 0x40000000 +#define RPMURL_DEBUG_REFS 0x20000000 + +static int url_debug = 0; +#define DBG(_f, _m, _x) if ((url_debug | (_f)) & (_m)) fprintf _x + +#define DBGIO(_f, _x) DBG((_f), RPMURL_DEBUG_IO, _x) +#define DBGREFS(_f, _x) DBG((_f), RPMURL_DEBUG_REFS, _x) + +/*@only@*/ /*@null@*/ static urlinfo *uCache = NULL; static int uCount = 0; -urlinfo *newUrlinfo(void) +urlinfo XurlLink(urlinfo u, const char *msg, const char *file, unsigned line) +{ + u->nrefs++; +DBGREFS(0, (stderr, "--> url %p ++ %d %s at %s:%u\n", u, u->nrefs, msg, file, line)); + return u; +} + +urlinfo XurlNew(const char *msg, const char *file, unsigned line) { - urlinfo *u; + urlinfo u; if ((u = xmalloc(sizeof(*u))) == NULL) return NULL; memset(u, 0, sizeof(*u)); @@ -27,11 +44,15 @@ urlinfo *newUrlinfo(void) u->port = -1; u->ftpControl = -1; u->ftpGetFileDoneNeeded = 0; - return u; + u->nrefs = 0; + return XurlLink(u, msg, file, line); } -void freeUrlinfo(urlinfo *u) +void XurlFree(urlinfo u, const char *msg, const char *file, unsigned line) { +DBGREFS(0, (stderr, "--> url %p -- %d %s at %s:%u\n", u, u->nrefs, msg, file, line)); + if (--u->nrefs > 0) + return; if (u->ftpControl >= 0) close(u->ftpControl); FREE(u->url); @@ -44,15 +65,18 @@ void freeUrlinfo(urlinfo *u) FREE(u->proxyu); FREE(u->proxyh); - FREE(u); + /*@-refcounttrans@*/ FREE(u); /*@-refcounttrans@*/ } -void freeUrlinfoCache(void) +void urlFreeCache(void) { int i; for (i = 0; i < uCount; i++) { - if (uCache[i]) - freeUrlinfo(uCache[i]); + if (uCache[i] == NULL) continue; + if (uCache[i]->nrefs != 1) + fprintf(stderr, "==> nrefs(%d) != 1 (%s %s)\n", uCache[i]->nrefs, + uCache[i]->host, uCache[i]->service); + urlFree(uCache[i], "uCache"); } if (uCache) free(uCache); @@ -69,10 +93,10 @@ static int urlStrcmp(const char *str1, const char *str2) return 0; } -static void findUrlinfo(urlinfo **uret, int mustAsk) +static void urlFind(urlinfo *uret, int mustAsk) { - urlinfo *u; - urlinfo **empty; + urlinfo u; + int ucx; int i; if (uret == NULL) @@ -80,12 +104,12 @@ static void findUrlinfo(urlinfo **uret, int mustAsk) u = *uret; - empty = NULL; + ucx = -1; for (i = 0; i < uCount; i++) { - urlinfo *ou; + urlinfo ou; if ((ou = uCache[i]) == NULL) { - if (empty == NULL) - empty = &uCache[i]; + if (ucx < 0) + ucx = i; continue; } @@ -109,28 +133,32 @@ static void findUrlinfo(urlinfo **uret, int mustAsk) } if (i == uCount) { - if (empty == NULL) { - uCount++; + if (ucx < 0) { + ucx = uCount++; if (uCache) uCache = xrealloc(uCache, sizeof(*uCache) * uCount); else uCache = xmalloc(sizeof(*uCache)); - empty = &uCache[i]; } - *empty = u; + uCache[i] = urlLink(u, "uCache (miss)"); + urlFree(u, "urlSplit (from urlFind miss)"); } else { - /* Swap original url and path into the cached structure */ + /* XXX Swap original url and path into the cached structure */ const char *up = uCache[i]->path; - uCache[i]->path = u->path; + ucx = i; + uCache[ucx]->path = u->path; u->path = up; - up = uCache[i]->url; - uCache[i]->url = u->url; + up = uCache[ucx]->url; + uCache[ucx]->url = u->url; u->url = up; - freeUrlinfo(u); + urlFree(u, "urlSplit (from urlFind hit)"); } /* This URL is now cached. */ - *uret = u = uCache[i]; + + u = urlLink(uCache[i], "uCache"); + *uret = u; + urlFree(u, "uCache (from urlFind)"); /* Zap proxy host and port in case they have been reset */ u->proxyp = -1; @@ -141,9 +169,9 @@ static void findUrlinfo(urlinfo **uret, int mustAsk) if (mustAsk || (u->user != NULL && u->password == NULL)) { char * prompt; - FREE(u->password); prompt = alloca(strlen(u->host) + strlen(u->user) + 40); sprintf(prompt, _("Password for %s@%s: "), u->user, u->host); + FREE(u->password); u->password = xstrdup( /*@-unrecog@*/ getpass(prompt) /*@=unrecog@*/ ); } @@ -235,19 +263,19 @@ urltype urlIsURL(const char * url) * Split URL into components. The URL can look like * service://user:password@host:port/path */ -int urlSplit(const char * url, urlinfo **uret) +int urlSplit(const char * url, urlinfo *uret) { - urlinfo *u; + urlinfo u; char *myurl; char *s, *se, *f, *fe; if (uret == NULL) return -1; - if ((u = newUrlinfo()) == NULL) + if ((u = urlNew("urlSplit")) == NULL) return -1; if ((se = s = myurl = xstrdup(url)) == NULL) { - freeUrlinfo(u); + urlFree(u, "urlSplit (error #1)"); return -1; } @@ -259,7 +287,7 @@ int urlSplit(const char * url, urlinfo **uret) if (*se == '\0') { /* XXX can't find path */ if (myurl) free(myurl); - freeUrlinfo(u); + urlFree(u, "urlSplit (error #2)"); return -1; } /* Item was service. Save service and go for the rest ...*/ @@ -304,7 +332,7 @@ int urlSplit(const char * url, urlinfo **uret) if (!(end && *end == '\0')) { rpmMessage(RPMMESS_ERROR, _("url port must be a number\n")); if (myurl) free(myurl); - freeUrlinfo(u); + urlFree(u, "urlSplit (error #3)"); return -1; } } @@ -325,7 +353,7 @@ int urlSplit(const char * url, urlinfo **uret) if (myurl) free(myurl); if (uret) { *uret = u; - findUrlinfo(uret, 0); + urlFind(uret, 0); } return 0; } @@ -334,9 +362,9 @@ int urlGetFile(const char * url, const char * dest) { int rc; FD_t sfd = NULL; FD_t tfd = NULL; - urlinfo * sfu; + urlinfo sfu; - sfd = ufdOpen(url, O_RDONLY, 0); + sfd = ufdio->open(url, O_RDONLY, 0); if (sfd == NULL || Ferror(sfd)) { /* XXX Fstrerror */ rpmMessage(RPMMESS_DEBUG, _("failed to open %s\n"), url); @@ -354,7 +382,7 @@ int urlGetFile(const char * url, const char * dest) { dest = fileName; } - tfd = fdOpen(dest, O_CREAT|O_WRONLY|O_TRUNC, 0600); + tfd = fdio->open(dest, O_CREAT|O_WRONLY|O_TRUNC, 0600); if (Ferror(tfd)) { /* XXX Fstrerror */ rpmMessage(RPMMESS_DEBUG, _("failed to create %s\n"), dest); |