2014-05-05 10:30:43 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-12-08 18:52:54 -08:00
|
|
|
#include "mozilla/ArrayUtils.h"
|
2013-01-10 00:39:40 -08:00
|
|
|
#include "mozilla/DebugOnly.h"
|
2013-11-21 19:35:42 -08:00
|
|
|
#include "mozilla/WindowsVersion.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include "nsCOMPtr.h"
|
2011-12-16 06:53:07 -08:00
|
|
|
#include "nsAutoPtr.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsMemory.h"
|
|
|
|
|
|
|
|
#include "nsLocalFile.h"
|
|
|
|
#include "nsIDirectoryEnumerator.h"
|
|
|
|
#include "nsNativeCharsetUtils.h"
|
|
|
|
|
|
|
|
#include "nsISimpleEnumerator.h"
|
|
|
|
#include "nsIComponentManager.h"
|
|
|
|
#include "prio.h"
|
|
|
|
#include "private/pprio.h" // To get PR_ImportFile
|
|
|
|
#include "prprf.h"
|
|
|
|
#include "prmem.h"
|
|
|
|
#include "nsHashKeys.h"
|
|
|
|
|
|
|
|
#include "nsXPIDLString.h"
|
|
|
|
#include "nsReadableUtils.h"
|
|
|
|
|
|
|
|
#include <direct.h>
|
|
|
|
#include <windows.h>
|
2012-02-06 11:11:30 -08:00
|
|
|
#include <shlwapi.h>
|
2008-08-14 08:48:06 -07:00
|
|
|
#include <aclapi.h>
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include "shellapi.h"
|
|
|
|
#include "shlguid.h"
|
|
|
|
|
|
|
|
#include <io.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <mbstring.h>
|
|
|
|
|
|
|
|
#include "nsXPIDLString.h"
|
|
|
|
#include "prproces.h"
|
2013-09-23 10:29:27 -07:00
|
|
|
#include "prlink.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-03-31 21:29:02 -07:00
|
|
|
#include "mozilla/Mutex.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "SpecialSystemDirectory.h"
|
|
|
|
|
2014-02-26 13:36:36 -08:00
|
|
|
#include "nsTraceRefcnt.h"
|
2012-02-26 13:46:38 -08:00
|
|
|
#include "nsXPCOMCIDInternal.h"
|
|
|
|
#include "nsThreadUtils.h"
|
2013-11-07 10:55:47 -08:00
|
|
|
#include "nsXULAppAPI.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-03-31 21:29:02 -07:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#define CHECK_mWorkingPath() \
|
|
|
|
PR_BEGIN_MACRO \
|
|
|
|
if (mWorkingPath.IsEmpty()) \
|
|
|
|
return NS_ERROR_NOT_INITIALIZED; \
|
|
|
|
PR_END_MACRO
|
|
|
|
|
2011-03-28 05:36:33 -07:00
|
|
|
// CopyFileEx only supports unbuffered I/O in Windows Vista and above
|
2012-08-14 02:17:34 -07:00
|
|
|
#ifndef COPY_FILE_NO_BUFFERING
|
2011-03-28 05:36:33 -07:00
|
|
|
#define COPY_FILE_NO_BUFFERING 0x00001000
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif
|
|
|
|
|
2009-04-20 18:06:03 -07:00
|
|
|
#ifndef FILE_ATTRIBUTE_NOT_CONTENT_INDEXED
|
|
|
|
#define FILE_ATTRIBUTE_NOT_CONTENT_INDEXED 0x00002000
|
|
|
|
#endif
|
|
|
|
|
2012-02-06 11:11:30 -08:00
|
|
|
#ifndef DRIVE_REMOTE
|
|
|
|
#define DRIVE_REMOTE 4
|
|
|
|
#endif
|
|
|
|
|
2012-02-26 13:46:38 -08:00
|
|
|
/**
|
2014-05-05 10:30:43 -07:00
|
|
|
* A runnable to dispatch back to the main thread when
|
2012-02-26 13:46:38 -08:00
|
|
|
* AsyncLocalFileWinOperation completes.
|
|
|
|
*/
|
|
|
|
class AsyncLocalFileWinDone : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
2014-05-05 10:30:43 -07:00
|
|
|
AsyncLocalFileWinDone() :
|
|
|
|
mWorkerThread(do_GetCurrentThread())
|
|
|
|
{
|
|
|
|
// Objects of this type must only be created on worker threads
|
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
|
|
|
}
|
2012-02-26 13:46:38 -08:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
// This event shuts down the worker thread and so must be main thread.
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2012-02-26 13:46:38 -08:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
// If we don't destroy the thread when we're done with it, it will hang
|
|
|
|
// around forever... and that is bad!
|
|
|
|
mWorkerThread->Shutdown();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-02-26 13:46:38 -08:00
|
|
|
|
|
|
|
private:
|
2014-05-05 10:30:43 -07:00
|
|
|
nsCOMPtr<nsIThread> mWorkerThread;
|
2012-02-26 13:46:38 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A runnable to dispatch from the main thread when an async operation should
|
2014-05-05 10:30:43 -07:00
|
|
|
* be performed.
|
2012-02-26 13:46:38 -08:00
|
|
|
*/
|
|
|
|
class AsyncLocalFileWinOperation : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
2014-05-05 10:30:43 -07:00
|
|
|
enum FileOp { RevealOp, LaunchOp };
|
2012-02-26 13:46:38 -08:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
AsyncLocalFileWinOperation(AsyncLocalFileWinOperation::FileOp aOperation,
|
2014-05-14 13:15:46 -07:00
|
|
|
const nsAString& aResolvedPath) :
|
2014-05-05 10:30:43 -07:00
|
|
|
mOperation(aOperation),
|
|
|
|
mResolvedPath(aResolvedPath)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
MOZ_ASSERT(!NS_IsMainThread(),
|
|
|
|
"AsyncLocalFileWinOperation should not be run on the main thread!");
|
|
|
|
|
|
|
|
CoInitialize(nullptr);
|
2014-05-14 13:15:46 -07:00
|
|
|
switch (mOperation) {
|
2014-05-05 10:30:43 -07:00
|
|
|
case RevealOp: {
|
|
|
|
Reveal();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case LaunchOp: {
|
|
|
|
Launch();
|
|
|
|
}
|
|
|
|
break;
|
2012-02-26 13:46:38 -08:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
CoUninitialize();
|
2012-02-26 13:46:38 -08:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
// Send the result back to the main thread so that it can shutdown
|
|
|
|
nsCOMPtr<nsIRunnable> resultrunnable = new AsyncLocalFileWinDone();
|
|
|
|
NS_DispatchToMainThread(resultrunnable);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-02-26 13:46:38 -08:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
private:
|
|
|
|
// Reveals the path in explorer.
|
|
|
|
nsresult Reveal()
|
|
|
|
{
|
|
|
|
DWORD attributes = GetFileAttributesW(mResolvedPath.get());
|
|
|
|
if (INVALID_FILE_ATTRIBUTES == attributes) {
|
|
|
|
return NS_ERROR_FILE_INVALID_PATH;
|
|
|
|
}
|
2012-02-26 13:46:38 -08:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
HRESULT hr;
|
|
|
|
if (attributes & FILE_ATTRIBUTE_DIRECTORY) {
|
|
|
|
// We have a directory so we should open the directory itself.
|
2014-05-14 13:15:46 -07:00
|
|
|
ITEMIDLIST* dir = ILCreateFromPathW(mResolvedPath.get());
|
2014-05-05 10:30:43 -07:00
|
|
|
if (!dir) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ITEMIDLIST* selection[] = { dir };
|
|
|
|
UINT count = ArrayLength(selection);
|
|
|
|
|
|
|
|
//Perform the open of the directory.
|
|
|
|
hr = SHOpenFolderAndSelectItems(dir, count, selection, 0);
|
|
|
|
CoTaskMemFree(dir);
|
|
|
|
} else {
|
|
|
|
int32_t len = mResolvedPath.Length();
|
|
|
|
// We don't currently handle UNC long paths of the form \\?\ anywhere so
|
|
|
|
// this should be fine.
|
|
|
|
if (len > MAX_PATH) {
|
|
|
|
return NS_ERROR_FILE_INVALID_PATH;
|
|
|
|
}
|
|
|
|
WCHAR parentDirectoryPath[MAX_PATH + 1] = { 0 };
|
|
|
|
wcsncpy(parentDirectoryPath, mResolvedPath.get(), MAX_PATH);
|
|
|
|
PathRemoveFileSpecW(parentDirectoryPath);
|
|
|
|
|
|
|
|
// We have a file so we should open the parent directory.
|
2014-05-14 13:15:46 -07:00
|
|
|
ITEMIDLIST* dir = ILCreateFromPathW(parentDirectoryPath);
|
2014-05-05 10:30:43 -07:00
|
|
|
if (!dir) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the item in the directory to select to the file we want to reveal.
|
2014-05-14 13:15:46 -07:00
|
|
|
ITEMIDLIST* item = ILCreateFromPathW(mResolvedPath.get());
|
2014-05-05 10:30:43 -07:00
|
|
|
if (!item) {
|
|
|
|
CoTaskMemFree(dir);
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ITEMIDLIST* selection[] = { item };
|
|
|
|
UINT count = ArrayLength(selection);
|
|
|
|
|
|
|
|
//Perform the selection of the file.
|
|
|
|
hr = SHOpenFolderAndSelectItems(dir, count, selection, 0);
|
|
|
|
|
|
|
|
CoTaskMemFree(dir);
|
|
|
|
CoTaskMemFree(item);
|
2012-02-26 13:46:38 -08:00
|
|
|
}
|
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
return SUCCEEDED(hr) ? NS_OK : NS_ERROR_FAILURE;
|
|
|
|
}
|
2012-02-26 13:46:38 -08:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
// Launches the default shell operation for the file path
|
|
|
|
nsresult Launch()
|
|
|
|
{
|
|
|
|
// use the app registry name to launch a shell execute....
|
|
|
|
SHELLEXECUTEINFOW seinfo;
|
|
|
|
memset(&seinfo, 0, sizeof(seinfo));
|
|
|
|
seinfo.cbSize = sizeof(SHELLEXECUTEINFOW);
|
|
|
|
if (XRE_GetWindowsEnvironment() == WindowsEnvironmentType_Metro) {
|
|
|
|
seinfo.fMask = SEE_MASK_FLAG_LOG_USAGE;
|
|
|
|
}
|
|
|
|
seinfo.hwnd = nullptr;
|
|
|
|
seinfo.lpVerb = nullptr;
|
|
|
|
seinfo.lpFile = mResolvedPath.get();
|
|
|
|
seinfo.lpParameters = nullptr;
|
|
|
|
seinfo.lpDirectory = nullptr;
|
|
|
|
seinfo.nShow = SW_SHOWNORMAL;
|
|
|
|
|
|
|
|
// Use the directory of the file we're launching as the working
|
|
|
|
// directory. That way if we have a self extracting EXE it won't
|
|
|
|
// suggest to extract to the install directory.
|
|
|
|
WCHAR workingDirectory[MAX_PATH + 1] = { L'\0' };
|
|
|
|
wcsncpy(workingDirectory, mResolvedPath.get(), MAX_PATH);
|
|
|
|
if (PathRemoveFileSpecW(workingDirectory)) {
|
|
|
|
seinfo.lpDirectory = workingDirectory;
|
|
|
|
} else {
|
|
|
|
NS_WARNING("Could not set working directory for launched file.");
|
2012-02-26 13:46:38 -08:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
|
|
|
|
if (ShellExecuteExW(&seinfo)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
DWORD r = GetLastError();
|
|
|
|
// if the file has no association, we launch windows'
|
|
|
|
// "what do you want to do" dialog
|
|
|
|
if (r == SE_ERR_NOASSOC) {
|
|
|
|
nsAutoString shellArg;
|
2014-05-26 11:54:10 -07:00
|
|
|
shellArg.AssignLiteral(MOZ_UTF16("shell32.dll,OpenAs_RunDLL "));
|
2014-05-21 20:48:52 -07:00
|
|
|
shellArg.Append(mResolvedPath);
|
2014-05-05 10:30:43 -07:00
|
|
|
seinfo.lpFile = L"RUNDLL32.EXE";
|
|
|
|
seinfo.lpParameters = shellArg.get();
|
2014-05-14 13:15:46 -07:00
|
|
|
if (ShellExecuteExW(&seinfo)) {
|
2012-02-26 13:46:38 -08:00
|
|
|
return NS_OK;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
r = GetLastError();
|
|
|
|
}
|
|
|
|
if (r < 32) {
|
|
|
|
switch (r) {
|
|
|
|
case 0:
|
|
|
|
case SE_ERR_OOM:
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
case ERROR_FILE_NOT_FOUND:
|
|
|
|
return NS_ERROR_FILE_NOT_FOUND;
|
|
|
|
case ERROR_PATH_NOT_FOUND:
|
|
|
|
return NS_ERROR_FILE_UNRECOGNIZED_PATH;
|
|
|
|
case ERROR_BAD_FORMAT:
|
|
|
|
return NS_ERROR_FILE_CORRUPTED;
|
|
|
|
case SE_ERR_ACCESSDENIED:
|
|
|
|
return NS_ERROR_FILE_ACCESS_DENIED;
|
|
|
|
case SE_ERR_ASSOCINCOMPLETE:
|
|
|
|
case SE_ERR_NOASSOC:
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
case SE_ERR_DDEBUSY:
|
|
|
|
case SE_ERR_DDEFAIL:
|
|
|
|
case SE_ERR_DDETIMEOUT:
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
case SE_ERR_DLLNOTFOUND:
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
case SE_ERR_SHARE:
|
|
|
|
return NS_ERROR_FILE_IS_LOCKED;
|
|
|
|
default:
|
|
|
|
return NS_ERROR_FILE_EXECUTION_FAILED;
|
|
|
|
}
|
2012-02-26 13:46:38 -08:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-02-26 13:46:38 -08:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
// Stores the operation that will be performed on the thread
|
|
|
|
AsyncLocalFileWinOperation::FileOp mOperation;
|
2012-08-14 02:17:34 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
// Stores the path to perform the operation on
|
|
|
|
nsString mResolvedPath;
|
2012-02-26 13:46:38 -08:00
|
|
|
};
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
class nsDriveEnumerator : public nsISimpleEnumerator
|
|
|
|
{
|
|
|
|
public:
|
2014-05-05 10:30:43 -07:00
|
|
|
nsDriveEnumerator();
|
|
|
|
virtual ~nsDriveEnumerator();
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSISIMPLEENUMERATOR
|
|
|
|
nsresult Init();
|
2007-03-22 10:30:00 -07:00
|
|
|
private:
|
2014-05-05 10:30:43 -07:00
|
|
|
/* mDrives stores the null-separated drive names.
|
|
|
|
* Init sets them.
|
|
|
|
* HasMoreElements checks mStartOfCurrentDrive.
|
|
|
|
* GetNext advances mStartOfCurrentDrive.
|
|
|
|
*/
|
|
|
|
nsString mDrives;
|
|
|
|
nsAString::const_iterator mStartOfCurrentDrive;
|
|
|
|
nsAString::const_iterator mEndOfDrivesString;
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
// short cut resolver
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
class ShortcutResolver
|
|
|
|
{
|
|
|
|
public:
|
2014-05-05 10:30:43 -07:00
|
|
|
ShortcutResolver();
|
|
|
|
// nonvirtual since we're not subclassed
|
|
|
|
~ShortcutResolver();
|
|
|
|
|
|
|
|
nsresult Init();
|
2014-05-14 13:15:46 -07:00
|
|
|
nsresult Resolve(const WCHAR* aIn, WCHAR* aOut);
|
|
|
|
nsresult SetShortcut(bool aUpdateExisting,
|
|
|
|
const WCHAR* aShortcutPath,
|
|
|
|
const WCHAR* aTargetPath,
|
|
|
|
const WCHAR* aWorkingDir,
|
|
|
|
const WCHAR* aArgs,
|
|
|
|
const WCHAR* aDescription,
|
|
|
|
const WCHAR* aIconFile,
|
|
|
|
int32_t aIconIndex);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
private:
|
2014-05-05 10:30:43 -07:00
|
|
|
Mutex mLock;
|
|
|
|
nsRefPtr<IPersistFile> mPersistFile;
|
|
|
|
nsRefPtr<IShellLinkW> mShellLink;
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
2011-12-16 06:53:07 -08:00
|
|
|
ShortcutResolver::ShortcutResolver() :
|
2014-05-05 10:30:43 -07:00
|
|
|
mLock("ShortcutResolver.mLock")
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
CoInitialize(nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
ShortcutResolver::~ShortcutResolver()
|
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
CoUninitialize();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
ShortcutResolver::Init()
|
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
// Get a pointer to the IPersistFile interface.
|
|
|
|
if (FAILED(CoCreateInstance(CLSID_ShellLink,
|
|
|
|
nullptr,
|
|
|
|
CLSCTX_INPROC_SERVER,
|
|
|
|
IID_IShellLinkW,
|
|
|
|
getter_AddRefs(mShellLink))) ||
|
|
|
|
FAILED(mShellLink->QueryInterface(IID_IPersistFile,
|
|
|
|
getter_AddRefs(mPersistFile)))) {
|
|
|
|
mShellLink = nullptr;
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// |out| must be an allocated buffer of size MAX_PATH
|
|
|
|
nsresult
|
2014-05-14 13:15:46 -07:00
|
|
|
ShortcutResolver::Resolve(const WCHAR* aIn, WCHAR* aOut)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-14 13:15:46 -07:00
|
|
|
if (!mShellLink) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
MutexAutoLock lock(mLock);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
if (FAILED(mPersistFile->Load(aIn, STGM_READ)) ||
|
2014-05-05 10:30:43 -07:00
|
|
|
FAILED(mShellLink->Resolve(nullptr, SLR_NO_UI)) ||
|
2014-05-14 13:15:46 -07:00
|
|
|
FAILED(mShellLink->GetPath(aOut, MAX_PATH, nullptr, SLGP_UNCPRIORITY))) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-04-18 15:01:15 -07:00
|
|
|
nsresult
|
2014-05-14 13:15:46 -07:00
|
|
|
ShortcutResolver::SetShortcut(bool aUpdateExisting,
|
|
|
|
const WCHAR* aShortcutPath,
|
|
|
|
const WCHAR* aTargetPath,
|
|
|
|
const WCHAR* aWorkingDir,
|
|
|
|
const WCHAR* aArgs,
|
|
|
|
const WCHAR* aDescription,
|
|
|
|
const WCHAR* aIconPath,
|
|
|
|
int32_t aIconIndex)
|
2012-04-18 15:01:15 -07:00
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
if (!mShellLink) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2012-04-18 15:01:15 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
if (!aShortcutPath) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2012-04-18 15:01:15 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
MutexAutoLock lock(mLock);
|
2012-04-18 15:01:15 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
if (aUpdateExisting) {
|
|
|
|
if (FAILED(mPersistFile->Load(aShortcutPath, STGM_READWRITE))) {
|
2012-04-18 15:01:15 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
} else {
|
2014-05-14 13:15:46 -07:00
|
|
|
if (!aTargetPath) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_FILE_TARGET_DOES_NOT_EXIST;
|
2012-04-18 15:01:15 -07:00
|
|
|
}
|
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
// Since we reuse our IPersistFile, we have to clear out any values that
|
|
|
|
// may be left over from previous calls to SetShortcut.
|
|
|
|
if (FAILED(mShellLink->SetWorkingDirectory(L""))
|
|
|
|
|| FAILED(mShellLink->SetArguments(L""))
|
|
|
|
|| FAILED(mShellLink->SetDescription(L""))
|
|
|
|
|| FAILED(mShellLink->SetIconLocation(L"", 0))) {
|
2012-04-18 15:01:15 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
}
|
2012-04-18 15:01:15 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
if (aTargetPath && FAILED(mShellLink->SetPath(aTargetPath))) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2012-04-18 15:01:15 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
if (aWorkingDir && FAILED(mShellLink->SetWorkingDirectory(aWorkingDir))) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2012-04-18 15:01:15 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
if (aArgs && FAILED(mShellLink->SetArguments(aArgs))) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2012-04-18 15:01:15 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
if (aDescription && FAILED(mShellLink->SetDescription(aDescription))) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
if (aIconPath && FAILED(mShellLink->SetIconLocation(aIconPath, aIconIndex))) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
if (FAILED(mPersistFile->Save(aShortcutPath,
|
2014-05-05 10:30:43 -07:00
|
|
|
TRUE))) {
|
|
|
|
// Second argument indicates whether the file path specified in the
|
|
|
|
// first argument should become the "current working file" for this
|
|
|
|
// IPersistFile
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
2012-04-18 15:01:15 -07:00
|
|
|
}
|
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
static ShortcutResolver* gResolver = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
static nsresult
|
|
|
|
NS_CreateShortcutResolver()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
gResolver = new ShortcutResolver();
|
2014-05-14 13:15:46 -07:00
|
|
|
if (!gResolver) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
return gResolver->Init();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
static void
|
|
|
|
NS_DestroyShortcutResolver()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
delete gResolver;
|
|
|
|
gResolver = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2008-10-08 20:51:54 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// static helper functions
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// certainly not all the error that can be
|
|
|
|
// encountered, but many of them common ones
|
2014-05-14 13:15:46 -07:00
|
|
|
static nsresult
|
|
|
|
ConvertWinError(DWORD aWinErr)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
nsresult rv;
|
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
switch (aWinErr) {
|
2014-05-05 10:30:43 -07:00
|
|
|
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_SHARING_VIOLATION: // CreateFile without sharing flags
|
|
|
|
case ERROR_LOCK_VIOLATION: // LockFile, LockFileEx
|
|
|
|
rv = NS_ERROR_FILE_IS_LOCKED;
|
|
|
|
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;
|
|
|
|
case ERROR_FILE_EXISTS:
|
|
|
|
case ERROR_ALREADY_EXISTS:
|
|
|
|
case ERROR_CANNOT_MAKE:
|
|
|
|
rv = NS_ERROR_FILE_ALREADY_EXISTS;
|
|
|
|
break;
|
|
|
|
case ERROR_FILENAME_EXCED_RANGE:
|
|
|
|
rv = NS_ERROR_FILE_NAME_TOO_LONG;
|
|
|
|
break;
|
|
|
|
case ERROR_DIRECTORY:
|
|
|
|
rv = NS_ERROR_FILE_NOT_DIRECTORY;
|
|
|
|
break;
|
|
|
|
case 0:
|
|
|
|
rv = NS_OK;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
rv = NS_ERROR_FAILURE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// as suggested in the MSDN documentation on SetFilePointer
|
2014-05-05 10:30:43 -07:00
|
|
|
static __int64
|
2007-03-22 10:30:00 -07:00
|
|
|
MyFileSeek64(HANDLE aHandle, __int64 aDistance, DWORD aMoveMethod)
|
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
LARGE_INTEGER li;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
li.QuadPart = aDistance;
|
|
|
|
li.LowPart = SetFilePointer(aHandle, li.LowPart, &li.HighPart, aMoveMethod);
|
2014-05-14 13:15:46 -07:00
|
|
|
if (li.LowPart == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) {
|
2014-05-05 10:30:43 -07:00
|
|
|
li.QuadPart = -1;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
return li.QuadPart;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
static bool
|
2014-05-14 13:15:46 -07:00
|
|
|
IsShortcutPath(const nsAString& aPath)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
// Under Windows, the shortcuts are just files with a ".lnk" extension.
|
|
|
|
// Note also that we don't resolve links in the middle of paths.
|
|
|
|
// i.e. "c:\foo.lnk\bar.txt" is invalid.
|
2014-05-14 13:15:46 -07:00
|
|
|
NS_ABORT_IF_FALSE(!aPath.IsEmpty(), "don't pass an empty string");
|
|
|
|
int32_t len = aPath.Length();
|
|
|
|
return len >= 4 && (StringTail(aPath, 4).LowerCaseEqualsASCII(".lnk"));
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2014-05-05 10:30:43 -07:00
|
|
|
// We need the following three definitions to make |OpenFile| convert a file
|
2007-03-22 10:30:00 -07:00
|
|
|
// handle to an NSPR file descriptor correctly when |O_APPEND| flag is
|
|
|
|
// specified. It is defined in a private header of NSPR (primpl.h) we can't
|
|
|
|
// include. As a temporary workaround until we decide how to extend
|
|
|
|
// |PR_ImportFile|, we define it here. Currently, |_PR_HAVE_PEEK_BUFFER|
|
|
|
|
// and |PR_STRICT_ADDR_LEN| are not defined for the 'w95'-dependent portion
|
|
|
|
// of NSPR so that fields of |PRFilePrivate| #ifdef'd by them are not copied.
|
|
|
|
// Similarly, |_MDFileDesc| is taken from nsprpub/pr/include/md/_win95.h.
|
2014-05-05 10:30:43 -07:00
|
|
|
// In an unlikely case we switch to 'NT'-dependent NSPR AND this temporary
|
|
|
|
// workaround last beyond the switch, |PRFilePrivate| and |_MDFileDesc|
|
2007-03-22 10:30:00 -07:00
|
|
|
// need to be changed to match the definitions for WinNT.
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
typedef enum {
|
2014-05-05 10:30:43 -07:00
|
|
|
_PR_TRI_TRUE = 1,
|
|
|
|
_PR_TRI_FALSE = 0,
|
|
|
|
_PR_TRI_UNKNOWN = -1
|
2007-03-22 10:30:00 -07:00
|
|
|
} _PRTriStateBool;
|
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
struct _MDFileDesc
|
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
PROsfd osfd;
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
struct PRFilePrivate
|
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
int32_t state;
|
|
|
|
bool nonblocking;
|
|
|
|
_PRTriStateBool inheritable;
|
2014-05-14 13:15:46 -07:00
|
|
|
PRFileDesc* next;
|
2014-05-05 10:30:43 -07:00
|
|
|
int lockCount; /* 0: not locked
|
|
|
|
* -1: a native lockfile call is in progress
|
|
|
|
* > 0: # times the file is locked */
|
|
|
|
bool appendMode;
|
|
|
|
_MDFileDesc md;
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Six static methods defined below (OpenFile, FileTimeToPRTime, GetFileInfo,
|
2014-05-05 10:30:43 -07:00
|
|
|
// OpenDir, CloseDir, ReadDir) should go away once the corresponding
|
|
|
|
// UTF-16 APIs are implemented on all the supported platforms (or at least
|
|
|
|
// Windows 9x/ME) in NSPR. Currently, they're only implemented on
|
2007-03-22 10:30:00 -07:00
|
|
|
// Windows NT4 or later. (bug 330665)
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
// copied from nsprpub/pr/src/{io/prfile.c | md/windows/w95io.c} :
|
2007-03-22 10:30:00 -07:00
|
|
|
// PR_Open and _PR_MD_OPEN
|
2014-03-14 07:13:26 -07:00
|
|
|
nsresult
|
2014-05-14 13:15:46 -07:00
|
|
|
OpenFile(const nsAFlatString& aName, int aOsflags, int aMode, PRFileDesc** aFd)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
int32_t access = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
int32_t disposition = 0;
|
|
|
|
int32_t attributes = 0;
|
2012-11-03 10:39:02 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
if (aOsflags & PR_SYNC) {
|
2014-05-05 10:30:43 -07:00
|
|
|
attributes = FILE_FLAG_WRITE_THROUGH;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
|
|
|
if (aOsflags & PR_RDONLY || aOsflags & PR_RDWR) {
|
2014-05-05 10:30:43 -07:00
|
|
|
access |= GENERIC_READ;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
|
|
|
if (aOsflags & PR_WRONLY || aOsflags & PR_RDWR) {
|
2014-05-05 10:30:43 -07:00
|
|
|
access |= GENERIC_WRITE;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
if (aOsflags & PR_CREATE_FILE && aOsflags & PR_EXCL) {
|
2014-05-05 10:30:43 -07:00
|
|
|
disposition = CREATE_NEW;
|
2014-05-14 13:15:46 -07:00
|
|
|
} else if (aOsflags & PR_CREATE_FILE) {
|
|
|
|
if (aOsflags & PR_TRUNCATE) {
|
2014-05-05 10:30:43 -07:00
|
|
|
disposition = CREATE_ALWAYS;
|
2014-05-14 13:15:46 -07:00
|
|
|
} else {
|
2014-05-05 10:30:43 -07:00
|
|
|
disposition = OPEN_ALWAYS;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
} else {
|
2014-05-14 13:15:46 -07:00
|
|
|
if (aOsflags & PR_TRUNCATE) {
|
2014-05-05 10:30:43 -07:00
|
|
|
disposition = TRUNCATE_EXISTING;
|
2014-05-14 13:15:46 -07:00
|
|
|
} else {
|
2014-05-05 10:30:43 -07:00
|
|
|
disposition = OPEN_EXISTING;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
if (aOsflags & nsIFile::DELETE_ON_CLOSE) {
|
2014-05-05 10:30:43 -07:00
|
|
|
attributes |= FILE_FLAG_DELETE_ON_CLOSE;
|
|
|
|
}
|
2009-03-19 13:56:05 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
if (aOsflags & nsIFile::OS_READAHEAD) {
|
2014-05-05 10:30:43 -07:00
|
|
|
attributes |= FILE_FLAG_SEQUENTIAL_SCAN;
|
|
|
|
}
|
2012-11-03 10:39:02 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
// If no write permissions are requested, and if we are possibly creating
|
|
|
|
// the file, then set the new file as read only.
|
|
|
|
// The flag has no effect if we happen to open the file.
|
2014-05-14 13:15:46 -07:00
|
|
|
if (!(aMode & (PR_IWUSR | PR_IWGRP | PR_IWOTH)) &&
|
2014-05-05 10:30:43 -07:00
|
|
|
disposition != OPEN_EXISTING) {
|
|
|
|
attributes |= FILE_ATTRIBUTE_READONLY;
|
|
|
|
}
|
2011-10-02 12:24:54 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
HANDLE file = ::CreateFileW(aName.get(), access,
|
|
|
|
FILE_SHARE_READ | FILE_SHARE_WRITE,
|
2014-05-05 10:30:43 -07:00
|
|
|
nullptr, disposition, attributes, nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
if (file == INVALID_HANDLE_VALUE) {
|
2014-05-14 13:15:46 -07:00
|
|
|
*aFd = nullptr;
|
2014-05-05 10:30:43 -07:00
|
|
|
return ConvertWinError(GetLastError());
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
*aFd = PR_ImportFile((PROsfd) file);
|
|
|
|
if (*aFd) {
|
2014-05-05 10:30:43 -07:00
|
|
|
// On Windows, _PR_HAVE_O_APPEND is not defined so that we have to
|
|
|
|
// add it manually. (see |PR_Open| in nsprpub/pr/src/io/prfile.c)
|
2014-05-14 13:15:46 -07:00
|
|
|
(*aFd)->secret->appendMode = (PR_APPEND & aOsflags) ? true : false;
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
nsresult rv = NS_ErrorAccordingToNSPR();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
CloseHandle(file);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
return rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// copied from nsprpub/pr/src/{io/prfile.c | md/windows/w95io.c} :
|
|
|
|
// PR_FileTimeToPRTime and _PR_FileTimeToPRTime
|
2014-05-14 13:15:46 -07:00
|
|
|
static void
|
|
|
|
FileTimeToPRTime(const FILETIME* aFiletime, PRTime* aPrtm)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
#ifdef __GNUC__
|
2014-05-05 10:30:43 -07:00
|
|
|
const PRTime _pr_filetime_offset = 116444736000000000LL;
|
2007-03-22 10:30:00 -07:00
|
|
|
#else
|
2014-05-05 10:30:43 -07:00
|
|
|
const PRTime _pr_filetime_offset = 116444736000000000i64;
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif
|
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
PR_ASSERT(sizeof(FILETIME) == sizeof(PRTime));
|
2014-05-14 13:15:46 -07:00
|
|
|
::CopyMemory(aPrtm, aFiletime, sizeof(PRTime));
|
2007-03-22 10:30:00 -07:00
|
|
|
#ifdef __GNUC__
|
2014-05-14 13:15:46 -07:00
|
|
|
*aPrtm = (*aPrtm - _pr_filetime_offset) / 10LL;
|
2007-03-22 10:30:00 -07:00
|
|
|
#else
|
2014-05-14 13:15:46 -07:00
|
|
|
*aPrtm = (*aPrtm - _pr_filetime_offset) / 10i64;
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// copied from nsprpub/pr/src/{io/prfile.c | md/windows/w95io.c} with some
|
|
|
|
// changes : PR_GetFileInfo64, _PR_MD_GETFILEINFO64
|
|
|
|
static nsresult
|
2014-05-14 13:15:46 -07:00
|
|
|
GetFileInfo(const nsAFlatString& aName, PRFileInfo64* aInfo)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
WIN32_FILE_ATTRIBUTE_DATA fileData;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
if (aName.IsEmpty() || aName.FindCharInSet(MOZ_UTF16("?*")) != kNotFound) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
if (!::GetFileAttributesExW(aName.get(), GetFileExInfoStandard, &fileData)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return ConvertWinError(GetLastError());
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
if (fileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
|
2014-05-14 13:15:46 -07:00
|
|
|
aInfo->type = PR_FILE_DIRECTORY;
|
2014-05-05 10:30:43 -07:00
|
|
|
} else {
|
2014-05-14 13:15:46 -07:00
|
|
|
aInfo->type = PR_FILE_FILE;
|
2014-05-05 10:30:43 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
aInfo->size = fileData.nFileSizeHigh;
|
|
|
|
aInfo->size = (aInfo->size << 32) + fileData.nFileSizeLow;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
FileTimeToPRTime(&fileData.ftLastWriteTime, &aInfo->modifyTime);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
if (0 == fileData.ftCreationTime.dwLowDateTime &&
|
|
|
|
0 == fileData.ftCreationTime.dwHighDateTime) {
|
2014-05-14 13:15:46 -07:00
|
|
|
aInfo->creationTime = aInfo->modifyTime;
|
2014-05-05 10:30:43 -07:00
|
|
|
} else {
|
2014-05-14 13:15:46 -07:00
|
|
|
FileTimeToPRTime(&fileData.ftCreationTime, &aInfo->creationTime);
|
2014-05-05 10:30:43 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
struct nsDir
|
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
HANDLE handle;
|
|
|
|
WIN32_FIND_DATAW data;
|
|
|
|
bool firstEntry;
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
static nsresult
|
2014-05-14 13:15:46 -07:00
|
|
|
OpenDir(const nsAFlatString& aName, nsDir** aDir)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_WARN_IF(!aDir)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
*aDir = nullptr;
|
|
|
|
if (aName.Length() + 3 >= MAX_PATH) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_FILE_NAME_TOO_LONG;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
nsDir* d = PR_NEW(nsDir);
|
|
|
|
if (!d) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
nsAutoString filename(aName);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
// If |aName| ends in a slash or backslash, do not append another backslash.
|
|
|
|
if (filename.Last() == L'/' || filename.Last() == L'\\') {
|
2014-05-05 10:30:43 -07:00
|
|
|
filename.Append('*');
|
2014-05-14 13:15:46 -07:00
|
|
|
} else {
|
2014-05-05 10:30:43 -07:00
|
|
|
filename.AppendLiteral("\\*");
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
filename.ReplaceChar(L'/', L'\\');
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
// FindFirstFileW Will have a last error of ERROR_DIRECTORY if
|
|
|
|
// <file_path>\* is passed in. If <unknown_path>\* is passed in then
|
|
|
|
// ERROR_PATH_NOT_FOUND will be the last error.
|
2014-05-14 13:15:46 -07:00
|
|
|
d->handle = ::FindFirstFileW(filename.get(), &(d->data));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
if (d->handle == INVALID_HANDLE_VALUE) {
|
|
|
|
PR_Free(d);
|
|
|
|
return ConvertWinError(GetLastError());
|
|
|
|
}
|
|
|
|
d->firstEntry = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
*aDir = d;
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static nsresult
|
2014-05-14 13:15:46 -07:00
|
|
|
ReadDir(nsDir* aDir, PRDirFlags aFlags, nsString& aName)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-14 13:15:46 -07:00
|
|
|
aName.Truncate();
|
|
|
|
if (NS_WARN_IF(!aDir)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
while (1) {
|
|
|
|
BOOL rv;
|
2014-05-14 13:15:46 -07:00
|
|
|
if (aDir->firstEntry) {
|
|
|
|
aDir->firstEntry = false;
|
2014-05-05 10:30:43 -07:00
|
|
|
rv = 1;
|
2014-05-14 13:15:46 -07:00
|
|
|
} else {
|
|
|
|
rv = ::FindNextFileW(aDir->handle, &(aDir->data));
|
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
if (rv == 0) {
|
2014-05-05 10:30:43 -07:00
|
|
|
break;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
const wchar_t* fileName;
|
2014-05-05 10:30:43 -07:00
|
|
|
nsString tmp;
|
2014-05-14 13:15:46 -07:00
|
|
|
fileName = (aDir)->data.cFileName;
|
2014-05-05 10:30:43 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
if ((aFlags & PR_SKIP_DOT) &&
|
|
|
|
(fileName[0] == L'.') && (fileName[1] == L'\0')) {
|
2014-05-05 10:30:43 -07:00
|
|
|
continue;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
|
|
|
if ((aFlags & PR_SKIP_DOT_DOT) &&
|
2014-05-05 10:30:43 -07:00
|
|
|
(fileName[0] == L'.') && (fileName[1] == L'.') &&
|
2014-05-14 13:15:46 -07:00
|
|
|
(fileName[2] == L'\0')) {
|
2014-05-05 10:30:43 -07:00
|
|
|
continue;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
DWORD attrib = aDir->data.dwFileAttributes;
|
|
|
|
if ((aFlags & PR_SKIP_HIDDEN) && (attrib & FILE_ATTRIBUTE_HIDDEN)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
continue;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
if (fileName == tmp.get()) {
|
|
|
|
aName = tmp;
|
|
|
|
} else {
|
|
|
|
aName = fileName;
|
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
DWORD err = GetLastError();
|
|
|
|
return err == ERROR_NO_MORE_FILES ? NS_OK : ConvertWinError(err);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static nsresult
|
2014-05-14 13:15:46 -07:00
|
|
|
CloseDir(nsDir*& aDir)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_WARN_IF(!aDir)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
BOOL isOk = FindClose(aDir->handle);
|
2014-05-05 10:30:43 -07:00
|
|
|
// PR_DELETE also nulls out the passed in pointer.
|
2014-05-14 13:15:46 -07:00
|
|
|
PR_DELETE(aDir);
|
2014-05-05 10:30:43 -07:00
|
|
|
return isOk ? NS_OK : ConvertWinError(GetLastError());
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsDirEnumerator
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
class nsDirEnumerator MOZ_FINAL
|
|
|
|
: public nsISimpleEnumerator
|
|
|
|
, public nsIDirectoryEnumerator
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
public:
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
NS_DECL_ISUPPORTS
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
nsDirEnumerator() : mDir(nullptr)
|
|
|
|
{
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
nsresult Init(nsIFile* aParent)
|
2014-05-05 10:30:43 -07:00
|
|
|
{
|
|
|
|
nsAutoString filepath;
|
2014-05-14 13:15:46 -07:00
|
|
|
aParent->GetTarget(filepath);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
if (filepath.IsEmpty()) {
|
|
|
|
aParent->GetPath(filepath);
|
2014-05-05 10:30:43 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
if (filepath.IsEmpty()) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
// IsDirectory is not needed here because OpenDir will return
|
|
|
|
// NS_ERROR_FILE_NOT_DIRECTORY if the passed in path is a file.
|
|
|
|
nsresult rv = OpenDir(filepath, &mDir);
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return rv;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
mParent = aParent;
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
NS_IMETHOD HasMoreElements(bool* aResult)
|
2014-05-05 10:30:43 -07:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2014-05-14 13:15:46 -07:00
|
|
|
if (!mNext && mDir) {
|
2014-05-05 10:30:43 -07:00
|
|
|
nsString name;
|
|
|
|
rv = ReadDir(mDir, PR_SKIP_BOTH, name);
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return rv;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
|
|
|
if (name.IsEmpty()) {
|
2014-05-05 10:30:43 -07:00
|
|
|
// end of dir entries
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_FAILED(CloseDir(mDir))) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
*aResult = false;
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
nsCOMPtr<nsIFile> file;
|
|
|
|
rv = mParent->Clone(getter_AddRefs(file));
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return rv;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
|
|
|
|
rv = file->Append(name);
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return rv;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
|
|
|
|
mNext = do_QueryInterface(file);
|
|
|
|
}
|
2014-05-14 13:15:46 -07:00
|
|
|
*aResult = mNext != nullptr;
|
|
|
|
if (!*aResult) {
|
2014-05-05 10:30:43 -07:00
|
|
|
Close();
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
NS_IMETHOD GetNext(nsISupports** aResult)
|
2014-05-05 10:30:43 -07:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
bool hasMore;
|
|
|
|
rv = HasMoreElements(&hasMore);
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
*aResult = mNext; // might return nullptr
|
|
|
|
NS_IF_ADDREF(*aResult);
|
2014-05-05 10:30:43 -07:00
|
|
|
|
|
|
|
mNext = nullptr;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
NS_IMETHOD GetNextFile(nsIFile** aResult)
|
2014-05-05 10:30:43 -07:00
|
|
|
{
|
2014-05-14 13:15:46 -07:00
|
|
|
*aResult = nullptr;
|
2014-05-05 10:30:43 -07:00
|
|
|
bool hasMore = false;
|
|
|
|
nsresult rv = HasMoreElements(&hasMore);
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_FAILED(rv) || !hasMore) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return rv;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
|
|
|
*aResult = mNext;
|
|
|
|
NS_IF_ADDREF(*aResult);
|
2014-05-05 10:30:43 -07:00
|
|
|
mNext = nullptr;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Close()
|
|
|
|
{
|
2014-05-14 13:15:46 -07:00
|
|
|
if (mDir) {
|
2014-05-05 10:30:43 -07:00
|
|
|
nsresult rv = CloseDir(mDir);
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "close failed");
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// dtor can be non-virtual since there are no subclasses, but must be
|
|
|
|
// public to use the class on the stack.
|
|
|
|
~nsDirEnumerator()
|
|
|
|
{
|
|
|
|
Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
nsDir* mDir;
|
|
|
|
nsCOMPtr<nsIFile> mParent;
|
|
|
|
nsCOMPtr<nsIFile> mNext;
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
2014-04-27 00:06:00 -07:00
|
|
|
NS_IMPL_ISUPPORTS(nsDirEnumerator, nsISimpleEnumerator, nsIDirectoryEnumerator)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsLocalFile <public>
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
nsLocalFile::nsLocalFile()
|
2011-10-17 07:59:28 -07:00
|
|
|
: mDirty(true)
|
2012-02-08 13:48:23 -08:00
|
|
|
, mResolveDirty(true)
|
2011-10-17 07:59:28 -07:00
|
|
|
, mFollowSymlinks(false)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-06-10 11:11:11 -07:00
|
|
|
nsresult
|
2014-05-14 13:15:46 -07:00
|
|
|
nsLocalFile::nsLocalFileConstructor(nsISupports* aOuter, const nsIID& aIID,
|
|
|
|
void** aInstancePtr)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_WARN_IF(!aInstancePtr)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
|
|
|
if (NS_WARN_IF(aOuter)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_NO_AGGREGATION;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
nsLocalFile* inst = new nsLocalFile();
|
|
|
|
if (inst == nullptr)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
nsresult rv = inst->QueryInterface(aIID, aInstancePtr);
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
delete inst;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsLocalFile::nsISupports
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2014-04-27 00:06:00 -07:00
|
|
|
NS_IMPL_ISUPPORTS(nsLocalFile,
|
|
|
|
nsILocalFile,
|
|
|
|
nsIFile,
|
|
|
|
nsILocalFileWin,
|
|
|
|
nsIHashable)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsLocalFile <private>
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
nsLocalFile::nsLocalFile(const nsLocalFile& aOther)
|
2011-10-17 07:59:28 -07:00
|
|
|
: mDirty(true)
|
2012-02-08 13:48:23 -08:00
|
|
|
, mResolveDirty(true)
|
2014-05-14 13:15:46 -07:00
|
|
|
, mFollowSymlinks(aOther.mFollowSymlinks)
|
|
|
|
, mWorkingPath(aOther.mWorkingPath)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
// Resolve the shortcut file from mWorkingPath and write the path
|
2007-03-22 10:30:00 -07:00
|
|
|
// it points to into mResolvedPath.
|
|
|
|
nsresult
|
|
|
|
nsLocalFile::ResolveShortcut()
|
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
// we can't do anything without the resolver
|
2014-05-14 13:15:46 -07:00
|
|
|
if (!gResolver) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
mResolvedPath.SetLength(MAX_PATH);
|
2014-05-14 13:15:46 -07:00
|
|
|
if (mResolvedPath.Length() != MAX_PATH) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
wchar_t* resolvedPath = wwc(mResolvedPath.BeginWriting());
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
// resolve this shortcut
|
|
|
|
nsresult rv = gResolver->Resolve(mWorkingPath.get(), resolvedPath);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
size_t len = NS_FAILED(rv) ? 0 : wcslen(resolvedPath);
|
|
|
|
mResolvedPath.SetLength(len);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
return rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Resolve any shortcuts and stat the resolved path. After a successful return
|
|
|
|
// the path is guaranteed valid and the members of mFileInfo64 can be used.
|
|
|
|
nsresult
|
|
|
|
nsLocalFile::ResolveAndStat()
|
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
// if we aren't dirty then we are already done
|
2014-05-14 13:15:46 -07:00
|
|
|
if (!mDirty) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_OK;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
// we can't resolve/stat anything that isn't a valid NSPR addressable path
|
2014-05-14 13:15:46 -07:00
|
|
|
if (mWorkingPath.IsEmpty()) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_FILE_INVALID_PATH;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
// this is usually correct
|
|
|
|
mResolvedPath.Assign(mWorkingPath);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
// slutty hack designed to work around bug 134796 until it is fixed
|
|
|
|
nsAutoString nsprPath(mWorkingPath.get());
|
2014-05-14 13:15:46 -07:00
|
|
|
if (mWorkingPath.Length() == 2 && mWorkingPath.CharAt(1) == L':') {
|
2014-05-05 10:30:43 -07:00
|
|
|
nsprPath.Append('\\');
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
// first we will see if the working path exists. If it doesn't then
|
|
|
|
// there is nothing more that can be done
|
|
|
|
nsresult rv = GetFileInfo(nsprPath, &mFileInfo64);
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return rv;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
// if this isn't a shortcut file or we aren't following symlinks then we're done
|
2014-05-14 13:15:46 -07:00
|
|
|
if (!mFollowSymlinks ||
|
|
|
|
mFileInfo64.type != PR_FILE_FILE ||
|
|
|
|
!IsShortcutPath(mWorkingPath)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
mDirty = false;
|
2012-02-08 13:48:23 -08:00
|
|
|
mResolveDirty = false;
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
// we need to resolve this shortcut to what it points to, this will
|
|
|
|
// set mResolvedPath. Even if it fails we need to have the resolved
|
|
|
|
// path equal to working path for those functions that always use
|
|
|
|
// the resolved path.
|
|
|
|
rv = ResolveShortcut();
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
mResolvedPath.Assign(mWorkingPath);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
mResolveDirty = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
// get the details of the resolved path
|
|
|
|
rv = GetFileInfo(mResolvedPath, &mFileInfo64);
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return rv;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
|
|
|
|
mDirty = false;
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-02-08 13:48:23 -08:00
|
|
|
/**
|
|
|
|
* Fills the mResolvedPath member variable with the file or symlink target
|
|
|
|
* if follow symlinks is on. This is a copy of the Resolve parts from
|
|
|
|
* ResolveAndStat. ResolveAndStat is much slower though because of the stat.
|
|
|
|
*
|
|
|
|
* @return NS_OK on success.
|
|
|
|
*/
|
|
|
|
nsresult
|
|
|
|
nsLocalFile::Resolve()
|
|
|
|
{
|
|
|
|
// if we aren't dirty then we are already done
|
|
|
|
if (!mResolveDirty) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// we can't resolve/stat anything that isn't a valid NSPR addressable path
|
|
|
|
if (mWorkingPath.IsEmpty()) {
|
|
|
|
return NS_ERROR_FILE_INVALID_PATH;
|
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
|
2012-02-08 13:48:23 -08:00
|
|
|
// this is usually correct
|
|
|
|
mResolvedPath.Assign(mWorkingPath);
|
|
|
|
|
|
|
|
// if this isn't a shortcut file or we aren't following symlinks then
|
|
|
|
// we're done.
|
2014-05-05 10:30:43 -07:00
|
|
|
if (!mFollowSymlinks ||
|
2012-02-08 13:48:23 -08:00
|
|
|
!IsShortcutPath(mWorkingPath)) {
|
|
|
|
mResolveDirty = false;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// we need to resolve this shortcut to what it points to, this will
|
|
|
|
// set mResolvedPath. Even if it fails we need to have the resolved
|
|
|
|
// path equal to working path for those functions that always use
|
|
|
|
// the resolved path.
|
|
|
|
nsresult rv = ResolveShortcut();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
mResolvedPath.Assign(mWorkingPath);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
mResolveDirty = false;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsLocalFile::nsIFile,nsILocalFile
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-14 13:15:46 -07:00
|
|
|
nsLocalFile::Clone(nsIFile** aFile)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
// Just copy-construct ourselves
|
2014-05-14 13:15:46 -07:00
|
|
|
*aFile = new nsLocalFile(*this);
|
|
|
|
if (!*aFile) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
NS_ADDREF(*aFile);
|
2014-05-05 10:30:43 -07:00
|
|
|
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-14 13:15:46 -07:00
|
|
|
nsLocalFile::InitWithFile(nsIFile* aFile)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_WARN_IF(!aFile)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
|
|
|
|
nsAutoString path;
|
|
|
|
aFile->GetPath(path);
|
2014-05-14 13:15:46 -07:00
|
|
|
if (path.IsEmpty()) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
return InitWithPath(path);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-14 13:15:46 -07:00
|
|
|
nsLocalFile::InitWithPath(const nsAString& aFilePath)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
MakeDirty();
|
2008-10-09 12:23:22 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
nsAString::const_iterator begin, end;
|
2014-05-14 13:15:46 -07:00
|
|
|
aFilePath.BeginReading(begin);
|
|
|
|
aFilePath.EndReading(end);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
// input string must not be empty
|
2014-05-14 13:15:46 -07:00
|
|
|
if (begin == end) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
char16_t firstChar = *begin;
|
|
|
|
char16_t secondChar = *(++begin);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07: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.
|
2014-05-14 13:15:46 -07:00
|
|
|
if (FindCharInReadable(L'/', begin, end)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_FILE_UNRECOGNIZED_PATH;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
if (secondChar != L':' && (secondChar != L'\\' || firstChar != L'\\')) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_FILE_UNRECOGNIZED_PATH;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
if (secondChar == L':') {
|
|
|
|
// Make sure we have a valid drive, later code assumes the drive letter
|
|
|
|
// is a single char a-z or A-Z.
|
2014-05-14 13:15:46 -07:00
|
|
|
if (PathGetDriveNumberW(aFilePath.Data()) == -1) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_FILE_UNRECOGNIZED_PATH;
|
2012-02-08 13:41:12 -08:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
}
|
2012-02-08 13:41:12 -08:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
mWorkingPath = aFilePath;
|
2014-05-05 10:30:43 -07:00
|
|
|
// kill any trailing '\'
|
2014-05-14 13:15:46 -07:00
|
|
|
if (mWorkingPath.Last() == L'\\') {
|
2014-05-05 10:30:43 -07:00
|
|
|
mWorkingPath.Truncate(mWorkingPath.Length() - 1);
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-14 13:15:46 -07:00
|
|
|
nsLocalFile::OpenNSPRFileDesc(int32_t aFlags, int32_t aMode,
|
|
|
|
PRFileDesc** aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
nsresult rv = Resolve();
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return rv;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
return OpenFile(mResolvedPath, aFlags, aMode, aResult);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-14 13:15:46 -07:00
|
|
|
nsLocalFile::OpenANSIFileDesc(const char* aMode, FILE** aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
nsresult rv = ResolveAndStat();
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_FAILED(rv) && rv != NS_ERROR_FILE_NOT_FOUND) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return rv;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
*aResult = _wfopen(mResolvedPath.get(), NS_ConvertASCIItoUTF16(aMode).get());
|
|
|
|
if (*aResult) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_OK;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-14 13:15:46 -07:00
|
|
|
nsLocalFile::Create(uint32_t aType, uint32_t aAttributes)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-14 13:15:46 -07:00
|
|
|
if (aType != NORMAL_FILE_TYPE && aType != DIRECTORY_TYPE) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_FILE_UNKNOWN_TYPE;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
nsresult rv = ResolveAndStat();
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_FAILED(rv) && rv != NS_ERROR_FILE_NOT_FOUND) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return rv;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07: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.
|
|
|
|
|
|
|
|
wchar_t* path = wwc(mResolvedPath.BeginWriting());
|
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
if (path[0] == L'\\' && path[1] == L'\\') {
|
2014-05-05 10:30:43 -07:00
|
|
|
// dealing with a UNC path here; skip past '\\machine\'
|
|
|
|
path = wcschr(path + 2, L'\\');
|
2014-05-14 13:15:46 -07:00
|
|
|
if (!path) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_FILE_INVALID_PATH;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
++path;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
// search for first slash after the drive (or volume) name
|
|
|
|
wchar_t* slash = wcschr(path, L'\\');
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
nsresult directoryCreateError = NS_OK;
|
2014-05-14 13:15:46 -07:00
|
|
|
if (slash) {
|
2014-05-05 10:30:43 -07:00
|
|
|
// skip the first '\\'
|
|
|
|
++slash;
|
|
|
|
slash = wcschr(slash, L'\\');
|
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
while (slash) {
|
2014-05-05 10:30:43 -07:00
|
|
|
*slash = L'\0';
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
if (!::CreateDirectoryW(mResolvedPath.get(), nullptr)) {
|
|
|
|
rv = ConvertWinError(GetLastError());
|
|
|
|
if (NS_ERROR_FILE_NOT_FOUND == rv &&
|
|
|
|
NS_ERROR_FILE_ACCESS_DENIED == directoryCreateError) {
|
|
|
|
// If a previous CreateDirectory failed due to access, return that.
|
|
|
|
return NS_ERROR_FILE_ACCESS_DENIED;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07: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.
|
2014-05-14 13:15:46 -07:00
|
|
|
else if (rv != NS_ERROR_FILE_ALREADY_EXISTS &&
|
|
|
|
rv != NS_ERROR_FILE_ACCESS_DENIED) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
directoryCreateError = rv;
|
|
|
|
}
|
|
|
|
*slash = L'\\';
|
|
|
|
++slash;
|
|
|
|
slash = wcschr(slash, L'\\');
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
if (aType == NORMAL_FILE_TYPE) {
|
2014-05-05 10:30:43 -07:00
|
|
|
PRFileDesc* file;
|
|
|
|
rv = OpenFile(mResolvedPath,
|
2014-05-14 13:15:46 -07:00
|
|
|
PR_RDONLY | PR_CREATE_FILE | PR_APPEND | PR_EXCL,
|
|
|
|
aAttributes, &file);
|
|
|
|
if (file) {
|
2014-05-05 10:30:43 -07:00
|
|
|
PR_Close(file);
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2009-01-27 16:50:14 -08:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
if (rv == NS_ERROR_FILE_ACCESS_DENIED) {
|
2014-05-05 10:30:43 -07:00
|
|
|
// need to return already-exists for directories (bug 452217)
|
|
|
|
bool isdir;
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_SUCCEEDED(IsDirectory(&isdir)) && isdir) {
|
2014-05-05 10:30:43 -07:00
|
|
|
rv = NS_ERROR_FILE_ALREADY_EXISTS;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
} else if (NS_ERROR_FILE_NOT_FOUND == rv &&
|
|
|
|
NS_ERROR_FILE_ACCESS_DENIED == directoryCreateError) {
|
|
|
|
// If a previous CreateDirectory failed due to access, return that.
|
|
|
|
return NS_ERROR_FILE_ACCESS_DENIED;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
return rv;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
if (aType == DIRECTORY_TYPE) {
|
2014-05-05 10:30:43 -07:00
|
|
|
if (!::CreateDirectoryW(mResolvedPath.get(), nullptr)) {
|
|
|
|
rv = ConvertWinError(GetLastError());
|
|
|
|
if (NS_ERROR_FILE_NOT_FOUND == rv &&
|
|
|
|
NS_ERROR_FILE_ACCESS_DENIED == directoryCreateError) {
|
|
|
|
// If a previous CreateDirectory failed due to access, return that.
|
|
|
|
return NS_ERROR_FILE_ACCESS_DENIED;
|
|
|
|
}
|
2014-05-14 13:15:46 -07:00
|
|
|
return rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2014-05-14 13:15:46 -07:00
|
|
|
return NS_OK;
|
2014-05-05 10:30:43 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_FILE_UNKNOWN_TYPE;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-14 13:15:46 -07:00
|
|
|
nsLocalFile::Append(const nsAString& aNode)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
// append this path, multiple components are not permitted
|
2014-05-14 13:15:46 -07:00
|
|
|
return AppendInternal(PromiseFlatString(aNode), false);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-14 13:15:46 -07:00
|
|
|
nsLocalFile::AppendRelativePath(const nsAString& aNode)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
// append this path, multiple components are permitted
|
2014-05-14 13:15:46 -07:00
|
|
|
return AppendInternal(PromiseFlatString(aNode), true);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
2014-05-14 13:15:46 -07:00
|
|
|
nsLocalFile::AppendInternal(const nsAFlatString& aNode,
|
|
|
|
bool aMultipleComponents)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-14 13:15:46 -07:00
|
|
|
if (aNode.IsEmpty()) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_OK;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
// check the relative path for validity
|
2014-05-14 13:15:46 -07:00
|
|
|
if (aNode.First() == L'\\' // can't start with an '\'
|
|
|
|
|| aNode.FindChar(L'/') != kNotFound // can't contain /
|
|
|
|
|| aNode.EqualsASCII("..")) { // can't be ..
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_FILE_UNRECOGNIZED_PATH;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
if (aMultipleComponents) {
|
2014-05-05 10:30:43 -07:00
|
|
|
// can't contain .. as a path component. Ensure that the valid components
|
|
|
|
// "foo..foo", "..foo", and "foo.." are not falsely detected,
|
|
|
|
// but the invalid paths "..\", "foo\..", "foo\..\foo",
|
|
|
|
// "..\foo", etc are.
|
|
|
|
NS_NAMED_LITERAL_STRING(doubleDot, "\\..");
|
|
|
|
nsAString::const_iterator start, end, offset;
|
2014-05-14 13:15:46 -07:00
|
|
|
aNode.BeginReading(start);
|
|
|
|
aNode.EndReading(end);
|
2014-05-05 10:30:43 -07:00
|
|
|
offset = end;
|
2014-05-14 13:15:46 -07:00
|
|
|
while (FindInReadable(doubleDot, start, offset)) {
|
|
|
|
if (offset == end || *offset == L'\\') {
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_FILE_UNRECOGNIZED_PATH;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
start = offset;
|
|
|
|
offset = end;
|
|
|
|
}
|
2008-10-09 12:23:22 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
// catches the remaining cases of prefixes
|
2014-05-14 13:15:46 -07:00
|
|
|
if (StringBeginsWith(aNode, NS_LITERAL_STRING("..\\"))) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_FILE_UNRECOGNIZED_PATH;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
}
|
|
|
|
// single components can't contain '\'
|
2014-05-14 13:15:46 -07:00
|
|
|
else if (aNode.FindChar(L'\\') != kNotFound) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_FILE_UNRECOGNIZED_PATH;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
|
|
|
|
MakeDirty();
|
|
|
|
|
2014-05-21 20:48:50 -07:00
|
|
|
mWorkingPath.Append('\\');
|
2014-05-21 21:25:28 -07:00
|
|
|
mWorkingPath.Append(aNode);
|
2014-05-05 10:30:43 -07:00
|
|
|
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#define TOUPPER(u) (((u) >= L'a' && (u) <= L'z') ? \
|
|
|
|
(u) - (L'a' - L'A') : (u))
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLocalFile::Normalize()
|
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
// XXX See bug 187957 comment 18 for possible problems with this implementation.
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
if (mWorkingPath.IsEmpty()) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_OK;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
nsAutoString path(mWorkingPath);
|
|
|
|
|
|
|
|
// find the index of the root backslash for the path. Everything before
|
|
|
|
// this is considered fully normalized and cannot be ascended beyond
|
|
|
|
// using ".." For a local drive this is the first slash (e.g. "c:\").
|
|
|
|
// For a UNC path it is the slash following the share name
|
|
|
|
// (e.g. "\\server\share\").
|
|
|
|
int32_t rootIdx = 2; // default to local drive
|
2014-05-14 13:15:46 -07:00
|
|
|
if (path.First() == L'\\') { // if a share then calculate the rootIdx
|
2014-05-05 10:30:43 -07:00
|
|
|
rootIdx = path.FindChar(L'\\', 2); // skip \\ in front of the server
|
2014-05-14 13:15:46 -07:00
|
|
|
if (rootIdx == kNotFound) {
|
|
|
|
return NS_OK; // already normalized
|
|
|
|
}
|
|
|
|
rootIdx = path.FindChar(L'\\', rootIdx + 1);
|
|
|
|
if (rootIdx == kNotFound) {
|
|
|
|
return NS_OK; // already normalized
|
|
|
|
}
|
|
|
|
} else if (path.CharAt(rootIdx) != L'\\') {
|
2014-05-05 10:30:43 -07:00
|
|
|
// The path has been specified relative to the current working directory
|
|
|
|
// for that drive. To normalize it, the current working directory for
|
|
|
|
// that drive needs to be inserted before the supplied relative path
|
|
|
|
// which will provide an absolute path (and the rootIdx will still be 2).
|
|
|
|
WCHAR cwd[MAX_PATH];
|
2014-05-14 13:15:46 -07:00
|
|
|
WCHAR* pcwd = cwd;
|
2014-05-05 10:30:43 -07:00
|
|
|
int drive = TOUPPER(path.First()) - 'A' + 1;
|
|
|
|
/* We need to worry about IPH, for details read bug 419326.
|
|
|
|
* _getdrives - http://msdn2.microsoft.com/en-us/library/xdhk0xd2.aspx
|
|
|
|
* uses a bitmask, bit 0 is 'a:'
|
|
|
|
* _chdrive - http://msdn2.microsoft.com/en-us/library/0d1409hb.aspx
|
|
|
|
* _getdcwd - http://msdn2.microsoft.com/en-us/library/7t2zk3s4.aspx
|
|
|
|
* take an int, 1 is 'a:'.
|
|
|
|
*
|
|
|
|
* Because of this, we need to do some math. Subtract 1 to convert from
|
|
|
|
* _chdrive/_getdcwd format to _getdrives drive numbering.
|
|
|
|
* Shift left x bits to convert from integer indexing to bitfield indexing.
|
|
|
|
* And of course, we need to find out if the drive is in the bitmask.
|
|
|
|
*
|
|
|
|
* If we're really unlucky, we can still lose, but only if the user
|
|
|
|
* manages to eject the drive between our call to _getdrives() and
|
|
|
|
* our *calls* to _wgetdcwd.
|
|
|
|
*/
|
2014-05-14 13:15:46 -07:00
|
|
|
if (!((1 << (drive - 1)) & _getdrives())) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_FILE_INVALID_PATH;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
|
|
|
if (!_wgetdcwd(drive, pcwd, MAX_PATH)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
pcwd = _wgetdcwd(drive, 0, 0);
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
|
|
|
if (!pcwd) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
nsAutoString currentDir(pcwd);
|
2014-05-14 13:15:46 -07:00
|
|
|
if (pcwd != cwd) {
|
2014-05-05 10:30:43 -07:00
|
|
|
free(pcwd);
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
if (currentDir.Last() == '\\') {
|
2014-05-05 10:30:43 -07:00
|
|
|
path.Replace(0, 2, currentDir);
|
2014-05-14 13:15:46 -07:00
|
|
|
} else {
|
2014-05-05 10:30:43 -07:00
|
|
|
path.Replace(0, 2, currentDir + NS_LITERAL_STRING("\\"));
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
}
|
|
|
|
NS_POSTCONDITION(0 < rootIdx && rootIdx < (int32_t)path.Length(), "rootIdx is invalid");
|
|
|
|
NS_POSTCONDITION(path.CharAt(rootIdx) == '\\', "rootIdx is invalid");
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
// if there is nothing following the root path then it is already normalized
|
2014-05-14 13:15:46 -07:00
|
|
|
if (rootIdx + 1 == (int32_t)path.Length()) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_OK;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
// assign the root
|
2014-05-14 13:15:46 -07:00
|
|
|
const char16_t* pathBuffer = path.get(); // simplify access to the buffer
|
|
|
|
mWorkingPath.SetCapacity(path.Length()); // it won't ever grow longer
|
2014-05-05 10:30:43 -07:00
|
|
|
mWorkingPath.Assign(pathBuffer, rootIdx);
|
|
|
|
|
|
|
|
// Normalize the path components. The actions taken are:
|
|
|
|
//
|
|
|
|
// "\\" condense to single backslash
|
|
|
|
// "." remove from path
|
|
|
|
// ".." up a directory
|
|
|
|
// "..." remove from path (any number of dots > 2)
|
|
|
|
//
|
|
|
|
// The last form is something that Windows 95 and 98 supported and
|
|
|
|
// is a shortcut for changing up multiple directories. Windows XP
|
|
|
|
// and ilk ignore it in a path, as is done here.
|
|
|
|
int32_t len, begin, end = rootIdx;
|
2014-05-14 13:15:46 -07:00
|
|
|
while (end < (int32_t)path.Length()) {
|
2014-05-05 10:30:43 -07:00
|
|
|
// find the current segment (text between the backslashes) to
|
|
|
|
// be examined, this will set the following variables:
|
|
|
|
// begin == index of first char in segment
|
|
|
|
// end == index 1 char after last char in segment
|
|
|
|
// len == length of segment
|
|
|
|
begin = end + 1;
|
|
|
|
end = path.FindChar('\\', begin);
|
2014-05-14 13:15:46 -07:00
|
|
|
if (end == kNotFound) {
|
2014-05-05 10:30:43 -07:00
|
|
|
end = path.Length();
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
len = end - begin;
|
|
|
|
|
|
|
|
// ignore double backslashes
|
2014-05-14 13:15:46 -07:00
|
|
|
if (len == 0) {
|
2014-05-05 10:30:43 -07:00
|
|
|
continue;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
|
|
|
|
// len != 0, and interesting paths always begin with a dot
|
2014-05-14 13:15:46 -07:00
|
|
|
if (pathBuffer[begin] == '.') {
|
2014-05-05 10:30:43 -07:00
|
|
|
// ignore single dots
|
2014-05-14 13:15:46 -07:00
|
|
|
if (len == 1) {
|
2014-05-05 10:30:43 -07:00
|
|
|
continue;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
|
|
|
|
// handle multiple dots
|
2014-05-14 13:15:46 -07:00
|
|
|
if (len >= 2 && pathBuffer[begin + 1] == L'.') {
|
2014-05-05 10:30:43 -07:00
|
|
|
// back up a path component on double dot
|
2014-05-14 13:15:46 -07:00
|
|
|
if (len == 2) {
|
2014-05-05 10:30:43 -07:00
|
|
|
int32_t prev = mWorkingPath.RFindChar('\\');
|
2014-05-14 13:15:46 -07:00
|
|
|
if (prev >= rootIdx) {
|
2014-05-05 10:30:43 -07:00
|
|
|
mWorkingPath.Truncate(prev);
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
continue;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
// length is > 2 and the first two characters are dots.
|
|
|
|
// if the rest of the string is dots, then ignore it.
|
|
|
|
int idx = len - 1;
|
2014-05-14 13:15:46 -07:00
|
|
|
for (; idx >= 2; --idx) {
|
|
|
|
if (pathBuffer[begin + idx] != L'.') {
|
2014-05-05 10:30:43 -07:00
|
|
|
break;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// this is true if the loop above didn't break
|
|
|
|
// and all characters in this segment are dots.
|
2014-05-14 13:15:46 -07:00
|
|
|
if (idx < 2) {
|
2014-05-05 10:30:43 -07:00
|
|
|
continue;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
// add the current component to the path, including the preceding backslash
|
|
|
|
mWorkingPath.Append(pathBuffer + begin - 1, len + 1);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
// kill trailing dots and spaces.
|
|
|
|
int32_t filePathLen = mWorkingPath.Length() - 1;
|
2014-05-14 13:15:46 -07:00
|
|
|
while (filePathLen > 0 && (mWorkingPath[filePathLen] == L' ' ||
|
|
|
|
mWorkingPath[filePathLen] == L'.')) {
|
2014-05-05 10:30:43 -07:00
|
|
|
mWorkingPath.Truncate(filePathLen--);
|
|
|
|
}
|
|
|
|
|
|
|
|
MakeDirty();
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-14 13:15:46 -07:00
|
|
|
nsLocalFile::GetLeafName(nsAString& aLeafName)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
aLeafName.Truncate();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
if (mWorkingPath.IsEmpty()) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_FILE_UNRECOGNIZED_PATH;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
int32_t offset = mWorkingPath.RFindChar(L'\\');
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
// if the working path is just a node without any lashes.
|
2014-05-14 13:15:46 -07:00
|
|
|
if (offset == kNotFound) {
|
2014-05-05 10:30:43 -07:00
|
|
|
aLeafName = mWorkingPath;
|
2014-05-14 13:15:46 -07:00
|
|
|
} else {
|
2014-05-05 10:30:43 -07:00
|
|
|
aLeafName = Substring(mWorkingPath, offset + 1);
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-14 13:15:46 -07:00
|
|
|
nsLocalFile::SetLeafName(const nsAString& aLeafName)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
MakeDirty();
|
2008-10-09 12:23:22 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
if (mWorkingPath.IsEmpty()) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_FILE_UNRECOGNIZED_PATH;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
// cannot use nsCString::RFindChar() due to 0x5c problem
|
|
|
|
int32_t offset = mWorkingPath.RFindChar(L'\\');
|
2014-05-14 13:15:46 -07:00
|
|
|
if (offset) {
|
|
|
|
mWorkingPath.Truncate(offset + 1);
|
2014-05-05 10:30:43 -07:00
|
|
|
}
|
|
|
|
mWorkingPath.Append(aLeafName);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-14 13:15:46 -07:00
|
|
|
nsLocalFile::GetPath(nsAString& aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-14 13:15:46 -07:00
|
|
|
aResult = mWorkingPath;
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-14 13:15:46 -07:00
|
|
|
nsLocalFile::GetCanonicalPath(nsAString& aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
EnsureShortPath();
|
|
|
|
aResult.Assign(mShortWorkingPath);
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
typedef struct
|
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
WORD wLanguage;
|
|
|
|
WORD wCodePage;
|
2007-03-22 10:30:00 -07:00
|
|
|
} LANGANDCODEPAGE;
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-14 13:15:46 -07:00
|
|
|
nsLocalFile::GetVersionInfoField(const char* aField, nsAString& aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
nsresult rv = ResolveAndStat();
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return rv;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
rv = NS_ERROR_FAILURE;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
const WCHAR* path = mFollowSymlinks ? mResolvedPath.get()
|
|
|
|
: mWorkingPath.get();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
DWORD dummy;
|
|
|
|
DWORD size = ::GetFileVersionInfoSizeW(path, &dummy);
|
2014-05-14 13:15:46 -07:00
|
|
|
if (!size) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return rv;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
void* ver = calloc(size, 1);
|
2014-05-14 13:15:46 -07:00
|
|
|
if (!ver) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
if (::GetFileVersionInfoW(path, 0, size, ver)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
LANGANDCODEPAGE* translate = nullptr;
|
|
|
|
UINT pageCount;
|
|
|
|
BOOL queryResult = ::VerQueryValueW(ver, L"\\VarFileInfo\\Translation",
|
|
|
|
(void**)&translate, &pageCount);
|
2014-05-14 13:15:46 -07:00
|
|
|
if (queryResult && translate) {
|
|
|
|
for (int32_t i = 0; i < 2; ++i) {
|
2014-05-05 10:30:43 -07:00
|
|
|
wchar_t subBlock[MAX_PATH];
|
|
|
|
_snwprintf(subBlock, MAX_PATH,
|
|
|
|
L"\\StringFileInfo\\%04x%04x\\%s",
|
|
|
|
(i == 0 ? translate[0].wLanguage
|
|
|
|
: ::GetUserDefaultLangID()),
|
|
|
|
translate[0].wCodePage,
|
|
|
|
NS_ConvertASCIItoUTF16(
|
|
|
|
nsDependentCString(aField)).get());
|
|
|
|
subBlock[MAX_PATH - 1] = 0;
|
|
|
|
LPVOID value = nullptr;
|
|
|
|
UINT size;
|
|
|
|
queryResult = ::VerQueryValueW(ver, subBlock, &value, &size);
|
2014-05-14 13:15:46 -07:00
|
|
|
if (queryResult && value) {
|
|
|
|
aResult.Assign(static_cast<char16_t*>(value));
|
|
|
|
if (!aResult.IsEmpty()) {
|
2014-05-05 10:30:43 -07:00
|
|
|
rv = NS_OK;
|
|
|
|
break;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
}
|
|
|
|
free(ver);
|
|
|
|
|
|
|
|
return rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-04-18 15:01:15 -07:00
|
|
|
NS_IMETHODIMP
|
2014-05-14 13:15:46 -07:00
|
|
|
nsLocalFile::SetShortcut(nsIFile* aTargetFile,
|
|
|
|
nsIFile* aWorkingDir,
|
|
|
|
const char16_t* aArgs,
|
|
|
|
const char16_t* aDescription,
|
|
|
|
nsIFile* aIconFile,
|
|
|
|
int32_t aIconIndex)
|
2012-04-18 15:01:15 -07:00
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
bool exists;
|
|
|
|
nsresult rv = this->Exists(&exists);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2012-04-18 15:01:15 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
const WCHAR* targetFilePath = nullptr;
|
|
|
|
const WCHAR* workingDirPath = nullptr;
|
|
|
|
const WCHAR* iconFilePath = nullptr;
|
2012-04-18 15:01:15 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
nsAutoString targetFilePathAuto;
|
2014-05-14 13:15:46 -07:00
|
|
|
if (aTargetFile) {
|
|
|
|
rv = aTargetFile->GetPath(targetFilePathAuto);
|
2014-05-05 10:30:43 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
2012-04-18 15:01:15 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
targetFilePath = targetFilePathAuto.get();
|
|
|
|
}
|
2012-04-18 15:01:15 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
nsAutoString workingDirPathAuto;
|
2014-05-14 13:15:46 -07:00
|
|
|
if (aWorkingDir) {
|
|
|
|
rv = aWorkingDir->GetPath(workingDirPathAuto);
|
2014-05-05 10:30:43 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
2012-04-18 15:01:15 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
workingDirPath = workingDirPathAuto.get();
|
|
|
|
}
|
2012-04-18 15:01:15 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
nsAutoString iconPathAuto;
|
2014-05-14 13:15:46 -07:00
|
|
|
if (aIconFile) {
|
|
|
|
rv = aIconFile->GetPath(iconPathAuto);
|
2014-05-05 10:30:43 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
2012-04-18 15:01:15 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
iconFilePath = iconPathAuto.get();
|
|
|
|
}
|
2012-04-18 15:01:15 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
rv = gResolver->SetShortcut(exists,
|
|
|
|
mWorkingPath.get(),
|
|
|
|
targetFilePath,
|
|
|
|
workingDirPath,
|
2014-05-14 13:15:46 -07:00
|
|
|
char16ptr_t(aArgs),
|
|
|
|
char16ptr_t(aDescription),
|
2014-05-05 10:30:43 -07:00
|
|
|
iconFilePath,
|
2014-05-14 13:15:46 -07:00
|
|
|
iconFilePath ? aIconIndex : 0);
|
2014-05-05 10:30:43 -07:00
|
|
|
if (targetFilePath && NS_SUCCEEDED(rv)) {
|
|
|
|
MakeDirty();
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
2012-04-18 15:01:15 -07:00
|
|
|
}
|
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
/**
|
2012-02-06 11:11:30 -08:00
|
|
|
* Determines if the drive type for the specified file is rmeote or local.
|
2014-05-05 10:30:43 -07:00
|
|
|
*
|
2012-02-06 11:11:30 -08:00
|
|
|
* @param path The path of the file to check
|
|
|
|
* @param remote Out parameter, on function success holds true if the specified
|
|
|
|
* file path is remote, or false if the file path is local.
|
|
|
|
* @return true on success. The return value implies absolutely nothing about
|
|
|
|
* wether the file is local or remote.
|
|
|
|
*/
|
|
|
|
static bool
|
2014-05-14 13:15:46 -07:00
|
|
|
IsRemoteFilePath(LPCWSTR aPath, bool& aRemote)
|
2012-02-06 11:11:30 -08:00
|
|
|
{
|
|
|
|
// Obtain the parent directory path and make sure it ends with
|
|
|
|
// a trailing backslash.
|
|
|
|
WCHAR dirPath[MAX_PATH + 1] = { 0 };
|
2014-05-14 13:15:46 -07:00
|
|
|
wcsncpy(dirPath, aPath, MAX_PATH);
|
2012-02-06 11:11:30 -08:00
|
|
|
if (!PathRemoveFileSpecW(dirPath)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
size_t len = wcslen(dirPath);
|
|
|
|
// In case the dirPath holds exaclty MAX_PATH and remains unchanged, we
|
|
|
|
// recheck the required length here since we need to terminate it with
|
|
|
|
// a backslash.
|
|
|
|
if (len >= MAX_PATH) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
dirPath[len] = L'\\';
|
|
|
|
dirPath[len + 1] = L'\0';
|
|
|
|
UINT driveType = GetDriveTypeW(dirPath);
|
2014-05-14 13:15:46 -07:00
|
|
|
aRemote = driveType == DRIVE_REMOTE;
|
2012-02-06 11:11:30 -08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult
|
2014-05-14 13:15:46 -07:00
|
|
|
nsLocalFile::CopySingleFile(nsIFile* aSourceFile, nsIFile* aDestParent,
|
|
|
|
const nsAString& aNewName, uint32_t aOptions)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
nsAutoString filePath;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
bool move = aOptions & (Move | Rename);
|
2014-01-06 03:27:11 -08:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
// get the path that we are going to copy to.
|
|
|
|
// Since windows does not know how to auto
|
|
|
|
// resolve shortcuts, we must work with the
|
|
|
|
// target.
|
|
|
|
nsAutoString destPath;
|
2014-05-14 13:15:46 -07:00
|
|
|
aDestParent->GetTarget(destPath);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
destPath.Append('\\');
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
if (aNewName.IsEmpty()) {
|
2014-05-05 10:30:43 -07:00
|
|
|
nsAutoString aFileName;
|
2014-05-14 13:15:46 -07:00
|
|
|
aSourceFile->GetLeafName(aFileName);
|
2014-05-05 10:30:43 -07:00
|
|
|
destPath.Append(aFileName);
|
2014-05-14 13:15:46 -07:00
|
|
|
} else {
|
|
|
|
destPath.Append(aNewName);
|
2014-05-05 10:30:43 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
if (aOptions & FollowSymlinks) {
|
|
|
|
rv = aSourceFile->GetTarget(filePath);
|
|
|
|
if (filePath.IsEmpty()) {
|
|
|
|
rv = aSourceFile->GetPath(filePath);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
rv = aSourceFile->GetPath(filePath);
|
2014-05-05 10:30:43 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return rv;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
// Pass the flag COPY_FILE_NO_BUFFERING to CopyFileEx as we may be copying
|
|
|
|
// to a SMBV2 remote drive. Without this parameter subsequent append mode
|
|
|
|
// file writes can cause the resultant file to become corrupt. We only need to do
|
|
|
|
// this if the major version of Windows is > 5(Only Windows Vista and above
|
|
|
|
// can support SMBV2). With a 7200RPM hard drive:
|
|
|
|
// Copying a 1KB file with COPY_FILE_NO_BUFFERING takes about 30-60ms.
|
|
|
|
// Copying a 1KB file without COPY_FILE_NO_BUFFERING takes < 1ms.
|
|
|
|
// So we only use COPY_FILE_NO_BUFFERING when we have a remote drive.
|
|
|
|
int copyOK;
|
|
|
|
DWORD dwCopyFlags = COPY_FILE_ALLOW_DECRYPTED_DESTINATION;
|
|
|
|
if (IsVistaOrLater()) {
|
|
|
|
bool path1Remote, path2Remote;
|
|
|
|
if (!IsRemoteFilePath(filePath.get(), path1Remote) ||
|
|
|
|
!IsRemoteFilePath(destPath.get(), path2Remote) ||
|
|
|
|
path1Remote || path2Remote) {
|
|
|
|
dwCopyFlags |= COPY_FILE_NO_BUFFERING;
|
2013-06-11 11:01:58 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
}
|
2013-06-11 11:01:58 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
if (!move) {
|
2014-05-05 10:30:43 -07:00
|
|
|
copyOK = ::CopyFileExW(filePath.get(), destPath.get(), nullptr,
|
|
|
|
nullptr, nullptr, dwCopyFlags);
|
2014-05-14 13:15:46 -07:00
|
|
|
} else {
|
|
|
|
copyOK = ::MoveFileExW(filePath.get(), destPath.get(),
|
|
|
|
MOVEFILE_REPLACE_EXISTING);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
// Check if copying the source file to a different volume,
|
|
|
|
// as this could be an SMBV2 mapped drive.
|
2014-05-14 13:15:46 -07:00
|
|
|
if (!copyOK && GetLastError() == ERROR_NOT_SAME_DEVICE) {
|
|
|
|
if (aOptions & Rename) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_FILE_ACCESS_DENIED;
|
|
|
|
}
|
|
|
|
copyOK = CopyFileExW(filePath.get(), destPath.get(), nullptr,
|
|
|
|
nullptr, nullptr, dwCopyFlags);
|
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
if (copyOK) {
|
2014-05-05 10:30:43 -07:00
|
|
|
DeleteFileW(filePath.get());
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2008-08-14 08:48:06 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
}
|
2011-05-01 11:59:24 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
if (!copyOK) { // CopyFileEx and MoveFileEx return zero at failure.
|
2014-05-05 10:30:43 -07:00
|
|
|
rv = ConvertWinError(GetLastError());
|
2014-05-14 13:15:46 -07:00
|
|
|
} else if (move && !(aOptions & SkipNtfsAclReset)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
// Set security permissions to inherit from parent.
|
|
|
|
// Note: propagates to all children: slow for big file trees
|
|
|
|
PACL pOldDACL = nullptr;
|
|
|
|
PSECURITY_DESCRIPTOR pSD = nullptr;
|
|
|
|
::GetNamedSecurityInfoW((LPWSTR)destPath.get(), SE_FILE_OBJECT,
|
|
|
|
DACL_SECURITY_INFORMATION,
|
|
|
|
nullptr, nullptr, &pOldDACL, nullptr, &pSD);
|
|
|
|
if (pOldDACL)
|
|
|
|
::SetNamedSecurityInfoW((LPWSTR)destPath.get(), SE_FILE_OBJECT,
|
|
|
|
DACL_SECURITY_INFORMATION |
|
|
|
|
UNPROTECTED_DACL_SECURITY_INFORMATION,
|
|
|
|
nullptr, nullptr, pOldDACL, nullptr);
|
2014-05-14 13:15:46 -07:00
|
|
|
if (pSD) {
|
2014-05-05 10:30:43 -07:00
|
|
|
LocalFree((HLOCAL)pSD);
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-05-14 13:15:46 -07:00
|
|
|
nsLocalFile::CopyMove(nsIFile* aParentDir, const nsAString& aNewName,
|
|
|
|
uint32_t aOptions)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-14 13:15:46 -07:00
|
|
|
bool move = aOptions & (Move | Rename);
|
|
|
|
bool followSymlinks = aOptions & FollowSymlinks;
|
2014-05-05 10:30:43 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> newParentDir = aParentDir;
|
|
|
|
// 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| option.
|
|
|
|
nsresult rv = ResolveAndStat();
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return rv;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
if (!newParentDir) {
|
2014-05-05 10:30:43 -07:00
|
|
|
// no parent was specified. We must rename.
|
2014-05-14 13:15:46 -07:00
|
|
|
if (aNewName.IsEmpty()) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2014-01-06 03:27:11 -08:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
rv = GetParent(getter_AddRefs(newParentDir));
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return rv;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
}
|
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
if (!newParentDir) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_FILE_DESTINATION_NOT_DIR;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
// make sure it exists and is a directory. Create it if not there.
|
|
|
|
bool exists;
|
|
|
|
newParentDir->Exists(&exists);
|
2014-05-14 13:15:46 -07:00
|
|
|
if (!exists) {
|
2014-05-05 10:30:43 -07:00
|
|
|
rv = newParentDir->Create(DIRECTORY_TYPE, 0644); // TODO, what permissions should we use
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return rv;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
|
|
|
} else {
|
2014-05-05 10:30:43 -07:00
|
|
|
bool isDir;
|
|
|
|
newParentDir->IsDirectory(&isDir);
|
2014-05-14 13:15:46 -07:00
|
|
|
if (!isDir) {
|
|
|
|
if (followSymlinks) {
|
2014-05-05 10:30:43 -07:00
|
|
|
bool isLink;
|
|
|
|
newParentDir->IsSymlink(&isLink);
|
2014-05-14 13:15:46 -07:00
|
|
|
if (isLink) {
|
2014-05-05 10:30:43 -07:00
|
|
|
nsAutoString target;
|
|
|
|
newParentDir->GetTarget(target);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> realDest = new nsLocalFile();
|
|
|
|
if (realDest == nullptr)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
rv = realDest->InitWithPath(target);
|
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return rv;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
return CopyMove(realDest, aNewName, aOptions);
|
2014-05-05 10:30:43 -07:00
|
|
|
}
|
2014-05-14 13:15:46 -07:00
|
|
|
} else {
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_FILE_DESTINATION_NOT_DIR;
|
2014-05-05 10:30:43 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
// Try different ways to move/copy files/directories
|
|
|
|
bool done = false;
|
|
|
|
bool isDir;
|
|
|
|
IsDirectory(&isDir);
|
|
|
|
bool isSymlink;
|
|
|
|
IsSymlink(&isSymlink);
|
|
|
|
|
|
|
|
// Try to move the file or directory, or try to copy a single file (or non-followed symlink)
|
2014-05-14 13:15:46 -07:00
|
|
|
if (move || !isDir || (isSymlink && !followSymlinks)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
// Copy/Move single file, or move a directory
|
|
|
|
if (!aParentDir) {
|
2014-05-14 13:15:46 -07:00
|
|
|
aOptions |= SkipNtfsAclReset;
|
2014-05-05 10:30:43 -07:00
|
|
|
}
|
2014-05-14 13:15:46 -07:00
|
|
|
rv = CopySingleFile(this, newParentDir, aNewName, aOptions);
|
2014-05-05 10:30:43 -07:00
|
|
|
done = NS_SUCCEEDED(rv);
|
|
|
|
// If we are moving a directory and that fails, fallback on directory
|
|
|
|
// enumeration. See bug 231300 for details.
|
2014-05-14 13:15:46 -07:00
|
|
|
if (!done && !(move && isDir)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return rv;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Not able to copy or move directly, so enumerate it
|
2014-05-14 13:15:46 -07:00
|
|
|
if (!done) {
|
2014-05-05 10:30:43 -07:00
|
|
|
// create a new target destination in the new parentDir;
|
|
|
|
nsCOMPtr<nsIFile> target;
|
|
|
|
rv = newParentDir->Clone(getter_AddRefs(target));
|
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return rv;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
|
|
|
|
nsAutoString allocatedNewName;
|
2014-05-14 13:15:46 -07:00
|
|
|
if (aNewName.IsEmpty()) {
|
2014-05-05 10:30:43 -07:00
|
|
|
bool isLink;
|
|
|
|
IsSymlink(&isLink);
|
2014-05-14 13:15:46 -07:00
|
|
|
if (isLink) {
|
2014-05-05 10:30:43 -07:00
|
|
|
nsAutoString temp;
|
|
|
|
GetTarget(temp);
|
|
|
|
int32_t offset = temp.RFindChar(L'\\');
|
2014-05-14 13:15:46 -07:00
|
|
|
if (offset == kNotFound) {
|
2014-05-05 10:30:43 -07:00
|
|
|
allocatedNewName = temp;
|
2014-05-14 13:15:46 -07:00
|
|
|
} else {
|
2014-05-05 10:30:43 -07:00
|
|
|
allocatedNewName = Substring(temp, offset + 1);
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
|
|
|
} else {
|
2014-05-05 10:30:43 -07:00
|
|
|
GetLeafName(allocatedNewName);// this should be the leaf name of the
|
|
|
|
}
|
2014-05-14 13:15:46 -07:00
|
|
|
} else {
|
|
|
|
allocatedNewName = aNewName;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
rv = target->Append(allocatedNewName);
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return rv;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
allocatedNewName.Truncate();
|
|
|
|
|
|
|
|
// check if the destination directory already exists
|
|
|
|
target->Exists(&exists);
|
2014-05-14 13:15:46 -07:00
|
|
|
if (!exists) {
|
2014-05-05 10:30:43 -07:00
|
|
|
// if the destination directory cannot be created, return an error
|
|
|
|
rv = target->Create(DIRECTORY_TYPE, 0644); // TODO, what permissions should we use
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return rv;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
|
|
|
} else {
|
2014-05-05 10:30:43 -07:00
|
|
|
// check if the destination directory is writable and empty
|
|
|
|
bool isWritable;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
target->IsWritable(&isWritable);
|
2014-05-14 13:15:46 -07:00
|
|
|
if (!isWritable) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_FILE_ACCESS_DENIED;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
nsCOMPtr<nsISimpleEnumerator> targetIterator;
|
|
|
|
rv = target->GetDirectoryEntries(getter_AddRefs(targetIterator));
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return rv;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
bool more;
|
|
|
|
targetIterator->HasMoreElements(&more);
|
|
|
|
// return error if target directory is not empty
|
2014-05-14 13:15:46 -07:00
|
|
|
if (more) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_FILE_DIR_NOT_EMPTY;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
nsDirEnumerator dirEnum;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
rv = dirEnum.Init(this);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("dirEnum initialization failed");
|
|
|
|
return rv;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
bool more = false;
|
2014-05-14 13:15:46 -07:00
|
|
|
while (NS_SUCCEEDED(dirEnum.HasMoreElements(&more)) && more) {
|
2014-05-05 10:30:43 -07:00
|
|
|
nsCOMPtr<nsISupports> item;
|
|
|
|
nsCOMPtr<nsIFile> file;
|
|
|
|
dirEnum.GetNext(getter_AddRefs(item));
|
|
|
|
file = do_QueryInterface(item);
|
2014-05-14 13:15:46 -07:00
|
|
|
if (file) {
|
2014-05-05 10:30:43 -07:00
|
|
|
bool isDir, isLink;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
file->IsDirectory(&isDir);
|
|
|
|
file->IsSymlink(&isLink);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
if (move) {
|
|
|
|
if (followSymlinks) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
|
|
|
|
rv = file->MoveTo(target, EmptyString());
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return rv;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (followSymlinks) {
|
2014-05-05 10:30:43 -07:00
|
|
|
rv = file->CopyToFollowingLinks(target, EmptyString());
|
2014-05-14 13:15:46 -07:00
|
|
|
} else {
|
2014-05-05 10:30:43 -07:00
|
|
|
rv = file->CopyTo(target, EmptyString());
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
|
|
|
if (NS_FAILED(rv)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return rv;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07: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.
|
2014-05-14 13:15:46 -07:00
|
|
|
if (move) {
|
2014-05-05 10:30:43 -07:00
|
|
|
rv = Remove(false /* recursive */);
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return rv;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
}
|
|
|
|
}
|
2008-10-09 12:23:22 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
// If we moved, we want to adjust this.
|
2014-05-14 13:15:46 -07:00
|
|
|
if (move) {
|
2014-05-05 10:30:43 -07:00
|
|
|
MakeDirty();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
nsAutoString newParentPath;
|
|
|
|
newParentDir->GetPath(newParentPath);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
if (newParentPath.IsEmpty()) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
if (aNewName.IsEmpty()) {
|
2014-05-05 10:30:43 -07:00
|
|
|
nsAutoString aFileName;
|
|
|
|
GetLeafName(aFileName);
|
|
|
|
|
|
|
|
InitWithPath(newParentPath);
|
|
|
|
Append(aFileName);
|
2014-05-14 13:15:46 -07:00
|
|
|
} else {
|
2014-05-05 10:30:43 -07:00
|
|
|
InitWithPath(newParentPath);
|
2014-05-14 13:15:46 -07:00
|
|
|
Append(aNewName);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-14 13:15:46 -07:00
|
|
|
nsLocalFile::CopyTo(nsIFile* aNewParentDir, const nsAString& aNewName)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-14 13:15:46 -07:00
|
|
|
return CopyMove(aNewParentDir, aNewName, 0);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-14 13:15:46 -07:00
|
|
|
nsLocalFile::CopyToFollowingLinks(nsIFile* aNewParentDir,
|
|
|
|
const nsAString& aNewName)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-14 13:15:46 -07:00
|
|
|
return CopyMove(aNewParentDir, aNewName, FollowSymlinks);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-14 13:15:46 -07:00
|
|
|
nsLocalFile::MoveTo(nsIFile* aNewParentDir, const nsAString& aNewName)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-14 13:15:46 -07:00
|
|
|
return CopyMove(aNewParentDir, aNewName, Move);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-01-06 00:51:39 -08:00
|
|
|
NS_IMETHODIMP
|
2014-05-14 13:15:46 -07:00
|
|
|
nsLocalFile::RenameTo(nsIFile* aNewParentDir, const nsAString& aNewName)
|
2014-01-06 00:51:39 -08:00
|
|
|
{
|
2014-05-14 13:15:46 -07:00
|
|
|
nsCOMPtr<nsIFile> targetParentDir = aNewParentDir;
|
2014-01-06 03:27:11 -08: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();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!targetParentDir) {
|
|
|
|
// no parent was specified. We must rename.
|
2014-05-14 13:15:46 -07:00
|
|
|
if (aNewName.IsEmpty()) {
|
2014-01-06 03:27:11 -08:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
rv = GetParent(getter_AddRefs(targetParentDir));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!targetParentDir) {
|
|
|
|
return NS_ERROR_FILE_DESTINATION_NOT_DIR;
|
|
|
|
}
|
|
|
|
|
|
|
|
// make sure it exists and is a directory. Create it if not there.
|
|
|
|
bool exists;
|
|
|
|
targetParentDir->Exists(&exists);
|
|
|
|
if (!exists) {
|
|
|
|
rv = targetParentDir->Create(DIRECTORY_TYPE, 0644);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
bool isDir;
|
|
|
|
targetParentDir->IsDirectory(&isDir);
|
|
|
|
if (!isDir) {
|
|
|
|
return NS_ERROR_FILE_DESTINATION_NOT_DIR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t options = Rename;
|
2014-05-14 13:15:46 -07:00
|
|
|
if (!aNewParentDir) {
|
2014-01-06 03:27:11 -08:00
|
|
|
options |= SkipNtfsAclReset;
|
|
|
|
}
|
|
|
|
// Move single file, or move a directory
|
2014-05-14 13:15:46 -07:00
|
|
|
return CopySingleFile(this, targetParentDir, aNewName, options);
|
2014-01-06 00:51:39 -08:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-14 13:15:46 -07:00
|
|
|
nsLocalFile::Load(PRLibrary** aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
// Check we are correctly initialized.
|
|
|
|
CHECK_mWorkingPath();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
bool isFile;
|
|
|
|
nsresult rv = IsFile(&isFile);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return rv;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
if (! isFile) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_FILE_IS_DIRECTORY;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#ifdef NS_BUILD_REFCNT_LOGGING
|
2014-05-05 10:30:43 -07:00
|
|
|
nsTraceRefcnt::SetActivityIsLegal(false);
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif
|
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
PRLibSpec libSpec;
|
|
|
|
libSpec.value.pathname_u = mResolvedPath.get();
|
|
|
|
libSpec.type = PR_LibSpec_PathnameU;
|
2014-05-14 13:15:46 -07:00
|
|
|
*aResult = PR_LoadLibraryWithFlags(libSpec, 0);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#ifdef NS_BUILD_REFCNT_LOGGING
|
2014-05-05 10:30:43 -07:00
|
|
|
nsTraceRefcnt::SetActivityIsLegal(true);
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif
|
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
if (*aResult) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_OK;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_NULL_POINTER;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-14 13:15:46 -07:00
|
|
|
nsLocalFile::Remove(bool aRecursive)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
// 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).
|
|
|
|
//
|
|
|
|
// Since shortcut files are no longer permitted to be used as unix-like
|
|
|
|
// symlinks interspersed in the path (e.g. "c:/file.lnk/foo/bar.txt")
|
|
|
|
// this processing is a lot simpler. Even if the shortcut file is
|
|
|
|
// pointing to a directory, only the mWorkingPath value is used and so
|
|
|
|
// only the shortcut file will be deleted.
|
|
|
|
|
|
|
|
// Check we are correctly initialized.
|
|
|
|
CHECK_mWorkingPath();
|
|
|
|
|
|
|
|
bool isDir, isLink;
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
isDir = false;
|
|
|
|
rv = IsSymlink(&isLink);
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return rv;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
|
|
|
|
// only check to see if we have a directory if it isn't a link
|
2014-05-14 13:15:46 -07:00
|
|
|
if (!isLink) {
|
2014-05-05 10:30:43 -07:00
|
|
|
rv = IsDirectory(&isDir);
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return rv;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
if (isDir) {
|
|
|
|
if (aRecursive) {
|
2014-05-05 10:30:43 -07:00
|
|
|
nsDirEnumerator dirEnum;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
rv = dirEnum.Init(this);
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return rv;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
|
|
|
|
bool more = false;
|
2014-05-14 13:15:46 -07:00
|
|
|
while (NS_SUCCEEDED(dirEnum.HasMoreElements(&more)) && more) {
|
2014-05-05 10:30:43 -07:00
|
|
|
nsCOMPtr<nsISupports> item;
|
|
|
|
dirEnum.GetNext(getter_AddRefs(item));
|
|
|
|
nsCOMPtr<nsIFile> file = do_QueryInterface(item);
|
2014-05-14 13:15:46 -07:00
|
|
|
if (file) {
|
|
|
|
file->Remove(aRecursive);
|
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2014-05-14 13:15:46 -07:00
|
|
|
if (RemoveDirectoryW(mWorkingPath.get()) == 0) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return ConvertWinError(GetLastError());
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (DeleteFileW(mWorkingPath.get()) == 0) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return ConvertWinError(GetLastError());
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
MakeDirty();
|
|
|
|
return rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-14 13:15:46 -07:00
|
|
|
nsLocalFile::GetLastModifiedTime(PRTime* aLastModifiedTime)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
// Check we are correctly initialized.
|
|
|
|
CHECK_mWorkingPath();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_WARN_IF(!aLastModifiedTime)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
// get the modified time of the target as determined by mFollowSymlinks
|
|
|
|
// If true, then this will be for the target of the shortcut file,
|
|
|
|
// otherwise it will be for the shortcut file itself (i.e. the same
|
|
|
|
// results as GetLastModifiedTimeOfLink)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
nsresult rv = ResolveAndStat();
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return rv;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
|
|
|
|
// microseconds -> milliseconds
|
|
|
|
*aLastModifiedTime = mFileInfo64.modifyTime / PR_USEC_PER_MSEC;
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-14 13:15:46 -07:00
|
|
|
nsLocalFile::GetLastModifiedTimeOfLink(PRTime* aLastModifiedTime)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
// Check we are correctly initialized.
|
|
|
|
CHECK_mWorkingPath();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_WARN_IF(!aLastModifiedTime)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
// The caller is assumed to have already called IsSymlink
|
|
|
|
// and to have found that this file is a link.
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
PRFileInfo64 info;
|
|
|
|
nsresult rv = GetFileInfo(mWorkingPath, &info);
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return rv;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
|
|
|
|
// microseconds -> milliseconds
|
|
|
|
*aLastModifiedTime = info.modifyTime / PR_USEC_PER_MSEC;
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-30 00:10:35 -07:00
|
|
|
nsLocalFile::SetLastModifiedTime(PRTime aLastModifiedTime)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
// Check we are correctly initialized.
|
|
|
|
CHECK_mWorkingPath();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
nsresult rv = ResolveAndStat();
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return rv;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
// set the modified time of the target as determined by mFollowSymlinks
|
|
|
|
// If true, then this will be for the target of the shortcut file,
|
|
|
|
// otherwise it will be for the shortcut file itself (i.e. the same
|
|
|
|
// results as SetLastModifiedTimeOfLink)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
rv = SetModDate(aLastModifiedTime, mResolvedPath.get());
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
MakeDirty();
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
return rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-30 00:10:35 -07:00
|
|
|
nsLocalFile::SetLastModifiedTimeOfLink(PRTime aLastModifiedTime)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
// The caller is assumed to have already called IsSymlink
|
|
|
|
// and to have found that this file is a link.
|
2008-10-09 12:23:22 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
nsresult rv = SetModDate(aLastModifiedTime, mWorkingPath.get());
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
MakeDirty();
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2008-10-09 12:23:22 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
return rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-05-14 13:15:46 -07:00
|
|
|
nsLocalFile::SetModDate(PRTime aLastModifiedTime, const wchar_t* aFilePath)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
// The FILE_FLAG_BACKUP_SEMANTICS is required in order to change the
|
|
|
|
// modification time for directories.
|
2014-05-14 13:15:46 -07:00
|
|
|
HANDLE file = ::CreateFileW(aFilePath, // pointer to name of the file
|
2014-05-05 10:30:43 -07:00
|
|
|
GENERIC_WRITE, // access (write) mode
|
|
|
|
0, // share mode
|
|
|
|
nullptr, // pointer to security attributes
|
|
|
|
OPEN_EXISTING, // how to create
|
|
|
|
FILE_FLAG_BACKUP_SEMANTICS, // file attributes
|
|
|
|
nullptr);
|
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
if (file == INVALID_HANDLE_VALUE) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return ConvertWinError(GetLastError());
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
FILETIME ft;
|
|
|
|
SYSTEMTIME st;
|
|
|
|
PRExplodedTime pret;
|
|
|
|
|
|
|
|
// PR_ExplodeTime expects usecs...
|
|
|
|
PR_ExplodeTime(aLastModifiedTime * PR_USEC_PER_MSEC, PR_GMTParameters, &pret);
|
|
|
|
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;
|
2014-05-14 13:15:46 -07:00
|
|
|
st.wMilliseconds = pret.tm_usec / 1000;
|
2014-05-05 10:30:43 -07:00
|
|
|
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
// if at least one of these fails...
|
|
|
|
if (!(SystemTimeToFileTime(&st, &ft) != 0 &&
|
2014-05-14 13:15:46 -07:00
|
|
|
SetFileTime(file, nullptr, &ft, &ft) != 0)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
rv = ConvertWinError(GetLastError());
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
CloseHandle(file);
|
|
|
|
return rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-14 13:15:46 -07:00
|
|
|
nsLocalFile::GetPermissions(uint32_t* aPermissions)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_WARN_IF(!aPermissions)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
// get the permissions of the target as determined by mFollowSymlinks
|
|
|
|
// If true, then this will be for the target of the shortcut file,
|
|
|
|
// otherwise it will be for the shortcut file itself (i.e. the same
|
|
|
|
// results as GetPermissionsOfLink)
|
|
|
|
nsresult rv = ResolveAndStat();
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return rv;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
bool isWritable, isExecutable;
|
|
|
|
IsWritable(&isWritable);
|
|
|
|
IsExecutable(&isExecutable);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
*aPermissions = PR_IRUSR | PR_IRGRP | PR_IROTH; // all read
|
|
|
|
if (isWritable) {
|
|
|
|
*aPermissions |= PR_IWUSR | PR_IWGRP | PR_IWOTH; // all write
|
|
|
|
}
|
|
|
|
if (isExecutable) {
|
|
|
|
*aPermissions |= PR_IXUSR | PR_IXGRP | PR_IXOTH; // all execute
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-14 13:15:46 -07:00
|
|
|
nsLocalFile::GetPermissionsOfLink(uint32_t* aPermissions)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
// Check we are correctly initialized.
|
|
|
|
CHECK_mWorkingPath();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_WARN_IF(!aPermissions)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
// The caller is assumed to have already called IsSymlink
|
|
|
|
// and to have found that this file is a link. It is not
|
|
|
|
// possible for a link file to be executable.
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
DWORD word = ::GetFileAttributesW(mWorkingPath.get());
|
2014-05-14 13:15:46 -07:00
|
|
|
if (word == INVALID_FILE_ATTRIBUTES) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_FILE_INVALID_PATH;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
bool isWritable = !(word & FILE_ATTRIBUTE_READONLY);
|
2014-05-14 13:15:46 -07:00
|
|
|
*aPermissions = PR_IRUSR | PR_IRGRP | PR_IROTH; // all read
|
|
|
|
if (isWritable) {
|
|
|
|
*aPermissions |= PR_IWUSR | PR_IWGRP | PR_IWOTH; // all write
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsLocalFile::SetPermissions(uint32_t aPermissions)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
// Check we are correctly initialized.
|
|
|
|
CHECK_mWorkingPath();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
// set the permissions of the target as determined by mFollowSymlinks
|
|
|
|
// If true, then this will be for the target of the shortcut file,
|
|
|
|
// otherwise it will be for the shortcut file itself (i.e. the same
|
|
|
|
// results as SetPermissionsOfLink)
|
|
|
|
nsresult rv = ResolveAndStat();
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return rv;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
// windows only knows about the following permissions
|
|
|
|
int mode = 0;
|
2014-05-14 13:15:46 -07:00
|
|
|
if (aPermissions & (PR_IRUSR | PR_IRGRP | PR_IROTH)) { // any read
|
2014-05-05 10:30:43 -07:00
|
|
|
mode |= _S_IREAD;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
|
|
|
if (aPermissions & (PR_IWUSR | PR_IWGRP | PR_IWOTH)) { // any write
|
2014-05-05 10:30:43 -07:00
|
|
|
mode |= _S_IWRITE;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
if (_wchmod(mResolvedPath.get(), mode) == -1) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsLocalFile::SetPermissionsOfLink(uint32_t aPermissions)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
// The caller is assumed to have already called IsSymlink
|
|
|
|
// and to have found that this file is a link.
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
// windows only knows about the following permissions
|
|
|
|
int mode = 0;
|
2014-05-14 13:15:46 -07:00
|
|
|
if (aPermissions & (PR_IRUSR | PR_IRGRP | PR_IROTH)) { // any read
|
2014-05-05 10:30:43 -07:00
|
|
|
mode |= _S_IREAD;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
|
|
|
if (aPermissions & (PR_IWUSR | PR_IWGRP | PR_IWOTH)) { // any write
|
2014-05-05 10:30:43 -07:00
|
|
|
mode |= _S_IWRITE;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
if (_wchmod(mWorkingPath.get(), mode) == -1) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-14 13:15:46 -07:00
|
|
|
nsLocalFile::GetFileSize(int64_t* aFileSize)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_WARN_IF(!aFileSize)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
nsresult rv = ResolveAndStat();
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return rv;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
*aFileSize = mFileInfo64.size;
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-14 13:15:46 -07:00
|
|
|
nsLocalFile::GetFileSizeOfLink(int64_t* aFileSize)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
// Check we are correctly initialized.
|
|
|
|
CHECK_mWorkingPath();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_WARN_IF(!aFileSize)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
// The caller is assumed to have already called IsSymlink
|
|
|
|
// and to have found that this file is a link.
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
PRFileInfo64 info;
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_FAILED(GetFileInfo(mWorkingPath, &info))) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_FILE_INVALID_PATH;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
*aFileSize = info.size;
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsLocalFile::SetFileSize(int64_t aFileSize)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
// Check we are correctly initialized.
|
|
|
|
CHECK_mWorkingPath();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
nsresult rv = ResolveAndStat();
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return rv;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
HANDLE hFile = ::CreateFileW(mResolvedPath.get(),// pointer to name of the file
|
|
|
|
GENERIC_WRITE, // access (write) mode
|
|
|
|
FILE_SHARE_READ, // share mode
|
|
|
|
nullptr, // pointer to security attributes
|
|
|
|
OPEN_EXISTING, // how to create
|
|
|
|
FILE_ATTRIBUTE_NORMAL, // file attributes
|
|
|
|
nullptr);
|
2014-05-14 13:15:46 -07:00
|
|
|
if (hFile == INVALID_HANDLE_VALUE) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return ConvertWinError(GetLastError());
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
// seek the file pointer to the new, desired end of file
|
|
|
|
// and then truncate the file at that position
|
|
|
|
rv = NS_ERROR_FAILURE;
|
|
|
|
aFileSize = MyFileSeek64(hFile, aFileSize, FILE_BEGIN);
|
2014-05-14 13:15:46 -07:00
|
|
|
if (aFileSize != -1 && SetEndOfFile(hFile)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
MakeDirty();
|
|
|
|
rv = NS_OK;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
CloseHandle(hFile);
|
|
|
|
return rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-14 13:15:46 -07:00
|
|
|
nsLocalFile::GetDiskSpaceAvailable(int64_t* aDiskSpaceAvailable)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
// Check we are correctly initialized.
|
|
|
|
CHECK_mWorkingPath();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_WARN_IF(!aDiskSpaceAvailable)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2011-08-26 04:29:45 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
ResolveAndStat();
|
|
|
|
|
|
|
|
if (mFileInfo64.type == PR_FILE_FILE) {
|
|
|
|
// Since GetDiskFreeSpaceExW works only on directories, use the parent.
|
|
|
|
nsCOMPtr<nsIFile> parent;
|
|
|
|
if (NS_SUCCEEDED(GetParent(getter_AddRefs(parent))) && parent) {
|
|
|
|
return parent->GetDiskSpaceAvailable(aDiskSpaceAvailable);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
ULARGE_INTEGER liFreeBytesAvailableToCaller, liTotalNumberOfBytes;
|
|
|
|
if (::GetDiskFreeSpaceExW(mResolvedPath.get(), &liFreeBytesAvailableToCaller,
|
2014-05-14 13:15:46 -07:00
|
|
|
&liTotalNumberOfBytes, nullptr)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
*aDiskSpaceAvailable = liFreeBytesAvailableToCaller.QuadPart;
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
2014-05-05 10:30:43 -07:00
|
|
|
}
|
|
|
|
*aDiskSpaceAvailable = 0;
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-14 13:15:46 -07:00
|
|
|
nsLocalFile::GetParent(nsIFile** aParent)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
// Check we are correctly initialized.
|
|
|
|
CHECK_mWorkingPath();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_WARN_IF(!aParent)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
// A two-character path must be a drive such as C:, so it has no parent
|
|
|
|
if (mWorkingPath.Length() == 2) {
|
|
|
|
*aParent = nullptr;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
int32_t offset = mWorkingPath.RFindChar(char16_t('\\'));
|
|
|
|
// adding this offset check that was removed in bug 241708 fixes mail
|
|
|
|
// directories that aren't relative to/underneath the profile dir.
|
|
|
|
// e.g., on a different drive. Before you remove them, please make
|
|
|
|
// sure local mail directories that aren't underneath the profile dir work.
|
2014-05-14 13:15:46 -07:00
|
|
|
if (offset == kNotFound) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_FILE_UNRECOGNIZED_PATH;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
|
|
|
|
// A path of the form \\NAME is a top-level path and has no parent
|
|
|
|
if (offset == 1 && mWorkingPath[0] == L'\\') {
|
|
|
|
*aParent = nullptr;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2007-03-25 00:28:06 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
nsAutoString parentPath(mWorkingPath);
|
2007-03-25 00:28:06 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
if (offset > 0) {
|
2014-05-05 10:30:43 -07:00
|
|
|
parentPath.Truncate(offset);
|
2014-05-14 13:15:46 -07:00
|
|
|
} else {
|
2014-05-05 10:30:43 -07:00
|
|
|
parentPath.AssignLiteral("\\\\.");
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
nsCOMPtr<nsIFile> localFile;
|
|
|
|
nsresult rv = NS_NewLocalFile(parentPath, mFollowSymlinks, getter_AddRefs(localFile));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2014-03-17 10:00:17 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
localFile.forget(aParent);
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-14 13:15:46 -07:00
|
|
|
nsLocalFile::Exists(bool* aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
// Check we are correctly initialized.
|
|
|
|
CHECK_mWorkingPath();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_WARN_IF(!aResult)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
|
|
|
*aResult = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
MakeDirty();
|
|
|
|
nsresult rv = ResolveAndStat();
|
2014-05-14 13:15:46 -07:00
|
|
|
*aResult = NS_SUCCEEDED(rv) || rv == NS_ERROR_FILE_IS_LOCKED;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-14 13:15:46 -07:00
|
|
|
nsLocalFile::IsWritable(bool* aIsWritable)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
// Check we are correctly initialized.
|
|
|
|
CHECK_mWorkingPath();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
// The read-only attribute on a FAT directory only means that it can't
|
|
|
|
// be deleted. It is still possible to modify the contents of the directory.
|
|
|
|
nsresult rv = IsDirectory(aIsWritable);
|
|
|
|
if (rv == NS_ERROR_FILE_ACCESS_DENIED) {
|
|
|
|
*aIsWritable = true;
|
|
|
|
return NS_OK;
|
|
|
|
} else if (rv == NS_ERROR_FILE_IS_LOCKED) {
|
|
|
|
// If the file is normally allowed write access
|
|
|
|
// we should still return that the file is writable.
|
|
|
|
} else if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2014-05-14 13:15:46 -07:00
|
|
|
if (*aIsWritable) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_OK;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
// writable if the file doesn't have the readonly attribute
|
|
|
|
rv = HasFileAttribute(FILE_ATTRIBUTE_READONLY, aIsWritable);
|
|
|
|
if (rv == NS_ERROR_FILE_ACCESS_DENIED) {
|
|
|
|
*aIsWritable = false;
|
|
|
|
return NS_OK;
|
|
|
|
} else if (rv == NS_ERROR_FILE_IS_LOCKED) {
|
|
|
|
// If the file is normally allowed write access
|
|
|
|
// we should still return that the file is writable.
|
|
|
|
} else if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
*aIsWritable = !*aIsWritable;
|
|
|
|
|
|
|
|
// If the read only attribute is not set, check to make sure
|
|
|
|
// we can open the file with write access.
|
|
|
|
if (*aIsWritable) {
|
|
|
|
PRFileDesc* file;
|
|
|
|
rv = OpenFile(mResolvedPath, PR_WRONLY, 0, &file);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
PR_Close(file);
|
|
|
|
} else if (rv == NS_ERROR_FILE_ACCESS_DENIED) {
|
|
|
|
*aIsWritable = false;
|
2011-09-22 06:21:03 -07:00
|
|
|
} else if (rv == NS_ERROR_FILE_IS_LOCKED) {
|
2014-05-05 10:30:43 -07:00
|
|
|
// If it is locked and read only we would have
|
|
|
|
// gotten access denied
|
|
|
|
*aIsWritable = true;
|
|
|
|
} else {
|
|
|
|
return rv;
|
2011-09-22 06:21:03 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
}
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-14 13:15:46 -07:00
|
|
|
nsLocalFile::IsReadable(bool* aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
// Check we are correctly initialized.
|
|
|
|
CHECK_mWorkingPath();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_WARN_IF(!aResult)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
|
|
|
*aResult = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
nsresult rv = ResolveAndStat();
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return rv;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
*aResult = true;
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-14 13:15:46 -07:00
|
|
|
nsLocalFile::IsExecutable(bool* aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
// Check we are correctly initialized.
|
|
|
|
CHECK_mWorkingPath();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_WARN_IF(!aResult)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
|
|
|
*aResult = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
nsresult rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
// only files can be executables
|
|
|
|
bool isFile;
|
|
|
|
rv = IsFile(&isFile);
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return rv;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
|
|
|
if (!isFile) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_OK;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
//TODO: shouldn't we be checking mFollowSymlinks here?
|
|
|
|
bool symLink;
|
|
|
|
rv = IsSymlink(&symLink);
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return rv;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
nsAutoString path;
|
2014-05-14 13:15:46 -07:00
|
|
|
if (symLink) {
|
2014-05-05 10:30:43 -07:00
|
|
|
GetTarget(path);
|
2014-05-14 13:15:46 -07:00
|
|
|
} else {
|
2014-05-05 10:30:43 -07:00
|
|
|
GetPath(path);
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
|
|
|
|
// kill trailing dots and spaces.
|
|
|
|
int32_t filePathLen = path.Length() - 1;
|
2014-05-14 13:15:46 -07:00
|
|
|
while (filePathLen > 0 && (path[filePathLen] == L' ' ||
|
|
|
|
path[filePathLen] == L'.')) {
|
2014-05-05 10:30:43 -07:00
|
|
|
path.Truncate(filePathLen--);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get extension.
|
|
|
|
int32_t dotIdx = path.RFindChar(char16_t('.'));
|
2014-05-14 13:15:46 -07:00
|
|
|
if (dotIdx != kNotFound) {
|
2014-05-05 10:30:43 -07:00
|
|
|
// Convert extension to lower case.
|
2014-05-14 13:15:46 -07:00
|
|
|
char16_t* p = path.BeginWriting();
|
|
|
|
for (p += dotIdx + 1; *p; ++p) {
|
|
|
|
*p += (*p >= L'A' && *p <= L'Z') ? 'a' - 'A' : 0;
|
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
|
|
|
|
// Search for any of the set of executable extensions.
|
2014-05-14 13:15:46 -07:00
|
|
|
static const char* const executableExts[] = {
|
2014-05-05 10:30:43 -07:00
|
|
|
"ad",
|
|
|
|
"ade", // access project extension
|
|
|
|
"adp",
|
|
|
|
"air", // Adobe AIR installer
|
|
|
|
"app", // executable application
|
|
|
|
"application", // from bug 348763
|
|
|
|
"asp",
|
|
|
|
"bas",
|
|
|
|
"bat",
|
|
|
|
"chm",
|
|
|
|
"cmd",
|
|
|
|
"com",
|
|
|
|
"cpl",
|
|
|
|
"crt",
|
|
|
|
"exe",
|
|
|
|
"fxp", // FoxPro compiled app
|
|
|
|
"hlp",
|
|
|
|
"hta",
|
|
|
|
"inf",
|
|
|
|
"ins",
|
|
|
|
"isp",
|
|
|
|
"jar", // java application bundle
|
|
|
|
"js",
|
|
|
|
"jse",
|
|
|
|
"lnk",
|
|
|
|
"mad", // Access Module Shortcut
|
|
|
|
"maf", // Access
|
|
|
|
"mag", // Access Diagram Shortcut
|
|
|
|
"mam", // Access Macro Shortcut
|
|
|
|
"maq", // Access Query Shortcut
|
|
|
|
"mar", // Access Report Shortcut
|
|
|
|
"mas", // Access Stored Procedure
|
|
|
|
"mat", // Access Table Shortcut
|
|
|
|
"mau", // Media Attachment Unit
|
|
|
|
"mav", // Access View Shortcut
|
|
|
|
"maw", // Access Data Access Page
|
|
|
|
"mda", // Access Add-in, MDA Access 2 Workgroup
|
|
|
|
"mdb",
|
|
|
|
"mde",
|
|
|
|
"mdt", // Access Add-in Data
|
|
|
|
"mdw", // Access Workgroup Information
|
|
|
|
"mdz", // Access Wizard Template
|
|
|
|
"msc",
|
|
|
|
"msh", // Microsoft Shell
|
|
|
|
"mshxml", // Microsoft Shell
|
|
|
|
"msi",
|
|
|
|
"msp",
|
|
|
|
"mst",
|
|
|
|
"ops", // Office Profile Settings
|
|
|
|
"pcd",
|
|
|
|
"pif",
|
|
|
|
"plg", // Developer Studio Build Log
|
|
|
|
"prf", // windows system file
|
|
|
|
"prg",
|
|
|
|
"pst",
|
|
|
|
"reg",
|
|
|
|
"scf", // Windows explorer command
|
|
|
|
"scr",
|
|
|
|
"sct",
|
|
|
|
"shb",
|
|
|
|
"shs",
|
|
|
|
"url",
|
|
|
|
"vb",
|
|
|
|
"vbe",
|
|
|
|
"vbs",
|
|
|
|
"vsd",
|
|
|
|
"vsmacros", // Visual Studio .NET Binary-based Macro Project
|
|
|
|
"vss",
|
|
|
|
"vst",
|
|
|
|
"vsw",
|
|
|
|
"ws",
|
|
|
|
"wsc",
|
|
|
|
"wsf",
|
2014-05-14 13:15:46 -07:00
|
|
|
"wsh"
|
|
|
|
};
|
2014-05-05 10:30:43 -07:00
|
|
|
nsDependentSubstring ext = Substring(path, dotIdx + 1);
|
2014-05-14 13:15:46 -07:00
|
|
|
for (size_t i = 0; i < ArrayLength(executableExts); ++i) {
|
|
|
|
if (ext.EqualsASCII(executableExts[i])) {
|
2014-05-05 10:30:43 -07:00
|
|
|
// Found a match. Set result and quit.
|
2014-05-14 13:15:46 -07:00
|
|
|
*aResult = true;
|
2014-05-05 10:30:43 -07:00
|
|
|
break;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-14 13:15:46 -07:00
|
|
|
nsLocalFile::IsDirectory(bool* aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-14 13:15:46 -07:00
|
|
|
return HasFileAttribute(FILE_ATTRIBUTE_DIRECTORY, aResult);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-14 13:15:46 -07:00
|
|
|
nsLocalFile::IsFile(bool* aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-14 13:15:46 -07:00
|
|
|
nsresult rv = HasFileAttribute(FILE_ATTRIBUTE_DIRECTORY, aResult);
|
2014-05-05 10:30:43 -07:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2014-05-14 13:15:46 -07:00
|
|
|
*aResult = !*aResult;
|
2014-05-05 10:30:43 -07:00
|
|
|
}
|
|
|
|
return rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-14 13:15:46 -07:00
|
|
|
nsLocalFile::IsHidden(bool* aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-14 13:15:46 -07:00
|
|
|
return HasFileAttribute(FILE_ATTRIBUTE_HIDDEN, aResult);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-05-14 13:15:46 -07:00
|
|
|
nsLocalFile::HasFileAttribute(DWORD aFileAttrib, bool* aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_WARN_IF(!aResult)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
nsresult rv = Resolve();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
DWORD attributes = GetFileAttributesW(mResolvedPath.get());
|
|
|
|
if (INVALID_FILE_ATTRIBUTES == attributes) {
|
|
|
|
return ConvertWinError(GetLastError());
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
*aResult = ((attributes & aFileAttrib) != 0);
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-14 13:15:46 -07:00
|
|
|
nsLocalFile::IsSymlink(bool* aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
// Check we are correctly initialized.
|
|
|
|
CHECK_mWorkingPath();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_WARN_IF(!aResult)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
// unless it is a valid shortcut path it's not a symlink
|
|
|
|
if (!IsShortcutPath(mWorkingPath)) {
|
2014-05-14 13:15:46 -07:00
|
|
|
*aResult = false;
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
// we need to know if this is a file or directory
|
|
|
|
nsresult rv = ResolveAndStat();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
// We should not check mFileInfo64.type here for PR_FILE_FILE because lnk
|
|
|
|
// files can point to directories or files. Important security checks
|
|
|
|
// depend on correctly identifying lnk files. mFileInfo64 now holds info
|
|
|
|
// about the target of the lnk file, not the actual lnk file!
|
2014-05-14 13:15:46 -07:00
|
|
|
*aResult = true;
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-14 13:15:46 -07:00
|
|
|
nsLocalFile::IsSpecial(bool* aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-14 13:15:46 -07:00
|
|
|
return HasFileAttribute(FILE_ATTRIBUTE_SYSTEM, aResult);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-14 13:15:46 -07:00
|
|
|
nsLocalFile::Equals(nsIFile* aInFile, bool* aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_WARN_IF(!aInFile)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
|
|
|
if (NS_WARN_IF(!aResult)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
EnsureShortPath();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
nsCOMPtr<nsILocalFileWin> lf(do_QueryInterface(aInFile));
|
2014-05-05 10:30:43 -07:00
|
|
|
if (!lf) {
|
2014-05-14 13:15:46 -07:00
|
|
|
*aResult = false;
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
nsAutoString inFilePath;
|
|
|
|
lf->GetCanonicalPath(inFilePath);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
// Ok : Win9x
|
2014-05-14 13:15:46 -07:00
|
|
|
*aResult = _wcsicmp(mShortWorkingPath.get(), inFilePath.get()) == 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-14 13:15:46 -07:00
|
|
|
nsLocalFile::Contains(nsIFile* aInFile, bool* aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
// Check we are correctly initialized.
|
|
|
|
CHECK_mWorkingPath();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
*aResult = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
nsAutoString myFilePath;
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_FAILED(GetTarget(myFilePath))) {
|
2014-05-05 10:30:43 -07:00
|
|
|
GetPath(myFilePath);
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
uint32_t myFilePathLen = myFilePath.Length();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
nsAutoString inFilePath;
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_FAILED(aInFile->GetTarget(inFilePath))) {
|
|
|
|
aInFile->GetPath(inFilePath);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
// make sure that the |aInFile|'s path has a trailing separator.
|
|
|
|
if (inFilePath.Length() >= myFilePathLen &&
|
|
|
|
inFilePath[myFilePathLen] == L'\\') {
|
|
|
|
if (_wcsnicmp(myFilePath.get(), inFilePath.get(), myFilePathLen) == 0) {
|
|
|
|
*aResult = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-14 13:15:46 -07:00
|
|
|
nsLocalFile::GetTarget(nsAString& aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-14 13:15:46 -07:00
|
|
|
aResult.Truncate();
|
2007-03-22 10:30:00 -07:00
|
|
|
#if STRICT_FAKE_SYMLINKS
|
2014-05-05 10:30:43 -07:00
|
|
|
bool symLink;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
nsresult rv = IsSymlink(&symLink);
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return rv;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
if (!symLink) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_FILE_INVALID_PATH;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif
|
2014-05-05 10:30:43 -07:00
|
|
|
ResolveAndStat();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
aResult = mResolvedPath;
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
/* attribute bool followLinks; */
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP
|
2014-05-14 13:15:46 -07:00
|
|
|
nsLocalFile::GetFollowLinks(bool* aFollowLinks)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
*aFollowLinks = mFollowSymlinks;
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
nsLocalFile::SetFollowLinks(bool aFollowLinks)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
MakeDirty();
|
|
|
|
mFollowSymlinks = aFollowLinks;
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-14 13:15:46 -07:00
|
|
|
nsLocalFile::GetDirectoryEntries(nsISimpleEnumerator** aEntries)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
nsresult rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
*aEntries = nullptr;
|
2014-05-05 10:30:43 -07:00
|
|
|
if (mWorkingPath.EqualsLiteral("\\\\.")) {
|
2014-05-14 13:15:46 -07:00
|
|
|
nsDriveEnumerator* drives = new nsDriveEnumerator;
|
|
|
|
if (!drives) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
NS_ADDREF(drives);
|
|
|
|
rv = drives->Init();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_RELEASE(drives);
|
|
|
|
return rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2014-05-14 13:15:46 -07:00
|
|
|
*aEntries = drives;
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
nsDirEnumerator* dirEnum = new nsDirEnumerator();
|
|
|
|
if (dirEnum == nullptr)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
NS_ADDREF(dirEnum);
|
|
|
|
rv = dirEnum->Init(this);
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
NS_RELEASE(dirEnum);
|
|
|
|
return rv;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
*aEntries = dirEnum;
|
2008-10-08 20:51:54 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-14 13:15:46 -07:00
|
|
|
nsLocalFile::GetPersistentDescriptor(nsACString& aPersistentDescriptor)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
CopyUTF16toUTF8(mWorkingPath, aPersistentDescriptor);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP
|
2014-05-14 13:15:46 -07:00
|
|
|
nsLocalFile::SetPersistentDescriptor(const nsACString& aPersistentDescriptor)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-14 13:15:46 -07:00
|
|
|
if (IsUTF8(aPersistentDescriptor)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return InitWithPath(NS_ConvertUTF8toUTF16(aPersistentDescriptor));
|
2014-05-14 13:15:46 -07:00
|
|
|
} else {
|
2014-05-05 10:30:43 -07:00
|
|
|
return InitWithNativePath(aPersistentDescriptor);
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-04-20 18:06:03 -07:00
|
|
|
/* attrib unsigned long fileAttributesWin; */
|
|
|
|
NS_IMETHODIMP
|
2014-05-14 13:15:46 -07:00
|
|
|
nsLocalFile::GetFileAttributesWin(uint32_t* aAttribs)
|
2009-04-20 18:06:03 -07:00
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
*aAttribs = 0;
|
|
|
|
DWORD dwAttrs = GetFileAttributesW(mWorkingPath.get());
|
2014-05-14 13:15:46 -07:00
|
|
|
if (dwAttrs == INVALID_FILE_ATTRIBUTES) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_FILE_INVALID_PATH;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2009-04-20 18:06:03 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
if (!(dwAttrs & FILE_ATTRIBUTE_NOT_CONTENT_INDEXED)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
*aAttribs |= WFA_SEARCH_INDEXED;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2009-04-20 18:06:03 -07:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsLocalFile::SetFileAttributesWin(uint32_t aAttribs)
|
2009-04-20 18:06:03 -07:00
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
DWORD dwAttrs = GetFileAttributesW(mWorkingPath.get());
|
2014-05-14 13:15:46 -07:00
|
|
|
if (dwAttrs == INVALID_FILE_ATTRIBUTES) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_FILE_INVALID_PATH;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2009-04-20 18:06:03 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
if (aAttribs & WFA_SEARCH_INDEXED) {
|
|
|
|
dwAttrs &= ~FILE_ATTRIBUTE_NOT_CONTENT_INDEXED;
|
|
|
|
} else {
|
|
|
|
dwAttrs |= FILE_ATTRIBUTE_NOT_CONTENT_INDEXED;
|
|
|
|
}
|
2009-04-20 18:06:03 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
if (aAttribs & WFA_READONLY) {
|
|
|
|
dwAttrs |= FILE_ATTRIBUTE_READONLY;
|
|
|
|
} else if ((aAttribs & WFA_READWRITE) &&
|
|
|
|
(dwAttrs & FILE_ATTRIBUTE_READONLY)) {
|
|
|
|
dwAttrs &= ~FILE_ATTRIBUTE_READONLY;
|
|
|
|
}
|
2012-05-22 07:50:04 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
if (SetFileAttributesW(mWorkingPath.get(), dwAttrs) == 0) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_OK;
|
2012-02-23 06:53:55 -08:00
|
|
|
}
|
2009-04-20 18:06:03 -07:00
|
|
|
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLocalFile::Reveal()
|
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
// This API should be main thread only
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2011-09-08 08:37:43 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
// make sure mResolvedPath is set
|
|
|
|
nsresult rv = Resolve();
|
|
|
|
if (NS_FAILED(rv) && rv != NS_ERROR_FILE_NOT_FOUND) {
|
|
|
|
return rv;
|
|
|
|
}
|
2011-09-08 08:37:43 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
// To create a new thread, get the thread manager
|
|
|
|
nsCOMPtr<nsIThreadManager> tm = do_GetService(NS_THREADMANAGER_CONTRACTID);
|
|
|
|
nsCOMPtr<nsIThread> mythread;
|
|
|
|
rv = tm->NewThread(0, 0, getter_AddRefs(mythread));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2011-09-08 08:37:43 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
nsCOMPtr<nsIRunnable> runnable =
|
|
|
|
new AsyncLocalFileWinOperation(AsyncLocalFileWinOperation::RevealOp,
|
|
|
|
mResolvedPath);
|
2011-09-08 08:37:43 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
// After the dispatch, the result runnable will shut down the worker
|
|
|
|
// thread, so we can let it go.
|
|
|
|
mythread->Dispatch(runnable, NS_DISPATCH_NORMAL);
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLocalFile::Launch()
|
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
// This API should be main thread only
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2012-02-26 13:46:38 -08:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
// make sure mResolvedPath is set
|
|
|
|
nsresult rv = Resolve();
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return rv;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2012-02-26 13:46:38 -08:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
// To create a new thread, get the thread manager
|
|
|
|
nsCOMPtr<nsIThreadManager> tm = do_GetService(NS_THREADMANAGER_CONTRACTID);
|
|
|
|
nsCOMPtr<nsIThread> mythread;
|
|
|
|
rv = tm->NewThread(0, 0, getter_AddRefs(mythread));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2012-02-26 13:46:38 -08:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
nsCOMPtr<nsIRunnable> runnable =
|
|
|
|
new AsyncLocalFileWinOperation(AsyncLocalFileWinOperation::LaunchOp,
|
|
|
|
mResolvedPath);
|
2012-02-26 13:46:38 -08:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
// After the dispatch, the result runnable will shut down the worker
|
|
|
|
// thread, so we can let it go.
|
|
|
|
mythread->Dispatch(runnable, NS_DISPATCH_NORMAL);
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-05-14 13:15:46 -07:00
|
|
|
NS_NewLocalFile(const nsAString& aPath, bool aFollowLinks, nsIFile** aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
nsLocalFile* file = new nsLocalFile();
|
|
|
|
if (file == nullptr)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
NS_ADDREF(file);
|
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
file->SetFollowLinks(aFollowLinks);
|
2014-05-05 10:30:43 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
if (!aPath.IsEmpty()) {
|
|
|
|
nsresult rv = file->InitWithPath(aPath);
|
2014-05-05 10:30:43 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_RELEASE(file);
|
|
|
|
return rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
*aResult = file;
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Native (lossy) interface
|
|
|
|
//-----------------------------------------------------------------------------
|
2014-05-05 10:30:43 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP
|
2014-05-14 13:15:46 -07:00
|
|
|
nsLocalFile::InitWithNativePath(const nsACString& aFilePath)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
nsAutoString tmp;
|
2014-05-14 13:15:46 -07:00
|
|
|
nsresult rv = NS_CopyNativeToUnicode(aFilePath, tmp);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return InitWithPath(tmp);
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
return rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-14 13:15:46 -07:00
|
|
|
nsLocalFile::AppendNative(const nsACString& aNode)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
nsAutoString tmp;
|
2014-05-14 13:15:46 -07:00
|
|
|
nsresult rv = NS_CopyNativeToUnicode(aNode, tmp);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return Append(tmp);
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
return rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-14 13:15:46 -07:00
|
|
|
nsLocalFile::AppendRelativeNativePath(const nsACString& aNode)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
nsAutoString tmp;
|
2014-05-14 13:15:46 -07:00
|
|
|
nsresult rv = NS_CopyNativeToUnicode(aNode, tmp);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return AppendRelativePath(tmp);
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
return rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-14 13:15:46 -07:00
|
|
|
nsLocalFile::GetNativeLeafName(nsACString& aLeafName)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
//NS_WARNING("This API is lossy. Use GetLeafName !");
|
|
|
|
nsAutoString tmp;
|
|
|
|
nsresult rv = GetLeafName(tmp);
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
rv = NS_CopyUnicodeToNative(tmp, aLeafName);
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
return rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-14 13:15:46 -07:00
|
|
|
nsLocalFile::SetNativeLeafName(const nsACString& aLeafName)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
nsAutoString tmp;
|
|
|
|
nsresult rv = NS_CopyNativeToUnicode(aLeafName, tmp);
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return SetLeafName(tmp);
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
return rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-14 13:15:46 -07:00
|
|
|
nsLocalFile::GetNativePath(nsACString& aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
//NS_WARNING("This API is lossy. Use GetPath !");
|
|
|
|
nsAutoString tmp;
|
|
|
|
nsresult rv = GetPath(tmp);
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = NS_CopyUnicodeToNative(tmp, aResult);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
return rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-14 13:15:46 -07:00
|
|
|
nsLocalFile::GetNativeCanonicalPath(nsACString& aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
NS_WARNING("This method is lossy. Use GetCanonicalPath !");
|
|
|
|
EnsureShortPath();
|
|
|
|
NS_CopyUnicodeToNative(mShortWorkingPath, aResult);
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-14 13:15:46 -07:00
|
|
|
nsLocalFile::CopyToNative(nsIFile* aNewParentDir, const nsACString& aNewName)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
// Check we are correctly initialized.
|
|
|
|
CHECK_mWorkingPath();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
if (aNewName.IsEmpty()) {
|
|
|
|
return CopyTo(aNewParentDir, EmptyString());
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
nsAutoString tmp;
|
2014-05-14 13:15:46 -07:00
|
|
|
nsresult rv = NS_CopyNativeToUnicode(aNewName, tmp);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
return CopyTo(aNewParentDir, tmp);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
return rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-14 13:15:46 -07:00
|
|
|
nsLocalFile::CopyToFollowingLinksNative(nsIFile* aNewParentDir,
|
|
|
|
const nsACString& aNewName)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-14 13:15:46 -07:00
|
|
|
if (aNewName.IsEmpty()) {
|
|
|
|
return CopyToFollowingLinks(aNewParentDir, EmptyString());
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
nsAutoString tmp;
|
2014-05-14 13:15:46 -07:00
|
|
|
nsresult rv = NS_CopyNativeToUnicode(aNewName, tmp);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
return CopyToFollowingLinks(aNewParentDir, tmp);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
return rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-14 13:15:46 -07:00
|
|
|
nsLocalFile::MoveToNative(nsIFile* aNewParentDir, const nsACString& aNewName)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
// Check we are correctly initialized.
|
|
|
|
CHECK_mWorkingPath();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
if (aNewName.IsEmpty()) {
|
|
|
|
return MoveTo(aNewParentDir, EmptyString());
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
nsAutoString tmp;
|
2014-05-14 13:15:46 -07:00
|
|
|
nsresult rv = NS_CopyNativeToUnicode(aNewName, tmp);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
return MoveTo(aNewParentDir, tmp);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
return rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-14 13:15:46 -07:00
|
|
|
nsLocalFile::GetNativeTarget(nsACString& aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
// Check we are correctly initialized.
|
|
|
|
CHECK_mWorkingPath();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
NS_WARNING("This API is lossy. Use GetTarget !");
|
|
|
|
nsAutoString tmp;
|
|
|
|
nsresult rv = GetTarget(tmp);
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = NS_CopyUnicodeToNative(tmp, aResult);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
return rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-05-14 13:15:46 -07:00
|
|
|
NS_NewNativeLocalFile(const nsACString& aPath, bool aFollowLinks,
|
|
|
|
nsIFile** aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
nsAutoString buf;
|
2014-05-14 13:15:46 -07:00
|
|
|
nsresult rv = NS_CopyNativeToUnicode(aPath, buf);
|
2014-05-05 10:30:43 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
2014-05-14 13:15:46 -07:00
|
|
|
*aResult = nullptr;
|
2014-05-05 10:30:43 -07:00
|
|
|
return rv;
|
|
|
|
}
|
2014-05-14 13:15:46 -07:00
|
|
|
return NS_NewLocalFile(buf, aFollowLinks, aResult);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsLocalFile::EnsureShortPath()
|
|
|
|
{
|
2014-05-14 13:15:46 -07:00
|
|
|
if (!mShortWorkingPath.IsEmpty()) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
|
|
|
|
WCHAR shortPath[MAX_PATH + 1];
|
|
|
|
DWORD lengthNeeded = ::GetShortPathNameW(mWorkingPath.get(), shortPath,
|
|
|
|
ArrayLength(shortPath));
|
|
|
|
// If an error occurred then lengthNeeded is set to 0 or the length of the
|
|
|
|
// needed buffer including null termination. If it succeeds the number of
|
|
|
|
// wide characters not including null termination is returned.
|
2014-05-14 13:15:46 -07:00
|
|
|
if (lengthNeeded != 0 && lengthNeeded < ArrayLength(shortPath)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
mShortWorkingPath.Assign(shortPath);
|
2014-05-14 13:15:46 -07:00
|
|
|
} else {
|
2014-05-05 10:30:43 -07:00
|
|
|
mShortWorkingPath.Assign(mWorkingPath);
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// nsIHashable
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-14 13:15:46 -07:00
|
|
|
nsLocalFile::Equals(nsIHashable* aOther, bool* aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
nsCOMPtr<nsIFile> otherfile(do_QueryInterface(aOther));
|
|
|
|
if (!otherfile) {
|
|
|
|
*aResult = false;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
return Equals(otherfile, aResult);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-14 13:15:46 -07:00
|
|
|
nsLocalFile::GetHashCode(uint32_t* aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
// In order for short and long path names to hash to the same value we
|
|
|
|
// always hash on the short pathname.
|
|
|
|
EnsureShortPath();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
*aResult = HashString(mShortWorkingPath);
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsLocalFile <static members>
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void
|
|
|
|
nsLocalFile::GlobalInit()
|
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
DebugOnly<nsresult> rv = NS_CreateShortcutResolver();
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "Shortcut resolver could not be created");
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsLocalFile::GlobalShutdown()
|
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
NS_DestroyShortcutResolver();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-04-27 00:06:00 -07:00
|
|
|
NS_IMPL_ISUPPORTS(nsDriveEnumerator, nsISimpleEnumerator)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsDriveEnumerator::nsDriveEnumerator()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsDriveEnumerator::~nsDriveEnumerator()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
nsresult
|
|
|
|
nsDriveEnumerator::Init()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
/* If the length passed to GetLogicalDriveStrings is smaller
|
|
|
|
* than the length of the string it would return, it returns
|
|
|
|
* the length required for the string. */
|
|
|
|
DWORD length = GetLogicalDriveStringsW(0, 0);
|
|
|
|
/* The string is null terminated */
|
2014-05-14 13:15:46 -07:00
|
|
|
if (!mDrives.SetLength(length + 1, fallible_t())) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
|
|
|
if (!GetLogicalDriveStringsW(length, wwc(mDrives.BeginWriting()))) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
mDrives.BeginReading(mStartOfCurrentDrive);
|
|
|
|
mDrives.EndReading(mEndOfDrivesString);
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDriveEnumerator::HasMoreElements(bool* aHasMore)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
*aHasMore = *mStartOfCurrentDrive != L'\0';
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDriveEnumerator::GetNext(nsISupports** aNext)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
/* GetLogicalDrives stored in mDrives is a concatenation
|
|
|
|
* of null terminated strings, followed by a null terminator.
|
|
|
|
* mStartOfCurrentDrive is an iterator pointing at the first
|
|
|
|
* character of the current drive. */
|
|
|
|
if (*mStartOfCurrentDrive == L'\0') {
|
|
|
|
*aNext = nullptr;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2009-02-26 09:34:07 -08:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
nsAString::const_iterator driveEnd = mStartOfCurrentDrive;
|
|
|
|
FindCharInReadable(L'\0', driveEnd, mEndOfDrivesString);
|
|
|
|
nsString drive(Substring(mStartOfCurrentDrive, driveEnd));
|
|
|
|
mStartOfCurrentDrive = ++driveEnd;
|
2009-02-26 09:34:07 -08:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
nsIFile* file;
|
2014-05-05 10:30:43 -07:00
|
|
|
nsresult rv = NS_NewLocalFile(drive, false, &file);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
*aNext = file;
|
|
|
|
return rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|