WebKit Bugzilla
Attachment 361644 Details for
Bug 194344
: [WTF] Add getting/changing current working directory to FileSystem
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Requests
|
Help
|
New Account
|
Log In
Remember
[x]
|
Forgot Password
Login:
[x]
[patch]
Add get/change current working directory functions
194344.3.diff (text/plain), 10.96 KB, created by
Stephan Szabo
on 2019-02-10 14:57:43 PST
(
hide
)
Description:
Add get/change current working directory functions
Filename:
MIME Type:
Creator:
Stephan Szabo
Created:
2019-02-10 14:57:43 PST
Size:
10.96 KB
patch
obsolete
>diff --git a/Source/WTF/ChangeLog b/Source/WTF/ChangeLog >index 13ca9221fd9..56ca79cd7b7 100644 >--- a/Source/WTF/ChangeLog >+++ b/Source/WTF/ChangeLog >@@ -1,3 +1,23 @@ >+2019-02-10 Stephan Szabo <stephan.szabo@sony.com> >+ >+ [WTF] Add getting/changing current working directory to FileSystem >+ https://bugs.webkit.org/show_bug.cgi?id=194344 >+ >+ Reviewed by NOBODY (OOPS!). >+ >+ * wtf/FileSystem.h: >+ Add new getCurrentWorkingDirectory and >+ changeCurrentWorkingDirectory methods. >+ * wtf/PlatformPlayStation.cmake: >+ Add new FileSystemPlayStation.cpp to build >+ * wtf/glib/FileSystemGlib.cpp: >+ * wtf/playstation/FileSystemPlayStation.cpp: Added. >+ As PlayStation doesn't provide user programs the same >+ sort of cwd/relative path support as other systems, >+ simulate it. >+ * wtf/posix/FileSystemPOSIX.cpp: >+ * wtf/win/FileSystemWin.cpp: >+ > 2019-02-03 Commit Queue <commit-queue@webkit.org> > > Unreviewed, rolling out r240896. >diff --git a/Source/WTF/wtf/FileSystem.h b/Source/WTF/wtf/FileSystem.h >index 903962ad46d..039bde49c76 100644 >--- a/Source/WTF/wtf/FileSystem.h >+++ b/Source/WTF/wtf/FileSystem.h >@@ -187,6 +187,9 @@ WTF_EXPORT_PRIVATE bool deleteNonEmptyDirectory(const String&); > > WTF_EXPORT_PRIVATE String realPath(const String&); > >+WTF_EXPORT_PRIVATE Optional<String> getCurrentWorkingDirectory(); >+WTF_EXPORT_PRIVATE bool changeCurrentWorkingDirectory(const String&); >+ > class MappedFileData { > public: > MappedFileData() { } >diff --git a/Source/WTF/wtf/PlatformPlayStation.cmake b/Source/WTF/wtf/PlatformPlayStation.cmake >index 4b6919076f3..937812a2e3c 100644 >--- a/Source/WTF/wtf/PlatformPlayStation.cmake >+++ b/Source/WTF/wtf/PlatformPlayStation.cmake >@@ -5,6 +5,8 @@ list(APPEND WTF_SOURCES > generic/RunLoopGeneric.cpp > generic/WorkQueueGeneric.cpp > >+ playstation/FileSystemPlayStation.cpp >+ > posix/FileSystemPOSIX.cpp > posix/OSAllocatorPOSIX.cpp > posix/ThreadingPOSIX.cpp >diff --git a/Source/WTF/wtf/glib/FileSystemGlib.cpp b/Source/WTF/wtf/glib/FileSystemGlib.cpp >index c3ec99a3d1b..b2c577bb92a 100644 >--- a/Source/WTF/wtf/glib/FileSystemGlib.cpp >+++ b/Source/WTF/wtf/glib/FileSystemGlib.cpp >@@ -465,5 +465,20 @@ bool unlockFile(PlatformFileHandle handle) > } > #endif // USE(FILE_LOCK) > >+Optional<String> getCurrentWorkingDirectory() >+{ >+ GUniquePtr<char> currentDir(g_get_current_dir()); >+ return stringFromFileSystemRepresentation(currentDir.get()); >+} >+ >+bool changeCurrentWorkingDirectory(const String& filePath) >+{ >+#if OS(WINDOWS) >+ return SetCurrentWorkingDirectory(filePath.charactersWithNullTermination().data()); >+#else >+ return !chdir(fileSystemRepresentation(filePath).data()); >+#endif >+} >+ > } // namespace FileSystemImpl > } // namespace WTF >diff --git a/Source/WTF/wtf/playstation/FileSystemPlayStation.cpp b/Source/WTF/wtf/playstation/FileSystemPlayStation.cpp >new file mode 100644 >index 00000000000..678acf11659 >--- /dev/null >+++ b/Source/WTF/wtf/playstation/FileSystemPlayStation.cpp >@@ -0,0 +1,71 @@ >+/* >+ * Copyright (C) 2019 Sony Interactive Entertainment Inc. >+ * >+ * Redistribution and use in source and binary forms, with or without >+ * modification, are permitted provided that the following conditions >+ * are met: >+ * >+ * 1. Redistributions of source code must retain the above copyright >+ * notice, this list of conditions and the following disclaimer. >+ * 2. Redistributions in binary form must reproduce the above copyright >+ * notice, this list of conditions and the following disclaimer in the >+ * documentation and/or other materials provided with the distribution. >+ * 3. Neither the name of Apple Inc. ("Apple") nor the names of >+ * its contributors may be used to endorse or promote products derived >+ * from this software without specific prior written permission. >+ * >+ * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY >+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED >+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE >+ * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY >+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES >+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; >+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND >+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT >+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF >+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. >+ */ >+#include "config.h" >+#include <wtf/FileSystem.h> >+#include <wtf/text/StringBuilder.h> >+ >+namespace WTF { >+ >+namespace FileSystemImpl { >+ >+char currentWorkingDirectory[PATH_MAX] = {0}; >+ >+Optional<String> getCurrentWorkingDirectory() >+{ >+ if (!currentWorkingDirectory[0]) >+ return WTF::nullopt; >+ return stringFromFileSystemRepresentation(currentWorkingDirectory); >+} >+ >+bool changeCurrentWorkingDirectory(const String &filePath) >+{ >+ CString path = fileSystemRepresentation(filePath); >+ if (path.length() > sizeof(currentWorkingDirectory) - 1) >+ return false; >+ strncpy(currentWorkingDirectory, path.data(), sizeof(currentWorkingDirectory)); >+ currentWorkingDirectory[sizeof(currentWorkingDirectory)-1] = 0; >+ return true; >+} >+ >+CString fileSystemRepresentation(const String& path) >+{ >+ if (path[0] != '/') { >+ auto cwd = getCurrentWorkingDirectory(); >+ if (cwd && cwd.value().length()) { >+ StringBuilder builder; >+ builder.append(cwd.value()); >+ builder.append('/'); >+ builder.append(path); >+ return builder.toString().utf8(); >+ } >+ } >+ return path.utf8(); >+} >+ >+} // namespace FileSystemImpl >+} // namespace WTF >diff --git a/Source/WTF/wtf/posix/FileSystemPOSIX.cpp b/Source/WTF/wtf/posix/FileSystemPOSIX.cpp >index 70d2f98e4d7..e239e50b3a5 100644 >--- a/Source/WTF/wtf/posix/FileSystemPOSIX.cpp >+++ b/Source/WTF/wtf/posix/FileSystemPOSIX.cpp >@@ -391,11 +391,13 @@ String stringFromFileSystemRepresentation(const char* path) > return String::fromUTF8(path); > } > >+#if !PLATFORM(PLAYSTATION) > CString fileSystemRepresentation(const String& path) > { > return path.utf8(); > } > #endif >+#endif > > #if !PLATFORM(COCOA) > bool moveFile(const String& oldPath, const String& newPath) >@@ -492,5 +494,20 @@ String realPath(const String& filePath) > return result ? String::fromUTF8(result) : filePath; > } > >+#if !PLATFORM(PLAYSTATION) >+Optional<String> getCurrentWorkingDirectory() >+{ >+ char workingDirectory[PATH_MAX]; >+ if (!getcwd(workingDirectory, sizeof(workingDirectory))) >+ return WTF::nullopt; >+ return stringFromFileSystemRepresentation(workingDirectory); >+} >+ >+bool changeCurrentWorkingDirectory(const String& filePath) >+{ >+ return !chdir(fileSystemRepresentation(filePath).data()); >+} >+#endif >+ > } // namespace FileSystemImpl > } // namespace WTF >diff --git a/Source/WTF/wtf/win/FileSystemWin.cpp b/Source/WTF/wtf/win/FileSystemWin.cpp >index d672af63746..be872877a90 100644 >--- a/Source/WTF/wtf/win/FileSystemWin.cpp >+++ b/Source/WTF/wtf/win/FileSystemWin.cpp >@@ -582,5 +582,26 @@ bool deleteNonEmptyDirectory(const String& directoryPath) > return !SHFileOperation(&deleteOperation); > } > >+Optional<String> getCurrentWorkingDirectory() >+{ >+ DWORD bufferLength = ::GetCurrentDirectoryW(0, nullptr); >+ if (!bufferLength) >+ return WTF::nullopt; >+ // In Windows, wchar_t is the UTF-16LE. >+ // https://msdn.microsoft.com/en-us/library/dd374081.aspx >+ // https://msdn.microsoft.com/en-us/library/windows/desktop/ff381407.aspx >+ Vector<wchar_t> buffer(bufferLength); >+ DWORD lengthNotIncludingNull = ::GetCurrentDirectoryW(bufferLength, buffer.data()); >+ if (!lengthNotIncludingNull) >+ return WTF::nullopt; >+ return wcharToString(buffer.data(), lengthNotIncludingNull); >+} >+ >+bool changeCurrentWorkingDirectory(const String& filePath) >+{ >+ String destination = filePath; >+ return SetCurrentDirectoryW(stringToNullTerminatedWChar(destination).data()); >+} >+ > } // namespace FileSystemImpl > } // namespace WTF >diff --git a/Tools/ChangeLog b/Tools/ChangeLog >index 1c2a54e926a..52661391ca0 100644 >--- a/Tools/ChangeLog >+++ b/Tools/ChangeLog >@@ -1,3 +1,15 @@ >+2019-02-10 Stephan Szabo <stephan.szabo@sony.com> >+ >+ [WTF] Add getting/changing current working directory to FileSystem >+ https://bugs.webkit.org/show_bug.cgi?id=194344 >+ >+ Reviewed by NOBODY (OOPS!). >+ >+ Add test for WTF::FileSystem::getCurrentWorkingDirectory and >+ WTF::FileSystem::changeCurrentWorkingDirectory. >+ >+ * TestWebKitAPI/Tests/WTF/FileSystem.cpp: >+ > 2019-02-03 Fujii Hironori <Hironori.Fujii@sony.com> > > [Win] WebKitTestRunners is failing to create the IndexedDB directory. >diff --git a/Tools/TestWebKitAPI/Tests/WTF/FileSystem.cpp b/Tools/TestWebKitAPI/Tests/WTF/FileSystem.cpp >index 830552556de..4c37ec510ec 100644 >--- a/Tools/TestWebKitAPI/Tests/WTF/FileSystem.cpp >+++ b/Tools/TestWebKitAPI/Tests/WTF/FileSystem.cpp >@@ -63,6 +63,9 @@ public: > > m_quoteContainingFilePath = FileSystem::openTemporaryFile("temp\"Empty\"TestFile", handle); > FileSystem::closeFile(handle); >+ >+ m_tempDirectoryPath = FileSystem::pathByAppendingComponent(FileSystem::directoryName(m_tempEmptyFilePath), "tempDirectory"); >+ FileSystem::makeAllDirectories(m_tempDirectoryPath); > } > > void TearDown() override >@@ -73,6 +76,7 @@ public: > FileSystem::deleteFile(m_spaceContainingFilePath); > FileSystem::deleteFile(m_bangContainingFilePath); > FileSystem::deleteFile(m_quoteContainingFilePath); >+ FileSystem::deleteEmptyDirectory(m_tempDirectoryPath); > } > > const String& tempFilePath() { return m_tempFilePath; } >@@ -81,6 +85,7 @@ public: > const String& spaceContainingFilePath() { return m_spaceContainingFilePath; } > const String& bangContainingFilePath() { return m_bangContainingFilePath; } > const String& quoteContainingFilePath() { return m_quoteContainingFilePath; } >+ const String& tempDirectoryPath() { return m_tempDirectoryPath; } > > private: > String m_tempFilePath; >@@ -89,6 +94,7 @@ private: > String m_spaceContainingFilePath; > String m_bangContainingFilePath; > String m_quoteContainingFilePath; >+ String m_tempDirectoryPath; > }; > > TEST_F(FileSystemTest, MappingMissingFile) >@@ -145,4 +151,17 @@ TEST_F(FileSystemTest, UnicodeDirectoryName) > EXPECT_TRUE(expectedDirectoryName == directoryName); > } > >+TEST_F(FileSystemTest, CurrentWorkingDirectory) >+{ >+ auto originalPath = FileSystem::getCurrentWorkingDirectory(); >+ EXPECT_TRUE(originalPath); >+ auto change = FileSystem::changeCurrentWorkingDirectory(tempDirectoryPath()); >+ EXPECT_TRUE(change); >+ auto newPath = FileSystem::getCurrentWorkingDirectory(); >+ auto changeBack = FileSystem::changeCurrentWorkingDirectory(originalPath.value()); >+ EXPECT_TRUE(newPath); >+ EXPECT_TRUE(newPath.value() == tempDirectoryPath()); >+ EXPECT_TRUE(changeBack); >+} >+ > }
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Formatted Diff
|
Diff
Attachments on
bug 194344
:
361321
|
361404
| 361644 |
361645