Search code examples
windowsinstallationnsisntfs

Detecting if the target volume is NTFS using NSIS


I'm creating an installer with NSIS for a program that needs to run on an NTFS volume. How do I detect if the install to path is on an NTFS volume and act accordingly (show a help/warning message)?


Solution

  • Using external tools is not always a good idea (Not every command line tool exists on the Home versions of windows ) It's always better to call the correct API directly with the system plug in.

    !include LogicLib.nsh
    
    StrCpy $0 "c:\"
    System::Call 'Kernel32::GetVolumeInformation(t "$0",t,i ${NSIS_MAX_STRLEN},*i,*i,*i,t.r1,i ${NSIS_MAX_STRLEN})i.r0'
    ${If} $0 <> 0
        MessageBox mb_ok "fs=$1"
    ${EndIf}
    

    But in this case, you should not be checking the file system type, but you should look for the actual feature you need (compression,encryption,junctions,sparse files etc)

    !define FILE_SUPPORTS_ENCRYPTION 0x00020000
    !define FILE_READ_ONLY_VOLUME 0x00080000
    !define FILE_VOLUME_QUOTAS 0x00000020
    
    !macro MakeBitFlagYesNo flags bit outvar
    IntOp ${outvar} ${flags} & ${bit}
    ${IfThen} ${outvar} <> 0 ${|} StrCpy ${outvar} "Yes" ${|}
    ${IfThen} ${outvar} == 0 ${|} StrCpy ${outvar} "No" ${|}
    !macroend
    
    StrCpy $0 "c:\"
    System::Call 'Kernel32::GetVolumeInformation(t "$0",t,i ${NSIS_MAX_STRLEN},*i,*i,*i.r1,t,i ${NSIS_MAX_STRLEN})i.r0'
    ${If} $0 <> 0
        !insertmacro MakeBitFlagYesNo $1 ${FILE_SUPPORTS_ENCRYPTION} $2
        !insertmacro MakeBitFlagYesNo $1 ${FILE_READ_ONLY_VOLUME} $3
        !insertmacro MakeBitFlagYesNo $1 ${FILE_VOLUME_QUOTAS} $4
        MessageBox mb_ok "flags=$1 $\nFILE_SUPPORTS_ENCRYPTION=$2$\nFILE_READ_ONLY_VOLUME=$3$\nFILE_VOLUME_QUOTAS=$4"
    ${EndIf}