2000-03-28 09:01:05 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
1999-12-04 04:13:44 +03: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/
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* The Original Code is Mozilla Communicator client code,
|
|
|
|
* released March 31, 1998.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Netscape Communications
|
|
|
|
* Corporation. Portions created by Netscape are
|
|
|
|
* Copyright (C) 1998-1999 Netscape Communications Corporation. All
|
|
|
|
* Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Doug Turner <dougt@netscape.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "nsCOMPtr.h"
|
2000-06-03 13:46:12 +04:00
|
|
|
#include "nsMemory.h"
|
1999-12-04 04:13:44 +03:00
|
|
|
|
2003-06-19 03:19:11 +04:00
|
|
|
#include "nsLocalFile.h"
|
2002-10-06 04:29:46 +04:00
|
|
|
#include "nsNativeCharsetUtils.h"
|
1999-12-04 04:13:44 +03:00
|
|
|
|
|
|
|
#include "nsISimpleEnumerator.h"
|
|
|
|
#include "nsIComponentManager.h"
|
|
|
|
#include "prtypes.h"
|
|
|
|
#include "prio.h"
|
|
|
|
|
2001-06-22 02:02:47 +04:00
|
|
|
#include "nsXPIDLString.h"
|
2001-11-01 12:09:57 +03:00
|
|
|
#include "nsReadableUtils.h"
|
2001-06-22 02:02:47 +04:00
|
|
|
|
1999-12-04 04:13:44 +03:00
|
|
|
#include <direct.h>
|
|
|
|
#include <windows.h>
|
|
|
|
|
|
|
|
#include "shellapi.h"
|
|
|
|
#include "shlguid.h"
|
|
|
|
|
2000-02-22 00:51:47 +03:00
|
|
|
#include <io.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2000-04-26 07:57:34 +04:00
|
|
|
#include <mbstring.h>
|
1999-12-04 04:13:44 +03:00
|
|
|
|
2001-01-30 08:02:48 +03:00
|
|
|
#include "nsXPIDLString.h"
|
2000-02-17 19:08:53 +03:00
|
|
|
#include "prproces.h"
|
2001-08-17 06:03:34 +04:00
|
|
|
#include "nsITimelineService.h"
|
2000-02-17 19:08:53 +03:00
|
|
|
|
2002-01-08 05:20:13 +03:00
|
|
|
#include "nsAutoLock.h"
|
|
|
|
|
2003-03-12 11:35:57 +03:00
|
|
|
// _mbsstr isn't declared in w32api headers but it's there in the libs
|
|
|
|
#ifdef __MINGW32__
|
|
|
|
extern "C" {
|
|
|
|
unsigned char *_mbsstr( const unsigned char *str,
|
|
|
|
const unsigned char *substr );
|
|
|
|
};
|
|
|
|
#endif
|
2002-01-08 05:20:13 +03:00
|
|
|
|
2002-04-27 09:33:09 +04:00
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
// short cut resolver
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
|
2002-01-08 05:20:13 +03:00
|
|
|
class ShortcutResolver
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ShortcutResolver();
|
|
|
|
virtual ~ShortcutResolver();
|
|
|
|
|
|
|
|
nsresult Init();
|
2003-03-07 09:07:56 +03:00
|
|
|
nsresult Resolve(const WCHAR* in, char* out);
|
2002-01-08 05:20:13 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
PRLock* mLock;
|
|
|
|
IPersistFile* mPersistFile;
|
|
|
|
IShellLink* mShellLink;
|
|
|
|
};
|
|
|
|
|
|
|
|
ShortcutResolver::ShortcutResolver()
|
|
|
|
{
|
|
|
|
mLock = nsnull;
|
|
|
|
mPersistFile = nsnull;
|
|
|
|
mShellLink = nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
ShortcutResolver::~ShortcutResolver()
|
|
|
|
{
|
|
|
|
if (mLock)
|
|
|
|
PR_DestroyLock(mLock);
|
|
|
|
|
|
|
|
// Release the pointer to the IPersistFile interface.
|
|
|
|
if (mPersistFile)
|
|
|
|
mPersistFile->Release();
|
|
|
|
|
|
|
|
// Release the pointer to the IShellLink interface.
|
|
|
|
if(mShellLink)
|
|
|
|
mShellLink->Release();
|
|
|
|
|
|
|
|
CoUninitialize();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
ShortcutResolver::Init()
|
|
|
|
{
|
|
|
|
CoInitialize(NULL); // FIX: we should probably move somewhere higher up during startup
|
|
|
|
|
|
|
|
mLock = PR_NewLock();
|
|
|
|
if (!mLock)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
HRESULT hres = CoCreateInstance(CLSID_ShellLink,
|
|
|
|
NULL,
|
|
|
|
CLSCTX_INPROC_SERVER,
|
|
|
|
IID_IShellLink,
|
|
|
|
(void**)&mShellLink);
|
|
|
|
if (SUCCEEDED(hres))
|
|
|
|
{
|
|
|
|
// Get a pointer to the IPersistFile interface.
|
|
|
|
hres = mShellLink->QueryInterface(IID_IPersistFile, (void**)&mPersistFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mPersistFile == nsnull || mShellLink == nsnull)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// |out| must be an allocated buffer of size MAX_PATH
|
|
|
|
nsresult
|
2003-03-07 09:07:56 +03:00
|
|
|
ShortcutResolver::Resolve(const WCHAR* in, char* out)
|
2002-01-08 05:20:13 +03:00
|
|
|
{
|
|
|
|
nsAutoLock lock(mLock);
|
|
|
|
|
|
|
|
// see if we can Load the path.
|
|
|
|
HRESULT hres = mPersistFile->Load(in, STGM_READ);
|
|
|
|
|
|
|
|
if (FAILED(hres))
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
// Resolve the link.
|
|
|
|
hres = mShellLink->Resolve(nsnull, SLR_NO_UI );
|
|
|
|
|
|
|
|
if (FAILED(hres))
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
WIN32_FIND_DATA wfd;
|
|
|
|
// Get the path to the link target.
|
|
|
|
hres = mShellLink->GetPath( out, MAX_PATH, &wfd, SLGP_UNCPRIORITY );
|
|
|
|
if (FAILED(hres))
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ShortcutResolver * gResolver = nsnull;
|
|
|
|
|
2002-04-27 09:33:09 +04:00
|
|
|
static nsresult NS_CreateShortcutResolver()
|
2002-01-08 05:20:13 +03:00
|
|
|
{
|
|
|
|
gResolver = new ShortcutResolver();
|
|
|
|
if (!gResolver)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
return gResolver->Init();
|
|
|
|
}
|
|
|
|
|
2002-04-27 09:33:09 +04:00
|
|
|
static void NS_DestroyShortcutResolver()
|
2002-01-08 05:20:13 +03:00
|
|
|
{
|
|
|
|
delete gResolver;
|
|
|
|
gResolver = nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-04-27 09:33:09 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// static helper functions
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
1999-12-04 04:13:44 +03:00
|
|
|
// certainly not all the error that can be
|
|
|
|
// encountered, but many of them common ones
|
|
|
|
static nsresult ConvertWinError(DWORD winErr)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
switch (winErr)
|
|
|
|
{
|
|
|
|
case ERROR_FILE_NOT_FOUND:
|
|
|
|
case ERROR_PATH_NOT_FOUND:
|
|
|
|
case ERROR_INVALID_DRIVE:
|
|
|
|
rv = NS_ERROR_FILE_NOT_FOUND;
|
|
|
|
break;
|
|
|
|
case ERROR_ACCESS_DENIED:
|
|
|
|
case ERROR_NOT_SAME_DEVICE:
|
|
|
|
rv = NS_ERROR_FILE_ACCESS_DENIED;
|
|
|
|
break;
|
|
|
|
case ERROR_NOT_ENOUGH_MEMORY:
|
|
|
|
case ERROR_INVALID_BLOCK:
|
|
|
|
case ERROR_INVALID_HANDLE:
|
|
|
|
case ERROR_ARENA_TRASHED:
|
|
|
|
rv = NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
break;
|
|
|
|
case ERROR_CURRENT_DIRECTORY:
|
|
|
|
rv = NS_ERROR_FILE_DIR_NOT_EMPTY;
|
|
|
|
break;
|
|
|
|
case ERROR_WRITE_PROTECT:
|
|
|
|
rv = NS_ERROR_FILE_READ_ONLY;
|
|
|
|
break;
|
|
|
|
case ERROR_HANDLE_DISK_FULL:
|
|
|
|
rv = NS_ERROR_FILE_TOO_BIG;
|
|
|
|
break;
|
2000-06-24 05:50:53 +04:00
|
|
|
case ERROR_FILE_EXISTS:
|
|
|
|
case ERROR_ALREADY_EXISTS:
|
|
|
|
case ERROR_CANNOT_MAKE:
|
|
|
|
rv = NS_ERROR_FILE_ALREADY_EXISTS;
|
|
|
|
break;
|
1999-12-04 04:13:44 +03:00
|
|
|
case 0:
|
|
|
|
rv = NS_OK;
|
|
|
|
default:
|
|
|
|
rv = NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
myLL_II2L(PRInt32 hi, PRInt32 lo, PRInt64 *result)
|
|
|
|
{
|
|
|
|
PRInt64 a64, b64; // probably could have been done with
|
|
|
|
// only one PRInt64, but these are macros,
|
|
|
|
// and I am a wimp.
|
|
|
|
|
|
|
|
// put hi in the low bits of a64.
|
|
|
|
LL_I2L(a64, hi);
|
|
|
|
// now shift it to the upperbit and place it the result in result
|
|
|
|
LL_SHL(b64, a64, 32);
|
|
|
|
// now put the low bits on by adding them to the result.
|
|
|
|
LL_ADD(*result, b64, lo);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-12-08 02:31:25 +03:00
|
|
|
static void
|
|
|
|
myLL_L2II(PRInt64 result, PRInt32 *hi, PRInt32 *lo )
|
|
|
|
{
|
|
|
|
PRInt64 a64, b64; // probably could have been done with
|
|
|
|
// only one PRInt64, but these are macros,
|
|
|
|
// and I am a wimp.
|
|
|
|
|
|
|
|
// shift the hi word to the low word, then push it into a long.
|
|
|
|
LL_SHR(a64, result, 32);
|
|
|
|
LL_L2I(*hi, a64);
|
|
|
|
|
|
|
|
// shift the low word to the hi word first, then shift it back.
|
|
|
|
LL_SHL(b64, result, 32);
|
|
|
|
LL_SHR(a64, b64, 32);
|
|
|
|
LL_L2I(*lo, a64);
|
|
|
|
}
|
|
|
|
|
2003-08-25 22:28:10 +04:00
|
|
|
static PRBool
|
|
|
|
IsShortcut(const char* workingPath, int filePathLen)
|
2002-11-27 00:37:58 +03:00
|
|
|
{
|
2003-08-25 22:28:10 +04:00
|
|
|
// XXX this is badly broken!!
|
|
|
|
// XXX consider "C:\FOO.LNK"
|
|
|
|
// XXX consider "C:\foo.lnkx\bar.lnk"
|
|
|
|
|
2002-11-27 00:37:58 +03:00
|
|
|
// check to see if it is shortcut, i.e., it has ".lnk" in it
|
|
|
|
unsigned char* dest = _mbsstr((unsigned char*)workingPath,
|
|
|
|
(unsigned char*)".lnk");
|
|
|
|
if (!dest)
|
|
|
|
return PR_FALSE;
|
|
|
|
|
|
|
|
// find index of ".lnk"
|
|
|
|
int result = (int)(dest - (unsigned char*)workingPath);
|
|
|
|
|
|
|
|
// if ".lnk" is not at the leaf of this path, we need to make sure the
|
|
|
|
// next char after ".lnk" is a '\\'. e.g. "c:\\foo.lnk\\a.html" is valid,
|
|
|
|
// whereas "c:\\foo.lnkx" is not.
|
|
|
|
if (result + 4 < filePathLen)
|
|
|
|
{
|
|
|
|
if (workingPath[result + 4] != '\\')
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
2002-04-27 09:33:09 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsDirEnumerator
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
1999-12-04 04:13:44 +03:00
|
|
|
class nsDirEnumerator : public nsISimpleEnumerator
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
nsDirEnumerator() : mDir(nsnull)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult Init(nsILocalFile* parent)
|
|
|
|
{
|
2002-04-27 09:33:09 +04:00
|
|
|
nsCAutoString filepath;
|
|
|
|
parent->GetNativeTarget(filepath);
|
1999-12-04 04:13:44 +03:00
|
|
|
|
2002-04-27 09:33:09 +04:00
|
|
|
if (filepath.IsEmpty())
|
1999-12-04 04:13:44 +03:00
|
|
|
{
|
2002-04-27 09:33:09 +04:00
|
|
|
parent->GetNativePath(filepath);
|
1999-12-04 04:13:44 +03:00
|
|
|
}
|
|
|
|
|
2002-04-27 09:33:09 +04:00
|
|
|
if (filepath.IsEmpty())
|
1999-12-04 04:13:44 +03:00
|
|
|
{
|
2002-04-27 09:33:09 +04:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
1999-12-04 04:13:44 +03:00
|
|
|
}
|
|
|
|
|
2002-04-27 09:33:09 +04:00
|
|
|
mDir = PR_OpenDir(filepath.get());
|
1999-12-04 04:13:44 +03:00
|
|
|
if (mDir == nsnull) // not a directory?
|
|
|
|
return NS_ERROR_FAILURE;
|
2000-01-28 09:17:06 +03:00
|
|
|
|
1999-12-04 04:13:44 +03:00
|
|
|
mParent = parent;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD HasMoreElements(PRBool *result)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
if (mNext == nsnull && mDir)
|
|
|
|
{
|
|
|
|
PRDirEntry* entry = PR_ReadDir(mDir, PR_SKIP_BOTH);
|
|
|
|
if (entry == nsnull)
|
|
|
|
{
|
|
|
|
// end of dir entries
|
|
|
|
|
|
|
|
PRStatus status = PR_CloseDir(mDir);
|
|
|
|
if (status != PR_SUCCESS)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
mDir = nsnull;
|
|
|
|
|
|
|
|
*result = PR_FALSE;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-12-22 04:56:45 +03:00
|
|
|
nsCOMPtr<nsIFile> file;
|
2000-02-11 07:50:23 +03:00
|
|
|
rv = mParent->Clone(getter_AddRefs(file));
|
2000-03-29 07:58:50 +04:00
|
|
|
if (NS_FAILED(rv))
|
2000-02-11 07:50:23 +03:00
|
|
|
return rv;
|
|
|
|
|
2002-04-27 09:33:09 +04:00
|
|
|
rv = file->AppendNative(nsDependentCString(entry->name));
|
1999-12-04 04:13:44 +03:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
2000-01-25 00:28:28 +03:00
|
|
|
|
|
|
|
// make sure the thing exists. If it does, try the next one.
|
|
|
|
PRBool exists;
|
2002-04-27 09:33:09 +04:00
|
|
|
rv = file->Exists(&exists);
|
2000-01-25 00:28:28 +03:00
|
|
|
if (NS_FAILED(rv) || !exists)
|
|
|
|
{
|
|
|
|
return HasMoreElements(result);
|
|
|
|
}
|
|
|
|
|
1999-12-22 04:56:45 +03:00
|
|
|
mNext = do_QueryInterface(file);
|
1999-12-04 04:13:44 +03:00
|
|
|
}
|
|
|
|
*result = mNext != nsnull;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD GetNext(nsISupports **result)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
PRBool hasMore;
|
|
|
|
rv = HasMoreElements(&hasMore);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
*result = mNext; // might return nsnull
|
|
|
|
NS_IF_ADDREF(*result);
|
2002-07-18 09:09:10 +04:00
|
|
|
|
2001-10-07 14:58:39 +04:00
|
|
|
mNext = nsnull;
|
1999-12-04 04:13:44 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~nsDirEnumerator()
|
|
|
|
{
|
|
|
|
if (mDir)
|
|
|
|
{
|
|
|
|
PRStatus status = PR_CloseDir(mDir);
|
|
|
|
NS_ASSERTION(status == PR_SUCCESS, "close failed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
PRDir* mDir;
|
|
|
|
nsCOMPtr<nsILocalFile> mParent;
|
|
|
|
nsCOMPtr<nsILocalFile> mNext;
|
|
|
|
};
|
|
|
|
|
2000-11-17 06:41:41 +03:00
|
|
|
NS_IMPL_ISUPPORTS1(nsDirEnumerator, nsISimpleEnumerator);
|
1999-12-04 04:13:44 +03:00
|
|
|
|
|
|
|
|
2002-04-27 09:33:09 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsLocalFile <public>
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
1999-12-04 04:13:44 +03:00
|
|
|
nsLocalFile::nsLocalFile()
|
|
|
|
{
|
2000-01-27 00:55:13 +03:00
|
|
|
mLastResolution = PR_FALSE;
|
2000-07-13 03:31:31 +04:00
|
|
|
mFollowSymlinks = PR_FALSE;
|
1999-12-04 04:13:44 +03:00
|
|
|
MakeDirty();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsLocalFile::~nsLocalFile()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_METHOD
|
2000-01-25 00:28:28 +03:00
|
|
|
nsLocalFile::nsLocalFileConstructor(nsISupports* outer, const nsIID& aIID, void* *aInstancePtr)
|
1999-12-04 04:13:44 +03:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aInstancePtr);
|
2000-01-25 00:28:28 +03:00
|
|
|
NS_ENSURE_NO_AGGREGATION(outer);
|
1999-12-04 04:13:44 +03:00
|
|
|
|
|
|
|
nsLocalFile* inst = new nsLocalFile();
|
|
|
|
if (inst == NULL)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
nsresult rv = inst->QueryInterface(aIID, aInstancePtr);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
{
|
|
|
|
delete inst;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2002-04-27 09:33:09 +04:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsLocalFile::nsISupports
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS2(nsLocalFile, nsILocalFile, nsIFile)
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsLocalFile <private>
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2003-07-19 02:14:16 +04:00
|
|
|
nsLocalFile::nsLocalFile(const nsLocalFile& other)
|
|
|
|
: mDirty(other.mDirty)
|
|
|
|
, mLastResolution(other.mLastResolution)
|
|
|
|
, mFollowSymlinks(other.mFollowSymlinks)
|
|
|
|
, mWorkingPath(other.mWorkingPath)
|
|
|
|
, mResolvedPath(other.mResolvedPath)
|
|
|
|
, mFileInfo64(other.mFileInfo64)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
1999-12-04 04:13:44 +03:00
|
|
|
// ResolvePath
|
|
|
|
// this function will walk the native path of |this| resolving any symbolic
|
|
|
|
// links found. The new resulting path will be placed into mResolvedPath.
|
|
|
|
nsresult
|
|
|
|
nsLocalFile::ResolvePath(const char* workingPath, PRBool resolveTerminal, char** resolvedPath)
|
|
|
|
{
|
2001-04-13 03:56:28 +04:00
|
|
|
|
1999-12-04 04:13:44 +03:00
|
|
|
nsresult rv = NS_OK;
|
2002-11-27 00:37:58 +03:00
|
|
|
PRBool isDir = PR_FALSE;
|
1999-12-04 04:13:44 +03:00
|
|
|
|
2002-11-27 00:37:58 +03:00
|
|
|
// check to see if it is shortcut, i.e., it has ".lnk" in it
|
|
|
|
int filePathLen = strlen(workingPath);
|
|
|
|
PRBool isShortcut = IsShortcut(workingPath, filePathLen);
|
|
|
|
if (!isShortcut)
|
2000-07-13 03:31:31 +04:00
|
|
|
return NS_ERROR_FILE_INVALID_PATH;
|
|
|
|
|
2002-01-08 05:20:13 +03:00
|
|
|
#ifdef DEBUG_dougt
|
|
|
|
printf("localfile - resolving symlink\n");
|
|
|
|
#endif
|
|
|
|
|
1999-12-04 04:13:44 +03:00
|
|
|
// Get the native path for |this|
|
2002-11-27 00:37:58 +03:00
|
|
|
// allocate extra bytes incase we need to append '\\' and '\0' to the
|
|
|
|
// workingPath, if it is just a drive letter and a colon
|
|
|
|
char *filePath = (char *) nsMemory::Alloc(filePathLen+2);
|
1999-12-04 04:13:44 +03:00
|
|
|
|
2002-11-27 00:37:58 +03:00
|
|
|
if (!filePath)
|
1999-12-04 04:13:44 +03:00
|
|
|
return NS_ERROR_NULL_POINTER;
|
2002-11-27 00:37:58 +03:00
|
|
|
|
|
|
|
memcpy(filePath, workingPath, filePathLen + 1);
|
1999-12-04 04:13:44 +03:00
|
|
|
|
|
|
|
// We are going to walk the native file path
|
|
|
|
// and stop at each slash. For each partial
|
|
|
|
// path (the string to the left of the slash)
|
|
|
|
// we will check to see if it is a shortcut.
|
|
|
|
// if it is, we will resolve it and continue
|
|
|
|
// with that resolved path.
|
|
|
|
|
2002-11-27 00:37:58 +03:00
|
|
|
// Get the first slash.
|
|
|
|
unsigned char* slash = _mbschr((unsigned char*) filePath, '\\');
|
|
|
|
|
|
|
|
if (slash == nsnull)
|
1999-12-04 04:13:44 +03:00
|
|
|
{
|
2002-11-27 00:37:58 +03:00
|
|
|
if (nsCRT::IsAsciiAlpha(filePath[0]) && filePath[1] == ':' && filePath[2] == '\0')
|
|
|
|
{
|
|
|
|
// we have a drive letter and a colon (eg 'c:'
|
|
|
|
// this is resolve already
|
|
|
|
filePath[filePathLen] = '\\';
|
|
|
|
filePath[filePathLen+1] = '\0';
|
|
|
|
|
|
|
|
*resolvedPath = filePath;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nsMemory::Free(filePath);
|
|
|
|
return NS_ERROR_FILE_INVALID_PATH;
|
|
|
|
}
|
1999-12-04 04:13:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// We really cant have just a drive letter as
|
|
|
|
// a shortcut, so we will skip the first '\\'
|
2002-11-27 00:37:58 +03:00
|
|
|
slash = _mbschr(++slash, '\\');
|
|
|
|
|
1999-12-04 04:13:44 +03:00
|
|
|
while (slash || resolveTerminal)
|
|
|
|
{
|
|
|
|
// Change the slash into a null so that
|
|
|
|
// we can use the partial path. It is is
|
|
|
|
// null, we know it is the terminal node.
|
|
|
|
if (slash)
|
|
|
|
{
|
|
|
|
*slash = '\0';
|
|
|
|
}
|
2002-11-27 00:37:58 +03:00
|
|
|
else if (resolveTerminal)
|
|
|
|
{
|
|
|
|
// this is our last time in this loop.
|
|
|
|
// set loop condition to false
|
|
|
|
resolveTerminal = PR_FALSE;
|
|
|
|
}
|
1999-12-04 04:13:44 +03:00
|
|
|
else
|
|
|
|
{
|
2002-11-27 00:37:58 +03:00
|
|
|
// something is wrong. we should not have
|
|
|
|
// both slash being null and resolveTerminal
|
|
|
|
// not set!
|
|
|
|
nsMemory::Free(filePath);
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
1999-12-04 04:13:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// check to see the file is a shortcut by the magic .lnk extension.
|
|
|
|
size_t offset = strlen(filePath) - 4;
|
2003-08-25 22:28:10 +04:00
|
|
|
if ((offset > 0) && (strnicmp( (filePath + offset), ".lnk", 4) == 0))
|
1999-12-04 04:13:44 +03:00
|
|
|
{
|
2002-11-27 00:37:58 +03:00
|
|
|
nsAutoString ucsBuf;
|
|
|
|
NS_CopyNativeToUnicode(nsDependentCString(filePath), ucsBuf);
|
|
|
|
char *temp = (char*) nsMemory::Alloc( MAX_PATH );
|
|
|
|
if (temp == nsnull)
|
|
|
|
{
|
|
|
|
nsMemory::Free(filePath);
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
1999-12-08 05:07:38 +03:00
|
|
|
|
2002-11-27 00:37:58 +03:00
|
|
|
if (gResolver)
|
|
|
|
rv = gResolver->Resolve(ucsBuf.get(), temp);
|
|
|
|
else
|
|
|
|
rv = NS_ERROR_FAILURE;
|
2002-01-08 05:20:13 +03:00
|
|
|
|
2002-11-27 00:37:58 +03:00
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
{
|
|
|
|
// found a new path.
|
2002-01-08 05:20:13 +03:00
|
|
|
|
2002-11-27 00:37:58 +03:00
|
|
|
// addend a slash on it since it does not come out of GetPath()
|
|
|
|
// with one only if it is a directory. If it is not a directory
|
|
|
|
// and there is more to append, than we have a problem.
|
2002-01-08 05:20:13 +03:00
|
|
|
|
2002-11-27 00:37:58 +03:00
|
|
|
struct stat st;
|
|
|
|
int statrv = stat(temp, &st);
|
2002-01-08 05:20:13 +03:00
|
|
|
|
2002-11-27 00:37:58 +03:00
|
|
|
if (0 == statrv && (_S_IFDIR & st.st_mode))
|
|
|
|
{
|
|
|
|
strcat(temp, "\\");
|
|
|
|
isDir = PR_TRUE;
|
|
|
|
}
|
2002-01-08 05:20:13 +03:00
|
|
|
|
2002-11-27 00:37:58 +03:00
|
|
|
if (slash)
|
|
|
|
{
|
|
|
|
// save where we left off.
|
|
|
|
char *carot= (temp + strlen(temp) -1 );
|
1999-12-08 05:07:38 +03:00
|
|
|
|
2002-11-27 00:37:58 +03:00
|
|
|
// append all the stuff that we have not done.
|
|
|
|
_mbscat((unsigned char*)temp, ++slash);
|
1999-12-08 05:07:38 +03:00
|
|
|
|
2002-11-27 00:37:58 +03:00
|
|
|
slash = (unsigned char*)carot;
|
|
|
|
}
|
1999-12-08 05:07:38 +03:00
|
|
|
|
2002-11-27 00:37:58 +03:00
|
|
|
nsMemory::Free(filePath);
|
|
|
|
filePath = temp;
|
1999-12-08 05:07:38 +03:00
|
|
|
|
2002-11-27 00:37:58 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// could not resolve shortcut. Return error;
|
|
|
|
nsMemory::Free(filePath);
|
|
|
|
return NS_ERROR_FILE_INVALID_PATH;
|
|
|
|
}
|
1999-12-04 04:13:44 +03:00
|
|
|
}
|
2002-11-27 00:37:58 +03:00
|
|
|
if (slash)
|
1999-12-04 04:13:44 +03:00
|
|
|
{
|
2002-11-27 00:37:58 +03:00
|
|
|
*slash = '\\';
|
|
|
|
++slash;
|
|
|
|
slash = _mbschr(slash, '\\');
|
1999-12-04 04:13:44 +03:00
|
|
|
}
|
2002-01-08 05:20:13 +03:00
|
|
|
}
|
1999-12-08 02:31:25 +03:00
|
|
|
|
2001-12-24 02:23:41 +03:00
|
|
|
// kill any trailing separator
|
1999-12-08 02:31:25 +03:00
|
|
|
char* temp = filePath;
|
|
|
|
int len = strlen(temp) - 1;
|
2002-11-27 00:37:58 +03:00
|
|
|
if((temp[len] == '\\') && (!isDir))
|
1999-12-08 02:31:25 +03:00
|
|
|
temp[len] = '\0';
|
|
|
|
|
1999-12-04 04:13:44 +03:00
|
|
|
*resolvedPath = filePath;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsLocalFile::ResolveAndStat(PRBool resolveTerminal)
|
|
|
|
{
|
2000-01-27 00:55:13 +03:00
|
|
|
if (!mDirty && mLastResolution == resolveTerminal)
|
2000-01-25 00:28:28 +03:00
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2000-01-27 00:55:13 +03:00
|
|
|
mLastResolution = resolveTerminal;
|
2000-07-13 03:31:31 +04:00
|
|
|
mResolvedPath.Assign(mWorkingPath); //until we know better.
|
2000-01-27 00:55:13 +03:00
|
|
|
|
2000-01-25 00:28:28 +03:00
|
|
|
// First we will see if the workingPath exists. If it does, then we
|
|
|
|
// can simply use that as the resolved path. This simplification can
|
|
|
|
// be done on windows cause its symlinks (shortcuts) use the .lnk
|
|
|
|
// file extension.
|
2000-02-22 00:51:47 +03:00
|
|
|
|
2000-03-08 05:53:04 +03:00
|
|
|
char temp[4];
|
2001-03-02 12:26:57 +03:00
|
|
|
const char* workingFilePath = mWorkingPath.get();
|
2000-03-08 05:53:04 +03:00
|
|
|
const char* nsprPath = workingFilePath;
|
|
|
|
|
|
|
|
if (mWorkingPath.Length() == 2 && mWorkingPath.CharAt(1) == ':') {
|
|
|
|
temp[0] = workingFilePath[0];
|
|
|
|
temp[1] = workingFilePath[1];
|
|
|
|
temp[2] = '\\';
|
|
|
|
temp[3] = '\0';
|
|
|
|
nsprPath = temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRStatus status = PR_GetFileInfo64(nsprPath, &mFileInfo64);
|
2000-02-22 00:51:47 +03:00
|
|
|
if ( status == PR_SUCCESS )
|
1999-12-04 04:13:44 +03:00
|
|
|
{
|
2000-07-13 03:31:31 +04:00
|
|
|
if (!resolveTerminal)
|
|
|
|
{
|
|
|
|
mDirty = PR_FALSE;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2000-08-23 18:06:37 +04:00
|
|
|
// check to see that this is shortcut, i.e., the leaf is ".lnk"
|
|
|
|
// if the length < 4, then it's not a link.
|
2000-07-13 03:31:31 +04:00
|
|
|
|
|
|
|
int pathLen = strlen(workingFilePath);
|
|
|
|
const char* leaf = workingFilePath + pathLen - 4;
|
|
|
|
|
2001-06-19 07:17:20 +04:00
|
|
|
// if we found the file and we are not following symlinks, then return success.
|
|
|
|
|
2003-08-25 22:28:10 +04:00
|
|
|
if (!mFollowSymlinks || pathLen < 4 || (stricmp(leaf, ".lnk") != 0))
|
2000-07-13 03:31:31 +04:00
|
|
|
{
|
|
|
|
mDirty = PR_FALSE;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2000-01-25 00:28:28 +03:00
|
|
|
}
|
|
|
|
|
2000-07-13 03:31:31 +04:00
|
|
|
if (!mFollowSymlinks)
|
|
|
|
return NS_ERROR_FILE_NOT_FOUND; // if we are not resolving, we just give up here.
|
|
|
|
|
2000-02-22 00:51:47 +03:00
|
|
|
nsresult result;
|
|
|
|
|
2000-01-27 00:55:13 +03:00
|
|
|
// okay, something is wrong with the working path. We will try to resolve it.
|
|
|
|
|
|
|
|
char *resolvePath;
|
2000-01-25 00:28:28 +03:00
|
|
|
|
2000-01-27 00:55:13 +03:00
|
|
|
result = ResolvePath(workingFilePath, resolveTerminal, &resolvePath);
|
|
|
|
if (NS_FAILED(result))
|
2000-07-13 03:31:31 +04:00
|
|
|
return NS_ERROR_FILE_NOT_FOUND;
|
2000-01-27 00:55:13 +03:00
|
|
|
|
2000-03-12 12:14:14 +03:00
|
|
|
mResolvedPath.Assign(resolvePath);
|
2000-06-03 13:46:12 +04:00
|
|
|
nsMemory::Free(resolvePath);
|
2000-01-25 00:28:28 +03:00
|
|
|
|
2001-11-01 12:09:57 +03:00
|
|
|
status = PR_GetFileInfo64(mResolvedPath.get(), &mFileInfo64);
|
2000-02-22 00:51:47 +03:00
|
|
|
|
|
|
|
if ( status == PR_SUCCESS )
|
2000-01-27 00:55:13 +03:00
|
|
|
mDirty = PR_FALSE;
|
2000-02-22 00:51:47 +03:00
|
|
|
else
|
2000-02-22 09:16:08 +03:00
|
|
|
result = NS_ERROR_FILE_NOT_FOUND;
|
1999-12-04 04:13:44 +03:00
|
|
|
|
2000-01-27 00:55:13 +03:00
|
|
|
return result;
|
1999-12-04 04:13:44 +03:00
|
|
|
}
|
|
|
|
|
2002-04-27 09:33:09 +04:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsLocalFile::nsIFile,nsILocalFile
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
1999-12-04 04:13:44 +03:00
|
|
|
NS_IMETHODIMP
|
1999-12-22 04:56:45 +03:00
|
|
|
nsLocalFile::Clone(nsIFile **file)
|
1999-12-04 04:13:44 +03:00
|
|
|
{
|
2002-02-06 23:40:40 +03:00
|
|
|
// Just copy-construct ourselves
|
2003-07-19 02:14:16 +04:00
|
|
|
*file = new nsLocalFile(*this);
|
|
|
|
if (!*file)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
1999-12-04 04:13:44 +03:00
|
|
|
|
2002-02-06 23:40:40 +03:00
|
|
|
NS_ADDREF(*file);
|
2003-07-19 02:14:16 +04:00
|
|
|
|
2002-02-06 23:40:40 +03:00
|
|
|
return NS_OK;
|
1999-12-04 04:13:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2002-04-27 09:33:09 +04:00
|
|
|
nsLocalFile::InitWithNativePath(const nsACString &filePath)
|
1999-12-04 04:13:44 +03:00
|
|
|
{
|
|
|
|
MakeDirty();
|
|
|
|
|
2002-04-27 09:33:09 +04:00
|
|
|
nsACString::const_iterator begin, end;
|
|
|
|
filePath.BeginReading(begin);
|
|
|
|
filePath.EndReading(end);
|
|
|
|
|
|
|
|
// input string must not be empty
|
|
|
|
if (begin == end)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
char firstChar = *begin;
|
|
|
|
char secondChar = *(++begin);
|
|
|
|
|
1999-12-04 04:13:44 +03:00
|
|
|
// just do a sanity check. if it has any forward slashes, it is not a Native path
|
|
|
|
// on windows. Also, it must have a colon at after the first char.
|
2002-04-27 09:33:09 +04:00
|
|
|
|
|
|
|
char *path = nsnull;
|
|
|
|
PRInt32 pathLen = 0;
|
|
|
|
|
|
|
|
if ( ( (secondChar == ':') && !FindCharInReadable('/', begin, end) ) || // normal path
|
|
|
|
( (firstChar == '\\') && (secondChar == '\\') ) ) // network path
|
1999-12-04 04:13:44 +03:00
|
|
|
{
|
|
|
|
// This is a native path
|
2002-04-27 09:33:09 +04:00
|
|
|
path = ToNewCString(filePath);
|
|
|
|
pathLen = filePath.Length();
|
1999-12-04 04:13:44 +03:00
|
|
|
}
|
|
|
|
|
2002-04-27 09:33:09 +04:00
|
|
|
if (path == nsnull)
|
1999-12-04 04:13:44 +03:00
|
|
|
return NS_ERROR_FILE_UNRECOGNIZED_PATH;
|
|
|
|
|
2002-04-27 09:33:09 +04:00
|
|
|
// kill any trailing '\' provided it isn't the second char of DBCS
|
|
|
|
PRInt32 len = pathLen - 1;
|
2002-05-08 00:09:07 +04:00
|
|
|
if (path[len] == '\\' && (!::IsDBCSLeadByte(path[len-1]) ||
|
|
|
|
_mbsrchr((const unsigned char *)path, '\\') == (const unsigned char *)path+len))
|
2002-04-27 09:33:09 +04:00
|
|
|
{
|
|
|
|
path[len] = '\0';
|
|
|
|
pathLen = len;
|
|
|
|
}
|
2000-07-13 03:31:31 +04:00
|
|
|
|
2002-04-27 09:33:09 +04:00
|
|
|
mWorkingPath.Adopt(path, pathLen);
|
1999-12-04 04:13:44 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-12-22 04:56:45 +03:00
|
|
|
NS_IMETHODIMP
|
2000-01-25 00:28:28 +03:00
|
|
|
nsLocalFile::OpenNSPRFileDesc(PRInt32 flags, PRInt32 mode, PRFileDesc **_retval)
|
1999-12-22 04:56:45 +03:00
|
|
|
{
|
2002-11-27 00:37:58 +03:00
|
|
|
// check to see if it is shortcut, i.e., it has ".lnk" in it
|
|
|
|
PRBool isShortcut = IsShortcut(mWorkingPath.get(), mWorkingPath.Length());
|
|
|
|
|
|
|
|
if (!isShortcut && mDirty)
|
2002-02-06 23:40:40 +03:00
|
|
|
{
|
2002-11-27 00:37:58 +03:00
|
|
|
// we will optimize here. If we are not a shortcut and we are opening
|
|
|
|
// a file and we are still dirty, assume that the working path is vaild
|
|
|
|
// and try to open it. The working path will be different from its
|
|
|
|
// resolved path for a shortcut file.
|
2002-02-06 23:40:40 +03:00
|
|
|
// If it does work, get the stat info via the file descriptor
|
|
|
|
mResolvedPath.Assign(mWorkingPath);
|
|
|
|
*_retval = PR_Open(mResolvedPath.get(), flags, mode);
|
|
|
|
if (*_retval)
|
2002-04-27 09:33:09 +04:00
|
|
|
{
|
|
|
|
PRStatus status = PR_GetOpenFileInfo64(*_retval, &mFileInfo64);
|
|
|
|
if (status == PR_SUCCESS)
|
|
|
|
{
|
|
|
|
mDirty = PR_FALSE;
|
|
|
|
mLastResolution = PR_TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
NS_ERROR("FileInfo64 invalid while PR_Open succeeded.");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2002-02-06 23:40:40 +03:00
|
|
|
}
|
|
|
|
|
1999-12-22 04:56:45 +03:00
|
|
|
nsresult rv = ResolveAndStat(PR_TRUE);
|
|
|
|
if (NS_FAILED(rv) && rv != NS_ERROR_FILE_NOT_FOUND)
|
|
|
|
return rv;
|
|
|
|
|
2001-11-01 12:09:57 +03:00
|
|
|
*_retval = PR_Open(mResolvedPath.get(), flags, mode);
|
1999-12-22 04:56:45 +03:00
|
|
|
|
2000-01-25 00:28:28 +03:00
|
|
|
if (*_retval)
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
return NS_ErrorAccordingToNSPR();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLocalFile::OpenANSIFileDesc(const char *mode, FILE * *_retval)
|
|
|
|
{
|
|
|
|
nsresult rv = ResolveAndStat(PR_TRUE);
|
|
|
|
if (NS_FAILED(rv) && rv != NS_ERROR_FILE_NOT_FOUND)
|
|
|
|
return rv;
|
|
|
|
|
2001-11-01 12:09:57 +03:00
|
|
|
*_retval = fopen(mResolvedPath.get(), mode);
|
2000-01-25 00:28:28 +03:00
|
|
|
|
1999-12-22 04:56:45 +03:00
|
|
|
if (*_retval)
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-12-04 04:13:44 +03:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLocalFile::Create(PRUint32 type, PRUint32 attributes)
|
|
|
|
{
|
|
|
|
if (type != NORMAL_FILE_TYPE && type != DIRECTORY_TYPE)
|
|
|
|
return NS_ERROR_FILE_UNKNOWN_TYPE;
|
|
|
|
|
|
|
|
nsresult rv = ResolveAndStat(PR_FALSE);
|
|
|
|
if (NS_FAILED(rv) && rv != NS_ERROR_FILE_NOT_FOUND)
|
|
|
|
return rv;
|
|
|
|
|
2003-05-15 01:56:49 +04:00
|
|
|
// create directories to target
|
|
|
|
//
|
|
|
|
// A given local file can be either one of these forms:
|
|
|
|
//
|
|
|
|
// - normal: X:\some\path\on\this\drive
|
|
|
|
// ^--- start here
|
|
|
|
//
|
|
|
|
// - UNC path: \\machine\volume\some\path\on\this\drive
|
|
|
|
// ^--- start here
|
|
|
|
//
|
|
|
|
// Skip the first 'X:\' for the first form, and skip the first full
|
|
|
|
// '\\machine\volume\' segment for the second form.
|
|
|
|
|
|
|
|
const unsigned char* path = (const unsigned char*) mResolvedPath.get();
|
|
|
|
if (path[0] == '\\' && path[1] == '\\')
|
|
|
|
{
|
|
|
|
// dealing with a UNC path here; skip past '\\machine\'
|
|
|
|
path = _mbschr(path + 2, '\\');
|
|
|
|
if (!path)
|
|
|
|
return NS_ERROR_FILE_INVALID_PATH;
|
|
|
|
++path;
|
|
|
|
}
|
|
|
|
|
|
|
|
// search for first slash after the drive (or volume) name
|
|
|
|
unsigned char* slash = _mbschr(path, '\\');
|
1999-12-04 04:13:44 +03:00
|
|
|
|
2002-01-08 05:20:13 +03:00
|
|
|
if (slash)
|
1999-12-04 04:13:44 +03:00
|
|
|
{
|
2002-01-08 05:20:13 +03:00
|
|
|
// skip the first '\\'
|
1999-12-04 04:13:44 +03:00
|
|
|
++slash;
|
2000-04-26 07:57:34 +04:00
|
|
|
slash = _mbschr(slash, '\\');
|
2002-01-08 05:20:13 +03:00
|
|
|
|
|
|
|
while (slash)
|
|
|
|
{
|
|
|
|
*slash = '\0';
|
2002-04-27 09:33:09 +04:00
|
|
|
|
|
|
|
if (!CreateDirectoryA(mResolvedPath.get(), NULL)) {
|
|
|
|
rv = ConvertWinError(GetLastError());
|
2003-05-15 01:56:49 +04:00
|
|
|
// perhaps the base path already exists, or perhaps we don't have
|
|
|
|
// permissions to create the directory. NOTE: access denied could
|
|
|
|
// occur on a parent directory even though it exists.
|
|
|
|
if (rv != NS_ERROR_FILE_ALREADY_EXISTS &&
|
|
|
|
rv != NS_ERROR_FILE_ACCESS_DENIED)
|
|
|
|
return rv;
|
2002-04-27 09:33:09 +04:00
|
|
|
}
|
|
|
|
*slash = '\\';
|
|
|
|
++slash;
|
|
|
|
slash = _mbschr(slash, '\\');
|
2002-01-08 05:20:13 +03:00
|
|
|
}
|
1999-12-04 04:13:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (type == NORMAL_FILE_TYPE)
|
|
|
|
{
|
2001-11-01 12:09:57 +03:00
|
|
|
PRFileDesc* file = PR_Open(mResolvedPath.get(), PR_RDONLY | PR_CREATE_FILE | PR_APPEND | PR_EXCL, attributes);
|
2000-06-24 05:50:53 +04:00
|
|
|
if (!file) return NS_ERROR_FILE_ALREADY_EXISTS;
|
|
|
|
|
|
|
|
PR_Close(file);
|
1999-12-04 04:13:44 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type == DIRECTORY_TYPE)
|
|
|
|
{
|
2001-11-01 12:09:57 +03:00
|
|
|
if (!CreateDirectoryA(mResolvedPath.get(), NULL))
|
2000-06-24 05:50:53 +04:00
|
|
|
return ConvertWinError(GetLastError());
|
|
|
|
else
|
|
|
|
return NS_OK;
|
1999-12-04 04:13:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_ERROR_FILE_UNKNOWN_TYPE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2002-04-27 09:33:09 +04:00
|
|
|
nsLocalFile::AppendNative(const nsACString &node)
|
1999-12-04 04:13:44 +03:00
|
|
|
{
|
2002-09-04 02:40:45 +04:00
|
|
|
if (node.IsEmpty())
|
|
|
|
return NS_OK;
|
|
|
|
|
2000-05-05 09:47:32 +04:00
|
|
|
// Append only one component. Check for subdirs.
|
2002-04-27 09:33:09 +04:00
|
|
|
// XXX can we avoid the PromiseFlatCString call?
|
2002-09-04 02:40:45 +04:00
|
|
|
if (_mbschr((const unsigned char*) PromiseFlatCString(node).get(), '\\') != nsnull)
|
2000-05-05 09:47:32 +04:00
|
|
|
return NS_ERROR_FILE_UNRECOGNIZED_PATH;
|
2002-09-04 02:40:45 +04:00
|
|
|
|
2002-04-27 09:33:09 +04:00
|
|
|
return AppendRelativeNativePath(node);
|
2000-05-05 09:47:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2002-04-27 09:33:09 +04:00
|
|
|
nsLocalFile::AppendRelativeNativePath(const nsACString &node)
|
2000-05-05 09:47:32 +04:00
|
|
|
{
|
|
|
|
// Cannot start with a / or have .. or have / anywhere
|
2002-04-27 09:33:09 +04:00
|
|
|
nsACString::const_iterator begin, end;
|
|
|
|
node.BeginReading(begin);
|
|
|
|
node.EndReading(end);
|
|
|
|
if (node.IsEmpty() || FindCharInReadable('/', begin, end))
|
1999-12-04 04:13:44 +03:00
|
|
|
{
|
2000-01-25 00:28:28 +03:00
|
|
|
return NS_ERROR_FILE_UNRECOGNIZED_PATH;
|
1999-12-04 04:13:44 +03:00
|
|
|
}
|
2000-01-25 00:28:28 +03:00
|
|
|
MakeDirty();
|
2002-04-27 09:33:09 +04:00
|
|
|
mWorkingPath.Append(NS_LITERAL_CSTRING("\\") + node);
|
1999-12-04 04:13:44 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLocalFile::Normalize()
|
|
|
|
{
|
1999-12-08 02:31:25 +03:00
|
|
|
return NS_OK;
|
1999-12-04 04:13:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2002-04-27 09:33:09 +04:00
|
|
|
nsLocalFile::GetNativeLeafName(nsACString &aLeafName)
|
1999-12-04 04:13:44 +03:00
|
|
|
{
|
2002-04-27 09:33:09 +04:00
|
|
|
aLeafName.Truncate();
|
1999-12-04 04:13:44 +03:00
|
|
|
|
2001-03-02 12:26:57 +03:00
|
|
|
const char* temp = mWorkingPath.get();
|
1999-12-04 04:13:44 +03:00
|
|
|
if(temp == nsnull)
|
|
|
|
return NS_ERROR_FILE_UNRECOGNIZED_PATH;
|
|
|
|
|
2000-04-26 07:57:34 +04:00
|
|
|
const char* leaf = (const char*) _mbsrchr((const unsigned char*) temp, '\\');
|
1999-12-04 04:13:44 +03:00
|
|
|
|
|
|
|
// if the working path is just a node without any lashes.
|
|
|
|
if (leaf == nsnull)
|
|
|
|
leaf = temp;
|
|
|
|
else
|
|
|
|
leaf++;
|
|
|
|
|
2002-04-27 09:33:09 +04:00
|
|
|
aLeafName.Assign(leaf);
|
1999-12-04 04:13:44 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2000-01-25 00:28:28 +03:00
|
|
|
NS_IMETHODIMP
|
2002-04-27 09:33:09 +04:00
|
|
|
nsLocalFile::SetNativeLeafName(const nsACString &aLeafName)
|
2000-01-25 00:28:28 +03:00
|
|
|
{
|
|
|
|
MakeDirty();
|
|
|
|
|
2001-03-02 12:26:57 +03:00
|
|
|
const unsigned char* temp = (const unsigned char*) mWorkingPath.get();
|
2000-01-25 00:28:28 +03:00
|
|
|
if(temp == nsnull)
|
|
|
|
return NS_ERROR_FILE_UNRECOGNIZED_PATH;
|
|
|
|
|
2000-04-26 07:57:34 +04:00
|
|
|
// cannot use nsCString::RFindChar() due to 0x5c problem
|
|
|
|
PRInt32 offset = (PRInt32) (_mbsrchr(temp, '\\') - temp);
|
2000-01-25 00:28:28 +03:00
|
|
|
if (offset)
|
|
|
|
{
|
|
|
|
mWorkingPath.Truncate(offset+1);
|
|
|
|
}
|
|
|
|
mWorkingPath.Append(aLeafName);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-12-04 04:13:44 +03:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2002-04-27 09:33:09 +04:00
|
|
|
nsLocalFile::GetNativePath(nsACString &_retval)
|
1999-12-04 04:13:44 +03:00
|
|
|
{
|
2002-04-27 09:33:09 +04:00
|
|
|
_retval = mWorkingPath;
|
1999-12-04 04:13:44 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2002-04-27 09:33:09 +04:00
|
|
|
nsLocalFile::CopySingleFile(nsIFile *sourceFile, nsIFile *destParent, const nsACString &newName, PRBool followSymlinks, PRBool move)
|
1999-12-04 04:13:44 +03:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2002-04-27 09:33:09 +04:00
|
|
|
nsCAutoString filePath;
|
1999-12-04 04:13:44 +03:00
|
|
|
|
|
|
|
// get the path that we are going to copy to.
|
|
|
|
// Since windows does not know how to auto
|
|
|
|
// resolve shortcust, we must work with the
|
|
|
|
// target.
|
2002-04-27 09:33:09 +04:00
|
|
|
nsCAutoString destPath;
|
|
|
|
destParent->GetNativeTarget(destPath);
|
1999-12-04 04:13:44 +03:00
|
|
|
|
|
|
|
destPath.Append("\\");
|
|
|
|
|
2002-04-27 09:33:09 +04:00
|
|
|
if (newName.IsEmpty())
|
1999-12-04 04:13:44 +03:00
|
|
|
{
|
2002-04-27 09:33:09 +04:00
|
|
|
nsCAutoString aFileName;
|
|
|
|
sourceFile->GetNativeLeafName(aFileName);
|
1999-12-04 04:13:44 +03:00
|
|
|
destPath.Append(aFileName);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
destPath.Append(newName);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (followSymlinks)
|
|
|
|
{
|
2002-04-27 09:33:09 +04:00
|
|
|
rv = sourceFile->GetNativeTarget(filePath);
|
|
|
|
if (filePath.IsEmpty())
|
|
|
|
rv = sourceFile->GetNativePath(filePath);
|
1999-12-04 04:13:44 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2002-04-27 09:33:09 +04:00
|
|
|
rv = sourceFile->GetNativePath(filePath);
|
1999-12-04 04:13:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
int copyOK;
|
|
|
|
|
|
|
|
if (!move)
|
2002-04-27 09:33:09 +04:00
|
|
|
copyOK = CopyFile(filePath.get(), destPath.get(), PR_TRUE);
|
2002-11-13 17:04:51 +03:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// What we have to do is check to see if the destPath exists. If it
|
|
|
|
// does, we have to move it out of the say so that MoveFile will
|
|
|
|
// succeed. However, we don't want to just remove it since MoveFile
|
|
|
|
// can fail leaving us without a file.
|
|
|
|
|
|
|
|
nsCAutoString backup;
|
|
|
|
PRFileInfo64 fileInfo64;
|
|
|
|
PRStatus status = PR_GetFileInfo64(destPath.get(), &fileInfo64);
|
|
|
|
if (status == PR_SUCCESS)
|
|
|
|
{
|
|
|
|
|
|
|
|
// the file exists. Check to make sure it is not a directory,
|
|
|
|
// then move it out of the way.
|
|
|
|
if (fileInfo64.type == PR_FILE_FILE)
|
|
|
|
{
|
|
|
|
backup.Append(destPath);
|
|
|
|
backup.Append(".moztmp");
|
|
|
|
|
|
|
|
// remove any existing backup file that we may already have.
|
|
|
|
// maybe we should be doing some kind of unique naming here,
|
|
|
|
// but why bother.
|
|
|
|
remove(backup.get());
|
|
|
|
|
|
|
|
// move destination file to backup file
|
|
|
|
copyOK = MoveFile(destPath.get(), backup.get());
|
|
|
|
if (!copyOK)
|
|
|
|
{
|
|
|
|
// I guess we can't do the backup copy, so return.
|
|
|
|
rv = ConvertWinError(GetLastError());
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// move source file to destination file
|
2002-04-27 09:33:09 +04:00
|
|
|
copyOK = MoveFile(filePath.get(), destPath.get());
|
1999-12-04 04:13:44 +03:00
|
|
|
|
2002-11-13 17:04:51 +03:00
|
|
|
if (!backup.IsEmpty())
|
|
|
|
{
|
|
|
|
if (copyOK)
|
|
|
|
{
|
|
|
|
// remove the backup copy.
|
2003-06-19 03:19:11 +04:00
|
|
|
remove(backup.get());
|
2002-11-13 17:04:51 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// restore backup
|
|
|
|
int backupOk = MoveFile(backup.get(), destPath.get());
|
|
|
|
NS_ASSERTION(backupOk, "move backup failed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1999-12-04 04:13:44 +03:00
|
|
|
if (!copyOK) // CopyFile and MoveFile returns non-zero if succeeds (backward if you ask me).
|
|
|
|
rv = ConvertWinError(GetLastError());
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
2002-04-27 09:33:09 +04:00
|
|
|
nsLocalFile::CopyMove(nsIFile *aParentDir, const nsACString &newName, PRBool followSymlinks, PRBool move)
|
1999-12-04 04:13:44 +03:00
|
|
|
{
|
2000-03-24 08:47:39 +03:00
|
|
|
nsCOMPtr<nsIFile> newParentDir = aParentDir;
|
1999-12-04 04:13:44 +03:00
|
|
|
// check to see if this exists, otherwise return an error.
|
|
|
|
// we will check this by resolving. If the user wants us
|
|
|
|
// to follow links, then we are talking about the target,
|
|
|
|
// hence we can use the |followSymlinks| parameter.
|
|
|
|
nsresult rv = ResolveAndStat(followSymlinks);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
2000-03-24 08:47:39 +03:00
|
|
|
if (!newParentDir)
|
|
|
|
{
|
|
|
|
// no parent was specified. We must rename.
|
|
|
|
|
2002-04-27 09:33:09 +04:00
|
|
|
if (newName.IsEmpty())
|
2000-03-24 08:47:39 +03:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
|
|
|
|
rv = GetParent(getter_AddRefs(newParentDir));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!newParentDir)
|
|
|
|
return NS_ERROR_FILE_DESTINATION_NOT_DIR;
|
|
|
|
|
1999-12-04 04:13:44 +03:00
|
|
|
// make sure it exists and is a directory. Create it if not there.
|
|
|
|
PRBool exists;
|
|
|
|
newParentDir->Exists(&exists);
|
2002-11-13 17:04:51 +03:00
|
|
|
if (!exists)
|
1999-12-04 04:13:44 +03:00
|
|
|
{
|
|
|
|
rv = newParentDir->Create(DIRECTORY_TYPE, 0644); // TODO, what permissions should we use
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
PRBool isDir;
|
|
|
|
newParentDir->IsDirectory(&isDir);
|
|
|
|
if (isDir == PR_FALSE)
|
|
|
|
{
|
|
|
|
if (followSymlinks)
|
|
|
|
{
|
|
|
|
PRBool isLink;
|
|
|
|
newParentDir->IsSymlink(&isLink);
|
|
|
|
if (isLink)
|
|
|
|
{
|
2002-04-27 09:33:09 +04:00
|
|
|
nsCAutoString target;
|
|
|
|
newParentDir->GetNativeTarget(target);
|
1999-12-04 04:13:44 +03:00
|
|
|
|
2000-01-25 00:28:28 +03:00
|
|
|
nsCOMPtr<nsILocalFile> realDest = new nsLocalFile();
|
|
|
|
if (realDest == nsnull)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
1999-12-04 04:13:44 +03:00
|
|
|
|
2002-04-27 09:33:09 +04:00
|
|
|
rv = realDest->InitWithNativePath(target);
|
1999-12-04 04:13:44 +03:00
|
|
|
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
return CopyMove(realDest, newName, followSymlinks, move);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return NS_ERROR_FILE_DESTINATION_NOT_DIR;
|
|
|
|
}
|
2000-03-24 08:47:39 +03:00
|
|
|
}
|
1999-12-04 04:13:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// check to see if we are a directory, if so enumerate it.
|
|
|
|
|
|
|
|
PRBool isDir;
|
|
|
|
IsDirectory(&isDir);
|
1999-12-08 05:07:38 +03:00
|
|
|
PRBool isSymlink;
|
|
|
|
IsSymlink(&isSymlink);
|
|
|
|
|
|
|
|
if (!isDir || (isSymlink && !followSymlinks))
|
1999-12-04 04:13:44 +03:00
|
|
|
{
|
|
|
|
rv = CopySingleFile(this, newParentDir, newName, followSymlinks, move);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// create a new target destination in the new parentDir;
|
1999-12-22 04:56:45 +03:00
|
|
|
nsCOMPtr<nsIFile> target;
|
|
|
|
rv = newParentDir->Clone(getter_AddRefs(target));
|
|
|
|
|
1999-12-04 04:13:44 +03:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
2002-04-27 09:33:09 +04:00
|
|
|
nsCAutoString allocatedNewName;
|
|
|
|
if (newName.IsEmpty())
|
1999-12-04 04:13:44 +03:00
|
|
|
{
|
1999-12-08 02:31:25 +03:00
|
|
|
PRBool isLink;
|
|
|
|
IsSymlink(&isLink);
|
|
|
|
if (isLink)
|
|
|
|
{
|
2002-04-27 09:33:09 +04:00
|
|
|
nsCAutoString temp;
|
|
|
|
GetNativeTarget(temp);
|
|
|
|
const char* leaf = (const char*) _mbsrchr((const unsigned char*) temp.get(), '\\');
|
1999-12-08 02:31:25 +03:00
|
|
|
if (leaf[0] == '\\')
|
|
|
|
leaf++;
|
2002-04-27 09:33:09 +04:00
|
|
|
allocatedNewName = leaf;
|
1999-12-08 02:31:25 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2002-04-27 09:33:09 +04:00
|
|
|
GetNativeLeafName(allocatedNewName);// this should be the leaf name of the
|
1999-12-08 02:31:25 +03:00
|
|
|
}
|
1999-12-04 04:13:44 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2002-04-27 09:33:09 +04:00
|
|
|
allocatedNewName = newName;
|
1999-12-04 04:13:44 +03:00
|
|
|
}
|
|
|
|
|
2002-04-27 09:33:09 +04:00
|
|
|
rv = target->AppendNative(allocatedNewName);
|
1999-12-04 04:13:44 +03:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
2002-04-27 09:33:09 +04:00
|
|
|
allocatedNewName.Truncate();
|
2002-11-13 17:04:51 +03:00
|
|
|
|
|
|
|
// check if the destination directory already exists
|
|
|
|
target->Exists(&exists);
|
|
|
|
if (!exists)
|
|
|
|
{
|
|
|
|
// if the destination directory cannot be created, return an error
|
|
|
|
rv = target->Create(DIRECTORY_TYPE, 0644); // TODO, what permissions should we use
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// check if the destination directory is writable and empty
|
|
|
|
PRBool isWritable;
|
|
|
|
|
|
|
|
target->IsWritable(&isWritable);
|
|
|
|
if (!isWritable)
|
|
|
|
return NS_ERROR_FILE_ACCESS_DENIED;
|
|
|
|
|
|
|
|
nsCOMPtr<nsISimpleEnumerator> targetIterator;
|
|
|
|
rv = target->GetDirectoryEntries(getter_AddRefs(targetIterator));
|
|
|
|
|
|
|
|
PRBool more;
|
|
|
|
targetIterator->HasMoreElements(&more);
|
|
|
|
// return error if target directory is not empty
|
|
|
|
if (more)
|
|
|
|
return NS_ERROR_FILE_DIR_NOT_EMPTY;
|
|
|
|
}
|
1999-12-04 04:13:44 +03:00
|
|
|
|
2003-08-25 22:28:10 +04:00
|
|
|
nsDirEnumerator dirEnum;
|
|
|
|
|
|
|
|
rv = dirEnum.Init(this);
|
2002-06-04 22:47:37 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("dirEnum initalization failed");
|
|
|
|
return rv;
|
|
|
|
}
|
1999-12-04 04:13:44 +03:00
|
|
|
|
|
|
|
PRBool more;
|
2003-08-25 22:28:10 +04:00
|
|
|
while (NS_SUCCEEDED(dirEnum.HasMoreElements(&more)) && more)
|
1999-12-04 04:13:44 +03:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsISupports> item;
|
|
|
|
nsCOMPtr<nsIFile> file;
|
2003-08-25 22:28:10 +04:00
|
|
|
dirEnum.GetNext(getter_AddRefs(item));
|
1999-12-04 04:13:44 +03:00
|
|
|
file = do_QueryInterface(item);
|
2002-07-18 09:09:10 +04:00
|
|
|
if (file)
|
|
|
|
{
|
|
|
|
PRBool isDir, isLink;
|
1999-12-04 04:13:44 +03:00
|
|
|
|
2002-07-18 09:09:10 +04:00
|
|
|
file->IsDirectory(&isDir);
|
|
|
|
file->IsSymlink(&isLink);
|
|
|
|
|
|
|
|
if (move)
|
|
|
|
{
|
|
|
|
if (followSymlinks)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
rv = file->MoveToNative(target, nsCString());
|
|
|
|
NS_ENSURE_SUCCESS(rv,rv);
|
|
|
|
}
|
1999-12-04 04:13:44 +03:00
|
|
|
else
|
2002-07-18 09:09:10 +04:00
|
|
|
{
|
|
|
|
if (followSymlinks)
|
|
|
|
rv = file->CopyToFollowingLinksNative(target, nsCString());
|
|
|
|
else
|
|
|
|
rv = file->CopyToNative(target, nsCString());
|
|
|
|
NS_ENSURE_SUCCESS(rv,rv);
|
|
|
|
}
|
1999-12-04 04:13:44 +03:00
|
|
|
}
|
|
|
|
}
|
2002-04-25 22:56:04 +04:00
|
|
|
// we've finished moving all the children of this directory
|
|
|
|
// in the new directory. so now delete the directory
|
|
|
|
// note, we don't need to do a recursive delete.
|
|
|
|
// MoveTo() is recursive. At this point,
|
|
|
|
// we've already moved the children of the current folder
|
|
|
|
// to the new location. nothing should be left in the folder.
|
|
|
|
if (move)
|
2002-06-04 22:47:37 +04:00
|
|
|
{
|
2002-04-25 22:56:04 +04:00
|
|
|
rv = Remove(PR_FALSE /* recursive */);
|
2002-06-04 22:47:37 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv,rv);
|
|
|
|
}
|
1999-12-04 04:13:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// If we moved, we want to adjust this.
|
|
|
|
if (move)
|
|
|
|
{
|
1999-12-22 04:56:45 +03:00
|
|
|
MakeDirty();
|
|
|
|
|
2002-04-27 09:33:09 +04:00
|
|
|
nsCAutoString newParentPath;
|
|
|
|
newParentDir->GetNativePath(newParentPath);
|
1999-12-22 04:56:45 +03:00
|
|
|
|
2002-04-27 09:33:09 +04:00
|
|
|
if (newParentPath.IsEmpty())
|
1999-12-22 04:56:45 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2002-04-27 09:33:09 +04:00
|
|
|
if (newName.IsEmpty())
|
1999-12-04 04:13:44 +03:00
|
|
|
{
|
2002-04-27 09:33:09 +04:00
|
|
|
nsCAutoString aFileName;
|
|
|
|
GetNativeLeafName(aFileName);
|
1999-12-22 04:56:45 +03:00
|
|
|
|
2002-04-27 09:33:09 +04:00
|
|
|
InitWithNativePath(newParentPath);
|
|
|
|
AppendNative(aFileName);
|
1999-12-04 04:13:44 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2002-04-27 09:33:09 +04:00
|
|
|
InitWithNativePath(newParentPath);
|
|
|
|
AppendNative(newName);
|
1999-12-04 04:13:44 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2002-04-27 09:33:09 +04:00
|
|
|
nsLocalFile::CopyToNative(nsIFile *newParentDir, const nsACString &newName)
|
1999-12-04 04:13:44 +03:00
|
|
|
{
|
|
|
|
return CopyMove(newParentDir, newName, PR_FALSE, PR_FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2002-04-27 09:33:09 +04:00
|
|
|
nsLocalFile::CopyToFollowingLinksNative(nsIFile *newParentDir, const nsACString &newName)
|
1999-12-04 04:13:44 +03:00
|
|
|
{
|
|
|
|
return CopyMove(newParentDir, newName, PR_TRUE, PR_FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2002-04-27 09:33:09 +04:00
|
|
|
nsLocalFile::MoveToNative(nsIFile *newParentDir, const nsACString &newName)
|
1999-12-04 04:13:44 +03:00
|
|
|
{
|
|
|
|
return CopyMove(newParentDir, newName, PR_FALSE, PR_TRUE);
|
|
|
|
}
|
|
|
|
|
1999-12-22 06:01:08 +03:00
|
|
|
NS_IMETHODIMP
|
1999-12-22 07:11:13 +03:00
|
|
|
nsLocalFile::Load(PRLibrary * *_retval)
|
1999-12-22 06:01:08 +03:00
|
|
|
{
|
|
|
|
PRBool isFile;
|
|
|
|
nsresult rv = IsFile(&isFile);
|
|
|
|
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
if (! isFile)
|
|
|
|
return NS_ERROR_FILE_IS_DIRECTORY;
|
|
|
|
|
2001-08-17 06:03:34 +04:00
|
|
|
NS_TIMELINE_START_TIMER("PR_LoadLibrary");
|
2001-11-01 12:09:57 +03:00
|
|
|
*_retval = PR_LoadLibrary(mResolvedPath.get());
|
2001-08-17 06:03:34 +04:00
|
|
|
NS_TIMELINE_STOP_TIMER("PR_LoadLibrary");
|
2001-11-17 23:23:48 +03:00
|
|
|
NS_TIMELINE_MARK_TIMER1("PR_LoadLibrary", mResolvedPath.get());
|
|
|
|
|
1999-12-22 06:01:08 +03:00
|
|
|
if (*_retval)
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
|
|
|
|
1999-12-04 04:13:44 +03:00
|
|
|
NS_IMETHODIMP
|
2001-07-24 22:38:25 +04:00
|
|
|
nsLocalFile::Remove(PRBool recursive)
|
1999-12-04 04:13:44 +03:00
|
|
|
{
|
2003-08-25 22:28:10 +04:00
|
|
|
nsresult rv;
|
|
|
|
PRBool isDir, isLink;
|
|
|
|
nsXPIDLCString buf;
|
|
|
|
const char *filePath;
|
|
|
|
|
|
|
|
// NOTE:
|
|
|
|
//
|
|
|
|
// if the working path points to a shortcut, then we will only
|
|
|
|
// delete the shortcut itself. even if the shortcut points to
|
|
|
|
// a directory, we will not recurse into that directory or
|
|
|
|
// delete that directory itself. likewise, if the shortcut
|
|
|
|
// points to a normal file, we will not delete the real file.
|
|
|
|
// this is done to be consistent with the other platforms that
|
|
|
|
// behave this way. we do this even if the followLinks attribute
|
|
|
|
// is set to true. this helps protect against misuse that could
|
|
|
|
// lead to security bugs (e.g., bug 210588).
|
|
|
|
//
|
|
|
|
// in the case of non-terminal shortcuts, those are all followed
|
|
|
|
// unconditionally. this is done because 1) we only delete
|
|
|
|
// terminal nodes and possibly their children, and 2) the remove
|
|
|
|
// and rmdir CRT calls don't know how to handle shortcuts.
|
|
|
|
|
|
|
|
IsSymlink(&isLink);
|
|
|
|
if (isLink)
|
|
|
|
{
|
|
|
|
isDir = PR_FALSE;
|
|
|
|
// resolve non-terminal nodes only.
|
|
|
|
rv = ResolvePath(mWorkingPath.get(), PR_FALSE, getter_Copies(buf));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
filePath = buf.get();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rv = IsDirectory(&isDir);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
// in this case, it doesn't matter that terminal nodes were
|
|
|
|
// resolved, so we can safely leverage mResolvedPath.
|
|
|
|
filePath = mResolvedPath.get();
|
|
|
|
}
|
1999-12-04 04:13:44 +03:00
|
|
|
|
|
|
|
if (isDir)
|
|
|
|
{
|
|
|
|
if (recursive)
|
|
|
|
{
|
2003-08-25 22:28:10 +04:00
|
|
|
nsDirEnumerator dirEnum;
|
|
|
|
|
|
|
|
rv = dirEnum.Init(this);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
1999-12-04 04:13:44 +03:00
|
|
|
|
|
|
|
PRBool more;
|
2003-08-25 22:28:10 +04:00
|
|
|
while (NS_SUCCEEDED(dirEnum.HasMoreElements(&more)) && more)
|
1999-12-04 04:13:44 +03:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsISupports> item;
|
2003-08-25 22:28:10 +04:00
|
|
|
dirEnum.GetNext(getter_AddRefs(item));
|
|
|
|
nsCOMPtr<nsIFile> file = do_QueryInterface(item);
|
2002-07-18 09:09:10 +04:00
|
|
|
if (file)
|
|
|
|
file->Remove(recursive);
|
1999-12-04 04:13:44 +03:00
|
|
|
}
|
|
|
|
}
|
2003-08-25 22:28:10 +04:00
|
|
|
rv = rmdir(filePath);
|
1999-12-04 04:13:44 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2003-08-25 22:28:10 +04:00
|
|
|
rv = remove(filePath);
|
1999-12-04 04:13:44 +03:00
|
|
|
}
|
2003-08-25 22:28:10 +04:00
|
|
|
|
|
|
|
// fixup error code if necessary...
|
|
|
|
if (rv == -1)
|
|
|
|
rv = NSRESULT_FOR_ERRNO();
|
1999-12-04 04:13:44 +03:00
|
|
|
|
2000-01-25 00:28:28 +03:00
|
|
|
MakeDirty();
|
2002-01-26 03:38:37 +03:00
|
|
|
return rv;
|
1999-12-04 04:13:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2001-11-26 11:05:05 +03:00
|
|
|
nsLocalFile::GetLastModifiedTime(PRInt64 *aLastModifiedTime)
|
1999-12-04 04:13:44 +03:00
|
|
|
{
|
2001-11-26 11:05:05 +03:00
|
|
|
NS_ENSURE_ARG(aLastModifiedTime);
|
1999-12-04 04:13:44 +03:00
|
|
|
|
2001-11-26 11:05:05 +03:00
|
|
|
*aLastModifiedTime = 0;
|
1999-12-04 04:13:44 +03:00
|
|
|
|
|
|
|
nsresult rv = ResolveAndStat(PR_TRUE);
|
|
|
|
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
2000-05-09 23:11:14 +04:00
|
|
|
// microseconds -> milliseconds
|
2001-11-26 11:05:05 +03:00
|
|
|
*aLastModifiedTime = mFileInfo64.modifyTime / PR_USEC_PER_MSEC;
|
1999-12-04 04:13:44 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2001-11-26 11:05:05 +03:00
|
|
|
nsLocalFile::GetLastModifiedTimeOfLink(PRInt64 *aLastModifiedTime)
|
1999-12-04 04:13:44 +03:00
|
|
|
{
|
2001-11-26 11:05:05 +03:00
|
|
|
NS_ENSURE_ARG(aLastModifiedTime);
|
1999-12-04 04:13:44 +03:00
|
|
|
|
2001-11-26 11:05:05 +03:00
|
|
|
*aLastModifiedTime = 0;
|
1999-12-04 04:13:44 +03:00
|
|
|
|
|
|
|
nsresult rv = ResolveAndStat(PR_FALSE);
|
|
|
|
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
2000-05-09 23:11:14 +04:00
|
|
|
// microseconds -> milliseconds
|
2001-11-26 11:05:05 +03:00
|
|
|
*aLastModifiedTime = mFileInfo64.modifyTime / PR_USEC_PER_MSEC;
|
2000-05-09 23:11:14 +04:00
|
|
|
|
2000-02-22 00:51:47 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
1999-12-04 04:13:44 +03:00
|
|
|
|
|
|
|
|
2000-02-22 00:51:47 +03:00
|
|
|
NS_IMETHODIMP
|
2001-11-26 11:05:05 +03:00
|
|
|
nsLocalFile::SetLastModifiedTime(PRInt64 aLastModifiedTime)
|
2000-02-22 00:51:47 +03:00
|
|
|
{
|
2001-11-26 11:05:05 +03:00
|
|
|
return nsLocalFile::SetModDate(aLastModifiedTime, PR_TRUE);
|
1999-12-04 04:13:44 +03:00
|
|
|
}
|
|
|
|
|
2000-02-22 00:51:47 +03:00
|
|
|
|
1999-12-04 04:13:44 +03:00
|
|
|
NS_IMETHODIMP
|
2001-11-26 11:05:05 +03:00
|
|
|
nsLocalFile::SetLastModifiedTimeOfLink(PRInt64 aLastModifiedTime)
|
1999-12-04 04:13:44 +03:00
|
|
|
{
|
2001-11-26 11:05:05 +03:00
|
|
|
return nsLocalFile::SetModDate(aLastModifiedTime, PR_FALSE);
|
2000-02-22 00:51:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2001-11-26 11:05:05 +03:00
|
|
|
nsLocalFile::SetModDate(PRInt64 aLastModifiedTime, PRBool resolveTerminal)
|
2000-02-22 00:51:47 +03:00
|
|
|
{
|
|
|
|
nsresult rv = ResolveAndStat(resolveTerminal);
|
1999-12-04 04:13:44 +03:00
|
|
|
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
2001-03-02 12:26:57 +03:00
|
|
|
const char *filePath = mResolvedPath.get();
|
1999-12-04 04:13:44 +03:00
|
|
|
|
|
|
|
HANDLE file = CreateFile( filePath, // pointer to name of the file
|
|
|
|
GENERIC_WRITE, // access (write) mode
|
|
|
|
0, // share mode
|
|
|
|
NULL, // pointer to security attributes
|
|
|
|
OPEN_EXISTING, // how to create
|
2000-03-22 04:37:52 +03:00
|
|
|
0, // file attributes
|
1999-12-04 04:13:44 +03:00
|
|
|
NULL);
|
|
|
|
|
2000-02-22 00:51:47 +03:00
|
|
|
MakeDirty();
|
|
|
|
|
1999-12-04 04:13:44 +03:00
|
|
|
if (!file)
|
|
|
|
{
|
2000-03-22 04:37:52 +03:00
|
|
|
return ConvertWinError(GetLastError());
|
1999-12-04 04:13:44 +03:00
|
|
|
}
|
|
|
|
|
2000-03-28 07:39:02 +04:00
|
|
|
FILETIME lft, ft;
|
|
|
|
SYSTEMTIME st;
|
|
|
|
PRExplodedTime pret;
|
2000-05-09 23:11:14 +04:00
|
|
|
|
|
|
|
// PR_ExplodeTime expects usecs...
|
2001-11-26 11:05:05 +03:00
|
|
|
PR_ExplodeTime(aLastModifiedTime * PR_USEC_PER_MSEC, PR_LocalTimeParameters, &pret);
|
2000-03-28 07:39:02 +04:00
|
|
|
st.wYear = pret.tm_year;
|
|
|
|
st.wMonth = pret.tm_month + 1; // Convert start offset -- Win32: Jan=1; NSPR: Jan=0
|
|
|
|
st.wDayOfWeek = pret.tm_wday;
|
|
|
|
st.wDay = pret.tm_mday;
|
|
|
|
st.wHour = pret.tm_hour;
|
|
|
|
st.wMinute = pret.tm_min;
|
|
|
|
st.wSecond = pret.tm_sec;
|
|
|
|
st.wMilliseconds = pret.tm_usec/1000;
|
|
|
|
|
|
|
|
if ( 0 == SystemTimeToFileTime(&st, &lft) )
|
|
|
|
{
|
|
|
|
rv = ConvertWinError(GetLastError());
|
|
|
|
}
|
|
|
|
else if ( 0 == LocalFileTimeToFileTime(&lft, &ft) )
|
|
|
|
{
|
|
|
|
rv = ConvertWinError(GetLastError());
|
|
|
|
}
|
|
|
|
else if ( 0 == SetFileTime(file, NULL, &ft, &ft) )
|
1999-12-04 04:13:44 +03:00
|
|
|
{
|
|
|
|
// could not set time
|
2000-03-22 04:37:52 +03:00
|
|
|
rv = ConvertWinError(GetLastError());
|
1999-12-04 04:13:44 +03:00
|
|
|
}
|
2000-03-28 07:39:02 +04:00
|
|
|
|
1999-12-04 04:13:44 +03:00
|
|
|
CloseHandle( file );
|
2000-03-22 04:37:52 +03:00
|
|
|
return rv;
|
2000-02-22 00:51:47 +03:00
|
|
|
}
|
2000-03-28 07:39:02 +04:00
|
|
|
|
1999-12-04 04:13:44 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLocalFile::GetPermissions(PRUint32 *aPermissions)
|
|
|
|
{
|
2000-02-22 00:51:47 +03:00
|
|
|
nsresult rv = ResolveAndStat(PR_TRUE);
|
|
|
|
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
2001-03-02 12:26:57 +03:00
|
|
|
const char *filePath = mResolvedPath.get();
|
2000-02-22 00:51:47 +03:00
|
|
|
|
|
|
|
|
|
|
|
return NS_OK;
|
1999-12-04 04:13:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLocalFile::GetPermissionsOfLink(PRUint32 *aPermissionsOfLink)
|
|
|
|
{
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLocalFile::SetPermissions(PRUint32 aPermissions)
|
|
|
|
{
|
2000-02-22 00:51:47 +03:00
|
|
|
nsresult rv = ResolveAndStat(PR_TRUE);
|
|
|
|
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
2001-03-02 12:26:57 +03:00
|
|
|
const char *filePath = mResolvedPath.get();
|
2000-02-22 00:51:47 +03:00
|
|
|
if( chmod(filePath, aPermissions) == -1 )
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
return NS_OK;
|
1999-12-04 04:13:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLocalFile::SetPermissionsOfLink(PRUint32 aPermissions)
|
|
|
|
{
|
2000-02-22 00:51:47 +03:00
|
|
|
nsresult rv = ResolveAndStat(PR_FALSE);
|
|
|
|
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
2001-03-02 12:26:57 +03:00
|
|
|
const char *filePath = mResolvedPath.get();
|
2000-02-22 00:51:47 +03:00
|
|
|
if( chmod(filePath, aPermissions) == -1 )
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
return NS_OK;
|
1999-12-04 04:13:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLocalFile::GetFileSize(PRInt64 *aFileSize)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG(aFileSize);
|
|
|
|
|
|
|
|
*aFileSize = 0;
|
|
|
|
|
|
|
|
nsresult rv = ResolveAndStat(PR_TRUE);
|
|
|
|
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
|
2000-02-22 00:51:47 +03:00
|
|
|
*aFileSize = mFileInfo64.size;
|
1999-12-04 04:13:44 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLocalFile::SetFileSize(PRInt64 aFileSize)
|
|
|
|
{
|
|
|
|
|
|
|
|
DWORD status;
|
|
|
|
HANDLE hFile;
|
|
|
|
|
|
|
|
nsresult rv = ResolveAndStat(PR_TRUE);
|
|
|
|
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
2001-03-02 12:26:57 +03:00
|
|
|
const char *filePath = mResolvedPath.get();
|
1999-12-04 04:13:44 +03:00
|
|
|
|
|
|
|
|
|
|
|
// Leave it to Microsoft to open an existing file with a function
|
|
|
|
// named "CreateFile".
|
|
|
|
hFile = CreateFile(filePath,
|
|
|
|
GENERIC_WRITE,
|
|
|
|
FILE_SHARE_READ,
|
|
|
|
NULL,
|
|
|
|
OPEN_EXISTING,
|
1999-12-11 02:33:30 +03:00
|
|
|
FILE_ATTRIBUTE_NORMAL,
|
1999-12-04 04:13:44 +03:00
|
|
|
NULL);
|
|
|
|
|
|
|
|
if (hFile == INVALID_HANDLE_VALUE)
|
2000-01-25 00:28:28 +03:00
|
|
|
{
|
|
|
|
MakeDirty();
|
1999-12-04 04:13:44 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
2000-01-25 00:28:28 +03:00
|
|
|
}
|
2000-01-27 00:55:13 +03:00
|
|
|
|
1999-12-04 04:13:44 +03:00
|
|
|
// Seek to new, desired end of file
|
1999-12-08 02:31:25 +03:00
|
|
|
PRInt32 hi, lo;
|
|
|
|
myLL_L2II(aFileSize, &hi, &lo );
|
|
|
|
|
|
|
|
status = SetFilePointer(hFile, lo, NULL, FILE_BEGIN);
|
1999-12-04 04:13:44 +03:00
|
|
|
if (status == 0xffffffff)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
// Truncate file at current cursor position
|
|
|
|
if (!SetEndOfFile(hFile))
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
if (!CloseHandle(hFile))
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2000-01-25 00:28:28 +03:00
|
|
|
MakeDirty();
|
1999-12-04 04:13:44 +03:00
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
error:
|
2000-01-25 00:28:28 +03:00
|
|
|
MakeDirty();
|
1999-12-04 04:13:44 +03:00
|
|
|
CloseHandle(hFile);
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLocalFile::GetFileSizeOfLink(PRInt64 *aFileSize)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG(aFileSize);
|
|
|
|
|
|
|
|
*aFileSize = 0;
|
|
|
|
|
|
|
|
nsresult rv = ResolveAndStat(PR_FALSE);
|
|
|
|
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
2000-02-22 00:51:47 +03:00
|
|
|
*aFileSize = mFileInfo64.size;
|
1999-12-04 04:13:44 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLocalFile::GetDiskSpaceAvailable(PRInt64 *aDiskSpaceAvailable)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG(aDiskSpaceAvailable);
|
|
|
|
|
|
|
|
ResolveAndStat(PR_FALSE);
|
|
|
|
|
|
|
|
PRInt64 int64;
|
|
|
|
|
|
|
|
LL_I2L(int64 , LONG_MAX);
|
|
|
|
|
|
|
|
// Check disk space
|
|
|
|
DWORD dwSecPerClus, dwBytesPerSec, dwFreeClus, dwTotalClus;
|
|
|
|
ULARGE_INTEGER liFreeBytesAvailableToCaller, liTotalNumberOfBytes, liTotalNumberOfFreeBytes;
|
|
|
|
double nBytes = 0;
|
|
|
|
|
|
|
|
BOOL (WINAPI* getDiskFreeSpaceExA)(LPCTSTR lpDirectoryName,
|
|
|
|
PULARGE_INTEGER lpFreeBytesAvailableToCaller,
|
|
|
|
PULARGE_INTEGER lpTotalNumberOfBytes,
|
|
|
|
PULARGE_INTEGER lpTotalNumberOfFreeBytes) = NULL;
|
|
|
|
|
|
|
|
HINSTANCE hInst = LoadLibrary("KERNEL32.DLL");
|
|
|
|
NS_ASSERTION(hInst != NULL, "COULD NOT LOAD KERNEL32.DLL");
|
|
|
|
if (hInst != NULL)
|
|
|
|
{
|
|
|
|
getDiskFreeSpaceExA = (BOOL (WINAPI*)(LPCTSTR lpDirectoryName,
|
|
|
|
PULARGE_INTEGER lpFreeBytesAvailableToCaller,
|
|
|
|
PULARGE_INTEGER lpTotalNumberOfBytes,
|
|
|
|
PULARGE_INTEGER lpTotalNumberOfFreeBytes))
|
|
|
|
GetProcAddress(hInst, "GetDiskFreeSpaceExA");
|
|
|
|
FreeLibrary(hInst);
|
|
|
|
}
|
|
|
|
|
2002-02-07 17:46:10 +03:00
|
|
|
if (getDiskFreeSpaceExA && (*getDiskFreeSpaceExA)(mResolvedPath.get(),
|
1999-12-04 04:13:44 +03:00
|
|
|
&liFreeBytesAvailableToCaller,
|
|
|
|
&liTotalNumberOfBytes,
|
|
|
|
&liTotalNumberOfFreeBytes))
|
|
|
|
{
|
|
|
|
nBytes = (double)(signed __int64)liFreeBytesAvailableToCaller.QuadPart;
|
|
|
|
}
|
2002-02-07 17:46:10 +03:00
|
|
|
else {
|
|
|
|
char aDrive[_MAX_DRIVE + 2];
|
|
|
|
_splitpath( mResolvedPath.get(), aDrive, NULL, NULL, NULL);
|
|
|
|
strcat(aDrive, "\\");
|
1999-12-04 04:13:44 +03:00
|
|
|
|
2002-02-07 17:46:10 +03:00
|
|
|
if ( GetDiskFreeSpace(aDrive, &dwSecPerClus, &dwBytesPerSec, &dwFreeClus, &dwTotalClus))
|
|
|
|
{
|
|
|
|
nBytes = (double)dwFreeClus*(double)dwSecPerClus*(double) dwBytesPerSec;
|
|
|
|
}
|
|
|
|
}
|
1999-12-04 04:13:44 +03:00
|
|
|
LL_D2L(*aDiskSpaceAvailable, nBytes);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLocalFile::GetParent(nsIFile * *aParent)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aParent);
|
|
|
|
|
2002-04-27 09:33:09 +04:00
|
|
|
nsCAutoString parentPath(mWorkingPath);
|
1999-12-04 04:13:44 +03:00
|
|
|
|
2000-04-26 07:57:34 +04:00
|
|
|
// cannot use nsCString::RFindChar() due to 0x5c problem
|
2001-03-02 12:26:57 +03:00
|
|
|
PRInt32 offset = (PRInt32) (_mbsrchr((const unsigned char *) parentPath.get(), '\\')
|
|
|
|
- (const unsigned char *) parentPath.get());
|
2000-09-01 11:36:55 +04:00
|
|
|
if (offset < 0)
|
1999-12-04 04:13:44 +03:00
|
|
|
return NS_ERROR_FILE_UNRECOGNIZED_PATH;
|
|
|
|
|
|
|
|
parentPath.Truncate(offset);
|
|
|
|
|
2000-01-25 00:28:28 +03:00
|
|
|
nsCOMPtr<nsILocalFile> localFile;
|
2002-04-27 09:33:09 +04:00
|
|
|
nsresult rv = NS_NewNativeLocalFile(parentPath, mFollowSymlinks, getter_AddRefs(localFile));
|
1999-12-04 04:13:44 +03:00
|
|
|
|
2000-01-25 00:28:28 +03:00
|
|
|
if(NS_SUCCEEDED(rv) && localFile)
|
|
|
|
{
|
2002-04-27 09:33:09 +04:00
|
|
|
return CallQueryInterface(localFile, aParent);
|
2000-01-25 00:28:28 +03:00
|
|
|
}
|
|
|
|
return rv;
|
1999-12-04 04:13:44 +03:00
|
|
|
}
|
2000-01-25 00:28:28 +03:00
|
|
|
|
1999-12-04 04:13:44 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLocalFile::Exists(PRBool *_retval)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG(_retval);
|
2000-01-25 00:28:28 +03:00
|
|
|
|
2000-04-25 06:39:42 +04:00
|
|
|
MakeDirty();
|
1999-12-04 04:13:44 +03:00
|
|
|
nsresult rv = ResolveAndStat( PR_TRUE );
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
*_retval = PR_TRUE;
|
|
|
|
else
|
|
|
|
*_retval = PR_FALSE;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLocalFile::IsWritable(PRBool *_retval)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG(_retval);
|
|
|
|
*_retval = PR_FALSE;
|
|
|
|
|
|
|
|
nsresult rv = ResolveAndStat(PR_TRUE);
|
|
|
|
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
2001-03-02 12:26:57 +03:00
|
|
|
const char *workingFilePath = mWorkingPath.get();
|
2000-02-22 00:51:47 +03:00
|
|
|
DWORD word = GetFileAttributes(workingFilePath);
|
|
|
|
|
|
|
|
*_retval = !((word & FILE_ATTRIBUTE_READONLY) != 0);
|
1999-12-04 04:13:44 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLocalFile::IsReadable(PRBool *_retval)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG(_retval);
|
|
|
|
*_retval = PR_FALSE;
|
|
|
|
|
|
|
|
nsresult rv = ResolveAndStat( PR_TRUE );
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
*_retval = PR_TRUE;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLocalFile::IsExecutable(PRBool *_retval)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG(_retval);
|
|
|
|
*_retval = PR_FALSE;
|
|
|
|
|
|
|
|
|
|
|
|
nsresult rv = ResolveAndStat( PR_TRUE );
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
2002-04-27 09:33:09 +04:00
|
|
|
nsCAutoString path;
|
1999-12-04 04:13:44 +03:00
|
|
|
PRBool symLink;
|
|
|
|
|
|
|
|
rv = IsSymlink(&symLink);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
if (symLink)
|
2002-04-27 09:33:09 +04:00
|
|
|
GetNativeTarget(path);
|
1999-12-04 04:13:44 +03:00
|
|
|
else
|
2002-04-27 09:33:09 +04:00
|
|
|
GetNativePath(path);
|
1999-12-04 04:13:44 +03:00
|
|
|
|
2001-05-23 10:03:59 +04:00
|
|
|
// Get extension.
|
2002-04-27 09:33:09 +04:00
|
|
|
char* ext = ::strrchr( path.get(), '.' );
|
2001-05-23 10:03:59 +04:00
|
|
|
if ( ext ) {
|
|
|
|
// Convert extension to lower case.
|
|
|
|
for( char *p = ext; *p; p++ ) {
|
|
|
|
if ( ::isupper( *p ) ) {
|
|
|
|
*p = ::tolower( *p );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Search for any of the set of executable extensions.
|
|
|
|
const char * const executableExts[] = { ".exe",
|
|
|
|
".bat",
|
|
|
|
".com",
|
|
|
|
".pif",
|
|
|
|
".cmd",
|
|
|
|
".js",
|
|
|
|
".vbs",
|
|
|
|
".lnk",
|
|
|
|
".reg",
|
|
|
|
".wsf",
|
|
|
|
0 };
|
|
|
|
for ( int i = 0; executableExts[i]; i++ ) {
|
|
|
|
if ( ::strcmp( executableExts[i], ext ) == 0 ) {
|
|
|
|
// Found a match. Set result and quit.
|
|
|
|
*_retval = PR_TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
1999-12-04 04:13:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLocalFile::IsDirectory(PRBool *_retval)
|
|
|
|
{
|
2003-08-25 22:28:10 +04:00
|
|
|
NS_PRECONDITION(_retval, "null pointer");
|
1999-12-04 04:13:44 +03:00
|
|
|
|
|
|
|
nsresult rv = ResolveAndStat(PR_TRUE);
|
2003-08-25 22:28:10 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
*_retval = PR_FALSE;
|
1999-12-04 04:13:44 +03:00
|
|
|
return rv;
|
2003-08-25 22:28:10 +04:00
|
|
|
}
|
2000-02-22 00:51:47 +03:00
|
|
|
|
|
|
|
*_retval = (mFileInfo64.type == PR_FILE_DIRECTORY);
|
1999-12-04 04:13:44 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLocalFile::IsFile(PRBool *_retval)
|
|
|
|
{
|
2000-02-22 00:51:47 +03:00
|
|
|
NS_ENSURE_ARG(_retval);
|
|
|
|
*_retval = PR_FALSE;
|
|
|
|
|
|
|
|
nsresult rv = ResolveAndStat(PR_TRUE);
|
|
|
|
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
1999-12-04 04:13:44 +03:00
|
|
|
|
2000-02-22 00:51:47 +03:00
|
|
|
*_retval = (mFileInfo64.type == PR_FILE_FILE);
|
2000-01-27 04:52:02 +03:00
|
|
|
return rv;
|
1999-12-04 04:13:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLocalFile::IsHidden(PRBool *_retval)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG(_retval);
|
|
|
|
*_retval = PR_FALSE;
|
|
|
|
|
|
|
|
nsresult rv = ResolveAndStat(PR_TRUE);
|
|
|
|
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
2002-11-27 00:37:58 +03:00
|
|
|
|
|
|
|
DWORD word;
|
|
|
|
if (mFollowSymlinks)
|
|
|
|
{
|
|
|
|
const char *resolvedFilePath = mResolvedPath.get();
|
|
|
|
word = GetFileAttributes(resolvedFilePath);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const char *workingFilePath = mWorkingPath.get();
|
|
|
|
word = GetFileAttributes(workingFilePath);
|
|
|
|
}
|
2000-02-22 00:51:47 +03:00
|
|
|
|
|
|
|
*_retval = ((word & FILE_ATTRIBUTE_HIDDEN) != 0);
|
1999-12-04 04:13:44 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLocalFile::IsSymlink(PRBool *_retval)
|
|
|
|
{
|
2003-08-25 22:28:10 +04:00
|
|
|
NS_PRECONDITION(_retval, "null pointer");
|
1999-12-04 04:13:44 +03:00
|
|
|
|
2003-08-25 22:28:10 +04:00
|
|
|
PRUint32 len = mWorkingPath.Length();
|
|
|
|
if (len < 4)
|
|
|
|
*_retval = PR_FALSE;
|
|
|
|
else {
|
|
|
|
const char* leaf = mWorkingPath.get() + len - 4;
|
|
|
|
*_retval = (strnicmp(leaf, ".lnk", 4) == 0);
|
1999-12-04 04:13:44 +03:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLocalFile::IsSpecial(PRBool *_retval)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG(_retval);
|
|
|
|
*_retval = PR_FALSE;
|
|
|
|
|
|
|
|
nsresult rv = ResolveAndStat(PR_TRUE);
|
|
|
|
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
2001-03-02 12:26:57 +03:00
|
|
|
const char *workingFilePath = mWorkingPath.get();
|
2000-02-22 00:51:47 +03:00
|
|
|
DWORD word = GetFileAttributes(workingFilePath);
|
|
|
|
|
|
|
|
*_retval = ((word & FILE_ATTRIBUTE_SYSTEM) != 0);
|
1999-12-04 04:13:44 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLocalFile::Equals(nsIFile *inFile, PRBool *_retval)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG(inFile);
|
|
|
|
NS_ENSURE_ARG(_retval);
|
|
|
|
|
2002-04-27 09:33:09 +04:00
|
|
|
nsCAutoString inFilePath;
|
|
|
|
inFile->GetNativePath(inFilePath);
|
1999-12-04 04:13:44 +03:00
|
|
|
|
2002-04-27 09:33:09 +04:00
|
|
|
*_retval = inFilePath.Equals(mWorkingPath);
|
1999-12-04 04:13:44 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2000-01-29 02:53:59 +03:00
|
|
|
nsLocalFile::Contains(nsIFile *inFile, PRBool recur, PRBool *_retval)
|
1999-12-04 04:13:44 +03:00
|
|
|
{
|
2000-01-29 02:53:59 +03:00
|
|
|
*_retval = PR_FALSE;
|
2000-01-29 03:00:21 +03:00
|
|
|
|
2002-04-27 09:33:09 +04:00
|
|
|
nsCAutoString myFilePath;
|
|
|
|
if ( NS_FAILED(GetNativeTarget(myFilePath)))
|
|
|
|
GetNativePath(myFilePath);
|
2000-01-29 02:53:59 +03:00
|
|
|
|
2002-04-27 09:33:09 +04:00
|
|
|
PRInt32 myFilePathLen = myFilePath.Length();
|
2000-01-29 03:00:21 +03:00
|
|
|
|
2002-04-27 09:33:09 +04:00
|
|
|
nsCAutoString inFilePath;
|
|
|
|
if ( NS_FAILED(inFile->GetNativeTarget(inFilePath)))
|
|
|
|
inFile->GetNativePath(inFilePath);
|
2000-01-29 03:00:21 +03:00
|
|
|
|
2003-04-18 09:43:41 +04:00
|
|
|
if ( strnicmp( myFilePath.get(), inFilePath.get(), myFilePathLen) == 0)
|
1999-12-08 02:31:25 +03:00
|
|
|
{
|
2000-01-25 00:28:28 +03:00
|
|
|
// now make sure that the |inFile|'s path has a trailing
|
|
|
|
// separator.
|
1999-12-08 02:31:25 +03:00
|
|
|
|
2000-01-29 03:00:21 +03:00
|
|
|
if (inFilePath[myFilePathLen] == '\\')
|
1999-12-08 02:31:25 +03:00
|
|
|
{
|
2000-01-25 00:28:28 +03:00
|
|
|
*_retval = PR_TRUE;
|
1999-12-08 02:31:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2000-01-25 00:28:28 +03:00
|
|
|
return NS_OK;
|
1999-12-04 04:13:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2002-04-27 09:33:09 +04:00
|
|
|
nsLocalFile::GetNativeTarget(nsACString &_retval)
|
1999-12-04 04:13:44 +03:00
|
|
|
{
|
2002-04-27 09:33:09 +04:00
|
|
|
_retval.Truncate();
|
1999-12-08 02:31:25 +03:00
|
|
|
#if STRICT_FAKE_SYMLINKS
|
1999-12-04 04:13:44 +03:00
|
|
|
PRBool symLink;
|
|
|
|
|
|
|
|
nsresult rv = IsSymlink(&symLink);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
if (!symLink)
|
|
|
|
{
|
|
|
|
return NS_ERROR_FILE_INVALID_PATH;
|
|
|
|
}
|
1999-12-08 02:31:25 +03:00
|
|
|
#endif
|
1999-12-04 04:13:44 +03:00
|
|
|
ResolveAndStat(PR_TRUE);
|
|
|
|
|
2002-04-27 09:33:09 +04:00
|
|
|
_retval = mResolvedPath;
|
1999-12-04 04:13:44 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-07-13 03:31:31 +04:00
|
|
|
/* attribute PRBool followLinks; */
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLocalFile::GetFollowLinks(PRBool *aFollowLinks)
|
|
|
|
{
|
|
|
|
*aFollowLinks = mFollowSymlinks;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLocalFile::SetFollowLinks(PRBool aFollowLinks)
|
|
|
|
{
|
|
|
|
MakeDirty();
|
|
|
|
mFollowSymlinks = aFollowLinks;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-12-04 04:13:44 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLocalFile::GetDirectoryEntries(nsISimpleEnumerator * *entries)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
*entries = nsnull;
|
|
|
|
|
|
|
|
PRBool isDir;
|
|
|
|
rv = IsDirectory(&isDir);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
if (!isDir)
|
|
|
|
return NS_ERROR_FILE_NOT_DIRECTORY;
|
|
|
|
|
|
|
|
nsDirEnumerator* dirEnum = new nsDirEnumerator();
|
|
|
|
if (dirEnum == nsnull)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
NS_ADDREF(dirEnum);
|
|
|
|
rv = dirEnum->Init(this);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
{
|
|
|
|
NS_RELEASE(dirEnum);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
*entries = dirEnum;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2000-07-17 19:03:05 +04:00
|
|
|
NS_IMETHODIMP
|
2002-04-27 09:33:09 +04:00
|
|
|
nsLocalFile::GetPersistentDescriptor(nsACString &aPersistentDescriptor)
|
2000-07-17 19:03:05 +04:00
|
|
|
{
|
2002-04-27 09:33:09 +04:00
|
|
|
return GetNativePath(aPersistentDescriptor);
|
2000-07-17 19:03:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2002-04-27 09:33:09 +04:00
|
|
|
nsLocalFile::SetPersistentDescriptor(const nsACString &aPersistentDescriptor)
|
2000-07-17 19:03:05 +04:00
|
|
|
{
|
2002-04-27 09:33:09 +04:00
|
|
|
return InitWithNativePath(aPersistentDescriptor);
|
2000-07-17 19:03:05 +04:00
|
|
|
}
|
|
|
|
|
2001-01-30 08:02:48 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLocalFile::Reveal()
|
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
PRBool isDirectory = PR_FALSE;
|
2002-04-27 09:33:09 +04:00
|
|
|
nsCAutoString path;
|
2002-05-08 03:07:19 +04:00
|
|
|
nsAutoString unicodePath;
|
2001-01-30 08:02:48 +03:00
|
|
|
|
|
|
|
IsDirectory(&isDirectory);
|
|
|
|
if (isDirectory)
|
|
|
|
{
|
2002-04-27 09:33:09 +04:00
|
|
|
GetNativePath(path);
|
2001-01-30 08:02:48 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIFile> parent;
|
|
|
|
GetParent(getter_AddRefs(parent));
|
|
|
|
if (parent)
|
2002-02-20 08:41:34 +03:00
|
|
|
{
|
2002-04-27 09:33:09 +04:00
|
|
|
parent->GetNativePath(path);
|
2002-05-08 03:07:19 +04:00
|
|
|
parent->GetPath(unicodePath);
|
2002-02-20 08:41:34 +03:00
|
|
|
}
|
2001-01-30 08:02:48 +03:00
|
|
|
}
|
|
|
|
|
2002-02-20 08:41:34 +03:00
|
|
|
// Remember the current fg window.
|
|
|
|
HWND origWin, fgWin;
|
|
|
|
origWin = fgWin = ::GetForegroundWindow();
|
|
|
|
|
2001-01-30 08:02:48 +03:00
|
|
|
// use the app registry name to launch a shell execute....
|
2002-04-27 09:33:09 +04:00
|
|
|
LONG r = (LONG) ::ShellExecute( NULL, "open", path.get(), NULL, NULL, SW_SHOWNORMAL);
|
2001-01-30 08:02:48 +03:00
|
|
|
if (r < 32)
|
2002-02-20 08:41:34 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
// If this is a directory, then we don't need to select a file.
|
|
|
|
if (isDirectory)
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
// Resources we may need to free when done.
|
|
|
|
IShellFolder *desktopFolder = 0;
|
|
|
|
IMalloc *shellMalloc = 0;
|
|
|
|
IShellFolder *folder = 0;
|
|
|
|
LPITEMIDLIST folder_pidl = 0;
|
|
|
|
LPITEMIDLIST file_pidl = 0;
|
|
|
|
LPITEMIDLIST win95_file_pidl = 0;
|
|
|
|
HMODULE shell32 = 0;
|
|
|
|
|
|
|
|
// We break out of this do/while non-loop at any point where we have to give up.
|
|
|
|
do {
|
|
|
|
// Wait for the window to open. We wait a maximum of 2 seconds.
|
|
|
|
// If we get the wrong window, that will be dealt with below.
|
|
|
|
for (int iter = 10; iter; iter--)
|
|
|
|
{
|
|
|
|
fgWin = ::GetForegroundWindow();
|
|
|
|
if (fgWin != origWin)
|
|
|
|
break; // for loopo
|
|
|
|
::Sleep(200);
|
|
|
|
}
|
|
|
|
// If we failed to locate the new window, give up.
|
|
|
|
if (origWin == fgWin)
|
|
|
|
break; // do/while
|
|
|
|
|
|
|
|
// Now we have the explorer window. We need to send it the "select item"
|
|
|
|
// message (which isn't trivial, so buckly your seat belt)...
|
|
|
|
|
|
|
|
// We need the explorer's process id.
|
|
|
|
DWORD pid = 0;
|
|
|
|
::GetWindowThreadProcessId(fgWin, &pid);
|
|
|
|
|
|
|
|
// Get desktop folder.
|
|
|
|
HRESULT rc = ::SHGetDesktopFolder(&desktopFolder);
|
|
|
|
if (!desktopFolder)
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Get IMalloc interface to use for shell pidls.
|
|
|
|
rc = ::SHGetMalloc(&shellMalloc);
|
|
|
|
if (!shellMalloc)
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Convert folder path to pidl. This requires the Unicode path name.
|
|
|
|
// It returns a pidl that must be freed via shellMalloc->Free.
|
|
|
|
ULONG eaten = 0;
|
|
|
|
rc = desktopFolder->ParseDisplayName( 0,
|
|
|
|
0,
|
|
|
|
(LPOLESTR)unicodePath.get(),
|
|
|
|
&eaten,
|
|
|
|
&folder_pidl,
|
|
|
|
0 );
|
|
|
|
if (!folder_pidl)
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Now get IShellFolder interface for the folder we opened.
|
|
|
|
rc = desktopFolder->BindToObject( folder_pidl,
|
|
|
|
0,
|
|
|
|
IID_IShellFolder,
|
|
|
|
(void**)&folder );
|
|
|
|
if (!folder)
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Now get file name pidl from that folder.
|
2002-05-08 03:07:19 +04:00
|
|
|
nsAutoString unicodeLeaf;
|
|
|
|
if (NS_FAILED(GetLeafName(unicodeLeaf)))
|
2002-02-20 08:41:34 +03:00
|
|
|
break;
|
|
|
|
rc = folder->ParseDisplayName( 0,
|
|
|
|
0,
|
|
|
|
(LPOLESTR)unicodeLeaf.get(),
|
|
|
|
&eaten,
|
|
|
|
&file_pidl,
|
|
|
|
0 );
|
|
|
|
if (!file_pidl)
|
|
|
|
break;
|
|
|
|
|
|
|
|
// We need the module handle for shell32.dll.
|
|
|
|
shell32 = ::GetModuleHandle("shell32.dll");
|
|
|
|
if (!shell32)
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Allocate shared memory copy of pidl. This uses the undocumented "SHAllocShared"
|
|
|
|
// function. Note that it is freed automatically after the ::SendMessage so we
|
|
|
|
// don't have to free it.
|
|
|
|
static HANDLE(WINAPI*SHAllocShared)(LPVOID,ULONG,DWORD) = (HANDLE(WINAPI*)(LPVOID,ULONG,DWORD))::GetProcAddress(shell32, (LPCTSTR)520);
|
|
|
|
HANDLE pidlHandle = 0;
|
|
|
|
if (SHAllocShared)
|
|
|
|
{
|
|
|
|
// We need the size of the pidl, which we get via another undocumented
|
|
|
|
// API: "ILGetSize".
|
|
|
|
UINT (WINAPI*ILGetSize)(LPCITEMIDLIST) = (UINT(WINAPI*)(LPCITEMIDLIST))::GetProcAddress(shell32, (LPCTSTR)152);
|
|
|
|
if (!ILGetSize)
|
|
|
|
break;
|
|
|
|
pidlHandle = SHAllocShared((void*)(ITEMIDLIST*)file_pidl,
|
|
|
|
ILGetSize(file_pidl),
|
|
|
|
pid);
|
|
|
|
if (!pidlHandle)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// On Win95, there is no SHAllocShared. Instead, we clone the file's pidl in
|
|
|
|
// the shell's global heap (via ILGlobalClone) and pass that.
|
|
|
|
LPITEMIDLIST(WINAPI*ILGlobalClone)(LPCITEMIDLIST) = (LPITEMIDLIST(WINAPI*)(LPCITEMIDLIST))::GetProcAddress(shell32, (LPCTSTR)20);
|
|
|
|
if (!ILGlobalClone)
|
|
|
|
break;
|
|
|
|
win95_file_pidl = ILGlobalClone(file_pidl);
|
|
|
|
if (!win95_file_pidl)
|
|
|
|
break;
|
|
|
|
// Arrange so that this pidl is passed on the ::SendMessage.
|
|
|
|
pidlHandle = win95_file_pidl;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send message to select this file.
|
|
|
|
::SendMessage(fgWin,
|
|
|
|
WM_USER+5,
|
|
|
|
SVSI_SELECT | SVSI_DESELECTOTHERS | SVSI_ENSUREVISIBLE,
|
|
|
|
(LPARAM)pidlHandle );
|
|
|
|
} while ( PR_FALSE );
|
|
|
|
|
|
|
|
// Clean up (freeing stuff as needed, in reverse order).
|
|
|
|
if (win95_file_pidl)
|
|
|
|
{
|
|
|
|
// We need to free this using ILGlobalFree, another undocumented API.
|
|
|
|
static void (WINAPI*ILGlobalFree)(LPCITEMIDLIST) = (void(WINAPI*)(LPCITEMIDLIST))::GetProcAddress(shell32,(LPCTSTR)156);
|
|
|
|
if (ILGlobalFree)
|
|
|
|
ILGlobalFree(win95_file_pidl);
|
|
|
|
}
|
|
|
|
if (file_pidl)
|
|
|
|
shellMalloc->Free(file_pidl);
|
|
|
|
if (folder_pidl)
|
|
|
|
shellMalloc->Free(folder_pidl);
|
|
|
|
if (folder)
|
|
|
|
folder->Release();
|
|
|
|
if (shellMalloc)
|
|
|
|
shellMalloc->Release();
|
|
|
|
if (desktopFolder)
|
|
|
|
desktopFolder->Release();
|
2001-01-30 08:02:48 +03:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLocalFile::Launch()
|
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
2002-04-27 09:33:09 +04:00
|
|
|
const nsCString &path = mWorkingPath;
|
2001-01-30 08:02:48 +03:00
|
|
|
|
|
|
|
// use the app registry name to launch a shell execute....
|
2002-04-27 09:33:09 +04:00
|
|
|
LONG r = (LONG) ::ShellExecute( NULL, NULL, path.get(), NULL, NULL, SW_SHOWNORMAL);
|
2002-03-06 04:11:34 +03:00
|
|
|
|
|
|
|
// if the file has no association, we launch windows' "what do you want to do" dialog
|
|
|
|
if (r == SE_ERR_NOASSOC) {
|
|
|
|
nsCAutoString shellArg;
|
2002-04-27 09:33:09 +04:00
|
|
|
shellArg.Assign(NS_LITERAL_CSTRING("shell32.dll,OpenAs_RunDLL ") + path);
|
2002-03-06 04:11:34 +03:00
|
|
|
r = (LONG) ::ShellExecute(NULL, NULL, "RUNDLL32.EXE", shellArg.get(),
|
|
|
|
NULL, SW_SHOWNORMAL);
|
|
|
|
}
|
2001-01-30 08:02:48 +03:00
|
|
|
if (r < 32)
|
|
|
|
rv = NS_ERROR_FAILURE;
|
|
|
|
else
|
2002-03-06 04:11:34 +03:00
|
|
|
rv = NS_OK;
|
2001-01-30 08:02:48 +03:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
1999-12-04 04:13:44 +03:00
|
|
|
|
2000-01-29 02:35:55 +03:00
|
|
|
nsresult
|
2002-04-27 09:33:09 +04:00
|
|
|
NS_NewNativeLocalFile(const nsACString &path, PRBool followLinks, nsILocalFile* *result)
|
2000-01-25 00:28:28 +03:00
|
|
|
{
|
|
|
|
nsLocalFile* file = new nsLocalFile();
|
|
|
|
if (file == nsnull)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
NS_ADDREF(file);
|
|
|
|
|
2000-07-13 03:31:31 +04:00
|
|
|
file->SetFollowLinks(followLinks);
|
|
|
|
|
2002-04-27 09:33:09 +04:00
|
|
|
if (!path.IsEmpty()) {
|
|
|
|
nsresult rv = file->InitWithNativePath(path);
|
2000-07-17 19:03:05 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_RELEASE(file);
|
|
|
|
return rv;
|
|
|
|
}
|
2000-01-25 00:28:28 +03:00
|
|
|
}
|
2000-07-13 03:31:31 +04:00
|
|
|
|
2000-01-25 00:28:28 +03:00
|
|
|
*result = file;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2002-04-27 09:33:09 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// UCS2 interface
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
nsresult
|
2002-05-08 03:07:19 +04:00
|
|
|
nsLocalFile::InitWithPath(const nsAString &filePath)
|
2002-02-01 00:55:01 +03:00
|
|
|
{
|
2002-10-06 04:29:46 +04:00
|
|
|
nsCAutoString tmp;
|
|
|
|
nsresult rv = NS_CopyUnicodeToNative(filePath, tmp);
|
2002-02-01 00:55:01 +03:00
|
|
|
if (NS_SUCCEEDED(rv))
|
2002-04-27 09:33:09 +04:00
|
|
|
return InitWithNativePath(tmp);
|
2002-02-01 00:55:01 +03:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2002-04-27 09:33:09 +04:00
|
|
|
nsresult
|
2002-05-08 03:07:19 +04:00
|
|
|
nsLocalFile::Append(const nsAString &node)
|
2002-02-01 00:55:01 +03:00
|
|
|
{
|
2002-10-06 04:29:46 +04:00
|
|
|
nsCAutoString tmp;
|
|
|
|
nsresult rv = NS_CopyUnicodeToNative(node, tmp);
|
2002-02-01 00:55:01 +03:00
|
|
|
if (NS_SUCCEEDED(rv))
|
2002-04-27 09:33:09 +04:00
|
|
|
return AppendNative(tmp);
|
2002-02-01 00:55:01 +03:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2002-04-27 09:33:09 +04:00
|
|
|
nsresult
|
2002-05-08 03:07:19 +04:00
|
|
|
nsLocalFile::AppendRelativePath(const nsAString &node)
|
2002-02-01 00:55:01 +03:00
|
|
|
{
|
2002-10-06 04:29:46 +04:00
|
|
|
nsCAutoString tmp;
|
|
|
|
nsresult rv = NS_CopyUnicodeToNative(node, tmp);
|
2002-02-01 00:55:01 +03:00
|
|
|
if (NS_SUCCEEDED(rv))
|
2002-04-27 09:33:09 +04:00
|
|
|
return AppendRelativeNativePath(tmp);
|
2002-02-01 00:55:01 +03:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2002-04-27 09:33:09 +04:00
|
|
|
nsresult
|
2002-05-08 03:07:19 +04:00
|
|
|
nsLocalFile::GetLeafName(nsAString &aLeafName)
|
2002-02-01 00:55:01 +03:00
|
|
|
{
|
2002-04-27 09:33:09 +04:00
|
|
|
nsCAutoString tmp;
|
|
|
|
nsresult rv = GetNativeLeafName(tmp);
|
2002-10-06 04:29:46 +04:00
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
rv = NS_CopyNativeToUnicode(tmp, aLeafName);
|
2002-02-01 00:55:01 +03:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2002-04-27 09:33:09 +04:00
|
|
|
nsresult
|
2002-05-08 03:07:19 +04:00
|
|
|
nsLocalFile::SetLeafName(const nsAString &aLeafName)
|
2002-02-01 00:55:01 +03:00
|
|
|
{
|
2002-10-06 04:29:46 +04:00
|
|
|
nsCAutoString tmp;
|
|
|
|
nsresult rv = NS_CopyUnicodeToNative(aLeafName, tmp);
|
2002-02-01 00:55:01 +03:00
|
|
|
if (NS_SUCCEEDED(rv))
|
2002-04-27 09:33:09 +04:00
|
|
|
return SetNativeLeafName(tmp);
|
2002-02-01 00:55:01 +03:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2002-04-27 09:33:09 +04:00
|
|
|
nsresult
|
2002-05-08 03:07:19 +04:00
|
|
|
nsLocalFile::GetPath(nsAString &_retval)
|
2002-02-01 00:55:01 +03:00
|
|
|
{
|
2002-10-06 04:29:46 +04:00
|
|
|
return NS_CopyNativeToUnicode(mWorkingPath, _retval);
|
2002-02-01 00:55:01 +03:00
|
|
|
}
|
|
|
|
|
2002-04-27 09:33:09 +04:00
|
|
|
nsresult
|
2002-05-08 03:07:19 +04:00
|
|
|
nsLocalFile::CopyTo(nsIFile *newParentDir, const nsAString &newName)
|
2002-02-01 00:55:01 +03:00
|
|
|
{
|
2002-05-08 03:07:19 +04:00
|
|
|
if (newName.IsEmpty())
|
2002-04-27 09:33:09 +04:00
|
|
|
return CopyToNative(newParentDir, nsCString());
|
2002-02-01 00:55:01 +03:00
|
|
|
|
2002-10-06 04:29:46 +04:00
|
|
|
nsCAutoString tmp;
|
|
|
|
nsresult rv = NS_CopyUnicodeToNative(newName, tmp);
|
2002-02-01 00:55:01 +03:00
|
|
|
if (NS_SUCCEEDED(rv))
|
2002-04-27 09:33:09 +04:00
|
|
|
return CopyToNative(newParentDir, tmp);
|
2002-02-01 00:55:01 +03:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2002-04-27 09:33:09 +04:00
|
|
|
nsresult
|
2002-05-08 03:07:19 +04:00
|
|
|
nsLocalFile::CopyToFollowingLinks(nsIFile *newParentDir, const nsAString &newName)
|
2002-02-01 00:55:01 +03:00
|
|
|
{
|
2002-05-08 03:07:19 +04:00
|
|
|
if (newName.IsEmpty())
|
2002-04-27 09:33:09 +04:00
|
|
|
return CopyToFollowingLinksNative(newParentDir, nsCString());
|
2002-02-01 00:55:01 +03:00
|
|
|
|
2002-10-06 04:29:46 +04:00
|
|
|
nsCAutoString tmp;
|
|
|
|
nsresult rv = NS_CopyUnicodeToNative(newName, tmp);
|
2002-02-01 00:55:01 +03:00
|
|
|
if (NS_SUCCEEDED(rv))
|
2002-04-27 09:33:09 +04:00
|
|
|
return CopyToFollowingLinksNative(newParentDir, tmp);
|
2002-02-01 00:55:01 +03:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2002-04-27 09:33:09 +04:00
|
|
|
nsresult
|
2002-05-08 03:07:19 +04:00
|
|
|
nsLocalFile::MoveTo(nsIFile *newParentDir, const nsAString &newName)
|
2002-02-01 00:55:01 +03:00
|
|
|
{
|
2002-05-08 03:07:19 +04:00
|
|
|
if (newName.IsEmpty())
|
2002-04-27 09:33:09 +04:00
|
|
|
return MoveToNative(newParentDir, nsCString());
|
2002-02-01 00:55:01 +03:00
|
|
|
|
2002-10-06 04:29:46 +04:00
|
|
|
nsCAutoString tmp;
|
|
|
|
nsresult rv = NS_CopyUnicodeToNative(newName, tmp);
|
2002-02-01 00:55:01 +03:00
|
|
|
if (NS_SUCCEEDED(rv))
|
2002-04-27 09:33:09 +04:00
|
|
|
return MoveToNative(newParentDir, tmp);
|
2002-02-01 00:55:01 +03:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2002-04-27 09:33:09 +04:00
|
|
|
nsresult
|
2002-05-08 03:07:19 +04:00
|
|
|
nsLocalFile::GetTarget(nsAString &_retval)
|
2002-02-01 00:55:01 +03:00
|
|
|
{
|
2002-04-27 09:33:09 +04:00
|
|
|
nsCAutoString tmp;
|
|
|
|
nsresult rv = GetNativeTarget(tmp);
|
2002-10-06 04:29:46 +04:00
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
rv = NS_CopyNativeToUnicode(tmp, _retval);
|
2002-02-01 00:55:01 +03:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2002-05-08 03:07:19 +04:00
|
|
|
NS_NewLocalFile(const nsAString &path, PRBool followLinks, nsILocalFile* *result)
|
2002-02-01 00:55:01 +03:00
|
|
|
{
|
2003-07-29 01:02:45 +04:00
|
|
|
nsCAutoString buf;
|
|
|
|
nsresult rv = NS_CopyUnicodeToNative(path, buf);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
*result = nsnull;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
return NS_NewNativeLocalFile(buf, followLinks, result);
|
2002-02-01 00:55:01 +03:00
|
|
|
}
|
2000-01-25 00:28:28 +03:00
|
|
|
|
2002-04-27 09:33:09 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsLocalFile <static members>
|
|
|
|
//-----------------------------------------------------------------------------
|
2000-01-25 00:28:28 +03:00
|
|
|
|
2002-04-27 09:33:09 +04:00
|
|
|
void
|
|
|
|
nsLocalFile::GlobalInit()
|
|
|
|
{
|
|
|
|
nsresult rv = NS_CreateShortcutResolver();
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "Shortcut resolver could not be created");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsLocalFile::GlobalShutdown()
|
|
|
|
{
|
|
|
|
NS_DestroyShortcutResolver();
|
|
|
|
}
|