summaryrefslogtreecommitdiffstats
path: root/dom/indexedDB/DatabaseFileManagerImpl.h
blob: 0753adf5ffddae53520691c6d3ea428ccfdb8317 (plain)
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
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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/. */

#ifndef DOM_INDEXEDDB_DATABASEFILEMANAGERIMPL_H_
#define DOM_INDEXEDDB_DATABASEFILEMANAGERIMPL_H_

#include "DatabaseFileManager.h"

// local includes
#include "ActorsParentCommon.h"

// global includes
#include "mozilla/dom/quota/Assertions.h"
#include "mozilla/dom/quota/QuotaCommon.h"
#include "nsIFile.h"
#include "nsString.h"

namespace mozilla::dom::indexedDB {

template <typename KnownDirEntryOp, typename UnknownDirEntryOp>
Result<Ok, nsresult> DatabaseFileManager::TraverseFiles(
    nsIFile& aDirectory, KnownDirEntryOp&& aKnownDirEntryOp,
    UnknownDirEntryOp&& aUnknownDirEntryOp) {
  quota::AssertIsOnIOThread();

  QM_TRY_INSPECT(const bool& exists,
                 MOZ_TO_RESULT_INVOKE_MEMBER(aDirectory, Exists));

  if (!exists) {
    return Ok{};
  }

  QM_TRY(quota::CollectEachFile(
      aDirectory,
      [&aKnownDirEntryOp, &aUnknownDirEntryOp](
          const nsCOMPtr<nsIFile>& file) -> Result<Ok, nsresult> {
        QM_TRY_INSPECT(const auto& dirEntryKind, quota::GetDirEntryKind(*file));

        switch (dirEntryKind) {
          case quota::nsIFileKind::ExistsAsDirectory: {
            QM_TRY_INSPECT(
                const auto& leafName,
                MOZ_TO_RESULT_INVOKE_MEMBER_TYPED(nsString, file, GetLeafName));

            if (leafName.Equals(kJournalDirectoryName)) {
              QM_TRY(std::forward<KnownDirEntryOp>(aKnownDirEntryOp)(
                  *file, /* isDirectory */ true));

              break;
            }

            Unused << WARN_IF_FILE_IS_UNKNOWN(*file);

            QM_TRY(std::forward<UnknownDirEntryOp>(aUnknownDirEntryOp)(
                *file, /* isDirectory */ true));

            break;
          }

          case quota::nsIFileKind::ExistsAsFile: {
            QM_TRY_INSPECT(
                const auto& leafName,
                MOZ_TO_RESULT_INVOKE_MEMBER_TYPED(nsString, file, GetLeafName));

            nsresult rv;
            leafName.ToInteger64(&rv);
            if (NS_SUCCEEDED(rv)) {
              QM_TRY(std::forward<KnownDirEntryOp>(aKnownDirEntryOp)(
                  *file, /* isDirectory */ false));

              break;
            }

            Unused << WARN_IF_FILE_IS_UNKNOWN(*file);

            QM_TRY(std::forward<UnknownDirEntryOp>(aUnknownDirEntryOp)(
                *file, /* isDirectory */ false));

            break;
          }

          case quota::nsIFileKind::DoesNotExist:
            // Ignore files that got removed externally while iterating.
            break;
        }

        return Ok{};
      }));

  return Ok{};
}

}  // namespace mozilla::dom::indexedDB

#endif  // DOM_INDEXEDDB_DATABASEFILEMANAGERIMPL_H_