如何在C Linux中将绝对路径转换为相对路径

如何在C Linux中将绝对路径转换为相对路径

问题描述:

我想知道如何在Linux(Ubuntu)OS上使用c语言基于给定目录(包括链接文件)将符号链接的绝对路径转换为相对路径.

I would like to know how an absolute path of a symbolic link can be converted to relative based on a given directory (that includes the linked file) in c language on linux (Ubuntu) OS.

我想搜索相对路径的子字符串,但是如果它已经存在于文件夹的层次结构中怎么办?

I thought searching for the sub-string of the relative path, but what if it already exists higher in the folder's hierarchy?

以下是我要执行的操作的更具体说明: 相对路径:

Here is a more specific description of what I want to do: Relative path:

 folder/folder1/folder2

绝对路径:

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

更改为

/home/giorgos/Desktop/myfolder/folder1/folder2/a.pdf

显然,我不能简单地搜索并替换"folder/",请考虑这种情况:

Obviously I cant' simply search for and replace "folder/", consider this case:

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

它可以向前和向后搜索,如果被替换,它仍然给出错误的输出 只有当我知道相对路径时,我才能向后搜索绝对值并将其替换,然后输出才是正确的:

It can be searched both forwards and backwards and if replaced it still gives a wrong output Only if I knew the relative path I could search the absolute backwards and replace it, then the output would be correct:

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

如果我正在编写此工具,我会很想做出这个决定,即当将绝对符号链接移至其他位置时,绝对符号链接应具有相同的值.文件系统-用户想要一个非常特定的文件.而且,相对符号链接在移动到文件系统中的其他位置时应具有相同的值-用户希望这些链接能够工作,而不管目录树的植根位置为何.

I'd be seriously tempted to make the decision, if I were writing this tool, that absolute symbolic links should have the same value when moved somewhere else in the filesystem -- the user wanted a very specific file. And, relative symbolic links should have the same value when moved somewhere else in the filesystem -- the user wanted the links to work regardless of where the directory tree was rooted.

但是,如果将两种类型的链接混合在一起,那么您将需要做更多的工作-这就是我现在所认为的. (Unix程序通常不会因为猜测用户的意图而宽容;如果您只是readlink(2)symlink(2)正是文件系统所说的,那么您的程序将永远不会感到惊讶.)

But if the two types of links were intermixed, then you'd have some more work to do -- which is where I assume you are now. (Unix programs are often not that forgiving about guessing a user's intent; if you just readlink(2) and symlink(2) exactly what the filesystem says, your program will never be surprising.)

rsync(1)可能具有一些可以使用的源代码-或至少可以学习. --safe-links命令行选项使rsync 忽略绝对符号链接和相对符号链接,这些链接指向被指示要复制的树的外部.这并不是您所希望的规范相对路径,但是它可能提供足够的代码来发现哪些链接指向所讨论的目录树之外.

rsync(1) might have some source code you can use -- or at least learn from. The --safe-links command line option causes rsync to ignore absolute symbolic links and relative symbolic links that point outside the trees it was instructed to copy. This isn't canonicalizing paths to relative as you wish but it may provide sufficient code for discovering which links point outside the directory tree in question.

有点相关; Linux特定的symlinkat(2)系统调用可以使您更轻松地创建符号链接. (...at()系统调用家族类似于为进程提供多个当前工作目录",而不必强迫您自己进行所有fchdir(2)调用.)

Slightly related; the Linux-specific symlinkat(2) system call may make it easier for you to create your symbolic links. (The family of ...at() system calls are something like providing a process with multiple "current working directories" without forcing you to make all the fchdir(2) calls yourself.)