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
|
/* -*- 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 "FileSystemFileManager.h"
#include "ResultStatement.h"
#include "mozStorageHelper.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/QuotaCommon.h"
#include "mozilla/dom/quota/QuotaManager.h"
#include "mozilla/dom/quota/ResultExtensions.h"
namespace mozilla::dom {
using FileSystemEntries = nsTArray<fs::FileSystemEntryMetadata>;
namespace fs::data {
namespace {
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> IsDirectoryEmpty(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 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<Path, QMResult> ResolveReversedPath(
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 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;
}
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 JOIN Entries USING (handle) "
"WHERE Files.name = :name AND Entries.parent = :parent ) "
";"_ns;
QM_TRY_RETURN(ApplyEntryExistsQuery(aConnection, existsQuery, aHandle));
}
Result<bool, QMResult> DoesFileExist(const FileSystemConnection& aConnection,
const EntryId& aEntry) {
MOZ_ASSERT(!aEntry.IsEmpty());
const nsCString existsQuery =
"SELECT EXISTS "
"(SELECT 1 FROM Files WHERE handle = :handle ) "
";"_ns;
QM_TRY_RETURN(ApplyEntryExistsQuery(aConnection, existsQuery, aEntry));
}
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;
}
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;
}
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));
}
Result<EntryId, QMResult> FindEntryId(const FileSystemConnection& aConnection,
const FileSystemChildMetadata& aHandle,
bool aIsFile) {
const nsCString aDirectoryQuery =
"SELECT Entries.handle FROM Directories JOIN Entries USING (handle) "
"WHERE Directories.name = :name AND Entries.parent = :parent "
";"_ns;
const nsCString aFileQuery =
"SELECT Entries.handle FROM Files 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;
}
bool IsSame(const FileSystemConnection& aConnection,
const FileSystemEntryMetadata& aHandle,
const FileSystemChildMetadata& aNewHandle, bool aIsFile) {
MOZ_ASSERT(!aNewHandle.parentId().IsEmpty());
QM_TRY_UNWRAP(EntryId entryId, FindEntryId(aConnection, aNewHandle, aIsFile),
false);
return entryId == aHandle.entryId();
}
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));
}
nsresult PerformRename(const FileSystemConnection& aConnection,
const FileSystemEntryMetadata& aHandle,
const Name& aNewName,
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;
}
auto toNSResult = [](const auto& aRv) { return ToNSResult(aRv); };
// 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));
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,
updateDirectoryNameQuery);
}
nsresult PerformRenameFile(const FileSystemConnection& aConnection,
const FileSystemEntryMetadata& aHandle,
const Name& aNewName) {
const nsLiteralCString updateFileNameQuery =
"UPDATE Files "
"SET name = :name "
"WHERE handle = :handle "
";"_ns;
return PerformRename(aConnection, aHandle, aNewName, updateFileNameQuery);
}
} // namespace
/* 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::UpdateUsageInDatabase(
const EntryId& aEntry, int64_t 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.BindEntryIdByName("handle"_ns, aEntry)));
QM_TRY(MOZ_TO_RESULT(stmt.Execute()));
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_UNWRAP(EntryId entryId, GetUniqueEntryId(mConnection, aHandle));
MOZ_ASSERT(!entryId.IsEmpty());
mozStorageTransaction transaction(
mConnection.get(), false, mozIStorageConnection::TRANSACTION_IMMEDIATE);
{
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
if (exists) {
return Err(QMResult(NS_ERROR_DOM_TYPE_MISMATCH_ERR));
}
QM_TRY_UNWRAP(exists, DoesFileExist(mConnection, aHandle));
if (exists) {
return FindEntryId(mConnection, aHandle, 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, name ) "
"VALUES "
"( :handle, :name ) "
";"_ns;
QM_TRY_UNWRAP(EntryId entryId, GetUniqueEntryId(mConnection, aHandle));
MOZ_ASSERT(!entryId.IsEmpty());
mozStorageTransaction transaction(
mConnection.get(), false, mozIStorageConnection::TRANSACTION_IMMEDIATE);
{
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, insertFileQuery));
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()));
return entryId;
}
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;
}
nsresult FileSystemDatabaseManagerVersion001::GetFile(
const EntryId& aEntryId, nsString& aType,
TimeStamp& lastModifiedMilliSeconds, nsTArray<Name>& aPath,
nsCOMPtr<nsIFile>& aFile) const {
MOZ_ASSERT(!aEntryId.IsEmpty());
QM_TRY_UNWRAP(aFile, mFileManager->GetOrCreateFile(aEntryId));
QM_TRY(MOZ_TO_RESULT(GetFileAttributes(mConnection, aEntryId, aType)));
PRTime lastModTime = 0;
QM_TRY(MOZ_TO_RESULT(aFile->GetLastModifiedTime(&lastModTime)));
lastModifiedMilliSeconds = static_cast<TimeStamp>(lastModTime);
FileSystemEntryPair endPoints(mRootEntry, aEntryId);
QM_TRY_UNWRAP(aPath, ResolveReversedPath(mConnection, endPoints));
if (aPath.IsEmpty()) {
return NS_ERROR_DOM_NOT_FOUND_ERR;
}
aPath.Reverse();
return NS_OK;
}
nsresult FileSystemDatabaseManagerVersion001::UpdateUsage(
const EntryId& aEntry) {
auto toNSResult = [](const auto& aRv) { return ToNSResult(aRv); };
// We don't track directories or non-existent files.
QM_TRY_UNWRAP(bool fileExists,
DoesFileExist(mConnection, aEntry).mapErr(toNSResult));
if (!fileExists) {
return NS_OK; // May be deleted before update, no assert
}
QM_TRY_UNWRAP(bool isFolder,
DoesDirectoryExist(mConnection, aEntry).mapErr(toNSResult));
if (isFolder) {
return NS_OK; // May be deleted and replaced by a folder, no assert
}
nsCOMPtr<nsIFile> file;
QM_TRY_UNWRAP(file, mFileManager->GetOrCreateFile(aEntry));
MOZ_ASSERT(file);
int64_t fileSize = 0;
QM_TRY(MOZ_TO_RESULT(file->GetFileSize(&fileSize)));
QM_TRY(MOZ_TO_RESULT(UpdateUsageInDatabase(aEntry, fileSize)));
return NS_OK;
}
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));
if (!aRecursive && !isEmpty) {
return Err(QMResult(NS_ERROR_DOM_INVALID_MODIFICATION_ERR));
}
// If it's empty or we can delete recursively, deleting the handle will
// cascade
const nsLiteralCString descendantsQuery =
"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;
const nsLiteralCString deleteEntryQuery =
"DELETE FROM Entries "
"WHERE handle = :handle "
";"_ns;
mozStorageTransaction transaction(
mConnection.get(), false, mozIStorageConnection::TRANSACTION_IMMEDIATE);
nsTArray<EntryId> descendants;
{
QM_TRY_UNWRAP(ResultStatement stmt,
ResultStatement::Create(mConnection, descendantsQuery));
QM_TRY(QM_TO_RESULT(stmt.BindEntryIdByName("handle"_ns, entryId)));
QM_TRY_UNWRAP(bool moreResults, stmt.ExecuteStep());
while (moreResults) {
QM_TRY_UNWRAP(EntryId entryId, stmt.GetEntryIdByColumn(/* Column */ 0u));
descendants.AppendElement(entryId);
QM_TRY_UNWRAP(moreResults, stmt.ExecuteStep());
}
}
{
QM_TRY_UNWRAP(ResultStatement stmt,
ResultStatement::Create(mConnection, deleteEntryQuery));
QM_TRY(QM_TO_RESULT(stmt.BindEntryIdByName("handle"_ns, entryId)));
QM_TRY(QM_TO_RESULT(stmt.Execute()));
}
QM_TRY(QM_TO_RESULT(transaction.Commit()));
for (const auto& child : descendants) {
QM_WARNONLY_TRY_UNWRAP(const auto maybeFileSize,
mFileManager->RemoveFile(child));
if (maybeFileSize) {
quota::QuotaManager* quotaManager = quota::QuotaManager::Get();
MOZ_ASSERT(quotaManager);
quotaManager->DecreaseUsageForClient(
quota::ClientMetadata{mDataManager->OriginMetadataRef(),
quota::Client::FILESYSTEM},
*maybeFileSize);
}
}
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
if (mDataManager->IsLocked(entryId)) {
LOG(("Trying to remove in-use file"));
return Err(QMResult(NS_ERROR_DOM_INVALID_MODIFICATION_ERR));
}
const nsLiteralCString deleteEntryQuery =
"DELETE FROM Entries "
"WHERE handle = :handle "
";"_ns;
mozStorageTransaction transaction(
mConnection.get(), false, mozIStorageConnection::TRANSACTION_IMMEDIATE);
{
QM_TRY_UNWRAP(ResultStatement stmt,
ResultStatement::Create(mConnection, deleteEntryQuery));
QM_TRY(QM_TO_RESULT(stmt.BindEntryIdByName("handle"_ns, entryId)));
QM_TRY(QM_TO_RESULT(stmt.Execute()));
}
QM_TRY(QM_TO_RESULT(transaction.Commit()));
QM_WARNONLY_TRY_UNWRAP(const auto maybeFileSize,
mFileManager->RemoveFile(entryId));
if (maybeFileSize) {
quota::QuotaManager* quotaManager = quota::QuotaManager::Get();
MOZ_ASSERT(quotaManager);
quotaManager->DecreaseUsageForClient(
quota::ClientMetadata{mDataManager->OriginMetadataRef(),
quota::Client::FILESYSTEM},
*maybeFileSize);
}
return true;
}
Result<bool, QMResult> FileSystemDatabaseManagerVersion001::RenameEntry(
const FileSystemEntryMetadata& aHandle, const Name& aNewName) {
// Can't rename root
if (mRootEntry == aHandle.entryId()) {
return Err(QMResult(NS_ERROR_DOM_NOT_FOUND_ERR));
}
// Verify the source exists
QM_TRY_UNWRAP(bool isFile, IsFile(mConnection, aHandle.entryId()), false);
// At this point, entry exists
if (isFile && mDataManager->IsLocked(aHandle.entryId())) {
LOG(("Trying to move in-use file"));
return Err(QMResult(NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR));
}
// Are we actually renaming?
if (aHandle.entryName() == aNewName) {
return true;
}
// If the destination file exists, fail explicitly.
FileSystemChildMetadata destination;
QM_TRY_UNWRAP(EntryId parent, FindParent(mConnection, aHandle.entryId()));
destination.parentId() = parent;
destination.childName() = aNewName;
QM_TRY_UNWRAP(bool exists, DoesFileExist(mConnection, destination));
if (exists) {
// If the destination file exists, check if it is in use
QM_TRY_INSPECT(const EntryId& destId,
FindEntryId(mConnection, destination, true));
if (mDataManager->IsLocked(destId)) {
LOG(("Trying to overwrite in-use file"));
return Err(QMResult(NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR));
}
QM_TRY_UNWRAP(DebugOnly<bool> isRemoved, RemoveFile(destination));
MOZ_ASSERT(isRemoved);
} else {
QM_TRY_UNWRAP(exists, DoesDirectoryExist(mConnection, destination));
if (exists) {
return Err(QMResult(NS_ERROR_DOM_INVALID_MODIFICATION_ERR));
}
}
mozStorageTransaction transaction(
mConnection.get(), false, mozIStorageConnection::TRANSACTION_IMMEDIATE);
if (isFile) {
QM_TRY(QM_TO_RESULT(PerformRenameFile(mConnection, aHandle, aNewName)));
} else {
QM_TRY(
QM_TO_RESULT(PerformRenameDirectory(mConnection, aHandle, aNewName)));
}
QM_TRY(QM_TO_RESULT(transaction.Commit()));
return true;
}
Result<bool, QMResult> FileSystemDatabaseManagerVersion001::MoveEntry(
const FileSystemEntryMetadata& aHandle,
const FileSystemChildMetadata& aNewDesignation) {
MOZ_ASSERT(!aHandle.entryId().IsEmpty());
const EntryId& entryId = aHandle.entryId();
const Name& newName = aNewDesignation.childName();
if (mRootEntry == entryId) {
return Err(QMResult(NS_ERROR_DOM_NOT_FOUND_ERR));
}
// Verify the source exists
QM_TRY_UNWRAP(bool isFile, IsFile(mConnection, entryId), false);
// If the rename doesn't change the name or directory, just return success.
// XXX Needs to be added to the spec
if (IsSame(mConnection, aHandle, aNewDesignation, isFile)) {
return true;
}
// At this point, entry exists
if (isFile && mDataManager->IsLocked(entryId)) {
LOG(("Trying to move in-use file"));
return Err(QMResult(NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR));
}
// If the destination file exists, fail explicitly. Spec author plans to
// revise the spec
QM_TRY_UNWRAP(bool exists, DoesFileExist(mConnection, aNewDesignation));
if (exists) {
QM_TRY_INSPECT(const EntryId& destId,
FindEntryId(mConnection, aNewDesignation, true));
if (mDataManager->IsLocked(destId)) {
LOG(("Trying to overwrite in-use file"));
return Err(QMResult(NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR));
}
QM_TRY_UNWRAP(DebugOnly<bool> isRemoved, RemoveFile(aNewDesignation));
MOZ_ASSERT(isRemoved);
} else {
QM_TRY_UNWRAP(exists, DoesDirectoryExist(mConnection, aNewDesignation));
if (exists) {
return Err(QMResult(NS_ERROR_DOM_INVALID_MODIFICATION_ERR));
}
}
// 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(mConnection, {aHandle.entryId(), aNewDesignation.parentId()}));
if (isDestinationUnderSelf) {
return Err(QMResult(NS_ERROR_DOM_INVALID_MODIFICATION_ERR));
}
const nsLiteralCString updateEntryParentQuery =
"UPDATE Entries "
"SET parent = :parent "
"WHERE handle = :handle "
";"_ns;
mozStorageTransaction transaction(
mConnection.get(), false, mozIStorageConnection::TRANSACTION_IMMEDIATE);
{
// 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()));
}
// Are we actually renaming?
if (aHandle.entryName() == newName) {
QM_TRY(QM_TO_RESULT(transaction.Commit()));
return true;
}
if (isFile) {
QM_TRY(QM_TO_RESULT(PerformRenameFile(mConnection, aHandle, newName)));
} else {
QM_TRY(QM_TO_RESULT(PerformRenameDirectory(mConnection, aHandle, newName)));
}
QM_TRY(QM_TO_RESULT(transaction.Commit()));
return true;
}
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;
}
void FileSystemDatabaseManagerVersion001::Close() { mConnection->Close(); }
} // namespace fs::data
} // namespace mozilla::dom
|