2014-03-05 07:26:39 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2015-05-03 22:32:37 +03:00
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2014-03-05 07:26:39 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "mozilla/dom/Directory.h"
|
|
|
|
|
2014-03-05 07:25:40 +04:00
|
|
|
#include "CreateDirectoryTask.h"
|
2014-03-05 12:40:48 +04:00
|
|
|
#include "CreateFileTask.h"
|
2014-03-05 07:24:19 +04:00
|
|
|
#include "FileSystemPermissionRequest.h"
|
2015-06-23 02:31:34 +03:00
|
|
|
#include "GetDirectoryListingTask.h"
|
2014-03-05 07:25:40 +04:00
|
|
|
#include "GetFileOrDirectoryTask.h"
|
2014-03-12 10:30:21 +04:00
|
|
|
#include "RemoveTask.h"
|
2014-03-05 07:25:40 +04:00
|
|
|
|
|
|
|
#include "nsCharSeparatedTokenizer.h"
|
2014-03-05 07:26:39 +04:00
|
|
|
#include "nsString.h"
|
|
|
|
#include "mozilla/dom/DirectoryBinding.h"
|
2014-03-05 07:25:40 +04:00
|
|
|
#include "mozilla/dom/FileSystemBase.h"
|
|
|
|
#include "mozilla/dom/FileSystemUtils.h"
|
2016-03-20 13:56:10 +03:00
|
|
|
#include "mozilla/dom/OSFileSystem.h"
|
2014-03-05 07:26:39 +04:00
|
|
|
|
|
|
|
// Resolve the name collision of Microsoft's API name with macros defined in
|
|
|
|
// Windows header files. Undefine the macro of CreateDirectory to avoid
|
|
|
|
// Directory#CreateDirectory being replaced by Directory#CreateDirectoryW.
|
|
|
|
#ifdef CreateDirectory
|
|
|
|
#undef CreateDirectory
|
|
|
|
#endif
|
2014-03-05 12:40:48 +04:00
|
|
|
// Undefine the macro of CreateFile to avoid Directory#CreateFile being replaced
|
|
|
|
// by Directory#CreateFileW.
|
|
|
|
#ifdef CreateFile
|
|
|
|
#undef CreateFile
|
|
|
|
#endif
|
2014-03-05 07:26:39 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2016-03-20 13:56:10 +03:00
|
|
|
namespace {
|
|
|
|
|
2016-03-20 13:56:10 +03:00
|
|
|
bool
|
|
|
|
TokenizerIgnoreNothing(char16_t /* aChar */)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-03-20 13:56:10 +03:00
|
|
|
bool
|
|
|
|
IsValidRelativeDOMPath(const nsString& aPath, nsTArray<nsString>& aParts)
|
|
|
|
{
|
|
|
|
// We don't allow empty relative path to access the root.
|
|
|
|
if (aPath.IsEmpty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Leading and trailing "/" are not allowed.
|
|
|
|
if (aPath.First() == FILESYSTEM_DOM_PATH_SEPARATOR_CHAR ||
|
|
|
|
aPath.Last() == FILESYSTEM_DOM_PATH_SEPARATOR_CHAR) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_NAMED_LITERAL_STRING(kCurrentDir, ".");
|
|
|
|
NS_NAMED_LITERAL_STRING(kParentDir, "..");
|
|
|
|
|
|
|
|
// Split path and check each path component.
|
2016-03-20 13:56:10 +03:00
|
|
|
nsCharSeparatedTokenizerTemplate<TokenizerIgnoreNothing>
|
|
|
|
tokenizer(aPath, FILESYSTEM_DOM_PATH_SEPARATOR_CHAR);
|
|
|
|
|
2016-03-20 13:56:10 +03:00
|
|
|
while (tokenizer.hasMoreTokens()) {
|
|
|
|
nsDependentSubstring pathComponent = tokenizer.nextToken();
|
|
|
|
// The path containing empty components, such as "foo//bar", is invalid.
|
|
|
|
// We don't allow paths, such as "../foo", "foo/./bar" and "foo/../bar",
|
|
|
|
// to walk up the directory.
|
|
|
|
if (pathComponent.IsEmpty() ||
|
|
|
|
pathComponent.Equals(kCurrentDir) ||
|
|
|
|
pathComponent.Equals(kParentDir)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
aParts.AppendElement(pathComponent);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
2015-12-30 11:53:38 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(Directory)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(Directory)
|
2016-03-20 13:56:10 +03:00
|
|
|
if (tmp->mFileSystem) {
|
|
|
|
tmp->mFileSystem->Unlink();
|
|
|
|
tmp->mFileSystem = nullptr;
|
|
|
|
}
|
2016-03-20 13:56:10 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mParent)
|
2015-12-30 11:53:38 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(Directory)
|
2016-03-20 13:56:10 +03:00
|
|
|
if (tmp->mFileSystem) {
|
|
|
|
tmp->mFileSystem->Traverse(cb);
|
|
|
|
}
|
2016-03-20 13:56:10 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mParent)
|
2015-12-30 11:53:38 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_WRAPPERCACHE(Directory)
|
|
|
|
|
2014-03-05 07:26:39 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(Directory)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(Directory)
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(Directory)
|
|
|
|
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
2014-03-05 07:25:40 +04:00
|
|
|
// static
|
|
|
|
already_AddRefed<Promise>
|
2014-07-19 05:31:11 +04:00
|
|
|
Directory::GetRoot(FileSystemBase* aFileSystem, ErrorResult& aRv)
|
2014-03-05 07:25:40 +04:00
|
|
|
{
|
2016-03-20 13:56:10 +03:00
|
|
|
MOZ_ASSERT(aFileSystem);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> path;
|
2016-03-30 09:17:15 +03:00
|
|
|
aRv = NS_NewNativeLocalFile(NS_ConvertUTF16toUTF8(aFileSystem->LocalOrDeviceStorageRootPath()),
|
2016-03-20 13:56:10 +03:00
|
|
|
true, getter_AddRefs(path));
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
2014-07-19 05:31:11 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
2016-03-20 13:56:10 +03:00
|
|
|
|
|
|
|
RefPtr<GetFileOrDirectoryTask> task =
|
|
|
|
GetFileOrDirectoryTask::Create(aFileSystem, path, eDOMRootDirectory, true, aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-03-05 07:24:19 +04:00
|
|
|
FileSystemPermissionRequest::RequestForTask(task);
|
2014-03-05 07:25:40 +04:00
|
|
|
return task->GetPromise();
|
|
|
|
}
|
|
|
|
|
2016-03-20 13:56:10 +03:00
|
|
|
/* static */ already_AddRefed<Directory>
|
2016-03-20 13:56:10 +03:00
|
|
|
Directory::Create(nsISupports* aParent, nsIFile* aFile,
|
2016-03-20 13:56:10 +03:00
|
|
|
DirectoryType aType, FileSystemBase* aFileSystem)
|
2014-03-05 07:26:39 +04:00
|
|
|
{
|
2016-03-20 13:56:10 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2016-03-20 13:56:10 +03:00
|
|
|
MOZ_ASSERT(aParent);
|
2016-03-20 13:56:10 +03:00
|
|
|
MOZ_ASSERT(aFile);
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
bool isDir;
|
|
|
|
nsresult rv = aFile->IsDirectory(&isDir);
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv) && isDir);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
RefPtr<Directory> directory =
|
2016-03-20 13:56:10 +03:00
|
|
|
new Directory(aParent, aFile, aType, aFileSystem);
|
2016-03-20 13:56:10 +03:00
|
|
|
return directory.forget();
|
|
|
|
}
|
|
|
|
|
2016-03-20 13:56:10 +03:00
|
|
|
Directory::Directory(nsISupports* aParent,
|
2016-03-20 13:56:10 +03:00
|
|
|
nsIFile* aFile,
|
|
|
|
DirectoryType aType,
|
|
|
|
FileSystemBase* aFileSystem)
|
2016-03-20 13:56:10 +03:00
|
|
|
: mParent(aParent)
|
2016-03-20 13:56:10 +03:00
|
|
|
, mFile(aFile)
|
|
|
|
, mType(aType)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(aFile);
|
|
|
|
|
|
|
|
// aFileSystem can be null. In this case we create a OSFileSystem when needed.
|
2016-03-20 13:58:01 +03:00
|
|
|
if (aFileSystem) {
|
|
|
|
// More likely, this is a OSFileSystem. This object keeps a reference of
|
|
|
|
// mParent but it's not cycle collectable and to avoid manual
|
|
|
|
// addref/release, it's better to have 1 object per directory. For this
|
|
|
|
// reason we clone it here.
|
|
|
|
mFileSystem = aFileSystem->Clone();
|
|
|
|
}
|
2014-03-05 07:26:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
Directory::~Directory()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-03-20 13:56:10 +03:00
|
|
|
nsISupports*
|
2014-03-05 07:26:39 +04:00
|
|
|
Directory::GetParentObject() const
|
|
|
|
{
|
2016-03-20 13:56:10 +03:00
|
|
|
return mParent;
|
2014-03-05 07:26:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
JSObject*
|
Bug 1117172 part 3. Change the wrappercached WrapObject methods to allow passing in aGivenProto. r=peterv
The only manual changes here are to BindingUtils.h, BindingUtils.cpp,
Codegen.py, Element.cpp, IDBFileRequest.cpp, IDBObjectStore.cpp,
dom/workers/Navigator.cpp, WorkerPrivate.cpp, DeviceStorageRequestChild.cpp,
Notification.cpp, nsGlobalWindow.cpp, MessagePort.cpp, nsJSEnvironment.cpp,
Sandbox.cpp, XPCConvert.cpp, ExportHelpers.cpp, and DataStoreService.cpp. The
rest of this diff was generated by running the following commands:
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObject\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(Binding(?:_workers)?::Wrap\((?:aCx|cx|aContext|aCtx|js), [^,)]+)\)/\1, aGivenProto)/g'
2015-03-19 17:13:33 +03:00
|
|
|
Directory::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
|
2014-03-05 07:26:39 +04:00
|
|
|
{
|
Bug 1117172 part 3. Change the wrappercached WrapObject methods to allow passing in aGivenProto. r=peterv
The only manual changes here are to BindingUtils.h, BindingUtils.cpp,
Codegen.py, Element.cpp, IDBFileRequest.cpp, IDBObjectStore.cpp,
dom/workers/Navigator.cpp, WorkerPrivate.cpp, DeviceStorageRequestChild.cpp,
Notification.cpp, nsGlobalWindow.cpp, MessagePort.cpp, nsJSEnvironment.cpp,
Sandbox.cpp, XPCConvert.cpp, ExportHelpers.cpp, and DataStoreService.cpp. The
rest of this diff was generated by running the following commands:
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObject\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(Binding(?:_workers)?::Wrap\((?:aCx|cx|aContext|aCtx|js), [^,)]+)\)/\1, aGivenProto)/g'
2015-03-19 17:13:33 +03:00
|
|
|
return DirectoryBinding::Wrap(aCx, this, aGivenProto);
|
2014-03-05 07:26:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-03-20 13:56:10 +03:00
|
|
|
Directory::GetName(nsAString& aRetval, ErrorResult& aRv)
|
2014-03-05 07:26:39 +04:00
|
|
|
{
|
|
|
|
aRetval.Truncate();
|
2014-03-05 07:25:40 +04:00
|
|
|
|
2016-03-20 13:56:10 +03:00
|
|
|
if (mType == eDOMRootDirectory) {
|
|
|
|
RefPtr<FileSystemBase> fs = GetFileSystem(aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fs->GetRootName(aRetval);
|
2014-03-05 07:25:40 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-03-20 13:56:10 +03:00
|
|
|
aRv = mFile->GetLeafName(aRetval);
|
|
|
|
NS_WARN_IF(aRv.Failed());
|
2014-03-05 07:26:39 +04:00
|
|
|
}
|
|
|
|
|
2014-03-05 12:40:48 +04:00
|
|
|
already_AddRefed<Promise>
|
2014-07-19 05:31:11 +04:00
|
|
|
Directory::CreateFile(const nsAString& aPath, const CreateFileOptions& aOptions,
|
|
|
|
ErrorResult& aRv)
|
2014-03-05 12:40:48 +04:00
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Blob> blobData;
|
2014-03-05 12:40:48 +04:00
|
|
|
InfallibleTArray<uint8_t> arrayData;
|
|
|
|
bool replace = (aOptions.mIfExists == CreateIfExistsMode::Replace);
|
|
|
|
|
|
|
|
// Get the file content.
|
|
|
|
if (aOptions.mData.WasPassed()) {
|
|
|
|
auto& data = aOptions.mData.Value();
|
|
|
|
if (data.IsString()) {
|
|
|
|
NS_ConvertUTF16toUTF8 str(data.GetAsString());
|
|
|
|
arrayData.AppendElements(reinterpret_cast<const uint8_t *>(str.get()),
|
|
|
|
str.Length());
|
|
|
|
} else if (data.IsArrayBuffer()) {
|
2014-06-24 00:03:57 +04:00
|
|
|
const ArrayBuffer& buffer = data.GetAsArrayBuffer();
|
Bug 999651, bug 995679, bug 1009952, bug 1011007, bug 991981. r=sfink, r=shu, r=jandem, r=jdm, r=luke, r=bbouvier, r=nmatsakis, r=bz, r=ehsan, r=jgilbert, r=smaug, r=sicking, r=terrence, r=bholley, r=bent, r=efaust, r=jorendorff
2014-05-28 01:32:41 +04:00
|
|
|
buffer.ComputeLengthAndData();
|
2014-03-05 12:40:48 +04:00
|
|
|
arrayData.AppendElements(buffer.Data(), buffer.Length());
|
|
|
|
} else if (data.IsArrayBufferView()){
|
2014-06-24 00:03:57 +04:00
|
|
|
const ArrayBufferView& view = data.GetAsArrayBufferView();
|
Bug 999651, bug 995679, bug 1009952, bug 1011007, bug 991981. r=sfink, r=shu, r=jandem, r=jdm, r=luke, r=bbouvier, r=nmatsakis, r=bz, r=ehsan, r=jgilbert, r=smaug, r=sicking, r=terrence, r=bholley, r=bent, r=efaust, r=jorendorff
2014-05-28 01:32:41 +04:00
|
|
|
view.ComputeLengthAndData();
|
2014-03-05 12:40:48 +04:00
|
|
|
arrayData.AppendElements(view.Data(), view.Length());
|
|
|
|
} else {
|
|
|
|
blobData = data.GetAsBlob();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-20 13:56:10 +03:00
|
|
|
nsCOMPtr<nsIFile> realPath;
|
|
|
|
nsresult error = DOMPathToRealPath(aPath, getter_AddRefs(realPath));
|
|
|
|
|
|
|
|
RefPtr<FileSystemBase> fs = GetFileSystem(aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
2014-03-05 12:40:48 +04:00
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CreateFileTask> task =
|
2016-03-20 13:56:10 +03:00
|
|
|
CreateFileTask::Create(fs, realPath, blobData, arrayData, replace, aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
2014-07-19 05:31:11 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
2016-03-20 13:56:10 +03:00
|
|
|
|
2014-03-05 12:40:48 +04:00
|
|
|
task->SetError(error);
|
|
|
|
FileSystemPermissionRequest::RequestForTask(task);
|
|
|
|
return task->GetPromise();
|
|
|
|
}
|
|
|
|
|
2014-03-05 07:26:39 +04:00
|
|
|
already_AddRefed<Promise>
|
2014-07-19 05:31:11 +04:00
|
|
|
Directory::CreateDirectory(const nsAString& aPath, ErrorResult& aRv)
|
2014-03-05 07:26:39 +04:00
|
|
|
{
|
2016-03-20 13:56:10 +03:00
|
|
|
nsCOMPtr<nsIFile> realPath;
|
|
|
|
nsresult error = DOMPathToRealPath(aPath, getter_AddRefs(realPath));
|
|
|
|
|
|
|
|
RefPtr<FileSystemBase> fs = GetFileSystem(aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
2014-03-05 07:25:40 +04:00
|
|
|
}
|
2016-03-20 13:56:10 +03:00
|
|
|
|
|
|
|
RefPtr<CreateDirectoryTask> task =
|
|
|
|
CreateDirectoryTask::Create(fs, realPath, aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
2014-07-19 05:31:11 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
2016-03-20 13:56:10 +03:00
|
|
|
|
2014-03-05 07:25:40 +04:00
|
|
|
task->SetError(error);
|
2014-03-05 07:24:19 +04:00
|
|
|
FileSystemPermissionRequest::RequestForTask(task);
|
2014-03-05 07:25:40 +04:00
|
|
|
return task->GetPromise();
|
2014-03-05 07:26:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<Promise>
|
2014-07-19 05:31:11 +04:00
|
|
|
Directory::Get(const nsAString& aPath, ErrorResult& aRv)
|
2014-03-05 07:26:39 +04:00
|
|
|
{
|
2016-03-20 13:56:10 +03:00
|
|
|
nsCOMPtr<nsIFile> realPath;
|
|
|
|
nsresult error = DOMPathToRealPath(aPath, getter_AddRefs(realPath));
|
|
|
|
|
|
|
|
RefPtr<FileSystemBase> fs = GetFileSystem(aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
2014-03-05 07:25:40 +04:00
|
|
|
}
|
2016-03-20 13:56:10 +03:00
|
|
|
|
|
|
|
RefPtr<GetFileOrDirectoryTask> task =
|
|
|
|
GetFileOrDirectoryTask::Create(fs, realPath, eNotDOMRootDirectory, false,
|
|
|
|
aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
2014-07-19 05:31:11 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
2016-03-20 13:56:10 +03:00
|
|
|
|
2014-03-05 07:25:40 +04:00
|
|
|
task->SetError(error);
|
2014-03-05 07:24:19 +04:00
|
|
|
FileSystemPermissionRequest::RequestForTask(task);
|
2014-03-05 07:25:40 +04:00
|
|
|
return task->GetPromise();
|
|
|
|
}
|
|
|
|
|
2014-03-12 10:30:21 +04:00
|
|
|
already_AddRefed<Promise>
|
2014-07-19 05:31:11 +04:00
|
|
|
Directory::Remove(const StringOrFileOrDirectory& aPath, ErrorResult& aRv)
|
2014-03-12 10:30:21 +04:00
|
|
|
{
|
2014-07-19 05:31:11 +04:00
|
|
|
return RemoveInternal(aPath, false, aRv);
|
2014-03-12 10:30:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<Promise>
|
2014-07-19 05:31:11 +04:00
|
|
|
Directory::RemoveDeep(const StringOrFileOrDirectory& aPath, ErrorResult& aRv)
|
2014-03-12 10:30:21 +04:00
|
|
|
{
|
2014-07-19 05:31:11 +04:00
|
|
|
return RemoveInternal(aPath, true, aRv);
|
2014-03-12 10:30:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<Promise>
|
2014-07-19 05:31:11 +04:00
|
|
|
Directory::RemoveInternal(const StringOrFileOrDirectory& aPath, bool aRecursive,
|
|
|
|
ErrorResult& aRv)
|
2014-03-12 10:30:21 +04:00
|
|
|
{
|
|
|
|
nsresult error = NS_OK;
|
2016-03-20 13:56:10 +03:00
|
|
|
nsCOMPtr<nsIFile> realPath;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<BlobImpl> blob;
|
2014-03-12 10:30:21 +04:00
|
|
|
|
|
|
|
// Check and get the target path.
|
|
|
|
|
2016-03-20 13:56:10 +03:00
|
|
|
RefPtr<FileSystemBase> fs = GetFileSystem(aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-03-12 10:30:21 +04:00
|
|
|
if (aPath.IsFile()) {
|
2015-05-12 15:11:03 +03:00
|
|
|
blob = aPath.GetAsFile().Impl();
|
2015-04-20 22:48:15 +03:00
|
|
|
} else if (aPath.IsString()) {
|
2016-03-20 13:56:10 +03:00
|
|
|
error = DOMPathToRealPath(aPath.GetAsString(), getter_AddRefs(realPath));
|
|
|
|
} else if (!fs->IsSafeDirectory(&aPath.GetAsDirectory())) {
|
2014-03-12 10:30:21 +04:00
|
|
|
error = NS_ERROR_DOM_SECURITY_ERR;
|
2015-04-20 22:48:15 +03:00
|
|
|
} else {
|
2016-03-20 13:56:10 +03:00
|
|
|
realPath = aPath.GetAsDirectory().mFile;
|
2015-04-20 22:48:15 +03:00
|
|
|
// The target must be a descendant of this directory.
|
2016-03-20 13:56:10 +03:00
|
|
|
if (!FileSystemUtils::IsDescendantPath(mFile, realPath)) {
|
2015-04-20 22:48:15 +03:00
|
|
|
error = NS_ERROR_DOM_FILESYSTEM_NO_MODIFICATION_ALLOWED_ERR;
|
|
|
|
}
|
2014-03-12 10:30:21 +04:00
|
|
|
}
|
|
|
|
|
2016-03-20 13:56:10 +03:00
|
|
|
RefPtr<RemoveTask> task =
|
|
|
|
RemoveTask::Create(fs, mFile, blob, realPath, aRecursive, aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
2014-07-19 05:31:11 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
2014-03-12 10:30:21 +04:00
|
|
|
task->SetError(error);
|
|
|
|
FileSystemPermissionRequest::RequestForTask(task);
|
|
|
|
return task->GetPromise();
|
|
|
|
}
|
|
|
|
|
2015-06-23 02:31:34 +03:00
|
|
|
void
|
2016-03-20 13:56:10 +03:00
|
|
|
Directory::GetPath(nsAString& aRetval, ErrorResult& aRv)
|
2015-06-23 02:31:34 +03:00
|
|
|
{
|
2016-03-20 13:56:10 +03:00
|
|
|
if (mType == eDOMRootDirectory) {
|
2016-03-20 13:56:10 +03:00
|
|
|
aRetval.AssignLiteral(FILESYSTEM_DOM_PATH_SEPARATOR_LITERAL);
|
2015-06-23 02:31:34 +03:00
|
|
|
} else {
|
2016-03-20 13:56:10 +03:00
|
|
|
// TODO: this should be a bit different...
|
|
|
|
GetName(aRetval, aRv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
Directory::GetFullRealPath(nsAString& aPath)
|
|
|
|
{
|
|
|
|
nsresult rv = mFile->GetPath(aPath);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
2015-06-23 02:31:34 +03:00
|
|
|
}
|
2016-03-20 13:56:10 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
2015-06-23 02:31:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<Promise>
|
2016-03-20 13:56:10 +03:00
|
|
|
Directory::GetFilesAndDirectories(ErrorResult& aRv)
|
2015-06-23 02:31:34 +03:00
|
|
|
{
|
2016-03-20 13:56:10 +03:00
|
|
|
RefPtr<FileSystemBase> fs = GetFileSystem(aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<GetDirectoryListingTask> task =
|
2016-03-20 13:56:10 +03:00
|
|
|
GetDirectoryListingTask::Create(fs, mFile, mType, mFilters, aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
2015-06-23 02:31:34 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
2016-02-01 23:56:43 +03:00
|
|
|
|
2015-06-23 02:31:34 +03:00
|
|
|
FileSystemPermissionRequest::RequestForTask(task);
|
|
|
|
return task->GetPromise();
|
|
|
|
}
|
|
|
|
|
2015-10-09 17:59:40 +03:00
|
|
|
void
|
|
|
|
Directory::SetContentFilters(const nsAString& aFilters)
|
|
|
|
{
|
|
|
|
mFilters = aFilters;
|
|
|
|
}
|
|
|
|
|
2014-03-12 10:30:21 +04:00
|
|
|
FileSystemBase*
|
2016-03-20 13:56:10 +03:00
|
|
|
Directory::GetFileSystem(ErrorResult& aRv)
|
2014-03-12 10:30:21 +04:00
|
|
|
{
|
2016-03-20 13:56:10 +03:00
|
|
|
if (!mFileSystem) {
|
2016-03-30 09:17:15 +03:00
|
|
|
// Any subdir inherits the FileSystem of the parent Directory. If we are
|
|
|
|
// here it's because we are dealing with the DOM root.
|
|
|
|
MOZ_ASSERT(mType == eDOMRootDirectory);
|
2016-03-20 13:56:10 +03:00
|
|
|
|
|
|
|
nsAutoString path;
|
2016-03-30 09:17:15 +03:00
|
|
|
aRv = mFile->GetPath(path);
|
2016-03-20 13:56:10 +03:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<OSFileSystem> fs = new OSFileSystem(path);
|
2016-03-20 13:56:10 +03:00
|
|
|
fs->Init(mParent);
|
2016-03-20 13:56:10 +03:00
|
|
|
|
|
|
|
mFileSystem = fs;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mFileSystem;
|
2014-03-12 10:30:21 +04:00
|
|
|
}
|
|
|
|
|
2016-03-20 13:56:10 +03:00
|
|
|
nsresult
|
|
|
|
Directory::DOMPathToRealPath(const nsAString& aPath, nsIFile** aFile) const
|
2014-03-05 07:25:40 +04:00
|
|
|
{
|
|
|
|
nsString relativePath;
|
|
|
|
relativePath = aPath;
|
|
|
|
|
|
|
|
// Trim white spaces.
|
|
|
|
static const char kWhitespace[] = "\b\t\r\n ";
|
|
|
|
relativePath.Trim(kWhitespace);
|
|
|
|
|
2016-03-20 13:56:10 +03:00
|
|
|
nsTArray<nsString> parts;
|
|
|
|
if (!IsValidRelativeDOMPath(relativePath, parts)) {
|
2016-03-20 13:56:10 +03:00
|
|
|
return NS_ERROR_DOM_FILESYSTEM_INVALID_PATH_ERR;
|
2014-03-05 07:25:40 +04:00
|
|
|
}
|
|
|
|
|
2016-03-20 13:56:10 +03:00
|
|
|
nsCOMPtr<nsIFile> file;
|
|
|
|
nsresult rv = mFile->Clone(getter_AddRefs(file));
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2016-03-20 13:56:10 +03:00
|
|
|
for (uint32_t i = 0; i < parts.Length(); ++i) {
|
|
|
|
rv = file->AppendRelativePath(parts[i]);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
2016-03-20 13:56:10 +03:00
|
|
|
}
|
2016-03-20 05:44:22 +03:00
|
|
|
|
2016-03-20 13:56:10 +03:00
|
|
|
file.forget(aFile);
|
|
|
|
return NS_OK;
|
2016-03-20 05:44:22 +03:00
|
|
|
}
|
|
|
|
|
2014-03-05 07:26:39 +04:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|