1999-09-13 20:20:21 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
*
|
1999-11-06 03:40:37 +00:00
|
|
|
* The contents of this file are subject to the Netscape Public
|
|
|
|
* License Version 1.1 (the "License"); you may not use this file
|
|
|
|
* except in compliance with the License. You may obtain a copy of
|
|
|
|
* the License at http://www.mozilla.org/NPL/
|
1999-09-13 20:20:21 +00:00
|
|
|
*
|
1999-11-06 03:40:37 +00:00
|
|
|
* Software distributed under the License is distributed on an "AS
|
|
|
|
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
|
|
|
* implied. See the License for the specific language governing
|
|
|
|
* rights and limitations under the License.
|
1999-09-13 20:20:21 +00:00
|
|
|
*
|
1999-11-06 03:40:37 +00:00
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Netscape
|
1999-09-13 20:20:21 +00:00
|
|
|
* Communications Corporation. Portions created by Netscape are
|
1999-11-06 03:40:37 +00:00
|
|
|
* Copyright (C) 1998 Netscape Communications Corporation. All
|
|
|
|
* Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
1999-09-13 20:20:21 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
The converts a filesystem directory into an "HTTP index" stream.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "nsEscape.h"
|
|
|
|
#include "nsDirectoryIndexStream.h"
|
|
|
|
#include "prio.h"
|
|
|
|
|
|
|
|
|
|
|
|
nsDirectoryIndexStream::nsDirectoryIndexStream()
|
2000-01-24 21:28:28 +00:00
|
|
|
: mOffset(0)
|
1999-09-13 20:20:21 +00:00
|
|
|
{
|
|
|
|
NS_INIT_REFCNT();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2000-01-24 21:28:28 +00:00
|
|
|
nsDirectoryIndexStream::Init(nsIFile* aDir)
|
1999-09-13 20:20:21 +00:00
|
|
|
{
|
2000-01-24 21:28:28 +00:00
|
|
|
nsresult rv;
|
|
|
|
PRBool isDir;
|
|
|
|
rv = aDir->IsDirectory(&isDir);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
NS_PRECONDITION(isDir, "not a directory");
|
|
|
|
if (!isDir)
|
1999-09-13 20:20:21 +00:00
|
|
|
return NS_ERROR_ILLEGAL_VALUE;
|
|
|
|
|
|
|
|
mDir = aDir;
|
|
|
|
|
|
|
|
// Sigh. We have to allocate on the heap because there are no
|
|
|
|
// assignment operators defined.
|
2000-01-24 21:28:28 +00:00
|
|
|
rv = mDir->GetDirectoryEntries(getter_AddRefs(mIter));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
1999-09-13 20:20:21 +00:00
|
|
|
|
2000-01-24 21:28:28 +00:00
|
|
|
mBuf = "200: filename content-length last-modified file-type\n";
|
1999-09-13 20:20:21 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsDirectoryIndexStream::~nsDirectoryIndexStream()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2000-01-24 21:28:28 +00:00
|
|
|
nsDirectoryIndexStream::Create(nsIFile* aDir, nsIInputStream** aResult)
|
1999-09-13 20:20:21 +00:00
|
|
|
{
|
|
|
|
nsDirectoryIndexStream* result = new nsDirectoryIndexStream();
|
|
|
|
if (! result)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
rv = result->Init(aDir);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
delete result;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aResult = result;
|
|
|
|
NS_ADDREF(*aResult);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2000-01-24 21:28:28 +00:00
|
|
|
NS_IMPL_ISUPPORTS2(nsDirectoryIndexStream,
|
|
|
|
nsIInputStream,
|
|
|
|
nsIBaseStream)
|
1999-09-13 20:20:21 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDirectoryIndexStream::Close()
|
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDirectoryIndexStream::Available(PRUint32* aLength)
|
|
|
|
{
|
|
|
|
// Lie, and tell the caller that the stream is endless (until we
|
|
|
|
// actually don't have anything left).
|
2000-01-24 21:28:28 +00:00
|
|
|
PRBool more;
|
|
|
|
nsresult rv = mIter->HasMoreElements(&more);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
if (more) {
|
1999-09-13 20:20:21 +00:00
|
|
|
*aLength = PRUint32(-1);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
*aLength = 0;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDirectoryIndexStream::Read(char* aBuf, PRUint32 aCount, PRUint32* aReadCount)
|
|
|
|
{
|
|
|
|
PRUint32 nread = 0;
|
|
|
|
|
|
|
|
// If anything is enqueued (or left-over) in mBuf, then feed it to
|
|
|
|
// the reader first.
|
|
|
|
while (mOffset < 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...
|
2000-01-24 21:28:28 +00:00
|
|
|
while (PRUint32(mBuf.Length()) < aCount) {
|
|
|
|
PRBool more;
|
|
|
|
nsresult rv = mIter->HasMoreElements(&more);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
if (!more) break;
|
|
|
|
|
|
|
|
nsCOMPtr<nsISupports> cur;
|
|
|
|
rv = mIter->GetNext(getter_AddRefs(cur));
|
|
|
|
nsCOMPtr<nsIFile> current = do_QueryInterface(cur, &rv);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
// rjc: don't return hidden files/directories!
|
|
|
|
PRBool hidden;
|
|
|
|
rv = current->IsHidden(&hidden);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
if (hidden) continue;
|
|
|
|
|
|
|
|
char* path;
|
|
|
|
rv = current->GetPath(&path);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
1999-09-13 20:20:21 +00:00
|
|
|
PRFileInfo fileinfo;
|
2000-01-24 21:28:28 +00:00
|
|
|
PRStatus status = PR_GetFileInfo(path, &fileinfo);
|
|
|
|
nsCRT::free(path);
|
1999-09-13 20:20:21 +00:00
|
|
|
if (status != PR_SUCCESS)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
mBuf += "201: ";
|
|
|
|
|
|
|
|
// The "filename" field
|
|
|
|
{
|
2000-01-24 21:28:28 +00:00
|
|
|
char* leafname;
|
|
|
|
rv = current->GetLeafName(&leafname);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
1999-09-13 20:20:21 +00:00
|
|
|
if (leafname) {
|
|
|
|
char* escaped = nsEscape(leafname, url_Path);
|
|
|
|
if (escaped) {
|
|
|
|
mBuf += escaped;
|
|
|
|
mBuf.Append(' ');
|
|
|
|
nsCRT::free(escaped);
|
|
|
|
}
|
|
|
|
nsCRT::free(leafname);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The "content-length" field
|
|
|
|
mBuf.Append(fileinfo.size, 10);
|
|
|
|
mBuf.Append(' ');
|
|
|
|
|
|
|
|
// The "last-modified" field
|
|
|
|
PRExplodedTime tm;
|
|
|
|
PR_ExplodeTime(fileinfo.modifyTime, 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
|
2000-01-24 21:28:28 +00:00
|
|
|
PRBool isFile;
|
|
|
|
rv = current->IsFile(&isFile);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
if (isFile) {
|
1999-09-13 20:20:21 +00:00
|
|
|
mBuf += "FILE ";
|
|
|
|
}
|
2000-01-24 21:28:28 +00:00
|
|
|
else {
|
|
|
|
PRBool isDir;
|
|
|
|
rv = current->IsDirectory(&isDir);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
if (isDir) {
|
|
|
|
mBuf += "DIRECTORY ";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PRBool isLink;
|
|
|
|
rv = current->IsSymlink(&isLink);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
if (isLink) {
|
|
|
|
mBuf += "SYMBOLIC-LINK ";
|
|
|
|
}
|
|
|
|
}
|
1999-09-13 20:20:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mBuf.Append('\n');
|
|
|
|
}
|
|
|
|
|
|
|
|
// ...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 < mBuf.Length() && aCount != 0) {
|
|
|
|
*(aBuf++) = char(mBuf.CharAt(mOffset++));
|
|
|
|
--aCount;
|
|
|
|
++nread;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*aReadCount = nread;
|
|
|
|
return NS_OK;
|
|
|
|
}
|