示例#1
0
static int
cat_without_context ()
{
        glfs_t *fs = NULL;
        int ret = -1;

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

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

        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 out;
                }
        }

        ret = gluster_get (fs, state->gluster_url->path);
        if (ret == -1) {
                goto out;
        }

        ret = 0;

out:
        if (fs) {
                glfs_fini (fs);
        }

        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;
}
示例#3
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;
}