in reply to Re^3: Getting all subpaths from a path
in thread Getting all subpaths from a path

You cannot create the links before both the source and target directories exist. So, postpone their creation to the end. The directories are processed from the shortest to the longest, so we always know the parent path exists. You should also add a check that a link doesn't point outside of the given directory tree.

I created the following Makefile to experiment with your data:

SHELL := /bin/bash .PHONY: test test: clean prepare echo $$'#!/bin/bash\ntarget=top2\nmkdir -p $$target/top' > 1.sh ./1.pl top >> 1.sh chmod u+x 1.sh ./1.sh diff -r top top2/top .PHONY: prepare prepare: mkdir -p top/usr mkdir -p top/root/site/tools/gauv/python3/3.6.3/{lib,bin} touch top/root/site/tools/gauv/python3/3.6.3/bin/python3.6 ln -s $(PWD)/top/root/site/tools/gauv top/usr/vsa ln -s 3.6.3 top/root/site/tools/gauv/python3/3.6.3a .PHONY: clean clean: rm -rf top top2 1.sh

And this was the script 1.pl:

#!/usr/bin/perl use warnings; use strict; use feature qw{ say }; use Path::Tiny qw{ path }; my $path = path(shift); my $paths = $path->visit( sub { $_[1]->{$_} = (-f) ? 'file' : (-l) ? [readlink] : (-d) ? 'dir' : 'unknown' }, { recurse => 1 } ); my @links; for my $found (sort { length $a <=> length $b } keys %$paths) { if ('file' eq $paths->{$found}) { say qq(cp '$found' "\$target/$found"); } elsif ('dir' eq $paths->{$found}) { say qq(mkdir "\$target/$found"); } elsif (ref [] eq ref $paths->{$found}) { my $to = path($paths->{$found}[0]); $to = $to->relative(path($found)->absolute->parent) if $to->is_absolute; my $source = path('$target', $found); push @links, qq(ln -s "$to" "$source"); } } say for @links;

It's just a toy. Use some of the ShellQuote modules to fix the filenames; but for the example given, it works.

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Replies are listed 'Best First'.
Re^5: Getting all subpaths from a path
by ovedpo15 (Pilgrim) on Mar 31, 2021 at 18:48 UTC
    Hi choroba! Thank you for your input as always. I have a few questions:
    1. As I understand the first part does opendir (somewhere) on the path my $path = path("/usr/vsa/pkgs/python3/3.6.3a/bin/python3.6"); and insert all of the files inside the directory into $paths but I'm interested only in the path itself. The input of the script is an array of paths. Each path (and only those paths) interest me. So if for example I have /usr/vsa/pkgs/python3/3.6.3a/bin/python3.6a (note the "a" at the end) and it does not exist in the array of paths, I don't want to copy it (only /usr/vsa/pkgs/python3/3.6.3a/bin/python3.6. See more info at (*)).
    2. It does opendir for some reason so it fails for files (the path /usr/vsa/pkgs/python3/3.6.3a/bin/python3.6 is a file and it fails with: Error opendir on '/usr/vsa/pkgs/python3/3.6.3a/bin/python3.6': Not a directory at line 113). Line 113 is "{ recurse => 1 }"
    3. It looks like path can't handle with some symlinks. I have a link /a -> /c/d/e and path("/a") returns nothing, while path("/a/b") returns paths under "/c/d/e/b".
    4. Also what if there is a link /a/ -> /b/ -> /c? Should it work?

    The cp/mkdir/ln part I will do myself. I'm just struggling to build the arrays of paths that are involved (and only them) - splitted into categories (file-links, dir-links, files, dirs).

    * A bit more explanation about the "involved paths": I have an array @array=("/usr/vsa/pkgs/python3/3.6.3a/bin/python3.6", ...). I want to iterate over this array and mkdir the directories that exist in the array (recursively), copy the files (not the directories but I want to have only the paths that are located in @array) and set the same links. Think of that as a container which you want to run your tool on and you know those paths are needed for running your tool (but any other path is not needed). So if your tool uses /a/b/c.file and under /a/b you also have /a/b/d.file, then it will create /a/b directory and copy /a/b/c to $target/a/b/c (and set links if needed). If I wanted to copy all of the files under directory, I would just copy the directory (instead of mkdir and cp files under directory).