MockNameServer.hpp 7.88 KB
Newer Older
1
/*
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 * The CERN Tape Archive (CTA) project
 * Copyright (C) 2015  CERN
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#pragma once

21
#include "common/archiveNS/ArchiveDirIterator.hpp"
22
#include "common/UserIdentity.hpp"
23
#include "nameserver/NameServer.hpp"
24
#include "common/SecurityIdentity.hpp"
25

26
#include <cstdatomic>
27
#include <list>
28
#include <mutex>
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <string>

namespace cta {

/**
 * Local file system implementation of a name server to contain the archive
 * namespace.
 */
class MockNameServer: public NameServer {

public:

  /**
   * Constructor.
   */
  MockNameServer();
45
46
47
48
49
50
51
  
  /**
   * Constructor for an already existing mock name server temporary directory
   *
   * @param path The already existing mock name server temporary directory
   */
  MockNameServer(const std::string &path);
52
53
54
55
56

  /**
   * Destructor.
   */
  ~MockNameServer() throw();  
57
58
59

  void createStorageClass(const SecurityIdentity &requester, const std::string &name, const uint16_t nbCopies); 

60
  void createStorageClass(const SecurityIdentity &requester, const std::string &name, const uint16_t nbCopies, const uint32_t id);
Steven Murray's avatar
WIP    
Steven Murray committed
61

62
63
64
  void deleteStorageClass(const SecurityIdentity &requester, const std::string &name);

  void updateStorageClass(const SecurityIdentity &requester, const std::string &name, const uint16_t nbCopies);
65
66
67
68
69
70
71
  
  void setDirStorageClass(const SecurityIdentity &requester, const std::string &path, const std::string &storageClassName);
  
  void clearDirStorageClass(const SecurityIdentity &requester, const std::string &path);
  
  std::string getDirStorageClass(const SecurityIdentity &requester, const std::string &path) const;
  
72
  void createFile(const SecurityIdentity &requester, const std::string &path, const mode_t mode, const uint64_t size);
73
74
75
76

  void setOwner(const SecurityIdentity &requester, const std::string &path, const UserIdentity &owner);

  UserIdentity getOwner(const SecurityIdentity &requester, const std::string &path) const;
77
  
78
  void createDir(const SecurityIdentity &requester, const std::string &path, const mode_t mode);
79
80
81
82
83
  
  void deleteFile(const SecurityIdentity &requester, const std::string &path);
  
  void deleteDir(const SecurityIdentity &requester, const std::string &path);
  
84
  std::unique_ptr<ArchiveFileStatus> statFile(const SecurityIdentity &requester, const std::string &path) const;
85
  
86
  ArchiveDirIterator getDirContents(const SecurityIdentity &requester, const std::string &path) const;
87
  
88
  std::string getVidOfFile(const SecurityIdentity &requester, const std::string &path, const uint16_t copyNb) const;
89
  
90
  void assertStorageClassIsNotInUse(const SecurityIdentity &requester, const std::string &storageClass, const std::string &path) const;
91

92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
  /**
   * Add the specified tape file entry to the archive namespace.
   *
   * @param requester The identity of the requester.
   * @param path The absolute path of the archive file.
   * @param nameServerTapeFile The tape file entry.
   */
  void addTapeFile(
    const SecurityIdentity &requester,
    const std::string &path,
    const NameServerTapeFile &tapeFile);

  /**
   * Gets the tape entries from the archive namespace corresponding the archive
   * with the specified path.
   *
   * @param requester The identity of the requester.
   * @param path The absolute path of the archive file.
   * @return The tape file entries.
   */
  std::list<NameServerTapeFile> getTapeFiles(
    const SecurityIdentity &requester,
114
    const std::string &path) const;
115
116
117
118
119
120
121
122
123
124
125
126
  
  /**
   * Delete the specified tape file entry from the archive namespace.
   *
   * @param requester The identity of the requester.
   * @param path The absolute path of the archive file.
   * @param copyNb The tape copy to delete.
   */
  virtual void deleteTapeFile(
    const SecurityIdentity &requester,
    const std::string &path,
    const uint16_t copyNb);
127

128
private:
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
  
  /**
   * Converts a NameServerTapeFile to a string to be used as an extended attribute
   * 
   * @param tapeFile The NameServerTapeFile object
   * @return the converted string
   */
  std::string fromNameServerTapeFileToString(const cta::NameServerTapeFile &tapeFile) const;
  
  /**
   * Converts a string (the value of an extended attribute) to a NameServerTapeFile
   * 
   * @param xAttributeString The value of an extended attribute
   * @return the NameServerTapeFile object resulting from the conversion
   */
  cta::NameServerTapeFile fromStringToNameServerTapeFile(const std::string &xAttributeString) const;
145

146
147
148
149
  std::string m_fsDir;
  
  void assertFsDirExists(const std::string &path) const;
  
Daniele Kruse's avatar
Daniele Kruse committed
150
151
  void assertFsFileExists(const std::string &path) const;
  
152
153
  void assertFsPathDoesNotExist(const std::string &path) const;
  
154
  std::list<cta::ArchiveDirEntry> getDirEntries(const SecurityIdentity &requester, const std::string &path) const;
155
156
157

  /**
   * Throws an exception if the specified user is not the owner of the
158
   * specified namespace entry.
159
   *
160
   * @param requester The identity of the requester.
161
162
163
164
   * @param user The user.
   * @param path The absolute path of the namespace entry.
   */
  void assertIsOwner(const SecurityIdentity &requester, const UserIdentity &user, const std::string &path) const;
165
166
167
168
169
170
171

  /**
   * Returns the directory entry corresponding to the specified path.
   *
   * @param requester The identity of the requester.
   * @param The absolute path of the namespace entry.
   */
172
  ArchiveDirEntry getArchiveDirEntry(
173
174
    const SecurityIdentity &requester,
    const std::string &path) const;
175

176
177
178
179
180
181
182
183
184
185
186
187
188
  /**
   * Returns the directory entry corresponding to the specified path and stat()
   * result.
   *
   * @param requester The identity of the requester.
   * @param The absolute path of the namespace entry.
   * @param statResult The result of running stat().
   */
  ArchiveDirEntry getArchiveDirEntry(
    const SecurityIdentity &requester,
    const std::string &path,
    const struct stat statResult) const;

189
  /**
190
   * The string name and numeric identifier of a storage class.
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
   */
  struct StorageClassNameAndId {
    std::string name;
    uint32_t id;

    StorageClassNameAndId(): id(0) {
    }

    StorageClassNameAndId(const std::string &name, const uint32_t id):
      name(name), id(id) {
    }
  };

  /**
   * The list of storage class.
   */
  std::list<StorageClassNameAndId> m_storageClasses;

  /**
   * Throws an exception if the specified storage class name already exists.
   *
   * @paran name The name of teh storage class.
   */
  void assertStorageClassNameDoesNotExist(const std::string &name) const;

  /**
   * Throws an exception if the specified storage class numeric identifier
   * already exists.
   *
220
   * @param id The numeric identifier of the storage class.
221
222
223
224
225
226
227
228
229
230
231
232
   */
  void assertStorageClassIdDoesNotExist(const uint32_t id) const;

  /**
   * Returns the next unique numeric identifier for a new storage class.
   *
   * Please note that the numeric identifiers of deleted storage classes can be
   * reused.
   *
   * @return The next unique numeric identifier for a new storage class.
   */
  uint32_t getNextStorageClassId() const;
233
234
235
236
237
  
  /**
   * Counter for file ID of new files
   */
  std::atomic<uint64_t> m_fileIdCounter;
238
239
240
241
242
  
  /**
   * Mutex to serialize access to the file system modification functions
   */
  std::mutex m_mutex;
243
244
245
246
247
  
  /**
   * Flag that determines whether the base path of the mock name server needs to be deleted by the destructor
   */
  bool m_deleteOnExit;
248

249
250
251
}; // class MockNameServer

} // namespace cta