c++ - How to get fileindex without using file handle? -
the function getfileinformationbyhandle gives structure values nfileindexhigh , nfileindexlow comprise of fileindex.
what number? same usn?
is there way fileindex of file without opening it (any other method except getfileinformationbyhandle)?
you can use zwquerydirectoryfile fileobjectidinformation
, fileidbothdirectoryinformation
, fileidfulldirectoryinformation
query object id information files in directory. in case have open directory having file , not file itself. can useful example if file opened exclusive access or of cause if has no permission open file , not have or want not use backup privilege.
updated: following test example
#include <windows.h> #include <stdio.h> #include <tchar.h> typedef long ntstatus; #define nt_success(status) (((ntstatus)(status)) >= 0) #define nt_status(x)((ntstatus) { x }) #define status_success ((ntstatus)0x00000000l) #define status_no_more_files ((ntstatus)0x80000006l) #define status_invalid_info_class ((ntstatus)0xc0000003l) typedef struct _unicode_string { ushort length; ushort maximumlength; pwstr buffer; } unicode_string, *punicode_string; typedef struct _io_status_block { union { ntstatus status; pvoid pointer; } dummyunionname; ulong_ptr information; } io_status_block, *pio_status_block; typedef void (ntapi *pio_apc_routine) (pvoid apccontext, pio_status_block iostatusblock, ulong reserved); typedef enum _file_information_class { filedirectoryinformation = 1, filefulldirectoryinformation, // 2 filebothdirectoryinformation, // 3 filebasicinformation, // 4 filestandardinformation, // 5 fileinternalinformation, // 6 fileeainformation, // 7 fileaccessinformation, // 8 filenameinformation, // 9 filerenameinformation, // 10 filelinkinformation, // 11 filenamesinformation, // 12 filedispositioninformation, // 13 filepositioninformation, // 14 filefulleainformation, // 15 filemodeinformation, // 16 filealignmentinformation, // 17 fileallinformation, // 18 fileallocationinformation, // 19 fileendoffileinformation, // 20 filealternatenameinformation, // 21 filestreaminformation, // 22 filepipeinformation, // 23 filepipelocalinformation, // 24 filepiperemoteinformation, // 25 filemailslotqueryinformation, // 26 filemailslotsetinformation, // 27 filecompressioninformation, // 28 fileobjectidinformation, // 29 filecompletioninformation, // 30 filemoveclusterinformation, // 31 filequotainformation, // 32 filereparsepointinformation, // 33 filenetworkopeninformation, // 34 fileattributetaginformation, // 35 filetrackinginformation, // 36 fileidbothdirectoryinformation, // 37 fileidfulldirectoryinformation, // 38 filevaliddatalengthinformation, // 39 fileshortnameinformation, // 40 fileiocompletionnotificationinformation, // 41 fileiostatusblockrangeinformation, // 42 fileiopriorityhintinformation, // 43 filesfioreserveinformation, // 44 filesfiovolumeinformation, // 45 filehardlinkinformation, // 46 fileprocessidsusingfileinformation, // 47 filenormalizednameinformation, // 48 filenetworkphysicalnameinformation, // 49 fileidglobaltxdirectoryinformation, // 50 fileisremotedeviceinformation, // 51 fileattributecacheinformation, // 52 filenumanodeinformation, // 53 filestandardlinkinformation, // 54 fileremoteprotocolinformation, // 55 filemaximuminformation } file_information_class, *pfile_information_class; typedef struct _file_id_full_dir_information { ulong nextentryoffset; ulong fileindex; large_integer creationtime; large_integer lastaccesstime; large_integer lastwritetime; large_integer changetime; large_integer endoffile; large_integer allocationsize; ulong fileattributes; ulong filenamelength; ulong easize; large_integer fileid; wchar filename[1]; } file_id_full_dir_information, *pfile_id_full_dir_information; typedef struct _file_id_both_dir_information { ulong nextentryoffset; ulong fileindex; large_integer creationtime; large_integer lastaccesstime; large_integer lastwritetime; large_integer changetime; large_integer endoffile; large_integer allocationsize; ulong fileattributes; ulong filenamelength; ulong easize; cchar shortnamelength; wchar shortname[12]; large_integer fileid; wchar filename[1]; } file_id_both_dir_information, *pfile_id_both_dir_information; typedef struct _file_id_global_tx_dir_information { ulong nextentryoffset; ulong fileindex; large_integer creationtime; large_integer lastaccesstime; large_integer lastwritetime; large_integer changetime; large_integer endoffile; large_integer allocationsize; ulong fileattributes; ulong filenamelength; large_integer fileid; guid lockingtransactionid; ulong txinfoflags; wchar filename[1]; } file_id_global_tx_dir_information, *pfile_id_global_tx_dir_information; #define file_id_global_tx_dir_info_flag_writelocked 0x00000001 #define file_id_global_tx_dir_info_flag_visible_to_tx 0x00000002 #define file_id_global_tx_dir_info_flag_visible_outside_tx 0x00000004 typedef struct _file_objectid_information { longlong filereference; uchar objectid[16]; union { struct { uchar birthvolumeid[16]; uchar birthobjectid[16]; uchar domainid[16]; } dummystructname; uchar extendedinfo[48]; } dummyunionname; } file_objectid_information, *pfile_objectid_information; typedef ntstatus (winapi *pzw_query_directory_file) (handle filehandle, handle event, pio_apc_routine apcroutine, pvoid apccontext, pio_status_block iostatusblock, pvoid fileinformation, ulong length, file_information_class fileinformationclass, boolean returnsingleentry, punicode_string filename, boolean restartscan); void dumpfileinformation (lpcwstr pszdirname, lpcwstr pszfilename) { wchar szfilename[32767]; unicode_string fn; io_status_block iosb; ntstatus status; longlong bybuffer[(32767+sizeof(file_id_full_dir_information))/sizeof(longlong)]; pfile_id_full_dir_information pfullinfo = (pfile_id_full_dir_information)bybuffer; //pfile_id_global_tx_dir_information pglobaltxdirinfo = (pfile_id_global_tx_dir_information)bybuffer; handle hdir = invalid_handle_value; pzw_query_directory_file zwquerydirectoryfile = (pzw_query_directory_file) getprocaddress(getmodulehandle(l"ntdll.dll"),"zwquerydirectoryfile"); __try { hdir = createfilew (pszdirname, file_list_directory, file_share_read | file_share_write, null, open_existing, file_flag_backup_semantics, null); if (hdir == invalid_handle_value) { _tprintf(text("can't open directory '%ls': error %d\n"), pszdirname, getlasterror()); __leave; } lstrcpyw (szfilename, pszfilename); fn.buffer = (lpwstr) szfilename; fn.length = lstrlen(szfilename)*sizeof(wchar); fn.maximumlength = sizeof(szfilename); rtlzeromemory ((pvoid)&iosb, sizeof(iosb)); status = zwquerydirectoryfile (hdir, null, null, null, &iosb, bybuffer, sizeof(bybuffer), fileidfulldirectoryinformation, true, &fn, false); if (nt_success(status)) { _tprintf (text("the file '%ls%ls%ls' has fileid: 0x%08x%08x\n"), pszdirname, fn.length>0 && pszdirname[fn.length/sizeof(wchar)-1] == l'\\' ? l"": l"\\", szfilename, pfullinfo->fileid.highpart, pfullinfo->fileid.lowpart); } } __finally { if (hdir != invalid_handle_value) closehandle (hdir); } } int _tmain () { dumpfileinformation (l"c:\\", l"system volume information"); dumpfileinformation (l"c:\\", l"pagefile.sys"); return 0; }
produce on computer output:
the file 'c:\\system volume information' has fileid: 0x000100000000a2f0 file 'c:\\pagefile.sys' has fileid: 0x006b00000000a673
Comments
Post a Comment