Bug 225255 - Start leveraging std::filesystem in WTF::FileSystem
Summary: Start leveraging std::filesystem in WTF::FileSystem
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: Web Template Framework (show other bugs)
Version: WebKit Nightly Build
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Chris Dumez
URL:
Keywords: InRadar
Depends on: 225327
Blocks:
  Show dependency treegraph
 
Reported: 2021-04-30 15:32 PDT by Chris Dumez
Modified: 2021-05-03 19:48 PDT (History)
18 users (show)

See Also:


Attachments
Patch (43.88 KB, patch)
2021-04-30 15:38 PDT, Chris Dumez
ews-feeder: commit-queue-
Details | Formatted Diff | Diff
Patch (44.31 KB, patch)
2021-04-30 15:41 PDT, Chris Dumez
ews-feeder: commit-queue-
Details | Formatted Diff | Diff
Patch (44.52 KB, patch)
2021-04-30 16:04 PDT, Chris Dumez
ews-feeder: commit-queue-
Details | Formatted Diff | Diff
Patch (45.27 KB, patch)
2021-04-30 16:24 PDT, Chris Dumez
ews-feeder: commit-queue-
Details | Formatted Diff | Diff
Patch (44.52 KB, patch)
2021-04-30 16:48 PDT, Chris Dumez
no flags Details | Formatted Diff | Diff
Patch (45.09 KB, patch)
2021-04-30 18:03 PDT, Chris Dumez
ews-feeder: commit-queue-
Details | Formatted Diff | Diff
Patch (45.09 KB, patch)
2021-04-30 18:07 PDT, Chris Dumez
no flags Details | Formatted Diff | Diff
Patch (45.35 KB, patch)
2021-05-01 10:30 PDT, Chris Dumez
no flags Details | Formatted Diff | Diff
Patch (45.65 KB, patch)
2021-05-01 13:47 PDT, Chris Dumez
no flags Details | Formatted Diff | Diff
Patch (210.86 KB, patch)
2021-05-01 14:01 PDT, Chris Dumez
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Chris Dumez 2021-04-30 15:32:18 PDT
Start leveraging std::filesystem in WTF::FileSystem to reduce the amount of platform-specific code.
Comment 1 Chris Dumez 2021-04-30 15:38:15 PDT
Created attachment 427460 [details]
Patch
Comment 2 Chris Dumez 2021-04-30 15:41:44 PDT
Created attachment 427461 [details]
Patch
Comment 3 Chris Dumez 2021-04-30 16:04:46 PDT
Created attachment 427466 [details]
Patch
Comment 4 Chris Dumez 2021-04-30 16:24:22 PDT
Created attachment 427467 [details]
Patch
Comment 5 Chris Dumez 2021-04-30 16:48:40 PDT
Created attachment 427468 [details]
Patch
Comment 6 Chris Dumez 2021-04-30 18:03:02 PDT
Created attachment 427474 [details]
Patch
Comment 7 Chris Dumez 2021-04-30 18:07:10 PDT
Created attachment 427476 [details]
Patch
Comment 8 Sam Weinig 2021-05-01 09:35:45 PDT
Awesome!
Comment 9 Chris Dumez 2021-05-01 09:38:03 PDT
API-gtk ews keeps saying I broke WTF.StringOperators API test. I am not quite sure how that’s possible. This test does not seem to rely on the file system api in any way…
Comment 10 Sam Weinig 2021-05-01 09:42:58 PDT
Comment on attachment 427476 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=427476&action=review

> Source/WTF/ChangeLog:9
> +        Start leveraging std::filesystem in WTF::FileSystem to reduce the amount of
> +        platform-specific code.

I think doing this in WTF::FileSystem at first is a good idea, but I would also like to eventually see WTF::FileSystem removed in favor of direct usage of std::filesystem if we can. Time will tell.

> Source/WTF/wtf/FileSystem.cpp:52
> +namespace fs = std::filesystem;

Not a huge fan of this. I think having longer names is probably ok, but I also think this is acceptable in a cpp file.

> Source/WTF/wtf/FileSystem.cpp:491
> +    return fs::exists(fileSystemRepresentation(path).data(), ec);

Would be good to explain why the error code is not being checked, since it seems odd at first glance.

> Source/WTF/wtf/FileSystem.cpp:501
> +#if !PLATFORM(MAC)

Would be good to have a comment explaining why macOS has a non-default implementation.
Comment 11 Sam Weinig 2021-05-01 09:45:07 PDT
I can't remember if we have officially dropped 10.14 yet, but if we have, we can also remove the copy of std::filesystem we have in WTF called StdFilesystem.h/cpp.
Comment 12 Chris Dumez 2021-05-01 10:21:54 PDT
(In reply to Sam Weinig from comment #10)
> Comment on attachment 427476 [details]
> Patch
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=427476&action=review
> 
> > Source/WTF/ChangeLog:9
> > +        Start leveraging std::filesystem in WTF::FileSystem to reduce the amount of
> > +        platform-specific code.
> 
> I think doing this in WTF::FileSystem at first is a good idea, but I would
> also like to eventually see WTF::FileSystem removed in favor of direct usage
> of std::filesystem if we can. Time will tell.

I see the WTF::FileSystem as a layer over std::filesystem allowing use to use WTF::String, which is convenient. Having to deal with conversion to and from file system representations would be annoying and potentially error-prone.

> > Source/WTF/wtf/FileSystem.cpp:52
> > +namespace fs = std::filesystem;
> 
> Not a huge fan of this. I think having longer names is probably ok, but I
> also think this is acceptable in a cpp file.

Ok. I will revert. I don't feel super strongly.

> > Source/WTF/wtf/FileSystem.cpp:491
> > +    return fs::exists(fileSystemRepresentation(path).data(), ec);
> 
> Would be good to explain why the error code is not being checked, since it
> seems odd at first glance.

Ok. Basically, exists() returns false in case of error so it is not useful to check ec. This function was already returning false in case of error so the behavior is maintained as far as I can tell.

> 
> > Source/WTF/wtf/FileSystem.cpp:501
> > +#if !PLATFORM(MAC)
> 
> Would be good to have a comment explaining why macOS has a non-default
> implementation.

I am planning to improve this in a follow-up. Basically the issue is that on Mac, we need to support removing a folder that has a hidden .DS_Store file in there. I'll add a comment for now but I'll improve things and write a test later on.
Comment 13 Chris Dumez 2021-05-01 10:29:25 PDT
(In reply to Sam Weinig from comment #11)
> I can't remember if we have officially dropped 10.14 yet, but if we have, we
> can also remove the copy of std::filesystem we have in WTF called
> StdFilesystem.h/cpp.

I will try and find out. I hope we have because the copy of std::filesystem in StdFilesystem.h doesn't seem to contain nearly enough API to support this patch.
Comment 14 Chris Dumez 2021-05-01 10:30:14 PDT
Created attachment 427506 [details]
Patch
Comment 15 Chris Dumez 2021-05-01 13:47:37 PDT
Created attachment 427511 [details]
Patch
Comment 16 Chris Dumez 2021-05-01 13:48:37 PDT
(In reply to Chris Dumez from comment #15)
> Created attachment 427511 [details]
> Patch

Should take care of WTF.StringOperators in gtk-ews. Turns out using operator+ for String in other API tests can cause WTF.StringOperators to fail. We should probably fix that at some point. For now, I am working around it by not using operator+.
Comment 17 Chris Dumez 2021-05-01 14:01:00 PDT
Created attachment 427512 [details]
Patch
Comment 18 Chris Dumez 2021-05-01 14:02:03 PDT
(In reply to Chris Dumez from comment #13)
> (In reply to Sam Weinig from comment #11)
> > I can't remember if we have officially dropped 10.14 yet, but if we have, we
> > can also remove the copy of std::filesystem we have in WTF called
> > StdFilesystem.h/cpp.
> 
> I will try and find out. I hope we have because the copy of std::filesystem
> in StdFilesystem.h doesn't seem to contain nearly enough API to support this
> patch.

I checked with Ryan and he said it was OK. As a result, I dropped our own copy of std::filesystem in StdFilesystem.h. I kept the StdFilesystem.h header to conditional include the experimental version when needed.
Comment 19 EWS 2021-05-01 19:36:22 PDT
Committed r276879 (237225@main): <https://commits.webkit.org/237225@main>

All reviewed patches have been landed. Closing bug and clearing flags on attachment 427512 [details].
Comment 20 Radar WebKit Bug Importer 2021-05-01 19:37:13 PDT
<rdar://problem/77425312>
Comment 21 Sam Weinig 2021-05-02 08:42:25 PDT
(In reply to Chris Dumez from comment #18)
> (In reply to Chris Dumez from comment #13)
> > (In reply to Sam Weinig from comment #11)
> > > I can't remember if we have officially dropped 10.14 yet, but if we have, we
> > > can also remove the copy of std::filesystem we have in WTF called
> > > StdFilesystem.h/cpp.
> > 
> > I will try and find out. I hope we have because the copy of std::filesystem
> > in StdFilesystem.h doesn't seem to contain nearly enough API to support this
> > patch.
> 
> I checked with Ryan and he said it was OK. As a result, I dropped our own
> copy of std::filesystem in StdFilesystem.h. I kept the StdFilesystem.h
> header to conditional include the experimental version when needed.

Excellent. Thanks for following up!

I added StdFilesystem.h to make work in WebKitTestRunner and DumpRenderTree nicer but am very glad to see it gone.
Comment 22 Darin Adler 2021-05-02 17:38:08 PDT
This patch removes some features from WTF::FileSystem that std::filesystem chose not to include. We need to decide whether they are valuable:

WTF::FileSystem::deleteEmptyDirectory and WTF::FileSystem::deleteNonEmptyDirectory on a file: The std::filesystem functions work on both files and directories, so these have lost their "if it’s a file just leave it alone" semantic, but without changing the function names.

WTF::FileSystem::deleteFile on a directory: The std::filesystem functions work on both files and directories, so it have lost its "if it’s a directory just leave it alone" semantic, but we made that change without changing the function name.

WTF::FileSystem::moveFile: The Cocoa implementation of this function had the power to move files across file systems, by copying and then deleting the original. As did the Windows version and probably the others as well. I don’t think that std::filesystem::rename does this. Not sure if it matters; need to audit callers to see if they need this cross-file-system behavior.

There may be other changes as well. I don’t object to this change, but really we should make WTF::FileSystem a thinner layer on top of std::filesystem and remove the names that imply we have features that we no longer have.

And in the process look very carefully at code calling these functions to see if there were benefits to the old implementation that are lost and make sure we made an informed decision.
Comment 23 Chris Dumez 2021-05-02 18:23:13 PDT
(In reply to Darin Adler from comment #22)
> This patch removes some features from WTF::FileSystem that std::filesystem
> chose not to include. We need to decide whether they are valuable:
> 
> WTF::FileSystem::deleteEmptyDirectory and
> WTF::FileSystem::deleteNonEmptyDirectory on a file: The std::filesystem
> functions work on both files and directories, so these have lost their "if
> it’s a file just leave it alone" semantic, but without changing the function
> names.
> 
> WTF::FileSystem::deleteFile on a directory: The std::filesystem functions
> work on both files and directories, so it have lost its "if it’s a directory
> just leave it alone" semantic, but we made that change without changing the
> function name.
> 
> WTF::FileSystem::moveFile: The Cocoa implementation of this function had the
> power to move files across file systems, by copying and then deleting the
> original. As did the Windows version and probably the others as well. I
> don’t think that std::filesystem::rename does this. Not sure if it matters;
> need to audit callers to see if they need this cross-file-system behavior.
> 
> There may be other changes as well. I don’t object to this change, but
> really we should make WTF::FileSystem a thinner layer on top of
> std::filesystem and remove the names that imply we have features that we no
> longer have.
> 
> And in the process look very carefully at code calling these functions to
> see if there were benefits to the old implementation that are lost and make
> sure we made an informed decision.

Thanks for pointing this out. I was trying to maintain previous behavior to reduce the chance of breakage but it looks like I missed some subtleties. I will follow up.
Comment 24 Chris Dumez 2021-05-03 07:47:45 PDT
(In reply to Darin Adler from comment #22)
> This patch removes some features from WTF::FileSystem that std::filesystem
> chose not to include. We need to decide whether they are valuable:
> 
> WTF::FileSystem::deleteEmptyDirectory and
> WTF::FileSystem::deleteNonEmptyDirectory on a file: The std::filesystem
> functions work on both files and directories, so these have lost their "if
> it’s a file just leave it alone" semantic, but without changing the function
> names.
> 
> WTF::FileSystem::deleteFile on a directory: The std::filesystem functions
> work on both files and directories, so it have lost its "if it’s a directory
> just leave it alone" semantic, but we made that change without changing the
> function name.

Restoring previous behavior via Bug 225289.

> 
> WTF::FileSystem::moveFile: The Cocoa implementation of this function had the
> power to move files across file systems, by copying and then deleting the
> original. As did the Windows version and probably the others as well. I
> don’t think that std::filesystem::rename does this. Not sure if it matters;
> need to audit callers to see if they need this cross-file-system behavior.

There are not many call sites:
1. IDBServer::renameOrigin(): Simple rename within the same directory so the new implementation is fine.
2. SQLiteIDBBackingStore::fullDatabaseDirectoryWithUpgrade(): Simple rename of a directory
3. CDMInstanceFairPlayStreamingAVFObjC::setStorageDirectory(): It is calling FileSystem::createTemporaryDirectory(@"MediaKeys") and then moving that directory to the CDMInstanceFairPlayStreamingAVFObjC storage directory. I think that in theory at least, the temp directory and the actual temp directory could be on different volumes so we may have an issue here. I'll look into it.
4. CurlDownload::curlDidComplete(): Moves files from FileSystem::openTemporaryFile("download", m_downloadFileHandle) to the download destination. In theory, those could be on different Volumes I think.
5. StorageManager::renameOrigin(): Simple rename within a folder

So it looks like there are 2 call sites that may be problematic. I have not checked yet what std::filesystem::rename() does across volumes.
Comment 25 Chris Dumez 2021-05-03 08:29:14 PDT
(In reply to Chris Dumez from comment #24)
> (In reply to Darin Adler from comment #22)
> > This patch removes some features from WTF::FileSystem that std::filesystem
> > chose not to include. We need to decide whether they are valuable:
> > 
> > WTF::FileSystem::deleteEmptyDirectory and
> > WTF::FileSystem::deleteNonEmptyDirectory on a file: The std::filesystem
> > functions work on both files and directories, so these have lost their "if
> > it’s a file just leave it alone" semantic, but without changing the function
> > names.
> > 
> > WTF::FileSystem::deleteFile on a directory: The std::filesystem functions
> > work on both files and directories, so it have lost its "if it’s a directory
> > just leave it alone" semantic, but we made that change without changing the
> > function name.
> 
> Restoring previous behavior via Bug 225289.
> 
> > 
> > WTF::FileSystem::moveFile: The Cocoa implementation of this function had the
> > power to move files across file systems, by copying and then deleting the
> > original. As did the Windows version and probably the others as well. I
> > don’t think that std::filesystem::rename does this. Not sure if it matters;
> > need to audit callers to see if they need this cross-file-system behavior.
> 
> There are not many call sites:
> 1. IDBServer::renameOrigin(): Simple rename within the same directory so the
> new implementation is fine.
> 2. SQLiteIDBBackingStore::fullDatabaseDirectoryWithUpgrade(): Simple rename
> of a directory
> 3. CDMInstanceFairPlayStreamingAVFObjC::setStorageDirectory(): It is calling
> FileSystem::createTemporaryDirectory(@"MediaKeys") and then moving that
> directory to the CDMInstanceFairPlayStreamingAVFObjC storage directory. I
> think that in theory at least, the temp directory and the actual temp
> directory could be on different volumes so we may have an issue here. I'll
> look into it.
> 4. CurlDownload::curlDidComplete(): Moves files from
> FileSystem::openTemporaryFile("download", m_downloadFileHandle) to the
> download destination. In theory, those could be on different Volumes I think.
> 5. StorageManager::renameOrigin(): Simple rename within a folder
> 
> So it looks like there are 2 call sites that may be problematic. I have not
> checked yet what std::filesystem::rename() does across volumes.

Give that call sites seem to need it, I am adding back support for moving across volumes via Bug 225307.