summaryrefslogtreecommitdiffstats
path: root/src/rbd_replay/rbd_loc.cc
blob: ce6e8e6ed830e2df134380e6384216c87f4af78c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
/*
 * Ceph - scalable distributed file system
 *
 * Copyright (C) 2014 Adam Crume <adamcrume@gmail.com>
 *
 * This is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1, as published by the Free Software
 * Foundation.  See file COPYING.
 *
 */

#include "rbd_loc.hpp"
#include "include/ceph_assert.h"


using namespace std;
using namespace rbd_replay;


rbd_loc::rbd_loc() {
}

rbd_loc::rbd_loc(const string &pool, const string &image, const string &snap)
  : pool(pool),
    image(image),
    snap(snap) {
}

bool rbd_loc::parse(string name_string) {
  int field = 0;
  string fields[3];
  bool read_slash = false;
  bool read_at = false;
  for (size_t i = 0, n = name_string.length(); i < n; i++) {
    char c = name_string[i];
    switch (c) {
    case '/':
      if (read_slash || read_at) {
	return false;
      }
      ceph_assert(field == 0);
      field++;
      read_slash = true;
      break;
    case '@':
      if (read_at) {
	return false;
      }
      ceph_assert(field < 2);
      field++;
      read_at = true;
      break;
    case '\\':
      if (i == n - 1) {
	return false;
      }
      fields[field].push_back(name_string[++i]);
      break;
    default:
      fields[field].push_back(c);
    }
  }

  if (read_slash) {
    pool = fields[0];
    image = fields[1];
    // note that if read_at is false, then fields[2] is the empty string,
    // so this is still correct
    snap = fields[2];
  } else {
    pool = "";
    image = fields[0];
    // note that if read_at is false, then fields[1] is the empty string,
    // so this is still correct
    snap = fields[1];
  }
  return true;
}


static void write(const string &in, string *out) {
  for (size_t i = 0, n = in.length(); i < n; i++) {
    char c = in[i];
    if (c == '@' || c == '/' || c == '\\') {
      out->push_back('\\');
    }
    out->push_back(c);
  }
}

string rbd_loc::str() const {
  string out;
  if (!pool.empty()) {
    write(pool, &out);
    out.push_back('/');
  }
  write(image, &out);
  if (!snap.empty()) {
    out.push_back('@');
    write(snap, &out);
  }
  return out;
}

int rbd_loc::compare(const rbd_loc& rhs) const {
  int c = pool.compare(rhs.pool);
  if (c) {
    return c;
  }
  c = image.compare(rhs.image);
  if (c) {
    return c;
  }
  c = snap.compare(rhs.snap);
  if (c) {
    return c;
  }
  return 0;
}

bool rbd_loc::operator==(const rbd_loc& rhs) const {
  return compare(rhs) == 0;
}

bool rbd_loc::operator<(const rbd_loc& rhs) const {
  return compare(rhs) < 0;
}