Click here to Skip to main content
13,291,503 members (66,972 online)
Click here to Skip to main content
Add your own
alternative version


46 bookmarked
Posted 9 Sep 2005

Eyes on NTFS

, 9 Sep 2005
Rate this:
Please Sign up or sign in to vote.
An introduction to NTFS's Change Journal and a simple tool to operate
Sample Image


The Change Journal is a database that contains a list of every change made to the files or directories on an NTFS 5.0 volume, that is, the Change Journal is a log file.

The structure of Change Journal's record includes USN, Reason, Filename, ParentFileReferenceNumber and TimeStamp, etc.
Let me explain them to you:

  • USN is ID
  • Reason is why the record is written, say, 'create'
  • Filename is merely name without path
  • ParentFileReferenceNumber is a foreign key to file's path
  • TimeStamp is the time when event happens

With this information, we can know what happened to a file, the only problem is where to find the path of the file. There is no simple/easy way to get there as far as I know. Let's face it, NTFS is not a RDBMS, we can't use one 'select' sentence from relevant tables to get all we need. Although, in concept, ParentFileReferenceNumber is a foreign key. Well, what Window2000 provides us to get a FileReferenceNumber is using an opened handle to the file, the API is GetFileInformationByHandle. We have to search the whole directory tree to find what we want! -- That's why I feel tired for hard drive every time I push the 'Path detail' button.

The other thing we should know is that the Change Journal is not available automatically. Somebody should activate/create it before using it.

To archive functions on Change Journal, the key API is DeviceIoControl. To fulfill a retrieval of the Change Journal records, the basic steps are: 

  1. Open the Change Journal file handle using API CreateFile 
  2. Call DeviceIoControl using FSCTL_QUERY_USN_JOURNAL as the second parameter 
  3. Call DeviceIoControl using FSCTL_READ_USN_JOURNAL as the second parameter

Step (1) opens the handle and the handle is also a parameter needed by step (2); Step (2) gets the general information about the Change Journal, the key is UsnJournalID and FirstUsn. UsnJournalID is Session ID, which is needed by every call of Step (3). FirstUsn is needed by first time call of Step (3). Step (3) is called repetitively until we get all the records. Every call of Step (3) requires a StartUsn which indicates from which record we are searching and it could be fetched from the previous call, is it simple for your loop?

The demo code includes a class CChangeJournal and its consumer which is a dialog. CChangeJournal is used to operate the Change Journal, including creating and disabling(deleting) it.

Note: I learned all this from MSDN, especially from the article: 'Keeping an Eye on Your NTFS Drives: the Windows 2000 Change Journal Explained ' by Jeffrey Cooperstein and Jeffrey Richter. In addition, a lot of the code in this demo is from their demo. I simplified the code by ignoring the OVERLAPPED I/O.


Sometimes, you may have done a series of operations on files and probably one of them is wrong, but after a short time, you couldn't remember where the file is and you don't want to roll back all the operations by using 'Undo' provided by the Explorer, because most operations are what you want. At such times, this tool might help.
Anyway, some real time anti-virus software may use this technique.

Using the Code

CChangeJournal provides some functions according to the steps I mentioned above. Let me explain with the code below:

CChangeJournal cj('c');              	// 'c' represents "C:" ;;                              	// open file handle    [step 1]

cj.query(...);                           	// get general information, 
				    	// key is UsnJournalID [step 2]

CJRecords recs;                        	// CJRecords is a vector of part 
				    	// structure of Change Journal

cj.getRecords(startUsn,..,recs,...);     	// get records we need [step 3]

// the other things
cj.create();                             	// create Change Journal

cj.disable(UsnJournalID);                	// delete Change Journal need a parameter 
				    	// from (step 2: query)

cj.setDriver('D');                       	// change Drive

cj.setMemoryBlock(bytes);                	// how large you want to allocate for 
				    	// reading records. it's called
                                         	// with default 1M size, you can change 
				    	// if you like. 

Points of Interest

There are several things I want to mention:

  1. There is another way to fetch records by using FSCTL_ ENUM_USN_DATA as the parameter of DeviceIoControl, find out from MSDN.
  2. In my demo, within the dialog(consumer), I set 'nextUsn' not the real next Usn, while the current last Usn, so I get one duplicate record every time (except page 1), but it prevents calling being blocked. This is because the query will be blocked if the Next usn hasn't been created. Unfortunately, I couldn't stop it being blocked when there is nothing in the database when the Change Journal is just being created!
  3. Firstly when I tested my demo, I could find all the paths associated with the ParentFileReferenceNumber except the root directory, in my case, say "C:". Then I tried "C:\", o.k., it started working. It bothers me so far. Could you tell me why when you find out, thanks a lot.
  4. What if a directory is renamed? It doesn't change its FileReferenceNumber; If we delete a directory, of course we can't find it any more, because we have no way to get its opened handle. Then what if we recreate a directory with the same name? The FileReferenceNumber changes! So, if we really want to keep an eye on NTFS, we should maintain a database of our own to trace every change we make -- just as the two Jeffreys do in their demo.
  5. What's the rule of a new FileReferenceNumber? I couldn't find it.


  • Article created on 9th September 2005


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Jason Q Zhang
Web Developer
Australia Australia
6 years developing on Windows, C++, RDBMS, Win32. STL is my favorite. Like really cool techniques

You may also be interested in...

Comments and Discussions

QuestionHow to find number of files and folders in any specified directory using USN Journal Pin
ajaytvish25-Sep-13 3:02
memberajaytvish25-Sep-13 3:02 
GeneralMy vote of 2 Pin
cocowalla11-May-11 4:06
membercocowalla11-May-11 4:06 
teubies7-Apr-11 3:43
memberteubies7-Apr-11 3:43 
GeneralRe: Never see USN_REASON_FILE_DELETE Pin
teubies1-May-11 22:28
memberteubies1-May-11 22:28 
Generalcomplete rollback Pin
vvurade26-Feb-11 20:28
membervvurade26-Feb-11 20:28 
QuestionHow to Read File Path and Last USN Number Pin
Gul Zakhmi29-Nov-07 19:04
memberGul Zakhmi29-Nov-07 19:04 
GeneralErrors while Compiling\linking Pin
MIAN KAMRAN11-Sep-06 0:20
memberMIAN KAMRAN11-Sep-06 0:20 
GeneralRe: Errors while Compiling\linking Pin
MIAN KAMRAN11-Sep-06 0:25
memberMIAN KAMRAN11-Sep-06 0:25 
GeneralRe: Errors while Compiling\linking Pin
Jason Q Zhang11-Sep-06 21:46
memberJason Q Zhang11-Sep-06 21:46 
GeneralReading a remote File change journal Pin
vikijain8-May-06 13:28
membervikijain8-May-06 13:28 
GeneralReadDirectoryChangesW Pin
umeca7413-Sep-05 2:28
memberumeca7413-Sep-05 2:28 
GeneralRe: ReadDirectoryChangesW Pin
Jason Q Zhang13-Sep-05 19:07
memberJason Q Zhang13-Sep-05 19:07 
GeneralRe: ReadDirectoryChangesW [modified] Pin
marc ochsenmeier27-Jun-10 23:21
membermarc ochsenmeier27-Jun-10 23:21 
GeneralNot active Pin
Spole11-Sep-05 7:22
memberSpole11-Sep-05 7:22 
GeneralRe: Not active Pin
Jason Q Zhang11-Sep-05 18:59
memberJason Q Zhang11-Sep-05 18:59 
GeneralFiles missing Pin
K-ballo9-Sep-05 23:36
memberK-ballo9-Sep-05 23:36 
GeneralTemporary links Pin
Ravi Bhavnani10-Sep-05 4:03
memberRavi Bhavnani10-Sep-05 4:03 
GeneralRe: Temporary links Pin
Jason Q Zhang11-Sep-05 3:55
memberJason Q Zhang11-Sep-05 3:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171207.1 | Last Updated 10 Sep 2005
Article Copyright 2005 by Jason Q Zhang
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid