Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / rbd_replay / rbd_loc.cc
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"
16 #include "include/assert.h"
17
18
19 using namespace std;
20 using namespace rbd_replay;
21
22
23 rbd_loc::rbd_loc() {
24 }
25
26 rbd_loc::rbd_loc(string pool, string image, string snap)
27   : pool(pool),
28     image(image),
29     snap(snap) {
30 }
31
32 bool 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       }
44       assert(field == 0);
45       field++;
46       read_slash = true;
47       break;
48     case '@':
49       if (read_at) {
50         return false;
51       }
52       assert(field < 2);
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
84 static 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
94 string 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
108 int 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
124 bool rbd_loc::operator==(const rbd_loc& rhs) const {
125   return compare(rhs) == 0;
126 }
127
128 bool rbd_loc::operator<(const rbd_loc& rhs) const {
129   return compare(rhs) < 0;
130 }