使用Inno Setup创建硬链接 [英] Create a hardlink with Inno Setup

查看:143
本文介绍了使用Inno Setup创建硬链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有成千上万的安装程序,需要一个关键的dll文件进行卸载,该dll文件大小约为2 mb,然后避免了不必要的磁盘空间(2mb * 100个安装程序),我想在{cf}中将文件存储一次然后为需要该文件的下一个安装程序建立一个硬链接.

I have thousand of own installers that requires a critical dll file for uninstallation step, this dll file sizes about 2 mb then to avoid unnecessary disk space (2mb*100 installers) I would like to store the file once in {cf} then make a hardlink for the next installers that requires that file.

我可以在Inno Setup中创建硬链接,而无需使用诸如 mklink.exe 之类的外部应用程序?

I could create a hardlink in Inno Setup without the need of external apps such as mklink.exe usage?

这是我所拥有的简短示例,我的所有安装程序都遵循相同的结构":

This is a brief example of what I have, all my installers follow the same "structure":

[Files]
; VCL Styles
Source: {tmp}\uninstall.vsf; DestDir: {app}; Flags: ignoreversion
Source: {tmp}\uninstall.dll; DestDir: {app}; Flags: ignoreversion uninsneveruninstall

; Temp files
Source: {tmp}\*; DestDir: {tmp}; Excludes: uninstall.dll, uninstall.vsf; Flags: recursesubdirs createallsubdirs ignoreversion

; Program
Source: {app}\*; DestDir: {app}; Flags: recursesubdirs createallsubdirs ignoreversion

如您所见,我将 uninstall.dll 移到了{app},但是我想做的是:如果不存在,请复制卸载. dll 文件到{cf}\InnoSetup\uninstall.dll文件路径,并建立到{app}\uninstall.dll的硬链接,如果该文件已经存在,则只需进行硬链接,仅此而已,我仍然不会存储 uninstall.dll > {app}\uninstall.dll中的文件,只是我想要一个符号引用,因为 uninstall.dll 文件永远都不能卸载.

As you could see, I'm moving the uninstall.dll to {app}, but what I would like to do is: If doesn't exists, copy the uninstall.dll file to {cf}\InnoSetup\uninstall.dll filepath and make a hardlink to {app}\uninstall.dll, if already exists the file then just make the hardlink, nothing more, I won't still store the uninstall.dll file in {app}\uninstall.dll, just I want a symbolic reference because the uninstall.dll file should never be uninstalled.

我该怎么做?

推荐答案

Inno Setup不支持本地创建硬链接.

Inno Setup does not support creating hardlinks natively.

我不会考虑 mklink 外部应用程序.这是内置的Windows工具.因此,如果您不需要支持Windows XP,则可以放心地依靠它.或者,如果mklink不可用,则可以回退到定期安装DLL.

I wouldn't consider the mklink an external application. It's a built-in Windows tool. So if you do not need to support Windows XP, you can safely rely on it. Or you can fallback to installing the DLL regularly, if the mklink is not available.

或使用 CreateHardLink函数Code部分.

#define MyApp "MyApp"
#define UninstallDll "uninstall.dll"

[Files]
Source: "{#UninstallDll}"; DestDir: "{cf}\{#MyApp}"; \
  Flags: ignoreversion uninsneveruninstall

[Code]
function CreateHardLink(lpFileName, lpExistingFileName: string;
  lpSecurityAttributes: Integer): Boolean;
  external 'CreateHardLinkW@kernel32.dll stdcall';

procedure CurStepChanged(CurStep: TSetupStep);
var
  ExistingFile, NewFile: string;
begin
  if CurStep = ssPostInstall then
  begin
    ExistingFile := ExpandConstant('{cf}\{#MyApp}\{#UninstallDll}');
    NewFile := ExpandConstant('{app}\{#UninstallDll}');
    if CreateHardLink(NewFile, ExistingFile, 0) then
    begin
      Log('Hardlink created');
    end
      else
    if FileCopy(ExistingFile, NewFile, False) then
    begin
      { FAT file system? }
      Log('Hardlink could not be created, file copied instead');
    end
      else
    begin
      MsgBox('Cannot install {#UninstallDll}', mbError, MB_OK);
    end;
  end;
end;

(已在 Inno Setup的Unicode版本中进行了测试–是Inno Setup 6的唯一版本)

(Tested on Unicode version of Inno Setup – The only version as of Inno Setup 6)

在卸载时,别忘了删除文件:

And do not forget to delete the file when uninstalling:

procedure CurUninstallStepChanged(CurUninstallStep: TUninstallStep);
begin
  if CurUninstallStep = usUninstall then
  begin
    if DeleteFile(ExpandConstant('{app}\{#UninstallDll}')) then
    begin
      Log('File deleted');
    end
      else
    begin
      Log('Cannot delete file');
    end;
  end;
end;

您当然也可以使用[UninstallDelete]条目.我只想使用与安装文件相同的技术来卸载文件.

You can of course use also the [UninstallDelete] entry. I just like to uninstall the file using the same technology used to install it.

您的问题标题是使用Inno Setup创建硬链接" .

Your question title is "Create a hardlink with Inno Setup".

CreateHardLink创建一个硬链接.硬链接是对相同内容的另一种引用.基本上,硬链接与原始文件是没有区别的(即使原始文件实际上也是一个 hardlink ).原始文件和硬链接都只是对相同内容的引用.如果删除原始文件(或新的硬链接),则实际上只删除对内容的一个引用.内容仍然保留.内容仅在最后一个引用的情况下被删除.硬链接不会占用磁盘上的额外空间(内容仅存储一次).

The CreateHardLink creates a hardlink. A hardlink is another reference to the same contents. Basically the hardlink is indistinguishable from the original file (even the original file is a hardlink actually). Both original file and the hardlink are just references to the same contents. If you delete the original file (or the new hardlink), you actually remove just one reference to the contents. The contents is still preserved. The contents is removed with the last reference only. The hardlink does not occupy an additional space on the disk (the contents is stored only once).

有关详细信息,请参见有关Wikipedia的硬链接文章.

For details see Hard link article on Wikipedia.

mklink默认情况下会创建一个符号链接(也称为符号链接).符号链接就像快捷方式一样,它是对原始文件(而不是内容)的引用.它是一个单独的文件,包含目标文件的路径.符号链接具有自己的大小(由对目标文件的引用占用).如果删除原始文件,则符号链接仍然存在(因为没有引用原始文件中的符号链接),但符号链接无效(内容消失了).同样,它类似于快捷方式.

While the mklink creates a symlink (aka symbolic link) by default. A symlink is like a shortcut, it's a reference to the the original file (not contents). It's a file on its own, that contains a path to the target file. The symlink has a size of its own (occupied by the reference to the target file). If you remove the original file, the symlink still exists (because there's no reference to the symlink from the original file), but becomes invalid (the contents is gone). Again, it's similar to a shortcut.

有关详细信息,请参见 Wikipedia上的符号链接文章.

For details see Symbolic link article on Wikipedia.

如果添加/H开关,则可以使用mklink创建硬链接:

You can create a hardlink with the mklink, if you add the /H switch:

/H创建硬链接而不是符号链接.

/H Creates a hard link instead of a symbolic link.

如果要创建符号链接而不是硬链接,则是另一个问题(尽管答案很简单,请使用

If you want to create the symlink instead of the hardlink, it's a different question (though the answer is simple, use the CreateSymbolicLink function). Though again, note that the hardlink does not occupy additional space on the disk, what seems to be your concern. So I believe you should keep using the CreateHardLink function.

这篇关于使用Inno Setup创建硬链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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