繁体   English   中英

复制目录,并带有指向复制文件的符号链接(在目录树内部)

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

我想复制一个文件夹及其所有内容,包括子文件夹。 我在Ubuntu上使用C。

到目前为止,复制常规文件和文件夹很容易并且很容易完成,但是复制链接的规范(现在是象征性的)是它们应该链接到复制的文件。 现在,我只对目录树中的链接感兴趣。

(尽管我认为树外的链接应该更容易-只需将完整路径复制到新链接,然后找出它们是否属于树中-很难,尽管sarnold给了我一些有关使用rsync实现该技巧的提示)

所以我有一个由readlink返回的绝对路径:

/home/giorgos/Desktop/folder/folder1/a.pdf

最坏的情况是:

/home/giorgos/Desktop/folder/folder/folder/folder1/a.pdf

但是我找不到一种方法来检索目录树的相对路径。 如果可以找到它,则可以用复制的目录名称替换它:

 /home/giorgos/Desktop/March/folder/myfolder/folder/folder1/a.pdf

我不能使用cp或system()函数或那种函数,解决方案必须是低级的。 我可以使用c库和GNU,但无论如何请发表答案。

让我们假设您需要将目录SRC_DIR复制到DST_DIR ,这两个都是绝对路径(如果不是绝对路径,那么使用getcwd转换它们就很简单了)。

该伪代码涵盖所有可能的情况(它可能涉及大量重复使用strtok在'/'分隔符处标记路径):

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
      }
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM