<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>How do the FILE_SHARE_* bits interact with the desired access bits?</title><link>http://blogs.msdn.com/oldnewthing/archive/2004/05/11/129759.aspx</link><description>Each sharing bit permits the corresponding access from other callers.</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Nasty table...</title><link>http://blogs.msdn.com/oldnewthing/archive/2004/05/11/129759.aspx#129947</link><pubDate>Tue, 11 May 2004 18:19:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:129947</guid><dc:creator>Marc Wallace</dc:creator><description>They really should have had borders on those table cells.  Looks truly unusable in Opera.&lt;br&gt;&lt;br&gt;Looks like pretty clear boolean logic, too.  I like that share and access rights are separated.</description></item><item><title>Cool to show how you can delete while open</title><link>http://blogs.msdn.com/oldnewthing/archive/2004/05/11/129759.aspx#130020</link><pubDate>Tue, 11 May 2004 20:16:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:130020</guid><dc:creator>David Kafrissen</dc:creator><description>Greetings,&lt;br&gt;&lt;br&gt;It is this kind of thing that makes Win32 more powerful than UNIX, because traditional UNIX does not handle file sharing to well.&lt;br&gt;&lt;br&gt;It is a cool trick to show students that you can open a file for reading two different ways, one in which you can delete the file from the file explorer while it is open and being read/written to in your program, and in another, you cannot delete the file in file explorer while it is open and being read/written to from your program. This all depends on which flags you choose when you open the file.&lt;br&gt;&lt;br&gt;</description></item><item><title>re: How do the FILE_SHARE_* bits interact with the desired access bits?</title><link>http://blogs.msdn.com/oldnewthing/archive/2004/05/11/129759.aspx#130028</link><pubDate>Tue, 11 May 2004 20:27:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:130028</guid><dc:creator>Cooney</dc:creator><description>&amp;gt; It is a cool trick to show students that you can open a file for reading two different ways, one in which you can delete the file from the file explorer while it is open and being read/written to in your program, and in another, you cannot delete the file in file explorer while it is open and being read/written to from your program. This all depends on which flags you choose when you open the file.&lt;br&gt;&lt;br&gt;&lt;br&gt;It's too bad that it isn't realized fully - in Unix, I can upgrade a library by copying the new version over it (or changing a link). This doesn't affect running programs, only new ones. The analogous situation involves SFP, backup directories, and a reboot. Much less satisfying.</description></item><item><title>re: How do the FILE_SHARE_* bits interact with the desired access bits?</title><link>http://blogs.msdn.com/oldnewthing/archive/2004/05/11/129759.aspx#130030</link><pubDate>Tue, 11 May 2004 20:31:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:130030</guid><dc:creator>Raymond Chen</dc:creator><description>I would think that on UNIX, overwrite would be a problem (since the inode hasn't changed); you need to do a delete/copy or a rename/copy.&lt;br&gt;&lt;br&gt;SFP protects only OS files. Applications shouldn't be messing with OS files anyway.</description></item><item><title>re: How do the FILE_SHARE_* bits interact with the desired access bits?</title><link>http://blogs.msdn.com/oldnewthing/archive/2004/05/11/129759.aspx#130121</link><pubDate>Tue, 11 May 2004 23:28:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:130121</guid><dc:creator>Dan Maas</dc:creator><description>On UNIX you'd create the new version of the file under a temporary name and then rename() to the final name, which also has the benefit of being atomic. (although user-interface wise, I can see the logic of why Windows generally won't let you delete a file that a program has openened)&lt;br&gt;&lt;br&gt;Is there a way to atomically replace a file on Windows?</description></item><item><title>re: How do the FILE_SHARE_* bits interact with the desired access bits?</title><link>http://blogs.msdn.com/oldnewthing/archive/2004/05/11/129759.aspx#130135</link><pubDate>Wed, 12 May 2004 00:02:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:130135</guid><dc:creator>Jeremy Croy</dc:creator><description>Rename the old version something else, then put the new one on the system.&lt;br&gt;</description></item><item><title>re: How do the FILE_SHARE_* bits interact with the desired access bits?</title><link>http://blogs.msdn.com/oldnewthing/archive/2004/05/11/129759.aspx#130215</link><pubDate>Wed, 12 May 2004 03:44:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:130215</guid><dc:creator>Norman Diamond</dc:creator><description>There is an immense difference between the description in the base note and the description that programmers ordinarily find in MSDN such as &lt;a target="_new" href="http://msdn.microsoft.com/library/en-us/fileio/base/createfile.asp"&gt;http://msdn.microsoft.com/library/en-us/fileio/base/createfile.asp&lt;/a&gt;&lt;br&gt;&lt;br&gt;There are also differences between the table which Mr. Chen provided a link to, and the MSDN page on CreateFile.&lt;br&gt;&lt;br&gt;Furthermore, the MSDN page on CreateFile doesn't even give a link to the MSDN page that Mr. Chen gave a link to.  Furthermore, the MSDN page on CreateFile describes a flag FILE_SHARE_DELETE which the other page doesn't describe.  Could it possibly be that the table was outdated, wasn't even correct at the time it was published, and has been forgotten about by MSDN maintainers?&lt;br&gt;&lt;br&gt;Let's see what CreateFile really says:&lt;br&gt;&lt;br&gt;&amp;gt; FILE_SHARE_DELETE Enables subsequent open&lt;br&gt;&amp;gt; operations on the object to request delete&lt;br&gt;&amp;gt; access. Otherwise, other processes cannot&lt;br&gt;&amp;gt; open the object if they request delete&lt;br&gt;&amp;gt; access. &lt;br&gt;&amp;gt; Windows Me/98/95: This flag is not&lt;br&gt;&amp;gt; supported.&lt;br&gt;&amp;gt;&lt;br&gt;&amp;gt; FILE_SHARE_READ Enables subsequent open&lt;br&gt;&amp;gt; operations on the object to request read&lt;br&gt;&amp;gt; access. Otherwise, other processes cannot&lt;br&gt;&amp;gt; open the object if they request read&lt;br&gt;&amp;gt; access.&lt;br&gt;&amp;gt;&lt;br&gt;&amp;gt; FILE_SHARE_WRITE Enables subsequent open&lt;br&gt;&amp;gt; operations on the object to request write&lt;br&gt;&amp;gt; access. Otherwise, other processes cannot&lt;br&gt;&amp;gt; open the object if they request write&lt;br&gt;&amp;gt; access. &lt;br&gt;&lt;br&gt;Notice the effect on SUBSEQUENT open operations.  If someone else has a file open with FILE_SHARE_READ | FILE_SHARE_WRITE, and we want to open it for GENERIC_READ | GENERIC_WRITE but not share it with anyone else, we get the file and the previous opener still has the file.  Only SUBSEQUENT attempts to open for reading or writing will fail.  This is quite different from Mr. Chen's statement:&lt;br&gt;&lt;br&gt;FALSE&amp;gt; So suppose you omit the flag,&lt;br&gt;FALSE&amp;gt; indicating that you don't want to let&lt;br&gt;FALSE&amp;gt; others read the file. Then when you&lt;br&gt;FALSE&amp;gt; attempt to open it yourself, the open&lt;br&gt;FALSE&amp;gt; will fail if anybody else has the file&lt;br&gt;FALSE&amp;gt; open for reading.&lt;br&gt;&lt;br&gt;The wording of CreateFile also provides a number of other undesirable effects.  For example, we want to open a file and allow FILE_SHARE_READ.  Subsequently someone else want to open it with GENERIC_READ | GENERIC_WRITE.  MSDN says they get it, because their request includes GENERIC_READ.  The description of FILE_SHARE_DELETE is still so unclear that maybe developers of Windows file systems couldn't even follow it properly, which might explain why sometimes you can't delete a file because of a sharing violation even though no process has the file open.&lt;br&gt;&lt;br&gt;This is the kind of thing that makes Windows less powerful than every other OS on the market.  Neither Unix nor any other modern OS has fully copied the permissions structure that Honeywell got right 40 years ago, but at least each other modern OS is self-consistent.  In Unix you have to do arcane things to get file operations performed reliably, but in Windows you have to do even worse arcane things.&lt;br&gt;&lt;br&gt;(I'm again too busy to catch up with Mr. Chen's recent writings, but this one was just too important to let pass.)</description></item><item><title>re: How do the FILE_SHARE_* bits interact with the desired access bits?</title><link>http://blogs.msdn.com/oldnewthing/archive/2004/05/11/129759.aspx#130255</link><pubDate>Wed, 12 May 2004 05:11:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:130255</guid><dc:creator>Raymond Chen</dc:creator><description>The FILE_SHARE_* flags prohibit subsequent opens. They also prevent the current CreateFile from succeeding if they conflict with prior opens. This is implied by the fact that the previous GENERIC_* open prevents subsequent opens from succeeding if they do not specify FILE_SHARE_*. The docs are correct but leave a lot of the logical deductions to you, the reader.</description></item><item><title>re: How do the FILE_SHARE_* bits interact with the desired access bits?</title><link>http://blogs.msdn.com/oldnewthing/archive/2004/05/11/129759.aspx#130338</link><pubDate>Wed, 12 May 2004 08:22:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:130338</guid><dc:creator>Steven Youngs</dc:creator><description>&amp;gt;Is there a way to atomically replace a file on Windows? &lt;br&gt;&lt;br&gt;There is a Win32 function called ReplaceFile. Not sure if it's atomic or not.</description></item><item><title>re: How do the FILE_SHARE_* bits interact with the desired access bits?</title><link>http://blogs.msdn.com/oldnewthing/archive/2004/05/11/129759.aspx#130349</link><pubDate>Wed, 12 May 2004 08:54:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:130349</guid><dc:creator>Jordan Russell</dc:creator><description>&amp;gt; Is there a way to atomically replace a file on Windows?&lt;br&gt;&lt;br&gt;If I'm not mistaken: MoveFileEx(..., ..., MOVEFILE_REPLACE_EXISTING)</description></item><item><title>re: How do the FILE_SHARE_* bits interact with the desired access bits?</title><link>http://blogs.msdn.com/oldnewthing/archive/2004/05/11/129759.aspx#130393</link><pubDate>Wed, 12 May 2004 11:12:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:130393</guid><dc:creator>jojjo</dc:creator><description>Your comment about checkmarks is interesting. &lt;br&gt;In Sweden a checkmark is the standard way of marking an error on a test or a paper. The first time my mother saw those old Borland messageboxes with a checkmark on the OK button and an X on the Cancel button she pressed Cancel because the checkmark to her denoted that there was something wrong and the X that she accepted (there's a swedish expression &amp;quot;kryssa f&amp;#246;r&amp;quot; (cross for) which means to check something).</description></item><item><title>re: How do the FILE_SHARE_* bits interact with the desired access bits?</title><link>http://blogs.msdn.com/oldnewthing/archive/2004/05/11/129759.aspx#130604</link><pubDate>Wed, 12 May 2004 17:48:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:130604</guid><dc:creator>Raymond Chen</dc:creator><description>&amp;quot;Subsequently someone else want to open it with GENERIC_READ | GENERIC_WRITE. MSDN says they get it, because their request includes GENERIC_READ.&amp;quot; &lt;br&gt;&lt;br&gt;No, they don't get it because their request includes GENERIC_WRITE and you didn't allow write. &lt;br&gt;&lt;br&gt;In order for the open to succeed, ALL requirements must be met. This is such a basic rule that I'm surprised it needed to be mentioned explicitly. Did you serious believe that &amp;quot;Enables subsequent open operations on the object to request read access.&amp;quot; means that if you match the read sharing modes, then all other security/sharing flags become irrelevant?  Did you really think that it would override the ACLs on the file?&lt;br&gt;&lt;br&gt;Here's another basic rule that is so obvious that it isn't mentioned explicitly: The sharing modes applied to a handle are effective only as long as the handle remains open. &lt;br&gt;&lt;br&gt;In fact the addition of &amp;quot;subsequent&amp;quot; was my attempt to state something that should have been blatantly obvious: CreateFile is not a time machine. Some people apparently thought that the absence of &amp;quot;subsequently&amp;quot; meant that you could use CreateFile to revoke previously-opened handles... &lt;br&gt;&lt;br&gt;At some point you have to decide at what point you should stop saying obvious things in the documentation and leave the reader to use their own brainpower.</description></item><item><title>re: How do the FILE_SHARE_* bits interact with the desired access bits?</title><link>http://blogs.msdn.com/oldnewthing/archive/2004/05/11/129759.aspx#130800</link><pubDate>Wed, 12 May 2004 23:22:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:130800</guid><dc:creator>4nd3r$</dc:creator><description>&amp;gt;If I'm not mistaken: MoveFileEx(..., ..., MOVEFILE_REPLACE_EXISTING) &lt;br&gt;&lt;br&gt;...on NT, on 9x u need to write to wininit.ini (without using the ini file functions)</description></item><item><title>re: How do the FILE_SHARE_* bits interact with the desired access bits?</title><link>http://blogs.msdn.com/oldnewthing/archive/2004/05/11/129759.aspx#130864</link><pubDate>Thu, 13 May 2004 01:36:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:130864</guid><dc:creator>Jordan Russell</dc:creator><description>His question was how to atomically replace a file, a la Unix's rename(), not how to replace a file on reboot.</description></item><item><title>re: How do the FILE_SHARE_* bits interact with the desired access bits?</title><link>http://blogs.msdn.com/oldnewthing/archive/2004/05/11/129759.aspx#130900</link><pubDate>Thu, 13 May 2004 02:55:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:130900</guid><dc:creator>Norman Diamond</dc:creator><description>A few metarules:&lt;br&gt;&lt;br&gt;Specifically stated rules override basic rules.&lt;br&gt;&lt;br&gt;Stated rules override intuitive rules.&lt;br&gt;&lt;br&gt;The correctness of a basic rule or intuitive rule does not change an incorrect specific rule or incorrect stated rule into a correct one.&lt;br&gt;&lt;br&gt;I am glad to see that these are bugs in the MSDN library rather than in the OS.  Nonetheless, if the MSDN library is to be useful, its bugs must be fixed.  &amp;quot;RTFM&amp;quot; has to mean that TFM is to be R'd rather than F'ed.</description></item><item><title>re: How do the FILE_SHARE_* bits interact with the desired access bits?</title><link>http://blogs.msdn.com/oldnewthing/archive/2004/05/11/129759.aspx#130917</link><pubDate>Thu, 13 May 2004 03:30:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:130917</guid><dc:creator>Raymond Chen</dc:creator><description>Okay then: You tell me how that section should be written, now that you understand what FILE_SHARE_READ means. Since apparently nothing I write meets your high standards.</description></item><item><title>Why is it FILE_SHARE_READ and FILE_SHARE_WRITE anyway?</title><link>http://blogs.msdn.com/oldnewthing/archive/2004/05/11/129759.aspx#131264</link><pubDate>Thu, 13 May 2004 20:11:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:131264</guid><dc:creator>Larry Osterman's WebLog</dc:creator><description /></item><item><title>Deleted but not yet forgotten</title><link>http://blogs.msdn.com/oldnewthing/archive/2004/05/11/129759.aspx#150050</link><pubDate>Mon, 07 Jun 2004 17:02:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:150050</guid><dc:creator>The Old New Thing</dc:creator><description>What happens when you delete a file while it is open?</description></item><item><title>Determine share mode of opened file handle</title><link>http://blogs.msdn.com/oldnewthing/archive/2004/05/11/129759.aspx#8464916</link><pubDate>Wed, 07 May 2008 08:09:33 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8464916</guid><dc:creator>Programming and Peace of Mind</dc:creator><description>&lt;p&gt;If there is a file already opened and you also want to open it, you need to know the share mode: FILE_SHARE_*&lt;/p&gt;
</description></item></channel></rss>