2001-09-29 00:14:13 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2005-08-21 18:13:52 +04:00
|
|
|
/* vim:set sw=4 sts=4 et cin: */
|
2012-05-21 15:12:37 +04: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/. */
|
1999-09-14 00:20:21 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
|
2000-02-08 04:16:09 +03:00
|
|
|
The converts a filesystem directory into an "HTTP index" stream per
|
|
|
|
Lou Montulli's original spec:
|
|
|
|
|
2004-05-11 03:21:35 +04:00
|
|
|
http://www.mozilla.org/projects/netlib/dirindexformat.html
|
1999-09-14 00:20:21 +04:00
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "nsEscape.h"
|
|
|
|
#include "nsDirectoryIndexStream.h"
|
2015-05-19 21:15:34 +03:00
|
|
|
#include "mozilla/Logging.h"
|
2013-09-23 21:29:27 +04:00
|
|
|
#include "prtime.h"
|
2001-10-03 04:43:30 +04:00
|
|
|
#include "nsISimpleEnumerator.h"
|
2013-09-22 07:04:57 +04:00
|
|
|
#ifdef THREADSAFE_I18N
|
|
|
|
#include "nsCollationCID.h"
|
2001-10-03 04:43:30 +04:00
|
|
|
#include "nsICollation.h"
|
2013-09-22 07:04:57 +04:00
|
|
|
#endif
|
|
|
|
#include "nsIFile.h"
|
2002-09-13 23:32:45 +04:00
|
|
|
#include "nsURLHelper.h"
|
2006-05-02 10:24:11 +04:00
|
|
|
#include "nsNativeCharsetUtils.h"
|
2001-10-03 04:43:30 +04:00
|
|
|
|
|
|
|
// NOTE: This runs on the _file transport_ thread.
|
|
|
|
// The problem is that now that we're actually doing something with the data,
|
|
|
|
// we want to do stuff like i18n sorting. However, none of the collation stuff
|
2006-05-02 10:24:11 +04:00
|
|
|
// is threadsafe.
|
2001-10-03 04:43:30 +04:00
|
|
|
// So THIS CODE IS ASCII ONLY!!!!!!!! This is no worse than the current
|
|
|
|
// behaviour, though. See bug 99382.
|
|
|
|
// When this is fixed, #define THREADSAFE_I18N to get this code working
|
|
|
|
|
|
|
|
//#define THREADSAFE_I18N
|
|
|
|
|
2016-01-09 04:20:50 +03:00
|
|
|
using namespace mozilla;
|
|
|
|
static LazyLogModule gLog("nsDirectoryIndexStream");
|
2015-11-03 07:35:29 +03:00
|
|
|
|
1999-09-14 00:20:21 +04:00
|
|
|
nsDirectoryIndexStream::nsDirectoryIndexStream()
|
2018-11-30 13:46:48 +03:00
|
|
|
: mOffset(0), mStatus(NS_OK), mPos(0) {
|
|
|
|
MOZ_LOG(gLog, LogLevel::Debug, ("nsDirectoryIndexStream[%p]: created", this));
|
1999-09-14 00:20:21 +04:00
|
|
|
}
|
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
static int compare(nsIFile* aElement1, nsIFile* aElement2, void* aData) {
|
|
|
|
if (!NS_IsNativeUTF8()) {
|
|
|
|
// don't check for errors, because we can't report them anyway
|
|
|
|
nsAutoString name1, name2;
|
|
|
|
aElement1->GetLeafName(name1);
|
|
|
|
aElement2->GetLeafName(name2);
|
|
|
|
|
|
|
|
// Note - we should do the collation to do sorting. Why don't we?
|
|
|
|
// Because that is _slow_. Using TestProtocols to list file:///dev/
|
|
|
|
// goes from 3 seconds to 22. (This may be why nsXULSortService is
|
|
|
|
// so slow as well).
|
|
|
|
// Does this have bad effects? Probably, but since nsXULTree appears
|
|
|
|
// to use the raw RDF literal value as the sort key (which ammounts to an
|
|
|
|
// strcmp), it won't be any worse, I think.
|
|
|
|
// This could be made faster, by creating the keys once,
|
|
|
|
// but CompareString could still be smarter - see bug 99383 - bbaetz
|
|
|
|
// NB - 99393 has been WONTFIXed. So if the I18N code is ever made
|
|
|
|
// threadsafe so that this matters, we'd have to pass through a
|
|
|
|
// struct { nsIFile*, uint8_t* } with the pre-calculated key.
|
|
|
|
return Compare(name1, name2);
|
|
|
|
}
|
2001-10-03 04:43:30 +04:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
nsAutoCString name1, name2;
|
|
|
|
aElement1->GetNativeLeafName(name1);
|
|
|
|
aElement2->GetNativeLeafName(name2);
|
2006-05-02 10:24:11 +04:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
return Compare(name1, name2);
|
2001-10-03 04:43:30 +04:00
|
|
|
}
|
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
nsresult nsDirectoryIndexStream::Init(nsIFile* aDir) {
|
|
|
|
nsresult rv;
|
|
|
|
bool isDir;
|
|
|
|
rv = aDir->IsDirectory(&isDir);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
MOZ_ASSERT(isDir, "not a directory");
|
|
|
|
if (!isDir) return NS_ERROR_ILLEGAL_VALUE;
|
2000-02-08 04:16:09 +03:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
if (MOZ_LOG_TEST(gLog, LogLevel::Debug)) {
|
|
|
|
MOZ_LOG(gLog, LogLevel::Debug,
|
|
|
|
("nsDirectoryIndexStream[%p]: initialized on %s", this,
|
|
|
|
aDir->HumanReadablePath().get()));
|
|
|
|
}
|
2001-10-03 04:43:30 +04:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
// Sigh. We have to allocate on the heap because there are no
|
|
|
|
// assignment operators defined.
|
|
|
|
nsCOMPtr<nsIDirectoryEnumerator> iter;
|
|
|
|
rv = aDir->GetDirectoryEntries(getter_AddRefs(iter));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
2001-10-03 04:43:30 +04:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
// Now lets sort, because clients expect it that way
|
|
|
|
// XXX - should we do so here, or when the first item is requested?
|
|
|
|
// XXX - use insertion sort instead?
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> file;
|
|
|
|
while (NS_SUCCEEDED(iter->GetNextFile(getter_AddRefs(file))) && file) {
|
|
|
|
mArray.AppendObject(file); // addrefs
|
|
|
|
}
|
2001-10-03 04:43:30 +04:00
|
|
|
|
|
|
|
#ifdef THREADSAFE_I18N
|
2018-11-30 13:46:48 +03:00
|
|
|
nsCOMPtr<nsICollationFactory> cf =
|
|
|
|
do_CreateInstance(NS_COLLATIONFACTORY_CONTRACTID, &rv);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
2001-10-03 04:43:30 +04:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
nsCOMPtr<nsICollation> coll;
|
|
|
|
rv = cf->CreateCollation(getter_AddRefs(coll));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
1999-09-14 00:20:21 +04:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
mArray.Sort(compare, coll);
|
2001-10-03 04:43:30 +04:00
|
|
|
#else
|
2018-11-30 13:46:48 +03:00
|
|
|
mArray.Sort(compare, nullptr);
|
2001-10-03 04:43:30 +04:00
|
|
|
#endif
|
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
mBuf.AppendLiteral("300: ");
|
|
|
|
nsAutoCString url;
|
|
|
|
rv = net_GetURLSpecFromFile(aDir, url);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
mBuf.Append(url);
|
|
|
|
mBuf.Append('\n');
|
2001-10-03 04:43:30 +04:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
mBuf.AppendLiteral("200: filename content-length last-modified file-type\n");
|
2001-10-03 04:43:30 +04:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
return NS_OK;
|
1999-09-14 00:20:21 +04:00
|
|
|
}
|
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
nsDirectoryIndexStream::~nsDirectoryIndexStream() {
|
|
|
|
MOZ_LOG(gLog, LogLevel::Debug,
|
|
|
|
("nsDirectoryIndexStream[%p]: destroyed", this));
|
1999-09-14 00:20:21 +04:00
|
|
|
}
|
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
nsresult nsDirectoryIndexStream::Create(nsIFile* aDir,
|
|
|
|
nsIInputStream** aResult) {
|
|
|
|
RefPtr<nsDirectoryIndexStream> result = new nsDirectoryIndexStream();
|
|
|
|
if (!result) return NS_ERROR_OUT_OF_MEMORY;
|
1999-09-14 00:20:21 +04:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
nsresult rv = result->Init(aDir);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
1999-09-14 00:20:21 +04:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
result.forget(aResult);
|
|
|
|
return NS_OK;
|
1999-09-14 00:20:21 +04:00
|
|
|
}
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsDirectoryIndexStream, nsIInputStream)
|
1999-09-14 00:20:21 +04:00
|
|
|
|
2001-10-03 04:43:30 +04:00
|
|
|
// The below routines are proxied to the UI thread!
|
1999-09-14 00:20:21 +04:00
|
|
|
NS_IMETHODIMP
|
2018-11-30 13:46:48 +03:00
|
|
|
nsDirectoryIndexStream::Close() {
|
|
|
|
mStatus = NS_BASE_STREAM_CLOSED;
|
|
|
|
return NS_OK;
|
1999-09-14 00:20:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2018-11-30 13:46:48 +03:00
|
|
|
nsDirectoryIndexStream::Available(uint64_t* aLength) {
|
|
|
|
if (NS_FAILED(mStatus)) return mStatus;
|
2005-08-21 18:13:52 +04:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
// If there's data in our buffer, use that
|
|
|
|
if (mOffset < (int32_t)mBuf.Length()) {
|
|
|
|
*aLength = mBuf.Length() - mOffset;
|
2005-08-21 18:13:52 +04:00
|
|
|
return NS_OK;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returning one byte is not ideal, but good enough
|
|
|
|
*aLength = (mPos < mArray.Count()) ? 1 : 0;
|
|
|
|
return NS_OK;
|
1999-09-14 00:20:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2018-11-30 13:46:48 +03:00
|
|
|
nsDirectoryIndexStream::Read(char* aBuf, uint32_t aCount,
|
|
|
|
uint32_t* aReadCount) {
|
|
|
|
if (mStatus == NS_BASE_STREAM_CLOSED) {
|
|
|
|
*aReadCount = 0;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
if (NS_FAILED(mStatus)) return mStatus;
|
|
|
|
|
|
|
|
uint32_t nread = 0;
|
|
|
|
|
|
|
|
// If anything is enqueued (or left-over) in mBuf, then feed it to
|
|
|
|
// the reader first.
|
|
|
|
while (mOffset < (int32_t)mBuf.Length() && aCount != 0) {
|
|
|
|
*(aBuf++) = char(mBuf.CharAt(mOffset++));
|
|
|
|
--aCount;
|
|
|
|
++nread;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Room left?
|
|
|
|
if (aCount > 0) {
|
|
|
|
mOffset = 0;
|
|
|
|
mBuf.Truncate();
|
|
|
|
|
|
|
|
// Okay, now we'll suck stuff off of our iterator into the mBuf...
|
|
|
|
while (uint32_t(mBuf.Length()) < aCount) {
|
|
|
|
bool more = mPos < mArray.Count();
|
|
|
|
if (!more) break;
|
|
|
|
|
|
|
|
// don't addref, for speed - an addref happened when it
|
|
|
|
// was placed in the array, so it's not going to go stale
|
|
|
|
nsIFile* current = mArray.ObjectAt(mPos);
|
|
|
|
++mPos;
|
|
|
|
|
|
|
|
if (MOZ_LOG_TEST(gLog, LogLevel::Debug)) {
|
|
|
|
MOZ_LOG(gLog, LogLevel::Debug,
|
|
|
|
("nsDirectoryIndexStream[%p]: iterated %s", this,
|
|
|
|
current->HumanReadablePath().get()));
|
|
|
|
}
|
1999-09-14 00:20:21 +04:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
// rjc: don't return hidden files/directories!
|
|
|
|
// bbaetz: why not?
|
|
|
|
nsresult rv;
|
2001-10-03 04:43:30 +04:00
|
|
|
#ifndef XP_UNIX
|
2018-11-30 13:46:48 +03:00
|
|
|
bool hidden = false;
|
|
|
|
current->IsHidden(&hidden);
|
|
|
|
if (hidden) {
|
|
|
|
MOZ_LOG(gLog, LogLevel::Debug,
|
|
|
|
("nsDirectoryIndexStream[%p]: skipping hidden file/directory",
|
|
|
|
this));
|
|
|
|
continue;
|
|
|
|
}
|
2006-05-02 10:24:11 +04:00
|
|
|
#endif
|
2001-10-03 04:43:30 +04:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
int64_t fileSize = 0;
|
|
|
|
current->GetFileSize(&fileSize);
|
|
|
|
|
|
|
|
PRTime fileInfoModifyTime = 0;
|
|
|
|
current->GetLastModifiedTime(&fileInfoModifyTime);
|
|
|
|
fileInfoModifyTime *= PR_USEC_PER_MSEC;
|
|
|
|
|
|
|
|
mBuf.AppendLiteral("201: ");
|
1999-09-14 00:20:21 +04:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
// The "filename" field
|
|
|
|
if (!NS_IsNativeUTF8()) {
|
|
|
|
nsAutoString leafname;
|
|
|
|
rv = current->GetLeafName(leafname);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
nsAutoCString escaped;
|
|
|
|
if (!leafname.IsEmpty() &&
|
|
|
|
NS_Escape(NS_ConvertUTF16toUTF8(leafname), escaped, url_Path)) {
|
|
|
|
mBuf.Append(escaped);
|
|
|
|
mBuf.Append(' ');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
nsAutoCString leafname;
|
|
|
|
rv = current->GetNativeLeafName(leafname);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
nsAutoCString escaped;
|
|
|
|
if (!leafname.IsEmpty() && NS_Escape(leafname, escaped, url_Path)) {
|
|
|
|
mBuf.Append(escaped);
|
|
|
|
mBuf.Append(' ');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The "content-length" field
|
|
|
|
mBuf.AppendInt(fileSize, 10);
|
|
|
|
mBuf.Append(' ');
|
|
|
|
|
|
|
|
// The "last-modified" field
|
|
|
|
PRExplodedTime tm;
|
|
|
|
PR_ExplodeTime(fileInfoModifyTime, PR_GMTParameters, &tm);
|
|
|
|
{
|
|
|
|
char buf[64];
|
|
|
|
PR_FormatTimeUSEnglish(
|
|
|
|
buf, sizeof(buf), "%a,%%20%d%%20%b%%20%Y%%20%H:%M:%S%%20GMT ", &tm);
|
|
|
|
mBuf.Append(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
// The "file-type" field
|
|
|
|
bool isFile = true;
|
|
|
|
current->IsFile(&isFile);
|
|
|
|
if (isFile) {
|
|
|
|
mBuf.AppendLiteral("FILE ");
|
|
|
|
} else {
|
|
|
|
bool isDir;
|
|
|
|
rv = current->IsDirectory(&isDir);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
if (isDir) {
|
|
|
|
mBuf.AppendLiteral("DIRECTORY ");
|
|
|
|
} else {
|
|
|
|
bool isLink;
|
|
|
|
rv = current->IsSymlink(&isLink);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
if (isLink) {
|
|
|
|
mBuf.AppendLiteral("SYMBOLIC-LINK ");
|
|
|
|
}
|
1999-09-14 00:20:21 +04:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
mBuf.Append('\n');
|
1999-09-14 00:20:21 +04:00
|
|
|
}
|
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
// ...and once we've either run out of directory entries, or
|
|
|
|
// filled up the buffer, then we'll push it to the reader.
|
|
|
|
while (mOffset < (int32_t)mBuf.Length() && aCount != 0) {
|
|
|
|
*(aBuf++) = char(mBuf.CharAt(mOffset++));
|
|
|
|
--aCount;
|
|
|
|
++nread;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*aReadCount = nread;
|
|
|
|
return NS_OK;
|
1999-09-14 00:20:21 +04:00
|
|
|
}
|
2000-08-22 11:03:33 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2018-11-30 13:46:48 +03:00
|
|
|
nsDirectoryIndexStream::ReadSegments(nsWriteSegmentFun writer, void* closure,
|
|
|
|
uint32_t count, uint32_t* _retval) {
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
2000-08-22 11:03:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2018-11-30 13:46:48 +03:00
|
|
|
nsDirectoryIndexStream::IsNonBlocking(bool* aNonBlocking) {
|
|
|
|
*aNonBlocking = false;
|
|
|
|
return NS_OK;
|
2000-08-22 11:03:33 +04:00
|
|
|
}
|