1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* vim:set sw=4 sts=4 et cin: */
3 /* ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
16 * The Original Code is mozilla.org code.
18 * The Initial Developer of the Original Code is
19 * Netscape Communications Corporation.
20 * Portions created by the Initial Developer are Copyright (C) 1998
21 * the Initial Developer. All Rights Reserved.
24 * Bradley Baetz <bbaetz@cs.mcgill.ca>
26 * Alternatively, the contents of this file may be used under the terms of
27 * either the GNU General Public License Version 2 or later (the "GPL"), or
28 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
43 The converts a filesystem directory into an "HTTP index" stream per
44 Lou Montulli's original spec:
46 http://www.mozilla.org/projects/netlib/dirindexformat.html
51 #include "nsDirectoryIndexStream.h"
52 #include "nsXPIDLString.h"
57 static PRLogModuleInfo
* gLog
;
60 #include "nsISimpleEnumerator.h"
61 #include "nsICollation.h"
62 #include "nsILocale.h"
63 #include "nsILocaleService.h"
64 #include "nsCollationCID.h"
65 #include "nsIPlatformCharset.h"
66 #include "nsReadableUtils.h"
67 #include "nsURLHelper.h"
68 #include "nsNetUtil.h"
70 #include "nsNativeCharsetUtils.h"
72 // NOTE: This runs on the _file transport_ thread.
73 // The problem is that now that we're actually doing something with the data,
74 // we want to do stuff like i18n sorting. However, none of the collation stuff
76 // So THIS CODE IS ASCII ONLY!!!!!!!! This is no worse than the current
77 // behaviour, though. See bug 99382.
78 // When this is fixed, #define THREADSAFE_I18N to get this code working
80 //#define THREADSAFE_I18N
82 nsDirectoryIndexStream::nsDirectoryIndexStream()
83 : mOffset(0), mStatus(NS_OK
), mPos(0)
87 gLog
= PR_NewLogModule("nsDirectoryIndexStream");
90 PR_LOG(gLog
, PR_LOG_DEBUG
,
91 ("nsDirectoryIndexStream[%p]: created", this));
94 static int PR_CALLBACK
compare(nsIFile
* aElement1
,
98 if (!NS_IsNativeUTF8()) {
99 // don't check for errors, because we can't report them anyway
100 nsAutoString name1
, name2
;
101 aElement1
->GetLeafName(name1
);
102 aElement2
->GetLeafName(name2
);
104 // Note - we should do the collation to do sorting. Why don't we?
105 // Because that is _slow_. Using TestProtocols to list file:///dev/
106 // goes from 3 seconds to 22. (This may be why nsXULSortService is
108 // Does this have bad effects? Probably, but since nsXULTree appears
109 // to use the raw RDF literal value as the sort key (which ammounts to an
110 // strcmp), it won't be any worse, I think.
111 // This could be made faster, by creating the keys once,
112 // but CompareString could still be smarter - see bug 99383 - bbaetz
113 // NB - 99393 has been WONTFIXed. So if the I18N code is ever made
114 // threadsafe so that this matters, we'd have to pass through a
115 // struct { nsIFile*, PRUint8* } with the pre-calculated key.
116 return Compare(name1
, name2
);
119 nsCAutoString name1
, name2
;
120 aElement1
->GetNativeLeafName(name1
);
121 aElement2
->GetNativeLeafName(name2
);
123 return Compare(name1
, name2
);
127 nsDirectoryIndexStream::Init(nsIFile
* aDir
)
131 rv
= aDir
->IsDirectory(&isDir
);
132 if (NS_FAILED(rv
)) return rv
;
133 NS_PRECONDITION(isDir
, "not a directory");
135 return NS_ERROR_ILLEGAL_VALUE
;
138 if (PR_LOG_TEST(gLog
, PR_LOG_DEBUG
)) {
140 aDir
->GetNativePath(path
);
141 PR_LOG(gLog
, PR_LOG_DEBUG
,
142 ("nsDirectoryIndexStream[%p]: initialized on %s",
147 // Sigh. We have to allocate on the heap because there are no
148 // assignment operators defined.
149 nsCOMPtr
<nsISimpleEnumerator
> iter
;
150 rv
= aDir
->GetDirectoryEntries(getter_AddRefs(iter
));
151 if (NS_FAILED(rv
)) return rv
;
153 // Now lets sort, because clients expect it that way
154 // XXX - should we do so here, or when the first item is requested?
155 // XXX - use insertion sort instead?
158 nsCOMPtr
<nsISupports
> elem
;
159 while (NS_SUCCEEDED(iter
->HasMoreElements(&more
)) && more
) {
160 rv
= iter
->GetNext(getter_AddRefs(elem
));
161 if (NS_SUCCEEDED(rv
)) {
162 nsCOMPtr
<nsIFile
> file
= do_QueryInterface(elem
);
164 mArray
.AppendObject(file
); // addrefs
168 #ifdef THREADSAFE_I18N
169 nsCOMPtr
<nsILocaleService
> ls
= do_GetService(NS_LOCALESERVICE_CONTRACTID
,
171 if (NS_FAILED(rv
)) return rv
;
173 nsCOMPtr
<nsILocale
> locale
;
174 rv
= ls
->GetApplicationLocale(getter_AddRefs(locale
));
175 if (NS_FAILED(rv
)) return rv
;
177 nsCOMPtr
<nsICollationFactory
> cf
= do_CreateInstance(NS_COLLATIONFACTORY_CONTRACTID
,
179 if (NS_FAILED(rv
)) return rv
;
181 nsCOMPtr
<nsICollation
> coll
;
182 rv
= cf
->CreateCollation(locale
, getter_AddRefs(coll
));
183 if (NS_FAILED(rv
)) return rv
;
185 mArray
.Sort(compare
, coll
);
187 mArray
.Sort(compare
, nsnull
);
190 mBuf
.AppendLiteral("300: ");
192 rv
= net_GetURLSpecFromFile(aDir
, url
);
193 if (NS_FAILED(rv
)) return rv
;
197 mBuf
.AppendLiteral("200: filename content-length last-modified file-type\n");
202 nsDirectoryIndexStream::~nsDirectoryIndexStream()
204 PR_LOG(gLog
, PR_LOG_DEBUG
,
205 ("nsDirectoryIndexStream[%p]: destroyed", this));
209 nsDirectoryIndexStream::Create(nsIFile
* aDir
, nsIInputStream
** aResult
)
211 nsDirectoryIndexStream
* result
= new nsDirectoryIndexStream();
213 return NS_ERROR_OUT_OF_MEMORY
;
216 rv
= result
->Init(aDir
);
227 NS_IMPL_THREADSAFE_ISUPPORTS1(nsDirectoryIndexStream
, nsIInputStream
)
229 // The below routines are proxied to the UI thread!
231 nsDirectoryIndexStream::Close()
233 mStatus
= NS_BASE_STREAM_CLOSED
;
238 nsDirectoryIndexStream::Available(PRUint32
* aLength
)
240 if (NS_FAILED(mStatus
))
243 // If there's data in our buffer, use that
244 if (mOffset
< (PRInt32
)mBuf
.Length()) {
245 *aLength
= mBuf
.Length() - mOffset
;
249 // Returning one byte is not ideal, but good enough
250 *aLength
= (mPos
< mArray
.Count()) ? 1 : 0;
255 nsDirectoryIndexStream::Read(char* aBuf
, PRUint32 aCount
, PRUint32
* aReadCount
)
257 if (mStatus
== NS_BASE_STREAM_CLOSED
) {
261 if (NS_FAILED(mStatus
))
266 // If anything is enqueued (or left-over) in mBuf, then feed it to
268 while (mOffset
< (PRInt32
)mBuf
.Length() && aCount
!= 0) {
269 *(aBuf
++) = char(mBuf
.CharAt(mOffset
++));
279 // Okay, now we'll suck stuff off of our iterator into the mBuf...
280 while (PRUint32(mBuf
.Length()) < aCount
) {
281 PRBool more
= mPos
< mArray
.Count();
284 // don't addref, for speed - an addref happened when it
285 // was placed in the array, so it's not going to go stale
286 nsIFile
* current
= mArray
.ObjectAt(mPos
);
290 if (PR_LOG_TEST(gLog
, PR_LOG_DEBUG
)) {
292 current
->GetNativePath(path
);
293 PR_LOG(gLog
, PR_LOG_DEBUG
,
294 ("nsDirectoryIndexStream[%p]: iterated %s",
299 // rjc: don't return hidden files/directories!
303 PRBool hidden
= PR_FALSE
;
304 current
->IsHidden(&hidden
);
306 PR_LOG(gLog
, PR_LOG_DEBUG
,
307 ("nsDirectoryIndexStream[%p]: skipping hidden file/directory",
313 PRInt64 fileSize
= 0;
314 current
->GetFileSize( &fileSize
);
316 PRInt64 fileInfoModifyTime
= 0;
317 current
->GetLastModifiedTime( &fileInfoModifyTime
);
318 fileInfoModifyTime
*= PR_USEC_PER_MSEC
;
320 mBuf
.AppendLiteral("201: ");
322 // The "filename" field
323 char* escaped
= nsnull
;
324 if (!NS_IsNativeUTF8()) {
325 nsAutoString leafname
;
326 rv
= current
->GetLeafName(leafname
);
327 if (NS_FAILED(rv
)) return rv
;
328 if (!leafname
.IsEmpty())
329 escaped
= nsEscape(NS_ConvertUTF16toUTF8(leafname
).get(), url_Path
);
331 nsCAutoString leafname
;
332 rv
= current
->GetNativeLeafName(leafname
);
333 if (NS_FAILED(rv
)) return rv
;
334 if (!leafname
.IsEmpty())
335 escaped
= nsEscape(leafname
.get(), url_Path
);
340 nsMemory::Free(escaped
);
343 // The "content-length" field
344 mBuf
.AppendInt(fileSize
, 10);
347 // The "last-modified" field
349 PR_ExplodeTime(fileInfoModifyTime
, PR_GMTParameters
, &tm
);
352 PR_FormatTimeUSEnglish(buf
, sizeof(buf
), "%a,%%20%d%%20%b%%20%Y%%20%H:%M:%S%%20GMT ", &tm
);
356 // The "file-type" field
357 PRBool isFile
= PR_TRUE
;
358 current
->IsFile(&isFile
);
360 mBuf
.AppendLiteral("FILE ");
364 rv
= current
->IsDirectory(&isDir
);
365 if (NS_FAILED(rv
)) return rv
;
367 mBuf
.AppendLiteral("DIRECTORY ");
371 rv
= current
->IsSymlink(&isLink
);
372 if (NS_FAILED(rv
)) return rv
;
374 mBuf
.AppendLiteral("SYMBOLIC-LINK ");
382 // ...and once we've either run out of directory entries, or
383 // filled up the buffer, then we'll push it to the reader.
384 while (mOffset
< (PRInt32
)mBuf
.Length() && aCount
!= 0) {
385 *(aBuf
++) = char(mBuf
.CharAt(mOffset
++));
396 nsDirectoryIndexStream::ReadSegments(nsWriteSegmentFun writer
, void * closure
, PRUint32 count
, PRUint32
*_retval
)
398 return NS_ERROR_NOT_IMPLEMENTED
;
402 nsDirectoryIndexStream::IsNonBlocking(PRBool
*aNonBlocking
)
404 *aNonBlocking
= PR_FALSE
;