LCOV - differential code coverage report
Current view: top level - src/bin/scripts - dropuser.c (source / functions) Coverage Total Hit UNC UBC GNC CBC DUB DCB
Current: Differential Code Coverage HEAD vs 15 Lines: 61.6 % 99 61 6 32 1 60 6 1
Current Date: 2023-04-08 17:13:01 Functions: 100.0 % 2 2 1 1
Baseline: 15 Line coverage date bins:
Baseline Date: 2023-04-08 15:09:40 (60,120] days: 14.3 % 7 1 6 1
Legend: Lines: hit not hit (240..) days: 65.2 % 92 60 32 60
Function coverage date bins:
(240..) days: 100.0 % 2 2 1 1

 Age         Owner                  TLA  Line data    Source code
                                  1                 : /*-------------------------------------------------------------------------
                                  2                 :  *
                                  3                 :  * dropuser
                                  4                 :  *
                                  5                 :  * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group
                                  6                 :  * Portions Copyright (c) 1994, Regents of the University of California
                                  7                 :  *
                                  8                 :  * src/bin/scripts/dropuser.c
                                  9                 :  *
                                 10                 :  *-------------------------------------------------------------------------
                                 11                 :  */
                                 12                 : 
                                 13                 : #include "postgres_fe.h"
                                 14                 : #include "common.h"
                                 15                 : #include "common/logging.h"
                                 16                 : #include "common/string.h"
                                 17                 : #include "fe_utils/option_utils.h"
                                 18                 : #include "fe_utils/string_utils.h"
                                 19                 : 
                                 20                 : 
                                 21                 : static void help(const char *progname);
                                 22                 : 
                                 23                 : 
                                 24                 : int
 7327 peter_e                    25 CBC           5 : main(int argc, char *argv[])
                                 26                 : {
                                 27                 :     static int  if_exists = 0;
                                 28                 : 
                                 29                 :     static struct option long_options[] = {
                                 30                 :         {"host", required_argument, NULL, 'h'},
                                 31                 :         {"port", required_argument, NULL, 'p'},
                                 32                 :         {"username", required_argument, NULL, 'U'},
                                 33                 :         {"no-password", no_argument, NULL, 'w'},
                                 34                 :         {"password", no_argument, NULL, 'W'},
                                 35                 :         {"echo", no_argument, NULL, 'e'},
                                 36                 :         {"interactive", no_argument, NULL, 'i'},
                                 37                 :         {"if-exists", no_argument, &if_exists, 1},
                                 38                 :         {NULL, 0, NULL, 0}
                                 39                 :     };
                                 40                 : 
                                 41                 :     const char *progname;
                                 42                 :     int         optindex;
                                 43                 :     int         c;
                                 44                 : 
                                 45               5 :     char       *dropuser = NULL;
                                 46               5 :     char       *host = NULL;
                                 47               5 :     char       *port = NULL;
                                 48               5 :     char       *username = NULL;
 5155                            49               5 :     enum trivalue prompt_password = TRI_DEFAULT;
                                 50                 :     ConnParams  cparams;
 7327                            51               5 :     bool        echo = false;
                                 52               5 :     bool        interactive = false;
                                 53                 : 
                                 54                 :     PQExpBufferData sql;
                                 55                 : 
                                 56                 :     PGconn     *conn;
                                 57                 :     PGresult   *result;
                                 58                 : 
 1469 peter                      59               5 :     pg_logging_init(argv[0]);
 7327 peter_e                    60               5 :     progname = get_progname(argv[0]);
 5232                            61               5 :     set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pgscripts"));
                                 62                 : 
 7327                            63               5 :     handle_help_version_opts(argc, argv, "dropuser", help);
                                 64                 : 
  118 peter                      65 GNC           3 :     while ((c = getopt_long(argc, argv, "eh:ip:U:wW", long_options, &optindex)) != -1)
                                 66                 :     {
 7327 peter_e                    67 CBC           1 :         switch (c)
                                 68                 :         {
  118 peter                      69 UNC           0 :             case 'e':
                                 70               0 :                 echo = true;
                                 71               0 :                 break;
 7327 peter_e                    72 UBC           0 :             case 'h':
 3831 bruce                      73               0 :                 host = pg_strdup(optarg);
 7327 peter_e                    74               0 :                 break;
  118 peter                      75 UNC           0 :             case 'i':
                                 76               0 :                 interactive = true;
                                 77               0 :                 break;
 7327 peter_e                    78 UBC           0 :             case 'p':
 3831 bruce                      79               0 :                 port = pg_strdup(optarg);
 7327 peter_e                    80               0 :                 break;
                                 81               0 :             case 'U':
 3831 bruce                      82               0 :                 username = pg_strdup(optarg);
 7327 peter_e                    83               0 :                 break;
 5155                            84               0 :             case 'w':
                                 85               0 :                 prompt_password = TRI_NO;
                                 86               0 :                 break;
 7327                            87               0 :             case 'W':
 5155                            88               0 :                 prompt_password = TRI_YES;
 7327                            89               0 :                 break;
 4240 rhaas                      90               0 :             case 0:
                                 91                 :                 /* this covers the long options */
                                 92               0 :                 break;
 7327 peter_e                    93 CBC           1 :             default:
                                 94                 :                 /* getopt_long already emitted a complaint */
  366 tgl                        95               1 :                 pg_log_error_hint("Try \"%s --help\" for more information.", progname);
 7327 peter_e                    96               1 :                 exit(1);
                                 97                 :         }
                                 98                 :     }
                                 99                 : 
                                100               2 :     switch (argc - optind)
                                101                 :     {
 7327 peter_e                   102 UBC           0 :         case 0:
                                103               0 :             break;
 7327 peter_e                   104 CBC           2 :         case 1:
                                105               2 :             dropuser = argv[optind];
                                106               2 :             break;
 7327 peter_e                   107 UBC           0 :         default:
 1469 peter                     108               0 :             pg_log_error("too many command-line arguments (first is \"%s\")",
                                109                 :                          argv[optind + 1]);
  366 tgl                       110               0 :             pg_log_error_hint("Try \"%s --help\" for more information.", progname);
 7327 peter_e                   111               0 :             exit(1);
                                112                 :     }
                                113                 : 
 7327 peter_e                   114 CBC           2 :     if (dropuser == NULL)
                                115                 :     {
 4079 peter_e                   116 UBC           0 :         if (interactive)
                                117                 :         {
  948 tgl                       118               0 :             dropuser = simple_prompt("Enter name of role to drop: ", true);
                                119                 :         }
                                120                 :         else
                                121                 :         {
 1469 peter                     122               0 :             pg_log_error("missing required argument role name");
  366 tgl                       123               0 :             pg_log_error_hint("Try \"%s --help\" for more information.", progname);
 4079 peter_e                   124               0 :             exit(1);
                                125                 :         }
                                126                 :     }
                                127                 : 
 7327 peter_e                   128 CBC           2 :     if (interactive)
                                129                 :     {
 6446 tgl                       130 UBC           0 :         printf(_("Role \"%s\" will be permanently removed.\n"), dropuser);
 6043 peter_e                   131               0 :         if (!yesno_prompt("Are you sure?"))
 7327                           132               0 :             exit(0);
                                133                 :     }
                                134                 : 
  902 tgl                       135 CBC           2 :     cparams.dbname = NULL;      /* this program lacks any dbname option... */
                                136               2 :     cparams.pghost = host;
                                137               2 :     cparams.pgport = port;
                                138               2 :     cparams.pguser = username;
                                139               2 :     cparams.prompt_password = prompt_password;
                                140               2 :     cparams.override_dbname = NULL;
                                141                 : 
                                142               2 :     conn = connectMaintenanceDatabase(&cparams, progname, echo);
                                143                 : 
 7327 peter_e                   144               2 :     initPQExpBuffer(&sql);
 3345                           145               4 :     appendPQExpBuffer(&sql, "DROP ROLE %s%s;",
 4240 rhaas                     146               2 :                       (if_exists ? "IF EXISTS " : ""), fmtId(dropuser));
                                147                 : 
 7327 peter_e                   148               2 :     if (echo)
 3345 peter_e                   149 UBC           0 :         printf("%s\n", sql.data);
 7327 peter_e                   150 CBC           2 :     result = PQexec(conn, sql.data);
                                151                 : 
                                152               2 :     if (PQresultStatus(result) != PGRES_COMMAND_OK)
                                153                 :     {
 1469 peter                     154               1 :         pg_log_error("removal of role \"%s\" failed: %s",
                                155                 :                      dropuser, PQerrorMessage(conn));
 7327 peter_e                   156               1 :         PQfinish(conn);
                                157               1 :         exit(1);
                                158                 :     }
                                159                 : 
 6159 bruce                     160               1 :     PQclear(result);
 7327 peter_e                   161               1 :     PQfinish(conn);
                                162               1 :     exit(0);
                                163                 : }
                                164                 : 
                                165                 : 
                                166                 : static void
                                167               1 : help(const char *progname)
                                168                 : {
 6400                           169               1 :     printf(_("%s removes a PostgreSQL role.\n\n"), progname);
 7327                           170               1 :     printf(_("Usage:\n"));
 6400                           171               1 :     printf(_("  %s [OPTION]... [ROLENAME]\n"), progname);
 7327                           172               1 :     printf(_("\nOptions:\n"));
 7242 bruce                     173               1 :     printf(_("  -e, --echo                show the commands being sent to the server\n"));
 4079 peter_e                   174               1 :     printf(_("  -i, --interactive         prompt before deleting anything, and prompt for\n"
                                175                 :              "                            role name if not specified\n"));
 3947                           176               1 :     printf(_("  -V, --version             output version information, then exit\n"));
 4240 rhaas                     177               1 :     printf(_("  --if-exists               don't report error if user doesn't exist\n"));
 3947 peter_e                   178               1 :     printf(_("  -?, --help                show this help, then exit\n"));
 5156                           179               1 :     printf(_("\nConnection options:\n"));
 7242 bruce                     180               1 :     printf(_("  -h, --host=HOSTNAME       database server host or socket directory\n"));
 7327 peter_e                   181               1 :     printf(_("  -p, --port=PORT           database server port\n"));
                                182               1 :     printf(_("  -U, --username=USERNAME   user name to connect as (not the one to drop)\n"));
 5155                           183               1 :     printf(_("  -w, --no-password         never prompt for password\n"));
 5598 tgl                       184               1 :     printf(_("  -W, --password            force password prompt\n"));
 1136 peter                     185               1 :     printf(_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
                                186               1 :     printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
 7327 peter_e                   187               1 : }
        

Generated by: LCOV version v1.16-55-g56c0a2a