2012-05-21 04:12:37 -07: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 02:03:36 -08:00
|
|
|
|
|
|
|
#include "FileLocation.h"
|
|
|
|
#include "nsZipArchive.h"
|
|
|
|
#include "nsURLHelper.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
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 08:56:38 -07:00
|
|
|
int32_t i = mPath.RFindChar('/');
|
2011-12-08 02:03:36 -08: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-01 19:35:17 -07:00
|
|
|
nsAutoCString pathStr(path);
|
2011-12-08 02:03:36 -08:00
|
|
|
char *p;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t len = pathStr.GetMutableData(&p);
|
2011-12-08 02:03:36 -08:00
|
|
|
for (; len; ++p, --len) {
|
|
|
|
if ('/' == *p) {
|
|
|
|
*p = '\\';
|
|
|
|
}
|
|
|
|
}
|
2012-06-05 19:08:30 -07:00
|
|
|
cfile->AppendRelativeNativePath(pathStr);
|
2011-12-08 02:03:36 -08:00
|
|
|
#else
|
2012-06-05 19:08:30 -07:00
|
|
|
cfile->AppendRelativeNativePath(nsDependentCString(path));
|
2011-12-08 02:03:36 -08:00
|
|
|
#endif
|
2012-06-05 19:08:30 -07:00
|
|
|
Init(cfile);
|
2011-12-08 02:03:36 -08:00
|
|
|
} else {
|
|
|
|
Init(file.mBaseFile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-05 19:08:30 -07:00
|
|
|
already_AddRefed<nsIFile>
|
2011-12-08 02:03:36 -08:00
|
|
|
FileLocation::GetBaseFile()
|
|
|
|
{
|
|
|
|
if (IsZip() && mBaseZip) {
|
|
|
|
nsRefPtr<nsZipHandle> handler = mBaseZip->GetFD();
|
|
|
|
if (handler)
|
|
|
|
return handler->mFile.GetBaseFile();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-06-05 19:08:30 -07:00
|
|
|
nsCOMPtr<nsIFile> file = mBaseFile;
|
2011-12-08 02:03:36 -08: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 03:21:24 -07:00
|
|
|
return mBaseFile->OpenNSPRFileDesc(PR_RDONLY, 0444, &data.mFd.rwget());
|
2011-12-08 02:03:36 -08: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 08:56:38 -07:00
|
|
|
FileLocation::Data::GetSize(uint32_t *result)
|
2011-12-08 02:03:36 -08:00
|
|
|
{
|
|
|
|
if (mFd) {
|
|
|
|
PRFileInfo64 fileInfo;
|
|
|
|
if (PR_SUCCESS != PR_GetOpenFileInfo64(mFd, &fileInfo))
|
|
|
|
return NS_ErrorAccordingToNSPR();
|
|
|
|
|
2012-09-27 23:57:33 -07:00
|
|
|
if (fileInfo.size > int64_t(UINT32_MAX))
|
2011-12-08 02:03:36 -08: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 08:56:38 -07:00
|
|
|
FileLocation::Data::Copy(char *buf, uint32_t len)
|
2011-12-08 02:03:36 -08:00
|
|
|
{
|
|
|
|
if (mFd) {
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t totalRead = 0; totalRead < len; ) {
|
2012-09-27 23:57:33 -07:00
|
|
|
int32_t read = PR_Read(mFd, buf + totalRead, NS_MIN(len - totalRead, uint32_t(INT32_MAX)));
|
2011-12-08 02:03:36 -08:00
|
|
|
if (read < 0)
|
|
|
|
return NS_ErrorAccordingToNSPR();
|
|
|
|
totalRead += read;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
} else if (mItem) {
|
2012-08-22 08:56:38 -07:00
|
|
|
nsZipCursor cursor(mItem, mZip, reinterpret_cast<uint8_t *>(buf), len, true);
|
|
|
|
uint32_t readLen;
|
2011-12-08 02:03:36 -08:00
|
|
|
cursor.Copy(&readLen);
|
|
|
|
return (readLen == len) ? NS_OK : NS_ERROR_FILE_CORRUPTED;
|
|
|
|
}
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
|
|
|
|
|
|
|
} /* namespace mozilla */
|