From unknown Sat Jul 26 21:33:00 2025 X-Loop: help-debbugs@gnu.org Subject: bug#32455: cp gets confused by symlinks to parent directory Resent-From: Mike Crowe Original-Sender: "Debbugs-submit" Resent-CC: bug-coreutils@gnu.org Resent-Date: Thu, 16 Aug 2018 14:48:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 32455 X-GNU-PR-Package: coreutils X-GNU-PR-Keywords: To: 32455@debbugs.gnu.org X-Debbugs-Original-To: bug-coreutils@gnu.org Received: via spool by submit@debbugs.gnu.org id=B.153443084825550 (code B ref -1); Thu, 16 Aug 2018 14:48:01 +0000 Received: (at submit) by debbugs.gnu.org; 16 Aug 2018 14:47:28 +0000 Received: from localhost ([127.0.0.1]:52678 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1fqJYe-0006e2-4c for submit@debbugs.gnu.org; Thu, 16 Aug 2018 10:47:28 -0400 Received: from eggs.gnu.org ([208.118.235.92]:59717) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1fqJYc-0006do-Ea for submit@debbugs.gnu.org; Thu, 16 Aug 2018 10:47:26 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fqJYW-0000wq-Hv for submit@debbugs.gnu.org; Thu, 16 Aug 2018 10:47:21 -0400 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on eggs.gnu.org X-Spam-Level: X-Spam-Status: No, score=-0.0 required=5.0 tests=BAYES_20 autolearn=disabled version=3.3.2 Received: from lists.gnu.org ([2001:4830:134:3::11]:54245) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1fqJYW-0000wm-ET for submit@debbugs.gnu.org; Thu, 16 Aug 2018 10:47:20 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:52467) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fqJYV-00056z-Is for bug-coreutils@gnu.org; Thu, 16 Aug 2018 10:47:20 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fqJYS-0000vJ-Ue for bug-coreutils@gnu.org; Thu, 16 Aug 2018 10:47:19 -0400 Received: from avasout05.plus.net ([84.93.230.250]:49524) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1fqJYS-0000u1-NE for bug-coreutils@gnu.org; Thu, 16 Aug 2018 10:47:16 -0400 Received: from deneb ([80.229.24.9]) by smtp with ESMTP id qJYOfiTNR05ijqJYQfQaXq; Thu, 16 Aug 2018 15:47:14 +0100 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.3 cv=QJJGuDDL c=1 sm=1 tr=0 a=E/9URZZQ5L3bK/voZ0g0HQ==:117 a=E/9URZZQ5L3bK/voZ0g0HQ==:17 a=kj9zAlcOel0A:10 a=dapMudl6Dx4A:10 a=_GVmxRpA33QXqo8dIugA:9 a=CjuIK1q_8ugA:10 Received: from mac by deneb with local (Exim 4.89) (envelope-from ) id 1fqJYO-0003FB-Px for bug-coreutils@gnu.org; Thu, 16 Aug 2018 15:47:12 +0100 Date: Thu, 16 Aug 2018 15:47:12 +0100 From: Mike Crowe Message-ID: <20180816144712.qycwrcjtqgipzfca@mcrowe.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: NeoMutt/20170113 (1.7.2) X-CMAE-Envelope: MS4wfKO4rK99CIKOqMZ99RFmTfi/V7wlYknUOuSOTDFfxNeNmTaXfJfv/gvVpvoAL/UGvQ/UUZf2LsVP4M5nsoa/vPtgHlacvHb7//m2lOmbmRrhgOmdC+lw 3cgTjhWaYvpRd0OLZNdt70IbpOS8KPqlZlA= X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x (no timestamps) [generic] [fuzzy] X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6.x X-Received-From: 2001:4830:134:3::11 X-Spam-Score: -5.0 (-----) X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -6.0 (------) If cp is passed the -d option and told to copy a symlink to the directory containing the symlink then it ends up removing the target directory so it is unable to create the symlink. Reproduction script: --8<-- #!/bin/sh set -x rm -rf temp mkdir -p temp/src temp/dest ln -s . temp/src/self # This one works cp -vd temp/src/self temp/dest/self # This one fails cp -vd temp/src/self temp/dest/self # This one works again cp -vd temp/src/self temp/dest/self -->8-- Output: --8<-- + rm -rf temp + mkdir -p temp/src temp/dest + ln -s . temp/src/self + cp -vd temp/src/self temp/dest/self 'temp/src/self' -> 'temp/dest/self' + cp -vd temp/src/self temp/dest/self removed 'temp/dest/self/self' 'temp/src/self' -> 'temp/dest/self/self' cp: cannot create symbolic link 'temp/dest/self/self': No such file or directory + cp -vd temp/src/self temp/dest/self 'temp/src/self' -> 'temp/dest/self' -->8-- (coreutils 8.26, 8.28 and 8.30 all appear to behave the same way.) Expected behaviour: There should be no error message emitted by the second invocation of cp, and the target directory should be in the same state as it is after the first or third attempts to copy the symlink. Or, maybe I'm fundamentally misunderstanding something. I couldn't find this mentioned in the FAQ or "gotchas". Thanks. Mike. From unknown Sat Jul 26 21:33:00 2025 X-Loop: help-debbugs@gnu.org Subject: bug#32455: cp gets confused by symlinks to parent directory Resent-From: Assaf Gordon Original-Sender: "Debbugs-submit" Resent-CC: bug-coreutils@gnu.org Resent-Date: Fri, 18 Jan 2019 10:38:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 32455 X-GNU-PR-Package: coreutils X-GNU-PR-Keywords: To: Mike Crowe , 32455@debbugs.gnu.org Received: via spool by 32455-submit@debbugs.gnu.org id=B32455.154780787810677 (code B ref 32455); Fri, 18 Jan 2019 10:38:02 +0000 Received: (at 32455) by debbugs.gnu.org; 18 Jan 2019 10:37:58 +0000 Received: from localhost ([127.0.0.1]:35719 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1gkRXB-0002m8-8T for submit@debbugs.gnu.org; Fri, 18 Jan 2019 05:37:57 -0500 Received: from mail-pg1-f170.google.com ([209.85.215.170]:41877) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1gkRXA-0002ls-8U for 32455@debbugs.gnu.org; Fri, 18 Jan 2019 05:37:56 -0500 Received: by mail-pg1-f170.google.com with SMTP id m1so5861518pgq.8 for <32455@debbugs.gnu.org>; Fri, 18 Jan 2019 02:37:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-language:content-transfer-encoding; bh=bzrz+qjvqo1m4TxeutZzcbb8CKEMKB0gm4S0mhL5nbA=; b=duLteyljRg6KJAzL/NXh4eK8DaAcU523L9HE9tlTm36jFIuf9mNiUN+xurVyHUT9v/ l67AAxolZ35FihQtUUbBxMj50blJVLh4HDhRMfN9cLa6tsZdNm9klm837sL5f0enZ4NS KIRplKAFPDLNn5CqdKPTgJVD4aF0K3KWYP/yydvtL2izXvmmz0dC7j7mgn4VjxLHIL2Z 0I6bljDh9YIBvmCldHa4VIGi9X5PPJurgXWdGXaIhPolrWaIiqyZdSXKMtfckBrD0hse HVirhqZ6lqBQUH+/eNwkMIe1uy3b3lx80AUyvuJqIR8fXhQSASQS4qUI3nYhi67LBPzb mbjw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=bzrz+qjvqo1m4TxeutZzcbb8CKEMKB0gm4S0mhL5nbA=; b=ARdmUOwWmMNJWHr5aVOpTP6BSC/KssgZzMPC3J/+OERqelKV/x+lAtshywNM+twAiT oOhgiuHlRDUktF7kgq9Og3Xf0m8G2Lyu3YrrFs5mfmeGDyopkb5fhBf6FHg+qjRdjuDV 37S3scuZmeYm0UXCp24IbHouwTrCiGwhLDTO+o/zC22BbuuKSnzCzLpdCUmfXX3apqST CkSByrwsitZpvQOUKaOMtSEK9ObcCCGa6Ta/uX/y2G77dVvUkOsC+KWXJv7b8IF1rfUS IgYhCDSl7KadlAbp2A8ifNVWBhU5lZ3R/grPdJvT3rWEkjp8q3JivQHQysIrHtZaAmL9 eAAg== X-Gm-Message-State: AJcUukdbMQJN0WVSY5SpQutUD/ERom3FHB7HRV6yK0cbuqnxXF5HNpd6 WQsef4zao5Nq/dkJWfh5fmRfkoCXSZk= X-Google-Smtp-Source: ALg8bN5yJYzvDqMoUopINst8n28fBce5eWLbNUPRoevE7Wn59249OOgJ9/aGI4uswLENs5LRpvWfsQ== X-Received: by 2002:a63:bf0b:: with SMTP id v11mr17387652pgf.302.1547807869291; Fri, 18 Jan 2019 02:37:49 -0800 (PST) Received: from tomato.housegordon.com (moose.housegordon.com. [184.68.105.38]) by smtp.googlemail.com with ESMTPSA id 24sm16396914pfl.32.2019.01.18.02.37.47 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 18 Jan 2019 02:37:47 -0800 (PST) References: <20180816144712.qycwrcjtqgipzfca@mcrowe.com> From: Assaf Gordon Message-ID: <2ee73c38-4cf5-d6d3-ca03-3f0a5dedc964@gmail.com> Date: Fri, 18 Jan 2019 03:37:46 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.4.0 MIME-Version: 1.0 In-Reply-To: <20180816144712.qycwrcjtqgipzfca@mcrowe.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit X-Spam-Score: 0.0 (/) X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -1.0 (-) tags 32455 notabug close 32455 stop Hello, It seems your message has not been replied to in a long while. Sorry about that. On 2018-08-16 8:47 a.m., Mike Crowe wrote: > If cp is passed the -d option and told to copy a symlink to the directory > containing the symlink then it ends up removing the target directory so it > is unable to create the symlink. If my understanding is correct, the "-d" flag is not relevant to the issue. The problem is that "self" is a symlink to a directory: > > Reproduction script: > > --8<-- > #!/bin/sh > set -x > > rm -rf temp > mkdir -p temp/src temp/dest > > ln -s . temp/src/self > > # This one works > cp -vd temp/src/self temp/dest/self This works because "temp/dest/self" does not exist. In this case, "temp/dest" is taken as the destination directory, and "self" is taken as the name of the file/dir/symlink to create. That is, you could run "cp -vd temp/src/self temp/dest/foobar" to create "foobar" as a copy of "self". > # This one fails > cp -vd temp/src/self temp/dest/self Here, "temp/dest/self" already exists, and it is a symlink to a directory. Meaning, the request is: copy "temp/src/self" into the directory "temp/dest/self/" (and create "temp/dest/self/self"). This would have gone well, except that because "self" is a symlink to ".", it can be resolved indefinitely: $ file temp/dest/self temp/dest/self: symbolic link to . $ file temp/dest/self/self/self/self/self/self temp/dest/self/self/self/self/self/self: symbolic link to . $ file temp/dest/self/self/self/self/self/self/self temp/dest/self/self/self/self/self/self/self: symbolic link to . "cp" first removes "temp/dest/self/self" (which is valid), but then, "temp/dest/self" is gone (since it is the same file path after resolving it). Hence, "cp" fails by saying "no such directory" on "temp/dest/self/self". When this step is done, "temp/dest/self" does not exist, and so: > # This one works again > cp -vd temp/src/self temp/dest/self This works as before. You can observe what happens on the kernel level by adding "strace -e trace=file" before the "cp" commands, this might help in deeper understanding. To illustrate this differently: When creating regular directories and files, then deleting the innermost files, it is naively expected that the parent directories still exist: mkdir -p a/b/c/d touch a/b/c/d/e rm a/b/c/d/e That is, a normal program can call "dirname("a/b/c/d/e")" to get the parent directory of "e", and expect it to still exist even after "e" is deleted. But with your case: $ mkdir a $ ln -s . a/self $ rm a/self/self/self/self/self/self All the "apparent" parent directories ("self/self/self/self/self") are gone! > Expected behaviour: > > There should be no error message emitted by the second invocation of cp, > and the target directory should be in the same state as it is after the > first or third attempts to copy the symlink. Not exactly. What you want is for the DEST parameter of "cp" to always be a file, never to be considered a directory, i.e. "temp/dest/self" should always be interpreted as file "self" in directory "temp/dest", never as directory "temp/dest/self". Luckily, there is already an option for that: -T, --no-target-directory treat DEST as a normal file With "-T", repeated commands work as you expected: $ mkdir -p temp/src temp/dest $ ln -s . temp/src/self $ cp -vdT temp/src/self temp/dest/self 'temp/src/self' -> 'temp/dest/self' $ cp -vdT temp/src/self temp/dest/self removed 'temp/dest/self' 'temp/src/self' -> 'temp/dest/self' $ cp -vdT temp/src/self temp/dest/self removed 'temp/dest/self' 'temp/src/self' -> 'temp/dest/self' $ cp -vdT temp/src/self temp/dest/self removed 'temp/dest/self' 'temp/src/self' -> 'temp/dest/self' [... ad infinitum ...] I hope this addresses the issue, I'm closing this as "not a bug", but discussion can continue by replying to this thread. regards, - assaf From debbugs-submit-bounces@debbugs.gnu.org Fri Jan 18 05:38:54 2019 Received: (at control) by debbugs.gnu.org; 18 Jan 2019 10:38:54 +0000 Received: from localhost ([127.0.0.1]:35723 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1gkRY6-0002oR-4J for submit@debbugs.gnu.org; Fri, 18 Jan 2019 05:38:54 -0500 Received: from mail-pl1-f170.google.com ([209.85.214.170]:38650) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1gkRY4-0002oB-F7 for control@debbugs.gnu.org; Fri, 18 Jan 2019 05:38:52 -0500 Received: by mail-pl1-f170.google.com with SMTP id e5so6192350plb.5 for ; Fri, 18 Jan 2019 02:38:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=to:from:message-id:date:user-agent:mime-version:content-language :content-transfer-encoding; bh=1e6c/v8Qpv35wrhJggY5Uu56lp9/c7tj3WXY8Hd3Vf0=; b=oxSsmgOTSl9KofOm+292IySB5SIzXvBXsA3MlaVSteXaozUn5d+AC2ikRQq1BPsyNh x221c6Q0euy95j1axl/1BQf9aqk+CqKOSBKvyw1pXVq/lD4FU1ztbqrKHh1b2UgtLUKS PPXamFdVes960xriWfiuZkVuFxmZBWmRjISxIvJYi9GOrtLLkgwxDt9UJi4ZKyCqpeVy tElUn9ZerO0+zral6hIuBG2GXdV8X4laVDfW3FUYtCbmECBdSgb/Dt280YxYVZsjgHS1 9UKWC742HF4XT/EY5FZQZFDO/dA4/ycjLgo/xDhBXNa/OOWNjtO0sgyG8ERooRJvuN/e U2WA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:to:from:message-id:date:user-agent:mime-version :content-language:content-transfer-encoding; bh=1e6c/v8Qpv35wrhJggY5Uu56lp9/c7tj3WXY8Hd3Vf0=; b=ivaj7tFtrBhVUywRc6tGLvTEtmUDLV4pt9bJPTjQKzsiu4fg+inmZRK+7u5Xh+64PY f1B1umtwjq1XZLxjF4iKctRxPwp+N22mR6YFJm90dD9Z268Qgxo1JGhO+37C6FuEzVJx FZoUE/zb7o1zo0IyOkWCtJAcGwF6WY2ibsZtBGnYsCRlUllPPCNBD9uRxsTng83TYQ9a qXZX4DGMNqydfu/bgc3M6wZ1UimsUCWCyiQyPMxqCJzEcl17EL+pn8b0zd/pix+ahien nOeNoItzhxoMt2LvepcdaTAGAuSDm7cui0l8gVChRDCc6PTnQBlyfRG0NM66EMLb8F2u tVoA== X-Gm-Message-State: AJcUukcLA83A4h1OkMqVWfza4rGZ5c+4Vll5ywphXe1/mPlWUMkjsLDX Q4gFzmGondOC+5SXk/0FakNoWXZjNWM= X-Google-Smtp-Source: ALg8bN5joEBwlAA32zKTTdsbQ7sMKaKllXtkNtgqjRDRQI4OwC2xknZXCzddXhCtCzgrURlaJSuD0g== X-Received: by 2002:a17:902:1102:: with SMTP id d2mr18760392pla.138.1547807925887; Fri, 18 Jan 2019 02:38:45 -0800 (PST) Received: from tomato.housegordon.com (moose.housegordon.com. [184.68.105.38]) by smtp.googlemail.com with ESMTPSA id q187sm6202393pfq.128.2019.01.18.02.38.44 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 18 Jan 2019 02:38:45 -0800 (PST) To: control@debbugs.gnu.org From: Assaf Gordon Message-ID: <75227e4f-af3a-2bef-9f8c-4d8c5eb21fca@gmail.com> Date: Fri, 18 Jan 2019 03:38:44 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.4.0 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit X-Spam-Score: 2.0 (++) X-Spam-Report: Spam detection software, running on the system "debbugs.gnu.org", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: tags 32455 notabug close 32455 Content analysis details: (2.0 points, 10.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider (assafgordon[at]gmail.com) -0.0 SPF_PASS SPF: sender matches SPF record -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at http://www.dnswl.org/, no trust [209.85.214.170 listed in list.dnswl.org] 1.8 MISSING_SUBJECT Missing Subject: header 0.2 NO_SUBJECT Extra score for no subject X-Debbugs-Envelope-To: control X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: 1.0 (+) tags 32455 notabug close 32455