Bug 1428543 - Add mozilla::filesystem::Path and use it in nsIFile. r?froydnj
Currently only |value_type| is implemented.
MozReview-Commit-ID: 1mejzvkuako
new file mode 100644
--- /dev/null
+++ b/mfbt/Path.h
@@ -0,0 +1,32 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* 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/. */
+
+/* Represents the native path format on the platform. */
+
+#ifndef mozilla_Path_h
+#define mozilla_Path_h
+
+namespace mozilla {
+namespace filesystem {
+
+/*
+ * Mozilla vaiant of std::filesystem::path.
+ * Only |value_type| is implemented at the moment.
+ */
+class Path
+{
+public:
+#ifdef XP_WIN
+ using value_type = char16_t;
+#else
+ using value_type = char;
+#endif
+};
+
+} /* namespace filesystem */
+} /* namespace mozilla */
+
+#endif /* mozilla_Path_h */
--- a/mfbt/moz.build
+++ b/mfbt/moz.build
@@ -58,16 +58,17 @@ EXPORTS.mozilla = [
'MemoryChecking.h',
'MemoryReporting.h',
'Move.h',
'NotNull.h',
'NullPtr.h',
'Opaque.h',
'OperatorNewExtensions.h',
'Pair.h',
+ 'Path.h',
'PodOperations.h',
'Poison.h',
'Range.h',
'RangedArray.h',
'RangedPtr.h',
'ReentrancyGuard.h',
'RefCounted.h',
'RefCountType.h',
--- a/xpcom/io/FileDescriptorFile.cpp
+++ b/xpcom/io/FileDescriptorFile.cpp
@@ -115,20 +115,20 @@ FileDescriptorFile::GetNativeTarget(nsAC
}
nsresult
FileDescriptorFile::GetPath(nsAString& aRetVal)
{
return mFile->GetPath(aRetVal);
}
-NS_IMETHODIMP
-FileDescriptorFile::GetNativePath(nsACString& aRetVal)
+PathString
+FileDescriptorFile::NativePath()
{
- return mFile->GetNativePath(aRetVal);
+ return mFile->NativePath();
}
NS_IMETHODIMP
FileDescriptorFile::Equals(nsIFile* aOther, bool* aRetVal)
{
return mFile->Equals(aOther, aRetVal);
}
--- a/xpcom/io/nsIFile.idl
+++ b/xpcom/io/nsIFile.idl
@@ -4,21 +4,28 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "nsISupports.idl"
%{C++
struct PRFileDesc;
struct PRLibrary;
#include <stdio.h>
+#include "mozilla/Path.h"
+#include "nsStringFwd.h"
+namespace mozilla {
+using PathString = nsTString<filesystem::Path::value_type>;
+using PathSubstring = nsTSubstring<filesystem::Path::value_type>;
+} // namespace mozilla
%}
[ptr] native PRFileDescStar(PRFileDesc);
[ptr] native PRLibraryStar(PRLibrary);
[ptr] native FILE(FILE);
+native PathString(mozilla::PathString);
interface nsISimpleEnumerator;
/**
* An nsIFile is an abstract representation of a filename. It manages
* filename encoding issues, pathname component separators ('/' vs. '\\'
* vs. ':') and weird stuff like differing volumes with identical names, as
* on pre-Darwin Macintoshes.
@@ -246,17 +253,20 @@ interface nsIFile : nsISupports
*
* Note that the ACString attributes are returned in the
* native filesystem charset.
*
*/
readonly attribute AString target;
[noscript] readonly attribute ACString nativeTarget;
readonly attribute AString path;
- [noscript] readonly attribute ACString nativePath;
+ [notxpcom,nostdcall] PathString nativePath();
+%{C++
+ nsresult GetNativePath(nsACString& aPath);
+%}
boolean exists();
boolean isWritable();
boolean isReadable();
boolean isExecutable();
boolean isHidden();
boolean isDirectory();
boolean isFile();
--- a/xpcom/io/nsLocalFileUnix.cpp
+++ b/xpcom/io/nsLocalFileUnix.cpp
@@ -586,20 +586,26 @@ NS_IMETHODIMP
nsLocalFile::SetNativeLeafName(const nsACString& aLeafName)
{
nsACString::const_iterator begin, end;
LocateNativeLeafName(begin, end);
mPath.Replace(begin.get() - mPath.get(), Distance(begin, end), aLeafName);
return NS_OK;
}
-NS_IMETHODIMP
-nsLocalFile::GetNativePath(nsACString& aResult)
+nsCString
+nsLocalFile::NativePath()
{
- aResult = mPath;
+ return mPath;
+}
+
+nsresult
+nsIFile::GetNativePath(nsACString& aResult)
+{
+ aResult = NativePath();
return NS_OK;
}
nsresult
nsLocalFile::GetNativeTargetPathName(nsIFile* aNewParent,
const nsACString& aNewName,
nsACString& aResult)
{
--- a/xpcom/io/nsLocalFileWin.cpp
+++ b/xpcom/io/nsLocalFileWin.cpp
@@ -3547,27 +3547,27 @@ nsLocalFile::SetNativeLeafName(const nsA
if (NS_SUCCEEDED(rv)) {
return SetLeafName(tmp);
}
return rv;
}
-NS_IMETHODIMP
-nsLocalFile::GetNativePath(nsACString& aResult)
+nsString
+nsLocalFile::NativePath()
+{
+ return mWorkingPath;
+}
+
+nsresult
+nsIFile::GetNativePath(nsACString& aResult)
{
//NS_WARNING("This API is lossy. Use GetPath !");
- nsAutoString tmp;
- nsresult rv = GetPath(tmp);
- if (NS_SUCCEEDED(rv)) {
- rv = NS_CopyUnicodeToNative(tmp, aResult);
- }
-
- return rv;
+ return NS_CopyUnicodeToNative(NativePath(), aResult);
}
NS_IMETHODIMP
nsLocalFile::GetNativeCanonicalPath(nsACString& aResult)
{
NS_WARNING("This method is lossy. Use GetCanonicalPath !");
EnsureShortPath();