Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / rbd_replay / ImageNameMap.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 "ImageNameMap.hpp"
16
17
18 using namespace std;
19 using namespace rbd_replay;
20
21
22 bool ImageNameMap::parse_mapping(string mapping_string, Mapping *mapping) const {
23   string fields[2];
24   int field = 0;
25   for (size_t i = 0, n = mapping_string.length(); i < n; i++) {
26     char c = mapping_string[i];
27     switch (c) {
28     case '\\':
29       if (i != n - 1 && mapping_string[i + 1] == '=') {
30         i++;
31         fields[field].push_back('=');
32       } else {
33         fields[field].push_back('\\');
34       }
35       break;
36     case '=':
37       if (field == 1) {
38         return false;
39       }
40       field = 1;
41       break;
42     default:
43       fields[field].push_back(c);
44     }
45   }
46   if (field == 0) {
47     return false;
48   }
49   if (!mapping->first.parse(fields[0])) {
50     return false;
51   }
52   if (!mapping->second.parse(fields[1])) {
53     return false;
54   }
55   return true;
56 }
57
58 void ImageNameMap::add_mapping(const Mapping& mapping) {
59   m_map.insert(mapping);
60 }
61
62 rbd_loc ImageNameMap::map(const rbd_loc& name) const {
63   std::map<rbd_loc, rbd_loc>::const_iterator p(m_map.find(name));
64   if (p == m_map.end()) {
65     return name;
66   } else {
67     return p->second;
68   }
69 }