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
|
/* -*- 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"
#include "nsZipArchive.h"
#include "nsURLHelper.h"
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());
}
else {
Init(aFile.mBaseZip, aFile.mPath.get());
}
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)
{
mBaseZip = nullptr;
mBaseFile = aFile;
mPath.Truncate();
}
void
FileLocation::Init(nsIFile* aFile, const char* aPath)
{
mBaseZip = nullptr;
mBaseFile = aFile;
mPath = aPath;
}
void
FileLocation::Init(nsZipArchive* aZip, const char* aPath)
{
mBaseZip = aZip;
mBaseFile = nullptr;
mPath = aPath;
}
void
FileLocation::GetURIString(nsACString& aResult) const
{
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;
}
}
already_AddRefed<nsIFile>
FileLocation::GetBaseFile()
{
if (IsZip() && mBaseZip) {
RefPtr<nsZipHandle> handler = mBaseZip->GetFD();
if (handler) {
return handler->mFile.GetBaseFile();
}
return nullptr;
}
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 (a->mBaseZip) {
RefPtr<nsZipHandle> handler = a->mBaseZip->GetFD();
a = &handler->mFile;
}
if (b->mBaseZip) {
RefPtr<nsZipHandle> handler = b->mBaseZip->GetFD();
b = &handler->mFile;
}
return a->Equals(*b);
}
nsresult
FileLocation::GetData(Data& aData)
{
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;
}
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;
}
else if (mItem) {
*aResult = mItem->RealSize();
return NS_OK;
}
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;
}
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;
}
return NS_ERROR_NOT_INITIALIZED;
}
} /* namespace mozilla */
|