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

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

问题描述

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

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

最坏的情况是:

/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或system()函数或这种功能,解决方案必须是低水平。我可以使用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).

这个伪代码应该覆盖所有的可能性(它可能涉及到很多重复使用 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天全站免登陆