Files
palemoon27/xpcom/build/FileLocation.cpp
T
roytam1 61b14e0f32 import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1146204 - Build libopus in unified mode; r=cpearce (d4f89d6d30)
- tweak build files a little (ab9bba79a1)
- Bug 1222575 - use UniquePtr<T[]> instead of nsAutoArrayPtr<T> in modules/libjar/; r=aklotz (b723bc0f41)
- Bug 1225004 - Record reason for NS_ERROR_FILE_CORRUPTED in nsLayoutStylesheetCache::LoadSheet in crash reports. r=roc (e78457b15b)
- Bug 1214782, r=ehsan (63513465b5)
- Bug 1153259 - use NS_NewByteInputStream in zipwriter to reduce do_CreateInstance overhead; r=aklotz (e20238389f)
- Bug 1201636 - Use channel->asyncOpen2() in modules/libjar/zipwriter/nsZipWriter.cpp (r=sicking) (79a4741b44)
- Bug 1179069 - Remove docshell warnings in embedding. r=bz (b464524d63)
- Bug 1179058 - Implement shouldAddToSessionHistory in WebBrowserChromeJS object. r=adw (d7382a5f59)
- missing bit of Bug 1164049 - Fix some mode lines in embedding/. and some makefile fixes (9f1dc553d3)
- Bug 1202887 - Delay WebBrowserPersist error callbacks caused by IPC ActorDestroy. r=billm (e552a83758)
- Bug 1106321 - Serialize DEVMODE down to the content process when printing on Windows. r=jimm (bef76e4d3c)
- bug 147419 remove ununsed plexName colorspace resolutionName and downloadFonts r=roc (e010cd1704)
- Bug 1178799 - Filter X11 SelectionRequest events with an invalid requestor on GTK3. r=karlt (4ce7135a77)
- Bug 1227023 - Include the Gtk+3 version in update URL if available on Gtk+2 builds. r=karlt (b9689fdd31)
- Bug 1229099 - use snprintf instead of JS_snprintf in xpcom/; r=mccr8 (2cf37f7d5a)
- Bug 939790 - make SafeMutex::mOwnerThread a relaxed atomic variable; r=bsmedberg (0ffc8a5409)
- Bug 1183093 - Uninitialised value use in Probe::Trigger. r=dteller. (6d645ef81c)
- Bug 1140771 - Build more XPCOM code in unified mode; r=froydnj bug 1170585 - Don't try to build IO Poisoning on iOS. r=froydnj (9c5e98b533)
- Bug 1201287 - Cleanup nsSupportsPrimitives.cpp. r=smaug (5fc766af1a)
- pointer style (6ee79e071f)
- Bug 1225682 - Don't use nsAuto{,C}String as class member variables in js/xpconnect/. r=mccr8 (6e4ca9ebcb)
2023-05-10 15:16:44 +08:00

247 lines
5.5 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 "FileLocation.h"
#if !defined(MOZILLA_XPCOMRT_API)
#include "nsZipArchive.h"
#include "nsURLHelper.h"
#endif // !defined(MOZILLA_XPCOMRT_API)
namespace mozilla {
FileLocation::FileLocation()
{
}
FileLocation::~FileLocation()
{
}
FileLocation::FileLocation(nsIFile* aFile)
{
Init(aFile);
}
FileLocation::FileLocation(nsIFile* aFile, const char* aPath)
{
Init(aFile, aPath);
}
FileLocation::FileLocation(const FileLocation& aFile, const char* aPath)
{
if (aFile.IsZip()) {
if (aFile.mBaseFile) {
Init(aFile.mBaseFile, aFile.mPath.get());
}
#if !defined(MOZILLA_XPCOMRT_API)
else {
Init(aFile.mBaseZip, aFile.mPath.get());
}
#endif
if (aPath) {
int32_t i = mPath.RFindChar('/');
if (kNotFound == i) {
mPath.Truncate(0);
} else {
mPath.Truncate(i + 1);
}
mPath += aPath;
}
} else {
if (aPath) {
nsCOMPtr<nsIFile> cfile;
aFile.mBaseFile->GetParent(getter_AddRefs(cfile));
#if defined(XP_WIN)
nsAutoCString pathStr(aPath);
char* p;
uint32_t len = pathStr.GetMutableData(&p);
for (; len; ++p, --len) {
if ('/' == *p) {
*p = '\\';
}
}
cfile->AppendRelativeNativePath(pathStr);
#else
cfile->AppendRelativeNativePath(nsDependentCString(aPath));
#endif
Init(cfile);
} else {
Init(aFile.mBaseFile);
}
}
}
void
FileLocation::Init(nsIFile* aFile)
{
#if !defined(MOZILLA_XPCOMRT_API)
mBaseZip = nullptr;
#endif //!defined(MOZILLA_XPCOMRT_API)
mBaseFile = aFile;
mPath.Truncate();
}
void
FileLocation::Init(nsIFile* aFile, const char* aPath)
{
#if !defined(MOZILLA_XPCOMRT_API)
mBaseZip = nullptr;
#endif // !defined(MOZILLA_XPCOMRT_API)
mBaseFile = aFile;
mPath = aPath;
}
void
FileLocation::Init(nsZipArchive* aZip, const char* aPath)
{
#if !defined(MOZILLA_XPCOMRT_API)
mBaseZip = aZip;
#endif // !defined(MOZILLA_XPCOMRT_API)
mBaseFile = nullptr;
mPath = aPath;
}
void
FileLocation::GetURIString(nsACString& aResult) const
{
#if !defined(MOZILLA_XPCOMRT_API)
if (mBaseFile) {
net_GetURLSpecFromActualFile(mBaseFile, aResult);
} else if (mBaseZip) {
RefPtr<nsZipHandle> handler = mBaseZip->GetFD();
handler->mFile.GetURIString(aResult);
}
if (IsZip()) {
aResult.Insert("jar:", 0);
aResult += "!/";
aResult += mPath;
}
#endif // !defined(MOZILLA_XPCOMRT_API)
}
already_AddRefed<nsIFile>
FileLocation::GetBaseFile()
{
#if !defined(MOZILLA_XPCOMRT_API)
if (IsZip() && mBaseZip) {
RefPtr<nsZipHandle> handler = mBaseZip->GetFD();
if (handler) {
return handler->mFile.GetBaseFile();
}
return nullptr;
}
#endif // !defined(MOZILLA_XPCOMRT_API)
nsCOMPtr<nsIFile> file = mBaseFile;
return file.forget();
}
bool
FileLocation::Equals(const FileLocation& aFile) const
{
if (mPath != aFile.mPath) {
return false;
}
if (mBaseFile && aFile.mBaseFile) {
bool eq;
return NS_SUCCEEDED(mBaseFile->Equals(aFile.mBaseFile, &eq)) && eq;
}
const FileLocation* a = this;
const FileLocation* b = &aFile;
#if !defined(MOZILLA_XPCOMRT_API)
if (a->mBaseZip) {
RefPtr<nsZipHandle> handler = a->mBaseZip->GetFD();
a = &handler->mFile;
}
if (b->mBaseZip) {
RefPtr<nsZipHandle> handler = b->mBaseZip->GetFD();
b = &handler->mFile;
}
#endif // !defined(MOZILLA_XPCOMRT_API)
return a->Equals(*b);
}
nsresult
FileLocation::GetData(Data& aData)
{
#if !defined(MOZILLA_XPCOMRT_API)
if (!IsZip()) {
return mBaseFile->OpenNSPRFileDesc(PR_RDONLY, 0444, &aData.mFd.rwget());
}
aData.mZip = mBaseZip;
if (!aData.mZip) {
aData.mZip = new nsZipArchive();
aData.mZip->OpenArchive(mBaseFile);
}
aData.mItem = aData.mZip->GetItem(mPath.get());
if (aData.mItem) {
return NS_OK;
}
#endif // !defined(MOZILLA_XPCOMRT_API)
return NS_ERROR_FILE_UNRECOGNIZED_PATH;
}
nsresult
FileLocation::Data::GetSize(uint32_t* aResult)
{
if (mFd) {
PRFileInfo64 fileInfo;
if (PR_SUCCESS != PR_GetOpenFileInfo64(mFd, &fileInfo)) {
return NS_ErrorAccordingToNSPR();
}
if (fileInfo.size > int64_t(UINT32_MAX)) {
return NS_ERROR_FILE_TOO_BIG;
}
*aResult = fileInfo.size;
return NS_OK;
}
#if !defined(MOZILLA_XPCOMRT_API)
else if (mItem) {
*aResult = mItem->RealSize();
return NS_OK;
}
#endif // !defined(MOZILLA_XPCOMRT_API)
return NS_ERROR_NOT_INITIALIZED;
}
nsresult
FileLocation::Data::Copy(char* aBuf, uint32_t aLen)
{
if (mFd) {
for (uint32_t totalRead = 0; totalRead < aLen;) {
int32_t read = PR_Read(mFd, aBuf + totalRead,
XPCOM_MIN(aLen - totalRead, uint32_t(INT32_MAX)));
if (read < 0) {
return NS_ErrorAccordingToNSPR();
}
totalRead += read;
}
return NS_OK;
}
#if !defined(MOZILLA_XPCOMRT_API)
else if (mItem) {
nsZipCursor cursor(mItem, mZip, reinterpret_cast<uint8_t*>(aBuf),
aLen, true);
uint32_t readLen;
cursor.Copy(&readLen);
if (readLen != aLen) {
nsZipArchive::sFileCorruptedReason = "FileLocation::Data: insufficient data";
return NS_ERROR_FILE_CORRUPTED;
}
return NS_OK;
}
#endif // !defined(MOZILLA_XPCOMRT_API)
return NS_ERROR_NOT_INITIALIZED;
}
} /* namespace mozilla */