如何在Linux上将常规文件转换为符号链接 [英] How to turn a regular file into a symlink on Linux

查看:114
本文介绍了如何在Linux上将常规文件转换为符号链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个(非)归档工具,它的设计方式是,在检查特殊属性之前,它首先从档案库创建一个常规文件,并可能实际上决定该项目是一个符号链接.

I'm writing an (un)archiving tool and the way it is designed it first creates a regular file from the archive before it examines the special attributes and may decide that this item is a symlink, in fact.

注意:在更多的人误解我之前,他们想创建文件的符号链接.不,我将符号链接数据(即其路径)写入文件,然后我要告诉文件系统这是符号链接

我一直在OS X上进行开发,可以通过简单地相应地设置其类型和创建者代码,将常规文件转换为符号链接.

I've been developing this on OS X, where it's possible to turn a regular file into a symlink by simply setting its Type and Creator codes accordingly.

现在,我也希望在Linux上也能使用此代码.所以我喜欢在那里找到类似的方式.

Now I like to get this code working on Linux as well. So I like to find a similar way there.

我知道创建符号链接的通常方法是调用symlink()函数,但是我想知道是否还有一种方法可以将常规文件更改为符号链接,就像在OSX的BSD系统中一样,这样我就不必过多重构我的工作代码了?

I am aware that the normal way to create a symlink is to call the symlink() function, but I wonder if there is also a way to change a regular file into a symlink, just like it's possible in OSX's BSD system, so that I do not have to refactor my working code too much?

有lstat(),它以st_mode的最高位返回文件类型.现在,我想知道此模式字段是否还有类似的setter函数.

There is lstat(), which returns the file type in st_mode's upmost bits. Now I wonder if there's also an analogous setter function for this mode field.

推荐答案

我不相信Linux中有您所描述的方法. IIRC,文件系统将符号链接信息存储在inode表中,而不是常规文件中,因此没有直接的方法将文件转换为链接.

I don't believe there is a way in Linux to do this as you describe. IIRC, the filesystem stores symlink information in the inode table and not in a regular file so there's no direct way of turning a file into a link.

如果符号链接的路径存储在文件内,为什么不读出路径,删除文件并在其位置创建符号链接?

If the symlink's path is stored inside the file, why not read out the path, delete the file, and create a symlink in its place?

这篇关于如何在Linux上将常规文件转换为符号链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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