summaryrefslogtreecommitdiffstats
path: root/netwerk/base/nsFileStreams.cpp
blob: f574d13f823d02437b2d3e542567ba2f08ad3209 (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
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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 "ipc/IPCMessageUtils.h"

#if defined(XP_UNIX)
#  include <unistd.h>
#elif defined(XP_WIN)
#  include <windows.h>
#  include "nsILocalFileWin.h"
#else
// XXX add necessary include file for ftruncate (or equivalent)
#endif

#include "private/pprio.h"
#include "prerror.h"

#include "IOActivityMonitor.h"
#include "nsFileStreams.h"
#include "nsIFile.h"
#include "nsReadLine.h"
#include "nsIClassInfoImpl.h"
#include "nsLiteralString.h"
#include "nsSocketTransport2.h"  // for ErrorAccordingToNSPR()
#include "mozilla/ipc/InputStreamUtils.h"
#include "mozilla/ipc/RandomAccessStreamParams.h"
#include "mozilla/Unused.h"
#include "mozilla/FileUtils.h"
#include "nsNetCID.h"
#include "nsXULAppAPI.h"

using FileHandleType = mozilla::ipc::FileDescriptor::PlatformHandleType;

using namespace mozilla::ipc;
using namespace mozilla::net;

using mozilla::DebugOnly;
using mozilla::Maybe;
using mozilla::Nothing;
using mozilla::Some;

////////////////////////////////////////////////////////////////////////////////
// nsFileStreamBase

nsFileStreamBase::~nsFileStreamBase() {
  // We don't want to try to rewrind the stream when shutting down.
  mBehaviorFlags &= ~nsIFileInputStream::REOPEN_ON_REWIND;

  Close();
}

NS_IMPL_ISUPPORTS(nsFileStreamBase, nsISeekableStream, nsITellableStream,
                  nsIFileMetadata)

NS_IMETHODIMP
nsFileStreamBase::Seek(int32_t whence, int64_t offset) {
  nsresult rv = DoPendingOpen();
  NS_ENSURE_SUCCESS(rv, rv);

  int64_t cnt = PR_Seek64(mFD, offset, (PRSeekWhence)whence);
  if (cnt == int64_t(-1)) {
    return NS_ErrorAccordingToNSPR();
  }
  return NS_OK;
}

NS_IMETHODIMP
nsFileStreamBase::Tell(int64_t* result) {
  if (mState == eDeferredOpen && !(mOpenParams.ioFlags & PR_APPEND)) {
    *result = 0;
    return NS_OK;
  }

  nsresult rv = DoPendingOpen();
  NS_ENSURE_SUCCESS(rv, rv);

  int64_t cnt = PR_Seek64(mFD, 0, PR_SEEK_CUR);
  if (cnt == int64_t(-1)) {
    return NS_ErrorAccordingToNSPR();
  }
  *result = cnt;
  return NS_OK;
}

NS_IMETHODIMP
nsFileStreamBase::SetEOF() {
  nsresult rv = DoPendingOpen();
  NS_ENSURE_SUCCESS(rv, rv);

#if defined(XP_UNIX)
  // Some system calls require an EOF offset.
  int64_t offset;
  rv = Tell(&offset);
  if (NS_FAILED(rv)) return rv;
#endif

#if defined(XP_UNIX)
  if (ftruncate(PR_FileDesc2NativeHandle(mFD), offset) != 0) {
    NS_ERROR("ftruncate failed");
    return NS_ERROR_FAILURE;
  }
#elif defined(XP_WIN)
  if (!SetEndOfFile((HANDLE)PR_FileDesc2NativeHandle(mFD))) {
    NS_ERROR("SetEndOfFile failed");
    return NS_ERROR_FAILURE;
  }
#else
  // XXX not implemented
#endif

  return NS_OK;
}

NS_IMETHODIMP
nsFileStreamBase::GetSize(int64_t* _retval) {
  nsresult rv = DoPendingOpen();
  NS_ENSURE_SUCCESS(rv, rv);

  PRFileInfo64 info;
  if (PR_GetOpenFileInfo64(mFD, &info) == PR_FAILURE) {
    return NS_BASE_STREAM_OSERROR;
  }

  *_retval = int64_t(info.size);

  return NS_OK;
}

NS_IMETHODIMP
nsFileStreamBase::GetLastModified(int64_t* _retval) {
  nsresult rv = DoPendingOpen();
  NS_ENSURE_SUCCESS(rv, rv);

  PRFileInfo64 info;
  if (PR_GetOpenFileInfo64(mFD, &info) == PR_FAILURE) {
    return NS_BASE_STREAM_OSERROR;
  }

  int64_t modTime = int64_t(info.modifyTime);
  if (modTime == 0) {
    *_retval = 0;
  } else {
    *_retval = modTime / int64_t(PR_USEC_PER_MSEC);
  }

  return NS_OK;
}

NS_IMETHODIMP
nsFileStreamBase::GetFileDescriptor(PRFileDesc** _retval) {
  nsresult rv = DoPendingOpen();
  NS_ENSURE_SUCCESS(rv, rv);

  *_retval = mFD;
  return NS_OK;
}

nsresult nsFileStreamBase::Close() {
  CleanUpOpen();

  nsresult rv = NS_OK;
  if (mFD) {
    if (PR_Close(mFD) == PR_FAILURE) rv = NS_BASE_STREAM_OSERROR;
    mFD = nullptr;
    mState = eClosed;
  }
  return rv;
}

nsresult nsFileStreamBase::Available(uint64_t* aResult) {
  nsresult rv = DoPendingOpen();
  NS_ENSURE_SUCCESS(rv, rv);

  // PR_Available with files over 4GB returns an error, so we have to
  // use the 64-bit version of PR_Available.
  int64_t avail = PR_Available64(mFD);
  if (avail == -1) {
    return NS_ErrorAccordingToNSPR();
  }

  // If available is greater than 4GB, return 4GB
  *aResult = (uint64_t)avail;
  return NS_OK;
}

nsresult nsFileStreamBase::Read(char* aBuf, uint32_t aCount,
                                uint32_t* aResult) {
  nsresult rv = DoPendingOpen();
  if (rv == NS_BASE_STREAM_CLOSED) {
    *aResult = 0;
    return NS_OK;
  }

  if (NS_FAILED(rv)) {
    return rv;
  }

  int32_t bytesRead = PR_Read(mFD, aBuf, aCount);
  if (bytesRead == -1) {
    return NS_ErrorAccordingToNSPR();
  }

  *aResult = bytesRead;
  return NS_OK;
}

nsresult nsFileStreamBase::ReadSegments(nsWriteSegmentFun aWriter,
                                        void* aClosure, uint32_t aCount,
                                        uint32_t* aResult) {
  // ReadSegments is not implemented because it would be inefficient when
  // the writer does not consume all data.  If you want to call ReadSegments,
  // wrap a BufferedInputStream around the file stream.  That will call
  // Read().

  return NS_ERROR_NOT_IMPLEMENTED;
}

nsresult nsFileStreamBase::IsNonBlocking(bool* aNonBlocking) {
  *aNonBlocking = false;
  return NS_OK;
}

nsresult nsFileStreamBase::Flush(void) {
  nsresult rv = DoPendingOpen();
  NS_ENSURE_SUCCESS(rv, rv);

  int32_t cnt = PR_Sync(mFD);
  if (cnt == -1) {
    return NS_ErrorAccordingToNSPR();
  }
  return NS_OK;
}

nsresult nsFileStreamBase::Write(const char* buf, uint32_t count,
                                 uint32_t* result) {
  nsresult rv = DoPendingOpen();
  NS_ENSURE_SUCCESS(rv, rv);

  int32_t cnt = PR_Write(mFD, buf, count);
  if (cnt == -1) {
    return NS_ErrorAccordingToNSPR();
  }
  *result = cnt;
  return NS_OK;
}

nsresult nsFileStreamBase::WriteFrom(nsIInputStream* inStr, uint32_t count,
                                     uint32_t* _retval) {
  MOZ_ASSERT_UNREACHABLE("WriteFrom (see source comment)");
  return NS_ERROR_NOT_IMPLEMENTED;
  // File streams intentionally do not support this method.
  // If you need something like this, then you should wrap
  // the file stream using nsIBufferedOutputStream
}

nsresult nsFileStreamBase::WriteSegments(nsReadSegmentFun reader, void* closure,
                                         uint32_t count, uint32_t* _retval) {
  return NS_ERROR_NOT_IMPLEMENTED;
  // File streams intentionally do not support this method.
  // If you need something like this, then you should wrap
  // the file stream using nsIBufferedOutputStream
}

nsresult nsFileStreamBase::MaybeOpen(nsIFile* aFile, int32_t aIoFlags,
                                     int32_t aPerm, bool aDeferred) {
  NS_ENSURE_STATE(aFile);

  mOpenParams.ioFlags = aIoFlags;
  mOpenParams.perm = aPerm;

  if (aDeferred) {
    // Clone the file, as it may change between now and the deferred open
    nsCOMPtr<nsIFile> file;
    nsresult rv = aFile->Clone(getter_AddRefs(file));
    NS_ENSURE_SUCCESS(rv, rv);

    mOpenParams.localFile = std::move(file);
    NS_ENSURE_TRUE(mOpenParams.localFile, NS_ERROR_UNEXPECTED);

    mState = eDeferredOpen;
    return NS_OK;
  }

  mOpenParams.localFile = aFile;

  // Following call open() at main thread.
  // Main thread might be blocked, while open a remote file.
  return DoOpen();
}

void nsFileStreamBase::CleanUpOpen() { mOpenParams.localFile = nullptr; }

nsresult nsFileStreamBase::DoOpen() {
  MOZ_ASSERT(mState == eDeferredOpen || mState == eUnitialized ||
             mState == eClosed);
  NS_ASSERTION(!mFD, "Already have a file descriptor!");
  NS_ASSERTION(mOpenParams.localFile, "Must have a file to open");

  PRFileDesc* fd;
  nsresult rv;

  if (mOpenParams.ioFlags & PR_CREATE_FILE) {
    nsCOMPtr<nsIFile> parent;
    mOpenParams.localFile->GetParent(getter_AddRefs(parent));

    // Result doesn't need to be checked. If the file's parent path does not
    // exist, make it. If it does exist, do nothing.
    if (parent) {
      Unused << parent->Create(nsIFile::DIRECTORY_TYPE, 0755);
    }
  }

#ifdef XP_WIN
  if (mBehaviorFlags & nsIFileInputStream::SHARE_DELETE) {
    nsCOMPtr<nsILocalFileWin> file = do_QueryInterface(mOpenParams.localFile);
    MOZ_ASSERT(file);

    rv = file->OpenNSPRFileDescShareDelete(mOpenParams.ioFlags,
                                           mOpenParams.perm, &fd);
  } else
#endif  // XP_WIN
  {
    rv = mOpenParams.localFile->OpenNSPRFileDesc(mOpenParams.ioFlags,
                                                 mOpenParams.perm, &fd);
  }

  if (rv == NS_OK && IOActivityMonitor::IsActive()) {
    auto nativePath = mOpenParams.localFile->NativePath();
    if (!nativePath.IsEmpty()) {
// registering the file to the activity monitor
#ifdef XP_WIN
      // 16 bits unicode
      IOActivityMonitor::MonitorFile(
          fd, NS_ConvertUTF16toUTF8(nativePath.get()).get());
#else
      // 8 bit unicode
      IOActivityMonitor::MonitorFile(fd, nativePath.get());
#endif
    }
  }

  CleanUpOpen();

  if (NS_FAILED(rv)) {
    mState = eError;
    mErrorValue = rv;
    return rv;
  }

  mFD = fd;
  mState = eOpened;

  return NS_OK;
}

nsresult nsFileStreamBase::DoPendingOpen() {
  switch (mState) {
    case eUnitialized:
      MOZ_CRASH("This should not happen.");
      return NS_ERROR_FAILURE;

    case eDeferredOpen:
      return DoOpen();

    case eOpened:
      MOZ_ASSERT(mFD);
      if (NS_WARN_IF(!mFD)) {
        return NS_ERROR_FAILURE;
      }
      return NS_OK;

    case eClosed:
      MOZ_ASSERT(!mFD);
      return NS_BASE_STREAM_CLOSED;

    case eError:
      return mErrorValue;
  }

  MOZ_CRASH("Invalid mState value.");
  return NS_ERROR_FAILURE;
}

////////////////////////////////////////////////////////////////////////////////
// nsFileInputStream

NS_IMPL_ADDREF_INHERITED(nsFileInputStream, nsFileStreamBase)
NS_IMPL_RELEASE_INHERITED(nsFileInputStream, nsFileStreamBase)

NS_IMPL_CLASSINFO(nsFileInputStream, nullptr, nsIClassInfo::THREADSAFE,
                  NS_LOCALFILEINPUTSTREAM_CID)

NS_INTERFACE_MAP_BEGIN(nsFileInputStream)
  NS_INTERFACE_MAP_ENTRY(nsIInputStream)
  NS_INTERFACE_MAP_ENTRY(nsIFileInputStream)
  NS_INTERFACE_MAP_ENTRY(nsILineInputStream)
  NS_INTERFACE_MAP_ENTRY(nsIIPCSerializableInputStream)
  NS_IMPL_QUERY_CLASSINFO(nsFileInputStream)
  NS_INTERFACE_MAP_ENTRY_CONDITIONAL(nsICloneableInputStream, IsCloneable())
NS_INTERFACE_MAP_END_INHERITING(nsFileStreamBase)

NS_IMPL_CI_INTERFACE_GETTER(nsFileInputStream, nsIInputStream,
                            nsIFileInputStream, nsISeekableStream,
                            nsITellableStream, nsILineInputStream)

nsresult nsFileInputStream::Create(REFNSIID aIID, void** aResult) {
  RefPtr<nsFileInputStream> stream = new nsFileInputStream();
  return stream->QueryInterface(aIID, aResult);
}

nsresult nsFileInputStream::Open(nsIFile* aFile, int32_t aIOFlags,
                                 int32_t aPerm) {
  nsresult rv = NS_OK;

  // If the previous file is open, close it
  if (mFD) {
    rv = Close();
    if (NS_FAILED(rv)) return rv;
  }

  // Open the file
  if (aIOFlags == -1) aIOFlags = PR_RDONLY;
  if (aPerm == -1) aPerm = 0;

  return MaybeOpen(aFile, aIOFlags, aPerm,
                   mBehaviorFlags & nsIFileInputStream::DEFER_OPEN);
}

NS_IMETHODIMP
nsFileInputStream::Init(nsIFile* aFile, int32_t aIOFlags, int32_t aPerm,
                        int32_t aBehaviorFlags) {
  NS_ENSURE_TRUE(!mFD, NS_ERROR_ALREADY_INITIALIZED);
  NS_ENSURE_TRUE(mState == eUnitialized || mState == eClosed,
                 NS_ERROR_ALREADY_INITIALIZED);

  mBehaviorFlags = aBehaviorFlags;
  mState = eUnitialized;

  mFile = aFile;
  mIOFlags = aIOFlags;
  mPerm = aPerm;

  return Open(aFile, aIOFlags, aPerm);
}

NS_IMETHODIMP
nsFileInputStream::Close() {
  // Get the cache position at the time the file was close. This allows
  // NS_SEEK_CUR on a closed file that has been opened with
  // REOPEN_ON_REWIND.
  if (mBehaviorFlags & REOPEN_ON_REWIND) {
    // Get actual position. Not one modified by subclasses
    nsFileStreamBase::Tell(&mCachedPosition);
  }

  // null out mLineBuffer in case Close() is called again after failing
  mLineBuffer = nullptr;
  return nsFileStreamBase::Close();
}

NS_IMETHODIMP
nsFileInputStream::Read(char* aBuf, uint32_t aCount, uint32_t* _retval) {
  nsresult rv = nsFileStreamBase::Read(aBuf, aCount, _retval);
  if (rv == NS_ERROR_FILE_NOT_FOUND) {
    // Don't warn if this is a deffered file not found.
    return rv;
  }

  NS_ENSURE_SUCCESS(rv, rv);

  // Check if we're at the end of file and need to close
  if (mBehaviorFlags & CLOSE_ON_EOF && *_retval == 0) {
    Close();
  }

  return NS_OK;
}

NS_IMETHODIMP
nsFileInputStream::ReadLine(nsACString& aLine, bool* aResult) {
  if (!mLineBuffer) {
    mLineBuffer = MakeUnique<nsLineBuffer<char>>();
  }
  return NS_ReadLine(this, mLineBuffer.get(), aLine, aResult);
}

NS_IMETHODIMP
nsFileInputStream::Seek(int32_t aWhence, int64_t aOffset) {
  return SeekInternal(aWhence, aOffset);
}

nsresult nsFileInputStream::SeekInternal(int32_t aWhence, int64_t aOffset,
                                         bool aClearBuf) {
  nsresult rv = DoPendingOpen();
  if (rv != NS_OK && rv != NS_BASE_STREAM_CLOSED) {
    return rv;
  }

  if (aClearBuf) {
    mLineBuffer = nullptr;
  }

  if (rv == NS_BASE_STREAM_CLOSED) {
    if (mBehaviorFlags & REOPEN_ON_REWIND) {
      rv = Open(mFile, mIOFlags, mPerm);
      NS_ENSURE_SUCCESS(rv, rv);

      // If the file was closed, and we do a relative seek, use the
      // position we cached when we closed the file to seek to the right
      // location.
      if (aWhence == NS_SEEK_CUR) {
        aWhence = NS_SEEK_SET;
        aOffset += mCachedPosition;
      }
      // If we're trying to seek to the start then we're done, so
      // return early to avoid Seek from calling DoPendingOpen and
      // opening the underlying file earlier than necessary.
      if (aWhence == NS_SEEK_SET && aOffset == 0) {
        return NS_OK;
      }
    } else {
      return NS_BASE_STREAM_CLOSED;
    }
  }

  return nsFileStreamBase::Seek(aWhence, aOffset);
}

NS_IMETHODIMP
nsFileInputStream::Tell(int64_t* aResult) {
  return nsFileStreamBase::Tell(aResult);
}

NS_IMETHODIMP
nsFileInputStream::Available(uint64_t* aResult) {
  return nsFileStreamBase::Available(aResult);
}

void nsFileInputStream::SerializedComplexity(uint32_t aMaxSize,
                                             uint32_t* aSizeUsed,
                                             uint32_t* aPipes,
                                             uint32_t* aTransferables) {
  *aTransferables = 1;
}

void nsFileInputStream::Serialize(InputStreamParams& aParams, uint32_t aMaxSize,
                                  uint32_t* aSizeUsed) {
  MOZ_ASSERT(aSizeUsed);
  *aSizeUsed = 0;

  FileInputStreamParams params;

  if (NS_SUCCEEDED(DoPendingOpen())) {
    MOZ_ASSERT(mFD);
    FileHandleType fd = FileHandleType(PR_FileDesc2NativeHandle(mFD));
    NS_ASSERTION(fd, "This should never be null!");

    params.fileDescriptor() = FileDescriptor(fd);

    Close();
  } else {
    NS_WARNING(
        "This file has not been opened (or could not be opened). "
        "Sending an invalid file descriptor to the other process!");

    params.fileDescriptor() = FileDescriptor();
  }

  int32_t behaviorFlags = mBehaviorFlags;

  // The receiving process (or thread) is going to have an open file
  // descriptor automatically so transferring this flag is meaningless.
  behaviorFlags &= ~nsIFileInputStream::DEFER_OPEN;

  params.behaviorFlags() = behaviorFlags;
  params.ioFlags() = mIOFlags;

  aParams = params;
}

bool nsFileInputStream::Deserialize(const InputStreamParams& aParams) {
  NS_ASSERTION(!mFD, "Already have a file descriptor?!");
  NS_ASSERTION(mState == nsFileStreamBase::eUnitialized, "Deferring open?!");
  NS_ASSERTION(!mFile, "Should never have a file here!");
  NS_ASSERTION(!mPerm, "This should always be 0!");

  if (aParams.type() != InputStreamParams::TFileInputStreamParams) {
    NS_WARNING("Received unknown parameters from the other process!");
    return false;
  }

  const FileInputStreamParams& params = aParams.get_FileInputStreamParams();

  const FileDescriptor& fd = params.fileDescriptor();

  if (fd.IsValid()) {
    auto rawFD = fd.ClonePlatformHandle();
    PRFileDesc* fileDesc = PR_ImportFile(PROsfd(rawFD.release()));
    if (!fileDesc) {
      NS_WARNING("Failed to import file handle!");
      return false;
    }
    mFD = fileDesc;
    mState = eOpened;
  } else {
    NS_WARNING("Received an invalid file descriptor!");
    mState = eError;
    mErrorValue = NS_ERROR_FILE_NOT_FOUND;
  }

  mBehaviorFlags = params.behaviorFlags();

  if (!XRE_IsParentProcess()) {
    // A child process shouldn't close when it reads the end because it will
    // not be able to reopen the file later.
    mBehaviorFlags &= ~nsIFileInputStream::CLOSE_ON_EOF;

    // A child process will not be able to reopen the file so this flag is
    // meaningless.
    mBehaviorFlags &= ~nsIFileInputStream::REOPEN_ON_REWIND;
  }

  mIOFlags = params.ioFlags();

  return true;
}

bool nsFileInputStream::IsCloneable() const {
  // This inputStream is cloneable only if has been created using Init() and
  // it owns a nsIFile. This is not true when it is deserialized from IPC.
  return XRE_IsParentProcess() && mFile;
}

NS_IMETHODIMP
nsFileInputStream::GetCloneable(bool* aCloneable) {
  *aCloneable = IsCloneable();
  return NS_OK;
}

NS_IMETHODIMP
nsFileInputStream::Clone(nsIInputStream** aResult) {
  MOZ_ASSERT(IsCloneable());
  return NS_NewLocalFileInputStream(aResult, mFile, mIOFlags, mPerm,
                                    mBehaviorFlags);
}

////////////////////////////////////////////////////////////////////////////////
// nsFileOutputStream

NS_IMPL_ISUPPORTS_INHERITED(nsFileOutputStream, nsFileStreamBase,
                            nsIOutputStream, nsIFileOutputStream)

nsresult nsFileOutputStream::Create(REFNSIID aIID, void** aResult) {
  RefPtr<nsFileOutputStream> stream = new nsFileOutputStream();
  return stream->QueryInterface(aIID, aResult);
}

NS_IMETHODIMP
nsFileOutputStream::Init(nsIFile* file, int32_t ioFlags, int32_t perm,
                         int32_t behaviorFlags) {
  NS_ENSURE_TRUE(mFD == nullptr, NS_ERROR_ALREADY_INITIALIZED);
  NS_ENSURE_TRUE(mState == eUnitialized || mState == eClosed,
                 NS_ERROR_ALREADY_INITIALIZED);

  mBehaviorFlags = behaviorFlags;
  mState = eUnitialized;

  if (ioFlags == -1) ioFlags = PR_WRONLY | PR_CREATE_FILE | PR_TRUNCATE;
  if (perm <= 0) perm = 0664;

  return MaybeOpen(file, ioFlags, perm,
                   mBehaviorFlags & nsIFileOutputStream::DEFER_OPEN);
}

nsresult nsFileOutputStream::InitWithFileDescriptor(
    const mozilla::ipc::FileDescriptor& aFd) {
  NS_ENSURE_TRUE(mFD == nullptr, NS_ERROR_ALREADY_INITIALIZED);
  NS_ENSURE_TRUE(mState == eUnitialized || mState == eClosed,
                 NS_ERROR_ALREADY_INITIALIZED);

  if (aFd.IsValid()) {
    auto rawFD = aFd.ClonePlatformHandle();
    PRFileDesc* fileDesc = PR_ImportFile(PROsfd(rawFD.release()));
    if (!fileDesc) {
      NS_WARNING("Failed to import file handle!");
      return NS_ERROR_FAILURE;
    }
    mFD = fileDesc;
    mState = eOpened;
  } else {
    mState = eError;
    mErrorValue = NS_ERROR_FILE_NOT_FOUND;
  }

  return NS_OK;
}

NS_IMETHODIMP
nsFileOutputStream::Preallocate(int64_t aLength) {
  if (!mFD) {
    return NS_ERROR_NOT_INITIALIZED;
  }

  if (!mozilla::fallocate(mFD, aLength)) {
    return NS_ERROR_FAILURE;
  }

  return NS_OK;
}

////////////////////////////////////////////////////////////////////////////////
// nsAtomicFileOutputStream

NS_IMPL_ISUPPORTS_INHERITED(nsAtomicFileOutputStream, nsFileOutputStream,
                            nsISafeOutputStream, nsIOutputStream,
                            nsIFileOutputStream)

NS_IMETHODIMP
nsAtomicFileOutputStream::Init(nsIFile* file, int32_t ioFlags, int32_t perm,
                               int32_t behaviorFlags) {
  // While `PR_APPEND` is not supported, `-1` is used as `ioFlags` parameter
  // in some places, and `PR_APPEND | PR_TRUNCATE` does not require appending
  // to existing file. So, throw an exception only if `PR_APPEND` is
  // explicitly specified without `PR_TRUNCATE`.
  if ((ioFlags & PR_APPEND) && !(ioFlags & PR_TRUNCATE)) {
    return NS_ERROR_INVALID_ARG;
  }
  return nsFileOutputStream::Init(file, ioFlags, perm, behaviorFlags);
}

nsresult nsAtomicFileOutputStream::DoOpen() {
  // Make sure mOpenParams.localFile will be empty if we bail somewhere in
  // this function
  nsCOMPtr<nsIFile> file;
  file.swap(mOpenParams.localFile);

  if (!file) {
    return NS_ERROR_NOT_INITIALIZED;
  }
  nsresult rv = file->Exists(&mTargetFileExists);
  if (NS_FAILED(rv)) {
    NS_ERROR("Can't tell if target file exists");
    mTargetFileExists =
        true;  // Safer to assume it exists - we just do more work.
  }

  // follow symlinks, for two reasons:
  // 1) if a user has deliberately set up a profile file as a symlink, we
  //    honor it
  // 2) to make the MoveToNative() in Finish() an atomic operation (which may
  //    not be the case if moving across directories on different
  //    filesystems).
  nsCOMPtr<nsIFile> tempResult;
  rv = file->Clone(getter_AddRefs(tempResult));
  if (NS_SUCCEEDED(rv) && mTargetFileExists) {
    tempResult->Normalize();
  }

  if (NS_SUCCEEDED(rv) && mTargetFileExists) {
    // Abort if |file| is not writable; it won't work as an output stream.
    bool isWritable;
    if (NS_SUCCEEDED(file->IsWritable(&isWritable)) && !isWritable) {
      return NS_ERROR_FILE_ACCESS_DENIED;
    }

    uint32_t origPerm;
    if (NS_FAILED(file->GetPermissions(&origPerm))) {
      NS_ERROR("Can't get permissions of target file");
      origPerm = mOpenParams.perm;
    }

    // XXX What if |perm| is more restrictive then |origPerm|?
    // This leaves the user supplied permissions as they were.
    rv = tempResult->CreateUnique(nsIFile::NORMAL_FILE_TYPE, origPerm);
  }
  if (NS_SUCCEEDED(rv)) {
    // nsFileOutputStream::DoOpen will work on the temporary file, so we
    // prepare it and place it in mOpenParams.localFile.
    mOpenParams.localFile = tempResult;
    mTempFile = tempResult;
    mTargetFile = file;
    rv = nsFileOutputStream::DoOpen();
  }
  return rv;
}

NS_IMETHODIMP
nsAtomicFileOutputStream::Close() {
  nsresult rv = nsFileOutputStream::Close();

  // the consumer doesn't want the original file overwritten -
  // so clean up by removing the temp file.
  if (mTempFile) {
    mTempFile->Remove(false);
    mTempFile = nullptr;
  }

  return rv;
}

NS_IMETHODIMP
nsAtomicFileOutputStream::Finish() {
  nsresult rv = nsFileOutputStream::Close();

  // if there is no temp file, don't try to move it over the original target.
  // It would destroy the targetfile if close() is called twice.
  if (!mTempFile) return rv;

  // Only overwrite if everything was ok, and the temp file could be closed.
  if (NS_SUCCEEDED(mWriteResult) && NS_SUCCEEDED(rv)) {
    NS_ENSURE_STATE(mTargetFile);

    if (!mTargetFileExists) {
      // If the target file did not exist when we were initialized, then the
      // temp file we gave out was actually a reference to the target file.
      // since we succeeded in writing to the temp file (and hence succeeded
      // in writing to the target file), there is nothing more to do.
#ifdef DEBUG
      bool equal;
      if (NS_FAILED(mTargetFile->Equals(mTempFile, &equal)) || !equal) {
        NS_WARNING("mTempFile not equal to mTargetFile");
      }
#endif
    } else {
      nsAutoString targetFilename;
      rv = mTargetFile->GetLeafName(targetFilename);
      if (NS_SUCCEEDED(rv)) {
        // This will replace target.
        rv = mTempFile->MoveTo(nullptr, targetFilename);
        if (NS_FAILED(rv)) mTempFile->Remove(false);
      }
    }
  } else {
    mTempFile->Remove(false);

    // if writing failed, propagate the failure code to the caller.
    if (NS_FAILED(mWriteResult)) rv = mWriteResult;
  }
  mTempFile = nullptr;
  return rv;
}

NS_IMETHODIMP
nsAtomicFileOutputStream::Write(const char* buf, uint32_t count,
                                uint32_t* result) {
  nsresult rv = nsFileOutputStream::Write(buf, count, result);
  if (NS_SUCCEEDED(mWriteResult)) {
    if (NS_FAILED(rv)) {
      mWriteResult = rv;
    } else if (count != *result) {
      mWriteResult = NS_ERROR_LOSS_OF_SIGNIFICANT_DATA;
    }

    if (NS_FAILED(mWriteResult) && count > 0) {
      NS_WARNING("writing to output stream failed! data may be lost");
    }
  }
  return rv;
}

////////////////////////////////////////////////////////////////////////////////
// nsSafeFileOutputStream

NS_IMETHODIMP
nsSafeFileOutputStream::Finish() {
  (void)Flush();
  return nsAtomicFileOutputStream::Finish();
}

////////////////////////////////////////////////////////////////////////////////
// nsFileRandomAccessStream

nsresult nsFileRandomAccessStream::Create(REFNSIID aIID, void** aResult) {
  RefPtr<nsFileRandomAccessStream> stream = new nsFileRandomAccessStream();
  return stream->QueryInterface(aIID, aResult);
}

NS_IMPL_ISUPPORTS_INHERITED(nsFileRandomAccessStream, nsFileStreamBase,
                            nsIRandomAccessStream, nsIFileRandomAccessStream,
                            nsIInputStream, nsIOutputStream)

NS_IMETHODIMP
nsFileRandomAccessStream::GetInputStream(nsIInputStream** aInputStream) {
  nsCOMPtr<nsIInputStream> inputStream(this);

  inputStream.forget(aInputStream);
  return NS_OK;
}

NS_IMETHODIMP
nsFileRandomAccessStream::GetOutputStream(nsIOutputStream** aOutputStream) {
  nsCOMPtr<nsIOutputStream> outputStream(this);

  outputStream.forget(aOutputStream);
  return NS_OK;
}

nsIInputStream* nsFileRandomAccessStream::InputStream() { return this; }

nsIOutputStream* nsFileRandomAccessStream::OutputStream() { return this; }

RandomAccessStreamParams nsFileRandomAccessStream::Serialize(
    nsIInterfaceRequestor* aCallbacks) {
  FileRandomAccessStreamParams params;

  if (NS_SUCCEEDED(DoPendingOpen())) {
    MOZ_ASSERT(mFD);
    FileHandleType fd = FileHandleType(PR_FileDesc2NativeHandle(mFD));
    MOZ_ASSERT(fd, "This should never be null!");

    params.fileDescriptor() = FileDescriptor(fd);

    Close();
  } else {
    NS_WARNING(
        "This file has not been opened (or could not be opened). "
        "Sending an invalid file descriptor to the other process!");

    params.fileDescriptor() = FileDescriptor();
  }

  int32_t behaviorFlags = mBehaviorFlags;

  // The receiving process (or thread) is going to have an open file
  // descriptor automatically so transferring this flag is meaningless.
  behaviorFlags &= ~nsIFileInputStream::DEFER_OPEN;

  params.behaviorFlags() = behaviorFlags;

  return params;
}

bool nsFileRandomAccessStream::Deserialize(
    RandomAccessStreamParams& aStreamParams) {
  MOZ_ASSERT(!mFD, "Already have a file descriptor?!");
  MOZ_ASSERT(mState == nsFileStreamBase::eUnitialized, "Deferring open?!");

  if (aStreamParams.type() !=
      RandomAccessStreamParams::TFileRandomAccessStreamParams) {
    NS_WARNING("Received unknown parameters from the other process!");
    return false;
  }

  const FileRandomAccessStreamParams& params =
      aStreamParams.get_FileRandomAccessStreamParams();

  const FileDescriptor& fd = params.fileDescriptor();

  if (fd.IsValid()) {
    auto rawFD = fd.ClonePlatformHandle();
    PRFileDesc* fileDesc = PR_ImportFile(PROsfd(rawFD.release()));
    if (!fileDesc) {
      NS_WARNING("Failed to import file handle!");
      return false;
    }
    mFD = fileDesc;
    mState = eOpened;
  } else {
    NS_WARNING("Received an invalid file descriptor!");
    mState = eError;
    mErrorValue = NS_ERROR_FILE_NOT_FOUND;
  }

  mBehaviorFlags = params.behaviorFlags();

  return true;
}

NS_IMETHODIMP
nsFileRandomAccessStream::Init(nsIFile* file, int32_t ioFlags, int32_t perm,
                               int32_t behaviorFlags) {
  NS_ENSURE_TRUE(mFD == nullptr, NS_ERROR_ALREADY_INITIALIZED);
  NS_ENSURE_TRUE(mState == eUnitialized || mState == eClosed,
                 NS_ERROR_ALREADY_INITIALIZED);

  mBehaviorFlags = behaviorFlags;
  mState = eUnitialized;

  if (ioFlags == -1) ioFlags = PR_RDWR;
  if (perm <= 0) perm = 0;

  return MaybeOpen(file, ioFlags, perm,
                   mBehaviorFlags & nsIFileRandomAccessStream::DEFER_OPEN);
}

////////////////////////////////////////////////////////////////////////////////