{"id":99335,"date":"2018-07-25T07:00:00","date_gmt":"2018-07-25T21:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/?p=99335"},"modified":"2019-03-13T00:40:52","modified_gmt":"2019-03-13T07:40:52","slug":"20180725-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20180725-00\/?p=99335","title":{"rendered":"Why are my file write operations synchronous, even though I opened the file as <CODE>FILE_<CODE><\/CODE>FLAG_<CODE><\/CODE>OVERLAPPED<\/CODE>?"},"content":{"rendered":"<p>A customer found that even though they opened a file with the <code>FILE_<code><\/code>FLAG_<code><\/code>OVERLAPPED<\/code>, their calls to <code>Write&shy;File<\/code> were nevertheless synchronous. Why are their overlapped writes completing synchronously? <\/p>\n<p>As currently implemented, there are at least <a HREF=\"https:\/\/support.microsoft.com\/en-us\/help\/156932\/asynchronous-disk-i-o-appears-as-synchronous-on-windows\">three things which will cause a write to be forced synchronous<\/a>: <\/p>\n<ol>\n<li>Writing to an NTFS-compressed file.<\/li>\n<li>Writing to an NTFS-encrypted file.<\/li>\n<li>Extending a file.<\/li>\n<\/ol>\n<p>The first two are easy to understand. The third is trickier. <\/p>\n<p>You might think you can avoid extending the file with a write by seeking to the desired final file size, performing a <code>Set&shy;End&shy;Of&shy;File<\/code>, seeking back to position 0, and then performing the desired write operations. <\/p>\n<p>It turns out this doesn&#8217;t work because setting the end-of-file extends the logical file size, but the physical file size is unchanged. There are two obvious ways to change the physical file size: <\/p>\n<p>First, you can use the <code>Set&shy;File&shy;Valid&shy;Data<\/code> function. This allocates space for the file on disk but does not zero-initialize those bytes. You just get whatever garbage was lying in those sectors at the time. This is naturally a violation of security since it allows a user to access data that belonged to another user, so this operation requires <code>Se&shy;Manage&shy;Volume&shy;Privilege<\/code> which is normally assigned only to administrators. <\/p>\n<p>Less scary is seeking to the desired end of file position minus one, then writing one byte. This write will be synchronous, and it may take a while since the system needs to zero-initialize all the bytes in between, but it does finish with the desired state, and it doesn&#8217;t require administrator privileges. <\/p>\n<p>Once you&#8217;ve caused the file to be extended to the desired size, you can seek back to position 0 and perform your overlapped write operations, which will now complete asynchronously, assuming you haven&#8217;t encountered any other obstacles. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Extending the valid bytes is always synchronous.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-99335","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Extending the valid bytes is always synchronous.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/99335","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=99335"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/99335\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=99335"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=99335"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=99335"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}