Marco Mariani
Marco Mariani

Reputation: 13776

Will we ever be able to delete an open file in Windows?

Disclaimer: I am mainly a linux/web developer.

Windows has this "nice" feature where it denies permission to delete any file that is held open by any process. So if an antivirus hits the wrong file at the wrong time, some random program might misbehave and possibly crash.

Am I right? Are there plans to fix this?

Do any of you find this acceptable, or, how could it possibly seem a good idea at the time?

Edit:

It works very differently on Unix, and has been so for decades.

As an example:

Actually, a common usage pattern for temporary files on Unix is: open-remove-read/write-close.

Upvotes: 19

Views: 7313

Answers (2)

mafu
mafu

Reputation: 32700

Your initial statement is not correct. Windows does allow open files to be deleted. You just have to specify FILE_SHARE_DELETE and you're all set. Careful programmers should sensibly decide if that flag (or sharing for reading/writing) make sense and pass it.

An anti virus product that does not open files with full sharing (including deletion) enabled is buggy.

Windows does, however, remember the current working directory of any process and prevents it from being deleted. This working directory is independent of the location of any files opened by the process.

Upvotes: 9

Daniel T.
Daniel T.

Reputation: 38410

This is perfectly acceptable. Imagine a situation where you're reading a database file in your application, and some other application comes along and deletes that database file from right under you. How does your application know to check that the file still exists? How will it ensure that the file stream does not all of a sudden attempt to read that file may be there one millisecond, but not the next? This is why programs can lock files, to ensure that the file will always be there until the program determines that it is done with it.

It may be more helpful to tell us why this file locking is undesirable in your situation. I'm pretty sure anti-virus programs do an optimistic lock on files, unless it's cleaning them.

Upvotes: 1

Related Questions