michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim: set ts=2 et sw=2 tw=80: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "mozilla/dom/Directory.h" michael@0: michael@0: #include "CreateDirectoryTask.h" michael@0: #include "CreateFileTask.h" michael@0: #include "FileSystemPermissionRequest.h" michael@0: #include "GetFileOrDirectoryTask.h" michael@0: #include "RemoveTask.h" michael@0: michael@0: #include "nsCharSeparatedTokenizer.h" michael@0: #include "nsString.h" michael@0: #include "mozilla/dom/DirectoryBinding.h" michael@0: #include "mozilla/dom/FileSystemBase.h" michael@0: #include "mozilla/dom/FileSystemUtils.h" michael@0: #include "mozilla/dom/UnionTypes.h" michael@0: michael@0: // Resolve the name collision of Microsoft's API name with macros defined in michael@0: // Windows header files. Undefine the macro of CreateDirectory to avoid michael@0: // Directory#CreateDirectory being replaced by Directory#CreateDirectoryW. michael@0: #ifdef CreateDirectory michael@0: #undef CreateDirectory michael@0: #endif michael@0: // Undefine the macro of CreateFile to avoid Directory#CreateFile being replaced michael@0: // by Directory#CreateFileW. michael@0: #ifdef CreateFile michael@0: #undef CreateFile michael@0: #endif michael@0: michael@0: namespace mozilla { michael@0: namespace dom { michael@0: michael@0: NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_0(Directory) michael@0: NS_IMPL_CYCLE_COLLECTING_ADDREF(Directory) michael@0: NS_IMPL_CYCLE_COLLECTING_RELEASE(Directory) michael@0: NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(Directory) michael@0: NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY michael@0: NS_INTERFACE_MAP_ENTRY(nsISupports) michael@0: NS_INTERFACE_MAP_END michael@0: michael@0: // static michael@0: already_AddRefed michael@0: Directory::GetRoot(FileSystemBase* aFileSystem) michael@0: { michael@0: nsRefPtr task = new GetFileOrDirectoryTask( michael@0: aFileSystem, EmptyString(), true); michael@0: FileSystemPermissionRequest::RequestForTask(task); michael@0: return task->GetPromise(); michael@0: } michael@0: michael@0: Directory::Directory(FileSystemBase* aFileSystem, michael@0: const nsAString& aPath) michael@0: : mFileSystem(aFileSystem) michael@0: , mPath(aPath) michael@0: { michael@0: MOZ_ASSERT(aFileSystem, "aFileSystem should not be null."); michael@0: // Remove the trailing "/". michael@0: mPath.Trim(FILESYSTEM_DOM_PATH_SEPARATOR, false, true); michael@0: michael@0: SetIsDOMBinding(); michael@0: } michael@0: michael@0: Directory::~Directory() michael@0: { michael@0: } michael@0: michael@0: nsPIDOMWindow* michael@0: Directory::GetParentObject() const michael@0: { michael@0: return mFileSystem->GetWindow(); michael@0: } michael@0: michael@0: JSObject* michael@0: Directory::WrapObject(JSContext* aCx) michael@0: { michael@0: return DirectoryBinding::Wrap(aCx, this); michael@0: } michael@0: michael@0: void michael@0: Directory::GetName(nsString& aRetval) const michael@0: { michael@0: aRetval.Truncate(); michael@0: michael@0: if (mPath.IsEmpty()) { michael@0: aRetval = mFileSystem->GetRootName(); michael@0: return; michael@0: } michael@0: michael@0: aRetval = Substring(mPath, michael@0: mPath.RFindChar(FileSystemUtils::kSeparatorChar) + 1); michael@0: } michael@0: michael@0: already_AddRefed michael@0: Directory::CreateFile(const nsAString& aPath, const CreateFileOptions& aOptions) michael@0: { michael@0: nsresult error = NS_OK; michael@0: nsString realPath; michael@0: nsRefPtr blobData; michael@0: InfallibleTArray arrayData; michael@0: bool replace = (aOptions.mIfExists == CreateIfExistsMode::Replace); michael@0: michael@0: // Get the file content. michael@0: if (aOptions.mData.WasPassed()) { michael@0: auto& data = aOptions.mData.Value(); michael@0: if (data.IsString()) { michael@0: NS_ConvertUTF16toUTF8 str(data.GetAsString()); michael@0: arrayData.AppendElements(reinterpret_cast(str.get()), michael@0: str.Length()); michael@0: } else if (data.IsArrayBuffer()) { michael@0: ArrayBuffer& buffer = data.GetAsArrayBuffer(); michael@0: buffer.ComputeLengthAndData(); michael@0: arrayData.AppendElements(buffer.Data(), buffer.Length()); michael@0: } else if (data.IsArrayBufferView()){ michael@0: ArrayBufferView& view = data.GetAsArrayBufferView(); michael@0: view.ComputeLengthAndData(); michael@0: arrayData.AppendElements(view.Data(), view.Length()); michael@0: } else { michael@0: blobData = data.GetAsBlob(); michael@0: } michael@0: } michael@0: michael@0: if (!DOMPathToRealPath(aPath, realPath)) { michael@0: error = NS_ERROR_DOM_FILESYSTEM_INVALID_PATH_ERR; michael@0: } michael@0: michael@0: nsRefPtr task = new CreateFileTask(mFileSystem, realPath, michael@0: blobData, arrayData, replace); michael@0: task->SetError(error); michael@0: FileSystemPermissionRequest::RequestForTask(task); michael@0: return task->GetPromise(); michael@0: } michael@0: michael@0: already_AddRefed michael@0: Directory::CreateDirectory(const nsAString& aPath) michael@0: { michael@0: nsresult error = NS_OK; michael@0: nsString realPath; michael@0: if (!DOMPathToRealPath(aPath, realPath)) { michael@0: error = NS_ERROR_DOM_FILESYSTEM_INVALID_PATH_ERR; michael@0: } michael@0: nsRefPtr task = new CreateDirectoryTask( michael@0: mFileSystem, realPath); michael@0: task->SetError(error); michael@0: FileSystemPermissionRequest::RequestForTask(task); michael@0: return task->GetPromise(); michael@0: } michael@0: michael@0: already_AddRefed michael@0: Directory::Get(const nsAString& aPath) michael@0: { michael@0: nsresult error = NS_OK; michael@0: nsString realPath; michael@0: if (!DOMPathToRealPath(aPath, realPath)) { michael@0: error = NS_ERROR_DOM_FILESYSTEM_INVALID_PATH_ERR; michael@0: } michael@0: nsRefPtr task = new GetFileOrDirectoryTask( michael@0: mFileSystem, realPath, false); michael@0: task->SetError(error); michael@0: FileSystemPermissionRequest::RequestForTask(task); michael@0: return task->GetPromise(); michael@0: } michael@0: michael@0: already_AddRefed michael@0: Directory::Remove(const StringOrFileOrDirectory& aPath) michael@0: { michael@0: return RemoveInternal(aPath, false); michael@0: } michael@0: michael@0: already_AddRefed michael@0: Directory::RemoveDeep(const StringOrFileOrDirectory& aPath) michael@0: { michael@0: return RemoveInternal(aPath, true); michael@0: } michael@0: michael@0: already_AddRefed michael@0: Directory::RemoveInternal(const StringOrFileOrDirectory& aPath, bool aRecursive) michael@0: { michael@0: nsresult error = NS_OK; michael@0: nsString realPath; michael@0: nsCOMPtr file; michael@0: michael@0: // Check and get the target path. michael@0: michael@0: if (aPath.IsFile()) { michael@0: file = aPath.GetAsFile(); michael@0: goto parameters_check_done; michael@0: } michael@0: michael@0: if (aPath.IsString()) { michael@0: if (!DOMPathToRealPath(aPath.GetAsString(), realPath)) { michael@0: error = NS_ERROR_DOM_FILESYSTEM_INVALID_PATH_ERR; michael@0: } michael@0: goto parameters_check_done; michael@0: } michael@0: michael@0: if (!mFileSystem->IsSafeDirectory(&aPath.GetAsDirectory())) { michael@0: error = NS_ERROR_DOM_SECURITY_ERR; michael@0: goto parameters_check_done; michael@0: } michael@0: michael@0: realPath = aPath.GetAsDirectory().mPath; michael@0: // The target must be a descendant of this directory. michael@0: if (!FileSystemUtils::IsDescendantPath(mPath, realPath)) { michael@0: error = NS_ERROR_DOM_FILESYSTEM_NO_MODIFICATION_ALLOWED_ERR; michael@0: } michael@0: michael@0: parameters_check_done: michael@0: michael@0: nsRefPtr task = new RemoveTask(mFileSystem, mPath, file, realPath, michael@0: aRecursive); michael@0: task->SetError(error); michael@0: FileSystemPermissionRequest::RequestForTask(task); michael@0: return task->GetPromise(); michael@0: } michael@0: michael@0: FileSystemBase* michael@0: Directory::GetFileSystem() const michael@0: { michael@0: return mFileSystem.get(); michael@0: } michael@0: michael@0: bool michael@0: Directory::DOMPathToRealPath(const nsAString& aPath, nsAString& aRealPath) const michael@0: { michael@0: aRealPath.Truncate(); michael@0: michael@0: nsString relativePath; michael@0: relativePath = aPath; michael@0: michael@0: // Trim white spaces. michael@0: static const char kWhitespace[] = "\b\t\r\n "; michael@0: relativePath.Trim(kWhitespace); michael@0: michael@0: if (!IsValidRelativePath(relativePath)) { michael@0: return false; michael@0: } michael@0: michael@0: aRealPath = mPath + NS_LITERAL_STRING(FILESYSTEM_DOM_PATH_SEPARATOR) + michael@0: relativePath; michael@0: michael@0: return true; michael@0: } michael@0: michael@0: // static michael@0: bool michael@0: Directory::IsValidRelativePath(const nsString& aPath) michael@0: { michael@0: // We don't allow empty relative path to access the root. michael@0: if (aPath.IsEmpty()) { michael@0: return false; michael@0: } michael@0: michael@0: // Leading and trailing "/" are not allowed. michael@0: if (aPath.First() == FileSystemUtils::kSeparatorChar || michael@0: aPath.Last() == FileSystemUtils::kSeparatorChar) { michael@0: return false; michael@0: } michael@0: michael@0: NS_NAMED_LITERAL_STRING(kCurrentDir, "."); michael@0: NS_NAMED_LITERAL_STRING(kParentDir, ".."); michael@0: michael@0: // Split path and check each path component. michael@0: nsCharSeparatedTokenizer tokenizer(aPath, FileSystemUtils::kSeparatorChar); michael@0: while (tokenizer.hasMoreTokens()) { michael@0: nsDependentSubstring pathComponent = tokenizer.nextToken(); michael@0: // The path containing empty components, such as "foo//bar", is invalid. michael@0: // We don't allow paths, such as "../foo", "foo/./bar" and "foo/../bar", michael@0: // to walk up the directory. michael@0: if (pathComponent.IsEmpty() || michael@0: pathComponent.Equals(kCurrentDir) || michael@0: pathComponent.Equals(kParentDir)) { michael@0: return false; michael@0: } michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: } // namespace dom michael@0: } // namespace mozilla