On Thu, Oct 18, 2007 at 10:06:42PM +1300, Richard Toohey wrote:
> This looks like fun ... 8-)  And this is open source, so let's follow
> the code and learn something as we go along ...
> 
> But first, I guess it IS following your instructions ...
> 
> You asked it to copy what's in directory foo, recursively.  And you
> are changing what's in foo at the same time ...
> 
> 1. What's in foo? foo
> 2. So copy foo to foo - giving foo/foo.
> 3. What's in foo?  foo/foo
> 4. So copy foo/foo to foo/foo/foo.
> 5. Repeat.
> 
> Until it goes boom.
> 
:
:
> 
> JUST FOR FUN I have tried to "fix" this.  What I know about C code
> can be written on the back of a postage stamp - this was an attempt
> to get something working.  And a lot more fun than watching TV ...
> And because I can.
> 
> # diff -u /tmp/cp.c cp.c
> --- /tmp/cp.c   Thu Oct 18 21:50:07 2007
> +++ cp.c        Thu Oct 18 22:48:37 2007
> @@ -237,6 +237,10 @@
>                  */
>                 type = FILE_TO_DIR;
> +       if (type == FILE_TO_DIR)
> +               if (strcmp(to.p_path,*argv)==0)
> +                       errx(1,"source and destination directories (%
> s) would cause cycle",to.p_path);
> +
>         exit (copy(argv, type, fts_options));
> }
> 
> After the change:
> 
> # cp -R foo foo
> cp: source and destination directories (foo) would cause cycle
> 
> Done no other testing or anything useful.
> 
> But now someone will rap me on the fingers and say this is POSIX
> compliant or some-such.  I'll try it on FreeBSD 6.2:
> 

I am afraid solving the general case is harder.
You would have to do a full tree search down into the directories
to copy and see if you find the destination anywhere. During the
tree scan you would have to use all rules about following
links that the actual copy recursion would use.

And that would not be enough... I made a little test and if the
target contains directories that have the same name(s) as in
the source, they are retained (their inode number does not change).

This should mean that the target may contain an arbitrarily deep
directory structure that on any point can hard link to somewhere
in the source directory structure, causing a cycle that is very
expensive to find, even if you do the cycle detection
during the copy recursion.

The path length limit actually works as a crude, cheep and effective
cycle detection.

While Windows Finder solves the simple case of copying into
yourself, I do not think it has solved the general case.
Anyone curious to try?

-- 

/ Raimo Niskanen, Erlang/OTP, Ericsson AB

Reply via email to