Re: How to determine if a directory is a subdirectory of another directory?



Arjen Markus <arjen.markus@xxxxxxxxxx> wrote:
Directory contents:
work/
example.inp
Script:
foreach f [glob *] {
file copy $f work
}
which led to an almost endless loop (the path became too long, that is
why it stopped)

Comparing the normalized source and target (each with a slash appended!)
for substring-equality should suffice, unless you're on a platform that
supports symlinks, where you'd first have to resolve all symlinks along
the path, which is a non-trivial task.

Linux's "cp" detects such situations:
cp: cannot copy a directory, `work', into itself, `work/work'
(but still creates an empty directory `work/work')
Solaris's "cp" runs into the same trouble as tcl.
So, if you're on linux, you could [exec cp -r ...]

Perhaps a TIP to get this protection into tcl's file command might
be adequate (as I'd expect symlinks to be another possible cause of
trouble).

I'm not sure, if the problem is principially solveable at all.
Perhaps by keeping an array of the created inodes and refuse to
re-copy any of these?

.



Relevant Pages