Bug 478086 - enumerating drives/disks on Win32 using nsIFile.directoryEntries returns a single string with embedded NULLs in it; r=benjamin

This commit is contained in:
Nickolay_Ponomarev
2009-02-26 18:34:07 +01:00
parent e8d57925e8
commit b85e312102
2 changed files with 44 additions and 13 deletions

View File

@@ -108,13 +108,14 @@ public:
NS_DECL_NSISIMPLEENUMERATOR
nsresult Init();
private:
/* mDrives and mLetter share data
/* mDrives stores the null-separated drive names.
* Init sets them.
* HasMoreElements reads mLetter.
* GetNext advances mLetter.
* HasMoreElements checks mStartOfCurrentDrive.
* GetNext advances mStartOfCurrentDrive.
*/
nsString mDrives;
const PRUnichar *mLetter;
nsAString::const_iterator mStartOfCurrentDrive;
nsAString::const_iterator mEndOfDrivesString;
};
//----------------------------------------------------------------------------
@@ -3033,7 +3034,6 @@ nsLocalFile::GlobalShutdown()
NS_IMPL_ISUPPORTS1(nsDriveEnumerator, nsISimpleEnumerator)
nsDriveEnumerator::nsDriveEnumerator()
: mLetter(0)
{
}
@@ -3046,32 +3046,39 @@ nsresult nsDriveEnumerator::Init()
/* If the length passed to GetLogicalDriveStrings is smaller
* than the length of the string it would return, it returns
* the length required for the string. */
DWORD length = GetLogicalDriveStrings(0, 0);
DWORD length = GetLogicalDriveStringsW(0, 0);
/* The string is null terminated */
if (!EnsureStringLength(mDrives, length+1))
return NS_ERROR_OUT_OF_MEMORY;
if (!GetLogicalDriveStringsW(length, mDrives.BeginWriting()))
return NS_ERROR_FAILURE;
mLetter = mDrives.get();
mDrives.BeginReading(mStartOfCurrentDrive);
mDrives.EndReading(mEndOfDrivesString);
return NS_OK;
}
NS_IMETHODIMP nsDriveEnumerator::HasMoreElements(PRBool *aHasMore)
{
*aHasMore = *mLetter != '\0';
*aHasMore = *mStartOfCurrentDrive != L'\0';
return NS_OK;
}
NS_IMETHODIMP nsDriveEnumerator::GetNext(nsISupports **aNext)
{
/* GetLogicalDrives stored in mLetter is a concatenation
* of null terminated strings, followed by a null terminator. */
if (!*mLetter) {
/* GetLogicalDrives stored in mDrives is a concatenation
* of null terminated strings, followed by a null terminator.
* mStartOfCurrentDrive is an iterator pointing at the first
* character of the current drive. */
if (*mStartOfCurrentDrive == L'\0') {
*aNext = nsnull;
return NS_OK;
}
nsString drive(mDrives);
mLetter += drive.Length() + 1;
nsAString::const_iterator driveEnd = mStartOfCurrentDrive;
FindCharInReadable(L'\0', driveEnd, mEndOfDrivesString);
nsString drive(Substring(mStartOfCurrentDrive, driveEnd));
mStartOfCurrentDrive = ++driveEnd;
nsILocalFile *file;
nsresult rv = NS_NewLocalFile(drive, PR_FALSE, &file);