Copy directory with symbolic links pointing to the copied files (inside directory tree)

穿精又带淫゛_ 提交于 2019-12-06 05:16:47

Let us assume that you need to copy the directory SRC_DIR to DST_DIR, both of which are absolute paths (if not, it is trivial for you to convert them using getcwd).

This pseudocode should cover all possibilities (it probably involves a lot of repeated use of strtok to tokenize paths at the '/' separators):

if (SRC_DIR/SUBDIRS/LINK is a symlink that points to TARGET_LOCATION)
  {
    // TARGET_LOCATION may be relative *or* absolute. Make it absolute:
    if (TARGET_LOCATION does not begin with '/')
      {
        prepend SRC_DIR/ to it
      }
    if (TARGET_LOCATION contains a subdirectory named '..')
      {
        replace occurances of 'SOMEDIRNAME/..' with ''
      }
    if (TARGET_LOCATION contains a subdirectory named '.')
      {
        replace occurances of '/.' with ''
      }
    // now TARGET_LOCATION is an absolute path

    if (TARGET_LOCATION does not begin with SRC_DIR)
      {
        // symlink points outside tree
        create symlink DST_DIR/SUBDIRS/LINK pointing to TARGET_LOCATION
      }
    else
      {
        // symlink points inside tree
        strip SRC_DIR from beginning of TARGET_LOCATION
        prepend DST_DIR to TARGET_LOCATION
        create symlink DST_DIR/SUBDIRS/LINK pointing to TARGET_LOCATION
      }
  }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!