复制包含符号链接的继承目录

This is similar to questions that have been asked before, but I promise it is different.

I have a site that involves inheritance from a parent to a child site. Part of this is media. To make media serve faster, I am creating a symlink-ed copy of the parent directory in the child, then override those with any files the child specifically defines. This allows apache to serve statically from the media (which may come from a parent) while inheriting html form the parent.

Example after build where inheritance is common->maps->cars :

/some/directory/common/
   images/
      cats.png
      muffins.png
   css/
      styles.css

/some/directory/maps/
   images/
      cats.png -> /some/directory/common/images/cats.png
      muffins.png (overridden by child)
   css/
      styles.css -> /some/directory/common/css/styles.css
      mapsStyles.css (child only)

/some/directory/cars/
   images/
      cats.png -> /some/directory/common/images/cats.png
      muffins.png -> /some/directory/maps/images/muffins.png
   css/
      styles.css -> /some/directory/common/css/styles.css
      carsStyles.css (child only)

So I could symlink to /images/muffins.png and receive the correct media based on the site that I'm on. The site-specific media is stored in the same directory structure, but in a different location only containing media specific to that site.

The issue is that no matter how I try to do it, the second child ends up with a symlink to its parent, not the originator. In the example above, I cannot get /some/direcory/cars/images/cats.png to point to the common media programatically, only the maps media.

Currently I am using the command cp -sR /some/directory/maps/* /some/directory/cars/

I'm running this on PHP, but currently using cp and ln commands in the PHP exec() function to build these structures.

It seems you should use tar and not cp : please try (I can't test it right now; watch out what your versino of tar does by default with symlinks. it should save them as symlinks, not follow them... but ymmv) :

#step 1: create an archive of one of the directories_using_common_symlinks:
cd /some/directory/maps/
tar cvf /somewhere/wholeshebang.tar images/ css/
        #ie, tar everything (directories, symlinks, etc).
        #Please add whatever is missing
        #note: it will also contain extra (specific) stuff, which we get rid of below

#step 2: create the real archive of JUST the common stuff
cd /some/temporary_dir/
tar xvf /somewhere/wholeshebang.tar  #regurgitate all the content.
rm      /somewhere/wholeshebang.tar  #we won't need that one anymore, it contains too much
rm images/muffins.png  css/mapStyles.css #delete all extra stuff
tar cvf /somewhere/just_commons.tar  #recreate tar, this time with only common stuff.

#step 3: use the new archive to "kickstart" the other directories
cd /the/destination
tar xvf /somewhere/just_commons.tar  #regurgitte the common stuff (dirs, symlinks)
#then add whatever is specific to /the/destination

If the issue is just the links ..... you could try "cp -l" .... ( check man cp for more info )

man page :

-l, --link
     hard link files instead of copying

hence that should resolve this !