Bug 1333007 - Coverity 1399445: Remove DBS map file code r=franziskus
authorTim Taubert <ttaubert@mozilla.com>
Mon, 23 Jan 2017 10:36:59 +0100
changeset 13049 3b61cb1e5ea878b9b689bcbfb19dfba3c809f318
parent 13048 ee21c9892907a8119959f77cf0f025578a00b210
child 13050 6ed5cded5b9bf993865252a648b4bf48226d1921
push id1949
push userttaubert@mozilla.com
push dateMon, 23 Jan 2017 09:37:46 +0000
reviewersfranziskus
bugs1333007, 1399445
Bug 1333007 - Coverity 1399445: Remove DBS map file code r=franziskus Differential Revision: https://nss-review.dev.mozaws.net/D167
lib/softoken/legacydb/dbmshim.c
--- a/lib/softoken/legacydb/dbmshim.c
+++ b/lib/softoken/legacydb/dbmshim.c
@@ -42,19 +42,16 @@
 /* a Shim data structure. This data structure has a db built into it. */
 typedef struct DBSStr DBS;
 
 struct DBSStr {
     DB db;
     char *blobdir;
     int mode;
     PRBool readOnly;
-    PRFileMap *dbs_mapfile;
-    unsigned char *dbs_addr;
-    PRUint32 dbs_len;
     char staticBlobArea[BLOB_BUF_LEN];
 };
 
 /*
  * return true if the Datablock contains a blobtype
  */
 static PRBool
 dbs_IsBlob(DBT *blobData)
@@ -239,53 +236,16 @@ loser:
         PR_smprintf_free(file);
     }
     /* don't let close or delete reset the error */
     PR_SetError(error, 0);
     return -1;
 }
 
 /*
- * we need to keep a address map in memory between calls to DBM.
- * remember what we have mapped can close it when we get another dbm
- * call.
- *
- * NOTE: Not all platforms support mapped files. This code is designed to
- * detect this at runtime. If map files aren't supported the OS will indicate
- * this by failing the PR_Memmap call. In this case we emulate mapped files
- * by just reading in the file into regular memory. We signal this state by
- * making dbs_mapfile NULL and dbs_addr non-NULL.
- */
-
-static void
-dbs_freemap(DBS *dbsp)
-{
-    if (dbsp->dbs_mapfile) {
-        PR_MemUnmap(dbsp->dbs_addr, dbsp->dbs_len);
-        PR_CloseFileMap(dbsp->dbs_mapfile);
-        dbsp->dbs_mapfile = NULL;
-        dbsp->dbs_addr = NULL;
-        dbsp->dbs_len = 0;
-    } else if (dbsp->dbs_addr) {
-        PORT_Free(dbsp->dbs_addr);
-        dbsp->dbs_addr = NULL;
-        dbsp->dbs_len = 0;
-    }
-    return;
-}
-
-static void
-dbs_setmap(DBS *dbsp, PRFileMap *mapfile, unsigned char *addr, PRUint32 len)
-{
-    dbsp->dbs_mapfile = mapfile;
-    dbsp->dbs_addr = addr;
-    dbsp->dbs_len = len;
-}
-
-/*
  * platforms that cannot map the file need to read it into a temp buffer.
  */
 static unsigned char *
 dbs_EmulateMap(PRFileDesc *filed, int len)
 {
     unsigned char *addr;
     PRInt32 dataRead;
 
@@ -312,17 +272,16 @@ dbs_EmulateMap(PRFileDesc *filed, int le
  * data points to the blob record on input and the real record (if we could
  * read it) on output. if there is an error data is not modified.
  */
 static int
 dbs_readBlob(DBS *dbsp, DBT *data)
 {
     char *file = NULL;
     PRFileDesc *filed = NULL;
-    PRFileMap *mapfile = NULL;
     unsigned char *addr = NULL;
     int error;
     int len = -1;
 
     file = dbs_getBlobFilePath(dbsp->blobdir, data);
     if (!file) {
         goto loser;
     }
@@ -339,28 +298,24 @@ dbs_readBlob(DBS *dbsp, DBT *data)
      * https://msdn.microsoft.com/en-us/library/windows/desktop/aa366761(v=vs.85).aspx
      * Let's always use the emulated map, i.e. read the file.
      */
     addr = dbs_EmulateMap(filed, len);
     if (addr == NULL) {
         goto loser;
     }
     PR_Close(filed);
-    dbs_setmap(dbsp, mapfile, addr, len);
 
     data->data = addr;
     data->size = len;
     return 0;
 
 loser:
     /* preserve the error code */
     error = PR_GetError();
-    if (mapfile) {
-        PR_CloseFileMap(mapfile);
-    }
     if (filed) {
         PR_Close(filed);
     }
     PR_SetError(error, 0);
     return -1;
 }
 
 /*
@@ -368,36 +323,32 @@ loser:
  */
 static int
 dbs_get(const DB *dbs, const DBT *key, DBT *data, unsigned int flags)
 {
     int ret;
     DBS *dbsp = (DBS *)dbs;
     DB *db = (DB *)dbs->internal;
 
-    dbs_freemap(dbsp);
-
     ret = (*db->get)(db, key, data, flags);
     if ((ret == 0) && dbs_IsBlob(data)) {
         ret = dbs_readBlob(dbsp, data);
     }
 
     return (ret);
 }
 
 static int
 dbs_put(const DB *dbs, DBT *key, const DBT *data, unsigned int flags)
 {
     DBT blob;
     int ret = 0;
     DBS *dbsp = (DBS *)dbs;
     DB *db = (DB *)dbs->internal;
 
-    dbs_freemap(dbsp);
-
     /* If the db is readonly, just pass the data down to rdb and let it fail */
     if (!dbsp->readOnly) {
         DBT oldData;
         int ret1;
 
         /* make sure the current record is deleted if it's a blob */
         ret1 = (*db->get)(db, key, &oldData, 0);
         if ((ret1 == 0) && flags == R_NOOVERWRITE) {
@@ -422,30 +373,26 @@ dbs_put(const DB *dbs, DBT *key, const D
 }
 
 static int
 dbs_sync(const DB *dbs, unsigned int flags)
 {
     DB *db = (DB *)dbs->internal;
     DBS *dbsp = (DBS *)dbs;
 
-    dbs_freemap(dbsp);
-
     return (*db->sync)(db, flags);
 }
 
 static int
 dbs_del(const DB *dbs, const DBT *key, unsigned int flags)
 {
     int ret;
     DBS *dbsp = (DBS *)dbs;
     DB *db = (DB *)dbs->internal;
 
-    dbs_freemap(dbsp);
-
     if (!dbsp->readOnly) {
         DBT oldData;
         ret = (*db->get)(db, key, &oldData, 0);
         if ((ret == 0) && dbs_IsBlob(&oldData)) {
             dbs_removeBlob(dbsp, &oldData);
         }
     }
 
@@ -454,35 +401,32 @@ dbs_del(const DB *dbs, const DBT *key, u
 
 static int
 dbs_seq(const DB *dbs, DBT *key, DBT *data, unsigned int flags)
 {
     int ret;
     DBS *dbsp = (DBS *)dbs;
     DB *db = (DB *)dbs->internal;
 
-    dbs_freemap(dbsp);
-
     ret = (*db->seq)(db, key, data, flags);
     if ((ret == 0) && dbs_IsBlob(data)) {
         /* don't return a blob read as an error so traversals keep going */
         (void)dbs_readBlob(dbsp, data);
     }
 
     return (ret);
 }
 
 static int
 dbs_close(DB *dbs)
 {
     DBS *dbsp = (DBS *)dbs;
     DB *db = (DB *)dbs->internal;
     int ret;
 
-    dbs_freemap(dbsp);
     ret = (*db->close)(db);
     PORT_Free(dbsp->blobdir);
     PORT_Free(dbsp);
     return ret;
 }
 
 static int
 dbs_fd(const DB *dbs)
@@ -563,19 +507,16 @@ dbsopen(const char *dbname, int flags, i
     dbs = &dbsp->db;
 
     dbsp->blobdir = dbs_mkBlobDirName(dbname);
     if (dbsp->blobdir == NULL) {
         goto loser;
     }
     dbsp->mode = mode;
     dbsp->readOnly = (PRBool)(flags == NO_RDONLY);
-    dbsp->dbs_mapfile = NULL;
-    dbsp->dbs_addr = NULL;
-    dbsp->dbs_len = 0;
 
     /* the real dbm call */
     db = dbopen(dbname, flags, mode, type, &dbs_hashInfo);
     if (db == NULL) {
         goto loser;
     }
     dbs->internal = (void *)db;
     dbs->type = type;