Jump to content

NTFS links

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by Beland (talk | contribs) at 18:35, 27 April 2022 (convert special characters found by Wikipedia:Typo Team/moss (via WP:JWB)). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

The NTFS file system defines various ways to redirect files and folders, e.g., to make a file point to another file or its contents. The object being pointed to is called the target. There are three classes of links:

  • Hard links, which have files share the same MFT entry (inode), in the same filesystem.
  • Junction points, which are similar to hard links, but defined for folders. Only accepts local, absolute paths.[1][2]
  • Symbolic links, which record the path of another file that the links contents should show. Can accept relative paths. Support for directory and UNC paths were added in NTFS 3.1.

All NTFS links are designed to be transparent to applications. This means that the application accessing a link will be seamlessly redirected by the file system driver, and no special handling is needed. To users, they appear as normal directories or files. This also leads to an aliasing effect: writes to a link will pass the write to the underlying, linked file or MFT entry.

Symbolic links and junction points, which need to carry additional data including the path they point to, are based on NTFS reparse points. Their path-recording nature means that they can link to files on other volumes or even remote files. On the other hand, hard links are created simply by giving an entry in the MFT a new filename to take on, so it is restricted to files in the same filesystem.

An NTFS symbolic link is not the same as a Windows shortcut file, which is a regular file. The latter may be created on any filesystem (such as the earlier FAT32), may contain metadata (such as an icon to display when the shortcut is viewed in Windows Explorer), and is not transparent to applications.

History

Symbolic links to directories or volumes, called junction points and mount points, were introduced with NTFS 3.0 that shipped with Windows 2000. From NTFS 3.1 onwards, symbolic links can be created for any kind of file system object. NTFS 3.1 was introduced together with Windows XP, but the functionality was not made available (through ntfs.sys) to user mode applications. Third-party filter drivers – such as Masatoshi Kimura's opensource senable driver – could however be installed to make the feature available in user mode as well. The ntfs.sys released with Windows Vista made the functionality available to user mode applications by default.

Since NTFS 3.1, a symbolic link can also point to a file or remote SMB network path. While NTFS junction points support only absolute paths on local drives, the NTFS symbolic links allow linking using relative paths. Additionally, the NTFS symbolic link implementation provides full support for cross-filesystem links. However, the functionality enabling cross-host symbolic links requires that the remote system also support them, which effectively limits their support to Windows Vista and later Windows operating systems.

Restrictions and drawbacks

The default security settings in Windows Vista/Windows 7 disallow non-elevated administrators and all non-administrators from creating symbolic links but not junctions. This behavior can be changed running "secpol.msc" the Local Security Policy management console (under: Security Settings\Local Policies\User Rights Assignment\Create symbolic links). It can be worked around by starting cmd.exe with Run as administrator option or the runas command. Starting with Windows 10 Insiders build 14972 the requirement for elevated administrator privileges was removed in Windows "Developer Mode", allowing symlinks to be created without needing to elevate the console as administrator. On the API level, a SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE flag is supplied for this purpose.[3]

Non-junctionable files

Neither the Windows NT startup process nor the Windows Vista startup process support Junction points, so it is impossible to redirect certain system folders:

  • \Windows
  • \Windows\System32
  • \Windows\Config

However it is possible to redirect non-critical folders:

  • \Users
  • \Documents and Settings
  • \Program Files
  • \Program Files (x86)

Creating junctions for \Users and \ProgramData pointing to another drive is not recommended as it breaks updates and Windows Store Apps.[4]

Creating junctions for \Users, \ProgramData, "\Program Files" or "\Program Files (x86)" pointing to other locations breaks installation or upgrade of Windows.[5]

Creating junctions for "\Program Files" or "\Program Files (x86)" pointing to another drive breaks Windows' Component Based Servicing which hardlinks files from its repository \Windows\SxS to their installation directory.[citation needed]

Examples of use

Built-in uses

  • Windows Component Store (WinSxS) use hard links to keep track of different versions of DLLs stored on the hard disk drive.
  • Symlinks are in Windows Server 2008 for a \Users\All Users\ → \ProgramData\ redirection (in basic installation).

Program redirection

By setting a junction point that points to a directory containing a particular version of a piece of software, it may be possible to add another version of the software and redirect the junction point to point to the version desired.

Saving disk space

The contents of a junction use almost no disk space (they simply point to the original directory). If you need to have multiple points of entry to a large directory, junction points will serve that purpose well. Junction points should not be confused with a copy of something as junctions simply point to the original. If directories need to be modified separately a junction cannot be used as it does not provide a distinct copy of the directory or files within.

Likewise, symbolic links and hard links are useful for merging the contents of individual files.

Circumventing predefined paths

Since reinstalling Windows (or installing a new version) often requires deleting the contents of the C: drive, it is advantageous to create multiple partitions so only one partition needs to be deleted during the installation. However, some programs don't let the user choose the installation directory, or install some of their files to the C: drive even when they are installed to a different drive. By creating a junction point, the program can be tricked into installing to a different directory.

Deferred target invalidation hazards

Significant hazards lurk in the use of hard links schemes that either:

  • include links that refer to their own parent folders, such as creating hard link X:\path\to\parent which points to either X:\path\ or X:\path\to\, or
  • specify targets by using volume drive letters, such as X:, in X:\some\path\.

The problem in the first case is that it creates recursive paths, which further implies infinite recursion in the directory structure. By introducing reentrancy, the presence of one or more directory junctions changes the structure of the file system from a simple proper tree into a directed graph, but recursive linking further complicates the graph-theoretical character from acyclic to cyclic. Since the same files and directories can now be encountered through multiple paths, applications which traverse reentrant or recursive structures naively may give incorrect or incoherent results, or may never terminate. Worse, if recursively deleting, such programs may attempt to delete a parent of the directory it is currently traversing.

Note that both of the conditions listed above exist in the system of hard links established on the C: drive in the default Windows setup. For example, every Windows 10 installation defines the recursive path:

C:\ProgramData\
C:\ProgramData\Application Data\
C:\ProgramData\Application Data\Application Data\
C:\ProgramData\Application Data\Application Data\Application Data\
C:\ProgramData\Application Data\Application Data\Application Data\Application Data\
C:\ProgramData\Application Data\Application Data\Application Data\Application Data\Application Data\ ...

Each additional path name in this seemingly infinite set is an actual valid Windows path which refers to the same location. In practice, path names are limited by the 260-character DOS path limit (or newer 32,767 character limit), but truncation may result in incomplete or invalid path and file names. Whenever a copy of a Windows installation is archived, with directory junctions intact, to another volume on the same—or worse—another computer, the archived copy may still incorporate active folders from the running installation. For example, depending on the method used for copying, a backup copy of a Windows drive X:\archive\... will include a hard link called X:\archive\Users\USERNAME\My Documents which still points to folder C:\Users\USERNAME\Documents\ in the current, active installation.

The second form of deferred target mis-referral, while conceptually simpler, can have more severe consequences. When a self-consistent volume or directory structure containing hard links which use volume drive-letter path names is copied or moved to another volume (or when the drive letter of a volume is reassigned by some other means), such links may no longer point to the corresponding target in the copied structure. Again the results depend on the software that was used for copying; while some programs may intercede by modifying any fully subsumed hard links in the copy in order to preserve structural consistency, others may ignore, copy exactly, or even traverse into hard links, copying their contents.

The serious problems occur if hard links are copied exactly such that they become, in the new copy, cross-volume hard links which still point to original files and folders on the source volume. Unintentional cross-volume hard links, such as hard links in an "archive" folder which still point to locations on the original volume (according to drive letter), are catastrophes waiting to happen. For example, deleting what is much later presumed to be an unused archive directory on an disused backup volume may result in deleting current, active user data or system files.

A preventative measure for the drive-letter hazard is to use volume GUID path syntax,[6] rather than paths containing volume drive letters, when specifying the target path for a directory junction. For example, consider creating an alias for X:\Some\Other\Path at X:\Some\Path\Foo:

X:\Some\Path>linkd Foo X:\Some\Other\Path

As described above, if the folder structure that contains the resulting link is moved to a disk with a drive letter other than X:, or if the letter is changed on drive X: itself, the data content at the target location is vulnerable to accidental corruption or malicious abuse. A more resilient version of this link can partially mitigate this risk by referencing the target volume by its GUID identifier value (which can be discovered by running the fsutil volume list command).

X:\Some\Path>linkd Foo \\?\Volume{12345678-abcd-1234--abcdefghijkl}\Some\Other\Path

Doing so ensures that the junction will remain valid if drive letter X: is changed by any means.

As for a proactive means of avoiding directory junction disasters, the command dir /AL /S /B "X:\Some\Path" can be used to obtain, for careful analysis prior to committing any irreversible file system alterations, a list of all hard links "below" a certain file system location. While by definition every link in the resulting list has a path name that starts with X:\Some\Path\, if any of those hard links contains a target which is not subsumed by X:\Some\Path, then the specified scope has been escaped, and the starting directory you specified is not fully-subsuming. Extra caution may be indicated in this case, since the specified directory includes files and directories which reside on other physical volumes, or whose own parent-traversal-to-root does not include the specified directory.

Command-line tools

Windows comes with several tools capable of creating and manipulating NTFS links.

  • PowerShell: The New-Item cmdlet of Windows PowerShell that can create empty files, folders, junctions, and hard links.[7] In PowerShell 5.0 and later, it can create symbolic links as well.[8] The Get-Item and Get-ChildItem cmdlets can be used to interrogate file system objects, and if they are NTFS links, find information about them. The Remove-Item cmdlet can remove said items, although there has been a record of a bug preventing this cmdlet from working properly.[9]
  • Windows Command Prompt: Starting with Windows Vista and Windows Server 2008, the mklink internal command can create junctions, hard links, and symbolic links.[10] This command is also available in ReactOS.[11] In addition, the venerable dir command can display and filter junction points via the /aL switch.[12] Finally, the rd command (also known as rmdir) can delete junction points.
  • fsutil.exe: A command-line utility introduced with Windows 2000. Its hardlink sub-command can make hard links or list hard links associated with a file.[13] Another sub-command, reparsepoint, can query or delete reparse points, the file system objects that make up junction points, hard links, and symbolic links.[14]

In addition, the following utilities can create NTFS links, even though they don't come with Windows.

  • linkd: It is a component of the Resource Kit for Windows 2000 and Windows Server 2003.[15] It can make junction points.[16]
  • junction: A free command-line utility from Microsoft, it can create or delete junctions.[2]
  • PowerShell Community Extensions (PSCX): Hosted on Microsoft PowerShell Gallery,[17] this module adds several cmdlets for dealing with NTFS links, including: New-Hardlink, New-Junction, Get-ReparsePoint, Remove-ReparsePoint, and New-Symlink.[18]

APIs

To create hard links, apps may use the CreateHardLink() function of Windows API. All versions of the Windows NT family can use GetFileInformationByHandle() to determine the number of hard links associated with a file. There can be up to 1024 links associated with an MFT entry. Similarly, the CreateSymbolicLink() function can create symbolic links. Junctions are more complex to create. They require manual reparse point information filling.[19] A code example is found in libuv.[20] Junctions are defined for directories only: although the API does not fail when one creates a junction pointing to a file, the junction will not be interpreted successfully when used later.

Junctions and symbolic links, even those pointing to directories, can be removed with pNtSetInformationFile. Libuv's implementation of unlink on Windows demonstrates this use.[21] Alternatively, the .NET System.IO.Directory.Delete() method works on them as well.[22]

Other properties

Symbolic links can point to non-existent targets because the operating system does not check to see if the target exists.[23]

Limitations

Symbolic links do not work at boot, so it's impossible to redirect e.g.:

  • hiberfil.sys
  • \Windows
  • \Windows\system32
  • \Windows\system32\Config

Windows Installer does not fully support symbolic links. A redirected \Windows\Installer will cause most .msi-based Windows installers to fail with error 2755 and/or error 1632.

Nevertheless, it is possible to redirect:

  • \Documents and Settings
  • \ProgramData
  • \Program Files
  • \Program Files (x86)
  • \Users

Creating symbolic links for \Users and \ProgramData pointing to another drive is not recommended as it breaks updates and Windows Store Apps.[4]

Creating symbolic links for \Users, \ProgramData, "\Program Files" or "\Program Files (x86)" pointing to other locations breaks installation resp. upgrade of Windows.[5]

Creating symbolic links for "\Program Files" or "\Program Files (x86)" pointing to another drive breaks Windows' Component Based Servicing which hardlinks files from its repository \Windows\WinSxS to their installation directory.

Since Windows XP uses the same NTFS format version as later releases, it's feasible to enable symbolic links support in it. For using NTFS symbolic links under Windows 2000 and XP, a third-party driver exists that does it by installing itself as a file system filter.[24][25]

See also

References

  1. ^ "Hard Links and Junctions". Windows Developer. Microsoft. 31 May 2018.
  2. ^ a b "Junction v1.07". Sysinternals. Microsoft. 4 July 2016.
  3. ^ "Symlinks in Windows 10! - Windows Developer BlogWindows Developer Blog". blogs.windows.com. 2 December 2016.
  4. ^ a b "Relocation of the Users directory and the ProgramData directory to a drive other than the drive that contains the Windows directory". Retrieved 2015-03-12.
  5. ^ a b "You encounter an error when trying to install Windows 8.1 due to redirecting the Users or Program Files folder to another partition". Retrieved 2015-03-12.
  6. ^ "File path formats on Windows systems". docs.microsoft.com.
  7. ^ "New-Item (PowerShell 3.0)". Microsoft Docs. Microsoft. 22 June 2020. If your location is in a FileSystem drive, the following values are allowed: If your location is in a FileSystem drive, the following values are allowed: File[,] Directory[,] Junction[,] HardLink
  8. ^ "New-Item (PowerShell 5.0)". Microsoft Docs. Microsoft. 22 June 2020.
  9. ^ "Fix Remove-Item <symbolic link to directory>". PowerShell repo. Microsoft. 4 March 2016 – via GitHub.
  10. ^ "mklink.c". reactos repo. ReactOS Deutschland e.V. 3 October 2017 – via GitHub.com.
  11. ^ "Dir". Microsoft Docs. Microsoft. 18 April 2012.
  12. ^ "Fsutil hardlink". Microsoft Docs. Microsoft. 18 April 2012.
  13. ^ "Fsutil reparsepoint". Microsoft Docs. Microsoft. 18 April 2012.
  14. ^ "Download Windows Server 2003 Resource Kit Tools". Download Center. 2015-03-10. Archived from the original on 2015-03-10.
  15. ^ "How to create and manipulate NTFS junction points". 2015-03-09. Archived from the original on 2015-03-09.
  16. ^ "Pscx". PowerShell Gallery. Microsoft. 17 January 2018.
  17. ^ Hill, Keith; Grehan, Oisin (17 January 2018). "README.md". pscx repo – via GitHub.
  18. ^ "NTFS Links, Directory Junctions, and Windows Shortcuts". www.flexhex.com. Retrieved 4 October 2019.
  19. ^ "libuv/src/win/fs.c". GitHub. Retrieved 12 December 2019.
  20. ^ "libuv/src/win/fs.c fs__unlink". GitHub. Retrieved 12 December 2019.
  21. ^ "PowerShell Remove Junction". StackOverflow. Retrieved 12 December 2019.
  22. ^ "Programming Considerations (Windows)". msdn.microsoft.com.
  23. ^ Hermann Schinagl (August 23, 2013). "ln - commandline hardlinks - Symbolic links for Windows XP".
  24. ^ "リンク/ジャンクション作成ツール". emk.name.