]> git.proxmox.com Git - ceph.git/blame - ceph/src/rbd_replay/rbd_loc.cc
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / rbd_replay / rbd_loc.cc
CommitLineData
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) 2014 Adam Crume <adamcrume@gmail.com>
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#include "rbd_loc.hpp"
11fdf7f2 16#include "include/ceph_assert.h"
7c673cae
FG
17
18
19using namespace std;
20using namespace rbd_replay;
21
22
23rbd_loc::rbd_loc() {
24}
25
11fdf7f2 26rbd_loc::rbd_loc(const string &pool, const string &image, const string &snap)
7c673cae
FG
27 : pool(pool),
28 image(image),
29 snap(snap) {
30}
31
32bool rbd_loc::parse(string name_string) {
33 int field = 0;
34 string fields[3];
35 bool read_slash = false;
36 bool read_at = false;
37 for (size_t i = 0, n = name_string.length(); i < n; i++) {
38 char c = name_string[i];
39 switch (c) {
40 case '/':
41 if (read_slash || read_at) {
42 return false;
43 }
11fdf7f2 44 ceph_assert(field == 0);
7c673cae
FG
45 field++;
46 read_slash = true;
47 break;
48 case '@':
49 if (read_at) {
50 return false;
51 }
11fdf7f2 52 ceph_assert(field < 2);
7c673cae
FG
53 field++;
54 read_at = true;
55 break;
56 case '\\':
57 if (i == n - 1) {
58 return false;
59 }
60 fields[field].push_back(name_string[++i]);
61 break;
62 default:
63 fields[field].push_back(c);
64 }
65 }
66
67 if (read_slash) {
68 pool = fields[0];
69 image = fields[1];
70 // note that if read_at is false, then fields[2] is the empty string,
71 // so this is still correct
72 snap = fields[2];
73 } else {
74 pool = "";
75 image = fields[0];
76 // note that if read_at is false, then fields[1] is the empty string,
77 // so this is still correct
78 snap = fields[1];
79 }
80 return true;
81}
82
83
84static void write(const string &in, string *out) {
85 for (size_t i = 0, n = in.length(); i < n; i++) {
86 char c = in[i];
87 if (c == '@' || c == '/' || c == '\\') {
88 out->push_back('\\');
89 }
90 out->push_back(c);
91 }
92}
93
94string rbd_loc::str() const {
95 string out;
96 if (!pool.empty()) {
97 write(pool, &out);
98 out.push_back('/');
99 }
100 write(image, &out);
101 if (!snap.empty()) {
102 out.push_back('@');
103 write(snap, &out);
104 }
105 return out;
106}
107
108int rbd_loc::compare(const rbd_loc& rhs) const {
109 int c = pool.compare(rhs.pool);
110 if (c) {
111 return c;
112 }
113 c = image.compare(rhs.image);
114 if (c) {
115 return c;
116 }
117 c = snap.compare(rhs.snap);
118 if (c) {
119 return c;
120 }
121 return 0;
122}
123
124bool rbd_loc::operator==(const rbd_loc& rhs) const {
125 return compare(rhs) == 0;
126}
127
128bool rbd_loc::operator<(const rbd_loc& rhs) const {
129 return compare(rhs) < 0;
130}