Bug 1741548 - Add ignoreAbsent option to `IOUtils.getChildren` method. r=barret

Remove an incorrect comment.

Differential Revision: https://phabricator.services.mozilla.com/D131323
This commit is contained in:
Mathew Hodson 2021-11-25 10:45:55 +00:00
Родитель 01fe642138
Коммит 691b1a0c53
4 изменённых файлов: 42 добавлений и 11 удалений

Просмотреть файл

@ -176,8 +176,7 @@ namespace IOUtils {
Promise<long long> setModificationTime(DOMString path, optional long long modification);
/**
* Retrieves a (possibly empty) list of immediate children of the directory at
* |path|. If the file at |path| is not a directory, this method resolves with
* an empty list.
* |path|.
*
* @param path An absolute file path.
*
@ -185,7 +184,7 @@ namespace IOUtils {
* children of the directory at |path|, otherwise rejects with a
* DOMException.
*/
Promise<sequence<DOMString>> getChildren(DOMString path);
Promise<sequence<DOMString>> getChildren(DOMString path, optional GetChildrenOptions options = {});
/**
* Set the permissions of the file at |path|.
*
@ -413,6 +412,16 @@ dictionary CopyOptions {
boolean recursive = false;
};
/**
* Options to be passed to the |IOUtils.getChildren| method.
*/
dictionary GetChildrenOptions {
/**
* If true, no error will be reported if the target file is missing.
*/
boolean ignoreAbsent = false;
};
/**
* Types of files that are recognized by the |IOUtils.stat| method.
*/

Просмотреть файл

@ -737,8 +737,9 @@ already_AddRefed<Promise> IOUtils::SetModificationTime(
}
/* static */
already_AddRefed<Promise> IOUtils::GetChildren(GlobalObject& aGlobal,
const nsAString& aPath) {
already_AddRefed<Promise> IOUtils::GetChildren(
GlobalObject& aGlobal, const nsAString& aPath,
const GetChildrenOptions& aOptions) {
MOZ_DIAGNOSTIC_ASSERT(XRE_IsParentProcess());
RefPtr<Promise> promise = CreateJSPromise(aGlobal);
if (!promise) {
@ -751,7 +752,9 @@ already_AddRefed<Promise> IOUtils::GetChildren(GlobalObject& aGlobal,
DispatchAndResolve<nsTArray<nsString>>(
state.ref()->mEventQueue, promise,
[file = std::move(file)]() { return GetChildrenSync(file); });
[file = std::move(file), ignoreAbsent = aOptions.mIgnoreAbsent]() {
return GetChildrenSync(file, ignoreAbsent);
});
} else {
RejectShuttingDown(promise);
}
@ -1465,11 +1468,15 @@ Result<int64_t, IOUtils::IOError> IOUtils::SetModificationTimeSync(
/* static */
Result<nsTArray<nsString>, IOUtils::IOError> IOUtils::GetChildrenSync(
nsIFile* aFile) {
nsIFile* aFile, bool aIgnoreAbsent) {
MOZ_ASSERT(!NS_IsMainThread());
nsTArray<nsString> children;
nsCOMPtr<nsIDirectoryEnumerator> iter;
nsresult rv = aFile->GetDirectoryEntries(getter_AddRefs(iter));
if (aIgnoreAbsent && IsFileNotFound(rv)) {
return children;
}
if (NS_FAILED(rv)) {
IOError err(rv);
if (IsFileNotFound(rv)) {
@ -1484,7 +1491,6 @@ Result<nsTArray<nsString>, IOUtils::IOError> IOUtils::GetChildrenSync(
}
return Err(err);
}
nsTArray<nsString> children;
bool hasMoreElements = false;
MOZ_TRY(iter->HasMoreElements(&hasMoreElements));

Просмотреть файл

@ -112,8 +112,9 @@ class IOUtils final {
GlobalObject& aGlobal, const nsAString& aPath,
const Optional<int64_t>& aModification);
static already_AddRefed<Promise> GetChildren(GlobalObject& aGlobal,
const nsAString& aPath);
static already_AddRefed<Promise> GetChildren(
GlobalObject& aGlobal, const nsAString& aPath,
const GetChildrenOptions& aOptions);
static already_AddRefed<Promise> SetPermissions(GlobalObject& aGlobal,
const nsAString& aPath,
@ -339,7 +340,8 @@ class IOUtils final {
* @return An array of absolute paths identifying the children of |aFile|.
* If there are no children, an empty array. Otherwise, an error.
*/
static Result<nsTArray<nsString>, IOError> GetChildrenSync(nsIFile* aFile);
static Result<nsTArray<nsString>, IOError> GetChildrenSync(
nsIFile* aFile, bool aIgnoreAbsent);
/**
* Set the permissions of the given file.

Просмотреть файл

@ -72,6 +72,20 @@
await cleanup(emptyDir);
});
add_task(async function iterate_ignore_missing_dir() {
info("Try to get the children of a missing file with ignoreAbsent");
const tmpDir = await PathUtils.getTempDir();
const notExists = PathUtils.join(tmpDir, "does_not_exist_dir.tmp.d");
is(
(await IOUtils.getChildren(notExists, { ignoreAbsent: true })).length,
0,
"IOUtils::getChildren returns an empty array when called with ignoreAbsent on a missing file"
);
ok(!await fileExists(notExists), `Expected ${notExists} not to exist`);
});
</script>
</head>