How do I make Windows file-locking more like UNIX file-locking?

后端 未结 4 1283
攒了一身酷
攒了一身酷 2020-12-09 16:52

UNIX file-locking is dead-easy: The operating system assumes that you know what you are doing and lets you do what you want:

For example, if you try to delete a file

相关标签:
4条回答
  • 2020-12-09 17:10

    According to MSDN you can specify to CreateFile() 3rd parameter (dwSharedMode) shared mode flag FILE_SHARE_DELETE which:

    Enables subsequent open operations on a file or device to request delete access.

    Otherwise, other processes cannot open the file or device if they request delete access.

    If this flag is not specified, but the file or device has been opened for delete access, the function fails.

    Note Delete access allows both delete and rename operations.

    http://msdn.microsoft.com/en-us/library/aa363858(VS.85).aspx

    So if you're can control your applications you can use this flag.

    0 讨论(0)
  • 2020-12-09 17:13

    Note that Process Explorer allow for force closing of file handles (for processes local to the box on which you are running it) via Handle -> Close Handle.

    Unlocker purports to do a lot more, and provides a helpful list of other tools.

    Also deleting on reboot is an option (though this sounds like not what you want)

    0 讨论(0)
  • 2020-12-09 17:18

    That doesn't really help if the hung process still has the handle open. It won't release the resources until that hung process releases the handle. But anyway, in Windows it is possible to force close a file out from under a process that's using it. Process Explorer from sysinternals.com will let you look at and close handles that a process has open.

    0 讨论(0)
  • 2020-12-09 17:20

    No. Windows is designed for the "average user", that is people who don't understand anything about a computer. Therefore, the OS tries to be smart to avoid PEBKACs. To quote Bill Gates: "There are no issues with Windows that any number of people want to be fixed." Of course, he knows that 99.9999% of all Windows users can't tell whether the program just did something odd because of them or the guy who wrote it.

    Unix was designed when the world was more simple and anyone close enough to a computer to touch it, probably knew how to assemble it from dirty sand. Therefore, the OS usually lets you do what you want because it assumes that you know better (and if you didn't, you will next time).

    Technical answer: Unix allocates an "i-nodes" if you create a file. I-nodes can be shared between processes. If two processes create the same file (that is, two processes call create() with the same path), then you end up with two i-nodes. This is by design. It allows for a fancy security feature: You can create files which no one can open but yourself:

    1. Open a file
    2. Delete it (but keep the file handle)
    3. Use the file any way you like
    4. Close the file

    After step #2, the only process in the universe who can access the file is the one who created it (unless you want to read the hard disk block by block). The OS will keep the data alive until you either close the file or your process dies (at which time Unix will clean up after you).

    This design is the foundation of all Unix filesystems. The Windows file system NTFS works much the same way but the high level API is different. Many applications open files in exclusive mode (which prevents anyone, even backup programs) to read the file. This is even true for applications which just display information like PDF viewers.

    That means you'll have to fix all the Windows applications to achieve the desired effect. If you have access to the source, you can create a file in a shared mode. That would allow other processes to access it at the same time but then, you will have to check before every read/write if the file still exists, whether someone has made changes, etc.

    0 讨论(0)
提交回复
热议问题