简体   繁体   English

如何知道两个硬链接是否指向同一个inode? (C#)

[英]How to know if two hard links point to the same inode? (C#)

Anyway of checking, in C#, that two files (hard links) point to the same inode? 无论如何在C#中检查两个文件(硬链接)是否指向同一个inode? And also get the count of this inode, in case there are more than two... ? 还得到这个inode的数量,如果有两个以上......?

You can get count of hard links pointing to the node using GetFileInformationByHandle function. 您可以使用GetFileInformationByHandle函数获取指向节点的硬链接数。 For example: 例如:

[DllImport("kernel32.dll", SetLastError = true)]
static extern bool GetFileInformationByHandle(
    SafeFileHandle hFile,
    out BY_HANDLE_FILE_INFORMATION lpFileInformation
);

[StructLayout(LayoutKind.Sequential)]
struct BY_HANDLE_FILE_INFORMATION {
    public uint FileAttributes;
    public FILETIME CreationTime;
    public FILETIME LastAccessTime;
    public FILETIME LastWriteTime;
    public uint VolumeSerialNumber;
    public uint FileSizeHigh;
    public uint FileSizeLow;
    public uint NumberOfLinks;
    public uint FileIndexHigh;
    public uint FileIndexLow;
}

// then in another place
using (var fs = File.OpenRead("path to your file")) {                
    BY_HANDLE_FILE_INFORMATION info;
    GetFileInformationByHandle(fs.SafeFileHandle, out info);
    var numberOfLinks = info.NumberOfLinks;
}

To get what files they are pointing to, you will need another win api functions: FindFirstFileNameW and FineNextFileNameW . 要获取它们指向的文件,您将需要另一个win api函数: FindFirstFileNameWFineNextFileNameW Use them like this: 像这样使用它们:

[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
static extern IntPtr FindFirstFileNameW(
       string lpFileName,
       uint dwFlags,
       ref uint stringLength,
       StringBuilder fileName);

[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
static extern bool FindNextFileNameW(
        IntPtr hFindStream,
        ref uint stringLength,
        StringBuilder fileName);

[DllImport("kernel32.dll", SetLastError = true)]
static extern bool FindClose(IntPtr fFindHandle);

public static string[] GetFileHardLinks(string filePath) {
    // first get drive letter
    var drive = new DriveInfo(Path.GetPathRoot(filePath));
    var result = new List<string>();
    // buffer for return value
    var sb = new StringBuilder(256);
    // length of buffer
    uint sbLength = 256;
    // third argument contains reference to buffer length (buffer is StringBuilder). 
    // it's a reference because if it's too small, call returns an error and will put required length there instead
    IntPtr findHandle = FindFirstFileNameW(filePath, 0, ref sbLength, sb);
    // returns -1 on error
    if (findHandle.ToInt64() != -1) {
        do {
            // combine the result with drive letter (it comes without it)
            result.Add(Path.Combine(drive.RootDirectory.FullName, sb.ToString().TrimStart(new [] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar})));
            sb.Clear();
            sbLength = 256;
            // and repeat
        } while (FindNextFileNameW(findHandle, ref sbLength, sb));
        FindClose(findHandle);
        return result.ToArray();
    }
    return null;
}

This code might be not production ready, so take care. 此代码可能未准备好生产,因此请注意。 But it should at least give you an idea. 但它至少应该给你一个想法。 If you will use it - carefully read what those function return on errors and act accordingly (for example, handle the case when buffer length is not enough, or just use larger buffer than 256). 如果您将使用它 - 仔细阅读这些函数返回错误并采取相应措施(例如,处理缓冲区长度不足时的情况,或者只使用大于256的缓冲区)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何知道GraphicsPath是否包含C#中的一个点 - How to know if a GraphicsPath contains a point in C# 列出文件的硬链接(在 C# 中) - List Hard Links of a file (in C#) C# 检查两个泛型变量是否指向同一个元素 - C# Check if two generic variables point to same element 如何知道图表中点(X,Y)的X值,如何知道YC#的值? - How I can know X value of point (X, Y) in chart and I know the value of Y C#? C#:如何使用xml linq知道两个元素是否连续 - c#: how to know if the two element are in a row using xml linq 如何在 C# 图表中的同一个“x”点上的“y”中添加值 - how to add value in 'y' on same 'x' point in c# chart 如何使用C#查找硬盘驱动器型号? - How to lookup Hard Drive model with C#? 如何使用C#对硬盘进行分区 - How to partition a hard drive using C# 如果您知道起始点和停止点但不知道 asp.net C# 中的中间点,如何拆分字符串 - How do you split a string if you know the start and stop point but not the middle in asp.net C# 是否可以在不需要不安全上下文的情况下判断两个 WeakReferences 是否指向 c# 中的同一个 object? - Is it possible to tell whether two WeakReferences point to the same object in c# without requiring an unsafe context?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM