forked from maidsafe-archive/MaidSafe-Drive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdirectory.h
147 lines (117 loc) · 5.98 KB
/
directory.h
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/* Copyright 2013 MaidSafe.net limited
This MaidSafe Software is licensed to you under (1) the MaidSafe.net Commercial License,
version 1.0 or later, or (2) The General Public License (GPL), version 3, depending on which
licence you accepted on initial access to the Software (the "Licences").
By contributing code to the MaidSafe Software, or to this project generally, you agree to be
bound by the terms of the MaidSafe Contributor Agreement, version 1.0, found in the root
directory of this project at LICENSE, COPYING and CONTRIBUTOR respectively and also
available at: http://www.maidsafe.net/licenses
Unless required by applicable law or agreed to in writing, the MaidSafe Software distributed
under the GPL Licence is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
OF ANY KIND, either express or implied.
See the Licences for the specific language governing permissions and limitations relating to
use of the MaidSafe Software. */
#ifndef MAIDSAFE_DRIVE_DIRECTORY_H_
#define MAIDSAFE_DRIVE_DIRECTORY_H_
#include <chrono>
#include <condition_variable>
#include <deque>
#include <memory>
#include <mutex>
#include <string>
#include <vector>
#include "boost/asio/io_service.hpp"
#include "boost/asio/steady_timer.hpp"
#include "boost/filesystem/path.hpp"
#include "boost/system/error_code.hpp"
#include "maidsafe/common/tagged_value.h"
#include "maidsafe/common/types.h"
#include "maidsafe/common/data_types/immutable_data.h"
#include "maidsafe/common/data_types/structured_data_versions.h"
#include "maidsafe/drive/config.h"
#include "maidsafe/drive/file_context.h"
namespace maidsafe {
namespace drive {
namespace detail {
class Directory;
namespace test {
void DirectoriesMatch(const Directory& lhs, const Directory& rhs);
class DirectoryTest;
} // namespace test
class Directory {
public:
Directory(ParentId parent_id, DirectoryId directory_id, boost::asio::io_service& io_service,
std::function<void(Directory*)> put_functor, // NOLINT
std::function<void(const ImmutableData&)> put_chunk_functor,
std::function<void(const std::vector<ImmutableData::Name>&)> increment_chunks_functor,
const boost::filesystem::path& path); // NOLINT
Directory(ParentId parent_id, const std::string& serialised_directory,
const std::vector<StructuredDataVersions::VersionName>& versions,
boost::asio::io_service& io_service, std::function<void(Directory*)> put_functor, // NOLINT
std::function<void(const ImmutableData&)> put_chunk_functor,
std::function<void(const std::vector<ImmutableData::Name>&)> increment_chunks_functor,
const boost::filesystem::path& path);
~Directory();
// This marks the start of an attempt to store the directory. It serialises the appropriate
// member data (critically parent_id_ must never be serialised), and sets 'store_state_' to
// kOngoing. It also calls 'FlushChild' on all children (see below).
std::string Serialise();
// Stores all new chunks from 'child', increments all the other chunks, and resets child's
// self_encryptor & buffer.
void FlushChildAndDeleteEncryptor(FileContext* child);
size_t VersionsCount() const;
std::tuple<DirectoryId, StructuredDataVersions::VersionName>
InitialiseVersions(ImmutableData::Name version_id);
// This marks the end of an attempt to store the directory. It returns directory_id and most
// recent 2 version names (including the one passed in), and sets 'store_state_' to kComplete.
std::tuple<DirectoryId, StructuredDataVersions::VersionName, StructuredDataVersions::VersionName>
AddNewVersion(ImmutableData::Name version_id);
bool HasChild(const boost::filesystem::path& name) const;
const FileContext* GetChild(const boost::filesystem::path& name) const;
FileContext* GetMutableChild(const boost::filesystem::path& name);
const FileContext* GetChildAndIncrementCounter();
void AddChild(FileContext&& child);
FileContext RemoveChild(const boost::filesystem::path& name);
void RenameChild(const boost::filesystem::path& old_name,
const boost::filesystem::path& new_name);
void ResetChildrenCounter();
bool empty() const;
ParentId parent_id() const;
// This will block while a store attempt is ongoing.
void SetNewParent(const ParentId parent_id, std::function<void(Directory*)> put_functor, // NOLINT
const boost::filesystem::path& path);
DirectoryId directory_id() const;
void ScheduleForStoring();
void StoreImmediatelyIfPending();
friend void test::DirectoriesMatch(const Directory& lhs, const Directory& rhs);
friend class test::DirectoryTest;
// TODO(Fraser#5#): 2014-01-30 - BEFORE_RELEASE - Make mutex_ private.
mutable std::mutex mutex_;
private:
Directory(const Directory& other);
Directory(Directory&& other);
Directory& operator=(Directory other);
typedef std::vector<std::unique_ptr<FileContext>> Children;
Children::iterator Find(const boost::filesystem::path& name);
Children::const_iterator Find(const boost::filesystem::path& name) const;
void SortAndResetChildrenCounter();
void DoScheduleForStoring(bool use_delay = true);
std::condition_variable cond_var_;
ParentId parent_id_;
DirectoryId directory_id_;
boost::asio::steady_timer timer_;
std::function<void(const boost::system::error_code&)> store_functor_;
std::function<void(const ImmutableData&)> put_chunk_functor_;
std::function<void(std::vector<ImmutableData::Name>)> increment_chunks_functor_;
std::vector<ImmutableData::Name> chunks_to_be_incremented_;
std::deque<StructuredDataVersions::VersionName> versions_;
MaxVersions max_versions_;
Children children_;
size_t children_count_position_;
enum class StoreState { kPending, kOngoing, kComplete } store_state_;
};
bool operator<(const Directory& lhs, const Directory& rhs);
} // namespace detail
} // namespace drive
} // namespace maidsafe
#endif // MAIDSAFE_DRIVE_DIRECTORY_H_