clitar: get tar context handle via helper function
authorDavid Disseldorp <ddiss@samba.org>
Fri, 14 Feb 2014 16:16:14 +0000 (17:16 +0100)
committerAndreas Schneider <asn@samba.org>
Wed, 19 Feb 2014 17:22:29 +0000 (18:22 +0100)
Add and use tar_get_ctx() to get the tarmode context handle in client.c,
rather than declaring an extern.
Also, add checks for NULL context pointer arguments.

Signed-off-by: David Disseldorp <ddiss@samba.org>
Reviewed-by: Andreas Schneider <asn@samba.org>
source3/client/client.c
source3/client/clitar.c
source3/client/clitar_proto.h

index 0cd59234d6ce31a1d921a56378ea834f608da74a..5157fc9797b3015f48b012b3fdc8058ed505511f 100644 (file)
@@ -5318,7 +5318,7 @@ static int do_host_query(const char *query_host)
 
 static int do_tar_op(const char *base_directory)
 {
-       extern struct tar tar_ctx;
+       struct tar *tar_ctx = tar_get_ctx();
        int ret = 0;
 
        /* do we already have a connection? */
@@ -5345,7 +5345,7 @@ static int do_tar_op(const char *base_directory)
                }
        }
 
-       ret = tar_process(&tar_ctx);
+       ret = tar_process(tar_ctx);
 
  out_cli:
        cli_shutdown(cli);
@@ -5393,7 +5393,7 @@ int main(int argc,char *argv[])
        int rc = 0;
        bool tar_opt = false;
        bool service_opt = false;
-    extern struct tar tar_ctx;
+       struct tar *tar_ctx = tar_get_ctx();
 
        struct poptOption long_options[] = {
                POPT_AUTOHELP
@@ -5517,7 +5517,7 @@ int main(int argc,char *argv[])
                                                break;
                                }
                                i++;
-                               if (tar_parse_args(&tar_ctx, poptGetOptArg(pc),
+                               if (tar_parse_args(tar_ctx, poptGetOptArg(pc),
                                                   const_argv + i, argc - i)) {
                                        poptPrintUsage(pc, stderr, 0);
                                        exit(1);
@@ -5611,7 +5611,7 @@ int main(int argc,char *argv[])
        if(new_name_resolve_order)
                lp_set_cmdline("name resolve order", new_name_resolve_order);
 
-       if (!tar_to_process(&tar_ctx) && !query_host && !service && !message) {
+       if (!tar_to_process(tar_ctx) && !query_host && !service && !message) {
                poptPrintUsage(pc, stderr, 0);
                exit(1);
        }
@@ -5626,7 +5626,7 @@ int main(int argc,char *argv[])
 
        max_protocol = lp_client_max_protocol();
 
-       if (tar_to_process(&tar_ctx)) {
+       if (tar_to_process(tar_ctx)) {
                if (cmdstr)
                        process_command_string(cmdstr);
                rc = do_tar_op(base_directory);
index e0c9c94d3354e6fd33e234a6964b974f40ccbf45..37c31ac3d9e8a2cb7d0abc95b3a17c4a2dd24cc2 100644 (file)
@@ -24,8 +24,8 @@
  * the context of the backup process.
  *
  * The current tar context can be accessed via the global variable
- * `tar_ctx`. It's not static but you should avoid accessing it
- * directly.
+ * `tar_ctx`. It's publicly exported as an opaque handle via
+ * tar_get_ctx().
  *
  * A tar context is first configured through tar_parse_args() which
  * can be called from either the CLI (in client.c) or the interactive
@@ -232,6 +232,13 @@ static int max_token (const char *str);
 static bool is_subpath(const char *sub, const char *full);
 static int set_remote_attr(const char *filename, uint16 new_attr, int mode);
 
+/**
+ * tar_get_ctx - retrieve global tar context handle
+ */
+struct tar *tar_get_ctx()
+{
+    return &tar_ctx;
+}
 
 /**
  * cmd_block - interactive command to change tar blocksize
@@ -475,12 +482,17 @@ int cmd_setmode(void)
 int tar_parse_args(struct tar* t, const char *flag,
                    const char **val, int valsize)
 {
-    TALLOC_CTX *ctx = tar_reset_mem_context(t);
+    TALLOC_CTX *ctx;
     bool list = false;
-
     /* index of next value to use */
     int ival = 0;
 
+    if (t == NULL) {
+        DBG(0, ("Invalid tar context\n"));
+        return 1;
+    }
+
+    ctx = tar_reset_mem_context(t);
     /*
      * Reset back some options - could be from interactive version
      * all other modes are left as they are
@@ -659,6 +671,11 @@ int tar_process(struct tar *t)
 {
     int rc = 0;
 
+    if (t == NULL) {
+        DBG(0, ("Invalid tar context\n"));
+        return 1;
+    }
+
     switch(t->mode.operation) {
     case TAR_EXTRACT:
         rc = tar_extract(t);
@@ -1353,6 +1370,10 @@ static bool tar_create_skip_path(struct tar *t,
  */
 bool tar_to_process (struct tar *t)
 {
+    if (t == NULL) {
+        DBG(0, ("Invalid tar context\n"));
+        return false;
+    }
     return t->to_process;
 }
 
@@ -1683,13 +1704,6 @@ static char *path_base_name (const char *path)
 
 #define NOT_IMPLEMENTED DEBUG(0, ("tar mode not compiled. build with --with-libarchive\n"))
 
-struct tar
-{
-    int dummy;
-};
-
-struct tar tar_ctx;
-
 int cmd_block(void)
 {
     NOT_IMPLEMENTED;
@@ -1732,4 +1746,9 @@ bool tar_to_process(struct tar *tar)
     return false;
 }
 
+struct tar *tar_get_ctx()
+{
+    return NULL;
+}
+
 #endif
index 3b2671a93e94ca2770b1c68d91717c94234b0d40..65204768ea99a67b7fd9cefe0540ffbb7fb41e9c 100644 (file)
@@ -29,5 +29,6 @@ int cmd_tar(void);
 int tar_process(struct tar* tar);
 int tar_parse_args(struct tar *tar, const char *flag, const char **val, int valsize);
 bool tar_to_process(struct tar *tar);
+struct tar *tar_get_ctx(void);
 
 #endif /* _CLITAR_PROTO_H_ */