Jump to content

Getting Size on Disk


SoulA
 Share

Recommended Posts

@Cars0n66: I think you misunderstood the question from SoulA! Read it again carefully!

Br,

UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

I knew I already read it somewhere, found the MSDN article again: "There are three types of file links supported in the NTFS file system: hard links, junctions, and symbolic links."

Hey KaFu, thanks for the link. Had time recently to revisit this and it makes sense to me now. Hard Links are no different than the original file itself basically. Both point to the same record in the MFT, and deleting the 'original' doesn't affect the hard link, just the 'link count'.

To that end I've created the following function which can be used to find out if there are hardlinks (index[6] = # of links). I've also added URL's for info on hard links and whatnot (more for my benefit I guess than anyone else's):

#include <WinAPI.au3>
; ===================================================================================================================
; Func _FileGetInfoByOpening($sFile)
;
; Gets file attribute & size information by opening a file.  Also gets hard-link counts and other stuff.
;
; NOTES:
; - [Hard Links] (8th element of return structure):
;   A few good references on Hard Links (vs Symbolic Links and Junctions):
;   MSDN: 'Hard Links and Junctions (Windows)' [also links to 'Creating Symbolic Links (Windows)' article]:
;       http://msdn.microsoft.com/en-us/library/aa365006%28v=VS.85%29.aspx
;   FlexHex: 'NTFS Hard Links, Directory Junctions, and Windows Shortcuts':
;       http://www.flexhex.com/docs/articles/hard-links.phtml
;   Wikipedia: 'Hard Link':
;       http://en.wikipedia.org/wiki/Hard_link
;
;   *Ultimately a Hard Link is the same as the actual original file listing - both point to one file*
;   (deleting the 'original' will not delete the hard link, as they both are just 'pointers' to an MFT entry)
;
; - [FileIndex]:
;   MSDN:
;   'The [FileIndex] identifier ... and the volume serial number uniquely identify a file on a single computer.
;   To determine whether two open handles represent the same file, combine the identifier and the
;   volume serial number for each file and compare them.'
;
; Returns:
;   Success: An 8-element array of File Info (@error=0):
;       [0] = File Attributes
;       [1] = Creation Time (as 64-bit FileTime)
;       [2] = Last-Access Time (as 64-bit FileTime)
;       [3] = Last-Write Time (as 64-bit FileTime)
;       [4] = Volume Serial Number (MSDN: 'The serial number of the volume that contains a file.')
;       [5] = File Size (in bytes)
;       [6] = Number of Links to this file (>1 means there is at least 1 hardlink created)
;       [7] = File Index (unique identifier - see above MSDN info)
;   Failure: '' and @error set:
;       @error = -1 = _WinAPI_CreateFile() error - call GetLastError for more info
;       @error =  1 = Invalid parameter
;       @error =  2 = DLLCall error. @extended = actual DLLCall error-code (see AutoIt Help)
;       @error =  3 = API call returned failure
;
; Author: Ascend4nt
; ===================================================================================================================

Func _FileGetInfoByOpening($sFile)
    If Not IsString($sFile) Or $sFile='' Then Return SetError(1,0,-1)
    Local $aRet,$hFile,$iErr,$stBHFileInfo
    $hFile=_WinAPI_CreateFile($sFile,2,2,6)
    If $hFile=0 Then Return SetError(-1,@error,'')
    ; BY_HANDLE_FILE_INFORMATION: dwFileAttributes,ftCreationTime,ftLastAccessTime,ftLastWriteTime,
    ;   dwVolumeSerialNumber,nFileSizeHigh,nFileSizeLow,nNumberOfLinks,nFileIndexHigh,nFileIndexLow
    $stBHFileInfo=DllStructCreate('align 4;dword;uint64;uint64;uint64;dword;dword;dword;dword;dword;dword')
    $aRet=DllCall('kernel32.dll','bool','GetFileInformationByHandle','handle',$hFile,'ptr',DllStructGetPtr($stBHFileInfo))
    $iErr=@error
    _WinAPI_CloseHandle($hFile)
    If $iErr Then Return SetError(2,$iErr,'')
    If Not $aRet[0] Then Return SetError(3,0,'')
    ; For converting backwards split 64-bit values FileSize and FileIndex
    Local $stTo64=DllStructCreate('uint64'),$stTwo32=DllStructCreate('dword[2]',DllStructGetPtr($stTo64))
    Dim $aRet[8]
    For $i=0 To 4
        $aRet[$i]=DllStructGetData($stBHFileInfo,$i+1)
    Next
    ; FileSize next
    DllStructSetData($stTwo32,1,DllStructGetData($stBHFileInfo,7),1)
    DllStructSetData($stTwo32,1,DllStructGetData($stBHFileInfo,6),2)
    $aRet[5]=DllStructGetData($stTo64,1)
    $aRet[6]=DllStructGetData($stBHFileInfo,8)  ; nNumberOfLinks - If >1, there are Hard Links
    ; FileIndex next
    DllStructSetData($stTwo32,1,DllStructGetData($stBHFileInfo,10),1)
    DllStructSetData($stTwo32,1,DllStructGetData($stBHFileInfo,9),2)
    $aRet[7]=DllStructGetData($stTo64,1)
    Return $aRet
EndFunc

Thanks again!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...