https://redmine.ruby-lang.org/https://redmine.ruby-lang.org/favicon.ico?17113305112017-06-29T03:16:27ZRuby Issue Tracking SystemRuby master - Feature #13696: Add exchange and noreplace options to File.renamehttps://redmine.ruby-lang.org/issues/13696?journal_id=655182017-06-29T03:16:27Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul><li><strong>Description</strong> updated (<a title="View differences" href="/journals/65518/diff?detail_id=45443">diff</a>)</li></ul><p>Glass_saga (Masaki Matsushita) wrote:</p>
<blockquote>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="no">File</span><span class="p">.</span><span class="nf">rename</span><span class="p">(</span><span class="s2">"hoge"</span><span class="p">,</span> <span class="s2">"fuga"</span><span class="p">,</span> <span class="ss">exchange: </span><span class="kp">true</span><span class="p">)</span> <span class="c1"># atomically exchanged</span>
</code></pre>
</blockquote>
<p>How about another method, say, <code>File.exchange</code>?</p>
<blockquote>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="no">File</span><span class="p">.</span><span class="nf">rename</span><span class="p">(</span><span class="s2">"hoge"</span><span class="p">,</span> <span class="s2">"fuga"</span><span class="p">,</span> <span class="ss">noreplace: </span><span class="kp">true</span><span class="p">)</span> <span class="c1">#=> File exists @ syserr_fail2_in - fuga (Errno::EEXIST)</span>
</code></pre>
</blockquote>
<p>I prefer positive-form to negative-form as an option, i.e., <code>File.rename(old, new[, replace: true])</code>.</p> Ruby master - Feature #13696: Add exchange and noreplace options to File.renamehttps://redmine.ruby-lang.org/issues/13696?journal_id=655502017-06-29T17:49:06Zshevegen (Robert A. Heiler)shevegen@gmail.com
<ul></ul><blockquote>
<p>I prefer positive-form to negative-form as an option</p>
</blockquote>
<p>I do too. I found it in general easier (for me) to understand positive/forward<br>
declarations.</p> Ruby master - Feature #13696: Add exchange and noreplace options to File.renamehttps://redmine.ruby-lang.org/issues/13696?journal_id=655532017-06-30T03:48:57ZGlass_saga (Masaki Matsushita)glass.saga@gmail.com
<ul><li><strong>File</strong> <a href="/attachments/6620">patch.diff</a> <a class="icon-only icon-download" title="Download" href="/attachments/download/6620/patch.diff">patch.diff</a> added</li></ul><p>Thank you for your comments.<br>
I implemented FIle.exchange and made the noreplace flag in positive-form.</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="no">File</span><span class="p">.</span><span class="nf">write</span><span class="p">(</span><span class="s2">"hoge"</span><span class="p">,</span> <span class="s2">"hoge"</span><span class="p">)</span>
<span class="no">File</span><span class="p">.</span><span class="nf">write</span><span class="p">(</span><span class="s2">"fuga"</span><span class="p">,</span> <span class="s2">"fuga"</span><span class="p">)</span>
<span class="no">File</span><span class="p">.</span><span class="nf">exchange</span><span class="p">(</span><span class="s2">"hoge"</span><span class="p">,</span> <span class="s2">"fuga"</span><span class="p">)</span>
<span class="nb">p</span> <span class="no">File</span><span class="p">.</span><span class="nf">read</span><span class="p">(</span><span class="s2">"fuga"</span><span class="p">)</span> <span class="c1">#=> "hoge"</span>
<span class="no">File</span><span class="p">.</span><span class="nf">rename</span><span class="p">(</span><span class="s2">"hoge"</span><span class="p">,</span> <span class="s2">"fuga"</span><span class="p">,</span> <span class="ss">replace: </span><span class="kp">false</span><span class="p">)</span> <span class="c1">#=> File exists @ syserr_fail2_in - fuga (Errno::EEXIST)</span>
</code></pre> Ruby master - Feature #13696: Add exchange and noreplace options to File.renamehttps://redmine.ruby-lang.org/issues/13696?journal_id=655542017-06-30T04:24:35Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul></ul><p>Is "atomically" important?<br>
If no, it'd be possible to implement without kernel support.</p> Ruby master - Feature #13696: Add exchange and noreplace options to File.renamehttps://redmine.ruby-lang.org/issues/13696?journal_id=655642017-06-30T08:21:37Znormalperson (Eric Wong)normalperson@yhbt.net
<ul></ul><p><a href="mailto:nobu@ruby-lang.org" class="email">nobu@ruby-lang.org</a> wrote:</p>
<blockquote>
<p>Is "atomically" important?<br>
If no, it'd be possible to implement without kernel support.</p>
</blockquote>
<p>Yes, atomicity is an important distinction.</p>
<p>We should not try to implement this non-atomically without kernel<br>
support; it will quietly break code which expects the atomicity.</p>
<p>I am recalling similar problems around posix_fallocate in glibc:<br>
<a href="https://sourceware.org/bugzilla/show_bug.cgi?id=15661" class="external">https://sourceware.org/bugzilla/show_bug.cgi?id=15661</a></p> Ruby master - Feature #13696: Add exchange and noreplace options to File.renamehttps://redmine.ruby-lang.org/issues/13696?journal_id=657582017-07-13T11:01:14ZGlass_saga (Masaki Matsushita)glass.saga@gmail.com
<ul></ul><p>I think atomicity is important. If the kernel doesn't support atomic exchange, File.exchange should raise NotImplementedError and users can handle it properly.</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">begin</span>
<span class="no">File</span><span class="p">.</span><span class="nf">exchange</span><span class="p">(</span><span class="s2">"hoge"</span><span class="p">,</span> <span class="s2">"fuga"</span><span class="p">)</span>
<span class="k">rescue</span> <span class="no">NotImplementedError</span>
<span class="c1"># exchange files in another way</span>
<span class="k">end</span>
</code></pre> Ruby master - Feature #13696: Add exchange and noreplace options to File.renamehttps://redmine.ruby-lang.org/issues/13696?journal_id=668892017-09-25T08:54:14Zduerst (Martin Dürst)duerst@it.aoyama.ac.jp
<ul></ul><p>nobu (Nobuyoshi Nakada) wrote:</p>
<blockquote>
<p>Glass_saga (Masaki Matsushita) wrote:</p>
<blockquote>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="no">File</span><span class="p">.</span><span class="nf">rename</span><span class="p">(</span><span class="s2">"hoge"</span><span class="p">,</span> <span class="s2">"fuga"</span><span class="p">,</span> <span class="ss">exchange: </span><span class="kp">true</span><span class="p">)</span> <span class="c1"># atomically exchanged</span>
</code></pre>
</blockquote>
<p>How about another method, say, <code>File.exchange</code>?</p>
<blockquote>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="no">File</span><span class="p">.</span><span class="nf">rename</span><span class="p">(</span><span class="s2">"hoge"</span><span class="p">,</span> <span class="s2">"fuga"</span><span class="p">,</span> <span class="ss">noreplace: </span><span class="kp">true</span><span class="p">)</span> <span class="c1">#=> File exists @ syserr_fail2_in - fuga (Errno::EEXIST)</span>
</code></pre>
</blockquote>
<p>I prefer positive-form to negative-form as an option, i.e., <code>File.rename(old, new[, replace: true])</code>.</p>
</blockquote>
<p>I think both <code>exchange:</code> and <code>noreplace:</code> are difficult to understand for an outside user. I think <code>overwrite:</code> is much easier to understand.</p> Ruby master - Feature #13696: Add exchange and noreplace options to File.renamehttps://redmine.ruby-lang.org/issues/13696?journal_id=669012017-09-25T12:37:28Zshyouhei (Shyouhei Urabe)shyouhei@ruby-lang.org
<ul></ul><p>I think it's hard to have platform-specific API extensions like this to be in-core. If atomicity is such an important feature (and I'm sure it is), it's hard to emulate this in pure-userland for people without OS that support it.</p>
<p>This should be implemented as a gem. That way, gem install can bail out purposefully for unsupported platforms. That should prevent hidden race condition that you want to avoid.</p> Ruby master - Feature #13696: Add exchange and noreplace options to File.renamehttps://redmine.ruby-lang.org/issues/13696?journal_id=681202017-12-01T16:34:25Znaruse (Yui NARUSE)naruse@airemix.jp
<ul><li><strong>Target version</strong> deleted (<del><i>2.5</i></del>)</li></ul>