Ejemplo n.º 1
0
int
do_rm (struct cli_context *ctx)
{
        int argc = ctx->argc;
        char **argv = ctx->argv;
        int ret = -1;

        state = init_state ();
        if (state == NULL) {
                error (0, errno, "failed to initialize state");
                ret = -1;
                goto out;
        }

        state->debug = ctx->options->debug;

        if (ctx->fs) {
                ret = parse_options (argc, argv, true);
                if (ret != 0) {
                        goto out;
                }

                ret = rm (ctx->fs);
        } else {
                ret = parse_options (argc, argv, false);
                switch (ret) {
                        case -2:
                                // Fall through
                                ret = 0;
                        case -1:
                                goto out;
                }

                ret = rm_without_context ();
        }

out:
        if (state) {
                gluster_url_free (state->gluster_url);
                free (state->url);
        }

        free (state);

        return ret;
}
int
cli_disconnect (struct cli_context *ctx)
{
        int ret = 0;
        struct fd_list *cur, *ptr = NULL;

        free_xlator_options (&ctx->options->xlator_options);

        /* Traverse fd_list and cleanup each entry.*/
        ptr = ctx->flist;
        while (ptr) {
                if (ptr->fd) {
                        glfs_close (ptr->fd);
                        ptr->fd = NULL;
                }

                if (ptr->path) {
                        free (ptr->path);
                        ptr->path = NULL;
                }

                cur = ptr;
                ptr = ptr->next;
                free (cur);
        }

        if (ctx->fs) {
                // FIXME: Memory leak occurs here in GFS >= 3.6. Test with 3.7
                // and if fixed, remove the entry (xlator_mem_acct_init) from
                // the valgrind suppression file.
                ret = glfs_fini (ctx->fs);
                ctx->fs = NULL;
        }

        if (ctx->url) {
                gluster_url_free (ctx->url);
                ctx->url = NULL;
        }

        free (ctx->conn_str);
        ctx->conn_str = NULL;

        return ret;
}
int
cli_connect (struct cli_context *ctx)
{
        int argc = ctx->argc;
        char **argv = ctx->argv;
        int ret;
        int opt;
        int option_index;
        struct gluster_url *url = NULL;
        glfs_t *fs = NULL;
        uint16_t port = GLUSTER_DEFAULT_PORT;
        struct xlator_option *xlator_options = NULL;
        struct xlator_option *option;

        while ((opt = getopt_long (argc, argv, "o:p:", connect_options, &option_index)) != -1) {
                switch (opt) {
                        case 'o':
                                option = parse_xlator_option (optarg);
                                if (option == NULL) {
                                        error (0, errno, "%s", optarg);
                                        goto err;
                                }

                                if (append_xlator_option (&xlator_options, option) == -1) {
                                        error (0, errno, "append_xlator_option: %s", optarg);
                                        goto err;
                                }

                                break;
                        case 'p':
                                port = strtoport (optarg);
                                if (port == 0) {
                                        goto err;
                                }

                                break;
                        default:
                                goto err;
                }
        }

        ret = gluster_parse_url (argv[argc - 1], &url);
        if (ret == -1) {
                printf ("Usage: %s [OPTION]... URL\n"
                        "Connect to a Gluster volume for this session.\n\n"
                        "  -o, --xlator-option=OPTION   specify a translator option for the\n"
                        "                               connection. Multiple options are supported\n"
                        "                               and take the form xlator.key=value.\n"
                        "  -p, --port=PORT              specify the port on which to connect\n",
                        argv[0]);
                goto err;
        }

        url->port = port;

        ret = gluster_getfs (&fs, url);
        if (ret == -1) {
                error (0, errno, "failed to connect to %s/%s", url->host, url->volume);
                goto err;
        }

        ret = apply_xlator_options (fs, &xlator_options);
        if (ret == -1) {
                error (0, errno, "failed to apply translator options");
                goto err;
        }

        ret = cli_disconnect (ctx);
        if (ret == -1) {
                error (0, 0, "failed to terminate previous connection");
                goto err;
        }

        ctx->fs = fs;
        ctx->url = url;

        // TODO(craigcabrey): Look into using asprintf here.
        // 5 is the length of the string format: (%s/%s)
        size_t length = strlen (ctx->url->host) + strlen (ctx->url->volume) + 5;

        ctx->conn_str = malloc (length);
        if (ctx->conn_str == NULL) {
                error (0, errno, "malloc");
                handle_quit (ctx);
        }

        snprintf (ctx->conn_str,
                        length,
                        "(%s/%s)",
                        ctx->url->host,
                        ctx->url->volume);

        goto out;

err:
        ret = -1;
        gluster_url_free (url);
out:
        return ret;
}
Ejemplo n.º 4
0
int
main (int argc, char *argv[])
{
        glfs_t *fs;
        int ret;

        program_invocation_name = basename (argv[0]);
        atexit (close_stdout);

        state = init_state ();
        if (state == NULL) {
                error (0, errno, "failed to initialize state");
                goto err;
        }

        parse_options (argc, argv);

        ret = gluster_getfs (&fs, state->gluster_url);
        if (ret == -1) {
                error (0, errno, state->url);
                goto err;
        }

        ret = apply_xlator_options (fs, &state->xlator_options);
        if (ret == -1) {
                error (0, errno, "failed to apply translator options");
                goto err;
        }

        if (state->debug) {
                ret = glfs_set_logging (fs, "/dev/stderr", GF_LOG_DEBUG);

                if (ret == -1) {
                        error (0, errno, "failed to set logging level");
                        goto err;
                }
        }

        ret = gluster_put (fs, state);
        if (ret == -1) {
                error (0, errno, state->url);
                goto err;
        }

        ret = EXIT_SUCCESS;
        goto out;

err:
        ret = EXIT_FAILURE;
out:
        if (fs) {
                glfs_fini (fs);
        }

        if (state) {
                gluster_url_free (state->gluster_url);
                free (state->url);
        }

        free (state);

        return ret;
}