2012-05-21 11:12:37 +00:00
|
|
|
/* 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/. */
|
2011-12-08 10:03:36 +00:00
|
|
|
|
|
|
|
#include "FileLocation.h"
|
|
|
|
#include "nsZipArchive.h"
|
|
|
|
#include "nsURLHelper.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2013-09-23 17:26:47 +00:00
|
|
|
FileLocation::FileLocation()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
FileLocation::~FileLocation()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
FileLocation::FileLocation(nsIFile* file)
|
|
|
|
{
|
|
|
|
Init(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
FileLocation::FileLocation(nsIFile* file, const char *path)
|
|
|
|
{
|
|
|
|
Init(file, path);
|
|
|
|
}
|
|
|
|
|
2011-12-08 10:03:36 +00:00
|
|
|
FileLocation::FileLocation(const FileLocation &file, const char *path)
|
|
|
|
{
|
|
|
|
if (file.IsZip()) {
|
|
|
|
if (file.mBaseFile) {
|
|
|
|
Init(file.mBaseFile, file.mPath.get());
|
|
|
|
} else {
|
|
|
|
Init(file.mBaseZip, file.mPath.get());
|
|
|
|
}
|
|
|
|
if (path) {
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t i = mPath.RFindChar('/');
|
2011-12-08 10:03:36 +00:00
|
|
|
if (kNotFound == i) {
|
|
|
|
mPath.Truncate(0);
|
|
|
|
} else {
|
|
|
|
mPath.Truncate(i + 1);
|
|
|
|
}
|
|
|
|
mPath += path;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (path) {
|
|
|
|
nsCOMPtr<nsIFile> cfile;
|
|
|
|
file.mBaseFile->GetParent(getter_AddRefs(cfile));
|
|
|
|
|
|
|
|
#if defined(XP_WIN) || defined(XP_OS2)
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString pathStr(path);
|
2011-12-08 10:03:36 +00:00
|
|
|
char *p;
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t len = pathStr.GetMutableData(&p);
|
2011-12-08 10:03:36 +00:00
|
|
|
for (; len; ++p, --len) {
|
|
|
|
if ('/' == *p) {
|
|
|
|
*p = '\\';
|
|
|
|
}
|
|
|
|
}
|
2012-06-06 02:08:30 +00:00
|
|
|
cfile->AppendRelativeNativePath(pathStr);
|
2011-12-08 10:03:36 +00:00
|
|
|
#else
|
2012-06-06 02:08:30 +00:00
|
|
|
cfile->AppendRelativeNativePath(nsDependentCString(path));
|
2011-12-08 10:03:36 +00:00
|
|
|
#endif
|
2012-06-06 02:08:30 +00:00
|
|
|
Init(cfile);
|
2011-12-08 10:03:36 +00:00
|
|
|
} else {
|
|
|
|
Init(file.mBaseFile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-23 17:26:47 +00:00
|
|
|
void
|
|
|
|
FileLocation::Init(nsIFile* file)
|
|
|
|
{
|
2013-10-10 20:41:00 +00:00
|
|
|
mBaseZip = nullptr;
|
2013-09-23 17:26:47 +00:00
|
|
|
mBaseFile = file;
|
|
|
|
mPath.Truncate();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FileLocation::Init(nsIFile* file, const char* path)
|
|
|
|
{
|
2013-10-10 20:41:00 +00:00
|
|
|
mBaseZip = nullptr;
|
2013-09-23 17:26:47 +00:00
|
|
|
mBaseFile = file;
|
|
|
|
mPath = path;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FileLocation::Init(nsZipArchive* zip, const char* path)
|
|
|
|
{
|
|
|
|
mBaseZip = zip;
|
2013-10-10 20:41:00 +00:00
|
|
|
mBaseFile = nullptr;
|
2013-09-23 17:26:47 +00:00
|
|
|
mPath = path;
|
|
|
|
}
|
|
|
|
|
2011-12-08 10:03:36 +00:00
|
|
|
void
|
|
|
|
FileLocation::GetURIString(nsACString &result) const
|
|
|
|
{
|
|
|
|
if (mBaseFile) {
|
|
|
|
net_GetURLSpecFromActualFile(mBaseFile, result);
|
|
|
|
} else if (mBaseZip) {
|
|
|
|
nsRefPtr<nsZipHandle> handler = mBaseZip->GetFD();
|
|
|
|
handler->mFile.GetURIString(result);
|
|
|
|
}
|
|
|
|
if (IsZip()) {
|
|
|
|
result.Insert("jar:", 0);
|
|
|
|
result += "!/";
|
|
|
|
result += mPath;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-06 02:08:30 +00:00
|
|
|
already_AddRefed<nsIFile>
|
2011-12-08 10:03:36 +00:00
|
|
|
FileLocation::GetBaseFile()
|
|
|
|
{
|
|
|
|
if (IsZip() && mBaseZip) {
|
|
|
|
nsRefPtr<nsZipHandle> handler = mBaseZip->GetFD();
|
|
|
|
if (handler)
|
|
|
|
return handler->mFile.GetBaseFile();
|
2013-06-14 03:57:47 +00:00
|
|
|
return nullptr;
|
2011-12-08 10:03:36 +00:00
|
|
|
}
|
|
|
|
|
2012-06-06 02:08:30 +00:00
|
|
|
nsCOMPtr<nsIFile> file = mBaseFile;
|
2011-12-08 10:03:36 +00:00
|
|
|
return file.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
FileLocation::Equals(const FileLocation &file) const
|
|
|
|
{
|
|
|
|
if (mPath != file.mPath)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (mBaseFile && file.mBaseFile) {
|
|
|
|
bool eq;
|
|
|
|
return NS_SUCCEEDED(mBaseFile->Equals(file.mBaseFile, &eq)) && eq;
|
|
|
|
}
|
|
|
|
|
|
|
|
const FileLocation *a = this, *b = &file;
|
|
|
|
if (a->mBaseZip) {
|
|
|
|
nsRefPtr<nsZipHandle> handler = a->mBaseZip->GetFD();
|
|
|
|
a = &handler->mFile;
|
|
|
|
}
|
|
|
|
if (b->mBaseZip) {
|
|
|
|
nsRefPtr<nsZipHandle> handler = b->mBaseZip->GetFD();
|
|
|
|
b = &handler->mFile;
|
|
|
|
}
|
|
|
|
return a->Equals(*b);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
FileLocation::GetData(Data &data)
|
|
|
|
{
|
|
|
|
if (!IsZip()) {
|
2012-04-12 10:21:24 +00:00
|
|
|
return mBaseFile->OpenNSPRFileDesc(PR_RDONLY, 0444, &data.mFd.rwget());
|
2011-12-08 10:03:36 +00:00
|
|
|
}
|
|
|
|
data.mZip = mBaseZip;
|
|
|
|
if (!data.mZip) {
|
|
|
|
data.mZip = new nsZipArchive();
|
|
|
|
data.mZip->OpenArchive(mBaseFile);
|
|
|
|
}
|
|
|
|
data.mItem = data.mZip->GetItem(mPath.get());
|
|
|
|
if (data.mItem)
|
|
|
|
return NS_OK;
|
|
|
|
return NS_ERROR_FILE_UNRECOGNIZED_PATH;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-08-22 15:56:38 +00:00
|
|
|
FileLocation::Data::GetSize(uint32_t *result)
|
2011-12-08 10:03:36 +00:00
|
|
|
{
|
|
|
|
if (mFd) {
|
|
|
|
PRFileInfo64 fileInfo;
|
|
|
|
if (PR_SUCCESS != PR_GetOpenFileInfo64(mFd, &fileInfo))
|
|
|
|
return NS_ErrorAccordingToNSPR();
|
|
|
|
|
2012-09-28 06:57:33 +00:00
|
|
|
if (fileInfo.size > int64_t(UINT32_MAX))
|
2011-12-08 10:03:36 +00:00
|
|
|
return NS_ERROR_FILE_TOO_BIG;
|
|
|
|
|
|
|
|
*result = fileInfo.size;
|
|
|
|
return NS_OK;
|
|
|
|
} else if (mItem) {
|
|
|
|
*result = mItem->RealSize();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-08-22 15:56:38 +00:00
|
|
|
FileLocation::Data::Copy(char *buf, uint32_t len)
|
2011-12-08 10:03:36 +00:00
|
|
|
{
|
|
|
|
if (mFd) {
|
2012-08-22 15:56:38 +00:00
|
|
|
for (uint32_t totalRead = 0; totalRead < len; ) {
|
2013-01-15 12:22:03 +00:00
|
|
|
int32_t read = PR_Read(mFd, buf + totalRead, XPCOM_MIN(len - totalRead, uint32_t(INT32_MAX)));
|
2011-12-08 10:03:36 +00:00
|
|
|
if (read < 0)
|
|
|
|
return NS_ErrorAccordingToNSPR();
|
|
|
|
totalRead += read;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
} else if (mItem) {
|
2012-08-22 15:56:38 +00:00
|
|
|
nsZipCursor cursor(mItem, mZip, reinterpret_cast<uint8_t *>(buf), len, true);
|
|
|
|
uint32_t readLen;
|
2011-12-08 10:03:36 +00:00
|
|
|
cursor.Copy(&readLen);
|
|
|
|
return (readLen == len) ? NS_OK : NS_ERROR_FILE_CORRUPTED;
|
|
|
|
}
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
|
|
|
|
|
|
|
} /* namespace mozilla */
|