News for hackers. Stuff that matters.
Re:Do not change Linux files using Windows apps and tools by vesinisa on Thursday November 17, 2016 @03:39PM (#12981297)
EDIT: Misinformed, see wfunction's comment below.

I can understand their frustration, because this is probably really hard to fix.

I guess there would not be problems if Windows apps were not overwriting files by first deleting them and then re-creating them with new contents. It's easy to see why this leads to data loss (file permission metadata is data in the Unix world). You rarely see Unix apps re-creating files: there you instead you specify the behaviour in case the file exists with the flags of the open(2) call.

I guess such contract is absent / not as strict in the Windows world (disc: I have never programmed for Windows), which would make solving the conflicting APIs almost impossible: if a re-created file would inherit a previously deleted synonymous file's permissions, what happens to a program who is deleting a file exactly to get rid of its permissions?

I think MS has done pretty impressive work with the Linux subsystem. It is certainly not a trivial task, and I am looking forward to seeing more awesome stuff running on Windows.

Re:Do not change Linux files using Windows apps and tools by wfunction on Thursday November 17, 2016 @04:06PM (#12981571)
> You rarely see Unix apps re-creating files: there you instead you specify the behaviour in case the file exists with the flags of the open(2) call.

I think your lack of knowledge about this problem is betraying you. This is outright wrong. First, look up CreateFile() in Windows; it has flags to specify things like this as well. Second, the reason programs delete files in the first place has nothing to do with the lack of such flags. It has to do with the fact that they want to write new contents but don't want to lose data in the event of an abnormal termination. If you truncate the file that you're opening instead, then you lose that data if something goes wrong. So they create a new file and replace it with the old one when it's written.

Finally, as far as I know, 'nix tools have a tendency to outright replace files MORE than Windows tools do. That's why the underlying Windows kernel API function (NtCreateFile/ZwCreateFile) has a FILE_SUPERSEDE parameter... whose entire intention is to mimic POSIX behavior:

> The CreateDisposition value FILE_SUPERSEDE requires that the caller have DELETE access to a existing file object. If so, a successful call to ZwCreateFile with FILE_SUPERSEDE on an existing file effectively deletes that file, and then recreates it. [...] Note that this type of disposition is consistent with the POSIX style of overwriting files.

https://msdn.microsoft.com/en-us/library/windows/hardware/ff...

Re:Do not change Linux files using Windows apps and tools by asveikau on Thursday November 17, 2016 @10:52PM (#12984166)
I would go so far to say (though I've met few people who grok this, as Windows filesystems is pretty rare as a specialty these days) that delete-and-recreate is a huge antipattern and recipe for failure on Windows.

The reason: Delete behavior.

In NT, when you delete a file and handles are open, the name sticks around until all handles are closed. Any attempt to re-open (including re-create) with the same name will fail with STATUS_DELETE_PENDING while this is the case.

Combine that with the fact that most operations in NT (including delete or rename) happen via opening handles to the file first and you have lots of chaos and race conditions.

So the delete + recreate pattern is very likely to bite you later as a "re-create fails randomly".

Re:Do not change Linux files using Windows apps and tools by wfunction on Friday November 18, 2016 @12:46AM (#12984630)
OK, so let me explain why your reasoning is wrong.

The problems here only occur if your app is modifying files that do not "belong" to it. For apps that store config files, databases, etc., these problems should not arise, because those files should only be touched by those apps, and (a) the app is already aware what metadata the file should have and is the one maintaining control over the files, and (b) the app can make sure that it is never using a file that it is trying to delete. If the user is messing with the app's files, then the user is just as responsible for not preventing their deletion as he/she is responsible for not corrupting their contents. You can't blame the deletion behavior for that; the responsibility for proper care rests entirely on the user, and having a different delete behavior doesn't fix the core problem.

For files that don't belong to the app, the situation is entirely different. First, apps should not be deleting files that don't belong to them without user consent. And if the user is consenting, the user is responsible for ensuring this does not cause problems. Second, those that modify files they do not own are responsible for all aspects of this, including preserving metadata. This is of course difficult and quite a burden, but at this point, it is considered a bug in the application if it does not do this correctly. Again, the app is the one mucking with files that it does not own, so the app and the user together are responsible for maintaining consistency, not the OS.

Re:Do not change Linux files using Windows apps and tools by asveikau on Friday November 18, 2016 @01:25AM (#12984778)
You're funny. Telling me my years of experience of this behavior biting me in the rear end is "wrong".

There isn't even such thing as "my files" and not. Are you aware that many people run antivirus products that sit between you and the filesystem and might choose to also open "my" files in an asynchronous fashion based on my own actions? That such an antivirus product has been built into Windows for some time now? (msmpeng.exe) Suddenly that file you thought it safe to have exclusive access to... it wasn't.

Even controlling your own actions is hard and yields unexpected results all the time. Almost any time setting dwShareMode to something other than 0x7 and I saw it bite me in some unexpected way due to things my own process is doing, that I did not think of ahead of time.

Once you have Windows filesystem code doing this at any appreciable scale you will see this. It takes enough of seeing sharing violations and delete pending errors time after time to realize these are just dangerous patterns on the platform, avoid them and move on.

Re:Do not change Linux files using Windows apps and tools by wfunction on Friday November 18, 2016 @02:46AM (#12985087)
I didn't say your experience was "wrong" though? That doesn't even make sense; your experience is what it is. I only said your reasoning about your experience was wrong, i.e. you should be blaming the applications and not the OS (whether they're made by MS or anyone else).

Regarding antiviruses: yes, of course I am. That's why they should be opening the files with FILE_SHARE_DELETE, and properly handling the edge cases. Unless you're telling me it's impossible (which I strongly suspect isn't the case, but then again I've never tried to write an AV myself), I don't see how it's the OS's fault. This flag and the associated functionality obviously exists for a reason, right? It's not an OS design flaw if people aren't using it, is it? People can refuse to play along with anything... that doesn't mean the OS is flawed.

Re:Do not change Linux files using Windows apps and tools by asveikau on Friday November 18, 2016 @09:13AM (#12986853)
Sharing modes don't help with the STATUS_DELETE_PENDING issue. Nor does it let you delete a directory while children are open.

Also some AV will duplicate your handles, meaning they get the same sharing mode you asked for.

> This flag and the associated functionality obviously exists for a reason, right?

It's the kind of stuff that sounds reasonable when you hear about it, but when you see it put into practice is the source of way too many bugs.

Re:Do not change Linux files using Windows apps and tools by wfunction on Friday November 18, 2016 @03:59PM (#12990025)
> Sharing modes don't help with the STATUS_DELETE_PENDING issue.

Maybe I misunderstood the problem. But I think my reasoning holds regardless? To be clear, it seems you're talking about a situation where an app is trying to delete and re-create a file, and an AV is scanning the file in between, hence a recreation fails.

But with their file system filter drivers, AVs can detect such recreation attempts, so they should be able to handle it properly. So isn't it their fault for not doing so?

> Nor does it let you delete a directory while children are open.

Right, but same as above: there exists functionality to detect this, right? So if it isn't used, whose fault is it?

Re:Do not change Linux files using Windows apps and tools by wfunction on Thursday November 17, 2016 @11:02PM (#12984223)
What's your alternate recipe for success?
Re:Do not change Linux files using Windows apps and tools by asveikau on Thursday November 17, 2016 @11:21PM (#12984324)
I don't think I have one unfortunately. It depends greatly on the scenario.

Usually I'd say come up with a unique name if you can. Otherwise I'd say try to overwrite (possibly overwrite via rename, rather than CreateFile, to avoid the data loss you mention).

I've even written some code to rename to a unique name first, then delete.

Re:Do not change Linux files using Windows apps and tools by paulddraper on Thursday November 17, 2016 @04:09PM (#12981595)
Exactly. Most file systems have an atomic move, that you can leverage into an "atomic write".
Re:Do not change Linux files using Windows apps and tools by vesinisa on Thursday November 17, 2016 @04:50PM (#12982033)
Thanks for correcting.

That makes it sound almost like this could be actually fixed by swimming deep enough to the NTFS driver in Windows.

Re:Do not change Linux files using Windows apps and tools by wfunction on Thursday November 17, 2016 @11:03PM (#12984227)
There is such a thing as NTFS tunneling, if that's what you mean. Not sure what you have in mind if you mean otherwise.
Re:Do not change Linux files using Windows apps and tools by asveikau on Thursday November 17, 2016 @11:31PM (#12984362)
NTFS tunneling is a hilarious feature.

To those who are not well versed in this:

This was a hack for 2 things that exist in the universe:

1. Apps that save their documents by "delete document, re-create document by the same name".

2. The fact that said apps (especially when you consider that they might be written for Win3.1 or DOS) could be accessing the documents by 8.3 names (eg. MICROS~1.TXT)

What ntfs.sys will do is: for some period of time after deleting a file, remember the "long filename" of a corresponding short name, and re-hydrate it when you re-create. So when you delete MICROS~1.TXT and create a new MICROS~1.TXT, ntfs.sys will remember that this file is also called "Microsoft.txt" and re-create that name.

Re:Do not change Linux files using Windows apps and tools by pritambaral on Thursday November 17, 2016 @07:34PM (#12983254)
> You rarely see Unix apps re-creating files

Vim's default behaviour is to save via complete re-creation and atomic rename. I wouldn't be surprised if other classic editors used the same technique.

Re:Do not change Linux files using Windows apps and tools by jovdg on Friday November 18, 2016 @12:40AM (#12984620)
That is also what "sed -i" does.
Re:Do not change Linux files using Windows apps and tools by cestith on Thursday November 17, 2016 @04:10PM (#12981603)
Actually it's fairly common when making changes within a file, especially a text file, to read from one file, write the changed data stream to a new file, then do an atomic rename of the target file to the name of the source file.
Re:Do not change Linux files using Windows apps and tools by userbinator on Friday November 18, 2016 @06:55AM (#12986115)
It's easy to see why this leads to data loss (file permission metadata is data in the Unix world).

Completely disagree. Considering that some file systems which have been around for decades don't even have this permissions metadata, I would say that the file contents are the most important to users. MS should absolutely give a warning that the extended metadata can be lost, but the "If WSL can’t find Linux file metadata for a given file, it assumes the file is corrupted/damaged" behaviour noted in the article is completely contrary to user expectations.

The filesystem model in Linux and Windows is not identical, but there is considerable overlap. It therefore makes perfect sense --- and is expected --- for the overlapping parts to work.

Re:Do not change Linux files using Windows apps and tools by kalleboo on Friday November 18, 2016 @12:25AM (#12984577)
> because this is probably really hard to fix.

The alternative I can think of is spreading hidden metadata files all over the place, à la Apple with their .DS_Store and AppleDouble files that everyone always complains about. That comes with its own set of problems (moving files around will lose the metadata when they're no longer in the source directory along with the metadata)