Platform SDK: Hardware

USN_RECORD

The USN_RECORD structure contains the information for a change journal version 2.0 record. Win32-based applications should not attempt to work with change journal versions earlier than 2.0.

typedef struct {
  DWORD RecordLength;
  WORD MajorVersion;
  WORD MinorVersion;
  DWORDLONG FileReferenceNumber;
  DWORDLONG ParentFileReferenceNumber;
  USN Usn;
  LARGE_INTEGER TimeStamp;
  DWORD Reason;
  DWORD SourceInfo;
  DWORD SecurityId;
  DWORD FileAttributes;
  WORD FileNameLength;
  WORD FileNameOffset;
  WCHAR FileName[1];
} USN_RECORD, *PUSN_RECORD;

Members

RecordLength
Total length of the record in bytes. Because USN_RECORD is of variable size, the RecordLength member should be used when calculating the address of the next record in an output buffer, such as a buffer returned from operations for the DeviceIoControl function that work with USN_RECORD.

The size in bytes of any change journal record is at most the size of the USN_RECORD structure, plus MaximumComponentLength characters minus 1 (for the character declared in the structure) times the size of a wide character. The value of MaximumComponentLength may be determined by calling the function GetVolumeInformation. In C, you can determine a record's size as follows:

(MaximumComponentLength * sizeof(WCHAR) + sizeof(USN_RECORD) - sizeof(WCHAR))
 

To maintain compatibility across version changes of the change journal software, use a run-time calculation to determine the size of the USN_RECORD structure. For more information about compatibility across version changes, see the Remarks section later in this topic.

MajorVersion
Major version number of the change journal software for this record. For example, if the change journal software is version 2.0, the major version number is 2.
MinorVersion
Minor version number of the change journal software for this record. For example, if the change journal software is version 2.0, the minor version number is 0.
FileReferenceNumber
Ordinal number of the file or directory for which this record notes changes. This is an arbitrarily assigned value that associates a journal record with a file.
ParentFileReferenceNumber
Ordinal number of the directory in which the file or directory associated with this record is located. This is an arbitrarily assigned value that associates a journal record with a parent directory.
Usn
USN of this record.
TimeStamp
Standard UTC time stamp of this record, in 64-bit format.
Reason
Flags describing reasons for changes that have accumulated in this file or directory journal record since the file or directory opened. When a file or directory closes, then a final USN record is generated with the USN_REASON_CLOSE flag set. The next change (for example, after the next open operation or deletion) starts a new record with a new set of reason flags.

Note that a rename or move operation generates two USN records, one recording the old parent directory for the item and one recording a new parent.

The list of valid flags is as follows. Unused bits are reserved.
Value Meaning
USN_REASON_BASIC_INFO_CHANGE A user has either changed one or more file or directory attributes (such as the read-only, hidden, system, archive, or sparse attribute), or one or more time stamps.
USN_REASON_CLOSE The file or directory was closed.
USN_REASON_COMPRESSION_CHANGE The compression state of the file or directory was changed from or to compressed.
USN_REASON_DATA_EXTEND The file or directory was added to.
USN_REASON_DATA_OVERWRITE Data in the file or directory was overwritten.
USN_REASON_DATA_TRUNCATION The file or directory was truncated.
USN_REASON_EA_CHANGE The user made a change to the file's or directory's extended attributes. These NTFS attributes are not accessible to Win32-based applications.
USN_REASON_ENCRYPTION_CHANGE The file or directory was encrypted or decrypted.
USN_REASON_FILE_CREATE The file or directory was created for the first time.
USN_REASON_FILE_DELETE The file or directory was deleted.
USN_REASON_HARD_LINK_CHANGE An NTFS hard link was added to or removed from the file or directory. An NTFS hard link, similar to a POSIX hard link, is one of several directory entries that refer to the same file or directory.
USN_REASON_INDEXABLE_CHANGE A user changed the FILE_ATTRIBUTE_NOT_CONTENT_INDEXED attribute. That is, the user changed the file or directory from one that can be content indexed to one that cannot, or vice versa. (Content indexing permits rapid searching of data by building a database of selected content.)
USN_REASON_NAMED_DATA_EXTEND One or more named data streams for the file were added to.
USN_REASON_NAMED_DATA_OVERWRITE Data in one or more named data streams for the file was overwritten.
USN_REASON_NAMED_DATA_TRUNCATION One or more named data streams for the file was truncated.
USN_REASON_OBJECT_ID_CHANGE The object identifier of the file or directory was changed.
USN_REASON_RENAME_NEW_NAME The file or directory was renamed, and the file name in this USN_RECORD structure is the new name.
USN_REASON_RENAME_OLD_NAME The file or directory was renamed, and the file name in this USN_RECORD structure is the previous name.
USN_REASON_REPARSE_POINT_CHANGE The reparse point contained in the file or directory was changed, or a reparse point was added to or deleted from the file or directory.
USN_REASON_SECURITY_CHANGE A change was made in the access rights to the file or directory.
USN_REASON_STREAM_CHANGE A named stream has been added to or removed from the file, or a named stream has been renamed.

SourceInfo
Additional information about the source of the change, set by the FSCTL_MARK_HANDLE DeviceIoControl operation. One of the two following values can be set.
Value Meaning
USN_SOURCE_DATA_MANAGEMENT The operation provides information about a change to the file or directory made by the operating system.

A typical use is when the Remote Storage system moves data from external to local storage. Remote Storage is the hierarchical storage management software in Windows 2000. Such a move usually at a minimum adds the USN_REASON_DATA_OVERWRITE flag to a USN record. However, the data has not changed from the users' point of view. By noting USN_SOURCE_DATA_MANAGEMENT in the SourceInfo member, you can determine that although a write operation was performed on the item, data has not changed.

USN_SOURCE_AUXILIARY_DATA The operation adds a private data stream to a file or directory.

An example might be a virus detector adding checksum information. As the virus detector modifies the item, the system generates USN records. USN_SOURCE_AUXILIARY_DATA indicates that the modifications did not change the application data.


SecurityId
Unique security identifier assigned to the file or directory associated with this record.
FileAttributes
Attributes for the file or directory associated with this record, as returned by the GetFileAttributes function. Attributes of streams associated with the file or directory are excluded.
FileNameLength
Length, in bytes, of the name of the file or directory associated with this record. The FileName member contains the name for which the FileNameLength member indicates the length. Use FileNameLength to determine file name length, rather than depending on a trailing '\0' to delimit the file name in FileName.
FileNameOffset
Offset of the FileName member from the beginning of the structure.
FileName
Name of the file or directory associated with this record in Unicode format. This file or directory name is of variable length.

When working with FileName, do not count on the file name containing a trailing '\0' delimiter, but instead determine the length of the file name with FileNameLength.

Do not perform any compile-time pointer arithmetic using FileName. Instead, make any necessary calculations at run time using the value of the FileNameOffset member. Doing so helps make your code compatible with any future versions of USN_RECORD.

Remarks

In output buffers returned from DeviceIoControl operations that work with USN_RECORD, all records are aligned on 64-bit boundaries from the start of the buffer.

To provide a path for upward compatibility in change journal clients, Microsoft provides a major and minor version number of the change journal software in the USN_RECORD structure. Your code should examine these values, detect its own compatibility with the change journal software, and if necessary gracefully handle any incompatibility.

A change in the minor version number indicates that the existing USN_RECORD structure members are still valid, but that new members may have been added between the penultimate member and the last, which is a variable-length string. In order to handle such a change gracefully, your code should not do any compile-time pointer arithmetic that relies on the location of the last member. For example, this makes the C code sizeof(USN_RECORD) unreliable. Instead, rely on run-time calculations using the RecordLength member.

An increase in the major version number of the change journal software indicates that the USN_RECORD structure may have undergone major changes and that the current definition may not be reliable. If your code detects a change in the major version number of the change journal software, it should not work with the change journal.

For more information on working with change journal records, see Creating, Modifying, and Deleting a Change Journal.

Requirements

  Windows NT/2000: Requires Windows 2000.
  Windows 95/98: Unsupported.
  Header: Declared in Winioctl.h.

See Also

Device Input and Output Overview, Device Input and Output Structures, DeviceIoControl, GetVolumeInformation, FSCTL_MARK_HANDLE, FSCTL_READ_USN_JOURNAL, GetFileAttributes, READ_USN_JOURNAL_DATA