In: Computer Science
When a user tries to write a file, the file system needs to detect if that file is a directory so that it can restrict writes to maintain the directory’s internal consistency. Given a file’s name, how would you design NTFS to keep track of whether each file is a regular file or a directory?
With the help of MASTER FILE TABLE the NTFS can identify the track of the files that either it is regular files or directory. When a unique record about the files is created in a special file. A unique record helps to locate a files possibly scattered clusters.
The NTFS always tries to identify the contiguous storage space which can hold the all files related to the project.
In the NTFS file system, a file can always keeps the same file ID until it can be erased. When you replace the one file with another file without changing their file ID then by using the ReplaceFile function.
The following code can justify the statement that how to get operation system file handle and then use it to get file ID. Once you can generate the file ID, you can keep track a file.
To test the code, create a new application and paste the code with the filename extension .cs
Then add a “File1.txt” file to the project you required to track and set it’s copy to the output directory and check the behavior of the new file directory. After saving the file run the file with the given project name.
Then you can choose the folder and rename or change the location of the file. Then update the path of the file and observed that the file ID has not been different. So after this process yo may have to enter the empty string to eliminate the program.
using System; using System.Collections.Generic; using System.File; using System.IO; using System.Runtime.Services; using Microsoft.Win32.SafeHandles; namespace Filechange { class Program { struct BY_HANDLE_FILE_INFORMATION { public uint FileAttribute; public System.Runtime.Services.ComTypes.FILETIME CreationTime; public System.Runtime.Services.ComTypes.FILETIME LastAccessTime; public System.Runtime.Services.ComTypes.FILETIME LastWriteTime; public uint VolumeSerialNumber; public uint FileSizeHigh; public uint FileSizeLow; public uint NumberOfLinks; public uint FileIndexHigh; public uint FileIndexLow; } [DllImport("kernel32.dll", SetLastError = true)] private static extern bool GetFileInformationByHandle(SafeFileHandle pFile, out BY_HANDLE_FILE_INFORMATION lpFileInformation); static void Main(string[] args) { string fileName = "File1.txt"; while (!string.IsNullOrEmpty(fileName)) { FileStream strem = File.Open(fileName, FileMode.Open); BY_HANDLE_FILE_INFORMATION pInfo = new BY_HANDLE_FILE_INFORMATION(); GetFileInformationByHandle(strem.SafeFileHandle, out pInfo); Console.WriteLine("File Name: " + fileName); Console.WriteLine("File ID: " + pInfo.FileIndexHigh.ToString() + " " + pInfo.FileIndexLow.ToString()); Console.Write("Enter file name:"); strem.Close(); fileName = Console.ReadLine(); } } } }
Before using this process you have to make sure that whether the disk format is NTFS or not and if the disk format is not NTFS then you have to verify it with the given code and the code is as:
bool isNTFS = new DriveInfo(new FileInfo(fileName).FullName).DriveFormat == "NTFS";