Chromium Code Reviews| Index: runtime/vm/dev_fs.cc |
| diff --git a/runtime/vm/dev_fs.cc b/runtime/vm/dev_fs.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..9cca7804614215807790b043b214822a01ab74d7 |
| --- /dev/null |
| +++ b/runtime/vm/dev_fs.cc |
| @@ -0,0 +1,479 @@ |
| +// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +#include <map> |
| +#include <string> |
| +#include <vector> |
| + |
| +#include "vm/dev_fs.h" |
| + |
| +#include "vm/hash_table.h" |
| +#include "vm/json_stream.h" |
| +#include "vm/lockers.h" |
| +#include "vm/object.h" |
| +#include "vm/unicode.h" |
| + |
| +namespace dart { |
| + |
| +static const uint8_t decode_table[256] = { |
| + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, |
| + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, |
| + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 64, 64, 63, |
| + 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 64, 64, 64, |
| + 64, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, |
| + 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 64, 64, 64, 64, 64, |
| + 64, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, |
| + 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 64, 64, 64, 64, 64, |
| + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, |
| + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, |
| + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, |
| + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, |
| + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, |
| + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, |
| + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, |
| + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 |
| +}; |
| + |
| +class Base64 { |
| + public: |
| + static void decode(const char* base64, |
| + std::vector<uint8_t>* output) { |
| + ASSERT(output != NULL); |
| + ASSERT(base64 != NULL); |
| + const intptr_t base64_len = strlen(base64); |
| + int b[4]; |
| + for (intptr_t i = 0; i < base64_len; i += 4) { |
| + b[0] = decode_table[static_cast<uint8_t>(base64[i])]; |
| + b[1] = decode_table[static_cast<uint8_t>(base64[i + 1])]; |
| + b[2] = decode_table[static_cast<uint8_t>(base64[i + 2])]; |
| + b[3] = decode_table[static_cast<uint8_t>(base64[i + 3])]; |
| + output->push_back((b[0] << 2) | (b[1] >> 4)); |
| + if (b[2] < 64) { |
| + output->push_back((b[1] << 4) | (b[2] >> 2)); |
| + if (b[3] < 64) { |
| + output->push_back((b[2] << 6) | b[3]); |
| + } |
| + } |
| + } |
| + } |
| +}; |
| + |
| + |
| +class FileSystem { |
| + public: |
| + explicit FileSystem(const std::string& name) |
| + : name_(name) { |
| + } |
| + |
| + ~FileSystem() { |
| + } |
| + |
| + bool ReadFile(const std::string& path, |
| + std::vector<uint8_t>** file_contents) { |
| + *file_contents = NULL; |
| + std::map<std::string, std::vector<uint8_t>*>::iterator iter; |
| + iter = files_.find(path); |
| + if (iter == files_.end()) { |
| + return false; |
| + } |
| + *file_contents = iter->second; |
| + return true; |
| + } |
| + |
| + void DeleteFile(const std::string& path) { |
| + std::map<std::string, std::vector<uint8_t>*>::iterator iter; |
| + iter = files_.find(path); |
| + if (iter == files_.end()) { |
| + return; |
| + } |
| + std::vector<uint8_t>* contents = iter->second; |
| + files_.erase(iter); |
| + delete contents; |
| + } |
| + |
| + void WriteFile(const std::string& path, |
| + const char* file_contents) { |
| + DeleteFile(path); |
| + std::vector<uint8_t>* data = new std::vector<uint8_t>(); |
| + Base64::decode(file_contents, data); |
| + files_[path] = data; |
| + } |
| + |
| + void WriteFileRaw(const std::string& path, |
| + const char* file_contents) { |
| + DeleteFile(path); |
| + std::vector<uint8_t>* data = new std::vector<uint8_t>(); |
| + Base64::decode(file_contents, data); |
| + files_[path] = data; |
| + } |
| + |
| + void ListFiles(JSONStream* js) { |
| + JSONObject jsobj(js); |
| + jsobj.AddProperty("type", "FSFilesList"); |
| + JSONArray jsarr(&jsobj, "files"); |
| + std::map<std::string, std::vector<uint8_t>*>::iterator iter; |
| + for (iter = files_.begin(); iter != files_.end(); iter++) { |
| + JSONObject file_info(&jsarr); |
| + file_info.AddProperty("name", iter->first.c_str()); |
| + file_info.AddProperty("size", static_cast<int64_t>(iter->second->size())); |
| + } |
| + } |
| + |
| + private: |
| + std::string name_; |
| + |
| + std::map<std::string, std::vector<uint8_t>*> files_; |
| +}; |
| + |
| +// Some static state is held outside of the DevFS class so that we don't |
| +// have to include stl headers in our vm/ headers. |
| +static std::map<std::string, FileSystem*>* file_systems_; |
| + |
| +Mutex* DevFS::mutex_ = NULL; |
| + |
| + |
| +void DevFS::Init() { |
| + if (mutex_ != NULL) { |
| + // Already initialized. |
| + ASSERT(file_systems_ != NULL); |
| + return; |
| + } |
| + mutex_ = new Mutex(); |
| + file_systems_ = new std::map<std::string, FileSystem*>(); |
| + ASSERT(mutex_ != NULL); |
| + ASSERT(file_systems_ != NULL); |
| +} |
| + |
| + |
| +void DevFS::Cleanup() { |
| + delete mutex_; |
| + mutex_ = NULL; |
| + std::map<std::string, FileSystem*>::iterator iter; |
| + for (iter = file_systems_->begin(); iter != file_systems_->end(); iter++) { |
| + FileSystem* fs = iter->second; |
| + delete fs; |
| + } |
| + delete file_systems_; |
| + file_systems_ = NULL; |
| +} |
| + |
| + |
| +void DevFS::ListFileSystems(JSONStream* js) { |
| + SafepointMutexLocker ml(mutex_); |
| + JSONObject jsobj(js); |
| + jsobj.AddProperty("type", "FSList"); |
| + JSONArray jsarr(&jsobj, "ids"); |
|
turnidge
2016/06/10 18:45:34
Do we want to call this ids still? Maybe fsNames?
|
| + |
| + std::map<std::string, FileSystem*>::iterator iter; |
| + for (iter = file_systems_->begin(); iter != file_systems_->end(); iter++) { |
| + const std::string& key = iter->first; |
| + jsarr.AddValue(key.c_str()); |
| + } |
| +} |
| + |
| + |
| +FileSystem* DevFS::LookupFileSystem(const char* fs_name) { |
| + std::string key = std::string(fs_name); |
| + std::map<std::string, FileSystem*>::iterator iter; |
| + iter = file_systems_->find(key); |
| + if (iter != file_systems_->end()) { |
| + return iter->second; |
| + } |
| + return NULL; |
| +} |
| + |
| + |
| +FileSystem* DevFS::LookupFileSystem(const String& fs_name) { |
| + return LookupFileSystem(fs_name.ToCString()); |
| +} |
| + |
| + |
| +void DevFS::CreateFileSystem(JSONStream* js, const String& fs_name) { |
| + SafepointMutexLocker ml(mutex_); |
| + // TODO(turnidge): Ensure that fs_name is a legal URI host value, i.e. ascii. |
| + if (LookupFileSystem(fs_name) != NULL) { |
| + js->PrintError(kFileSystemAlreadyExists, |
| + "%s: file system '%s' already exists", |
| + js->method(), fs_name.ToCString()); |
| + return; |
| + } |
| + |
| + std::string key = std::string(fs_name.ToCString()); |
| + FileSystem* file_system = new FileSystem(key); |
| + (*file_systems_)[key] = file_system; |
| + |
| + JSONObject jsobj(js); |
| + jsobj.AddProperty("type", "Success"); |
| +} |
| + |
| + |
| +void DevFS::DeleteFileSystem(JSONStream* js, const String& fs_name) { |
| + SafepointMutexLocker ml(mutex_); |
| + FileSystem* file_system = LookupFileSystem(fs_name); |
| + if (file_system == NULL) { |
| + js->PrintError(kFileSystemDoesNotExist, |
| + "%s: file system '%s' does not exist", |
| + js->method(), fs_name.ToCString()); |
| + return; |
| + } |
| + std::string key = std::string(fs_name.ToCString()); |
| + file_systems_->erase(key); |
| + delete file_system; |
| + JSONObject jsobj(js); |
| + jsobj.AddProperty("type", "Success"); |
| +} |
| + |
| + |
| +void DevFS::ListFiles(JSONStream* js, const String& fs_name) { |
| + SafepointMutexLocker ml(mutex_); |
| + FileSystem* file_system = LookupFileSystem(fs_name); |
| + if (file_system == NULL) { |
| + js->PrintError(kFileSystemDoesNotExist, |
| + "%s: file system '%s' does not exist", |
| + js->method(), fs_name.ToCString()); |
| + return; |
| + } |
| + |
| + file_system->ListFiles(js); |
| +} |
| + |
| + |
| +static void PrintWriteFilesError(JSONStream* js, |
| + intptr_t i) { |
| + js->PrintError(kInvalidParams, |
| + "%s: files array invalid at index '%" Pd "'", |
| + js->method(), i); |
| +} |
| + |
| + |
| +void DevFS::WriteFiles(JSONStream* js, |
| + const String& fs_name, |
| + const Array& files) { |
| + SafepointMutexLocker ml(mutex_); |
| + FileSystem* file_system = LookupFileSystem(fs_name); |
| + if (file_system == NULL) { |
| + js->PrintError(kFileSystemDoesNotExist, |
| + "%s: file system '%s' does not exist", |
| + js->method(), fs_name.ToCString()); |
| + return; |
| + } |
| + |
| + Object& test = Object::Handle(); |
| + GrowableObjectArray& file_info = GrowableObjectArray::Handle(); |
| + String& path = String::Handle(); |
| + String& file_contents = String::Handle(); |
| + |
| + // First, validate the array of files is properly formed. |
| + for (intptr_t i = 0; i < files.Length(); i++) { |
| + test = files.At(i); |
| + if (!test.IsGrowableObjectArray()) { |
| + PrintWriteFilesError(js, i); |
| + return; |
| + } |
| + file_info ^= test.raw(); |
| + if (file_info.Length() != 2) { |
| + PrintWriteFilesError(js, i); |
| + return; |
| + } |
| + test = file_info.At(0); |
| + if (!test.IsString()) { |
| + PrintWriteFilesError(js, i); |
| + return; |
| + } |
| + test = file_info.At(1); |
| + if (!test.IsString()) { |
| + PrintWriteFilesError(js, i); |
| + return; |
| + } |
| + } |
| + |
| + // Now atomically update the file system. |
| + for (intptr_t i = 0; i < files.Length(); i++) { |
| + file_info = GrowableObjectArray::RawCast(files.At(i)); |
| + path = String::RawCast(file_info.At(0)); |
| + file_contents = String::RawCast(file_info.At(1)); |
| + file_system->WriteFile(path.ToCString(), |
| + file_contents.ToCString()); |
| + } |
| + |
| + JSONObject jsobj(js); |
| + jsobj.AddProperty("type", "Success"); |
| +} |
| + |
| + |
| +void DevFS::WriteFile(JSONStream* js, |
| + const String& fs_name, |
| + const String& path, |
| + const String& file_contents) { |
| + SafepointMutexLocker ml(mutex_); |
| + FileSystem* file_system = LookupFileSystem(fs_name); |
| + if (file_system == NULL) { |
| + js->PrintError(kFileSystemDoesNotExist, |
| + "%s: file system '%s' does not exist", |
| + js->method(), fs_name.ToCString()); |
| + return; |
| + } |
| + |
| + file_system->WriteFile(path.ToCString(), |
| + file_contents.ToCString()); |
| + |
| + JSONObject jsobj(js); |
| + jsobj.AddProperty("type", "Success"); |
| +} |
| + |
| + |
| +void DevFS::WriteFileRaw(JSONStream* js, |
| + const String& fs_name, |
| + const String& path, |
| + const String& file_contents) { |
| + SafepointMutexLocker ml(mutex_); |
| + FileSystem* file_system = LookupFileSystem(fs_name); |
| + if (file_system == NULL) { |
| + js->PrintError(kFileSystemDoesNotExist, |
| + "%s: file system '%s' does not exist", |
| + js->method(), fs_name.ToCString()); |
| + return; |
| + } |
| + |
| + file_system->WriteFile(path.ToCString(), |
| + file_contents.ToCString()); |
| + |
| + JSONObject jsobj(js); |
| + jsobj.AddProperty("type", "Success"); |
| +} |
| + |
| + |
| +void DevFS::ReadFile(JSONStream* js, |
| + const String& fs_name, |
| + const String& path) { |
| + SafepointMutexLocker ml(mutex_); |
| + FileSystem* file_system = LookupFileSystem(fs_name); |
| + if (file_system == NULL) { |
| + js->PrintError(kFileSystemDoesNotExist, |
| + "%s: file system '%s' does not exist", |
| + js->method(), fs_name.ToCString()); |
| + return; |
| + } |
| + |
| + std::string key = std::string(path.ToCString()); |
| + std::vector<uint8_t>* file_contents; |
| + |
| + bool success = file_system->ReadFile(key, &file_contents); |
| + |
| + if (!success) { |
| + js->PrintError(kFileDoesNotExist, |
| + "%s: file 'dart-devfs://%s/%s' does not exist", |
| + js->method(), fs_name.ToCString(), path.ToCString()); |
| + return; |
| + } |
| + |
| + JSONObject jsobj(js); |
| + jsobj.AddProperty("type", "FSFile"); |
| + jsobj.AddPropertyBase64("fileContents", |
| + &((*file_contents)[0]), |
| + file_contents->size()); |
| +} |
| + |
| +Dart_Handle DevFS::ReadFile(const char* fs_name, |
| + const char* path) { |
| + SafepointMutexLocker ml(mutex_); |
| + Thread* T = Thread::Current(); |
| + FileSystem* file_system = LookupFileSystem(fs_name); |
| + if (file_system == NULL) { |
| + return Api::NewError("File system '%s' does not exist", fs_name); |
| + } |
| + |
| + std::string key = std::string(path); |
| + std::vector<uint8_t>* file_contents; |
| + |
| + bool success = file_system->ReadFile(key, &file_contents); |
| + |
| + if (!success) { |
| + return Api::NewError("File '%s' does not exist", path); |
| + } |
| + |
| + CHECK_CALLBACK_STATE(T); |
| + |
| + const TypedData& typed_data = |
| + TypedData::Handle(TypedData::New(kTypedDataUint8ArrayCid, |
| + file_contents->size())); |
| + |
| + { |
| + NoSafepointScope scope; |
| + uint8_t* destination = reinterpret_cast<uint8_t*>(typed_data.DataAddr(0)); |
| + memmove(destination, &((*file_contents)[0]), file_contents->size()); |
| + } |
| + |
| + return Api::NewHandle(T, typed_data.raw()); |
| +} |
| + |
| + |
| +Dart_Handle DevFS::ReadFileAsString(const char* fs_name, |
| + const char* path) { |
| + SafepointMutexLocker ml(mutex_); |
| + Thread* T = Thread::Current(); |
| + FileSystem* file_system = LookupFileSystem(fs_name); |
| + if (file_system == NULL) { |
| + return Api::NewError("File system '%s' does not exist", fs_name); |
| + } |
| + |
| + std::string key = std::string(path); |
| + std::vector<uint8_t>* file_contents; |
| + |
| + bool success = file_system->ReadFile(key, &file_contents); |
| + |
| + if (!success) { |
| + return Api::NewError("File '%s' does not exist", path); |
| + } |
| + |
| + uint8_t* data = &((*file_contents)[0]); |
| + int64_t length = file_contents->size(); |
| + CHECK_LENGTH(length, String::kMaxElements); |
| + if (!Utf8::IsValid(&((*file_contents)[0]), file_contents->size())) { |
| + return Api::NewError("File '%s' does not contain a valid UTF-8 string", |
| + path); |
| + } |
| + CHECK_CALLBACK_STATE(T); |
| + return Api::NewHandle(T, String::FromUTF8(data, length)); |
| +} |
| + |
| + |
| +Dart_Handle LibraryTagHandler(Dart_LibraryTag tag, |
| + Dart_Handle library, |
| + Dart_Handle url) { |
| + if (tag == Dart_kCanonicalizeUrl) { |
| + return Dart_DefaultCanonicalizeUrl(library, url); |
| + } |
| + |
| + Dart_Handle source = Dart_DevFSReadFileAsUTF8String(url); |
| + |
| + if (Dart_IsError(source)) { |
| + return source; |
| + } |
| + |
| + // Lookup the library's url. |
| + Dart_Handle library_url = Dart_LibraryUrl(library); |
| + |
| + switch (tag) { |
| + case Dart_kImportTag: |
| + return Dart_LoadLibrary(url, source, 0, 0); |
| + break; |
| + case Dart_kSourceTag: { |
| + Dart_Handle library = Dart_LookupLibrary(library_url); |
| + if (Dart_IsError(library)) { |
| + return library; |
| + } |
| + return Dart_LoadSource(library, url, source, 0, 0); |
| + } |
| + break; |
| + case Dart_kScriptTag: |
| + return Dart_LoadScript(url, source, 0, 0); |
| + break; |
| + default: |
| + UNREACHABLE(); |
| + return Dart_Null(); |
| + } |
| +} |
| + |
| +} // namespace dart |