summaryrefslogtreecommitdiffstats
path: root/dom/fs/parent/datamodel/FileSystemDatabaseManagerVersion001.cpp
blob: a898f1afb61cfa4f57d76f04dbb1cdc1dead2bae (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
/* -*- 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/. */

#include "FileSystemDatabaseManagerVersion001.h"

#include "ErrorList.h"
#include "FileSystemContentTypeGuess.h"
#include "FileSystemDataManager.h"
#include "FileSystemFileManager.h"
#include "FileSystemParentTypes.h"
#include "ResultStatement.h"
#include "StartedTransaction.h"
#include "mozStorageHelper.h"
#include "mozilla/CheckedInt.h"
#include "mozilla/dom/FileSystemDataManager.h"
#include "mozilla/dom/FileSystemHandle.h"
#include "mozilla/dom/FileSystemLog.h"
#include "mozilla/dom/FileSystemTypes.h"
#include "mozilla/dom/PFileSystemManager.h"
#include "mozilla/dom/quota/Client.h"
#include "mozilla/dom/quota/QuotaCommon.h"
#include "mozilla/dom/quota/QuotaManager.h"
#include "mozilla/dom/quota/QuotaObject.h"
#include "mozilla/dom/quota/ResultExtensions.h"
#include "nsString.h"

namespace mozilla::dom {

using FileSystemEntries = nsTArray<fs::FileSystemEntryMetadata>;

namespace fs::data {

namespace {

constexpr const nsLiteralCString gDescendantsQuery =
    "WITH RECURSIVE traceChildren(handle, parent) AS ( "
    "SELECT handle, parent "
    "FROM Entries "
    "WHERE handle=:handle "
    "UNION "
    "SELECT Entries.handle, Entries.parent FROM traceChildren, Entries "
    "WHERE traceChildren.handle=Entries.parent ) "
    "SELECT handle "
    "FROM traceChildren INNER JOIN Files "
    "USING(handle) "
    ";"_ns;

Result<bool, QMResult> IsDirectoryEmpty(const FileSystemConnection& mConnection,
                                        const EntryId& aEntryId) {
  const nsLiteralCString isDirEmptyQuery =
      "SELECT EXISTS ("
      "SELECT 1 FROM Entries WHERE parent = :parent "
      ");"_ns;

  QM_TRY_UNWRAP(ResultStatement stmt,
                ResultStatement::Create(mConnection, isDirEmptyQuery));
  QM_TRY(QM_TO_RESULT(stmt.BindEntryIdByName("parent"_ns, aEntryId)));
  QM_TRY_UNWRAP(bool childrenExist, stmt.YesOrNoQuery());

  return !childrenExist;
}

Result<bool, QMResult> DoesDirectoryExist(
    const FileSystemConnection& mConnection,
    const FileSystemChildMetadata& aHandle) {
  MOZ_ASSERT(!aHandle.parentId().IsEmpty());

  const nsCString existsQuery =
      "SELECT EXISTS "
      "(SELECT 1 FROM Directories INNER JOIN Entries USING (handle) "
      "WHERE Directories.name = :name AND Entries.parent = :parent ) "
      ";"_ns;

  QM_TRY_RETURN(ApplyEntryExistsQuery(mConnection, existsQuery, aHandle));
}

Result<bool, QMResult> DoesDirectoryExist(
    const FileSystemConnection& mConnection, const EntryId& aEntry) {
  MOZ_ASSERT(!aEntry.IsEmpty());

  const nsCString existsQuery =
      "SELECT EXISTS "
      "(SELECT 1 FROM Directories WHERE handle = :handle ) "
      ";"_ns;

  QM_TRY_RETURN(ApplyEntryExistsQuery(mConnection, existsQuery, aEntry));
}

Result<bool, QMResult> IsAncestor(const FileSystemConnection& aConnection,
                                  const FileSystemEntryPair& aEndpoints) {
  const nsCString pathQuery =
      "WITH RECURSIVE followPath(handle, parent) AS ( "
      "SELECT handle, parent "
      "FROM Entries "
      "WHERE handle=:entryId "
      "UNION "
      "SELECT Entries.handle, Entries.parent FROM followPath, Entries "
      "WHERE followPath.parent=Entries.handle ) "
      "SELECT EXISTS "
      "(SELECT 1 FROM followPath "
      "WHERE handle=:possibleAncestor ) "
      ";"_ns;

  QM_TRY_UNWRAP(ResultStatement stmt,
                ResultStatement::Create(aConnection, pathQuery));
  QM_TRY(
      QM_TO_RESULT(stmt.BindEntryIdByName("entryId"_ns, aEndpoints.childId())));
  QM_TRY(QM_TO_RESULT(
      stmt.BindEntryIdByName("possibleAncestor"_ns, aEndpoints.parentId())));

  return stmt.YesOrNoQuery();
}

Result<bool, QMResult> DoesFileExist(const FileSystemConnection& aConnection,
                                     const FileSystemChildMetadata& aHandle) {
  MOZ_ASSERT(!aHandle.parentId().IsEmpty());

  const nsCString existsQuery =
      "SELECT EXISTS "
      "(SELECT 1 FROM Files INNER JOIN Entries USING (handle) "
      "WHERE Files.name = :name AND Entries.parent = :parent ) "
      ";"_ns;

  QM_TRY_RETURN(ApplyEntryExistsQuery(aConnection, existsQuery, aHandle));
}

nsresult GetEntries(const FileSystemConnection& aConnection,
                    const nsACString& aUnboundStmt, const EntryId& aParent,
                    PageNumber aPage, bool aDirectory,
                    FileSystemEntries& aEntries) {
  // The entries inside a directory are sent to the child process in batches
  // of pageSize items. Large value ensures that iteration is less often delayed
  // by IPC messaging and querying the database.
  // TODO: The current value 1024 is not optimized.
  // TODO: Value "pageSize" is shared with the iterator implementation and
  // should be defined in a common place.
  const int32_t pageSize = 1024;

  QM_TRY_UNWRAP(bool exists, DoesDirectoryExist(aConnection, aParent));
  if (!exists) {
    return NS_ERROR_DOM_NOT_FOUND_ERR;
  }

  QM_TRY_UNWRAP(ResultStatement stmt,
                ResultStatement::Create(aConnection, aUnboundStmt));
  QM_TRY(QM_TO_RESULT(stmt.BindEntryIdByName("parent"_ns, aParent)));
  QM_TRY(QM_TO_RESULT(stmt.BindPageNumberByName("pageSize"_ns, pageSize)));
  QM_TRY(QM_TO_RESULT(
      stmt.BindPageNumberByName("pageOffset"_ns, aPage * pageSize)));

  QM_TRY_UNWRAP(bool moreResults, stmt.ExecuteStep());

  while (moreResults) {
    QM_TRY_UNWRAP(EntryId entryId, stmt.GetEntryIdByColumn(/* Column */ 0u));
    QM_TRY_UNWRAP(Name entryName, stmt.GetNameByColumn(/* Column */ 1u));

    FileSystemEntryMetadata metadata(entryId, entryName, aDirectory);
    aEntries.AppendElement(metadata);

    QM_TRY_UNWRAP(moreResults, stmt.ExecuteStep());
  }

  return NS_OK;
}

Result<EntryId, QMResult> GetUniqueEntryId(
    const FileSystemConnection& aConnection,
    const FileSystemChildMetadata& aHandle) {
  const nsCString existsQuery =
      "SELECT EXISTS "
      "(SELECT 1 FROM Entries "
      "WHERE handle = :handle )"
      ";"_ns;

  FileSystemChildMetadata generatorInput = aHandle;

  const size_t maxRounds = 1024u;

  for (size_t hangGuard = 0u; hangGuard < maxRounds; ++hangGuard) {
    QM_TRY_UNWRAP(EntryId entryId, fs::data::GetEntryHandle(generatorInput));
    QM_TRY_UNWRAP(ResultStatement stmt,
                  ResultStatement::Create(aConnection, existsQuery));
    QM_TRY(QM_TO_RESULT(stmt.BindEntryIdByName("handle"_ns, entryId)));

    QM_TRY_UNWRAP(bool alreadyInUse, stmt.YesOrNoQuery());

    if (!alreadyInUse) {
      return entryId;
    }

    generatorInput.parentId() = entryId;
  }

  return Err(QMResult(NS_ERROR_UNEXPECTED));
}

nsresult PerformRename(const FileSystemConnection& aConnection,
                       const FileSystemEntryMetadata& aHandle,
                       const Name& aNewName, const ContentType& aNewType,
                       const nsLiteralCString& aNameUpdateQuery) {
  MOZ_ASSERT(!aHandle.entryId().IsEmpty());
  MOZ_ASSERT(IsValidName(aHandle.entryName()));

  // same-name is checked in RenameEntry()
  if (!IsValidName(aNewName)) {
    return NS_ERROR_DOM_TYPE_MISMATCH_ERR;
  }

  // TODO: This should fail when handle doesn't exist - the
  // explicit file or directory existence queries are redundant
  QM_TRY_UNWRAP(ResultStatement stmt,
                ResultStatement::Create(aConnection, aNameUpdateQuery)
                    .mapErr(toNSResult));
  if (!aNewType.IsVoid()) {
    QM_TRY(MOZ_TO_RESULT(stmt.BindContentTypeByName("type"_ns, aNewType)));
  }
  QM_TRY(MOZ_TO_RESULT(stmt.BindNameByName("name"_ns, aNewName)));
  QM_TRY(MOZ_TO_RESULT(stmt.BindEntryIdByName("handle"_ns, aHandle.entryId())));
  QM_TRY(MOZ_TO_RESULT(stmt.Execute()));

  return NS_OK;
}

nsresult PerformRenameDirectory(const FileSystemConnection& aConnection,
                                const FileSystemEntryMetadata& aHandle,
                                const Name& aNewName) {
  const nsLiteralCString updateDirectoryNameQuery =
      "UPDATE Directories "
      "SET name = :name "
      "WHERE handle = :handle "
      ";"_ns;

  return PerformRename(aConnection, aHandle, aNewName, VoidCString(),
                       updateDirectoryNameQuery);
}

nsresult PerformRenameFile(const FileSystemConnection& aConnection,
                           const FileSystemEntryMetadata& aHandle,
                           const Name& aNewName, const ContentType& aNewType) {
  const nsLiteralCString updateFileTypeAndNameQuery =
      "UPDATE Files SET type = :type, name = :name "
      "WHERE handle = :handle ;"_ns;

  const nsLiteralCString updateFileNameQuery =
      "UPDATE Files SET name = :name WHERE handle = :handle ;"_ns;

  if (aNewType.IsVoid()) {
    return PerformRename(aConnection, aHandle, aNewName, aNewType,
                         updateFileNameQuery);
  }

  return PerformRename(aConnection, aHandle, aNewName, aNewType,
                       updateFileTypeAndNameQuery);
}

template <class HandlerType>
nsresult SetUsageTrackingImpl(const FileSystemConnection& aConnection,
                              const FileId& aFileId, bool aTracked,
                              HandlerType&& aOnMissingFile) {
  const nsLiteralCString setTrackedQuery =
      "INSERT INTO Usages "
      "( handle, tracked ) "
      "VALUES "
      "( :handle, :tracked ) "
      "ON CONFLICT(handle) DO "
      "UPDATE SET tracked = excluded.tracked "
      ";"_ns;

  const nsresult customReturnValue =
      aTracked ? NS_ERROR_DOM_NOT_FOUND_ERR : NS_OK;

  QM_TRY_UNWRAP(ResultStatement stmt,
                ResultStatement::Create(aConnection, setTrackedQuery));
  QM_TRY(MOZ_TO_RESULT(stmt.BindFileIdByName("handle"_ns, aFileId)));
  QM_TRY(MOZ_TO_RESULT(stmt.BindBooleanByName("tracked"_ns, aTracked)));
  QM_TRY(MOZ_TO_RESULT(stmt.Execute()), customReturnValue,
         std::forward<HandlerType>(aOnMissingFile));

  return NS_OK;
}

Result<nsTArray<FileId>, QMResult> GetTrackedFiles(
    const FileSystemConnection& aConnection) {
  // The same query works for both 001 and 002 schemas because handle is
  // an entry id and later on a file id, respectively.
  static const nsLiteralCString getTrackedFilesQuery =
      "SELECT handle FROM Usages WHERE tracked = TRUE;"_ns;
  nsTArray<FileId> trackedFiles;

  QM_TRY_UNWRAP(ResultStatement stmt,
                ResultStatement::Create(aConnection, getTrackedFilesQuery));
  QM_TRY_UNWRAP(bool moreResults, stmt.ExecuteStep());

  while (moreResults) {
    QM_TRY_UNWRAP(FileId fileId, stmt.GetFileIdByColumn(/* Column */ 0u));

    trackedFiles.AppendElement(fileId);  // TODO: fallible?

    QM_TRY_UNWRAP(moreResults, stmt.ExecuteStep());
  }

  return trackedFiles;
}

/** This handles the file not found error by assigning 0 usage to the dangling
 * handle and puts the handle to a non-tracked state. Otherwise, when the
 * file or database cannot be reached, the file remains in the tracked state.
 */
template <class QuotaCacheUpdate>
nsresult UpdateUsageForFileEntry(const FileSystemConnection& aConnection,
                                 const FileSystemFileManager& aFileManager,
                                 const FileId& aFileId,
                                 const nsLiteralCString& aUpdateQuery,
                                 QuotaCacheUpdate&& aUpdateCache) {
  QM_TRY_INSPECT(const auto& fileHandle, aFileManager.GetFile(aFileId));

  // A file could have changed in a way which doesn't allow to read its size.
  QM_TRY_UNWRAP(
      const Usage fileSize,
      QM_OR_ELSE_WARN_IF(
          // Expression.
          MOZ_TO_RESULT_INVOKE_MEMBER(fileHandle, GetFileSize),
          // Predicate.
          ([](const nsresult rv) { return rv == NS_ERROR_FILE_NOT_FOUND; }),
          // Fallback. If the file does no longer exist, treat it as 0-sized.
          ErrToDefaultOk<Usage>));

  QM_TRY(MOZ_TO_RESULT(aUpdateCache(fileSize)));

  // No transaction as one statement succeeds or fails atomically
  QM_TRY_UNWRAP(ResultStatement stmt,
                ResultStatement::Create(aConnection, aUpdateQuery));

  QM_TRY(MOZ_TO_RESULT(stmt.BindFileIdByName("handle"_ns, aFileId)));

  QM_TRY(MOZ_TO_RESULT(stmt.BindUsageByName("usage"_ns, fileSize)));

  QM_TRY(MOZ_TO_RESULT(stmt.Execute()));

  return NS_OK;
}

nsresult UpdateUsageUnsetTracked(const FileSystemConnection& aConnection,
                                 const FileSystemFileManager& aFileManager,
                                 const FileId& aFileId) {
  static const nsLiteralCString updateUsagesUnsetTrackedQuery =
      "UPDATE Usages SET usage = :usage, tracked = FALSE "
      "WHERE handle = :handle;"_ns;

  auto noCacheUpdateNeeded = [](auto) { return NS_OK; };

  return UpdateUsageForFileEntry(aConnection, aFileManager, aFileId,
                                 updateUsagesUnsetTrackedQuery,
                                 std::move(noCacheUpdateNeeded));
}

/**
 * @brief Get the recorded usage only if the file is in tracked state.
 * During origin initialization, if the usage on disk is unreadable, the latest
 * recorded usage is reported to the quota manager for the tracked files.
 * To allow writing, we attempt to update the real usage with one database and
 * one file size query.
 */
Result<Maybe<Usage>, QMResult> GetMaybeTrackedUsage(
    const FileSystemConnection& aConnection, const FileId& aFileId) {
  const nsLiteralCString trackedUsageQuery =
      "SELECT usage FROM Usages WHERE tracked = TRUE AND handle = :handle "
      ");"_ns;

  QM_TRY_UNWRAP(ResultStatement stmt,
                ResultStatement::Create(aConnection, trackedUsageQuery));
  QM_TRY(QM_TO_RESULT(stmt.BindFileIdByName("handle"_ns, aFileId)));

  QM_TRY_UNWRAP(const bool moreResults, stmt.ExecuteStep());
  if (!moreResults) {
    return Maybe<Usage>(Nothing());
  }

  QM_TRY_UNWRAP(Usage trackedUsage, stmt.GetUsageByColumn(/* Column */ 0u));

  return Some(trackedUsage);
}

Result<bool, nsresult> ScanTrackedFiles(
    const FileSystemConnection& aConnection,
    const FileSystemFileManager& aFileManager) {
  QM_TRY_INSPECT(const nsTArray<FileId>& trackedFiles,
                 GetTrackedFiles(aConnection).mapErr(toNSResult));

  bool ok = true;
  for (const auto& fileId : trackedFiles) {
    // On success, tracked is set to false, otherwise its value is kept (= true)
    QM_WARNONLY_TRY(MOZ_TO_RESULT(UpdateUsageUnsetTracked(
                        aConnection, aFileManager, fileId)),
                    [&ok](const auto& /*aRv*/) { ok = false; });
  }

  return ok;
}

Result<Ok, QMResult> DeleteEntry(const FileSystemConnection& aConnection,
                                 const EntryId& aEntryId) {
  // If it's a directory, deleting the handle will cascade
  const nsLiteralCString deleteEntryQuery =
      "DELETE FROM Entries "
      "WHERE handle = :handle "
      ";"_ns;

  QM_TRY_UNWRAP(ResultStatement stmt,
                ResultStatement::Create(aConnection, deleteEntryQuery));

  QM_TRY(QM_TO_RESULT(stmt.BindEntryIdByName("handle"_ns, aEntryId)));

  QM_TRY(QM_TO_RESULT(stmt.Execute()));

  return Ok{};
}

Result<int32_t, QMResult> GetTrackedFilesCount(
    const FileSystemConnection& aConnection) {
  // TODO: We could query the count directly
  QM_TRY_INSPECT(const auto& trackedFiles, GetTrackedFiles(aConnection));

  CheckedInt32 checkedFileCount = trackedFiles.Length();
  QM_TRY(OkIf(checkedFileCount.isValid()),
         Err(QMResult(NS_ERROR_ILLEGAL_VALUE)));

  return checkedFileCount.value();
}

void LogWithFilename(const FileSystemFileManager& aFileManager,
                     const char* aFormat, const FileId& aFileId) {
  if (!LOG_ENABLED()) {
    return;
  }

  QM_TRY_INSPECT(const auto& localFile, aFileManager.GetFile(aFileId), QM_VOID);

  nsAutoString localPath;
  QM_TRY(MOZ_TO_RESULT(localFile->GetPath(localPath)), QM_VOID);
  LOG((aFormat, NS_ConvertUTF16toUTF8(localPath).get()));
}

Result<bool, QMResult> IsAnyDescendantLocked(
    const FileSystemConnection& aConnection,
    const FileSystemDataManager& aDataManager, const EntryId& aEntryId) {
  QM_TRY_UNWRAP(ResultStatement stmt,
                ResultStatement::Create(aConnection, gDescendantsQuery));
  QM_TRY(QM_TO_RESULT(stmt.BindEntryIdByName("handle"_ns, aEntryId)));
  QM_TRY_UNWRAP(bool moreResults, stmt.ExecuteStep());

  while (moreResults) {
    // Works only for version 001
    QM_TRY_INSPECT(const EntryId& entryId,
                   stmt.GetEntryIdByColumn(/* Column */ 0u));

    QM_TRY_UNWRAP(const bool isLocked, aDataManager.IsLocked(entryId), true);
    if (isLocked) {
      return true;
    }

    QM_TRY_UNWRAP(moreResults, stmt.ExecuteStep());
  }

  return false;
}

}  // namespace

FileSystemDatabaseManagerVersion001::FileSystemDatabaseManagerVersion001(
    FileSystemDataManager* aDataManager, FileSystemConnection&& aConnection,
    UniquePtr<FileSystemFileManager>&& aFileManager, const EntryId& aRootEntry)
    : mDataManager(aDataManager),
      mConnection(aConnection),
      mFileManager(std::move(aFileManager)),
      mRootEntry(aRootEntry),
      mClientMetadata(aDataManager->OriginMetadataRef(),
                      quota::Client::FILESYSTEM),
      mFilesOfUnknownUsage(-1) {}

/* static */
nsresult FileSystemDatabaseManagerVersion001::RescanTrackedUsages(
    const FileSystemConnection& aConnection,
    const quota::OriginMetadata& aOriginMetadata) {
  QM_TRY_UNWRAP(UniquePtr<FileSystemFileManager> fileManager,
                data::FileSystemFileManager::CreateFileSystemFileManager(
                    aOriginMetadata));

  QM_TRY_UNWRAP(bool ok, ScanTrackedFiles(aConnection, *fileManager));
  if (ok) {
    return NS_OK;
  }

  // Retry once without explicit delay
  QM_TRY_UNWRAP(ok, ScanTrackedFiles(aConnection, *fileManager));
  if (!ok) {
    return NS_ERROR_UNEXPECTED;
  }

  return NS_OK;
}

/* static */
Result<Usage, QMResult> FileSystemDatabaseManagerVersion001::GetFileUsage(
    const FileSystemConnection& aConnection) {
  const nsLiteralCString sumUsagesQuery = "SELECT sum(usage) FROM Usages;"_ns;

  QM_TRY_UNWRAP(ResultStatement stmt,
                ResultStatement::Create(aConnection, sumUsagesQuery));

  QM_TRY_UNWRAP(const bool moreResults, stmt.ExecuteStep());
  if (!moreResults) {
    return Err(QMResult(NS_ERROR_DOM_FILE_NOT_READABLE_ERR));
  }

  QM_TRY_UNWRAP(Usage totalFiles, stmt.GetUsageByColumn(/* Column */ 0u));

  return totalFiles;
}

nsresult FileSystemDatabaseManagerVersion001::UpdateUsage(
    const FileId& aFileId) {
  // We don't track directories or non-existent files.
  QM_TRY_UNWRAP(bool fileExists, DoesFileIdExist(aFileId).mapErr(toNSResult));
  if (!fileExists) {
    return NS_OK;  // May be deleted before update, no assert
  }

  QM_TRY_UNWRAP(nsCOMPtr<nsIFile> file, mFileManager->GetFile(aFileId));
  MOZ_ASSERT(file);

  Usage fileSize = 0;
  bool exists = false;
  QM_TRY(MOZ_TO_RESULT(file->Exists(&exists)));
  if (exists) {
    QM_TRY(MOZ_TO_RESULT(file->GetFileSize(&fileSize)));
  }

  QM_TRY(MOZ_TO_RESULT(UpdateUsageInDatabase(aFileId, fileSize)));

  return NS_OK;
}

Result<EntryId, QMResult>
FileSystemDatabaseManagerVersion001::GetOrCreateDirectory(
    const FileSystemChildMetadata& aHandle, bool aCreate) {
  MOZ_ASSERT(!aHandle.parentId().IsEmpty());

  const auto& name = aHandle.childName();
  // Belt and suspenders: check here as well as in child.
  if (!IsValidName(name)) {
    return Err(QMResult(NS_ERROR_DOM_TYPE_MISMATCH_ERR));
  }
  MOZ_ASSERT(!(name.IsVoid() || name.IsEmpty()));

  bool exists = true;
  QM_TRY_UNWRAP(exists, DoesFileExist(mConnection, aHandle));

  // By spec, we don't allow a file and a directory
  // to have the same name and parent
  if (exists) {
    return Err(QMResult(NS_ERROR_DOM_TYPE_MISMATCH_ERR));
  }

  QM_TRY_UNWRAP(exists, DoesDirectoryExist(mConnection, aHandle));

  // exists as directory
  if (exists) {
    return FindEntryId(mConnection, aHandle, false);
  }

  if (!aCreate) {
    return Err(QMResult(NS_ERROR_DOM_NOT_FOUND_ERR));
  }

  const nsLiteralCString insertEntryQuery =
      "INSERT OR IGNORE INTO Entries "
      "( handle, parent ) "
      "VALUES "
      "( :handle, :parent ) "
      ";"_ns;

  const nsLiteralCString insertDirectoryQuery =
      "INSERT OR IGNORE INTO Directories "
      "( handle, name ) "
      "VALUES "
      "( :handle, :name ) "
      ";"_ns;

  QM_TRY_INSPECT(const EntryId& entryId, GetEntryId(aHandle));
  MOZ_ASSERT(!entryId.IsEmpty());

  QM_TRY_UNWRAP(auto transaction, StartedTransaction::Create(mConnection));

  {
    QM_TRY_UNWRAP(ResultStatement stmt,
                  ResultStatement::Create(mConnection, insertEntryQuery));
    QM_TRY(QM_TO_RESULT(stmt.BindEntryIdByName("handle"_ns, entryId)));
    QM_TRY(
        QM_TO_RESULT(stmt.BindEntryIdByName("parent"_ns, aHandle.parentId())));
    QM_TRY(QM_TO_RESULT(stmt.Execute()));
  }

  {
    QM_TRY_UNWRAP(ResultStatement stmt,
                  ResultStatement::Create(mConnection, insertDirectoryQuery));
    QM_TRY(QM_TO_RESULT(stmt.BindEntryIdByName("handle"_ns, entryId)));
    QM_TRY(QM_TO_RESULT(stmt.BindNameByName("name"_ns, name)));
    QM_TRY(QM_TO_RESULT(stmt.Execute()));
  }

  QM_TRY(QM_TO_RESULT(transaction.Commit()));

  QM_TRY_UNWRAP(DebugOnly<bool> doesItExistNow,
                DoesDirectoryExist(mConnection, aHandle));
  MOZ_ASSERT(doesItExistNow);

  return entryId;
}

Result<EntryId, QMResult> FileSystemDatabaseManagerVersion001::GetOrCreateFile(
    const FileSystemChildMetadata& aHandle, bool aCreate) {
  MOZ_ASSERT(!aHandle.parentId().IsEmpty());

  const auto& name = aHandle.childName();
  // Belt and suspenders: check here as well as in child.
  if (!IsValidName(name)) {
    return Err(QMResult(NS_ERROR_DOM_TYPE_MISMATCH_ERR));
  }
  MOZ_ASSERT(!(name.IsVoid() || name.IsEmpty()));

  QM_TRY_UNWRAP(bool exists, DoesDirectoryExist(mConnection, aHandle));

  // By spec, we don't allow a file and a directory
  // to have the same name and parent
  QM_TRY(OkIf(!exists), Err(QMResult(NS_ERROR_DOM_TYPE_MISMATCH_ERR)));

  QM_TRY_UNWRAP(exists, DoesFileExist(mConnection, aHandle));

  if (exists) {
    QM_TRY_RETURN(FindEntryId(mConnection, aHandle, /* aIsFile */ true));
  }

  if (!aCreate) {
    return Err(QMResult(NS_ERROR_DOM_NOT_FOUND_ERR));
  }

  const nsLiteralCString insertEntryQuery =
      "INSERT INTO Entries "
      "( handle, parent ) "
      "VALUES "
      "( :handle, :parent ) "
      ";"_ns;

  const nsLiteralCString insertFileQuery =
      "INSERT INTO Files "
      "( handle, type, name ) "
      "VALUES "
      "( :handle, :type, :name ) "
      ";"_ns;

  QM_TRY_INSPECT(const EntryId& entryId, GetEntryId(aHandle));
  MOZ_ASSERT(!entryId.IsEmpty());

  const ContentType type = DetermineContentType(name);

  QM_TRY_UNWRAP(auto transaction, StartedTransaction::Create(mConnection));

  {
    QM_TRY_UNWRAP(ResultStatement stmt,
                  ResultStatement::Create(mConnection, insertEntryQuery));
    QM_TRY(QM_TO_RESULT(stmt.BindEntryIdByName("handle"_ns, entryId)));
    QM_TRY(
        QM_TO_RESULT(stmt.BindEntryIdByName("parent"_ns, aHandle.parentId())));
    QM_TRY(QM_TO_RESULT(stmt.Execute()), QM_PROPAGATE,
           ([this, &aHandle](const auto& aRv) {
             QM_TRY_UNWRAP(bool parentExists,
                           DoesDirectoryExist(mConnection, aHandle.parentId()),
                           QM_VOID);
             QM_TRY(OkIf(parentExists), QM_VOID);
           }));
  }

  {
    QM_TRY_UNWRAP(ResultStatement stmt,
                  ResultStatement::Create(mConnection, insertFileQuery));
    QM_TRY(QM_TO_RESULT(stmt.BindEntryIdByName("handle"_ns, entryId)));
    QM_TRY(QM_TO_RESULT(stmt.BindContentTypeByName("type"_ns, type)));
    QM_TRY(QM_TO_RESULT(stmt.BindNameByName("name"_ns, name)));
    QM_TRY(QM_TO_RESULT(stmt.Execute()));
  }

  QM_TRY(QM_TO_RESULT(transaction.Commit()));

  return entryId;
}

nsresult FileSystemDatabaseManagerVersion001::GetFile(
    const EntryId& aEntryId, const FileId& aFileId, const FileMode& aMode,
    ContentType& aType, TimeStamp& lastModifiedMilliSeconds,
    nsTArray<Name>& aPath, nsCOMPtr<nsIFile>& aFile) const {
  MOZ_ASSERT(!aFileId.IsEmpty());
  MOZ_ASSERT(aMode == FileMode::EXCLUSIVE);

  const FileSystemEntryPair endPoints(mRootEntry, aEntryId);
  QM_TRY_UNWRAP(aPath, ResolveReversedPath(mConnection, endPoints));
  if (aPath.IsEmpty()) {
    return NS_ERROR_DOM_NOT_FOUND_ERR;
  }

  QM_TRY(MOZ_TO_RESULT(GetFileAttributes(mConnection, aEntryId, aType)));
  QM_TRY_UNWRAP(aFile, mFileManager->GetOrCreateFile(aFileId));

  PRTime lastModTime = 0;
  QM_TRY(MOZ_TO_RESULT(aFile->GetLastModifiedTime(&lastModTime)));
  lastModifiedMilliSeconds = static_cast<TimeStamp>(lastModTime);

  aPath.Reverse();

  return NS_OK;
}

Result<FileSystemDirectoryListing, QMResult>
FileSystemDatabaseManagerVersion001::GetDirectoryEntries(
    const EntryId& aParent, PageNumber aPage) const {
  // TODO: Offset is reported to have bad performance - see Bug 1780386.
  const nsCString directoriesQuery =
      "SELECT Dirs.handle, Dirs.name "
      "FROM Directories AS Dirs "
      "INNER JOIN ( "
      "SELECT handle "
      "FROM Entries "
      "WHERE parent = :parent "
      "LIMIT :pageSize "
      "OFFSET :pageOffset ) "
      "AS Ents "
      "ON Dirs.handle = Ents.handle "
      ";"_ns;
  const nsCString filesQuery =
      "SELECT Files.handle, Files.name "
      "FROM Files "
      "INNER JOIN ( "
      "SELECT handle "
      "FROM Entries "
      "WHERE parent = :parent "
      "LIMIT :pageSize "
      "OFFSET :pageOffset ) "
      "AS Ents "
      "ON Files.handle = Ents.handle "
      ";"_ns;

  FileSystemDirectoryListing entries;
  QM_TRY(
      QM_TO_RESULT(GetEntries(mConnection, directoriesQuery, aParent, aPage,
                              /* aDirectory */ true, entries.directories())));

  QM_TRY(QM_TO_RESULT(GetEntries(mConnection, filesQuery, aParent, aPage,
                                 /* aDirectory */ false, entries.files())));

  return entries;
}

Result<bool, QMResult> FileSystemDatabaseManagerVersion001::RemoveDirectory(
    const FileSystemChildMetadata& aHandle, bool aRecursive) {
  MOZ_ASSERT(!aHandle.parentId().IsEmpty());

  if (aHandle.childName().IsEmpty()) {
    return false;
  }

  DebugOnly<Name> name = aHandle.childName();
  MOZ_ASSERT(!name.inspect().IsVoid());

  QM_TRY_UNWRAP(bool exists, DoesDirectoryExist(mConnection, aHandle));

  if (!exists) {
    return false;
  }

  // At this point, entry exists and is a directory.
  QM_TRY_UNWRAP(EntryId entryId, FindEntryId(mConnection, aHandle, false));
  MOZ_ASSERT(!entryId.IsEmpty());

  QM_TRY_UNWRAP(bool isEmpty, IsDirectoryEmpty(mConnection, entryId));

  MOZ_ASSERT(mDataManager);
  QM_TRY_UNWRAP(const bool isLocked,
                IsAnyDescendantLocked(mConnection, *mDataManager, entryId));

  QM_TRY(OkIf(!isLocked),
         Err(QMResult(NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR)));

  if (!aRecursive && !isEmpty) {
    return Err(QMResult(NS_ERROR_DOM_INVALID_MODIFICATION_ERR));
  }

  QM_TRY_UNWRAP(Usage usage, GetUsagesOfDescendants(entryId));

  QM_TRY_INSPECT(const nsTArray<FileId>& descendants,
                 FindFilesUnderEntry(entryId));

  nsTArray<FileId> failedRemovals;
  QM_TRY_UNWRAP(DebugOnly<Usage> removedUsage,
                mFileManager->RemoveFiles(descendants, failedRemovals));

  // Usage is for the current main file but we remove temporary files too.
  MOZ_ASSERT_IF(failedRemovals.IsEmpty() && (0 == mFilesOfUnknownUsage),
                usage <= removedUsage);

  TryRemoveDuringIdleMaintenance(failedRemovals);

  auto isInFailedRemovals = [&failedRemovals](const auto& aFileId) {
    return failedRemovals.cend() !=
           std::find_if(failedRemovals.cbegin(), failedRemovals.cend(),
                        [&aFileId](const auto& aFailedRemoval) {
                          return aFileId == aFailedRemoval;
                        });
  };

  for (const auto& fileId : descendants) {
    if (!isInFailedRemovals(fileId)) {
      QM_WARNONLY_TRY(QM_TO_RESULT(RemoveFileId(fileId)));
    }
  }

  if (usage > 0) {  // Performance!
    DecreaseCachedQuotaUsage(usage);
  }

  QM_TRY(DeleteEntry(mConnection, entryId));

  return true;
}

Result<bool, QMResult> FileSystemDatabaseManagerVersion001::RemoveFile(
    const FileSystemChildMetadata& aHandle) {
  MOZ_ASSERT(!aHandle.parentId().IsEmpty());

  if (aHandle.childName().IsEmpty()) {
    return false;
  }

  DebugOnly<Name> name = aHandle.childName();
  MOZ_ASSERT(!name.inspect().IsVoid());

  // Make it more evident that we won't remove directories
  QM_TRY_UNWRAP(bool exists, DoesFileExist(mConnection, aHandle));

  if (!exists) {
    return false;
  }

  // At this point, entry exists and is a file
  QM_TRY_UNWRAP(EntryId entryId, FindEntryId(mConnection, aHandle, true));
  MOZ_ASSERT(!entryId.IsEmpty());

  // XXX This code assumes the spec question is resolved to state
  // removing an in-use file should fail.  If it shouldn't fail, we need to
  // do something to neuter all the extant FileAccessHandles/WritableFileStreams
  // that reference it
  QM_TRY_UNWRAP(const bool isLocked, mDataManager->IsLocked(entryId));
  if (isLocked) {
    LOG(("Trying to remove in-use file"));
    return Err(QMResult(NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR));
  }

  QM_TRY_INSPECT(const nsTArray<FileId>& diskItems,
                 FindFilesUnderEntry(entryId));

  QM_TRY_UNWRAP(Usage usage, GetUsagesOfDescendants(entryId));

  nsTArray<FileId> failedRemovals;
  QM_TRY_UNWRAP(DebugOnly<Usage> removedUsage,
                mFileManager->RemoveFiles(diskItems, failedRemovals));

  // We only check the most common case. This can fail spuriously if an external
  // application writes to the file, or OS reports zero size due to corruption.
  MOZ_ASSERT_IF(failedRemovals.IsEmpty() && (0 == mFilesOfUnknownUsage),
                usage == removedUsage);

  TryRemoveDuringIdleMaintenance(failedRemovals);

  auto isInFailedRemovals = [&failedRemovals](const auto& aFileId) {
    return failedRemovals.cend() !=
           std::find_if(failedRemovals.cbegin(), failedRemovals.cend(),
                        [&aFileId](const auto& aFailedRemoval) {
                          return aFileId == aFailedRemoval;
                        });
  };

  for (const auto& fileId : diskItems) {
    if (!isInFailedRemovals(fileId)) {
      QM_WARNONLY_TRY(QM_TO_RESULT(RemoveFileId(fileId)));
    }
  }

  if (usage > 0) {  // Performance!
    DecreaseCachedQuotaUsage(usage);
  }

  QM_TRY(DeleteEntry(mConnection, entryId));

  return true;
}

Result<EntryId, QMResult> FileSystemDatabaseManagerVersion001::RenameEntry(
    const FileSystemEntryMetadata& aHandle, const Name& aNewName) {
  const auto& entryId = aHandle.entryId();

  // Can't rename root
  if (mRootEntry == entryId) {
    return Err(QMResult(NS_ERROR_DOM_NOT_FOUND_ERR));
  }

  // Verify the source exists
  QM_TRY_UNWRAP(bool isFile, IsFile(mConnection, entryId),
                Err(QMResult(NS_ERROR_DOM_NOT_FOUND_ERR)));

  // Are we actually renaming?
  if (aHandle.entryName() == aNewName) {
    return entryId;
  }

  QM_TRY(QM_TO_RESULT(PrepareRenameEntry(mConnection, mDataManager, aHandle,
                                         aNewName, isFile)));

  QM_TRY_UNWRAP(auto transaction, StartedTransaction::Create(mConnection));

  if (isFile) {
    const ContentType type = DetermineContentType(aNewName);
    QM_TRY(
        QM_TO_RESULT(PerformRenameFile(mConnection, aHandle, aNewName, type)));
  } else {
    QM_TRY(
        QM_TO_RESULT(PerformRenameDirectory(mConnection, aHandle, aNewName)));
  }

  QM_TRY(QM_TO_RESULT(transaction.Commit()));

  return entryId;
}

Result<EntryId, QMResult> FileSystemDatabaseManagerVersion001::MoveEntry(
    const FileSystemEntryMetadata& aHandle,
    const FileSystemChildMetadata& aNewDesignation) {
  const auto& entryId = aHandle.entryId();
  MOZ_ASSERT(!entryId.IsEmpty());

  if (mRootEntry == entryId) {
    return Err(QMResult(NS_ERROR_DOM_NOT_FOUND_ERR));
  }

  // Verify the source exists
  QM_TRY_UNWRAP(bool isFile, IsFile(mConnection, entryId),
                Err(QMResult(NS_ERROR_DOM_NOT_FOUND_ERR)));

  // If the rename doesn't change the name or directory, just return success.
  // XXX Needs to be added to the spec
  QM_WARNONLY_TRY_UNWRAP(Maybe<bool> maybeSame,
                         IsSame(mConnection, aHandle, aNewDesignation, isFile));
  if (maybeSame && maybeSame.value()) {
    return entryId;
  }

  QM_TRY(QM_TO_RESULT(PrepareMoveEntry(mConnection, mDataManager, aHandle,
                                       aNewDesignation, isFile)));

  const nsLiteralCString updateEntryParentQuery =
      "UPDATE Entries "
      "SET parent = :parent "
      "WHERE handle = :handle "
      ";"_ns;

  QM_TRY_UNWRAP(auto transaction, StartedTransaction::Create(mConnection));

  {
    // We always change the parent because it's simpler than checking if the
    // parent needs to be changed
    QM_TRY_UNWRAP(ResultStatement stmt,
                  ResultStatement::Create(mConnection, updateEntryParentQuery));
    QM_TRY(QM_TO_RESULT(
        stmt.BindEntryIdByName("parent"_ns, aNewDesignation.parentId())));
    QM_TRY(QM_TO_RESULT(stmt.BindEntryIdByName("handle"_ns, entryId)));
    QM_TRY(QM_TO_RESULT(stmt.Execute()));
  }

  const Name& newName = aNewDesignation.childName();

  // Are we actually renaming?
  if (aHandle.entryName() == newName) {
    QM_TRY(QM_TO_RESULT(transaction.Commit()));

    return entryId;
  }

  if (isFile) {
    const ContentType type = DetermineContentType(newName);
    QM_TRY(
        QM_TO_RESULT(PerformRenameFile(mConnection, aHandle, newName, type)));
  } else {
    QM_TRY(QM_TO_RESULT(PerformRenameDirectory(mConnection, aHandle, newName)));
  }

  QM_TRY(QM_TO_RESULT(transaction.Commit()));

  return entryId;
}

Result<Path, QMResult> FileSystemDatabaseManagerVersion001::Resolve(
    const FileSystemEntryPair& aEndpoints) const {
  QM_TRY_UNWRAP(Path path, ResolveReversedPath(mConnection, aEndpoints));
  // Note: if not an ancestor, returns null

  path.Reverse();
  return path;
}

Result<EntryId, QMResult> FileSystemDatabaseManagerVersion001::GetEntryId(
    const FileSystemChildMetadata& aHandle) const {
  return GetUniqueEntryId(mConnection, aHandle);
}

Result<EntryId, QMResult> FileSystemDatabaseManagerVersion001::GetEntryId(
    const FileId& aFileId) const {
  return aFileId.Value();
}

Result<FileId, QMResult> FileSystemDatabaseManagerVersion001::EnsureFileId(
    const EntryId& aEntryId) {
  return FileId(aEntryId);
}

Result<FileId, QMResult>
FileSystemDatabaseManagerVersion001::EnsureTemporaryFileId(
    const EntryId& aEntryId) {
  return FileId(aEntryId);
}

Result<FileId, QMResult> FileSystemDatabaseManagerVersion001::GetFileId(
    const EntryId& aEntryId) const {
  return FileId(aEntryId);
}

nsresult FileSystemDatabaseManagerVersion001::MergeFileId(
    const EntryId& /* aEntryId */, const FileId& /* aFileId */,
    bool /* aAbort */) {
  // Version 001 should always use exclusive mode and not get here.
  return NS_ERROR_NOT_IMPLEMENTED;
}

void FileSystemDatabaseManagerVersion001::Close() { mConnection->Close(); }

nsresult FileSystemDatabaseManagerVersion001::BeginUsageTracking(
    const FileId& aFileId) {
  MOZ_ASSERT(!aFileId.IsEmpty());

  // If file is already tracked but we cannot read its size, error.
  // If file does not exist, this will succeed because usage is zero.
  QM_TRY(EnsureUsageIsKnown(aFileId));

  // If file does not exist, set usage tracking to true fails with
  // file not found error.
  QM_TRY(MOZ_TO_RESULT(SetUsageTracking(aFileId, true)));

  return NS_OK;
}

nsresult FileSystemDatabaseManagerVersion001::EndUsageTracking(
    const FileId& aFileId) {
  // This is expected to fail only if database is unreachable.
  QM_TRY(MOZ_TO_RESULT(SetUsageTracking(aFileId, false)));

  return NS_OK;
}

Result<bool, QMResult> FileSystemDatabaseManagerVersion001::DoesFileIdExist(
    const FileId& aFileId) const {
  MOZ_ASSERT(!aFileId.IsEmpty());

  QM_TRY_RETURN(DoesFileExist(mConnection, aFileId.Value()));
}

nsresult FileSystemDatabaseManagerVersion001::RemoveFileId(
    const FileId& /* aFileId */) {
  return NS_OK;
}

/**
 * @brief Get the sum of usages for all file descendants of a directory entry.
 * We obtain the value with one query, which is presumably better than having a
 * separate query for each individual descendant.
 * TODO: Check if this is true
 *
 * Please see GetFileUsage documentation for why we use the latest recorded
 * value from the database instead of the file size property from the disk.
 */
Result<Usage, QMResult>
FileSystemDatabaseManagerVersion001::GetUsagesOfDescendants(
    const EntryId& aEntryId) const {
  const nsLiteralCString descendantUsagesQuery =
      "WITH RECURSIVE traceChildren(handle, parent) AS ( "
      "SELECT handle, parent "
      "FROM Entries "
      "WHERE handle=:handle "
      "UNION "
      "SELECT Entries.handle, Entries.parent FROM traceChildren, Entries "
      "WHERE traceChildren.handle=Entries.parent ) "
      "SELECT sum(Usages.usage) "
      "FROM traceChildren INNER JOIN Usages "
      "USING(handle) "
      ";"_ns;

  QM_TRY_UNWRAP(ResultStatement stmt,
                ResultStatement::Create(mConnection, descendantUsagesQuery));
  QM_TRY(QM_TO_RESULT(stmt.BindEntryIdByName("handle"_ns, aEntryId)));
  QM_TRY_UNWRAP(const bool moreResults, stmt.ExecuteStep());
  if (!moreResults) {
    return 0;
  }

  QM_TRY_RETURN(stmt.GetUsageByColumn(/* Column */ 0u));
}

Result<nsTArray<FileId>, QMResult>
FileSystemDatabaseManagerVersion001::FindFilesUnderEntry(
    const EntryId& aEntryId) const {
  nsTArray<FileId> descendants;
  {
    QM_TRY_UNWRAP(ResultStatement stmt,
                  ResultStatement::Create(mConnection, gDescendantsQuery));
    QM_TRY(QM_TO_RESULT(stmt.BindEntryIdByName("handle"_ns, aEntryId)));
    QM_TRY_UNWRAP(bool moreResults, stmt.ExecuteStep());

    while (moreResults) {
      // Works only for version 001
      QM_TRY_INSPECT(const FileId& fileId,
                     stmt.GetFileIdByColumn(/* Column */ 0u));

      descendants.AppendElement(fileId);

      QM_TRY_UNWRAP(moreResults, stmt.ExecuteStep());
    }
  }

  return descendants;
}

nsresult FileSystemDatabaseManagerVersion001::SetUsageTracking(
    const FileId& aFileId, bool aTracked) {
  auto onMissingFile = [this, &aFileId](const auto& aRv) {
    // Usages constrains entryId to be present in Files
    MOZ_ASSERT(NS_ERROR_STORAGE_CONSTRAINT == ToNSResult(aRv));

    // The query *should* fail if and only if file does not exist
    QM_TRY_UNWRAP(DebugOnly<bool> fileExists, DoesFileIdExist(aFileId),
                  QM_VOID);
    MOZ_ASSERT(!fileExists);
  };

  return SetUsageTrackingImpl(mConnection, aFileId, aTracked, onMissingFile);
}

nsresult FileSystemDatabaseManagerVersion001::UpdateUsageInDatabase(
    const FileId& aFileId, Usage aNewDiskUsage) {
  const nsLiteralCString updateUsageQuery =
      "INSERT INTO Usages "
      "( handle, usage ) "
      "VALUES "
      "( :handle, :usage ) "
      "ON CONFLICT(handle) DO "
      "UPDATE SET usage = excluded.usage "
      ";"_ns;

  QM_TRY_UNWRAP(ResultStatement stmt,
                ResultStatement::Create(mConnection, updateUsageQuery));
  QM_TRY(MOZ_TO_RESULT(stmt.BindUsageByName("usage"_ns, aNewDiskUsage)));
  QM_TRY(MOZ_TO_RESULT(stmt.BindFileIdByName("handle"_ns, aFileId)));
  QM_TRY(MOZ_TO_RESULT(stmt.Execute()));

  return NS_OK;
}

Result<Ok, QMResult> FileSystemDatabaseManagerVersion001::EnsureUsageIsKnown(
    const FileId& aFileId) {
  if (mFilesOfUnknownUsage < 0) {  // Lazy initialization
    QM_TRY_UNWRAP(mFilesOfUnknownUsage, GetTrackedFilesCount(mConnection));
  }

  if (mFilesOfUnknownUsage == 0) {
    return Ok{};
  }

  QM_TRY_UNWRAP(Maybe<Usage> oldUsage,
                GetMaybeTrackedUsage(mConnection, aFileId));
  if (oldUsage.isNothing()) {
    return Ok{};  // Usage is 0 or it was successfully recorded at unlocking.
  }

  auto quotaCacheUpdate = [this, &aFileId,
                           oldSize = oldUsage.value()](Usage aNewSize) {
    return UpdateCachedQuotaUsage(aFileId, oldSize, aNewSize);
  };

  static const nsLiteralCString updateUsagesKeepTrackedQuery =
      "UPDATE Usages SET usage = :usage WHERE handle = :handle;"_ns;

  // If usage update fails, we log an error and keep things the way they were.
  QM_TRY(QM_TO_RESULT(UpdateUsageForFileEntry(
             mConnection, *mFileManager, aFileId, updateUsagesKeepTrackedQuery,
             std::move(quotaCacheUpdate))),
         Err(QMResult(NS_ERROR_DOM_FILE_NOT_READABLE_ERR)),
         ([this, &aFileId](const auto& /*aRv*/) {
           LogWithFilename(*mFileManager, "Could not read the size of file %s",
                           aFileId);
         }));

  // We read and updated the quota usage successfully.
  --mFilesOfUnknownUsage;
  MOZ_ASSERT(mFilesOfUnknownUsage >= 0);

  return Ok{};
}

void FileSystemDatabaseManagerVersion001::DecreaseCachedQuotaUsage(
    int64_t aDelta) {
  quota::QuotaManager* quotaManager = quota::QuotaManager::Get();
  MOZ_ASSERT(quotaManager);

  quotaManager->DecreaseUsageForClient(mClientMetadata, aDelta);
}

nsresult FileSystemDatabaseManagerVersion001::UpdateCachedQuotaUsage(
    const FileId& aFileId, Usage aOldUsage, Usage aNewUsage) const {
  quota::QuotaManager* quotaManager = quota::QuotaManager::Get();
  MOZ_ASSERT(quotaManager);

  QM_TRY_UNWRAP(nsCOMPtr<nsIFile> fileObj,
                mFileManager->GetFile(aFileId).mapErr(toNSResult));

  RefPtr<quota::QuotaObject> quotaObject = quotaManager->GetQuotaObject(
      quota::PERSISTENCE_TYPE_DEFAULT, mClientMetadata,
      quota::Client::FILESYSTEM, fileObj, aOldUsage);
  MOZ_ASSERT(quotaObject);

  QM_TRY(OkIf(quotaObject->MaybeUpdateSize(aNewUsage, /* aTruncate */ true)),
         NS_ERROR_FILE_NO_DEVICE_SPACE);

  return NS_OK;
}

nsresult FileSystemDatabaseManagerVersion001::ClearDestinationIfNotLocked(
    const FileSystemConnection& aConnection,
    const FileSystemDataManager* const aDataManager,
    const FileSystemEntryMetadata& aHandle,
    const FileSystemChildMetadata& aNewDesignation) {
  // If the destination file exists, fail explicitly.  Spec author plans to
  // revise the spec
  QM_TRY_UNWRAP(bool exists, DoesFileExist(aConnection, aNewDesignation));
  if (exists) {
    QM_TRY_INSPECT(const EntryId& destId,
                   FindEntryId(aConnection, aNewDesignation, true));
    QM_TRY_UNWRAP(const bool isLocked, aDataManager->IsLocked(destId));
    if (isLocked) {
      LOG(("Trying to overwrite in-use file"));
      return NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR;
    }

    QM_TRY_UNWRAP(DebugOnly<bool> isRemoved, RemoveFile(aNewDesignation));
    MOZ_ASSERT(isRemoved);
  } else {
    QM_TRY_UNWRAP(exists, DoesDirectoryExist(aConnection, aNewDesignation));
    if (exists) {
      // Fails if directory contains locked files, otherwise total wipeout
      QM_TRY_UNWRAP(DebugOnly<bool> isRemoved,
                    MOZ_TO_RESULT(RemoveDirectory(aNewDesignation,
                                                  /* recursive */ true)));
      MOZ_ASSERT(isRemoved);
    }
  }

  return NS_OK;
}

nsresult FileSystemDatabaseManagerVersion001::PrepareRenameEntry(
    const FileSystemConnection& aConnection,
    const FileSystemDataManager* const aDataManager,
    const FileSystemEntryMetadata& aHandle, const Name& aNewName,
    bool aIsFile) {
  const EntryId& entryId = aHandle.entryId();

  // At this point, entry exists
  if (aIsFile) {
    QM_TRY_UNWRAP(const bool isLocked, aDataManager->IsLocked(entryId));
    if (isLocked) {
      LOG(("Trying to move in-use file"));
      return NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR;
    }
  }

  // If the destination file exists, fail explicitly.
  FileSystemChildMetadata destination;
  QM_TRY_UNWRAP(EntryId parent, FindParent(mConnection, entryId));
  destination.parentId() = parent;
  destination.childName() = aNewName;

  QM_TRY(MOZ_TO_RESULT(ClearDestinationIfNotLocked(mConnection, mDataManager,
                                                   aHandle, destination)));

  return NS_OK;
}

nsresult FileSystemDatabaseManagerVersion001::PrepareMoveEntry(
    const FileSystemConnection& aConnection,
    const FileSystemDataManager* const aDataManager,
    const FileSystemEntryMetadata& aHandle,
    const FileSystemChildMetadata& aNewDesignation, bool aIsFile) {
  const EntryId& entryId = aHandle.entryId();

  // At this point, entry exists
  if (aIsFile) {
    QM_TRY_UNWRAP(const bool isLocked, aDataManager->IsLocked(entryId));
    if (isLocked) {
      LOG(("Trying to move in-use file"));
      return NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR;
    }
  }

  QM_TRY(QM_TO_RESULT(ClearDestinationIfNotLocked(aConnection, aDataManager,
                                                  aHandle, aNewDesignation)));

  // XXX: This should be before clearing the target

  // To prevent cyclic paths, we check that there is no path from
  // the item to be moved to the destination folder.
  QM_TRY_UNWRAP(const bool isDestinationUnderSelf,
                IsAncestor(aConnection, {entryId, aNewDesignation.parentId()}));
  if (isDestinationUnderSelf) {
    return NS_ERROR_DOM_INVALID_MODIFICATION_ERR;
  }

  return NS_OK;
}

/**
 * Free functions
 */

Result<bool, QMResult> ApplyEntryExistsQuery(
    const FileSystemConnection& aConnection, const nsACString& aQuery,
    const FileSystemChildMetadata& aHandle) {
  QM_TRY_UNWRAP(ResultStatement stmt,
                ResultStatement::Create(aConnection, aQuery));
  QM_TRY(QM_TO_RESULT(stmt.BindEntryIdByName("parent"_ns, aHandle.parentId())));
  QM_TRY(QM_TO_RESULT(stmt.BindNameByName("name"_ns, aHandle.childName())));

  return stmt.YesOrNoQuery();
}

Result<bool, QMResult> ApplyEntryExistsQuery(
    const FileSystemConnection& aConnection, const nsACString& aQuery,
    const EntryId& aEntry) {
  QM_TRY_UNWRAP(ResultStatement stmt,
                ResultStatement::Create(aConnection, aQuery));
  QM_TRY(QM_TO_RESULT(stmt.BindEntryIdByName("handle"_ns, aEntry)));

  return stmt.YesOrNoQuery();
}

Result<bool, QMResult> DoesFileExist(const FileSystemConnection& aConnection,
                                     const EntryId& aEntryId) {
  MOZ_ASSERT(!aEntryId.IsEmpty());

  const nsCString existsQuery =
      "SELECT EXISTS "
      "(SELECT 1 FROM Files WHERE handle = :handle ) "
      ";"_ns;

  QM_TRY_RETURN(ApplyEntryExistsQuery(aConnection, existsQuery, aEntryId));
}

Result<bool, QMResult> IsFile(const FileSystemConnection& aConnection,
                              const EntryId& aEntryId) {
  QM_TRY_UNWRAP(bool exists, DoesFileExist(aConnection, aEntryId));
  if (exists) {
    return true;
  }

  QM_TRY_UNWRAP(exists, DoesDirectoryExist(aConnection, aEntryId));
  if (exists) {
    return false;
  }

  // Doesn't exist
  return Err(QMResult(NS_ERROR_DOM_NOT_FOUND_ERR));
}

Result<EntryId, QMResult> FindEntryId(const FileSystemConnection& aConnection,
                                      const FileSystemChildMetadata& aHandle,
                                      bool aIsFile) {
  const nsCString aDirectoryQuery =
      "SELECT Entries.handle FROM Directories "
      "INNER JOIN Entries USING (handle) "
      "WHERE Directories.name = :name AND Entries.parent = :parent "
      ";"_ns;

  const nsCString aFileQuery =
      "SELECT Entries.handle FROM Files INNER JOIN Entries USING (handle) "
      "WHERE Files.name = :name AND Entries.parent = :parent "
      ";"_ns;

  QM_TRY_UNWRAP(ResultStatement stmt,
                ResultStatement::Create(
                    aConnection, aIsFile ? aFileQuery : aDirectoryQuery));
  QM_TRY(QM_TO_RESULT(stmt.BindEntryIdByName("parent"_ns, aHandle.parentId())));
  QM_TRY(QM_TO_RESULT(stmt.BindNameByName("name"_ns, aHandle.childName())));
  QM_TRY_UNWRAP(bool moreResults, stmt.ExecuteStep());

  if (!moreResults) {
    return Err(QMResult(NS_ERROR_DOM_NOT_FOUND_ERR));
  }

  QM_TRY_UNWRAP(EntryId entryId, stmt.GetEntryIdByColumn(/* Column */ 0u));

  return entryId;
}

Result<EntryId, QMResult> FindParent(const FileSystemConnection& aConnection,
                                     const EntryId& aEntryId) {
  const nsCString aParentQuery =
      "SELECT handle FROM Entries "
      "WHERE handle IN ( "
      "SELECT parent FROM Entries WHERE "
      "handle = :entryId ) "
      ";"_ns;

  QM_TRY_UNWRAP(ResultStatement stmt,
                ResultStatement::Create(aConnection, aParentQuery));
  QM_TRY(QM_TO_RESULT(stmt.BindEntryIdByName("entryId"_ns, aEntryId)));
  QM_TRY_UNWRAP(bool moreResults, stmt.ExecuteStep());

  if (!moreResults) {
    return Err(QMResult(NS_ERROR_DOM_NOT_FOUND_ERR));
  }

  QM_TRY_UNWRAP(EntryId parentId, stmt.GetEntryIdByColumn(/* Column */ 0u));
  return parentId;
}

Result<bool, QMResult> IsSame(const FileSystemConnection& aConnection,
                              const FileSystemEntryMetadata& aHandle,
                              const FileSystemChildMetadata& aNewHandle,
                              bool aIsFile) {
  MOZ_ASSERT(!aNewHandle.parentId().IsEmpty());

  // Typically aNewHandle does not exist which is not an error
  QM_TRY_RETURN(QM_OR_ELSE_LOG_VERBOSE_IF(
      // Expression.
      FindEntryId(aConnection, aNewHandle, aIsFile)
          .map([&aHandle](const EntryId& entryId) {
            return entryId == aHandle.entryId();
          }),
      // Predicate.
      IsSpecificError<NS_ERROR_DOM_NOT_FOUND_ERR>,
      // Fallback.
      ErrToOkFromQMResult<false>));
}

Result<Path, QMResult> ResolveReversedPath(
    const FileSystemConnection& aConnection,
    const FileSystemEntryPair& aEndpoints) {
  const nsLiteralCString pathQuery =
      "WITH RECURSIVE followPath(handle, parent) AS ( "
      "SELECT handle, parent "
      "FROM Entries "
      "WHERE handle=:entryId "
      "UNION "
      "SELECT Entries.handle, Entries.parent FROM followPath, Entries "
      "WHERE followPath.parent=Entries.handle ) "
      "SELECT COALESCE(Directories.name, Files.name), handle "
      "FROM followPath "
      "LEFT JOIN Directories USING(handle) "
      "LEFT JOIN Files USING(handle);"_ns;

  QM_TRY_UNWRAP(ResultStatement stmt,
                ResultStatement::Create(aConnection, pathQuery));
  QM_TRY(
      QM_TO_RESULT(stmt.BindEntryIdByName("entryId"_ns, aEndpoints.childId())));
  QM_TRY_UNWRAP(bool moreResults, stmt.ExecuteStep());

  Path pathResult;
  while (moreResults) {
    QM_TRY_UNWRAP(Name entryName, stmt.GetNameByColumn(/* Column */ 0u));
    QM_TRY_UNWRAP(EntryId entryId, stmt.GetEntryIdByColumn(/* Column */ 1u));

    if (aEndpoints.parentId() == entryId) {
      return pathResult;
    }
    pathResult.AppendElement(entryName);

    QM_TRY_UNWRAP(moreResults, stmt.ExecuteStep());
  }

  // Spec wants us to return 'null' for not-an-ancestor case
  pathResult.Clear();
  return pathResult;
}

nsresult GetFileAttributes(const FileSystemConnection& aConnection,
                           const EntryId& aEntryId, ContentType& aType) {
  const nsLiteralCString getFileLocation =
      "SELECT type FROM Files INNER JOIN Entries USING(handle) "
      "WHERE handle = :entryId "
      ";"_ns;

  QM_TRY_UNWRAP(ResultStatement stmt,
                ResultStatement::Create(aConnection, getFileLocation));
  QM_TRY(QM_TO_RESULT(stmt.BindEntryIdByName("entryId"_ns, aEntryId)));
  QM_TRY_UNWRAP(bool hasEntries, stmt.ExecuteStep());

  // Type is an optional attribute
  if (!hasEntries || stmt.IsNullByColumn(/* Column */ 0u)) {
    return NS_OK;
  }

  QM_TRY_UNWRAP(aType, stmt.GetContentTypeByColumn(/* Column */ 0u));

  return NS_OK;
}

// TODO: Implement idle maintenance
void TryRemoveDuringIdleMaintenance(
    const nsTArray<FileId>& /* aItemToRemove */) {
  // Not implemented
}

ContentType DetermineContentType(const Name& aName) {
  QM_TRY_UNWRAP(
      auto typeResult,
      QM_OR_ELSE_LOG_VERBOSE(
          FileSystemContentTypeGuess::FromPath(aName),
          ([](const auto& aRv) -> Result<ContentType, QMResult> {
            const nsresult rv = ToNSResult(aRv);
            switch (rv) {
              case NS_ERROR_FAILURE: /* There is an unknown new extension. */
                return ContentType(""_ns); /* We clear the old extension. */

              case NS_ERROR_INVALID_ARG: /* The name is garbled. */
                [[fallthrough]];
              case NS_ERROR_NOT_AVAILABLE: /* There is no extension. */
                return VoidCString();      /* We keep the old extension. */

              default:
                MOZ_ASSERT_UNREACHABLE("Should never get here!");
                return Err(aRv);
            }
          })),
      ContentType(""_ns));

  return typeResult;
}

}  // namespace fs::data

}  // namespace mozilla::dom