<!DOCTYPE html><htmllang="en-US"><head><metacharset="UTF-8"><metahttp-equiv="X-UA-Compatible"content="IE=Edge"><title>Safe Saving - SyncFiles Documentation</title><linkrel="shortcut icon"href="/syncfiles/favicon.ico"type="image/x-icon"><linkrel="stylesheet"href="/syncfiles/assets/css/just-the-docs-default.css"><scripttype="text/javascript"src="/syncfiles/assets/js/vendor/lunr.min.js"></script><scripttype="text/javascript"src="/syncfiles/assets/js/just-the-docs.js"></script><metaname="viewport"content="width=device-width, initial-scale=1"><!-- Begin Jekyll SEO tag v2.8.0 --><title>Safe Saving | SyncFiles Documentation</title><metaname="generator"content="Jekyll v4.2.2"/><metaproperty="og:title"content="Safe Saving"/><metaproperty="og:locale"content="en_US"/><metaname="description"content="Write an awesome description for your new site here. You can edit this line in _config.yml. It will appear in your document head meta (for Google search results) and in your feed.xml site description."/><metaproperty="og:description"content="Write an awesome description for your new site here. You can edit this line in _config.yml. It will appear in your document head meta (for Google search results) and in your feed.xml site description."/><linkrel="canonical"href="https://depp.github.io/syncfiles/tech/safe-saving"/><metaproperty="og:url"content="https://depp.github.io/syncfiles/tech/safe-saving"/><metaproperty="og:site_name"content="SyncFiles Documentation"/><metaproperty="og:type"content="website"/><metaname="twitter:card"content="summary"/><metaproperty="twitter:title"content="Safe Saving"/><scripttype="application/ld+json">{"@context":"https://schema.org","@type":"WebPage","description":"Write an awesome description for your new site here. You can edit this line in _config.yml. It will appear in your document head meta (for Google search results) and in your feed.xml site description.","headline":"Safe Saving","url":"https://depp.github.io/syncfiles/tech/safe-saving"}</script><!-- End Jekyll SEO tag --></head><body><svgxmlns="http://www.w3.org/2000/svg"style="display: none;"><symbolid="svg-link"viewBox="0 0 24 24"><title>Link</title><svgxmlns="http://www.w3.org/2000/svg"width="24"height="24"viewBox="0 0 24 24"fill="none"stroke="currentColor"stroke-width="2"stroke-linecap="round"stroke-linejoin="round"class="feather feather-link"><pathd="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><pathd="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></symbol><symbolid="svg-search"viewBox="0 0 24 24"><title>Search</title><svgxmlns="http://www.w3.org/2000/svg"width="24"height="24"viewBox="0 0 24 24"fill="none"stroke="currentColor"stroke-width="2"stroke-linecap="round"stroke-linejoin="round"class="feather feather-search"><circlecx="11"cy="11"r="8"></circle><linex1="21"y1="21"x2="16.65"y2="16.65"></line></svg></symbol><symbolid="svg-menu"viewBox="0 0 24 24"><title>Menu</title><svgxmlns="http://www.w3.org/2000/svg"width="24"height="24"viewBox="0 0 24 24"fill="none"stroke="currentColor"stroke-width="2"stroke-linecap="round"stroke-linejoin="round"class="feather feather-menu"><linex1="3"y1="12"x2="21"y2="12"></line><linex1="3"y1="6"x2="21"y2="6"></line><linex1="3"y1="18"x2="21"y2="18"></line></svg></symbol><symbolid="svg-arrow-right"viewBox="0 0 24 24"><title>Expand</title><svgxmlns="http://www.w3.org/2000/svg"width="24"height="24"viewBox="0 0 24 24"fill="none"stroke="currentColor"stroke-width="2"stroke-linecap="round"stroke-linejoin="round"class="feather feather-chevron-right"><polylinepoints="9 18 15 12 9 6"></polyline></svg></symbol><symbolid="svg-doc"viewBox="0 0 24 24"><title>Document</title><svgxmlns="http://www.w3.org/2000/svg"width="24"height="24"viewBox="0 0 24 24"fill="none"stroke="currentColor"stroke-width="2"stroke-linecap="round"stroke-linejoin="round"class="feather feather-file"><pathd="M132H6a2200
</code></pre></div></div><p>This function exchanges the <em>contents</em> of the two files (both forks), and exchanges the modification dates, but leaves the other metadata alone.</p><p>The recipe for safe saving on HFS volumes is:</p><ol><li><p>Save the document to a temporary file on the same volume.</p></li><li><p>Exchange the contents of the original file and the temporary file with <codeclass="language-plaintext highlighter-rouge">FSpExchangeFiles</code>.</p></li></ol><p>You can test that <codeclass="language-plaintext highlighter-rouge">FSpExchangeFiles</code> is supported by a volume by getting the volume parameters. Not all filesystems support this operation.</p><h2id="mac-os-x"><ahref="#mac-os-x"class="anchor-heading"aria-labelledby="mac-os-x"><svgviewBox="0 0 16 16"aria-hidden="true"><usexlink:href="#svg-link"></use></svg></a> Mac OS X </h2><p>Mac OS X provides a Unix system call that provides the same functionality as <codeclass="language-plaintext highlighter-rouge">FSpExchangeFiles</code>, but with a Unix API.</p><divclass="language-c highlighter-rouge"><divclass="highlight"><preclass="highlight"><code><spanclass="kt">int</span><spanclass="nf">exchangedata</span><spanclass="p">(</span>
</code></pre></div></div><p>However, this function does not work on APFS.</p><h2id="mac-os-x-106"><ahref="#mac-os-x-106"class="anchor-heading"aria-labelledby="mac-os-x-106"><svgviewBox="0 0 16 16"aria-hidden="true"><usexlink:href="#svg-link"></use></svg></a> Mac OS X 10.6+ </h2><p>Starting on Mac OS X 10.6, the Foundation framework provides a method for safely replacing an item on the filesystem with a new item. This method is present on <codeclass="language-plaintext highlighter-rouge">NSFileManager</code>:</p><divclass="language-objc highlighter-rouge"><divclass="highlight"><preclass="highlight"><code><spanclass="k">-</span><spanclass="p">(</span><spanclass="n">BOOL</span><spanclass="p">)</span><spanclass="nf">replaceItemAtURL</span><spanclass="p">:(</span><spanclass="n">NSURL</span><spanclass="o">*</span><spanclass="p">)</span><spanclass="nv">originalItemURL</span>
</code></pre></div></div><p>This method should be preferred for Mac OS X 10.6 and newer. Unlike <codeclass="language-plaintext highlighter-rouge">exchangedata()</code>, this function works on APFS.</p></div></div><divclass="search-overlay"></div></div></body></html>