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
|
|
|
|
|
|
|
#include "nsLocalFileWin.h"
|
|
|
|
|
|
|
|
#include "nsISimpleEnumerator.h"
|
|
|
|
#include "nsIComponentManager.h"
|
|
|
|
#include "prtypes.h"
|
|
|
|
#include "prio.h"
|
|
|
|
|
2001-06-22 02:02:47 +04:00
|
|
|
#include "nsEscape.h"
|
|
|
|
#include "nsXPIDLString.h"
|
|
|
|
|
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
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2000-01-27 00:55:13 +03:00
|
|
|
|
1999-12-04 04:13:44 +03:00
|
|
|
class nsDirEnumerator : public nsISimpleEnumerator
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
nsDirEnumerator() : mDir(nsnull)
|
|
|
|
{
|
|
|
|
NS_INIT_REFCNT();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult Init(nsILocalFile* parent)
|
|
|
|
{
|
|
|
|
char* filepath;
|
|
|
|
parent->GetTarget(&filepath);
|
|
|
|
|
|
|
|
if (filepath == nsnull)
|
|
|
|
{
|
|
|
|
parent->GetPath(&filepath);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (filepath == nsnull)
|
|
|
|
{
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
mDir = PR_OpenDir(filepath);
|
|
|
|
if (mDir == nsnull) // not a directory?
|
|
|
|
return NS_ERROR_FAILURE;
|
2000-01-28 09:17:06 +03:00
|
|
|
|
2000-06-03 13:46:12 +04:00
|
|
|
nsMemory::Free(filepath);
|
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;
|
|
|
|
|
2000-01-25 00:28:28 +03:00
|
|
|
rv = file->Append(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;
|
2000-03-22 04:37:52 +03: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);
|
|
|
|
|
|
|
|
mNext = null_nsCOMPtr();
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
nsLocalFile::nsLocalFile()
|
|
|
|
{
|
|
|
|
NS_INIT_REFCNT();
|
2000-01-25 00:28:28 +03:00
|
|
|
|
|
|
|
mPersistFile = nsnull;
|
|
|
|
mShellLink = nsnull;
|
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()
|
|
|
|
{
|
|
|
|
// Release the pointer to the IPersistFile interface.
|
|
|
|
if (mPersistFile)
|
|
|
|
mPersistFile->Release();
|
|
|
|
|
|
|
|
// Release the pointer to the IShellLink interface.
|
|
|
|
if(mShellLink)
|
|
|
|
mShellLink->Release();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* nsISupports interface implementation. */
|
2000-03-06 00:26:01 +03:00
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS2(nsLocalFile, nsILocalFile, nsIFile)
|
2000-01-25 00:28:28 +03:00
|
|
|
|
1999-12-04 04:13:44 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This function resets any cached information about the file.
|
|
|
|
void
|
|
|
|
nsLocalFile::MakeDirty()
|
|
|
|
{
|
2000-07-13 03:31:31 +04:00
|
|
|
mDirty = PR_TRUE;
|
1999-12-04 04:13:44 +03:00
|
|
|
}
|
|
|
|
|
2000-01-27 00:55:13 +03:00
|
|
|
|
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
|
|
|
#ifdef DOUGT_debug
|
|
|
|
printf("localfile - resolving symlink\n");
|
|
|
|
#endif
|
|
|
|
|
1999-12-04 04:13:44 +03:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
2000-07-13 03:31:31 +04:00
|
|
|
if (strstr(workingPath, ".lnk") == nsnull)
|
|
|
|
return NS_ERROR_FILE_INVALID_PATH;
|
|
|
|
|
1999-12-04 04:13:44 +03:00
|
|
|
if (mPersistFile == nsnull || mShellLink == nsnull)
|
2000-01-25 00:28:28 +03:00
|
|
|
{
|
|
|
|
HRESULT hres;
|
|
|
|
// FIX. This should be in a service.
|
|
|
|
// Get a pointer to the IShellLink interface.
|
|
|
|
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_FILE_INVALID_PATH;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-12-04 04:13:44 +03:00
|
|
|
|
|
|
|
// Get the native path for |this|
|
2000-06-03 13:46:12 +04:00
|
|
|
char* filePath = (char*) nsMemory::Clone( workingPath, strlen(workingPath)+1 );
|
1999-12-04 04:13:44 +03:00
|
|
|
|
|
|
|
if (filePath == nsnull)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
// Get the first slash.
|
|
|
|
char* slash = strchr(filePath, '\\');
|
|
|
|
|
|
|
|
if (slash == nsnull)
|
|
|
|
{
|
2000-01-25 00:28:28 +03:00
|
|
|
if (filePath[0] != nsnull && filePath[1] == ':' && filePath[2] == '\0')
|
1999-12-04 04:13:44 +03:00
|
|
|
{
|
|
|
|
// we have a drive letter and a colon (eg 'c:'
|
|
|
|
// this is resolve already
|
|
|
|
|
2000-07-29 12:13:39 +04:00
|
|
|
int filePathLen = strlen(filePath);
|
|
|
|
char* rp = (char*) nsMemory::Alloc( filePathLen + 2 );
|
|
|
|
if (!rp)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
memcpy( rp, filePath, filePathLen );
|
|
|
|
rp[filePathLen] = '\\';
|
|
|
|
rp[filePathLen+1] = 0;
|
|
|
|
*resolvedPath = rp;
|
1999-12-04 04:13:44 +03:00
|
|
|
|
2000-06-03 13:46:12 +04:00
|
|
|
nsMemory::Free(filePath);
|
1999-12-04 04:13:44 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2000-06-03 13:46:12 +04:00
|
|
|
nsMemory::Free(filePath);
|
1999-12-04 04:13:44 +03:00
|
|
|
return NS_ERROR_FILE_INVALID_PATH;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// We really cant have just a drive letter as
|
|
|
|
// a shortcut, so we will skip the first '\\'
|
|
|
|
slash = strchr(++slash, '\\');
|
|
|
|
|
|
|
|
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';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (resolveTerminal)
|
|
|
|
{
|
|
|
|
// this is our last time in this loop.
|
|
|
|
// set loop condition to false
|
|
|
|
|
|
|
|
resolveTerminal = PR_FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// something is wrong. we should not have
|
|
|
|
// both slash being null and resolveTerminal
|
|
|
|
// not set!
|
2000-06-03 13:46:12 +04:00
|
|
|
nsMemory::Free(filePath);
|
1999-12-04 04:13:44 +03:00
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
WORD wsz[MAX_PATH]; // TODO, Make this dynamically allocated.
|
|
|
|
|
|
|
|
// check to see the file is a shortcut by the magic .lnk extension.
|
|
|
|
size_t offset = strlen(filePath) - 4;
|
|
|
|
if ((offset > 0) && (strncmp( (filePath + offset), ".lnk", 4) == 0))
|
|
|
|
{
|
|
|
|
MultiByteToWideChar(CP_ACP, 0, filePath, -1, wsz, MAX_PATH);
|
1999-12-08 05:07:38 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
char linkStr[MAX_PATH];
|
|
|
|
strcpy(linkStr, filePath);
|
|
|
|
strcat(linkStr, ".lnk");
|
1999-12-04 04:13:44 +03:00
|
|
|
|
1999-12-08 05:07:38 +03:00
|
|
|
// Ensure that the string is Unicode.
|
|
|
|
MultiByteToWideChar(CP_ACP, 0, linkStr, -1, wsz, MAX_PATH);
|
|
|
|
}
|
1999-12-04 04:13:44 +03:00
|
|
|
|
1999-12-08 05:07:38 +03:00
|
|
|
HRESULT hres;
|
|
|
|
|
|
|
|
// see if we can Load the path.
|
|
|
|
hres = mPersistFile->Load(wsz, STGM_READ);
|
|
|
|
|
|
|
|
if (SUCCEEDED(hres))
|
|
|
|
{
|
|
|
|
// Resolve the link.
|
|
|
|
hres = mShellLink->Resolve(nsnull, SLR_NO_UI );
|
1999-12-04 04:13:44 +03:00
|
|
|
if (SUCCEEDED(hres))
|
1999-12-08 05:07:38 +03:00
|
|
|
{
|
|
|
|
WIN32_FIND_DATA wfd;
|
|
|
|
|
2000-06-03 13:46:12 +04:00
|
|
|
char *temp = (char*) nsMemory::Alloc( MAX_PATH );
|
1999-12-08 05:07:38 +03:00
|
|
|
if (temp == nsnull)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
// Get the path to the link target.
|
|
|
|
hres = mShellLink->GetPath( temp, MAX_PATH, &wfd, SLGP_UNCPRIORITY );
|
|
|
|
|
|
|
|
if (SUCCEEDED(hres))
|
|
|
|
{
|
|
|
|
// found a new path.
|
1999-12-04 04:13:44 +03:00
|
|
|
|
1999-12-08 05:07:38 +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.
|
1999-12-04 04:13:44 +03:00
|
|
|
|
1999-12-08 05:07:38 +03:00
|
|
|
struct stat st;
|
|
|
|
int statrv = stat(temp, &st);
|
|
|
|
|
|
|
|
if (0 == statrv && (_S_IFDIR & st.st_mode))
|
1999-12-04 04:13:44 +03:00
|
|
|
{
|
1999-12-08 05:07:38 +03:00
|
|
|
strcat(temp, "\\");
|
1999-12-04 04:13:44 +03:00
|
|
|
}
|
1999-12-08 05:07:38 +03:00
|
|
|
|
|
|
|
if (slash)
|
1999-12-04 04:13:44 +03:00
|
|
|
{
|
1999-12-08 05:07:38 +03:00
|
|
|
// save where we left off.
|
|
|
|
char *carot= (temp + strlen(temp) -1 );
|
|
|
|
|
|
|
|
// append all the stuff that we have not done.
|
|
|
|
strcat(temp, ++slash);
|
|
|
|
|
|
|
|
slash = carot;
|
1999-12-04 04:13:44 +03:00
|
|
|
}
|
1999-12-08 05:07:38 +03:00
|
|
|
|
2000-06-03 13:46:12 +04:00
|
|
|
nsMemory::Free(filePath);
|
1999-12-08 05:07:38 +03:00
|
|
|
filePath = temp;
|
|
|
|
|
1999-12-04 04:13:44 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2000-06-03 13:46:12 +04:00
|
|
|
nsMemory::Free(temp);
|
1999-12-04 04:13:44 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
1999-12-08 05:07:38 +03:00
|
|
|
// could not resolve shortcut. Return error;
|
2000-06-03 13:46:12 +04:00
|
|
|
nsMemory::Free(filePath);
|
1999-12-04 04:13:44 +03:00
|
|
|
return NS_ERROR_FILE_INVALID_PATH;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (slash)
|
|
|
|
{
|
|
|
|
*slash = '\\';
|
|
|
|
++slash;
|
|
|
|
slash = strchr(slash, '\\');
|
|
|
|
}
|
|
|
|
}
|
1999-12-08 02:31:25 +03:00
|
|
|
|
|
|
|
// kill any trailing seperator
|
|
|
|
char* temp = filePath;
|
|
|
|
int len = strlen(temp) - 1;
|
|
|
|
if(temp[len] == '\\')
|
|
|
|
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.
|
|
|
|
|
|
|
|
if (!mFollowSymlinks || pathLen < 4 || (strcmp(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
|
|
|
|
2000-07-13 03:31:31 +04:00
|
|
|
status = PR_GetFileInfo64(mResolvedPath, &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
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
1999-12-22 04:56:45 +03:00
|
|
|
nsLocalFile::Clone(nsIFile **file)
|
1999-12-04 04:13:44 +03:00
|
|
|
{
|
2000-01-25 00:28:28 +03:00
|
|
|
nsresult rv;
|
|
|
|
char * aFilePath;
|
|
|
|
GetPath(&aFilePath);
|
1999-12-22 04:56:45 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsILocalFile> localFile;
|
1999-12-04 04:13:44 +03:00
|
|
|
|
2000-07-13 03:31:31 +04:00
|
|
|
rv = NS_NewLocalFile(aFilePath, mFollowSymlinks, getter_AddRefs(localFile));
|
2000-06-03 13:46:12 +04:00
|
|
|
nsMemory::Free(aFilePath);
|
1999-12-22 04:56:45 +03:00
|
|
|
|
2000-01-25 00:28:28 +03:00
|
|
|
if (NS_SUCCEEDED(rv) && localFile)
|
|
|
|
{
|
|
|
|
return localFile->QueryInterface(NS_GET_IID(nsIFile), (void**)file);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
1999-12-04 04:13:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLocalFile::InitWithPath(const char *filePath)
|
|
|
|
{
|
|
|
|
MakeDirty();
|
|
|
|
NS_ENSURE_ARG(filePath);
|
|
|
|
|
|
|
|
char* nativeFilePath = nsnull;
|
|
|
|
|
|
|
|
// 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.
|
2000-05-17 04:29:59 +04:00
|
|
|
if (filePath[0] == 0)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
if ( (filePath[2] == 0) && (filePath[1] == ':') )
|
|
|
|
{
|
|
|
|
// C : //
|
2000-07-29 12:13:39 +04:00
|
|
|
nativeFilePath = (char*) nsMemory::Alloc( 4 );
|
|
|
|
if (!nativeFilePath)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
nativeFilePath[0] = filePath[0];
|
|
|
|
nativeFilePath[1] = ':';
|
2000-05-17 04:29:59 +04:00
|
|
|
nativeFilePath[2] = '\\';
|
2000-07-29 12:13:39 +04:00
|
|
|
nativeFilePath[3] = 0;
|
2000-05-17 04:29:59 +04:00
|
|
|
}
|
2000-07-29 12:13:39 +04:00
|
|
|
// XXX is this an 'else'? Otherwise 'nativeFilePath' could leak.
|
2000-05-17 04:29:59 +04:00
|
|
|
if ( ( (filePath[1] == ':') && (strchr(filePath, '/') == 0) ) || // normal windows path
|
1999-12-04 04:13:44 +03:00
|
|
|
( (filePath[0] == '\\') && (filePath[1] == '\\') ) ) // netwerk path
|
|
|
|
{
|
|
|
|
// This is a native path
|
2000-08-02 02:57:58 +04:00
|
|
|
if (nativeFilePath) nsCRT::free(nativeFilePath);
|
2000-06-03 13:46:12 +04:00
|
|
|
nativeFilePath = (char*) nsMemory::Clone( filePath, strlen(filePath)+1 );
|
1999-12-04 04:13:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (nativeFilePath == nsnull)
|
|
|
|
return NS_ERROR_FILE_UNRECOGNIZED_PATH;
|
|
|
|
|
|
|
|
// kill any trailing seperator
|
|
|
|
char* temp = nativeFilePath;
|
|
|
|
int len = strlen(temp) - 1;
|
2000-04-26 07:57:34 +04:00
|
|
|
// Is '\' second charactor of DBCS?
|
|
|
|
if(temp[len] == '\\' && !::IsDBCSLeadByte(temp[len-1]))
|
1999-12-04 04:13:44 +03:00
|
|
|
temp[len] = '\0';
|
|
|
|
|
2000-03-12 12:14:14 +03:00
|
|
|
mWorkingPath.Assign(nativeFilePath);
|
2000-06-03 13:46:12 +04:00
|
|
|
nsMemory::Free( nativeFilePath );
|
2000-07-13 03:31:31 +04:00
|
|
|
|
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
|
|
|
{
|
|
|
|
nsresult rv = ResolveAndStat(PR_TRUE);
|
|
|
|
if (NS_FAILED(rv) && rv != NS_ERROR_FILE_NOT_FOUND)
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
*_retval = PR_Open(mResolvedPath, flags, mode);
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
*_retval = fopen(mResolvedPath, mode);
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
// create nested directories to target
|
2001-03-02 12:26:57 +03:00
|
|
|
unsigned char* slash = _mbschr((const unsigned char*) mResolvedPath.get(), '\\');
|
1999-12-04 04:13:44 +03:00
|
|
|
// skip the first '\\'
|
|
|
|
++slash;
|
2000-04-26 07:57:34 +04:00
|
|
|
slash = _mbschr(slash, '\\');
|
1999-12-04 04:13:44 +03:00
|
|
|
|
|
|
|
while (slash)
|
|
|
|
{
|
|
|
|
*slash = '\0';
|
|
|
|
|
2000-06-24 05:50:53 +04:00
|
|
|
if (!CreateDirectoryA(mResolvedPath, NULL)) {
|
|
|
|
rv = ConvertWinError(GetLastError());
|
|
|
|
if (rv != NS_ERROR_FILE_ALREADY_EXISTS) return rv;
|
|
|
|
}
|
1999-12-04 04:13:44 +03:00
|
|
|
*slash = '\\';
|
|
|
|
++slash;
|
2000-04-26 07:57:34 +04:00
|
|
|
slash = _mbschr(slash, '\\');
|
1999-12-04 04:13:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (type == NORMAL_FILE_TYPE)
|
|
|
|
{
|
2000-06-24 05:50:53 +04:00
|
|
|
PRFileDesc* file = PR_Open(mResolvedPath, PR_RDONLY | PR_CREATE_FILE | PR_APPEND | PR_EXCL, attributes);
|
|
|
|
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)
|
|
|
|
{
|
2000-06-24 05:50:53 +04:00
|
|
|
if (!CreateDirectoryA(mResolvedPath, NULL))
|
|
|
|
return ConvertWinError(GetLastError());
|
|
|
|
else
|
|
|
|
return NS_OK;
|
1999-12-04 04:13:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_ERROR_FILE_UNKNOWN_TYPE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2000-01-25 00:28:28 +03:00
|
|
|
nsLocalFile::Append(const char *node)
|
1999-12-04 04:13:44 +03:00
|
|
|
{
|
2000-05-05 09:47:32 +04:00
|
|
|
// Append only one component. Check for subdirs.
|
|
|
|
if (!node || (_mbschr((const unsigned char*) node, '\\') != nsnull))
|
|
|
|
{
|
|
|
|
return NS_ERROR_FILE_UNRECOGNIZED_PATH;
|
|
|
|
}
|
|
|
|
return AppendRelativePath(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLocalFile::AppendRelativePath(const char *node)
|
|
|
|
{
|
|
|
|
// Cannot start with a / or have .. or have / anywhere
|
2001-02-23 02:52:26 +03:00
|
|
|
if (!node || strchr(node, '/'))
|
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();
|
1999-12-04 04:13:44 +03:00
|
|
|
mWorkingPath.Append("\\");
|
2000-01-25 00:28:28 +03:00
|
|
|
mWorkingPath.Append(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
|
|
|
|
nsLocalFile::GetLeafName(char * *aLeafName)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aLeafName);
|
|
|
|
|
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++;
|
|
|
|
|
2000-06-03 13:46:12 +04:00
|
|
|
*aLeafName = (char*) nsMemory::Clone(leaf, strlen(leaf)+1);
|
1999-12-04 04:13:44 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2000-01-25 00:28:28 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLocalFile::SetLeafName(const char * aLeafName)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
nsLocalFile::GetPath(char **_retval)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(_retval);
|
2000-06-03 13:46:12 +04:00
|
|
|
*_retval = (char*) nsMemory::Clone(mWorkingPath, strlen(mWorkingPath)+1);
|
1999-12-04 04:13:44 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsLocalFile::CopySingleFile(nsIFile *sourceFile, nsIFile *destParent, const char * newName, PRBool followSymlinks, PRBool move)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
char* filePath;
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
char* inFilePath;
|
|
|
|
destParent->GetTarget(&inFilePath);
|
2000-08-20 11:33:54 +04:00
|
|
|
nsCString destPath(inFilePath);
|
2000-06-03 13:46:12 +04:00
|
|
|
nsMemory::Free(inFilePath);
|
1999-12-04 04:13:44 +03:00
|
|
|
|
|
|
|
destPath.Append("\\");
|
|
|
|
|
|
|
|
if (newName == nsnull)
|
|
|
|
{
|
|
|
|
char *aFileName;
|
|
|
|
sourceFile->GetLeafName(&aFileName);
|
|
|
|
destPath.Append(aFileName);
|
2000-06-03 13:46:12 +04:00
|
|
|
nsMemory::Free(aFileName);
|
1999-12-04 04:13:44 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
destPath.Append(newName);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (followSymlinks)
|
|
|
|
{
|
1999-12-08 02:31:25 +03:00
|
|
|
rv = sourceFile->GetTarget(&filePath);
|
|
|
|
if (!filePath)
|
|
|
|
rv = sourceFile->GetPath(&filePath);
|
1999-12-04 04:13:44 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rv = sourceFile->GetPath(&filePath);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
int copyOK;
|
|
|
|
|
|
|
|
if (!move)
|
|
|
|
copyOK = CopyFile(filePath, destPath, PR_TRUE);
|
|
|
|
else
|
|
|
|
copyOK = MoveFile(filePath, destPath);
|
|
|
|
|
|
|
|
if (!copyOK) // CopyFile and MoveFile returns non-zero if succeeds (backward if you ask me).
|
|
|
|
rv = ConvertWinError(GetLastError());
|
|
|
|
|
2000-06-03 13:46:12 +04:00
|
|
|
nsMemory::Free(filePath);
|
1999-12-04 04:13:44 +03:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
2000-03-24 08:47:39 +03:00
|
|
|
nsLocalFile::CopyMove(nsIFile *aParentDir, const char *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.
|
|
|
|
|
|
|
|
if (!newName)
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
|
|
|
|
move = PR_TRUE;
|
|
|
|
|
|
|
|
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);
|
|
|
|
if (exists == PR_FALSE)
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
char* target;
|
|
|
|
newParentDir->GetTarget(&target);
|
|
|
|
|
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
|
|
|
|
|
|
|
rv = realDest->InitWithPath(target);
|
|
|
|
|
2000-06-03 13:46:12 +04:00
|
|
|
nsMemory::Free(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;
|
|
|
|
|
|
|
|
char *allocatedNewName;
|
|
|
|
if (!newName)
|
|
|
|
{
|
1999-12-08 02:31:25 +03:00
|
|
|
PRBool isLink;
|
|
|
|
IsSymlink(&isLink);
|
|
|
|
if (isLink)
|
|
|
|
{
|
|
|
|
char* temp;
|
|
|
|
GetTarget(&temp);
|
2000-04-26 07:57:34 +04:00
|
|
|
const char* leaf = (const char*) _mbsrchr((const unsigned char*) temp, '\\');
|
1999-12-08 02:31:25 +03:00
|
|
|
if (leaf[0] == '\\')
|
|
|
|
leaf++;
|
2000-06-03 13:46:12 +04:00
|
|
|
allocatedNewName = (char*) nsMemory::Clone( leaf, strlen(leaf)+1 );
|
1999-12-08 02:31:25 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
GetLeafName(&allocatedNewName);// this should be the leaf name of the
|
|
|
|
}
|
1999-12-04 04:13:44 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2000-06-03 13:46:12 +04:00
|
|
|
allocatedNewName = (char*) nsMemory::Clone( newName, strlen(newName)+1 );
|
1999-12-04 04:13:44 +03:00
|
|
|
}
|
|
|
|
|
2000-01-25 00:28:28 +03:00
|
|
|
rv = target->Append(allocatedNewName);
|
1999-12-04 04:13:44 +03:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
2000-06-03 13:46:12 +04:00
|
|
|
nsMemory::Free(allocatedNewName);
|
1999-12-04 04:13:44 +03:00
|
|
|
|
|
|
|
target->Create(DIRECTORY_TYPE, 0644); // TODO, what permissions should we use
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
nsDirEnumerator* dirEnum = new nsDirEnumerator();
|
|
|
|
if (!dirEnum)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
rv = dirEnum->Init(this);
|
|
|
|
|
|
|
|
nsCOMPtr<nsISimpleEnumerator> iterator = do_QueryInterface(dirEnum);
|
|
|
|
|
|
|
|
PRBool more;
|
|
|
|
iterator->HasMoreElements(&more);
|
|
|
|
while (more)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsISupports> item;
|
|
|
|
nsCOMPtr<nsIFile> file;
|
|
|
|
iterator->GetNext(getter_AddRefs(item));
|
|
|
|
file = do_QueryInterface(item);
|
|
|
|
PRBool isDir, isLink;
|
|
|
|
|
|
|
|
file->IsDirectory(&isDir);
|
|
|
|
file->IsSymlink(&isLink);
|
|
|
|
|
|
|
|
if (move)
|
|
|
|
{
|
|
|
|
if (followSymlinks)
|
1999-12-08 05:07:38 +03:00
|
|
|
rv = NS_ERROR_FAILURE;
|
1999-12-04 04:13:44 +03:00
|
|
|
else
|
|
|
|
rv = file->MoveTo(target, nsnull);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (followSymlinks)
|
|
|
|
rv = file->CopyToFollowingLinks(target, nsnull);
|
|
|
|
else
|
|
|
|
rv = file->CopyTo(target, nsnull);
|
|
|
|
}
|
|
|
|
|
|
|
|
iterator->HasMoreElements(&more);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// If we moved, we want to adjust this.
|
|
|
|
if (move)
|
|
|
|
{
|
1999-12-22 04:56:45 +03:00
|
|
|
MakeDirty();
|
|
|
|
|
|
|
|
char* newParentPath;
|
|
|
|
newParentDir->GetPath(&newParentPath);
|
|
|
|
|
|
|
|
if (newParentPath == nsnull)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
1999-12-04 04:13:44 +03:00
|
|
|
if (newName == nsnull)
|
|
|
|
{
|
|
|
|
char *aFileName;
|
|
|
|
GetLeafName(&aFileName);
|
1999-12-22 04:56:45 +03:00
|
|
|
|
2000-03-24 08:47:39 +03:00
|
|
|
InitWithPath(newParentPath);
|
2000-01-25 00:28:28 +03:00
|
|
|
Append(aFileName);
|
2000-03-24 08:47:39 +03:00
|
|
|
|
2000-06-03 13:46:12 +04:00
|
|
|
nsMemory::Free(aFileName);
|
1999-12-04 04:13:44 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2000-03-24 08:47:39 +03:00
|
|
|
InitWithPath(newParentPath);
|
|
|
|
Append(newName);
|
1999-12-04 04:13:44 +03:00
|
|
|
}
|
1999-12-22 04:56:45 +03:00
|
|
|
|
2000-06-03 13:46:12 +04:00
|
|
|
nsMemory::Free(newParentPath);
|
1999-12-04 04:13:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLocalFile::CopyTo(nsIFile *newParentDir, const char *newName)
|
|
|
|
{
|
|
|
|
return CopyMove(newParentDir, newName, PR_FALSE, PR_FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLocalFile::CopyToFollowingLinks(nsIFile *newParentDir, const char *newName)
|
|
|
|
{
|
|
|
|
return CopyMove(newParentDir, newName, PR_TRUE, PR_FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLocalFile::MoveTo(nsIFile *newParentDir, const char *newName)
|
|
|
|
{
|
|
|
|
return CopyMove(newParentDir, newName, PR_FALSE, PR_TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2000-02-17 18:35:54 +03:00
|
|
|
nsLocalFile::Spawn(const char **args, PRUint32 count)
|
1999-12-04 04:13:44 +03:00
|
|
|
{
|
|
|
|
PRBool isFile;
|
|
|
|
nsresult rv = IsFile(&isFile);
|
|
|
|
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
2000-02-17 18:35:54 +03:00
|
|
|
// make sure that when we allocate we have 1 greater than the
|
|
|
|
// count since we need to null terminate the list for the argv to
|
|
|
|
// pass into PR_CreateProcessDetached
|
|
|
|
char **my_argv = NULL;
|
|
|
|
|
2001-09-05 07:51:38 +04:00
|
|
|
my_argv = (char **)nsMemory::Alloc(sizeof(char *) * (count + 2) );
|
2000-02-17 18:35:54 +03:00
|
|
|
if (!my_argv) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
1999-12-04 04:13:44 +03:00
|
|
|
|
2000-02-17 18:35:54 +03:00
|
|
|
// copy the args
|
|
|
|
PRUint32 i;
|
|
|
|
for (i=0; i < count; i++) {
|
|
|
|
my_argv[i+1] = (char *)args[i];
|
1999-12-04 04:13:44 +03:00
|
|
|
}
|
2000-02-17 18:35:54 +03:00
|
|
|
// we need to set argv[0] to the program name.
|
2001-03-20 08:37:56 +03:00
|
|
|
my_argv[0] = NS_CONST_CAST(char*, mResolvedPath.get());
|
2000-02-17 18:35:54 +03:00
|
|
|
|
|
|
|
// null terminate the array
|
|
|
|
my_argv[count+1] = NULL;
|
|
|
|
rv = PR_CreateProcessDetached(mResolvedPath, my_argv, NULL, NULL);
|
1999-12-04 04:13:44 +03:00
|
|
|
|
2000-02-17 18:35:54 +03:00
|
|
|
// free up our argv
|
2000-06-03 13:46:12 +04:00
|
|
|
nsMemory::Free(my_argv);
|
1999-12-04 04:13:44 +03:00
|
|
|
|
2000-02-17 18:35:54 +03:00
|
|
|
if (PR_SUCCESS == rv)
|
|
|
|
return NS_OK;
|
|
|
|
else
|
|
|
|
return NS_ERROR_FILE_EXECUTION_FAILED;
|
1999-12-04 04:13:44 +03:00
|
|
|
}
|
|
|
|
|
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");
|
1999-12-22 06:01:08 +03:00
|
|
|
*_retval = PR_LoadLibrary(mResolvedPath);
|
2001-08-17 06:03:34 +04:00
|
|
|
NS_TIMELINE_STOP_TIMER("PR_LoadLibrary");
|
|
|
|
NS_TIMELINE_MARK_TIMER("PR_LoadLibrary");
|
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
|
|
|
{
|
|
|
|
PRBool isDir;
|
|
|
|
|
|
|
|
nsresult rv = IsDirectory(&isDir);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
2001-03-02 12:26:57 +03:00
|
|
|
const char *filePath = mResolvedPath.get();
|
2001-06-19 07:17:20 +04:00
|
|
|
|
1999-12-04 04:13:44 +03:00
|
|
|
if (isDir)
|
|
|
|
{
|
|
|
|
if (recursive)
|
|
|
|
{
|
|
|
|
nsDirEnumerator* dirEnum = new nsDirEnumerator();
|
2000-05-17 02:01:59 +04:00
|
|
|
if (dirEnum == nsnull)
|
1999-12-04 04:13:44 +03:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
rv = dirEnum->Init(this);
|
|
|
|
|
|
|
|
nsCOMPtr<nsISimpleEnumerator> iterator = do_QueryInterface(dirEnum);
|
|
|
|
|
|
|
|
PRBool more;
|
|
|
|
iterator->HasMoreElements(&more);
|
|
|
|
while (more)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsISupports> item;
|
|
|
|
nsCOMPtr<nsIFile> file;
|
|
|
|
iterator->GetNext(getter_AddRefs(item));
|
|
|
|
file = do_QueryInterface(item);
|
|
|
|
|
2001-07-24 22:38:25 +04:00
|
|
|
file->Remove(recursive);
|
1999-12-04 04:13:44 +03:00
|
|
|
|
|
|
|
iterator->HasMoreElements(&more);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rmdir(filePath); // todo: save return value?
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
remove(filePath); // todo: save return value?
|
|
|
|
}
|
|
|
|
|
2000-01-25 00:28:28 +03:00
|
|
|
MakeDirty();
|
1999-12-04 04:13:44 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLocalFile::GetLastModificationDate(PRInt64 *aLastModificationDate)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG(aLastModificationDate);
|
|
|
|
|
|
|
|
*aLastModificationDate = 0;
|
|
|
|
|
|
|
|
nsresult rv = ResolveAndStat(PR_TRUE);
|
|
|
|
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
2000-05-09 23:11:14 +04:00
|
|
|
// microseconds -> milliseconds
|
|
|
|
*aLastModificationDate = mFileInfo64.modifyTime / PR_USEC_PER_MSEC;
|
1999-12-04 04:13:44 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLocalFile::GetLastModificationDateOfLink(PRInt64 *aLastModificationDate)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG(aLastModificationDate);
|
|
|
|
|
|
|
|
*aLastModificationDate = 0;
|
|
|
|
|
|
|
|
nsresult rv = ResolveAndStat(PR_FALSE);
|
|
|
|
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
2000-05-09 23:11:14 +04:00
|
|
|
// microseconds -> milliseconds
|
|
|
|
*aLastModificationDate = mFileInfo64.modifyTime / PR_USEC_PER_MSEC;
|
|
|
|
|
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
|
|
|
|
nsLocalFile::SetLastModificationDate(PRInt64 aLastModificationDate)
|
|
|
|
{
|
|
|
|
return nsLocalFile::SetModDate(aLastModificationDate, 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
|
|
|
|
nsLocalFile::SetLastModificationDateOfLink(PRInt64 aLastModificationDate)
|
|
|
|
{
|
2000-02-22 00:51:47 +03:00
|
|
|
return nsLocalFile::SetModDate(aLastModificationDate, PR_FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsLocalFile::SetModDate(PRInt64 aLastModificationDate, PRBool resolveTerminal)
|
|
|
|
{
|
|
|
|
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...
|
|
|
|
PR_ExplodeTime(aLastModificationDate * 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);
|
|
|
|
|
2001-03-02 12:26:57 +03:00
|
|
|
const char *filePath = mResolvedPath.get();
|
1999-12-04 04:13:44 +03:00
|
|
|
|
|
|
|
PRInt64 int64;
|
|
|
|
|
|
|
|
LL_I2L(int64 , LONG_MAX);
|
|
|
|
|
|
|
|
char aDrive[_MAX_DRIVE + 2];
|
|
|
|
_splitpath( (const char*)filePath, aDrive, NULL, NULL, NULL);
|
|
|
|
strcat(aDrive, "\\");
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (getDiskFreeSpaceExA && (*getDiskFreeSpaceExA)(aDrive,
|
|
|
|
&liFreeBytesAvailableToCaller,
|
|
|
|
&liTotalNumberOfBytes,
|
|
|
|
&liTotalNumberOfFreeBytes))
|
|
|
|
{
|
|
|
|
nBytes = (double)(signed __int64)liFreeBytesAvailableToCaller.QuadPart;
|
|
|
|
}
|
|
|
|
else if ( GetDiskFreeSpace(aDrive, &dwSecPerClus, &dwBytesPerSec, &dwFreeClus, &dwTotalClus))
|
|
|
|
{
|
|
|
|
nBytes = (double)dwFreeClus*(double)dwSecPerClus*(double) dwBytesPerSec;
|
|
|
|
}
|
|
|
|
|
|
|
|
LL_D2L(*aDiskSpaceAvailable, nBytes);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLocalFile::GetParent(nsIFile * *aParent)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aParent);
|
|
|
|
|
|
|
|
nsCString parentPath = mWorkingPath;
|
|
|
|
|
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;
|
2001-03-02 12:26:57 +03:00
|
|
|
nsresult rv = NS_NewLocalFile(parentPath.get(), 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)
|
|
|
|
{
|
|
|
|
return localFile->QueryInterface(NS_GET_IID(nsIFile), (void**)aParent);
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
|
2000-04-26 07:57:34 +04:00
|
|
|
char* path = nsnull;
|
1999-12-04 04:13:44 +03:00
|
|
|
PRBool symLink;
|
|
|
|
|
|
|
|
rv = IsSymlink(&symLink);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
if (symLink)
|
|
|
|
GetTarget(&path);
|
|
|
|
else
|
|
|
|
GetPath(&path);
|
|
|
|
|
2001-05-23 10:03:59 +04:00
|
|
|
// Get extension.
|
|
|
|
char* ext = ::strrchr( path, '.' );
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2000-06-03 13:46:12 +04:00
|
|
|
nsMemory::Free(path);
|
1999-12-04 04:13:44 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLocalFile::IsDirectory(PRBool *_retval)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG(_retval);
|
|
|
|
*_retval = PR_FALSE;
|
|
|
|
|
|
|
|
nsresult rv = ResolveAndStat(PR_TRUE);
|
|
|
|
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
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;
|
|
|
|
|
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_HIDDEN) != 0);
|
1999-12-04 04:13:44 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLocalFile::IsSymlink(PRBool *_retval)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG(_retval);
|
|
|
|
*_retval = PR_FALSE;
|
|
|
|
|
|
|
|
char* path;
|
|
|
|
int pathLen;
|
|
|
|
|
|
|
|
GetPath(&path);
|
|
|
|
pathLen = strlen(path);
|
|
|
|
|
|
|
|
const char* leaf = path + pathLen - 4;
|
|
|
|
|
|
|
|
if ( (strcmp(leaf, ".lnk") == 0))
|
|
|
|
{
|
|
|
|
*_retval = PR_TRUE;
|
|
|
|
}
|
|
|
|
|
2000-06-03 13:46:12 +04:00
|
|
|
nsMemory::Free(path);
|
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);
|
|
|
|
*_retval = PR_FALSE;
|
|
|
|
|
|
|
|
char* inFilePath;
|
|
|
|
inFile->GetPath(&inFilePath);
|
|
|
|
|
|
|
|
char* filePath;
|
|
|
|
GetPath(&filePath);
|
|
|
|
|
|
|
|
if (strcmp(inFilePath, filePath) == 0)
|
|
|
|
*_retval = PR_TRUE;
|
|
|
|
|
2000-06-03 13:46:12 +04:00
|
|
|
nsMemory::Free(inFilePath);
|
|
|
|
nsMemory::Free(filePath);
|
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
|
|
|
|
|
|
|
char* myFilePath;
|
|
|
|
if ( NS_FAILED(GetTarget(&myFilePath)))
|
|
|
|
GetPath(&myFilePath);
|
2000-01-29 02:53:59 +03:00
|
|
|
|
2000-01-29 03:00:21 +03:00
|
|
|
PRInt32 myFilePathLen = strlen(myFilePath);
|
|
|
|
|
|
|
|
char* inFilePath;
|
|
|
|
if ( NS_FAILED(inFile->GetTarget(&inFilePath)))
|
|
|
|
inFile->GetPath(&inFilePath);
|
|
|
|
|
|
|
|
if ( strncmp( myFilePath, inFilePath, 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-29 03:00:21 +03:00
|
|
|
|
2000-06-03 13:46:12 +04:00
|
|
|
nsMemory::Free(inFilePath);
|
|
|
|
nsMemory::Free(myFilePath);
|
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
|
|
|
|
nsLocalFile::GetTarget(char **_retval)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG(_retval);
|
|
|
|
*_retval = nsnull;
|
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);
|
|
|
|
|
2000-06-03 13:46:12 +04:00
|
|
|
*_retval = (char*) nsMemory::Clone( mResolvedPath, strlen(mResolvedPath)+1 );
|
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-09-12 12:05:13 +04:00
|
|
|
NS_IMETHODIMP nsLocalFile::GetURL(char * *aURL)
|
|
|
|
{
|
2001-06-22 02:02:47 +04:00
|
|
|
NS_ENSURE_ARG_POINTER(aURL);
|
|
|
|
*aURL = nsnull;
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
char* ePath = nsnull;
|
|
|
|
nsCAutoString escPath;
|
|
|
|
|
|
|
|
rv = GetPath(&ePath);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
|
|
|
|
// Replace \ with / to convert to an url
|
|
|
|
char* s = ePath;
|
|
|
|
while (*s)
|
|
|
|
{
|
|
|
|
// We need to call IsDBCSLeadByte because
|
|
|
|
// Japanese windows can have 0x5C in the sencond byte
|
|
|
|
// of a Japanese character, for example 0x8F 0x5C is
|
|
|
|
// one Japanese character
|
|
|
|
if(::IsDBCSLeadByte(*s) && *(s+1) != nsnull) {
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
else if (*s == '\\')
|
|
|
|
*s = '/';
|
|
|
|
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Escape the path with the directory mask
|
|
|
|
rv = nsStdEscape(ePath, esc_Directory+esc_Forced, escPath);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
|
|
|
|
escPath.Insert("file:///", 0);
|
|
|
|
|
|
|
|
PRBool dir;
|
|
|
|
rv = IsDirectory(&dir);
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "Cannot tell if this is a directory");
|
|
|
|
if (NS_SUCCEEDED(rv) && dir && escPath[escPath.Length() - 1] != '/') {
|
|
|
|
// make sure we have a trailing slash
|
|
|
|
escPath += "/";
|
|
|
|
}
|
|
|
|
*aURL = nsCRT::strdup((const char *)escPath);
|
|
|
|
rv = *aURL ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CRTFREEIF(ePath);
|
|
|
|
return rv;
|
2000-09-12 12:05:13 +04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsLocalFile::SetURL(const char * aURL)
|
|
|
|
{
|
2001-06-22 02:02:47 +04:00
|
|
|
NS_ENSURE_ARG(aURL);
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
nsXPIDLCString host, directory, fileBaseName, fileExtension;
|
|
|
|
|
|
|
|
rv = ParseURL(aURL, getter_Copies(host), getter_Copies(directory),
|
|
|
|
getter_Copies(fileBaseName), getter_Copies(fileExtension));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
nsCAutoString path;
|
|
|
|
nsCAutoString component;
|
|
|
|
|
|
|
|
if (host)
|
|
|
|
{
|
|
|
|
// We can end up with a host when given: file://C|/ instead of file:///
|
|
|
|
if (strlen((const char *)host) == 2 && ((const char *)host)[1] == '|')
|
|
|
|
{
|
|
|
|
path += host;
|
|
|
|
path.SetCharAt(':', 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (directory)
|
|
|
|
{
|
|
|
|
nsStdEscape(directory, esc_Directory, component);
|
|
|
|
if (!host && component.Length() > 2 && component.CharAt(2) == '|')
|
|
|
|
component.SetCharAt(':', 2);
|
|
|
|
component.ReplaceChar('/', '\\');
|
|
|
|
path += component;
|
|
|
|
}
|
|
|
|
if (fileBaseName)
|
|
|
|
{
|
|
|
|
nsStdEscape(fileBaseName, esc_FileBaseName, component);
|
|
|
|
path += component;
|
|
|
|
}
|
|
|
|
if (fileExtension)
|
|
|
|
{
|
|
|
|
nsStdEscape(fileExtension, esc_FileExtension, component);
|
|
|
|
path += '.';
|
|
|
|
path += component;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsUnescape((char*)path.get());
|
|
|
|
|
|
|
|
// remove leading '\'
|
|
|
|
if (path.CharAt(0) == '\\')
|
|
|
|
path.Cut(0, 1);
|
|
|
|
|
|
|
|
rv = InitWithPath(path);
|
|
|
|
|
|
|
|
return rv;
|
2000-09-12 12:05:13 +04:00
|
|
|
}
|
|
|
|
|
2000-07-17 19:03:05 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLocalFile::GetPersistentDescriptor(char * *aPersistentDescriptor)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aPersistentDescriptor);
|
|
|
|
return GetPath(aPersistentDescriptor);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLocalFile::SetPersistentDescriptor(const char * aPersistentDescriptor)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG(aPersistentDescriptor);
|
|
|
|
return InitWithPath(aPersistentDescriptor);
|
|
|
|
}
|
|
|
|
|
2001-01-30 08:02:48 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLocalFile::Reveal()
|
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
PRBool isDirectory = PR_FALSE;
|
|
|
|
nsXPIDLCString path;
|
|
|
|
|
|
|
|
IsDirectory(&isDirectory);
|
|
|
|
if (isDirectory)
|
|
|
|
{
|
|
|
|
GetPath(getter_Copies(path));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIFile> parent;
|
|
|
|
GetParent(getter_AddRefs(parent));
|
|
|
|
if (parent)
|
|
|
|
parent->GetPath(getter_Copies(path));
|
|
|
|
}
|
|
|
|
|
|
|
|
// use the app registry name to launch a shell execute....
|
|
|
|
LONG r = (LONG) ::ShellExecute( NULL, "explore", (const char *) path, NULL, NULL, SW_SHOWNORMAL);
|
|
|
|
if (r < 32)
|
|
|
|
rv = NS_ERROR_FAILURE;
|
|
|
|
else
|
|
|
|
rv = NS_OK;
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLocalFile::Launch()
|
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
nsXPIDLCString path;
|
|
|
|
GetPath(getter_Copies(path));
|
|
|
|
|
|
|
|
// use the app registry name to launch a shell execute....
|
|
|
|
LONG r = (LONG) ::ShellExecute( NULL, "open", (const char *) path, NULL, NULL, SW_SHOWNORMAL);
|
|
|
|
if (r < 32)
|
|
|
|
rv = NS_ERROR_FAILURE;
|
|
|
|
else
|
|
|
|
rv = NS_OK;
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
1999-12-04 04:13:44 +03:00
|
|
|
|
2000-01-29 02:35:55 +03:00
|
|
|
nsresult
|
2000-07-13 03:31:31 +04:00
|
|
|
NS_NewLocalFile(const char* 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);
|
|
|
|
|
2000-07-17 19:03:05 +04:00
|
|
|
if (path) {
|
|
|
|
nsresult rv = file->InitWithPath(path);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
1999-12-04 04:13:44 +03:00
|
|
|
|
|
|
|
|