Use more portable file access mode tests (ticket #5)
authorPanu Matilainen <pmatilai@redhat.com>
Tue, 4 Nov 2008 07:52:19 +0000 (09:52 +0200)
committerPanu Matilainen <pmatilai@redhat.com>
Tue, 4 Nov 2008 07:52:19 +0000 (09:52 +0200)
- 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
lib/rpmchecksig.c
lib/rpmdb.c
rpmio/rpmio.c

index 12c13b4..55b8533 100644 (file)
@@ -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,
index a81f558..7e8131e 100644 (file)
@@ -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));
index bde76eb..78a97b2 100644 (file)
@@ -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);
index be9e99d..8a16c19 100644 (file)
@@ -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: