Source code

Revision control

Other Tools

1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim:set sw=2 sts=2 et cin: */
3
/* This Source Code Form is subject to the terms of the Mozilla Public
4
* License, v. 2.0. If a copy of the MPL was not distributed with this
5
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
/*
8
9
The converts a filesystem directory into an "HTTP index" stream per
10
Lou Montulli's original spec:
11
13
14
*/
15
16
#include "nsEscape.h"
17
#include "nsDirectoryIndexStream.h"
18
#include "mozilla/Logging.h"
19
#include "prtime.h"
20
#include "nsISimpleEnumerator.h"
21
#ifdef THREADSAFE_I18N
22
# include "nsCollationCID.h"
23
# include "nsICollation.h"
24
#endif
25
#include "nsIFile.h"
26
#include "nsURLHelper.h"
27
#include "nsNativeCharsetUtils.h"
28
29
// NOTE: This runs on the _file transport_ thread.
30
// The problem is that now that we're actually doing something with the data,
31
// we want to do stuff like i18n sorting. However, none of the collation stuff
32
// is threadsafe.
33
// So THIS CODE IS ASCII ONLY!!!!!!!! This is no worse than the current
34
// behaviour, though. See bug 99382.
35
// When this is fixed, #define THREADSAFE_I18N to get this code working
36
37
//#define THREADSAFE_I18N
38
39
using namespace mozilla;
40
static LazyLogModule gLog("nsDirectoryIndexStream");
41
42
nsDirectoryIndexStream::nsDirectoryIndexStream()
43
: mOffset(0), mStatus(NS_OK), mPos(0) {
44
MOZ_LOG(gLog, LogLevel::Debug, ("nsDirectoryIndexStream[%p]: created", this));
45
}
46
47
static int compare(nsIFile* aElement1, nsIFile* aElement2, void* aData) {
48
if (!NS_IsNativeUTF8()) {
49
// don't check for errors, because we can't report them anyway
50
nsAutoString name1, name2;
51
aElement1->GetLeafName(name1);
52
aElement2->GetLeafName(name2);
53
54
// Note - we should do the collation to do sorting. Why don't we?
55
// Because that is _slow_. Using TestProtocols to list file:///dev/
56
// goes from 3 seconds to 22. (This may be why nsXULSortService is
57
// so slow as well).
58
// Does this have bad effects? Probably, but since nsXULTree appears
59
// to use the raw RDF literal value as the sort key (which ammounts to an
60
// strcmp), it won't be any worse, I think.
61
// This could be made faster, by creating the keys once,
62
// but CompareString could still be smarter - see bug 99383 - bbaetz
63
// NB - 99393 has been WONTFIXed. So if the I18N code is ever made
64
// threadsafe so that this matters, we'd have to pass through a
65
// struct { nsIFile*, uint8_t* } with the pre-calculated key.
66
return Compare(name1, name2);
67
}
68
69
nsAutoCString name1, name2;
70
aElement1->GetNativeLeafName(name1);
71
aElement2->GetNativeLeafName(name2);
72
73
return Compare(name1, name2);
74
}
75
76
nsresult nsDirectoryIndexStream::Init(nsIFile* aDir) {
77
nsresult rv;
78
bool isDir;
79
rv = aDir->IsDirectory(&isDir);
80
if (NS_FAILED(rv)) return rv;
81
MOZ_ASSERT(isDir, "not a directory");
82
if (!isDir) return NS_ERROR_ILLEGAL_VALUE;
83
84
if (MOZ_LOG_TEST(gLog, LogLevel::Debug)) {
85
MOZ_LOG(gLog, LogLevel::Debug,
86
("nsDirectoryIndexStream[%p]: initialized on %s", this,
87
aDir->HumanReadablePath().get()));
88
}
89
90
// Sigh. We have to allocate on the heap because there are no
91
// assignment operators defined.
92
nsCOMPtr<nsIDirectoryEnumerator> iter;
93
rv = aDir->GetDirectoryEntries(getter_AddRefs(iter));
94
if (NS_FAILED(rv)) return rv;
95
96
// Now lets sort, because clients expect it that way
97
// XXX - should we do so here, or when the first item is requested?
98
// XXX - use insertion sort instead?
99
100
nsCOMPtr<nsIFile> file;
101
while (NS_SUCCEEDED(iter->GetNextFile(getter_AddRefs(file))) && file) {
102
mArray.AppendObject(file); // addrefs
103
}
104
105
#ifdef THREADSAFE_I18N
106
nsCOMPtr<nsICollationFactory> cf =
107
do_CreateInstance(NS_COLLATIONFACTORY_CONTRACTID, &rv);
108
if (NS_FAILED(rv)) return rv;
109
110
nsCOMPtr<nsICollation> coll;
111
rv = cf->CreateCollation(getter_AddRefs(coll));
112
if (NS_FAILED(rv)) return rv;
113
114
mArray.Sort(compare, coll);
115
#else
116
mArray.Sort(compare, nullptr);
117
#endif
118
119
mBuf.AppendLiteral("300: ");
120
nsAutoCString url;
121
rv = net_GetURLSpecFromFile(aDir, url);
122
if (NS_FAILED(rv)) return rv;
123
mBuf.Append(url);
124
mBuf.Append('\n');
125
126
mBuf.AppendLiteral("200: filename content-length last-modified file-type\n");
127
128
return NS_OK;
129
}
130
131
nsDirectoryIndexStream::~nsDirectoryIndexStream() {
132
MOZ_LOG(gLog, LogLevel::Debug,
133
("nsDirectoryIndexStream[%p]: destroyed", this));
134
}
135
136
nsresult nsDirectoryIndexStream::Create(nsIFile* aDir,
137
nsIInputStream** aResult) {
138
RefPtr<nsDirectoryIndexStream> result = new nsDirectoryIndexStream();
139
if (!result) return NS_ERROR_OUT_OF_MEMORY;
140
141
nsresult rv = result->Init(aDir);
142
if (NS_FAILED(rv)) {
143
return rv;
144
}
145
146
result.forget(aResult);
147
return NS_OK;
148
}
149
150
NS_IMPL_ISUPPORTS(nsDirectoryIndexStream, nsIInputStream)
151
152
// The below routines are proxied to the UI thread!
153
NS_IMETHODIMP
154
nsDirectoryIndexStream::Close() {
155
mStatus = NS_BASE_STREAM_CLOSED;
156
return NS_OK;
157
}
158
159
NS_IMETHODIMP
160
nsDirectoryIndexStream::Available(uint64_t* aLength) {
161
if (NS_FAILED(mStatus)) return mStatus;
162
163
// If there's data in our buffer, use that
164
if (mOffset < (int32_t)mBuf.Length()) {
165
*aLength = mBuf.Length() - mOffset;
166
return NS_OK;
167
}
168
169
// Returning one byte is not ideal, but good enough
170
*aLength = (mPos < mArray.Count()) ? 1 : 0;
171
return NS_OK;
172
}
173
174
NS_IMETHODIMP
175
nsDirectoryIndexStream::Read(char* aBuf, uint32_t aCount,
176
uint32_t* aReadCount) {
177
if (mStatus == NS_BASE_STREAM_CLOSED) {
178
*aReadCount = 0;
179
return NS_OK;
180
}
181
if (NS_FAILED(mStatus)) return mStatus;
182
183
uint32_t nread = 0;
184
185
// If anything is enqueued (or left-over) in mBuf, then feed it to
186
// the reader first.
187
while (mOffset < (int32_t)mBuf.Length() && aCount != 0) {
188
*(aBuf++) = char(mBuf.CharAt(mOffset++));
189
--aCount;
190
++nread;
191
}
192
193
// Room left?
194
if (aCount > 0) {
195
mOffset = 0;
196
mBuf.Truncate();
197
198
// Okay, now we'll suck stuff off of our iterator into the mBuf...
199
while (uint32_t(mBuf.Length()) < aCount) {
200
bool more = mPos < mArray.Count();
201
if (!more) break;
202
203
// don't addref, for speed - an addref happened when it
204
// was placed in the array, so it's not going to go stale
205
nsIFile* current = mArray.ObjectAt(mPos);
206
++mPos;
207
208
if (MOZ_LOG_TEST(gLog, LogLevel::Debug)) {
209
MOZ_LOG(gLog, LogLevel::Debug,
210
("nsDirectoryIndexStream[%p]: iterated %s", this,
211
current->HumanReadablePath().get()));
212
}
213
214
// rjc: don't return hidden files/directories!
215
// bbaetz: why not?
216
nsresult rv;
217
#ifndef XP_UNIX
218
bool hidden = false;
219
current->IsHidden(&hidden);
220
if (hidden) {
221
MOZ_LOG(gLog, LogLevel::Debug,
222
("nsDirectoryIndexStream[%p]: skipping hidden file/directory",
223
this));
224
continue;
225
}
226
#endif
227
228
int64_t fileSize = 0;
229
current->GetFileSize(&fileSize);
230
231
PRTime fileInfoModifyTime = 0;
232
current->GetLastModifiedTime(&fileInfoModifyTime);
233
fileInfoModifyTime *= PR_USEC_PER_MSEC;
234
235
mBuf.AppendLiteral("201: ");
236
237
// The "filename" field
238
if (!NS_IsNativeUTF8()) {
239
nsAutoString leafname;
240
rv = current->GetLeafName(leafname);
241
if (NS_FAILED(rv)) return rv;
242
243
nsAutoCString escaped;
244
if (!leafname.IsEmpty() &&
245
NS_Escape(NS_ConvertUTF16toUTF8(leafname), escaped, url_Path)) {
246
mBuf.Append(escaped);
247
mBuf.Append(' ');
248
}
249
} else {
250
nsAutoCString leafname;
251
rv = current->GetNativeLeafName(leafname);
252
if (NS_FAILED(rv)) return rv;
253
254
nsAutoCString escaped;
255
if (!leafname.IsEmpty() && NS_Escape(leafname, escaped, url_Path)) {
256
mBuf.Append(escaped);
257
mBuf.Append(' ');
258
}
259
}
260
261
// The "content-length" field
262
mBuf.AppendInt(fileSize, 10);
263
mBuf.Append(' ');
264
265
// The "last-modified" field
266
PRExplodedTime tm;
267
PR_ExplodeTime(fileInfoModifyTime, PR_GMTParameters, &tm);
268
{
269
char buf[64];
270
PR_FormatTimeUSEnglish(
271
buf, sizeof(buf), "%a,%%20%d%%20%b%%20%Y%%20%H:%M:%S%%20GMT ", &tm);
272
mBuf.Append(buf);
273
}
274
275
// The "file-type" field
276
bool isFile = true;
277
current->IsFile(&isFile);
278
if (isFile) {
279
mBuf.AppendLiteral("FILE ");
280
} else {
281
bool isDir;
282
rv = current->IsDirectory(&isDir);
283
if (NS_FAILED(rv)) return rv;
284
if (isDir) {
285
mBuf.AppendLiteral("DIRECTORY ");
286
} else {
287
bool isLink;
288
rv = current->IsSymlink(&isLink);
289
if (NS_FAILED(rv)) return rv;
290
if (isLink) {
291
mBuf.AppendLiteral("SYMBOLIC-LINK ");
292
}
293
}
294
}
295
296
mBuf.Append('\n');
297
}
298
299
// ...and once we've either run out of directory entries, or
300
// filled up the buffer, then we'll push it to the reader.
301
while (mOffset < (int32_t)mBuf.Length() && aCount != 0) {
302
*(aBuf++) = char(mBuf.CharAt(mOffset++));
303
--aCount;
304
++nread;
305
}
306
}
307
308
*aReadCount = nread;
309
return NS_OK;
310
}
311
312
NS_IMETHODIMP
313
nsDirectoryIndexStream::ReadSegments(nsWriteSegmentFun writer, void* closure,
314
uint32_t count, uint32_t* _retval) {
315
return NS_ERROR_NOT_IMPLEMENTED;
316
}
317
318
NS_IMETHODIMP
319
nsDirectoryIndexStream::IsNonBlocking(bool* aNonBlocking) {
320
*aNonBlocking = false;
321
return NS_OK;
322
}