| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2016, 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 #include "vm/dev_fs.h" |
| 6 |
| 7 #include "vm/hash_table.h" |
| 8 #include "vm/json_stream.h" |
| 9 #include "vm/object.h" |
| 10 |
| 11 namespace dart { |
| 12 |
| 13 class StringMapTraits { |
| 14 public: |
| 15 static bool ReportStats() { return false; } |
| 16 static const char* Name() { return "StringMapTraits"; } |
| 17 |
| 18 static bool IsMatch(const Object& a, const Object& b) { |
| 19 return String::Cast(a).Equals(String::Cast(b)); |
| 20 } |
| 21 |
| 22 static uword Hash(const Object& obj) { |
| 23 return String::Cast(obj).Hash(); |
| 24 } |
| 25 }; |
| 26 |
| 27 |
| 28 Mutex* DevFS::mutex_ = NULL; |
| 29 RawArray* DevFS::fs_map_storage_ = NULL; |
| 30 |
| 31 |
| 32 void DevFS::VisitObjectPointers(ObjectPointerVisitor* visitor) { |
| 33 visitor->VisitPointers(from(), to()); |
| 34 } |
| 35 |
| 36 |
| 37 void DevFS::Init() { |
| 38 mutex_ = new Mutex(); |
| 39 } |
| 40 |
| 41 |
| 42 void DevFS::EnsureStorage() { |
| 43 if (fs_map_storage_ == NULL) { |
| 44 fs_map_storage_ = HashTables::New<UnorderedHashMap<StringMapTraits> >(4); |
| 45 } |
| 46 } |
| 47 |
| 48 |
| 49 void DevFS::ListFileSystems(JSONStream* js) { |
| 50 EnsureStorage(); |
| 51 |
| 52 JSONObject jsobj(js); |
| 53 jsobj.AddProperty("type", "FSList"); |
| 54 JSONArray jsarr(&jsobj, "ids"); |
| 55 |
| 56 String& fs_name = String::Handle(); |
| 57 UnorderedHashMap<StringMapTraits> fs_map(fs_map_storage_); |
| 58 { |
| 59 UnorderedHashMap<StringMapTraits>::Iterator it(&fs_map); |
| 60 while (it.MoveNext()) { |
| 61 const intptr_t entry = it.Current(); |
| 62 fs_name = String::RawCast(fs_map.GetKey(entry)); |
| 63 jsarr.AddValue(fs_name.ToCString()); |
| 64 } |
| 65 } |
| 66 fs_map.Release(); |
| 67 return; |
| 68 } |
| 69 |
| 70 |
| 71 RawArray* DevFS::LookupFileSystem(const String& fs_name) { |
| 72 EnsureStorage(); |
| 73 UnorderedHashMap<StringMapTraits> map(fs_map_storage_); |
| 74 Array& fs = Array::Handle(); |
| 75 fs ^= map.GetOrNull(fs_name); |
| 76 // No need to update storage address because no mutation occurred. |
| 77 map.Release(); |
| 78 return fs.raw(); |
| 79 } |
| 80 |
| 81 |
| 82 bool DevFS::UpdateFileSystem(const String& fs_name, |
| 83 const Array& fs_contents) { |
| 84 UnorderedHashMap<StringMapTraits> map(fs_map_storage_); |
| 85 bool updated = map.UpdateOrInsert(fs_name, fs_contents); |
| 86 fs_map_storage_ = map.Release().raw(); |
| 87 return updated; |
| 88 } |
| 89 |
| 90 |
| 91 void DevFS::CreateFileSystem(JSONStream* js, const char* fs_name) { |
| 92 EnsureStorage(); |
| 93 // TODO(turnidge): Ensure that fs_name is a legal URI host value, i.e. ascii. |
| 94 const String& fs = String::Handle(String::New(fs_name)); |
| 95 if (LookupFileSystem(fs) != Array::null()) { |
| 96 js->PrintError(kFileSystemAlreadyExists, |
| 97 "%s: file system '%s' already exists", |
| 98 js->method(), fs_name); |
| 99 return; |
| 100 } |
| 101 |
| 102 bool update = UpdateFileSystem( |
| 103 fs, |
| 104 Array::Handle(HashTables::New<UnorderedHashMap<StringMapTraits> >(4))); |
| 105 ASSERT(!update); |
| 106 |
| 107 JSONObject jsobj(js); |
| 108 jsobj.AddProperty("type", "Success"); |
| 109 } |
| 110 |
| 111 |
| 112 void DevFS::DeleteFileSystem(JSONStream* js, const char* fs_name) { |
| 113 EnsureStorage(); |
| 114 const String& fs = String::Handle(String::New(fs_name)); |
| 115 if (LookupFileSystem(fs) == Array::null()) { |
| 116 js->PrintError(kFileSystemDoesNotExist, |
| 117 "%s: file system '%s' does not exist", |
| 118 js->method(), fs_name); |
| 119 return; |
| 120 } |
| 121 |
| 122 UnorderedHashMap<StringMapTraits> map(fs_map_storage_); |
| 123 map.Remove(fs); |
| 124 fs_map_storage_ = map.Release().raw(); |
| 125 |
| 126 JSONObject jsobj(js); |
| 127 jsobj.AddProperty("type", "Success"); |
| 128 } |
| 129 |
| 130 |
| 131 void DevFS::WriteFile(JSONStream* js, |
| 132 const char* fs_name, |
| 133 const char* path_param, |
| 134 const char* file_contents) { |
| 135 EnsureStorage(); |
| 136 const String& fs = String::Handle(String::New(fs_name)); |
| 137 const Array& fs_contents = Array::Handle(LookupFileSystem(fs)); |
| 138 if (fs_contents.IsNull()) { |
| 139 js->PrintError(kFileSystemDoesNotExist, |
| 140 "%s: file system '%s' does not exist", |
| 141 js->method(), fs_name); |
| 142 return; |
| 143 } |
| 144 |
| 145 const String& path = String::Handle(String::New(path_param)); |
| 146 UnorderedHashMap<StringMapTraits> map(fs_contents.raw()); |
| 147 map.UpdateOrInsert(path, String::Handle(String::New(file_contents))); |
| 148 UpdateFileSystem(fs, map.Release()); |
| 149 |
| 150 JSONObject jsobj(js); |
| 151 jsobj.AddProperty("type", "Success"); |
| 152 } |
| 153 |
| 154 |
| 155 void DevFS::ReadFile(JSONStream* js, |
| 156 const char* fs_name, |
| 157 const char* path_param) { |
| 158 EnsureStorage(); |
| 159 const String& fs = String::Handle(String::New(fs_name)); |
| 160 const Array& fs_contents = Array::Handle(LookupFileSystem(fs)); |
| 161 if (fs_contents.IsNull()) { |
| 162 js->PrintError(kFileSystemDoesNotExist, |
| 163 "%s: file system '%s' does not exist", |
| 164 js->method(), fs_name); |
| 165 return; |
| 166 } |
| 167 |
| 168 const String& path = String::Handle(String::New(path_param)); |
| 169 UnorderedHashMap<StringMapTraits> map(fs_contents.raw()); |
| 170 String& file_contents = String::Handle(); |
| 171 file_contents ^= map.GetOrNull(path); |
| 172 // No need to update storage address because no mutation occurred. |
| 173 map.Release(); |
| 174 |
| 175 if (file_contents.IsNull()) { |
| 176 js->PrintError(kFileDoesNotExist, |
| 177 "%s: file 'dart-devfs://%s/%s' does not exist", |
| 178 js->method(), fs_name, path_param); |
| 179 return; |
| 180 } |
| 181 |
| 182 JSONObject jsobj(js); |
| 183 jsobj.AddProperty("type", "FSFile"); |
| 184 jsobj.AddProperty("fileContents", file_contents.ToCString()); |
| 185 } |
| 186 |
| 187 } // namespace dart |
| OLD | NEW |