1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #include "FileLocation.h"
6 #include "nsZipArchive.h"
7 #include "nsURLHelper.h"
11 FileLocation::FileLocation()
15 FileLocation::~FileLocation()
19 FileLocation::FileLocation(nsIFile
* file
)
24 FileLocation::FileLocation(nsIFile
* file
, const char *path
)
29 FileLocation::FileLocation(const FileLocation
&file
, const char *path
)
33 Init(file
.mBaseFile
, file
.mPath
.get());
35 Init(file
.mBaseZip
, file
.mPath
.get());
38 int32_t i
= mPath
.RFindChar('/');
42 mPath
.Truncate(i
+ 1);
48 nsCOMPtr
<nsIFile
> cfile
;
49 file
.mBaseFile
->GetParent(getter_AddRefs(cfile
));
52 nsAutoCString
pathStr(path
);
54 uint32_t len
= pathStr
.GetMutableData(&p
);
55 for (; len
; ++p
, --len
) {
60 cfile
->AppendRelativeNativePath(pathStr
);
62 cfile
->AppendRelativeNativePath(nsDependentCString(path
));
72 FileLocation::Init(nsIFile
* file
)
80 FileLocation::Init(nsIFile
* file
, const char* path
)
88 FileLocation::Init(nsZipArchive
* zip
, const char* path
)
96 FileLocation::GetURIString(nsACString
&result
) const
99 net_GetURLSpecFromActualFile(mBaseFile
, result
);
100 } else if (mBaseZip
) {
101 nsRefPtr
<nsZipHandle
> handler
= mBaseZip
->GetFD();
102 handler
->mFile
.GetURIString(result
);
105 result
.Insert("jar:", 0);
111 already_AddRefed
<nsIFile
>
112 FileLocation::GetBaseFile()
114 if (IsZip() && mBaseZip
) {
115 nsRefPtr
<nsZipHandle
> handler
= mBaseZip
->GetFD();
117 return handler
->mFile
.GetBaseFile();
121 nsCOMPtr
<nsIFile
> file
= mBaseFile
;
122 return file
.forget();
126 FileLocation::Equals(const FileLocation
&file
) const
128 if (mPath
!= file
.mPath
)
131 if (mBaseFile
&& file
.mBaseFile
) {
133 return NS_SUCCEEDED(mBaseFile
->Equals(file
.mBaseFile
, &eq
)) && eq
;
136 const FileLocation
*a
= this, *b
= &file
;
138 nsRefPtr
<nsZipHandle
> handler
= a
->mBaseZip
->GetFD();
142 nsRefPtr
<nsZipHandle
> handler
= b
->mBaseZip
->GetFD();
145 return a
->Equals(*b
);
149 FileLocation::GetData(Data
&data
)
152 return mBaseFile
->OpenNSPRFileDesc(PR_RDONLY
, 0444, &data
.mFd
.rwget());
154 data
.mZip
= mBaseZip
;
156 data
.mZip
= new nsZipArchive();
157 data
.mZip
->OpenArchive(mBaseFile
);
159 data
.mItem
= data
.mZip
->GetItem(mPath
.get());
162 return NS_ERROR_FILE_UNRECOGNIZED_PATH
;
166 FileLocation::Data::GetSize(uint32_t *result
)
169 PRFileInfo64 fileInfo
;
170 if (PR_SUCCESS
!= PR_GetOpenFileInfo64(mFd
, &fileInfo
))
171 return NS_ErrorAccordingToNSPR();
173 if (fileInfo
.size
> int64_t(UINT32_MAX
))
174 return NS_ERROR_FILE_TOO_BIG
;
176 *result
= fileInfo
.size
;
179 *result
= mItem
->RealSize();
182 return NS_ERROR_NOT_INITIALIZED
;
186 FileLocation::Data::Copy(char *buf
, uint32_t len
)
189 for (uint32_t totalRead
= 0; totalRead
< len
; ) {
190 int32_t read
= PR_Read(mFd
, buf
+ totalRead
, XPCOM_MIN(len
- totalRead
, uint32_t(INT32_MAX
)));
192 return NS_ErrorAccordingToNSPR();
197 nsZipCursor
cursor(mItem
, mZip
, reinterpret_cast<uint8_t *>(buf
), len
, true);
199 cursor
.Copy(&readLen
);
200 return (readLen
== len
) ? NS_OK
: NS_ERROR_FILE_CORRUPTED
;
202 return NS_ERROR_NOT_INITIALIZED
;
205 } /* namespace mozilla */