Skip to content
This repository was archived by the owner on Aug 8, 2024. It is now read-only.

FileSystem.Unix.File.Move uses "rename" in more cases (#40611) - #395

Merged
baulig merged 2 commits into
mono:masterfrom
mdh1418:mdhwang/update_filesystem_file_move
Apr 2, 2020
Merged

baulig merged 2 commits into
mono:masterfrom
mdh1418:mdhwang/update_filesystem_file_move

Conversation

@mdh1418

@mdh1418 mdh1418 commented Mar 30, 2020 •

Copy link
Copy Markdown

Cherry-picked commit message

* FileSystem.Unix.File.Move use rename in more cases, avoid link/copy when possible, improve performance on file systems that do not support hard links, such as FAT

* Adapt Unit Tests for accounting FileSystemWatcher events fired by FileSystem.Unix.File.Move implementation that use rename in more cases, avoiding link/copy when possible

This PR looks to fix the MonoDevelop.Projects.DotNetCoreFileWatcherTests.AddRenameRemoveSingleFile error described in DevDiv workitem 1089785.
A FileSystemWatcher regression occurred when bots were upgraded from Mojave to Catalina.
It turns out that File Renamed events were not being sent on Catalina though the files were being properly renamed.
To fix, a cherry-pick of a commit on dotnet/corefx was applied.

Testing:

Reproduced using desktop mono (6.13.0) on macOS Catalina 10.15.3 using the sample code provided, which was uploaded here https://gist.github.com/mdh1418/f2a47838fbd9b53efa53fe18789a5ebb

Mitchells-MacBook-Pro-2:mono mdhwang$ runtime/_tmpinst/bin/csc Hello.cs; mono/mini/mono Hello.exe
Microsoft (R) Visual C# Compiler version 3.5.0-beta4-20153-05 (20b9af91)
Copyright (C) Microsoft Corporation. All rights reserved.
Watching files...
Press a key to quit.
FileChanged: /Users/mdhwang/mono/NewFile0.txt
FileCreated: /Users/mdhwang/mono/NewFile0.txt
FileDeleted: /Users/mdhwang/mono/NewFile0.txt
FileChanged: /Users/mdhwang/mono/NewFile1.txt
FileCreated: /Users/mdhwang/mono/NewFile1.txt
FileDeleted: /Users/mdhwang/mono/NewFile1.txt
FileChanged: /Users/mdhwang/mono/NewFile1.txt
FileCreated: /Users/mdhwang/mono/NewFile1.txt
FileChanged: /Users/mdhwang/mono/NewFile2.txt
FileCreated: /Users/mdhwang/mono/NewFile2.txt
FileChanged: /Users/mdhwang/mono/NewFile2.txt
FileCreated: /Users/mdhwang/mono/NewFile2.txt
FileDeleted: /Users/mdhwang/mono/NewFile2.txt
FileChanged: /Users/mdhwang/mono/NewFile3.txt
FileChanged: /Users/mdhwang/mono/NewFile3.txt
FileDeleted: /Users/mdhwang/mono/NewFile3.txt

Fixing:

Applied the changes to FileSystem.Unix.cs to mono/src/System.IO.FileSystem/src/System/IO/FileSystem.Unix.cs, ran make -j and re-ran the sample program.

lo.exe
Microsoft (R) Visual C# Compiler version 3.5.0-beta4-20153-05 (20b9af91)
Copyright (C) Microsoft Corporation. All rights reserved.

Watching files...
Press a key to quit.
FileChanged: /Users/mdhwang/mono/NewFile0.txt
FileCreated: /Users/mdhwang/mono/NewFile0.txt
FileChanged: /Users/mdhwang/mono/NewFile0.txt
FileCreated: /Users/mdhwang/mono/NewFile0.txt
FileRenamed: /Users/mdhwang/mono/NewFile0.txt -> /Users/mdhwang/mono/RenamedFile0.txt
FileChanged: /Users/mdhwang/mono/NewFile1.txt
FileCreated: /Users/mdhwang/mono/NewFile1.txt
FileChanged: /Users/mdhwang/mono/NewFile1.txt
FileCreated: /Users/mdhwang/mono/NewFile1.txt
FileRenamed: /Users/mdhwang/mono/NewFile1.txt -> /Users/mdhwang/mono/RenamedFile1.txt
FileChanged: /Users/mdhwang/mono/NewFile2.txt
FileCreated: /Users/mdhwang/mono/NewFile2.txt
FileChanged: /Users/mdhwang/mono/NewFile2.txt
FileRenamed: /Users/mdhwang/mono/NewFile2.txt -> /Users/mdhwang/mono/RenamedFile2.txt
FileCreated: /Users/mdhwang/mono/NewFile2.txt
FileChanged: /Users/mdhwang/mono/NewFile3.txt
FileCreated: /Users/mdhwang/mono/NewFile3.txt
FileRenamed: /Users/mdhwang/mono/NewFile3.txt -> /Users/mdhwang/mono/RenamedFile3.txt
FileChanged: /Users/mdhwang/mono/NewFile3.txt
FileCreated: /Users/mdhwang/mono/NewFile3.txt

Port of dotnet#40611

* FileSystem.Unix.File.Move use rename in more cases, avoid link/copy when possible, improve performance on file systems that do not support hard links, such as FAT

* Adapt Unit Tests for accounting FileSystemWatcher events fired by FileSystem.Unix.File.Move implementation that use rename in more cases, avoiding link/copy when possible
@steveisok

Copy link
Copy Markdown

@baulig Please give this a review as I don't want to regress against Android.

sourceStat.Ino == destStat.Ino && // and source and dest are the same file on that device
(Interop.Sys.LStat(destFullPath, out destStat) != 0 || // dest file does not exist
sourceStat.Ino == destStat.Ino) && // source and dest are the same file on that device
Interop.Sys.Rename(sourceFullPath, destFullPath) == 0) // try the rename

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like you removed the same device check here - sourceStat.Dev == destStat.Dev. Is this intentional?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@baulig I asked Mitch to essentially cherry pick the corefx change as it would solve the problem VS is having. I noticed the change and wanted you to guide Mitch so that we would not regress on Android. So, to answer your question, yes it was intentional, but we probably need to put your checks back in. What do you think?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, looking into it further, I'm not sure if it was intentional to call rename for the scenario in which sourceFullPath and destFullPath both lead to files that exist, but exist on different devices, yet still happen to have the same Inode number (file serial number).
It appears that the original case was to catch a simple change in file case, ("aa.txt" -> "AA.txt"), and it looks like the cherry-picked PR was intended to widen the scope in which Rename is called and lower expectations of delete events firing.

Looking at the test cases, it's not obvious to me whether or not there is a check for moving a file from one device to another. Locally adding that same device check and testing, the intended functionality of Rename events firing is maintained.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are checking whether both files have the same inode (sourceStat.Ino == destStat.Ino), but without checking for the same device (sourceStat.Dev == destStat.Dev).

The inode check alone is not enough because two different files on two different devices may have the same inode by pure chance. And when that happens, that code is now unexpectedly behaving differently. And since that will be such a rare occurrence, it will be a total nightmare to track down.

Please either put back the device check or explain in a comment why the above mentioned scenario cannot happen, as I don't see that from looking at the code.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When files exist on different devices, their inode numbers are completely meaningless and you shouldn't even attempt to "compare" them. An inode number is just a way of uniquely indexing a file in a particular file system - and it's therefore unique to that file system. They're also generated mostly randomly (not in a cryptographic sense of course!) and really have no special meaning other than ensuring uniqueness.

I spent some more time looking at that code and I believe the reason we are comparing the inode numbers is to identity the case where one of the files is a link to the other one - or when both are links to the same third file. Using lstat() is far more efficient than readlink().

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or in other words, if you ever see a piece of code that looks at an inode number without also looking at the device id - something is likely wrong.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @baulig. I missed seeing this removal, and the omission seems like an accident/error in the original PR as it is not addressed.

We should probably just re-add the line. We should bring this up in dotnet/runtime too.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the 👀. I re-added the logic to check that the source and dest files have the same device ID and inode serial number.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me now, but let's wait for the CoreFX folks to also address it on their end in dotnet/runtime#34330

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dotnet/runtime#34343 has squashed and merged

@mdh1418
mdh1418 force-pushed the mdhwang/update_filesystem_file_move branch from dd59c95 to 3509c94 Compare March 31, 2020 15:52
@mdh1418
mdh1418 force-pushed the mdhwang/update_filesystem_file_move branch from 3509c94 to 217db7e Compare March 31, 2020 16:06
@baulig
baulig merged commit bec9343 into mono:master Apr 2, 2020
mdh1418 added a commit to mdh1418/mono that referenced this pull request Apr 2, 2020
@akoeplinger

Copy link
Copy Markdown
Member

@monojenkins backport mono-2019-12

monojenkins pushed a commit to monojenkins/mono that referenced this pull request Apr 3, 2020
akoeplinger pushed a commit to mono/mono that referenced this pull request Apr 3, 2020
akoeplinger pushed a commit to mono/mono that referenced this pull request Apr 3, 2020
jonpryor added a commit to jonpryor/xamarin-android that referenced this pull request Apr 8, 2020
Changes: mono/mono@ecde086...165f4b0

  * mono/mono@165f4b03417: [2020-02][debugger] Fix NOT_IMPLEMENTED while debugging. (#19450)
  * mono/mono@b04dc7e6a48: Bump corefx to get mono/corefx#395 (#19426)
mandel-macaque added a commit to mandel-macaque/xamarin-macios that referenced this pull request Apr 8, 2020
Commits:

* mono/mono@165f4b03417: [2020-02][debugger] Fix NOT_IMPLEMENTED while debugging. (dotnet#19450)
* mono/mono@b04dc7e6a48: Bump corefx to get mono/corefx#395 (dotnet#19426)

Diff: mono/mono@ecde086...165f4b0

Related android PR: dotnet/android#4540
mandel-macaque added a commit to dotnet/macios that referenced this pull request Apr 8, 2020
Commits:

* mono/mono@165f4b03417: [2020-02][debugger] Fix NOT_IMPLEMENTED while debugging. (#19450)
* mono/mono@b04dc7e6a48: Bump corefx to get mono/corefx#395 (#19426)

Diff: mono/mono@ecde086...165f4b0

Related android PR: dotnet/android#4540
mandel-macaque added a commit to mandel-macaque/xamarin-macios that referenced this pull request Apr 8, 2020
Commits:

* mono/mono@165f4b03417: [2020-02][debugger] Fix NOT_IMPLEMENTED while debugging. (dotnet#19450)
* mono/mono@b04dc7e6a48: Bump corefx to get mono/corefx#395 (dotnet#19426)

Diff: mono/mono@ecde086...165f4b0

Related android PR: dotnet/android#4540
mandel-macaque added a commit to dotnet/macios that referenced this pull request Apr 8, 2020
Commits:

* mono/mono@165f4b03417: [2020-02][debugger] Fix NOT_IMPLEMENTED while debugging. (#19450)
* mono/mono@b04dc7e6a48: Bump corefx to get mono/corefx#395 (#19426)

Diff: mono/mono@ecde086...165f4b0

Related android PR: dotnet/android#4540
jonpryor added a commit to dotnet/android that referenced this pull request Apr 9, 2020
Changes: mono/mono@ecde086...165f4b0

  * mono/mono@165f4b03417: [2020-02][debugger] Fix NOT_IMPLEMENTED while debugging. (#19450)
  * mono/mono@b04dc7e6a48: Bump corefx to get mono/corefx#395 (#19426)
jonpryor added a commit to dotnet/android that referenced this pull request Apr 9, 2020
Changes: mono/mono@ecde086...165f4b0

  * mono/mono@165f4b03417: [2020-02][debugger] Fix NOT_IMPLEMENTED while debugging. (#19450)
  * mono/mono@b04dc7e6a48: Bump corefx to get mono/corefx#395 (#19426)
jonpryor added a commit to dotnet/android that referenced this pull request Apr 9, 2020
Changes: mono/mono@ecde086...165f4b0

  * mono/mono@165f4b03417: [2020-02][debugger] Fix NOT_IMPLEMENTED while debugging. (#19450)
  * mono/mono@b04dc7e6a48: Bump corefx to get mono/corefx#395 (#19426)
@mdh1418
mdh1418 deleted the mdhwang/update_filesystem_file_move branch April 9, 2020 13:31
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants