与指向复制的文件符号链接复制目录(目录树中) [英] Copy directory with symbolic links pointing to the copied files (inside directory tree)

查看:219
本文介绍了与指向复制的文件符号链接复制目录(目录树中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要一个文件夹及其所有内容,包括子文件夹复制。我使用Ubuntu的温度。

I want to copy a folder and all of its contents including sub-folders. I am using C on Ubuntu.

复制常规文件和文件夹,是容易的,迄今所做的,但复制链接规格(​​符号现在)是,他们应该链接到复制的文件。现在我只在目录树中的链接感兴趣。

Copying regular files, and folders is easy and done so far, but the specifications for copying links (symbolic for now) is that they should link to the copied files. For now I am only interested in links inside the directory tree.

(虽然树我觉得应该更容易外链接 - 完整路径只是复制到新的链接加上发现他们是否属于树与否 - 这是很难虽然的 sarnold 给了我一个提示关于使用的rsync 来实现这一目标)

(Although links outside the tree I think should be easier - just copy the full path to the new link plus find out if they belong in the tree or not - that's hard although sarnold gave me a tip about using rsync to achieve that)

所以,我必须通过返回的readlink绝对路径:

So I have an absolute path returned by readlink:

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

其中在最坏的情况下将是:

Which in the worst case would be :

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

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

but I can't find a way to retrieve the relative path to my directory tree. If I could find it I could replace it with the copied directory's name:

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

我不能使用CP或系统()函数或诸如此类的功能,该方案必须是低电平。我可以使用C库加GNU,但是请无论如何,我很感兴趣张贴一个答案。

I can't use cp or the system() function or functions of that sort, the solution has to be low level. I can use c libraries plus GNU, but please post an answer anyway I am interested.

推荐答案

让我们假设你需要将目录 SRC_DIR 复制到 DST_DIR ,这两者都是绝对路径(如果不是,它是平凡的,你给他们使用的 GETCWD )。

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).

这个伪code的的包括所有的可能性(它可能涉及到很多重复使用的<​​a href的=htt​​p://pubs.opengroup.org/onlinepubs/009604599/functions /strtok.html相对=nofollow> strtok的 在/分隔符来标记路径):

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

这篇关于与指向复制的文件符号链接复制目录(目录树中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆