From dc06dca3b99f809f826f25565ace384b54f3b488 Mon Sep 17 00:00:00 2001 From: Panu Matilainen Date: Tue, 4 Nov 2008 09:52:19 +0200 Subject: [PATCH] Use more portable file access mode tests (ticket #5) - don't make assumptions about how O_RDWR and friends are defined, use the portable way of masking flags with O_ACCMODE and explicit comparison against the mode we're interested in --- lib/backend/db3.c | 10 +++++----- lib/rpmchecksig.c | 2 +- lib/rpmdb.c | 2 +- rpmio/rpmio.c | 8 ++++++-- 4 files changed, 13 insertions(+), 9 deletions(-) diff --git a/lib/backend/db3.c b/lib/backend/db3.c index 12c13b4..55b8533 100644 --- a/lib/backend/db3.c +++ b/lib/backend/db3.c @@ -824,7 +824,7 @@ static int db3open(rpmdb rpmdb, rpmTag rpmtag, dbiIndex * dbip) oflags &= ~DB_RDONLY; dbi->dbi_oflags &= ~DB_RDONLY; } else { - if (!(dbi->dbi_mode & (O_RDWR|O_WRONLY))) oflags |= DB_RDONLY; + if ((dbi->dbi_mode & O_ACCMODE) == O_RDONLY) oflags |= DB_RDONLY; if (dbi->dbi_mode & O_CREAT) { oflags |= DB_CREATE; dbi->dbi_oeflags |= DB_CREATE; @@ -1133,8 +1133,8 @@ static int db3open(rpmdb rpmdb, rpmTag rpmtag, dbiIndex * dbip) l.l_whence = 0; l.l_start = 0; l.l_len = 0; - l.l_type = (dbi->dbi_mode & (O_RDWR|O_WRONLY)) - ? F_WRLCK : F_RDLCK; + l.l_type = (dbi->dbi_mode & O_ACCMODE) == O_RDONLY + ? F_RDLCK : F_WRLCK; l.l_pid = 0; rc = fcntl(fdno, F_SETLK, (void *) &l); @@ -1146,8 +1146,8 @@ static int db3open(rpmdb rpmdb, rpmTag rpmtag, dbiIndex * dbip) ? 0 : 1); rpmlog( (rc ? RPMLOG_ERR : RPMLOG_WARNING), _("cannot get %s lock on %s/%s\n"), - ((dbi->dbi_mode & (O_RDWR|O_WRONLY)) - ? _("exclusive") : _("shared")), + ((dbi->dbi_mode & O_ACCMODE) == O_RDONLY) + ? _("shared") : _("exclusive"), dbhome, (dbfile ? dbfile : "")); } else if (dbfile) { rpmlog(RPMLOG_DEBUG, diff --git a/lib/rpmchecksig.c b/lib/rpmchecksig.c index a81f558..7e8131e 100644 --- a/lib/rpmchecksig.c +++ b/lib/rpmchecksig.c @@ -45,7 +45,7 @@ static int manageFile(FD_t *fdp, const char *fn, int flags) /* open a file and set *fdp */ if (*fdp == NULL && fn != NULL) { - fd = Fopen(fn, ((flags & O_WRONLY) ? "w.ufdio" : "r.ufdio")); + fd = Fopen(fn, (flags & O_ACCMODE) == O_WRONLY ? "w.ufdio" : "r.ufdio"); if (fd == NULL || Ferror(fd)) { rpmlog(RPMLOG_ERR, _("%s: open failed: %s\n"), fn, Fstrerror(fd)); diff --git a/lib/rpmdb.c b/lib/rpmdb.c index bde76eb..78a97b2 100644 --- a/lib/rpmdb.c +++ b/lib/rpmdb.c @@ -910,7 +910,7 @@ static int openDatabase(const char * prefix, if (dbp) *dbp = NULL; - if (mode & O_WRONLY) + if ((mode & O_ACCMODE) == O_WRONLY) return 1; db = newRpmdb(prefix, dbpath, mode, perms, flags); diff --git a/rpmio/rpmio.c b/rpmio/rpmio.c index be9e99d..8a16c19 100644 --- a/rpmio/rpmio.c +++ b/rpmio/rpmio.c @@ -738,8 +738,12 @@ fprintf(stderr, "*** ufdOpen(%s,0x%x,0%o)\n", url, (unsigned)flags, (unsigned)mo urlType = URL_IS_UNKNOWN; break; case URL_IS_DASH: - assert(!(flags & O_RDWR)); - fd = fdDup( ((flags & O_WRONLY) ? STDOUT_FILENO : STDIN_FILENO) ); + if ((flags & O_ACCMODE) == O_RDWR) { + fd = NULL; + } else { + fd = fdDup((flags & O_ACCMODE) == O_WRONLY ? + STDOUT_FILENO : STDIN_FILENO); + } timeout = 600; /* XXX W2DO? 10 mins? */ break; case URL_IS_PATH: -- 2.7.4