~michihenning/storage-framework/increase-timeout

« back to all changes in this revision

Viewing changes to src/qt/client/internal/local_client/RootImpl.cpp

  • Committer: Michi Henning
  • Date: 2016-08-10 06:45:41 UTC
  • mfrom: (10.10.15 more-coverage)
  • Revision ID: michi.henning@canonical.com-20160810064541-ty9vzh6dff8jgfpw
Merged lp:~michihenning/storage-framework/more-coverage:
Improved coverage. Better error reporting/handling.

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
#include <unity/storage/qt/client/Exceptions.h>
22
22
#include <unity/storage/qt/client/internal/make_future.h>
23
23
#include <unity/storage/qt/client/internal/local_client/FileImpl.h>
 
24
#include <unity/storage/qt/client/internal/local_client/storage_exception.h>
24
25
#include <unity/storage/qt/client/Root.h>
25
26
 
26
27
using namespace std;
68
69
 
69
70
QString RootImpl::name() const
70
71
{
 
72
    lock_guard<decltype(mutex_)> guard(mutex_);
 
73
 
 
74
    throw_if_destroyed("Root::name()");
71
75
    return "";
72
76
}
73
77
 
74
78
QFuture<QVector<Folder::SPtr>> RootImpl::parents() const
75
79
{
 
80
    lock_guard<decltype(mutex_)> guard(mutex_);
 
81
 
 
82
    throw_if_destroyed("Root::parents()");
76
83
    return make_ready_future(QVector<Folder::SPtr>());  // For the root, we return an empty vector.
77
84
}
78
85
 
79
86
QVector<QString> RootImpl::parent_ids() const
80
87
{
 
88
    lock_guard<decltype(mutex_)> guard(mutex_);
 
89
 
 
90
    throw_if_destroyed("Root::parent_ids()");
81
91
    return QVector<QString>();  // For the root, we return an empty vector.
82
92
}
83
93
 
84
94
QFuture<void> RootImpl::delete_item()
85
95
{
 
96
    lock_guard<decltype(mutex_)> guard(mutex_);
 
97
 
 
98
    try
 
99
    {
 
100
        throw_if_destroyed("Root::delete_item()");
 
101
    }
 
102
    catch (StorageException const& e)
 
103
    {
 
104
        return internal::make_exceptional_future(e);
 
105
    }
86
106
    // Cannot delete root.
87
 
    return make_exceptional_future(LogicException("Root::delete_item(): Cannot delete root folder"));
 
107
    return internal::make_exceptional_future(LogicException("Root::delete_item(): Cannot delete root folder"));
88
108
}
89
109
 
90
110
QFuture<int64_t> RootImpl::free_space_bytes() const
91
111
{
 
112
    lock_guard<decltype(mutex_)> guard(mutex_);
 
113
 
 
114
    try
 
115
    {
 
116
        throw_if_destroyed("Root::free_space_bytes()");
 
117
    }
 
118
    catch (StorageException const& e)
 
119
    {
 
120
        return internal::make_exceptional_future<int64_t>(e);
 
121
    }
 
122
 
92
123
    using namespace boost::filesystem;
93
124
 
94
125
    try
96
127
        space_info si = space(identity_.toStdString());
97
128
        return make_ready_future<int64_t>(si.available);
98
129
    }
99
 
    catch (std::exception const& e)
 
130
    // LCOV_EXCL_START
 
131
    catch (std::exception const&)
100
132
    {
101
 
        return make_exceptional_future<int64_t>(ResourceException(QString("Root::free_space_bytes(): ") + e.what()));
 
133
        return make_exceptional_future<int64_t>(QString("Root::free_space_bytes()"), current_exception());
102
134
    }
 
135
    // LCOV_EXCL_STOP
103
136
}
104
137
 
105
138
QFuture<int64_t> RootImpl::used_space_bytes() const
106
139
{
 
140
    lock_guard<decltype(mutex_)> guard(mutex_);
 
141
 
 
142
    try
 
143
    {
 
144
        throw_if_destroyed("Root::used_space_bytes()");
 
145
    }
 
146
    catch (StorageException const& e)
 
147
    {
 
148
        return internal::make_exceptional_future<int64_t>(e);
 
149
    }
 
150
 
107
151
    using namespace boost::filesystem;
108
152
 
109
153
    try
111
155
        space_info si = space(identity_.toStdString());
112
156
        return make_ready_future<int64_t>(si.capacity - si.available);
113
157
    }
114
 
    catch (std::exception const& e)
 
158
    // LCOV_EXCL_START
 
159
    catch (std::exception const&)
115
160
    {
116
 
        return make_exceptional_future<int64_t>(ResourceException(QString("Root::used_space_bytes(): ") + e.what()));
 
161
        return make_exceptional_future<int64_t>(QString("Root::used_space_bytes()"), current_exception());
117
162
    }
 
163
    // LCOV_EXCL_STOP
118
164
}
119
165
 
120
166
QFuture<Item::SPtr> RootImpl::get(QString native_identity) const
121
167
{
 
168
    lock_guard<decltype(mutex_)> guard(mutex_);
 
169
 
 
170
    try
 
171
    {
 
172
        throw_if_destroyed("Root::get()");
 
173
    }
 
174
    catch (StorageException const& e)
 
175
    {
 
176
        return internal::make_exceptional_future<Item::SPtr>(e);
 
177
    }
 
178
 
 
179
    auto root = get_root();
 
180
    if (!root)
 
181
    {
 
182
        return internal::make_exceptional_future<Item::SPtr>(RuntimeDestroyedException("Root::get()"));
 
183
    }
 
184
 
122
185
    using namespace boost::filesystem;
123
186
 
124
187
    QFutureInterface<Item::SPtr> qf;
127
190
        path id_path = native_identity.toStdString();
128
191
        if (!id_path.is_absolute())
129
192
        {
130
 
            QString msg = "Root::get(): identity must be an absolute path";
131
 
            return make_exceptional_future<Item::SPtr>(InvalidArgumentException(msg));
 
193
            QString msg = "Root::get(): identity \"" + native_identity + "\" must be an absolute path";
 
194
            throw InvalidArgumentException(msg);
132
195
        }
133
196
 
134
197
        // Make sure that native_identity is contained in or equal to the root path.
135
198
        id_path = canonical(id_path);
136
 
        auto root_path = path(root()->native_identity().toStdString());
 
199
        auto root_path = path(root->native_identity().toStdString());
137
200
        auto id_len = std::distance(id_path.begin(), id_path.end());
138
201
        auto root_len = std::distance(root_path.begin(), root_path.end());
139
202
        if (id_len < root_len || !std::equal(root_path.begin(), root_path.end(), id_path.begin()))
141
204
            // Too few components, or wrong path prefix. Therefore, native_identity can't
142
205
            // possibly point at something below the root.
143
206
            QString msg = QString("Root::get(): identity \"") + native_identity + "\" points outside the root folder";
144
 
            return make_exceptional_future<Item::SPtr>(InvalidArgumentException(msg));
 
207
            throw InvalidArgumentException(msg);
145
208
        }
146
209
 
147
210
        // Don't allow reserved files to be found.
148
211
        if (is_reserved_path(id_path))
149
212
        {
150
 
            QString msg = "Root::get(): no such item: " + native_identity;
151
 
            return make_exceptional_future<Item::SPtr>(NotExistsException(msg, native_identity));
 
213
            QString msg = "Root::get(): no such item: \"" + native_identity + "\"";
 
214
            throw NotExistsException(msg, native_identity);
152
215
        }
153
216
 
154
217
        file_status s = status(id_path);
157
220
        {
158
221
            if (id_path == root_path)
159
222
            {
160
 
                return make_ready_future<Item::SPtr>(make_root(path, account_));
 
223
                return make_ready_future<Item::SPtr>(make_root(path, account()));
161
224
            }
162
 
            return make_ready_future<Item::SPtr>(make_folder(path, root_));
 
225
            return make_ready_future<Item::SPtr>(make_folder(path, root));
163
226
        }
164
227
        if (is_regular_file(s))
165
228
        {
166
 
            return make_ready_future<Item::SPtr>(FileImpl::make_file(path, root_));
 
229
            return make_ready_future<Item::SPtr>(FileImpl::make_file(path, root));
167
230
        }
168
 
        QString msg = "Root::get(): no such item: " + native_identity;
169
 
        return make_exceptional_future<Item::SPtr>(NotExistsException(msg, native_identity));
 
231
        QString msg = "Root::get(): no such item: \"" + native_identity + "\"";
 
232
        throw NotExistsException(msg, native_identity);
170
233
    }
171
 
    catch (std::exception const& e)
 
234
    catch (std::exception const&)
172
235
    {
173
 
        return make_exceptional_future<Item::SPtr>(ResourceException(QString("Root::get(): ") + e.what()));
 
236
        return make_exceptional_future<Item::SPtr>(QString("Root::get()"), current_exception(), native_identity);
174
237
    }
175
238
}
176
239