Files
palemoon27/xpcom/base/nsDumpUtils.cpp
T
roytam1 f32bf3ebba import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1268976 - Assert that the micro-task queues are empty upon destruction of CycleCollectedJSRuntime; r=mccr8 (a459a4af91)
- Bug 1191354 part 1 - Clean up nsHTMLEditRules::GetHighestInlineParent; r=ehsan (a2f401887c)
- Bug 1191354 part 2 - Clean up nsHTMLEditRules::CheckInterlinePosition; r=ehsan (75d3829eec)
- Bug 1191354 part 3 - Clean up nsHTMLEditRules::InsertMozBRIfNeeded; r=ehsan (bedbba7c14)
- Bug 772796 - Handle newlines correctly when joining <div> and <pre>. r=roc (323eaf4fe9)
- Bug 1258085 - Avoid empty whitespace nodes when editing (b77825fb31)
- Bug 1156062 part 4 - Clean up ns*EditRules::WillInsert; r=ehsan (f351557776)
- Bug 1156062 part 5 - Make CreateBR return regular pointer; r=ehsan (8720917dee)
- Bug 1156062 part 6 - Clean up nsHTMLEditRules::WillInsertBreak; r=ehsan (d71bca568f)
- Bug 1156062 part 7 - Clean up nsHTMLEditRules::StandardBreakImpl; r=ehsan (625db32c02)
- Bug 1156062 part 8 - Clean up nsHTMLEditRules::JoinBlocks; r=ehsan (266b6c5150)
- Bug 1156062 part 9 - Clean up nsHTMLEditRules::WillAlign; r=masayuki (a6e3227001)
- Bug 1156062 part 10 - Clean up MarginPropertyAtomForIndent; r=masayuki (28612e062c)
- Bug 1184665 - Remove a spammy editor warning (a2db555918)
- Bug 1156062 part 11 - Clean up nsHTMLEditRules::Before/AfterEdit; r=masayuki (6be722082e)
- Bug 1156062 part 12 - Clean up nsHTMLEditRules::GetAlignment; r=masayuki (350be10ee8)
- Bug 1190172 part 1 - Clean up nsHTMLEditor::SplitStyleAbovePoint; r=ehsan (ce5e360af6)
- Bug 1145395 - Introduce an overload of nsHTMLEditor::SelElementPosition that takes an Element; r=ehsan (7ca9dfd2f8)
- Bug 1190172 part 2 - Clean up nsHTMLEditor::ClearStyle; r=ehsan (84334514ad)
- Bug 1190172 part 3 - Remove old method variants; r=ehsan (e042bd64d5)
- Bug 1190172 part 4 - Remove unused nsHTMLEditor::HasAttr; r=ehsan (a4f1563465)
- Bug 1190172 part 5 - Clean up nsHTMLEditor::NodeIsProperty; r=ehsan (98afb84bd4)
- Bug 1190172 part 6 - Clean up nsHTMLEditor::RelativeFontChangeOnTextNode; r=ehsan (0bf036d206)
- Bug 1190172 part 7 - Clean up nsHTMLEditor::PromoteInlineRange; r=ehsan (0a21347500)
- Bug 1190172 part 8 - Clean up nsHTMLEditor::PromoteRangeIfStartsOrEndsInNamedAnchor; r=ehsan (78cf619036)
- Bug 1190172 part 9 - Clean up nsHTMLEditor::ReturnInHeader; r=ehsan (1d8d151ff1)
- Bug 1190172 part 10 - Clean up nsHTMLEditor::ReturnInListItem; r=ehsan (0ce94d3cb1)
- Bug 1190172 part 11 - Clean up nsHTMLEditRules::IsEmptyBlock; r=ehsan (a8dc5092f8)
- Bug 1190172 part 12 - Clean up nsHTMLEditRules::ExpandSelectionForDeletion; r=ehsan (3d4a87a56d)
- Bug 1191354 part 4 - Clean up nsHTMLEditRules::AlignBlock; r=ehsan (b39372d726)
- Bug 1191354 part 5 - Clean up nsHTMLEditRules::IsEmptyInline; r=ehsan (0b8ab19fd1)
- Bug 1191354 part 6 - Clean up IsBlockNode/IsInlineNode in nsHTMLEditRules.cpp; r=ehsan (8ba8047e12)
- Bug 1191354 part 7 - Clean up nsHTMLEditRules::CheckForInvisibleBR; r=ehsan (0bc61269bd)
- Bug 1191354 part 8 - Typedefs in headers for readability; r=ehsan (50b43dc3e9)
- Bug 1191354 part 9 - Remove old nsHTMLEditRules::ConvertListType variant; r=ehsan (871f1b30a5)
- Bug 1191354 part 10 - More features for OwningNonNull; r=froydnj (8e365d079a)
- Bug 1191354 part 11 - Clean up nsHTMLEditRules::WillOutdent; r=ehsan (0c59a81c10)
- Bug 1191354 part 12 - Clean up nsHTMLEditRules::OutdentPartOfBlock; r=ehsan (243d7ab42f)
- Bug 1191354 part 13 - Clean up nsHTMLEditRules::SplitBlock; r=ehsan (c1141ab04d)
- Bug 1191356 part 1 - Clean up nsHTMLEditRules::RemoveListStructure; r=ehsan (1d2ad5faa6)
- Bug 1191356 part 2 - Clean up nsHTMLEditor::RemoveBlockContainer; r=ehsan (a9d140f511)
- Bug 1191356 part 3 - Clean up nsHTMLEditRules::MoveBlock; r=ehsan (6bc14cd40c)
- Bug 1191356 part 4 - Clean up nsHTMLEditRules::MoveNodeSmart, MoveContents; r=ehsan (00e6536e47)
- Bug 1191356 part 5 - Clean up nsHTMLEditRules::RelativeChangeIndentationOfElementNode; r=ehsan (c3139c7d90)
- Bug 1191356 part 6 - Clean up nsHTMLEditRules::WillMakeBasicBlock; r=ehsan (b3f73c2e12)
- Bug 1191356 part 7 - Clean up nsHTMLEditor::GetSelectionContainer; r=ehsan (091e999587)
- Bug 1191356 part 8 - Clean up nsHTMLEditRules::WillAbsolutePosition; r=ehsan (9982c65a99)
- Bug 1208884 - Fix a few null check ordering bugs in nsHTMLEditRules::SplitParagraph; r=roc (d04eece297)
- Bug 1209037 - Eliminate some unneeded null checks; r=jdm (b35d5308c5)
- Bug 1191356 part 9 - Convert nsHTMLEditRules::mNewBlock to Element; r=ehsan (f374780e22)
- Bug 1253734 - Add some more editor things to CC traversal. r=mccr8. (94a0c77bb1)
- Bug 1193762 part 1 - Remove nsEditor::IsBlockNode(nsIDOMNode*); r=ehsan (5e107cbdbb)
- Bug 1193762 part 2 - Remove nsHTMLEditor::IsVisBreak(nsIDOMNode*); r=ehsan (5e61c27d21)
- Bug 1193762 part 3 - Remove nsEditor::IsDescendantOfEditorRoot(nsIDOMNode*); r=ehsan (022a37d442)
- Bug 1193762 part 4 - Avoid nsCOMPtr in ternary operator; r=froydnj (e4bc35cbe0)
- Bug 1193762 part 5 - Convert DecodePool::threads to nsTArray; r=froydnj (c61f063f4d)
- Bug 1193762 part 6 - Return raw pointer, not nsCOMPtr; r=froydnj (a43c30f92e)
- Bug 1193762 part 7 - Use .get() when assigning to variables; r=froydnj (a61ad819f0)
- Bug 1193762 part 8 - Fix things that will break; r=froydnj (40a76db662)
- Bug 1193762 part 9 - Delete nsCOMPtr<T>::operator T*()&&; r=froydnj (022c1f0dba)
- Bug 1168223 - Fix a crash caused by unexpected flushes under nsIEditor::SetFlags. r=ehsan (cc49d468b8)
- Bug 1158837 - Ensure the start and end nodes are non-null before using them. r=roc (7c28121aa7)
- Bug 1269047 - Remove chained ENSURE calls from GetStartNodeAndOffset. r=ehsan (f36c1c67aa)
- Bug 1248078 - Remove the dedicated scroll acknowledgement message. r=botond (500b8d7574)
- Bug 1260806: Remove some more dead MessageLoop code. r=jld (1c223cafe8)
- Bug 1266595: Replace Chromium Task with Runnable. r=froydnj (5c1c056c83)
- Bug 1242343 - p1. ConstructSystem32Path from LoadLibrarySystem32 - r=jimm (b28fcbc0c4)
- Bug 1242343 - p2. Blacklist msmpeg2vdec.dll 12.0.9200.16426 & .17037 - r=cpearce (9a71795ab0)
- Bug 1253395 - Disable msmpeg2vdec.dll blackslisting - r=cpearce (e1d4dc47be)
- Bug 1242456 - Create RAIIs to manage HGLOBAL and printer HANDLE in ShowNativePrintDialog and CreateGlobalDevModeAndInit. r=jimm, r=bobowen (a22cecaebd)
- Bug 1262399 - remove Impl suffixes from nsSupports* implementations; r=mccr8 (6733db9394)
- Bug 1268772 (part 1) - Remove nsCheapSet::Put()'s return value. r=erahm. (5f3f553e84)
- Bug 1268772 (part 2) - Make infallible nsVariant methods return |void| instead of |nsresult|. r=erahm. (22ae74fdaf)
- Bug 1268772 (part 3) - Remove NS_NewWindowsRegKey()'s return value. r=erahm. (dc32bd698e)
- Bug 1268772 (part 4) - Use MOZ_MUST_USE with NS_NewISupportsArray(). r=erahm. (e51ecfdf9f)
- Bug 1268772 (part 5) - Use MOZ_MUST_USE in other parts of xpcom/ds/. r=erahm. (4d8a5183eb)
- Bug 1266027 part 1 - make the MediaDecoderReaderWrapper as a proxy of requesting media data; r=jwwang (fbaa79019d)
- Bug 1195601 - Remove MediaDecoderStateMachine::mLogicallySeeking. r=kinetik. (5ab1ff1ca0)
- Bug 1266027 part 2 - make MDSM and SeekTask to adopt new MediaDecoderReaderWrapper API; r=jwwang (1961e26e80)
- add mac specific dupes (4e4ff4b123)
- Bug 1269262 - Unbreak build without NO_EXPAND_LIBS in js/src/moz.build after bug 1239083. r=ted (46d2dec64a)
- Bug 1253215 - Initialize RequestSyncService only if its pref is enabled, r=ehsan (aec1b2f889)
- Bug 1269303 - Remove RequestSync API, r=fabrice (f774336e34)
2024-08-26 16:01:17 +08:00

515 lines
14 KiB
C++

/* -*- 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 "nsDumpUtils.h"
#include "nsDirectoryServiceDefs.h"
#include "nsDirectoryServiceUtils.h"
#include "prenv.h"
#include <errno.h>
#include "mozilla/Services.h"
#include "nsIObserverService.h"
#include "mozilla/ClearOnShutdown.h"
#include "mozilla/unused.h"
#ifdef XP_UNIX // {
#include "mozilla/Preferences.h"
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
using namespace mozilla;
/*
* The following code supports triggering a registered callback upon
* receiving a specific signal.
*
* Take about:memory for example, we register
* 1. doGCCCDump for doMemoryReport
* 2. doMemoryReport for sDumpAboutMemorySignum(SIGRTMIN)
* and sDumpAboutMemoryAfterMMUSignum(SIGRTMIN+1).
*
* When we receive one of these signals, we write the signal number to a pipe.
* The IO thread then notices that the pipe has been written to, and kicks off
* the appropriate task on the main thread.
*
* This scheme is similar to using signalfd(), except it's portable and it
* doesn't require the use of sigprocmask, which is problematic because it
* masks signals received by child processes.
*
* In theory, we could use Chromium's MessageLoopForIO::CatchSignal() for this.
* But that uses libevent, which does not handle the realtime signals (bug
* 794074).
*/
// This is the write-end of a pipe that we use to notice when a
// specific signal occurs.
static Atomic<int> sDumpPipeWriteFd(-1);
const char* const FifoWatcher::kPrefName =
"memory_info_dumper.watch_fifo.enabled";
static void
DumpSignalHandler(int aSignum)
{
// This is a signal handler, so everything in here needs to be
// async-signal-safe. Be careful!
if (sDumpPipeWriteFd != -1) {
uint8_t signum = static_cast<int>(aSignum);
Unused << write(sDumpPipeWriteFd, &signum, sizeof(signum));
}
}
NS_IMPL_ISUPPORTS(FdWatcher, nsIObserver);
void
FdWatcher::Init()
{
MOZ_ASSERT(NS_IsMainThread());
nsCOMPtr<nsIObserverService> os = services::GetObserverService();
os->AddObserver(this, "xpcom-shutdown", /* ownsWeak = */ false);
XRE_GetIOMessageLoop()->PostTask(
NewRunnableMethod(this, &FdWatcher::StartWatching));
}
// Implementations may call this function multiple times if they ensure that
// it's safe to call OpenFd() multiple times and they call StopWatching()
// first.
void
FdWatcher::StartWatching()
{
MOZ_ASSERT(XRE_GetIOMessageLoop() == MessageLoopForIO::current());
MOZ_ASSERT(mFd == -1);
mFd = OpenFd();
if (mFd == -1) {
LOG("FdWatcher: OpenFd failed.");
return;
}
MessageLoopForIO::current()->WatchFileDescriptor(
mFd, /* persistent = */ true,
MessageLoopForIO::WATCH_READ,
&mReadWatcher, this);
}
// Since implementations can call StartWatching() multiple times, they can of
// course call StopWatching() multiple times.
void
FdWatcher::StopWatching()
{
MOZ_ASSERT(XRE_GetIOMessageLoop() == MessageLoopForIO::current());
mReadWatcher.StopWatchingFileDescriptor();
if (mFd != -1) {
close(mFd);
mFd = -1;
}
}
StaticRefPtr<SignalPipeWatcher> SignalPipeWatcher::sSingleton;
/* static */ SignalPipeWatcher*
SignalPipeWatcher::GetSingleton()
{
if (!sSingleton) {
sSingleton = new SignalPipeWatcher();
sSingleton->Init();
ClearOnShutdown(&sSingleton);
}
return sSingleton;
}
void
SignalPipeWatcher::RegisterCallback(uint8_t aSignal,
PipeCallback aCallback)
{
MutexAutoLock lock(mSignalInfoLock);
for (SignalInfoArray::index_type i = 0; i < mSignalInfo.Length(); ++i) {
if (mSignalInfo[i].mSignal == aSignal) {
LOG("Register Signal(%d) callback failed! (DUPLICATE)", aSignal);
return;
}
}
SignalInfo signalInfo = { aSignal, aCallback };
mSignalInfo.AppendElement(signalInfo);
RegisterSignalHandler(signalInfo.mSignal);
}
void
SignalPipeWatcher::RegisterSignalHandler(uint8_t aSignal)
{
struct sigaction action;
memset(&action, 0, sizeof(action));
sigemptyset(&action.sa_mask);
action.sa_handler = DumpSignalHandler;
if (aSignal) {
if (sigaction(aSignal, &action, nullptr)) {
LOG("SignalPipeWatcher failed to register sig %d.", aSignal);
}
} else {
MutexAutoLock lock(mSignalInfoLock);
for (SignalInfoArray::index_type i = 0; i < mSignalInfo.Length(); i++) {
if (sigaction(mSignalInfo[i].mSignal, &action, nullptr)) {
LOG("SignalPipeWatcher failed to register signal(%d) "
"dump signal handler.", mSignalInfo[i].mSignal);
}
}
}
}
SignalPipeWatcher::~SignalPipeWatcher()
{
if (sDumpPipeWriteFd != -1) {
StopWatching();
}
}
int
SignalPipeWatcher::OpenFd()
{
MOZ_ASSERT(XRE_GetIOMessageLoop() == MessageLoopForIO::current());
// Create a pipe. When we receive a signal in our signal handler, we'll
// write the signum to the write-end of this pipe.
int pipeFds[2];
if (pipe(pipeFds)) {
LOG("SignalPipeWatcher failed to create pipe.");
return -1;
}
// Close this pipe on calls to exec().
fcntl(pipeFds[0], F_SETFD, FD_CLOEXEC);
fcntl(pipeFds[1], F_SETFD, FD_CLOEXEC);
int readFd = pipeFds[0];
sDumpPipeWriteFd = pipeFds[1];
RegisterSignalHandler();
return readFd;
}
void
SignalPipeWatcher::StopWatching()
{
MOZ_ASSERT(XRE_GetIOMessageLoop() == MessageLoopForIO::current());
// Close sDumpPipeWriteFd /after/ setting the fd to -1.
// Otherwise we have the (admittedly far-fetched) race where we
//
// 1) close sDumpPipeWriteFd
// 2) open a new fd with the same number as sDumpPipeWriteFd
// had.
// 3) receive a signal, then write to the fd.
int pipeWriteFd = sDumpPipeWriteFd.exchange(-1);
close(pipeWriteFd);
FdWatcher::StopWatching();
}
void
SignalPipeWatcher::OnFileCanReadWithoutBlocking(int aFd)
{
MOZ_ASSERT(XRE_GetIOMessageLoop() == MessageLoopForIO::current());
uint8_t signum;
ssize_t numReceived = read(aFd, &signum, sizeof(signum));
if (numReceived != sizeof(signum)) {
LOG("Error reading from buffer in "
"SignalPipeWatcher::OnFileCanReadWithoutBlocking.");
return;
}
{
MutexAutoLock lock(mSignalInfoLock);
for (SignalInfoArray::index_type i = 0; i < mSignalInfo.Length(); i++) {
if (signum == mSignalInfo[i].mSignal) {
mSignalInfo[i].mCallback(signum);
return;
}
}
}
LOG("SignalPipeWatcher got unexpected signum.");
}
StaticRefPtr<FifoWatcher> FifoWatcher::sSingleton;
/* static */ FifoWatcher*
FifoWatcher::GetSingleton()
{
if (!sSingleton) {
nsAutoCString dirPath;
Preferences::GetCString(
"memory_info_dumper.watch_fifo.directory", &dirPath);
sSingleton = new FifoWatcher(dirPath);
sSingleton->Init();
ClearOnShutdown(&sSingleton);
}
return sSingleton;
}
/* static */ bool
FifoWatcher::MaybeCreate()
{
MOZ_ASSERT(NS_IsMainThread());
if (!XRE_IsParentProcess()) {
// We want this to be main-process only, since two processes can't listen
// to the same fifo.
return false;
}
if (!Preferences::GetBool(kPrefName, false)) {
LOG("Fifo watcher disabled via pref.");
return false;
}
// The FifoWatcher is held alive by the observer service.
if (!sSingleton) {
GetSingleton();
}
return true;
}
void
FifoWatcher::RegisterCallback(const nsCString& aCommand, FifoCallback aCallback)
{
MutexAutoLock lock(mFifoInfoLock);
for (FifoInfoArray::index_type i = 0; i < mFifoInfo.Length(); ++i) {
if (mFifoInfo[i].mCommand.Equals(aCommand)) {
LOG("Register command(%s) callback failed! (DUPLICATE)", aCommand.get());
return;
}
}
FifoInfo aFifoInfo = { aCommand, aCallback };
mFifoInfo.AppendElement(aFifoInfo);
}
FifoWatcher::~FifoWatcher()
{
}
int
FifoWatcher::OpenFd()
{
// If the memory_info_dumper.directory pref is specified, put the fifo
// there. Otherwise, put it into the system's tmp directory.
nsCOMPtr<nsIFile> file;
nsresult rv;
if (mDirPath.Length() > 0) {
rv = XRE_GetFileFromPath(mDirPath.get(), getter_AddRefs(file));
if (NS_FAILED(rv)) {
LOG("FifoWatcher failed to open file \"%s\"", mDirPath.get());
return -1;
}
} else {
rv = NS_GetSpecialDirectory(NS_OS_TEMP_DIR, getter_AddRefs(file));
if (NS_WARN_IF(NS_FAILED(rv))) {
return -1;
}
}
rv = file->AppendNative(NS_LITERAL_CSTRING("debug_info_trigger"));
if (NS_WARN_IF(NS_FAILED(rv))) {
return -1;
}
nsAutoCString path;
rv = file->GetNativePath(path);
if (NS_WARN_IF(NS_FAILED(rv))) {
return -1;
}
// unlink might fail because the file doesn't exist, or for other reasons.
// But we don't care it fails; any problems will be detected later, when we
// try to mkfifo or open the file.
if (unlink(path.get())) {
LOG("FifoWatcher::OpenFifo unlink failed; errno=%d. "
"Continuing despite error.", errno);
}
if (mkfifo(path.get(), 0766)) {
LOG("FifoWatcher::OpenFifo mkfifo failed; errno=%d", errno);
return -1;
}
#ifdef ANDROID
// Android runs with a umask, so we need to chmod our fifo to make it
// world-writable.
chmod(path.get(), 0666);
#endif
int fd;
do {
// The fifo will block until someone else has written to it. In
// particular, open() will block until someone else has opened it for
// writing! We want open() to succeed and read() to block, so we open
// with NONBLOCK and then fcntl that away.
fd = open(path.get(), O_RDONLY | O_NONBLOCK);
} while (fd == -1 && errno == EINTR);
if (fd == -1) {
LOG("FifoWatcher::OpenFifo open failed; errno=%d", errno);
return -1;
}
// Make fd blocking now that we've opened it.
if (fcntl(fd, F_SETFL, 0)) {
close(fd);
return -1;
}
return fd;
}
void
FifoWatcher::OnFileCanReadWithoutBlocking(int aFd)
{
MOZ_ASSERT(XRE_GetIOMessageLoop() == MessageLoopForIO::current());
char buf[1024];
int nread;
do {
// sizeof(buf) - 1 to leave space for the null-terminator.
nread = read(aFd, buf, sizeof(buf));
} while (nread == -1 && errno == EINTR);
if (nread == -1) {
// We want to avoid getting into a situation where
// OnFileCanReadWithoutBlocking is called in an infinite loop, so when
// something goes wrong, stop watching the fifo altogether.
LOG("FifoWatcher hit an error (%d) and is quitting.", errno);
StopWatching();
return;
}
if (nread == 0) {
// If we get EOF, that means that the other side closed the fifo. We need
// to close and re-open the fifo; if we don't,
// OnFileCanWriteWithoutBlocking will be called in an infinite loop.
LOG("FifoWatcher closing and re-opening fifo.");
StopWatching();
StartWatching();
return;
}
nsAutoCString inputStr;
inputStr.Append(buf, nread);
// Trimming whitespace is important because if you do
// |echo "foo" >> debug_info_trigger|,
// it'll actually write "foo\n" to the fifo.
inputStr.Trim("\b\t\r\n");
{
MutexAutoLock lock(mFifoInfoLock);
for (FifoInfoArray::index_type i = 0; i < mFifoInfo.Length(); i++) {
const nsCString commandStr = mFifoInfo[i].mCommand;
if (inputStr == commandStr.get()) {
mFifoInfo[i].mCallback(inputStr);
return;
}
}
}
LOG("Got unexpected value from fifo; ignoring it.");
}
#endif // XP_UNIX }
// In Android case, this function will open a file named aFilename under
// /data/local/tmp/"aFoldername".
// Otherwise, it will open a file named aFilename under "NS_OS_TEMP_DIR".
/* static */ nsresult
nsDumpUtils::OpenTempFile(const nsACString& aFilename, nsIFile** aFile,
const nsACString& aFoldername, Mode aMode)
{
#ifdef ANDROID
// For Android, first try the downloads directory which is world-readable
// rather than the temp directory which is not.
if (!*aFile) {
char* env = PR_GetEnv("DOWNLOADS_DIRECTORY");
if (env) {
NS_NewNativeLocalFile(nsCString(env), /* followLinks = */ true, aFile);
}
}
#endif
nsresult rv;
if (!*aFile) {
rv = NS_GetSpecialDirectory(NS_OS_TEMP_DIR, aFile);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
}
#ifdef ANDROID
// /data/local/tmp is a true tmp directory; anyone can create a file there,
// but only the user which created the file can remove it. We want non-root
// users to be able to remove these files, so we write them into a
// subdirectory of the temp directory and chmod 777 that directory.
if (aFoldername != EmptyCString()) {
rv = (*aFile)->AppendNative(aFoldername);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
// It's OK if this fails; that probably just means that the directory already
// exists.
(*aFile)->Create(nsIFile::DIRECTORY_TYPE, 0777);
nsAutoCString dirPath;
rv = (*aFile)->GetNativePath(dirPath);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
while (chmod(dirPath.get(), 0777) == -1 && errno == EINTR) {
}
}
#endif
nsCOMPtr<nsIFile> file(*aFile);
rv = file->AppendNative(aFilename);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
if (aMode == CREATE_UNIQUE) {
rv = file->CreateUnique(nsIFile::NORMAL_FILE_TYPE, 0666);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
} else {
file->Create(nsIFile::NORMAL_FILE_TYPE, 0666);
}
#ifdef ANDROID
// Make this file world-read/writable; the permissions passed to the
// CreateUnique call above are not sufficient on Android, which runs with a
// umask.
nsAutoCString path;
rv = file->GetNativePath(path);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
while (chmod(path.get(), 0666) == -1 && errno == EINTR) {
}
#endif
return NS_OK;
}