LCOV - code coverage report
Current view: top level - bin - xapian-progsrv.cc (source / functions) Hit Total Coverage
Test: Test Coverage for xapian-core 954b5873a738 Lines: 19 36 52.8 %
Date: 2019-06-30 05:20:33 Functions: 3 4 75.0 %
Branches: 18 51 35.3 %

           Branch data     Line data    Source code
       1                 :            : /** @file xapian-progsrv.cc
       2                 :            :  * @brief Remote server for use with ProgClient.
       3                 :            :  */
       4                 :            : /* Copyright (C) 2002,2003,2006,2007,2008,2010,2011 Olly Betts
       5                 :            :  *
       6                 :            :  * This program is free software; you can redistribute it and/or modify
       7                 :            :  * it under the terms of the GNU General Public License as published by
       8                 :            :  * the Free Software Foundation; either version 2 of the License, or
       9                 :            :  * (at your option) any later version.
      10                 :            :  *
      11                 :            :  * This program is distributed in the hope that it will be useful,
      12                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      13                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14                 :            :  * GNU General Public License for more details.
      15                 :            :  *
      16                 :            :  * You should have received a copy of the GNU General Public License
      17                 :            :  * along with this program; if not, write to the Free Software
      18                 :            :  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
      19                 :            :  */
      20                 :            : 
      21                 :            : #include <config.h>
      22                 :            : 
      23                 :            : #include "net/remoteserver.h"
      24                 :            : 
      25                 :            : #include "gnu_getopt.h"
      26                 :            : #include "parseint.h"
      27                 :            : 
      28                 :            : #include <cstdlib>
      29                 :            : #include <iostream>
      30                 :            : #include <string>
      31                 :            : 
      32                 :            : using namespace std;
      33                 :            : 
      34                 :            : #define PROG_NAME "xapian-progsrv"
      35                 :            : #define PROG_DESC "Piped server for use with Xapian's remote backend"
      36                 :            : 
      37                 :            : #define OPT_HELP 1
      38                 :            : #define OPT_VERSION 2
      39                 :            : 
      40                 :            : static const char * opts = "t:w";
      41                 :            : static const struct option long_opts[] = {
      42                 :            :     {"timeout",               required_argument,      0, 't'},
      43                 :            :     {"writable",      no_argument,            0, 'w'},
      44                 :            :     {"help",          no_argument,            0, OPT_HELP},
      45                 :            :     {"version",               no_argument,            0, OPT_VERSION},
      46                 :            :     {NULL, 0, 0, 0}
      47                 :            : };
      48                 :            : 
      49                 :          0 : static void show_usage() {
      50                 :            :     cout << "Usage: " PROG_NAME " [OPTIONS] DATABASE_DIRECTORY...\n\n"
      51                 :            : "Options:\n"
      52                 :            : "  --timeout MSECS         set timeout\n"
      53                 :            : "  --writable              allow updates (only one database directory allowed)\n"
      54                 :            : "  --help                  display this help and exit\n"
      55                 :          0 : "  --version               output version information and exit" << endl;
      56                 :          0 : }
      57                 :            : 
      58                 :        391 : int main(int argc, char **argv)
      59                 :            : {
      60                 :        391 :     double timeout = 60.0;
      61                 :        391 :     bool writable = false;
      62                 :        391 :     bool syntax_error = false;
      63                 :            : 
      64                 :            :     int c;
      65         [ +  + ]:        861 :     while ((c = gnu_getopt_long(argc, argv, opts, long_opts, NULL)) != -1) {
      66   [ -  -  +  +  :        470 :         switch (c) {
                      - ]
      67                 :            :             case OPT_HELP:
      68         [ #  # ]:          0 :                 cout << PROG_NAME " - " PROG_DESC "\n\n";
      69         [ #  # ]:          0 :                 show_usage();
      70                 :          0 :                 exit(0);
      71                 :            :             case OPT_VERSION:
      72 [ #  # ][ #  # ]:          0 :                 cout << PROG_NAME " - " PACKAGE_STRING << endl;
      73                 :          0 :                 exit(0);
      74                 :            :             case 't':
      75                 :            :                 unsigned int temp;
      76 [ +  - ][ -  + ]:        383 :                 if (!parse_unsigned(optarg, temp)) {
      77 [ #  # ][ #  # ]:          0 :                     cout << "timeout must be a non-negative integer" << endl;
      78         [ #  # ]:          0 :                     show_usage();
      79                 :          0 :                     exit(1);
      80                 :            :                 }
      81                 :        383 :                 timeout = temp * 1e-3;
      82                 :        470 :                 break;
      83                 :            :             case 'w':
      84                 :         87 :                 writable = true;
      85                 :         87 :                 break;
      86                 :            :             default:
      87                 :          0 :                 syntax_error = true;
      88                 :            :         }
      89                 :            :     }
      90                 :            : 
      91 [ +  - ][ -  + ]:        391 :     if (syntax_error || optind == argc) {
      92         [ #  # ]:          0 :         show_usage();
      93                 :          0 :         exit(1);
      94                 :            :     }
      95                 :            : 
      96 [ +  + ][ -  + ]:        391 :     if (writable && (argc - optind) != 1) {
      97         [ #  # ]:          0 :         cerr << "Error: only one database directory allowed with '--writable'."
      98         [ #  # ]:          0 :              << endl;
      99                 :          0 :         exit(1);
     100                 :            :     }
     101                 :            : 
     102                 :            :     /* Unlike xapian-tcpsrv, xapian-progsrv only has a single 'connection'
     103                 :            :      * which is established immediately.  So, there is no point in attempting
     104                 :            :      * to open the database(s) to check they work - if they can't be opened the
     105                 :            :      * client will get an exception right away anyway.
     106                 :            :      */
     107         [ +  - ]:        391 :     vector<string> dbnames(argv + optind, argv + argc);
     108                 :            : 
     109                 :            :     try {
     110                 :            :         // We communicate with the client via stdin (fd 0) and stdout (fd 1).
     111                 :            :         // Note that RemoteServer closes these fds.
     112         [ +  - ]:        391 :         RemoteServer server(dbnames, 0, 1, timeout, timeout, writable);
     113                 :            : 
     114                 :            :         // If you have defined your own weighting scheme, register it here
     115                 :            :         // like so:
     116                 :            :         // server.register_weighting_scheme(FooWeight());
     117                 :            : 
     118         [ +  + ]:        391 :         server.run();
     119         [ +  - ]:          1 :     } catch (...) {
     120                 :            :         /* Catch and ignore any exceptions thrown by RemoteServer, since the
     121                 :            :          * RemoteServer will have passed the error to the client to be rethrown
     122                 :            :          * there.
     123                 :            :          *
     124                 :            :          * Our stdout is the (now closed) communication channel to the client,
     125                 :            :          * and our stderr is probably a closed fd so we don't have anywhere to
     126                 :            :          * send error messages to anyway!
     127                 :            :          */
     128                 :        391 :     }
     129 [ +  - ][ +  - ]:       1955 : }

Generated by: LCOV version 1.11