19
RootImpl::RootImpl(QString const& identity, weak_ptr<Account> const& account)
20
: FolderImpl(identity, ItemType::root)
23
using namespace boost::filesystem;
25
path id_path = path(identity.toStdString());
26
if (!id_path.is_absolute())
28
throw StorageException(); // TODO
30
path can_path = canonical(id_path);
31
auto id_len = std::distance(id_path.begin(), id_path.end());
32
auto can_len = std::distance(can_path.begin(), can_path.end());
33
if (id_len != can_len)
35
// identity denotes a weird path that we won't trust because
36
// it might contain ".." or similar.
37
throw StorageException(); // TODO
39
assert(account.lock());
42
QString RootImpl::name() const
47
QFuture<QVector<Folder::SPtr>> RootImpl::parents() const
49
QFutureInterface<QVector<Folder::SPtr>> qf;
50
qf.reportResult(QVector<Folder::SPtr>()); // For the root, we return an empty vector.
55
QVector<QString> RootImpl::parent_ids() const
57
return QVector<QString>(); // For the root, we return an empty vector.
21
60
QFuture<void> RootImpl::destroy()
23
62
// Cannot destroy root.
65
105
qf.reportException(StorageException()); // TODO
67
108
return qf.future();
70
111
QFuture<Item::SPtr> RootImpl::get(QString native_identity) const
72
return QFuture<Item::SPtr>(); // TODO
113
using namespace boost::filesystem;
115
QFutureInterface<Item::SPtr> qf;
118
path id_path = native_identity.toStdString();
119
if (!id_path.is_absolute())
121
throw StorageException(); // TODO
124
// Make sure that native_identity is contained in or equal to the root path.
125
id_path = canonical(id_path);
126
auto root_path = path(root()->native_identity().toStdString());
127
auto id_len = std::distance(id_path.begin(), id_path.end());
128
auto root_len = std::distance(root_path.begin(), root_path.end());
129
if (id_len < root_len)
131
// native_identity can't possibly point at something below the root.
132
throw StorageException(); // TODO
134
if (!std::equal(root_path.begin(), root_path.end(), id_path.begin()))
136
// id_path differs from root path in some path component, so id_path
137
// does not point at a location that's contained in root_path.
138
throw StorageException();
141
file_status s = status(id_path);
142
QString path = QString::fromStdString(id_path.native());
145
if (id_path == root_path)
147
qf.reportResult(make_root(path, account_));
151
qf.reportResult(make_folder(path, root_));
154
else if (is_regular_file(s))
156
qf.reportResult(FileImpl::make_file(path, root_));
160
// Ignore everything that's not a directory or file.
163
catch (std::exception const&)
165
qf.reportException(StorageException()); // TODO
171
Root::SPtr RootImpl::make_root(QString const& identity, std::weak_ptr<Account> const& account)
173
assert(account.lock());
175
auto impl = new RootImpl(identity, account);
176
Root::SPtr root(new Root(impl));
177
impl->set_root(root);
178
impl->set_public_instance(root);
75
182
} // namespace internal