Intro Download and install Frequently Asked Questions Tips and tricks

Homepage







© J.C. Kessels 2009
MyDefrag Forum
August 23, 2014, 07:55:13 pm *
Welcome, Guest. Please login or register.

Login with username, password and session length
News:
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Usage of OpenFileById for NTFS volumes  (Read 11324 times)
dennis
Guest
« on: June 14, 2007, 08:19:50 am »

Hello,

I was wondering you might consider using the OpenFilebyId function for NTFS volumes instead of directory listing.
This is the function used by many commercial implementations and is
much faster than searching every directory.
It should also be possible to use FileId <zero> to defragment the MFT.
Please take a look at the following links.

http://msdn2.microsoft.com/en-us/library/aa365432.aspx
http://www.microsoft.com/downloads/details.aspx?FamilyId=1DECC547-AB00-4963-A360-E4130EC079B8&displaylang=en

Dennis
Logged
jeroen
Administrator
JkDefrag Hero
*****
Posts: 7220



View Profile WWW
« Reply #1 on: June 14, 2007, 09:47:25 am »

Thanks for your suggestion, I appreciate it! I have taken a look, but I do not understand that function. What is a "FileID"? How does it help me get a list of all files? The Microsoft documentation is totally inadequate, as usual. It does say however that the function is only available in Vista. That's no use to me. And it must be a very new function, so how can it be "used by many commercial implementations"?
Logged
dennis
Guest
« Reply #2 on: June 14, 2007, 01:07:12 pm »

The function is available for every OS of the NT family and is only to use on NTFS volumes, FAT volumes don't use fileID's.
Diskeeper and Perfectdisk use this capability for sure( again, only NTFS volumes) but it could be that they directly call the underlying native NtCreateFile() function, the OpenFileById function is a win32 function like CreateFile and they are basically just wrappers around the native NtCreateFile function in ntdll.dll with other arguments.
A file ID is the number of the file, like the inode number on unix systems, $Mft has filenumber 0 and is the first entry (in itself). The Openfilebyid allows you to go through all the files and folder on the volume, you could just write one for loop which will open and analyze/defragment all your files and folders. When you call the function with fileId 0 for instance it will return you  handle to the $Mft,
when you try to open an file with a filenumber that doesn't exist, it returns a handle to the file that has the largest possible filenumber under the number you specified and gives you the filenumber of that file.
In other words you start by trying to open the largest possible filenumber 0xFFFFFFFF and you will get a handle to lets say file 45000, next you try 44999 and if it exists good and you do your stuff and defragment the file or folder, otherwise you get the next file that exists (e.g. 44587 ) and so on.

Dennis
Logged
jeroen
Administrator
JkDefrag Hero
*****
Posts: 7220



View Profile WWW
« Reply #3 on: June 14, 2007, 04:21:21 pm »

The link you gave earlier points to Microsoft's manpage. In the requirements section at the bottom it says "Requires Windows Vista". I'm confused...

The description you give of the FileID reminds me of the FSCTL_GET_NTFS_FILE_RECORD function. It fetches raw NTFS records for something called the FileReferenceNumber. I've played for a while with that function in the past, but interpreting raw NTFS records is quite difficult so I put it aside for more pressing and less complicated things in JkDefrag. If this function OpenFileById() of yours is what I think it is then it might be very useful... Trust Microsoft to hide this in a separate download and not mention it anywhere in the MSDN Library.
Logged
jeroen
Administrator
JkDefrag Hero
*****
Posts: 7220



View Profile WWW
« Reply #4 on: June 15, 2007, 06:37:12 am »

I have now looked at the OpenFileById() function but it simply doesn't work for me. Whatever I do it always returns a "The parameter is incorrect" error. I've looked on the internet for examples, but haven't found anything. Pity! But I cannot use this.
Logged
StCroixSkipper
Newbie
*
Posts: 1


View Profile
« Reply #5 on: April 13, 2010, 07:58:47 pm »

I've posted code on MSDN at http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/c1550294-d121-4511-ac32-31551497f64e which uses DeviceIoControl and FSCTL_ control codes to both read the Master File Table and work with the USN Journal.

You can see how to construct the arguments you send into DeviceIoControl and then parse the information back out.

I just ran across OpenFileById and will be playing with it.  I'll post my code here.
Logged
Dennis
Newbie
*
Posts: 1


View Profile
« Reply #6 on: July 05, 2010, 01:53:20 pm »


Ok better late than never but I recovered an app from under the dust to show you how to use it:
you will have to download the Win32 FileID API from:
http://www.microsoft.com/downloads/details.aspx?FamilyID=1DECC547-AB00-4963-A360-E4130EC079B8&amp;displaylang=en&displaylang=en

I have just built it using Visual C++ 2008 Express Edition on Win2003 , so it still works  Smiley

Dennis

P.S. See attachment for code


* Program.zip (2.85 KB - downloaded 290 times.)
Logged
jeroen
Administrator
JkDefrag Hero
*****
Posts: 7220



View Profile WWW
« Reply #7 on: July 05, 2010, 03:39:05 pm »

Thanks for the example code and the link to the library. I have taken a quick look, but at the moment I don't think I have use for OpenFileByID() anymore, because I have found different ways to solve it. And I think it does not work for Windows 2000? Anyway, I have saved your code and made a note, and will look at it in more detail when I have more time.
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.5 | SMF © 2006-2008, Simple Machines LLC Valid XHTML 1.0! Valid CSS!