LCOV - code coverage report
Current view: top level - backends - databasehelpers.cc (source / functions) Hit Total Coverage
Test: Test Coverage for xapian-core 954b5873a738 Lines: 18 22 81.8 %
Date: 2019-06-30 05:20:33 Functions: 1 1 100.0 %
Branches: 17 29 58.6 %

           Branch data     Line data    Source code
       1                 :            : /** @file databasehelpers.cc
       2                 :            :  * @brief Helper functions for database handling
       3                 :            :  */
       4                 :            : /* Copyright 2002-2019 Olly Betts
       5                 :            :  * Copyright 2008 Lemur Consulting Ltd
       6                 :            :  *
       7                 :            :  * This program is free software; you can redistribute it and/or
       8                 :            :  * modify it under the terms of the GNU General Public License as
       9                 :            :  * published by the Free Software Foundation; either version 2 of the
      10                 :            :  * License, or (at your option) any later version.
      11                 :            :  *
      12                 :            :  * This program is distributed in the hope that it will be useful,
      13                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      14                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      15                 :            :  * GNU General Public License for more details.
      16                 :            :  *
      17                 :            :  * You should have received a copy of the GNU General Public License
      18                 :            :  * along with this program; if not, write to the Free Software
      19                 :            :  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
      20                 :            :  * USA
      21                 :            :  */
      22                 :            : 
      23                 :            : #include <config.h>
      24                 :            : 
      25                 :            : #include "databasehelpers.h"
      26                 :            : 
      27                 :            : #include "backends.h"
      28                 :            : 
      29                 :            : #include "glass/glass_defs.h"
      30                 :            : #include "honey/honey_defs.h"
      31                 :            : 
      32                 :            : #include "io_utils.h"
      33                 :            : #include "omassert.h"
      34                 :            : #include "posixy_wrapper.h"
      35                 :            : 
      36                 :            : #include <algorithm>
      37                 :            : #include <cstring>
      38                 :            : #include <string>
      39                 :            : #include <sys/types.h>
      40                 :            : #include "safesysstat.h"
      41                 :            : #include "safeunistd.h"
      42                 :            : 
      43                 :            : using namespace std;
      44                 :            : 
      45                 :            : int
      46                 :        667 : test_if_single_file_db(const struct stat& sb,
      47                 :            :                        const string& path,
      48                 :            :                        int* fd_ptr)
      49                 :            : {
      50                 :            :     Assert(fd_ptr);
      51                 :            : #if defined XAPIAN_HAS_GLASS_BACKEND || \
      52                 :            :     defined XAPIAN_HAS_HONEY_BACKEND
      53         [ -  + ]:        667 :     if (!S_ISREG(sb.st_mode)) return BACKEND_UNKNOWN;
      54                 :            :     // Look at the size as a clue - if it's less than both GLASS_MIN_BLOCKSIZE
      55                 :            :     // and HONEY_MIN_DB_SIZE then it's not a single-file glass or honey
      56                 :            :     // database.  For a larger file, we peek at the start of the file to
      57                 :            :     // determine what it is.
      58 [ +  - ][ +  + ]:        667 :     if (sb.st_size < min(GLASS_MIN_BLOCKSIZE, HONEY_MIN_DB_SIZE))
      59                 :        356 :         return BACKEND_UNKNOWN;
      60                 :        311 :     int fd = posixy_open(path.c_str(), O_RDONLY|O_BINARY);
      61         [ +  - ]:        311 :     if (fd != -1) {
      62                 :            :         char magic_buf[14];
      63                 :            :         // FIXME: Don't duplicate magic check here...
      64   [ +  -  +  - ]:        933 :         if (io_read(fd, magic_buf, 14) == 14 &&
                 [ +  + ]
      65 [ +  - ][ +  + ]:        622 :             lseek(fd, 0, SEEK_SET) == 0 &&
      66                 :        311 :             memcmp(magic_buf, "\x0f\x0dXapian ", 9) == 0) {
      67      [ +  +  - ]:        310 :             switch (magic_buf[9]) {
      68                 :            :                 case 'G':
      69         [ +  - ]:        309 :                     if (memcmp(magic_buf + 10, "lass", 4) == 0) {
      70                 :        309 :                         *fd_ptr = fd;
      71                 :        310 :                         return BACKEND_GLASS;
      72                 :            :                     }
      73                 :          0 :                     break;
      74                 :            :                 case 'H':
      75         [ +  - ]:          1 :                     if (memcmp(magic_buf + 10, "oney", 4) == 0) {
      76                 :          1 :                         *fd_ptr = fd;
      77                 :          1 :                         return BACKEND_HONEY;
      78                 :            :                     }
      79                 :          0 :                     break;
      80                 :            :             }
      81         [ #  # ]:          0 :             ::close(fd);
      82                 :          0 :             return BACKEND_UNKNOWN;
      83                 :            :         }
      84         [ +  - ]:          1 :         ::close(fd);
      85                 :            :     }
      86                 :            : #else
      87                 :            :     (void)sb;
      88                 :            :     (void)path;
      89                 :            :     (void)fd_ptr;
      90                 :            : #endif
      91                 :        667 :     return BACKEND_UNKNOWN;
      92                 :            : }

Generated by: LCOV version 1.11