<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-us"><generator uri="https://gohugo.io/" version="0.171.0">Hugo</generator><title type="html">Macos on sunshowers</title><subtitle type="html">thoughts and ideas</subtitle><link href="https://sunshowers.io/tags/macos/" rel="alternate" type="text/html" title="html"/><link href="https://sunshowers.io/tags/macos/index.xml" rel="alternate" type="application/rss+xml" title="rss"/><link href="https://sunshowers.io/tags/macos/atom.xml" rel="self" type="application/atom+xml" title="atom"/><updated>2026-12-03T02:45:57+00:00</updated><rights>© Rain 2020-present. Licensed under CC BY-NC-SA 4.0 unless marked otherwise.</rights><author><name>Rain</name></author><id>https://sunshowers.io/tags/macos/</id><entry><title type="html">Dealing with tempfile cleaners</title><link href="https://sunshowers.io/posts/tmp-cleaner/?utm_source=atom_feed" rel="alternate" type="text/html"/><id>https://sunshowers.io/posts/tmp-cleaner/</id><published>2023-10-05T00:00:00+00:00</published><updated>2023-10-05T00:00:00+00:00</updated><content type="html"><![CDATA[<blockquote>Storing caches in an ephemeral spot.</blockquote><p><em>I used to use this as a place to write long-form thoughts, but with the demise of Twitter I want to
also write short-form posts here under the <a href="/tags/shorts/">#shorts</a> tag. I also post <a href="https://hachyderm.io/@rain">on
Mastodon</a> but that&rsquo;s a little more ephemeral and less searchable than
this blog.</em></p>
<p>Let&rsquo;s say you&rsquo;re creating a temporary directory with some cached artifacts to share across several
tests. It&rsquo;s a bit more ephemeral than, say, the Cargo <code>target/</code> directory, so your initial idea
might be to just put your artifacts in <a href="https://doc.rust-lang.org/nightly/std/env/fn.temp_dir.html">the temporary
directory</a>. Also, let&rsquo;s say that you
want to reuse the directory across a span of several days or more (perhaps by hashing a set of
inputs and using that to name the directory).</p>
<p>If your artifacts span multiple files, <em>do not do this</em>. The reason is that some operating systems
and environments have temporary file cleaners, also known as tempfile reapers. macOS <a href="https://apple.stackexchange.com/a/438931">ships one by
default</a>, and some other environments also have a cleaner
<a href="https://packages.debian.org/sid/tmpreaper">like <code>tmpreaper</code></a> configured. These reapers may end up
deleting some of your temporary files but not others, leading to an inconsistent cache with part of
it missing.</p>
<h2 id="what-can-you-do-instead">What can you do instead?</h2>
<p>The easiest alternative is to store artifacts in a single file rather than a directory. That way,
either the file exists or it got cleaned up&mdash;there&rsquo;s no in-between, inconsistent state.</p>
<ul>
<li>If you can get away with not extracting files and all you need is random access, a zip file works well.</li>
<li>For sequential access or if you need to extract files, a (possibly compressed) tarball is a great
option.</li>
</ul>
<p>Another option is to record the list of files in a manifest, and invalidate the cache if any of those
files are missing.</p>
<p>A third option is to store your cache somewhere other than the system temporary directory. But then
you need a way to evict old cache entries.</p>
<p>(Also, be aware that if your temporary directory is in a global location like <code>/tmp</code>, users can
overwrite each other&rsquo;s artifacts. You can avoid that with a
<a href="https://en.wikipedia.org/wiki/Umask"><code>umask</code></a> and/or something like
<a href="https://doc.rust-lang.org/stable/std/os/unix/fs/trait.OpenOptionsExt.html#tymethod.mode"><code>OpenOptionsExt::mode</code></a>).</p>
]]></content><category scheme="https://sunshowers.io/tags/macos" term="macos" label="macos"/><category scheme="https://sunshowers.io/tags/shorts" term="shorts" label="shorts"/></entry></feed>