New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mono is not able to notice a file change in a FileInfo object #6283

Closed
csances opened this Issue Dec 19, 2017 · 9 comments

Comments

Projects
None yet
4 participants
@csances

csances commented Dec 19, 2017

Steps to Reproduce

Scenario: We are running on Linux (Mono) a set of tests developed in Windows (c# .Net).
Some tests fail only in Linux.

  1. Create programatically a file and add some content inside,
  2. create a FileInfo object with the current status of the file,
  3. update again this file,
  4. created another FileInfo object with the current status of the file,
  5. compare both instances of the FileInfo.

Current Behavior

Both instances of the file are the same.
It seems like the event in charge to notice the changes does not work properly under Mono. If we iterate through the properties of the second FileInfo using reflection before the comparasion we will have the correct snapshot of the file in the second FileInfo. But it's a non desired workaround.

Expected Behavior

The two instances must be different after the update, becasue we are adding some content to the file.

On which platforms did you notice this

[ ] macOS
[X] Linux
[ ] Windows

Version Used:

5.4.1.6
4.6.1.5

Stacktrace

N/A

@csances csances changed the title from Mono is not able to notice an update in a file to Mono is not able to notice a file change in a FileInfo object Dec 19, 2017

@lambdageek

This comment has been minimized.

Member

lambdageek commented Dec 20, 2017

@csances thanks for reporting this issue. Could you provide us with some code the demonstrates the problem?
Thanks!

@migueldeicaza

This comment has been minimized.

Member

migueldeicaza commented Dec 20, 2017

Patch:

#6307

To reproduce:

With the current code on Mac:

$ csharp -e 'Console.WriteLine (new System.IO.FileInfo ("/etc/passwd").LastAccessTime.Ticks)'
636493820550000000

With the pull request applied on Mac:

$ csharp -e 'Console.WriteLine (new System.IO.FileInfo ("/etc/passwd").LastAccessTime)'
636493820391074624
$

Not all filesystems have nanosecond precision, so you might not see it.

@csances

This comment has been minimized.

csances commented Dec 21, 2017

Hi @migueldeicaza:

I think your suggestion is related with the bug number 6282 (Miliseconds information in datetime properties from FileInfo are not available on MONO). It was created by me too.
I will review your proposal.

Thank you for your help.

@csances

This comment has been minimized.

csances commented Dec 21, 2017

Hi @lambdageek:

It's enough for you if a paste a piece of code here in a post?.

It is an example of our current situation:

 var tracker = new ContentTracker(this.folderName, filePattern);
 this.AddFile(fileName);
// AddFile code Method:
System.IO.File.WriteAllText(Path.Combine(this.folderName, fileName), "Created");
Thread.Sleep(100);
// Next method review if we have added or changed file in a folder using a FileInfo object to do this
 tracker.GetChangedContent();
 this.UpdateFile(fileName);
// UpdateFile code Method:
System.IO.File.AppendAllText(Path.Combine(this.folderName, fileName), "Updated");
Thread.Sleep(100);
// Next method review if we have added or changed file in a folder using a FileInfo object to do this
var lastChanges = tracker.GetChangedContent();

The first time we call GetChangedContent, we notice the creation of the file when we compare the FileInfo object instances.
The second time we want to know how many changes we have done. Unfortunatelly Mono is not able to notice all these changes.

Then we decided to add some tricky code in order to catch the changes. We call a method to iterate through the FileInfo properties using reflection:

private static void RefreshContentFromFileInfoList(List<FileInfo> listFileInfo)
{
    if (OperatingSystem.IsUnixPlatform())
    {
        foreach (var fileInfo in listFileInfo)
        {
            if (fileInfo != null)
            {
                foreach (var p in fileInfo.GetType().GetProperties())
                {
                    p.GetValue(fileInfo, null).ToString();
                }
            }
        }
    }
}

Once we have executed this method, Mono is able to be noticed about the changes.

Please, don't hesitate to request more information if you need it.

I really appreciate your help.

@akoeplinger

This comment has been minimized.

Member

akoeplinger commented Jan 4, 2018

@csances I'm a bit confused by this issue: are you saying you expect the FileInfo object to automatically notice changes to the file? That's not how it works, you need to call the Refresh() method.

If I'm missing something please provide a self-contained code sample that shows the problem, i.e. I assume the interesting logic is behind ContentTracker in your code sample :)

@csances

This comment has been minimized.

csances commented Jan 5, 2018

Hi @akoeplinger:

I really appreciate your comments.
The thing is using this code in a windows environment I do not need to use a Refresh() command in C# code nor something similar.
When code compares the old file and the new file information in windows environment is able to detect the different value on the properties of fileinfo objects; using the same approach in Linux both objects contain the same information (I have to use RefreshContentFromFileInfoList custom method to force Linux to realize about the differences inside).
I think it is an issue in the way that Mono realizes about the changes inside a folder regarding a file.

Please don't hesitate in asking me if you need furhter explanation.

Thank you ;)

@akoeplinger

This comment has been minimized.

Member

akoeplinger commented Jan 5, 2018

@csances the thing we need is some code to reproduce the problem, that's all :)

Best would be to make a simple console application that shows the problem and share the code here.

@csances

This comment has been minimized.

csances commented Jan 15, 2018

@akoeplinger

Thank you for your help. Finally we have been able to fix the problem using Refresh method from FileInfo object.
We were not using it on Windows environment but once we have remove our own "Refresh" method for the Refresh from FileInfo object everything was ok.

@csances csances closed this Jan 15, 2018

@akoeplinger

This comment has been minimized.

Member

akoeplinger commented Jan 15, 2018

Ok good to hear although I still don't understand why it worked on Windows without calling Refresh() :)

jonpryor added a commit to xamarin/xamarin-android that referenced this issue Apr 25, 2018

Bump to mono/2018-02/0c5a524e (#1289)
Bumps to Java.Interop/master/0afb2b0f
Bumps to llvm/master/a9cfb50e.

Fixes: https://bugzilla.xamarin.com/show_bug.cgi?id=11771
Fixes: https://bugzilla.xamarin.com/show_bug.cgi?id=15051
Fixes: https://bugzilla.xamarin.com/show_bug.cgi?id=19436
Fixes: https://bugzilla.xamarin.com/show_bug.cgi?id=45901
Fixes: https://bugzilla.xamarin.com/show_bug.cgi?id=56071
Fixes: https://bugzilla.xamarin.com/show_bug.cgi?id=58413
Fixes: https://bugzilla.xamarin.com/show_bug.cgi?id=58413
Fixes: https://bugzilla.xamarin.com/show_bug.cgi?id=58413
Fixes: https://bugzilla.xamarin.com/show_bug.cgi?id=59184
fixes: https://bugzilla.xamarin.com/show_bug.cgi?id=60065
Fixes: https://bugzilla.xamarin.com/show_bug.cgi?id=60225
Fixes: https://bugzilla.xamarin.com/show_bug.cgi?id=60298
Fixes: https://bugzilla.xamarin.com/show_bug.cgi?id=60359
Fixes: https://bugzilla.xamarin.com/show_bug.cgi?id=60568
Fixes: https://bugzilla.xamarin.com/show_bug.cgi?id=60756
Fixes: https://bugzilla.xamarin.com/show_bug.cgi?id=60848
Fixes: https://bugzilla.xamarin.com/show_bug.cgi?id=60862
Fixes: https://bugzilla.xamarin.com/show_bug.cgi?id=60900
Fixes: https://bugzilla.xamarin.com/show_bug.cgi?id=60904
Fixes: https://bugzilla.xamarin.com/show_bug.cgi?id=60986
Fixes: https://github.com/mono/mono/issues/59400
Fixes: mono/mono#6169
Fixes: mono/mono#6187
Fixes: mono/mono#6192
Fixes: mono/mono#6255
Fixes: mono/mono#6264
Fixes: mono/mono#6266
Fixes: mono/mono#6281
Fixes: mono/mono#6283
Fixes: mono/mono#6320
Fixes: mono/mono#6339
Fixes: mono/mono#6343
Fixes: mono/mono#6349
Fixes: mono/mono#6379
Fixes: mono/mono#6383
Fixes: mono/mono#6401.
Fixes: mono/mono#6411
Fixes: mono/mono#6414
Fixes: mono/mono#6490
Fixes: mono/mono#6721
Fixes: mono/mono#6767
Fixes: mono/mono#6777
Fixes: mono/mono#6848
Fixes: mono/mono#6940
Fixes: mono/mono#6948
Fixes: mono/mono#6998
Fixes: mono/mono#7016
Fixes: mono/mono#7085
Fixes: mono/mono#7086
Fixes: mono/mono#7095
Fixes: mono/mono#7137
Fixes: mono/mono#7184
Fixes: mono/mono#7240
Fixes: mono/mono#7262
Fixes: mono/mono#7289
Fixes: mono/mono#7338
Fixes: mono/mono#7356
Fixes: mono/mono#7364
Fixes: mono/mono#7378
Fixes: mono/mono#7389
Fixes: mono/mono#7460
Fixes: mono/mono#7535
Fixes: mono/mono#7536
Fixes: mono/mono#7610
Fixes: mono/mono#7624
Fixes: mono/mono#7637
Fixes: mono/mono#7655
Fixes: mono/mono#7657
Fixes: mono/mono#7685
Fixes: mono/mono#7786
Fixes: mono/mono#7792
Fixes: mono/mono#7822
Fixes: mono/mono#7860
Fixes: mono/mono#8089
Fixes: mono/mono#8267
Fixes: mono/mono#8409
Fixes: xamarin/maccore#628
Fixes: xamarin/maccore#629
Fixes: xamarin/maccore#673
Fixes: xamarin/maccore#673
Fixes: #1561
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment