Not possible: HxD should allow others write access, when it opens a file for writing

Wishlists for new functionality and features.
Post Reply
AyrA
Posts: 3
Joined: 06 Aug 2018 19:12

Not possible: HxD should allow others write access, when it opens a file for writing

Post by AyrA » 06 Aug 2018 19:21

When HxD reopens a file for editing, it should attempt to use Read+Write if exclusive locking fails. Right now it looks like the editor tries to obtain exclusive access rights to the file which is fine as a first attempt, but if that fails, dwDesiredAccess should be set to FILE_SHARE_READ|FILE_SHARE_WRITE (See CreateFileW).

The reason is that some applications allow others to write to open files simultaneously. A process is not allowed to open such a file if he is not willing to propagate these access permissions.

Maël
Site Admin
Posts: 927
Joined: 12 Mar 2005 14:15

Re: Use FileShare.ReadWrite

Post by Maël » 06 Aug 2018 19:58

HxD has already been optimized for the most relaxed file sharing possible using caching and other techniques.

It actually opens files for reading such that other applications can open them for writing, already.
Exclusive access is only enforced after the file was modified inside of HxD.

Are you using HxD 2.0?

AyrA
Posts: 3
Joined: 06 Aug 2018 19:12

Re: Use FileShare.ReadWrite

Post by AyrA » 06 Aug 2018 20:05

The problem is not reading the file but writing to it. HxD is able to read the file that is open in my own application and properly displays changes every time it gets focus again, but it fails to write to it as soon as I attempt to edit hex values in it.

Maël
Site Admin
Posts: 927
Joined: 12 Mar 2005 14:15

Re: Use FileShare.ReadWrite

Post by Maël » 06 Aug 2018 20:06

So you want HxD to open a file for writing while also allowing another application to open it for writing as well?

AyrA
Posts: 3
Joined: 06 Aug 2018 19:12

Re: Use FileShare.ReadWrite

Post by AyrA » 06 Aug 2018 20:18

Only as a second method. The problem is if App1 allows Read+Write share, App2 is only able to write if it also grants this permission.
This is why I am suggesting that HxD as usual tries to exclusively lock the file (what most frameworks do by default on Windows) but if that fails, try to access the file with Read+Write share. There are two outcomes of this:

A) It works, this means that the file is only locked by applications that permit others to write to the file.
B) It doesn't works, this means that the file is locked by another application that doesn't allows others to write to the file, or it's inaccessible for other reasons like permissions or a write protected media.

Maël
Site Admin
Posts: 927
Joined: 12 Mar 2005 14:15

Re: HxD should allow others write access, when it opens a file for writing

Post by Maël » 07 Aug 2018 18:48

This will cause unpredictable results, because HxD only stores changed parts of the file to support arbitrary file sizes.
As soon as the file is changed externally, all the difference/change information becomes invalid, because the file may have grown or portions may have been moved around internally, and it is impossible to detect what changed.

There is no way to recover from this. It's best to just change the file, then save it; that will release the lock as well.

And if you have the file open in your program for writing, you should release it as soon as you can, and reopen it with reading rights only. That's what HxD does as well.

Post Reply