<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2682: filesystem::copy() won't create a symlink to a directory</title>
<meta property="og:title" content="Issue 2682: filesystem::copy() won't create a symlink to a directory">
<meta property="og:description" content="C++ library issue. Status: C++20">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2682.html">
<meta property="og:type" content="website">
<meta property="og:image" content="http://cplusplus.github.io/LWG/images/cpp_logo.png">
<meta property="og:image:alt" content="C++ logo">
<style>
  p {text-align:justify}
  li {text-align:justify}
  pre code.backtick::before { content: "`" }
  pre code.backtick::after { content: "`" }
  blockquote.note
  {
    background-color:#E0E0E0;
    padding-left: 15px;
    padding-right: 15px;
    padding-top: 1px;
    padding-bottom: 1px;
  }
  ins {background-color:#A0FFA0}
  del {background-color:#FFA0A0}
  table.issues-index { border: 1px solid; border-collapse: collapse; }
  table.issues-index th { text-align: center; padding: 4px; border: 1px solid; }
  table.issues-index td { padding: 4px; border: 1px solid; }
  table.issues-index td:nth-child(1) { text-align: right; }
  table.issues-index td:nth-child(2) { text-align: left; }
  table.issues-index td:nth-child(3) { text-align: left; }
  table.issues-index td:nth-child(4) { text-align: left; }
  table.issues-index td:nth-child(5) { text-align: center; }
  table.issues-index td:nth-child(6) { text-align: center; }
  table.issues-index td:nth-child(7) { text-align: left; }
  table.issues-index td:nth-child(5) span.no-pr { color: red; }
  @media (prefers-color-scheme: dark) {
     html {
        color: #ddd;
        background-color: black;
     }
     ins {
        background-color: #225522
     }
     del {
        background-color: #662222
     }
     a {
        color: #6af
     }
     a:visited {
        color: #6af
     }
     blockquote.note
     {
        background-color: rgba(255, 255, 255, .10)
     }
  }
</style>
</head>
<body>
<hr>
<p><em>This page is a snapshot from the LWG issues list, see the <a href="lwg-active.html">Library Active Issues List</a> for more information and the meaning of <a href="lwg-active.html#C++20">C++20</a> status.</em></p>
<h3 id="2682"><a href="lwg-defects.html#2682">2682</a>. <code>filesystem::copy()</code> won't create a symlink to a directory</h3>
<p><b>Section:</b> 31.12.13.4 <a href="https://wg21.link/fs.op.copy">[fs.op.copy]</a> <b>Status:</b> <a href="lwg-active.html#C++20">C++20</a>
 <b>Submitter:</b> Jonathan Wakely <b>Opened:</b> 2016-04-19 <b>Last modified:</b> 2021-02-25</p>
<p><b>Priority: </b>2
</p>
<p><b>View all other</b> <a href="lwg-index.html#fs.op.copy">issues</a> in [fs.op.copy].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#C++20">C++20</a> status.</p>
<p><b>Discussion:</b></p>
<p>(First raised in c++std-lib-38544)</p>

<p><code>filesystem::copy</code> doesn't create a symlink to a directory in this case:</p>

<blockquote>
<code>copy("/", "root", copy_options::create_symlinks);</code>
</blockquote>

<p>
If the first path is a file then a symlink is created, but I think my
implementation is correct to do nothing for a directory. We get to
bullet 31.12.13.4 <a href="https://wg21.link/fs.op.copy">[fs.op.copy]</a> (3.6) where <code>is_directory(f)</code> is true, but <code>options
== create_symlinks</code>, so we go to the next bullet (3.7) which says
"Otherwise, no effects."
<p/>
I think the case above should either create a symlink, or should
report an error. GNU cp -s gives an error in this case, printing
"omitting directory '/'". An error seems reasonable, you can use
<code>create_symlink</code> to create a symlink to a directory.
</p>

<p><i>[2016-05 Issues Telecon]</i></p>

<p>
This is related to <a href="lwg-defects.html#2681" title="filesystem::copy() cannot copy symlinks (Status: C++17)">2681</a><sup><a href="https://cplusplus.github.io/LWG/issue2681" title="Latest snapshot">(i)</a></sup>; and should be considered together.
</p>

<p><i>[2016-08 Chicago]</i></p>

<p>Wed AM: Move to Tentatively Ready</p>

<p><strong>Previous resolution [SUPERSEDED]:</strong></p>
<blockquote class="note">
<p>Add a new bullet following (3.6) in 31.12.13.4 <a href="https://wg21.link/fs.op.copy">[fs.op.copy]</a> as shown:</p>

<blockquote>
<ul>
<li><p>If <code>!exists(t)</code>, then <code>create_directory(to, from)</code>.</p></li>
<li><p>Then, iterate over the files in <code>from</code>, as if by <code>for (directory_entry&amp; x :
 directory_iterator(from))</code>, and for each iteration
  <code>copy(x.path(), to/x.path().filename(), options | copy_options::unspecified )</code></p></li>
<li><p><ins>Otherwise, if <code>is_directory(f) &amp;&amp; (options &amp;
copy_options::create_symlinks) != copy_options::none</code>, then report an
error with an <code>error_code</code> argument equal to
<code>make_error_code(errc::is_a_directory)</code>.</ins></p></li>
<li><p>Otherwise, no effects.</p></li>
</ul>
</blockquote>
</blockquote>

<p><i>[2016-10-16, Eric reopens and provides improved wording]</i></p>

<p>
The current PR makes using <code>copy(...)</code> to copy/create a directory symlink an error. For example, the following 
is now an error:
</p>
<blockquote><pre>
copy("/", "root", copy_options::create_symlinks);
</pre></blockquote>
<p>
However the current PR doesn't handle the case where both <code>copy_options::create_symlinks</code> and 
<code>copy_options::recursive</code> are specified. This case is still incorrectly handled by bullet (3.6) [fs.op.copy].
<p/>
I suggest we move the PR before this bullet so that it catches the recursive copy case, since currently the conditions 
are ordered:
</p>
<blockquote>
<p>
3.6 Otherwise if <code>is_directory(f) &amp;&amp; (bool(options &amp; copy_options::recursive) || ...)</code><br/>
3.X Otherwise if <code>is_directory(f) &amp;&amp; bool(options &amp; copy_options::create_symlinks)</code>
</p>
</blockquote>
<p>
So 3.6 catches <code>create_symlinks | recursive</code> but I believe we want 3.X to handle it instead.
</p>

<p><i>[2018-01-26 issues processing telecon]</i></p>

<p>Status to 'Review'; we think this is OK but want some implementation experience before adopting it.</p>

<p><i>[2018-01-29 Jonathan says]</i></p>

<p>The proposed resolution for LWG 2682 has been in GCC's Filesystem TS implementation for more than a year. 
It's also in our <code>std::filesystem</code> implementation on the subversion trunk.</p>

<p><i>[2018-06; Rapperswil Wednesday evening]</i></p>

<p>
JW: can we use the words we are shipping already since two years?<br/>
BO: what we got is better than what we had before<br/>
no objection to moving to Ready<br/>
ACTION move to Ready<br/>
ACTION link 2682 and LWG <a href="lwg-active.html#3057" title="Correct copy_options handling (Status: Open)">3057</a><sup><a href="https://cplusplus.github.io/LWG/issue3057" title="Latest snapshot">(i)</a></sup> and set a priority 2 and look at 3057 in San Diego 
<p/>
Daniel rebases wording to N4750.
</p>
<p><i>[2018-11, Adopted in San Diego]</i></p>



<p id="res-2682"><b>Proposed resolution:</b></p>
<p>This wording is relative to <a href="https://wg21.link/n4750">N4750</a>.</p>

<ol>
<li><p>Add a new bullet before (4.8) in 31.12.13.4 <a href="https://wg21.link/fs.op.copy">[fs.op.copy]</a> as shown:</p>

<blockquote>
<ol style="list-style-type: none">
<li><p>(4.7) &mdash; Otherwise, if <code>is_regular_file(f)</code>, then:</p>
<blockquote>
[&hellip;]
</blockquote>
</li>
<li><p><ins>(4.?) &mdash; Otherwise, if</ins></p>
<blockquote>
<blockquote><pre>
<ins>is_directory(f) &amp;&amp; 
(options &amp; copy_options::create_symlinks) != copy_options::none</ins>
</pre></blockquote>
<p> 
<ins>then report an error with an <code>error_code</code> argument equal to
<code>make_error_code(errc::is_a_directory)</code>.</ins></p></blockquote></li>
<li><p>(4.8) &mdash; Otherwise, if</p>
<blockquote>
<blockquote><pre>
is_directory(f) &amp;&amp;
((options &amp; copy_options::recursive) != copy_options::none ||
options == copy_options::none)
</pre></blockquote>
<p>
then:
</p>
</blockquote>
<ol style="list-style-type: none">
<li><p>(4.8.1) &mdash; If <code>exists(t)</code> is <code>false</code>, then <code>create_directory(to, from)</code>.</p></li>
<li><p>(4.8.2) &mdash; Then, iterate over the files in <code>from</code>, as if by</p> 
<blockquote><pre>
for (const directory_entry&amp; x : directory_iterator(from))
  copy(x.path(), to/x.path().filename(),
    options | copy_options::<i>in-recursive-copy</i>);
</pre></blockquote>
<p>where <code><i>in-recursive-copy</i></code> is a bitmask element of <code>copy_options</code> that is not one of the
elements in 31.12.8.3 <a href="https://wg21.link/fs.enum.copy.opts">[fs.enum.copy.opts]</a>.</p>
</li>
</ol>
</li>
<li><p>(4.9) &mdash; Otherwise, for the signature with argument <code>ec</code>, <code>ec.clear()</code>.</p></li>
<li><p>(4.10) &mdash; Otherwise, no effects.</p></li>
</ol>
</blockquote>

</li>
</ol>





</body>
</html>
