]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- |
2 | // vim: ts=8 sw=2 smarttab | |
3 | /* | |
4 | * Ceph - scalable distributed file system | |
5 | * | |
6 | * Copyright (C) 2011 New Dream Network | |
7 | * | |
8 | * This is free software; you can redistribute it and/or | |
9 | * modify it under the terms of the GNU Lesser General Public | |
10 | * License version 2.1, as published by the Free Software | |
11 | * Foundation. See file COPYING. | |
12 | * | |
13 | */ | |
14 | ||
15 | /* | |
16 | * Test Ioctx::operate | |
17 | */ | |
18 | ||
19 | #include "common/ceph_argparse.h" | |
20 | #include "common/debug.h" | |
21 | #include "common/config.h" | |
22 | #include "global/global_init.h" | |
23 | #include "include/rados/librados.hpp" | |
24 | #include "include/types.h" | |
25 | ||
26 | #include <errno.h> | |
27 | #include <iostream> | |
28 | #include <string> | |
29 | ||
30 | using std::cerr; | |
31 | using std::string; | |
32 | ||
33 | using namespace librados; | |
34 | ||
35 | static void usage(void) | |
36 | { | |
37 | cerr << "--oid set object id to 'operate' on" << std::endl; | |
38 | cerr << "--pool set pool to 'operate' on" << std::endl; | |
39 | } | |
40 | ||
41 | int main(int argc, const char **argv) | |
42 | { | |
20effc67 | 43 | auto args = argv_to_vec(argc, argv); |
7c673cae | 44 | auto cct = global_init(NULL, args, CEPH_ENTITY_TYPE_CLIENT, |
11fdf7f2 TL |
45 | CODE_ENVIRONMENT_UTILITY, |
46 | CINIT_FLAG_NO_DEFAULT_CONFIG_FILE); | |
7c673cae FG |
47 | common_init_finish(g_ceph_context); |
48 | ||
49 | string val; | |
50 | string oid("ceph_test_object"); | |
51 | string pool_name("test_pool"); | |
52 | for (std::vector<const char*>::iterator i = args.begin(); i != args.end(); ) { | |
53 | if (ceph_argparse_double_dash(args, i)) { | |
54 | break; | |
55 | } | |
56 | else if (ceph_argparse_witharg(args, i, &val, "--oid", "-o", (char*)NULL)) { | |
57 | oid = val; | |
58 | } | |
59 | else if (ceph_argparse_witharg(args, i, &val, "--pool", "-p", (char*)NULL)) { | |
60 | pool_name = val; | |
61 | } | |
62 | else { | |
63 | cerr << "unknown command line option: " << *i << std::endl; | |
64 | cerr << std::endl; | |
65 | usage(); | |
66 | return 2; | |
67 | } | |
68 | } | |
69 | ||
70 | Rados rados; | |
71 | if (rados.init_with_context(g_ceph_context) < 0) { | |
72 | cerr << "couldn't initialize rados!" << std::endl; | |
73 | return 1; | |
74 | } | |
75 | if (rados.conf_read_file(NULL) < 0) { | |
76 | cerr << "failed to read rados configuration file!" << std::endl; | |
77 | return 1; | |
78 | } | |
79 | if (rados.connect() < 0) { | |
80 | cerr << "couldn't connect to cluster!" << std::endl; | |
81 | return 1; | |
82 | } | |
83 | ||
20effc67 | 84 | int ret = 0; |
7c673cae FG |
85 | librados::ObjectWriteOperation o; |
86 | IoCtx ioctx; | |
87 | if (rados.pool_lookup(pool_name.c_str()) <= 0) { | |
88 | ret = rados.pool_create(pool_name.c_str()); | |
89 | if (ret) { | |
90 | cerr << "failed to create pool named '" << pool_name | |
91 | << "': error " << ret << std::endl; | |
92 | return 1; | |
93 | } | |
94 | } | |
95 | ret = rados.ioctx_create(pool_name.c_str(), ioctx); | |
96 | if (ret) { | |
97 | cerr << "failed to create ioctx for pool '" << pool_name | |
98 | << "': error " << ret << std::endl; | |
99 | return 1; | |
100 | } | |
c07f9fc5 FG |
101 | ioctx.application_enable("rados", true); |
102 | ||
7c673cae FG |
103 | librados::ObjectWriteOperation op; |
104 | op.create(true); | |
105 | ret = ioctx.operate(oid, &op); | |
106 | if (ret) { | |
107 | cerr << "ioctx.operate failed: ret = " << ret << std::endl; | |
108 | return 1; | |
109 | } | |
110 | ||
111 | return 0; | |
112 | } |