LCOV - code coverage report
Current view: top level - bin - xapian-progsrv.cc (source / functions) Hit Total Coverage
Test: Test Coverage for xapian-core c2b6f1024d3a Lines: 18 32 56.2 %
Date: 2019-05-16 09:13:18 Functions: 3 4 75.0 %
Branches: 16 41 39.0 %

           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                 :            : 
      27                 :            : #include <cstdlib>
      28                 :            : #include <iostream>
      29                 :            : #include <string>
      30                 :            : 
      31                 :            : using namespace std;
      32                 :            : 
      33                 :            : #define PROG_NAME "xapian-progsrv"
      34                 :            : #define PROG_DESC "Piped server for use with Xapian's remote backend"
      35                 :            : 
      36                 :            : #define OPT_HELP 1
      37                 :            : #define OPT_VERSION 2
      38                 :            : 
      39                 :            : static const char * opts = "t:w";
      40                 :            : static const struct option long_opts[] = {
      41                 :            :     {"timeout",               required_argument,      0, 't'},
      42                 :            :     {"writable",      no_argument,            0, 'w'},
      43                 :            :     {"help",          no_argument,            0, OPT_HELP},
      44                 :            :     {"version",               no_argument,            0, OPT_VERSION},
      45                 :            :     {NULL, 0, 0, 0}
      46                 :            : };
      47                 :            : 
      48                 :          0 : static void show_usage() {
      49                 :            :     cout << "Usage: " PROG_NAME " [OPTIONS] DATABASE_DIRECTORY...\n\n"
      50                 :            : "Options:\n"
      51                 :            : "  --timeout MSECS         set timeout\n"
      52                 :            : "  --writable              allow updates (only one database directory allowed)\n"
      53                 :            : "  --help                  display this help and exit\n"
      54                 :          0 : "  --version               output version information and exit" << endl;
      55                 :          0 : }
      56                 :            : 
      57                 :        363 : int main(int argc, char **argv)
      58                 :            : {
      59                 :        363 :     double timeout = 60.0;
      60                 :        363 :     bool writable = false;
      61                 :        363 :     bool syntax_error = false;
      62                 :            : 
      63                 :            :     int c;
      64         [ +  + ]:        805 :     while ((c = gnu_getopt_long(argc, argv, opts, long_opts, NULL)) != -1) {
      65   [ -  -  +  +  :        442 :         switch (c) {
                      - ]
      66                 :            :             case OPT_HELP:
      67         [ #  # ]:          0 :                 cout << PROG_NAME " - " PROG_DESC "\n\n";
      68         [ #  # ]:          0 :                 show_usage();
      69                 :          0 :                 exit(0);
      70                 :            :             case OPT_VERSION:
      71 [ #  # ][ #  # ]:          0 :                 cout << PROG_NAME " - " PACKAGE_STRING << endl;
      72                 :          0 :                 exit(0);
      73                 :            :             case 't':
      74                 :        355 :                 timeout = atoi(optarg) * 1e-3;
      75                 :        355 :                 break;
      76                 :            :             case 'w':
      77                 :         87 :                 writable = true;
      78                 :         87 :                 break;
      79                 :            :             default:
      80                 :          0 :                 syntax_error = true;
      81                 :            :         }
      82                 :            :     }
      83                 :            : 
      84 [ +  - ][ -  + ]:        363 :     if (syntax_error || optind == argc) {
      85         [ #  # ]:          0 :         show_usage();
      86                 :          0 :         exit(1);
      87                 :            :     }
      88                 :            : 
      89 [ +  + ][ -  + ]:        363 :     if (writable && (argc - optind) != 1) {
      90         [ #  # ]:          0 :         cerr << "Error: only one database directory allowed with '--writable'."
      91         [ #  # ]:          0 :              << endl;
      92                 :          0 :         exit(1);
      93                 :            :     }
      94                 :            : 
      95                 :            :     /* Unlike xapian-tcpsrv, xapian-progsrv only has a single 'connection'
      96                 :            :      * which is established immediately.  So, there is no point in attempting
      97                 :            :      * to open the database(s) to check they work - if they can't be opened the
      98                 :            :      * client will get an exception right away anyway.
      99                 :            :      */
     100         [ +  - ]:        363 :     vector<string> dbnames(argv + optind, argv + argc);
     101                 :            : 
     102                 :            :     try {
     103                 :            :         // We communicate with the client via stdin (fd 0) and stdout (fd 1).
     104                 :            :         // Note that RemoteServer closes these fds.
     105         [ +  - ]:        363 :         RemoteServer server(dbnames, 0, 1, timeout, timeout, writable);
     106                 :            : 
     107                 :            :         // If you have defined your own weighting scheme, register it here
     108                 :            :         // like so:
     109                 :            :         // server.register_weighting_scheme(FooWeight());
     110                 :            : 
     111         [ +  + ]:        363 :         server.run();
     112         [ +  - ]:          1 :     } catch (...) {
     113                 :            :         /* Catch and ignore any exceptions thrown by RemoteServer, since the
     114                 :            :          * RemoteServer will have passed the error to the client to be rethrown
     115                 :            :          * there.
     116                 :            :          *
     117                 :            :          * Our stdout is the (now closed) communication channel to the client,
     118                 :            :          * and our stderr is probably a closed fd so we don't have anywhere to
     119                 :            :          * send error messages to anyway!
     120                 :            :          */
     121                 :        363 :     }
     122 [ +  - ][ +  - ]:       1815 : }

Generated by: LCOV version 1.11