1 // Copyright 2018 The Fuchsia Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include <fs/synchronous-vfs.h>
6 
7 #include <fbl/unique_ptr.h>
8 #include <lib/async/cpp/task.h>
9 #include <lib/sync/completion.h>
10 
11 #include <utility>
12 
13 namespace fs {
14 
SynchronousVfs()15 SynchronousVfs::SynchronousVfs() : is_shutting_down_(false) {}
16 
SynchronousVfs(async_dispatcher_t * dispatcher)17 SynchronousVfs::SynchronousVfs(async_dispatcher_t* dispatcher) : Vfs(dispatcher),
18     is_shutting_down_(false) {}
19 
~SynchronousVfs()20 SynchronousVfs::~SynchronousVfs() {
21     Shutdown(nullptr);
22     ZX_DEBUG_ASSERT(connections_.is_empty());
23 }
24 
25 // Synchronously drop all connections.
Shutdown(ShutdownCallback handler)26 void SynchronousVfs::Shutdown(ShutdownCallback handler) {
27     is_shutting_down_ = true;
28 
29     UninstallAll(ZX_TIME_INFINITE);
30     while (!connections_.is_empty()) {
31         connections_.front().SyncTeardown();
32     }
33     ZX_ASSERT_MSG(connections_.is_empty(), "Failed to complete VFS shutdown");
34     if (handler) {
35         handler(ZX_OK);
36     }
37 }
38 
RegisterConnection(fbl::unique_ptr<Connection> connection)39 void SynchronousVfs::RegisterConnection(fbl::unique_ptr<Connection> connection) {
40     ZX_DEBUG_ASSERT(!is_shutting_down_);
41     connections_.push_back(std::move(connection));
42 }
43 
UnregisterConnection(Connection * connection)44 void SynchronousVfs::UnregisterConnection(Connection* connection) {
45     // We drop the result of |erase| on the floor, effectively destroying the
46     // connection.
47     connections_.erase(*connection);
48 }
49 
IsTerminating() const50 bool SynchronousVfs::IsTerminating() const {
51     return is_shutting_down_;
52 }
53 
54 } // namespace fs
55