Flutter Engine
The Flutter Engine
directory.h
Go to the documentation of this file.
1// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2// for details. All rights reserved. Use of this source code is governed by a
3// BSD-style license that can be found in the LICENSE file.
4
5#ifndef RUNTIME_BIN_DIRECTORY_H_
6#define RUNTIME_BIN_DIRECTORY_H_
7
8#include "bin/builtin.h"
9#include "bin/dartutils.h"
10#include "bin/namespace.h"
12#include "bin/thread.h"
13#include "platform/globals.h"
14
15namespace dart {
16namespace bin {
17
23 kListDone = 4
24};
25
27 public:
30
31 bool Add(const char* name);
32 bool AddW(const wchar_t* name);
33
34 char* AsString() const;
35 wchar_t* AsStringW() const;
36
37 // Makes a scope allocated copy of the string.
38 const char* AsScopedString() const;
39
40 void Reset(intptr_t new_length);
41
42 intptr_t length() const { return length_; }
43
44 private:
45 void* data_;
46 intptr_t length_;
47
48 DISALLOW_COPY_AND_ASSIGN(PathBuffer);
49};
50
51class DirectoryListing;
52
53struct LinkList;
54
55// DirectoryListingEntry is used as a stack item, when performing recursive
56// directory listing. By using DirectoryListingEntry as stack elements, a
57// directory listing can be paused e.g. when a buffer is full, and resumed
58// later on.
59//
60// The stack is managed by the DirectoryListing's PathBuffer. Each
61// DirectoryListingEntry stored a entry-length, that it'll reset the PathBuffer
62// to on each call to Next.
64 public:
66 : parent_(parent), fd_(-1), lister_(0), done_(false), link_(nullptr) {}
67
69
71
72 DirectoryListingEntry* parent() const { return parent_; }
73
74 LinkList* link() { return link_; }
75
76 void set_link(LinkList* link) { link_ = link; }
77
78 void ResetLink();
79
80 private:
81 DirectoryListingEntry* parent_;
82 intptr_t fd_;
83 intptr_t lister_;
84 bool done_;
85 int path_length_;
86 LinkList* link_;
87
88 DISALLOW_COPY_AND_ASSIGN(DirectoryListingEntry);
89};
90
92 public:
94 const char* dir_name,
95 bool recursive,
96 bool follow_links)
97 : namespc_(namespc),
98 top_(nullptr),
99 error_(false),
100 recursive_(recursive),
101 follow_links_(follow_links) {
102 if (!path_buffer_.Add(dir_name)) {
103 error_ = true;
104 }
105 Push(new DirectoryListingEntry(nullptr));
106 }
107
108 virtual ~DirectoryListing() { PopAll(); }
109
110 virtual bool HandleDirectory(const char* dir_name) = 0;
111 virtual bool HandleFile(const char* file_name) = 0;
112 virtual bool HandleLink(const char* link_name) = 0;
113 virtual bool HandleError() = 0;
114 virtual void HandleDone() {}
115
116 void Push(DirectoryListingEntry* directory) { top_ = directory; }
117
118 void Pop() {
119 ASSERT(!IsEmpty());
120 DirectoryListingEntry* current = top_;
121 top_ = top_->parent();
122 delete current;
123 }
124
125 bool IsEmpty() const { return top_ == nullptr; }
126
127 void PopAll() {
128 while (!IsEmpty()) {
129 Pop();
130 }
131 }
132
133 Namespace* namespc() const { return namespc_; }
134
135 DirectoryListingEntry* top() const { return top_; }
136
137 bool recursive() const { return recursive_; }
138
139 bool follow_links() const { return follow_links_; }
140
141 const char* CurrentPath() { return path_buffer_.AsScopedString(); }
142
143 PathBuffer& path_buffer() { return path_buffer_; }
144
145 bool error() const { return error_; }
146
147 private:
148 PathBuffer path_buffer_;
149 Namespace* namespc_;
151 bool error_;
152 bool recursive_;
153 bool follow_links_;
154};
155
156class AsyncDirectoryListing : public ReferenceCounted<AsyncDirectoryListing>,
157 public DirectoryListing {
158 public:
159 enum Response {
164 kListDone = 4
165 };
166
168 const char* dir_name,
169 bool recursive,
170 bool follow_links)
173 array_(nullptr),
174 index_(0),
175 length_(0) {}
176
177 virtual bool HandleDirectory(const char* dir_name);
178 virtual bool HandleFile(const char* file_name);
179 virtual bool HandleLink(const char* file_name);
180 virtual bool HandleError();
181 virtual void HandleDone();
182
183 void SetArray(CObjectArray* array, intptr_t length) {
184 ASSERT(length % 2 == 0);
185 array_ = array;
186 index_ = 0;
187 length_ = length;
188 }
189
190 intptr_t index() const { return index_; }
191
192 private:
194 bool AddFileSystemEntityToResponse(Response response, const char* arg);
195 CObjectArray* array_;
196 intptr_t index_;
197 intptr_t length_;
198
201};
202
204 public:
207 const char* dir_name,
208 bool recursive,
209 bool follow_links)
211 results_(results),
212 dart_error_(Dart_Null()) {
213 add_string_ = DartUtils::NewString("add");
214 from_raw_path_string_ = DartUtils::NewString("fromRawPath");
215 directory_type_ = DartUtils::GetDartType(DartUtils::kIOLibURL, "Directory");
216 file_type_ = DartUtils::GetDartType(DartUtils::kIOLibURL, "File");
217 link_type_ = DartUtils::GetDartType(DartUtils::kIOLibURL, "Link");
218 }
220 virtual bool HandleDirectory(const char* dir_name);
221 virtual bool HandleFile(const char* file_name);
222 virtual bool HandleLink(const char* file_name);
223 virtual bool HandleError();
224
225 Dart_Handle dart_error() { return dart_error_; }
226
227 private:
228 Dart_Handle results_;
229 Dart_Handle add_string_;
230 Dart_Handle from_raw_path_string_;
231 Dart_Handle directory_type_;
232 Dart_Handle file_type_;
233 Dart_Handle link_type_;
234 Dart_Handle dart_error_;
235
237 DISALLOW_IMPLICIT_CONSTRUCTORS(SyncDirectoryListing);
238};
239
241 public:
242 enum ExistsResult { UNKNOWN, EXISTS, DOES_NOT_EXIST };
243
244 static void List(DirectoryListing* listing);
245
246#if defined(DART_HOST_OS_WINDOWS)
247 static ExistsResult Exists(const wchar_t* path);
248#endif
249
250 static ExistsResult Exists(Namespace* namespc, const char* path);
251
252 // Returns the current working directory. The caller must call
253 // free() on the result.
254 static char* CurrentNoScope();
255
256 // Returns the current working directory. The returned string is allocated
257 // with Dart_ScopeAllocate(). It lasts only as long as the current API scope.
258 static const char* Current(Namespace* namespc);
259 static const char* SystemTemp(Namespace* namespc);
260 static const char* CreateTemp(Namespace* namespc, const char* path);
261 // Set the system temporary directory.
262 static void SetSystemTemp(const char* path);
263 static bool SetCurrent(Namespace* namespc, const char* path);
264 static bool Create(Namespace* namespc, const char* path);
265 static bool Delete(Namespace* namespc, const char* path, bool recursive);
267 const char* path,
268 const char* new_path);
269
270 static CObject* CreateRequest(const CObjectArray& request);
271 static CObject* DeleteRequest(const CObjectArray& request);
272 static CObject* ExistsRequest(const CObjectArray& request);
273 static CObject* CreateTempRequest(const CObjectArray& request);
275 static CObject* ListStartRequest(const CObjectArray& request);
276 static CObject* ListNextRequest(const CObjectArray& request);
277 static CObject* ListStopRequest(const CObjectArray& request);
278 static CObject* RenameRequest(const CObjectArray& request);
279
280 private:
281 static char* system_temp_path_override_;
283 DISALLOW_IMPLICIT_CONSTRUCTORS(Directory);
284};
285
286} // namespace bin
287} // namespace dart
288
289#endif // RUNTIME_BIN_DIRECTORY_H_
SkIDChangeListener::List List
void SetArray(CObjectArray *array, intptr_t length)
Definition: directory.h:183
virtual bool HandleDirectory(const char *dir_name)
Definition: directory.cc:397
virtual bool HandleLink(const char *file_name)
Definition: directory.cc:405
AsyncDirectoryListing(Namespace *namespc, const char *dir_name, bool recursive, bool follow_links)
Definition: directory.h:167
virtual bool HandleFile(const char *file_name)
Definition: directory.cc:401
static constexpr const char * kIOLibURL
Definition: dartutils.h:297
static Dart_Handle NewString(const char *str)
Definition: dartutils.h:214
static Dart_Handle GetDartType(const char *library_url, const char *class_name)
Definition: dartutils.cc:696
ListType Next(DirectoryListing *listing)
void set_link(LinkList *link)
Definition: directory.h:76
DirectoryListingEntry(DirectoryListingEntry *parent)
Definition: directory.h:65
DirectoryListingEntry * parent() const
Definition: directory.h:72
const char * CurrentPath()
Definition: directory.h:141
PathBuffer & path_buffer()
Definition: directory.h:143
DirectoryListingEntry * top() const
Definition: directory.h:135
void Push(DirectoryListingEntry *directory)
Definition: directory.h:116
virtual bool HandleFile(const char *file_name)=0
virtual void HandleDone()
Definition: directory.h:114
virtual bool HandleDirectory(const char *dir_name)=0
virtual bool HandleLink(const char *link_name)=0
Namespace * namespc() const
Definition: directory.h:133
DirectoryListing(Namespace *namespc, const char *dir_name, bool recursive, bool follow_links)
Definition: directory.h:93
virtual bool HandleError()=0
static bool Create(Namespace *namespc, const char *path)
static ExistsResult Exists(Namespace *namespc, const char *path)
static char * CurrentNoScope()
static bool Rename(Namespace *namespc, const char *path, const char *new_path)
static bool Delete(Namespace *namespc, const char *path, bool recursive)
static const char * CreateTemp(Namespace *namespc, const char *path)
static const char * SystemTemp(Namespace *namespc)
static CObject * CreateSystemTempRequest(const CObjectArray &request)
const char * AsScopedString() const
wchar_t * AsStringW() const
void Reset(intptr_t new_length)
bool AddW(const wchar_t *name)
char * AsString() const
intptr_t length() const
Definition: directory.h:42
bool Add(const char *name)
virtual bool HandleFile(const char *file_name)
Definition: directory.cc:478
SyncDirectoryListing(Dart_Handle results, Namespace *namespc, const char *dir_name, bool recursive, bool follow_links)
Definition: directory.h:205
virtual bool HandleLink(const char *file_name)
Definition: directory.cc:453
virtual bool HandleDirectory(const char *dir_name)
Definition: directory.cc:427
struct _Dart_Handle * Dart_Handle
Definition: dart_api.h:258
#define ASSERT(E)
size_t length
@ kListDone
Definition: directory.h:23
@ kListFile
Definition: directory.h:19
@ kListDirectory
Definition: directory.h:20
@ kListError
Definition: directory.h:22
@ kListLink
Definition: directory.h:21
Definition: dart_vm.cc:33
const char *const name
DART_EXPORT Dart_Handle Dart_Null()
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir path
Definition: switches.h:57
#define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName)
Definition: globals.h:593
#define DISALLOW_ALLOCATION()
Definition: globals.h:604