1 // Copyright 2016 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/vnode.h>
6 
7 #ifdef __Fuchsia__
8 #include <fs/connection.h>
9 
10 #include <utility>
11 #endif
12 
13 namespace fs {
14 
15 Vnode::Vnode() = default;
16 
17 Vnode::~Vnode() = default;
18 
19 #ifdef __Fuchsia__
Serve(fs::Vfs * vfs,zx::channel channel,uint32_t flags)20 zx_status_t Vnode::Serve(fs::Vfs* vfs, zx::channel channel, uint32_t flags) {
21     return vfs->ServeConnection(fbl::make_unique<Connection>(
22         vfs, fbl::WrapRefPtr(this), std::move(channel), flags));
23 }
24 
GetHandles(uint32_t flags,fuchsia_io_NodeInfo * info)25 zx_status_t Vnode::GetHandles(uint32_t flags, fuchsia_io_NodeInfo* info) {
26     info->tag = fuchsia_io_NodeInfoTag_service;
27     return ZX_OK;
28 }
29 
WatchDir(Vfs * vfs,uint32_t mask,uint32_t options,zx::channel watcher)30 zx_status_t Vnode::WatchDir(Vfs* vfs, uint32_t mask, uint32_t options, zx::channel watcher) {
31     return ZX_ERR_NOT_SUPPORTED;
32 }
33 #endif
34 
Notify(fbl::StringPiece name,unsigned event)35 void Vnode::Notify(fbl::StringPiece name, unsigned event) {}
36 
ValidateFlags(uint32_t flags)37 zx_status_t Vnode::ValidateFlags(uint32_t flags) {
38     return ZX_OK;
39 }
40 
Open(uint32_t flags,fbl::RefPtr<Vnode> * out_redirect)41 zx_status_t Vnode::Open(uint32_t flags, fbl::RefPtr<Vnode>* out_redirect) {
42     return ZX_OK;
43 }
44 
Close()45 zx_status_t Vnode::Close() {
46     return ZX_OK;
47 }
48 
Read(void * data,size_t len,size_t off,size_t * out_actual)49 zx_status_t Vnode::Read(void* data, size_t len, size_t off, size_t* out_actual) {
50     return ZX_ERR_NOT_SUPPORTED;
51 }
52 
Write(const void * data,size_t len,size_t offset,size_t * out_actual)53 zx_status_t Vnode::Write(const void* data, size_t len, size_t offset, size_t* out_actual) {
54     return ZX_ERR_NOT_SUPPORTED;
55 }
56 
Append(const void * data,size_t len,size_t * out_end,size_t * out_actual)57 zx_status_t Vnode::Append(const void* data, size_t len, size_t* out_end, size_t* out_actual) {
58     return ZX_ERR_NOT_SUPPORTED;
59 }
60 
Lookup(fbl::RefPtr<Vnode> * out,fbl::StringPiece name)61 zx_status_t Vnode::Lookup(fbl::RefPtr<Vnode>* out, fbl::StringPiece name) {
62     return ZX_ERR_NOT_SUPPORTED;
63 }
64 
Getattr(vnattr_t * a)65 zx_status_t Vnode::Getattr(vnattr_t* a) {
66     return ZX_ERR_NOT_SUPPORTED;
67 }
68 
Setattr(const vnattr_t * a)69 zx_status_t Vnode::Setattr(const vnattr_t* a) {
70     return ZX_ERR_NOT_SUPPORTED;
71 }
72 
Readdir(vdircookie_t * cookie,void * dirents,size_t len,size_t * out_actual)73 zx_status_t Vnode::Readdir(vdircookie_t* cookie, void* dirents, size_t len, size_t* out_actual) {
74     return ZX_ERR_NOT_SUPPORTED;
75 }
76 
Create(fbl::RefPtr<Vnode> * out,fbl::StringPiece name,uint32_t mode)77 zx_status_t Vnode::Create(fbl::RefPtr<Vnode>* out, fbl::StringPiece name, uint32_t mode) {
78     return ZX_ERR_NOT_SUPPORTED;
79 }
80 
Unlink(fbl::StringPiece name,bool must_be_dir)81 zx_status_t Vnode::Unlink(fbl::StringPiece name, bool must_be_dir) {
82     return ZX_ERR_NOT_SUPPORTED;
83 }
84 
Truncate(size_t len)85 zx_status_t Vnode::Truncate(size_t len) {
86     return ZX_ERR_NOT_SUPPORTED;
87 }
88 
Rename(fbl::RefPtr<Vnode> newdir,fbl::StringPiece oldname,fbl::StringPiece newname,bool src_must_be_dir,bool dst_must_be_dir)89 zx_status_t Vnode::Rename(fbl::RefPtr<Vnode> newdir, fbl::StringPiece oldname,
90                           fbl::StringPiece newname, bool src_must_be_dir,
91                           bool dst_must_be_dir) {
92     return ZX_ERR_NOT_SUPPORTED;
93 }
94 
Link(fbl::StringPiece name,fbl::RefPtr<Vnode> target)95 zx_status_t Vnode::Link(fbl::StringPiece name, fbl::RefPtr<Vnode> target) {
96     return ZX_ERR_NOT_SUPPORTED;
97 }
98 
GetVmo(int flags,zx_handle_t * out)99 zx_status_t Vnode::GetVmo(int flags, zx_handle_t* out) {
100     return ZX_ERR_NOT_SUPPORTED;
101 }
102 
Sync(SyncCallback closure)103 void Vnode::Sync(SyncCallback closure) {
104     closure(ZX_ERR_NOT_SUPPORTED);
105 }
106 
107 #ifdef __Fuchsia__
108 
QueryFilesystem(fuchsia_io_FilesystemInfo * out)109 zx_status_t Vnode::QueryFilesystem(fuchsia_io_FilesystemInfo* out) {
110     return ZX_ERR_NOT_SUPPORTED;
111 }
112 
GetDevicePath(size_t buffer_len,char * out_name,size_t * out_len)113 zx_status_t Vnode::GetDevicePath(size_t buffer_len, char* out_name, size_t* out_len) {
114     return ZX_ERR_NOT_SUPPORTED;
115 }
116 
AttachRemote(MountChannel h)117 zx_status_t Vnode::AttachRemote(MountChannel h) {
118     return ZX_ERR_NOT_SUPPORTED;
119 }
120 
IsRemote() const121 bool Vnode::IsRemote() const {
122     return false;
123 }
124 
DetachRemote()125 zx::channel Vnode::DetachRemote() {
126     return zx::channel();
127 }
128 
GetRemote() const129 zx_handle_t Vnode::GetRemote() const {
130     return ZX_HANDLE_INVALID;
131 }
132 
SetRemote(zx::channel remote)133 void Vnode::SetRemote(zx::channel remote) {
134     ZX_DEBUG_ASSERT(false);
135 }
136 #endif
137 
DirentFiller(void * ptr,size_t len)138 DirentFiller::DirentFiller(void* ptr, size_t len)
139     : ptr_(static_cast<char*>(ptr)), pos_(0), len_(len) {}
140 
Next(fbl::StringPiece name,uint8_t type,uint64_t ino)141 zx_status_t DirentFiller::Next(fbl::StringPiece name, uint8_t type, uint64_t ino) {
142     vdirent_t* de = reinterpret_cast<vdirent_t*>(ptr_ + pos_);
143     size_t sz = sizeof(vdirent_t) + name.length();
144 
145     if (sz > len_ - pos_ || name.length() > NAME_MAX) {
146         return ZX_ERR_INVALID_ARGS;
147     }
148     de->ino = ino;
149     de->size = static_cast<uint8_t>(name.length());
150     de->type = type;
151     memcpy(de->name, name.data(), name.length());
152     pos_ += sz;
153     return ZX_OK;
154 }
155 
156 } // namespace fs
157