如何将符号链接文件从Linux复制到Windows,然后再复制回Linux,但仍将其保留为符号链接 [英] How to copy symbolic link file from Linux to Windows and then back to Linux but still keeping it as a symbolic link

查看:807
本文介绍了如何将符号链接文件从Linux复制到Windows,然后再复制回Linux,但仍将其保留为符号链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Linux机器中有一个符号链接.

I have a symbolic link in my Linux machine.

我只想将符号链接(而非目标)复制到Windows机器,然后将此符号链接从Windows机器复制回其他Linux机器,并且符号链接应该继续工作.

I want to copy just the symbolic link (not the target) to a Windows machine and then copy this symbolic link from Windows machine back to some other Linux machine and symbolic link should continue to work.

我尝试过的:

  1. 我压缩了符号链接
  2. 使用WinSCP将压缩的文件移动到Windows计算机
  3. 提取的符号链接
  4. 使用WinSCP将符号文件移动到Linux机器

现在此文件不再是符号链接.

Now this file is not a symbolic link anymore.

有人知道这样做的窍门吗?

Do anybody knows a trick to do this?

推荐答案

在* nix中,符号链接通常只是具有"symlink"属性的纯文本文件.该文件包含链接目标的路径. Windows上不存在"symlink"属性.因此,当您在Windows上提取符号链接时,它会变成一个常规的文本文件[尽管它也可能会出错,但可能取决于您用来提取档案的工具].复制回* nix后,它将保留为常规文本文件.

In *nix a symlink is typically just a plain text file with a "symlink" attribute. The file contains the path to the link target. The "symlink" attribute does not exist on Windows. So when you extract the symlink on Windows, it becomes a regular text file [though it may also error, it may depend on a tool you use to extract the archive]. When copied back to *nix, it stays a regular text file.

唯一的解决方案是将"symlink"属性保留在某些外部元数据存储中,并在上载文件或创建档案时恢复该属性.

The only solution would be to keep the "symlink" attribute in some external metadata store and restore the attribute when uploading the file or creating the archive.

尽管我不知道有任何工具可以支持此功能.

Though I'm not aware of any tool that supports this.

您绝对可以对此进行编码.

You can definitely code this.

  1. 使用WinSCP :您编写了一个生成 WinSCP脚本的代码.该代码将递归地迭代本地目录结构.对于文件,它将生成 put 命令以将其上传.对于符号链接,它将生成 ln 命令以创建符号链接.为了区分符号链接,您可以仅使用一种简单的启发式方法(符号链接=带有斜线的简短的单行文本文件).正确的方法是在提取存档时记住文件symlink属性(但是您也必须自己编写提取代码,请参见下面的提示).

  1. Using WinSCP: You make a code that generates WinSCP script. The code would recursively iterate a local directory structure. For a file it will generate the put command to upload it. For a symlink it will generate the ln command to create a symlink. To distinguish the symlink, you can maybe use just a simple heuristics (symlink = a short one-line text file with slashes). A proper way would be to remember the file symlink attribute when extracting the archive (but you would have to code the extraction yourself too then, see also a hint below).

使用归档文件: 我最近为ZIP存档实现了此功能. (甚至在Windows上)您也可以使用PHP方法 ZipArchive::setExternalAttributes 将存档文件标记为符号链接.请注意,此功能仅自PHP 5.6起可用.

Using archive: I recently implemented this for a ZIP archive. (Even on Windows) You can use the PHP method ZipArchive::setExternalAttributes to flag an archived file as a symlink. Note that the function is available since PHP 5.6 only.

示例代码:

$symlink = true; // is symlink?
$dir = false; // is folder?
$mode = "755"; // permissions

$local_path = "C:\\zip\\folder\\mylink";
$zip_path = "folder/mylink";

$attr = 
    (1 << 14) | // this bit seems to be always set
    (1 << ($dir ? 30 : 31)) |
    ($symlink ? (1 << 29) : 0) |
    octdec($mode) << 16;

$zip->addFile($local_path, $zip_path);
$zip->setExternalAttributesName($zip_path, ZipArchive::OPSYS_UNIX, $attr);

如果您更熟悉Python,请参见如何使用Python的zipfile对ZIP文件中的文件设置权限(属性)模块?它仅处理权限,但是按照我的PHP示例,您可以使用symlink位轻松扩展它.

If you are more familiar with Python, see How do I set permissions (attributes) on a file in a ZIP file using Python's zipfile module? It deals with permissions only, but you can easily extend it with the symlink bit, as per my PHP example.

这篇关于如何将符号链接文件从Linux复制到Windows,然后再复制回Linux,但仍将其保留为符号链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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