]>
git.proxmox.com Git - ceph.git/blob - ceph/src/tools/scratchtoolpp.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
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.
15 #include "include/types.h"
16 #include "include/rados/librados.hpp"
18 using namespace librados
;
26 #pragma GCC diagnostic ignored "-Wpragmas"
27 #pragma GCC diagnostic push
28 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
30 void buf_to_hex(const unsigned char *buf
, int len
, char *str
)
33 for (int i
= 0; i
< len
; i
++) {
34 sprintf(&str
[i
*2], "%02x", (int)buf
[i
]);
38 class C_Watch
: public WatchCtx
{
41 void notify(uint8_t opcode
, uint64_t ver
, bufferlist
& bl
) override
{
42 cout
<< "C_Watch::notify() opcode=" << (int)opcode
<< " ver=" << ver
<< std::endl
;
46 void testradospp_milestone(void)
49 cout
<< "*** press enter to continue ***" << std::endl
;
50 while ((c
= getchar()) != EOF
) {
56 int main(int argc
, const char **argv
)
59 if (rados
.init(NULL
) < 0) {
60 cerr
<< "couldn't initialize rados!" << std::endl
;
64 if (rados
.conf_read_file(NULL
)) {
65 cerr
<< "couldn't read configuration file." << std::endl
;
68 rados
.conf_parse_argv(argc
, argv
);
70 if (!rados
.conf_set("config option that doesn't exist",
71 "some random value")) {
72 printf("error: succeeded in setting nonexistent config option\n");
75 if (rados
.conf_set("log to stderr", "true")) {
76 printf("error: error setting log_to_stderr\n");
80 if (rados
.conf_get("log to stderr", tmp
)) {
81 printf("error: failed to read log_to_stderr from config\n");
85 printf("error: new setting for log_to_stderr failed to take effect.\n");
89 if (rados
.connect()) {
90 printf("error connecting\n");
94 cout
<< "rados_initialize completed" << std::endl
;
95 testradospp_milestone();
98 bufferlist bl
, bl2
, blf
;
102 snprintf(buf
, 128, "%s", ctime(&tm
));
103 bl
.append(buf
, strlen(buf
));
106 const char *oid
= "bar";
108 int r
= rados
.pool_create("foo");
109 cout
<< "pool_create result = " << r
<< std::endl
;
112 r
= rados
.ioctx_create("foo", io_ctx
);
113 cout
<< "ioctx_create result = " << r
<< std::endl
;
115 r
= io_ctx
.write(oid
, bl
, bl
.length(), 0);
116 uint64_t objver
= io_ctx
.get_last_version();
118 cout
<< "io_ctx.write returned " << r
<< " last_ver=" << objver
<< std::endl
;
122 r
= io_ctx
.stat(oid
, &stat_size
, &stat_mtime
);
123 cout
<< "io_ctx.stat returned " << r
<< " size = " << stat_size
<< " mtime = " << stat_mtime
<< std::endl
;
125 r
= io_ctx
.stat(oid
, NULL
, NULL
);
126 cout
<< "io_ctx.stat(does_not_exist) = " << r
<< std::endl
;
130 r
= io_ctx
.watch(oid
, objver
, &handle
, &wc
);
131 cout
<< "io_ctx.watch returned " << r
<< std::endl
;
133 testradospp_milestone();
134 io_ctx
.set_notify_timeout(7);
135 bufferlist notify_bl
;
136 r
= io_ctx
.notify(oid
, objver
, notify_bl
);
137 cout
<< "io_ctx.notify returned " << r
<< std::endl
;
138 testradospp_milestone();
140 r
= io_ctx
.notify(oid
, objver
, notify_bl
);
141 cout
<< "io_ctx.notify returned " << r
<< std::endl
;
142 testradospp_milestone();
144 r
= io_ctx
.unwatch(oid
, handle
);
145 cout
<< "io_ctx.unwatch returned " << r
<< std::endl
;
146 testradospp_milestone();
148 r
= io_ctx
.notify(oid
, objver
, notify_bl
);
149 cout
<< "io_ctx.notify returned " << r
<< std::endl
;
150 testradospp_milestone();
151 io_ctx
.set_assert_version(objver
);
153 r
= io_ctx
.write(oid
, bl
, bl
.length() - 1, 0);
154 cout
<< "io_ctx.write returned " << r
<< std::endl
;
156 r
= io_ctx
.write(oid
, bl
, bl
.length() - 2, 0);
157 cout
<< "io_ctx.write returned " << r
<< std::endl
;
158 r
= io_ctx
.write(oid
, bl
, bl
.length() - 3, 0);
159 cout
<< "rados.write returned " << r
<< std::endl
;
160 r
= io_ctx
.append(oid
, bl
, bl
.length());
161 cout
<< "rados.write returned " << r
<< std::endl
;
162 r
= io_ctx
.write_full(oid
, blf
);
163 cout
<< "rados.write_full returned " << r
<< std::endl
;
164 r
= io_ctx
.read(oid
, bl
, bl
.length(), 0);
165 cout
<< "rados.read returned " << r
<< std::endl
;
166 r
= io_ctx
.trunc(oid
, 8);
167 cout
<< "rados.trunc returned " << r
<< std::endl
;
168 r
= io_ctx
.read(oid
, bl
, bl
.length(), 0);
169 cout
<< "rados.read returned " << r
<< std::endl
;
170 r
= io_ctx
.exec(oid
, "crypto", "md5", bl
, bl2
);
171 cout
<< "exec returned " << r
<< " buf size=" << bl2
.length() << std::endl
;
172 const unsigned char *md5
= (const unsigned char *)bl2
.c_str();
173 char md5_str
[bl2
.length()*2 + 1];
174 buf_to_hex(md5
, bl2
.length(), md5_str
);
175 cout
<< "md5 result=" << md5_str
<< std::endl
;
177 // test assert_version
178 r
= io_ctx
.read(oid
, bl
, 0, 1);
180 uint64_t v
= io_ctx
.get_last_version();
181 cout
<< oid
<< " version is " << v
<< std::endl
;
183 io_ctx
.set_assert_version(v
);
184 r
= io_ctx
.read(oid
, bl
, 0, 1);
186 io_ctx
.set_assert_version(v
- 1);
187 r
= io_ctx
.read(oid
, bl
, 0, 1);
188 assert(r
== -ERANGE
);
189 io_ctx
.set_assert_version(v
+ 1);
190 r
= io_ctx
.read(oid
, bl
, 0, 1);
191 assert(r
== -EOVERFLOW
);
193 r
= io_ctx
.exec(oid
, "crypto", "sha1", bl
, bl2
);
194 cout
<< "exec returned " << r
<< std::endl
;
195 const unsigned char *sha1
= (const unsigned char *)bl2
.c_str();
196 char sha1_str
[bl2
.length()*2 + 1];
197 buf_to_hex(sha1
, bl2
.length(), sha1_str
);
198 cout
<< "sha1 result=" << sha1_str
<< std::endl
;
200 r
= io_ctx
.exec(oid
, "acl", "set", bl
, bl2
);
201 cout
<< "exec (set) returned " << r
<< std::endl
;
202 r
= io_ctx
.exec(oid
, "acl", "get", bl
, bl2
);
203 cout
<< "exec (get) returned " << r
<< std::endl
;
204 if (bl2
.length() > 0) {
205 cout
<< "attr=" << bl2
.c_str() << std::endl
;
208 int size
= io_ctx
.read(oid
, bl2
, 128, 0);
210 cout
<< "failed to read oid " << oid
<< "." << std::endl
;
214 cout
<< "read too many bytes from oid " << oid
<< "." << std::endl
;
218 memcpy(rbuf
, bl2
.c_str(), size
);
220 cout
<< "read result='" << rbuf
<< "'" << std::endl
;
221 cout
<< "size=" << size
<< std::endl
;
223 const char *oid2
= "jjj10.rbd";
224 r
= io_ctx
.exec(oid2
, "rbd", "snap_list", bl
, bl2
);
225 cout
<< "snap_list result=" << r
<< std::endl
;
226 r
= io_ctx
.exec(oid2
, "rbd", "snap_add", bl
, bl2
);
227 cout
<< "snap_add result=" << r
<< std::endl
;
230 char *s
= bl2
.c_str();
231 for (int i
=0; i
<r
; i
++, s
+= strlen(s
) + 1)
232 cout
<< s
<< std::endl
;
235 cout
<< "compound operation..." << std::endl
;
236 ObjectWriteOperation o
;
238 o
.setxattr("foo", bl2
);
239 r
= io_ctx
.operate(oid
, &o
);
240 cout
<< "operate result=" << r
<< std::endl
;
242 cout
<< "cmpxattr" << std::endl
;
245 r
= io_ctx
.setxattr(oid
, "foo", val
);
248 ObjectReadOperation o
;
249 o
.cmpxattr("foo", CEPH_OSD_CMPXATTR_OP_EQ
, val
);
250 r
= io_ctx
.operate(oid
, &o
, &bl2
);
251 cout
<< " got " << r
<< " wanted >= 0" << std::endl
;
256 ObjectReadOperation o
;
257 o
.cmpxattr("foo", CEPH_OSD_CMPXATTR_OP_EQ
, val
);
258 r
= io_ctx
.operate(oid
, &o
, &bl2
);
259 cout
<< " got " << r
<< " wanted " << -ECANCELED
<< " (-ECANCELED)" << std::endl
;
260 assert(r
== -ECANCELED
);
263 io_ctx
.locator_set_key(string());
265 cout
<< "iterating over objects..." << std::endl
;
267 for (NObjectIterator iter
= io_ctx
.nobjects_begin();
268 iter
!= io_ctx
.nobjects_end(); ++iter
) {
270 cout
<< "'" << *iter
<< "'" << std::endl
;
272 cout
<< "iterated over " << num_objs
<< " objects." << std::endl
;
273 map
<string
, bufferlist
> attrset
;
274 io_ctx
.getxattrs(oid
, attrset
);
276 map
<string
, bufferlist
>::iterator it
;
277 for (it
= attrset
.begin(); it
!= attrset
.end(); ++it
) {
278 cout
<< "xattr: " << it
->first
<< std::endl
;
281 r
= io_ctx
.remove(oid
);
282 cout
<< "remove result=" << r
<< std::endl
;
284 r
= rados
.pool_delete("foo");
285 cout
<< "pool_delete result=" << r
<< std::endl
;
292 #pragma GCC diagnostic pop
293 #pragma GCC diagnostic warning "-Wpragmas"