Search in sources :

Example 1 with FILE_BASIC_INFO

use of com.sun.jna.platform.win32.WinBase.FILE_BASIC_INFO in project jna by java-native-access.

the class Kernel32Test method testFileTimeFromLargeInteger.

/**
     * Test FILETIME's LARGE_INTEGER constructor
     * @throws IOException
     */
public final void testFileTimeFromLargeInteger() throws IOException {
    File tmp = File.createTempFile("testGetFileInformationByHandleEx", "jna");
    tmp.deleteOnExit();
    HANDLE hFile = Kernel32.INSTANCE.CreateFile(tmp.getAbsolutePath(), WinNT.GENERIC_WRITE, WinNT.FILE_SHARE_WRITE, new WinBase.SECURITY_ATTRIBUTES(), WinNT.OPEN_EXISTING, WinNT.FILE_ATTRIBUTE_NORMAL, null);
    assertFalse(WinBase.INVALID_HANDLE_VALUE.equals(hFile));
    try {
        Memory p = new Memory(FILE_BASIC_INFO.sizeOf());
        if (false == Kernel32.INSTANCE.GetFileInformationByHandleEx(hFile, WinBase.FileBasicInfo, p, new DWORD(p.size()))) {
            fail("GetFileInformationByHandleEx failed with " + Kernel32.INSTANCE.GetLastError());
        }
        FILE_BASIC_INFO fbi = new FILE_BASIC_INFO(p);
        FILETIME ft = new FILETIME(fbi.LastWriteTime);
        SYSTEMTIME stUTC = new SYSTEMTIME();
        SYSTEMTIME stLocal = new SYSTEMTIME();
        Kernel32.INSTANCE.FileTimeToSystemTime(ft, stUTC);
        // Covert to local
        Kernel32.INSTANCE.SystemTimeToTzSpecificLocalTime(null, stUTC, stLocal);
        FileTime calculatedCreateTime = FileTime.fromMillis(stLocal.toCalendar().getTimeInMillis());
        // Actual file's createTime
        FileTime createTime = Files.getLastModifiedTime(Paths.get(tmp.getAbsolutePath()));
        assertEquals(createTime.toMillis(), calculatedCreateTime.toMillis());
    } finally {
        Kernel32.INSTANCE.CloseHandle(hFile);
    }
}
Also used : FILETIME(com.sun.jna.platform.win32.WinBase.FILETIME) Memory(com.sun.jna.Memory) SYSTEMTIME(com.sun.jna.platform.win32.WinBase.SYSTEMTIME) DWORD(com.sun.jna.platform.win32.WinDef.DWORD) FileTime(java.nio.file.attribute.FileTime) File(java.io.File) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE) FILE_BASIC_INFO(com.sun.jna.platform.win32.WinBase.FILE_BASIC_INFO)

Example 2 with FILE_BASIC_INFO

use of com.sun.jna.platform.win32.WinBase.FILE_BASIC_INFO in project jna by java-native-access.

the class Kernel32Test method testGetFileInformationByHandleEx.

public void testGetFileInformationByHandleEx() throws IOException {
    File tmp = File.createTempFile("testGetFileInformationByHandleEx", "jna");
    tmp.deleteOnExit();
    HANDLE hFile = Kernel32.INSTANCE.CreateFile(tmp.getAbsolutePath(), WinNT.GENERIC_WRITE, WinNT.FILE_SHARE_WRITE, new WinBase.SECURITY_ATTRIBUTES(), WinNT.OPEN_EXISTING, WinNT.FILE_ATTRIBUTE_NORMAL, null);
    assertFalse(WinBase.INVALID_HANDLE_VALUE.equals(hFile));
    try {
        Memory p = new Memory(FILE_BASIC_INFO.sizeOf());
        if (false == Kernel32.INSTANCE.GetFileInformationByHandleEx(hFile, WinBase.FileBasicInfo, p, new DWORD(p.size()))) {
            fail("GetFileInformationByHandleEx failed with " + Kernel32.INSTANCE.GetLastError());
        }
        FILE_BASIC_INFO fbi = new FILE_BASIC_INFO(p);
        // New file has non-zero creation time
        assertTrue(0 != fbi.CreationTime.getValue());
        p = new Memory(FILE_STANDARD_INFO.sizeOf());
        if (false == Kernel32.INSTANCE.GetFileInformationByHandleEx(hFile, WinBase.FileStandardInfo, p, new DWORD(p.size()))) {
            fail("GetFileInformationByHandleEx failed with " + Kernel32.INSTANCE.GetLastError());
        }
        FILE_STANDARD_INFO fsi = new FILE_STANDARD_INFO(p);
        // New file has 1 link
        assertEquals(1, fsi.NumberOfLinks);
        p = new Memory(FILE_COMPRESSION_INFO.sizeOf());
        if (false == Kernel32.INSTANCE.GetFileInformationByHandleEx(hFile, WinBase.FileCompressionInfo, p, new DWORD(p.size()))) {
            fail("GetFileInformationByHandleEx failed with " + Kernel32.INSTANCE.GetLastError());
        }
        FILE_COMPRESSION_INFO fci = new FILE_COMPRESSION_INFO(p);
        // Uncompressed file should be zero
        assertEquals(0, fci.CompressionFormat);
        p = new Memory(FILE_ATTRIBUTE_TAG_INFO.sizeOf());
        if (false == Kernel32.INSTANCE.GetFileInformationByHandleEx(hFile, WinBase.FileAttributeTagInfo, p, new DWORD(p.size()))) {
            fail("GetFileInformationByHandleEx failed with " + Kernel32.INSTANCE.GetLastError());
        }
        FILE_ATTRIBUTE_TAG_INFO fati = new FILE_ATTRIBUTE_TAG_INFO(p);
        // New files have the archive bit
        assertEquals(WinNT.FILE_ATTRIBUTE_ARCHIVE, fati.FileAttributes);
        p = new Memory(FILE_ID_INFO.sizeOf());
        if (false == Kernel32.INSTANCE.GetFileInformationByHandleEx(hFile, WinBase.FileIdInfo, p, new DWORD(p.size()))) {
            fail("GetFileInformationByHandleEx failed with " + Kernel32.INSTANCE.GetLastError());
        }
        FILE_ID_INFO fii = new FILE_ID_INFO(p);
        // Volume serial number should be non-zero
        assertFalse(fii.VolumeSerialNumber == 0);
    } finally {
        Kernel32.INSTANCE.CloseHandle(hFile);
    }
}
Also used : FILE_COMPRESSION_INFO(com.sun.jna.platform.win32.WinBase.FILE_COMPRESSION_INFO) FILE_ATTRIBUTE_TAG_INFO(com.sun.jna.platform.win32.WinBase.FILE_ATTRIBUTE_TAG_INFO) Memory(com.sun.jna.Memory) FILE_STANDARD_INFO(com.sun.jna.platform.win32.WinBase.FILE_STANDARD_INFO) DWORD(com.sun.jna.platform.win32.WinDef.DWORD) FILE_ID_INFO(com.sun.jna.platform.win32.WinBase.FILE_ID_INFO) File(java.io.File) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE) FILE_BASIC_INFO(com.sun.jna.platform.win32.WinBase.FILE_BASIC_INFO)

Example 3 with FILE_BASIC_INFO

use of com.sun.jna.platform.win32.WinBase.FILE_BASIC_INFO in project jna by java-native-access.

the class Kernel32Test method testSetFileInformationByHandleFileBasicInfo.

public void testSetFileInformationByHandleFileBasicInfo() throws IOException, InterruptedException {
    File tmp = File.createTempFile("testSetFileInformationByHandleFileBasicInfo", "jna");
    tmp.deleteOnExit();
    HANDLE hFile = Kernel32.INSTANCE.CreateFile(tmp.getAbsolutePath(), WinNT.GENERIC_READ | WinNT.GENERIC_WRITE, WinNT.FILE_SHARE_READ | WinNT.FILE_SHARE_WRITE, new WinBase.SECURITY_ATTRIBUTES(), WinNT.OPEN_EXISTING, WinNT.FILE_ATTRIBUTE_NORMAL, null);
    assertFalse(WinBase.INVALID_HANDLE_VALUE.equals(hFile));
    try {
        Memory p = new Memory(FILE_BASIC_INFO.sizeOf());
        if (false == Kernel32.INSTANCE.GetFileInformationByHandleEx(hFile, WinBase.FileBasicInfo, p, new DWORD(p.size())))
            fail("GetFileInformationByHandleEx failed with " + Kernel32.INSTANCE.GetLastError());
        FILE_BASIC_INFO fbi = new FILE_BASIC_INFO(p);
        // Add TEMP attribute
        fbi.FileAttributes = fbi.FileAttributes | WinNT.FILE_ATTRIBUTE_TEMPORARY;
        fbi.ChangeTime = new WinNT.LARGE_INTEGER(0);
        fbi.CreationTime = new WinNT.LARGE_INTEGER(0);
        fbi.LastAccessTime = new WinNT.LARGE_INTEGER(0);
        fbi.LastWriteTime = new WinNT.LARGE_INTEGER(0);
        fbi.write();
        if (false == Kernel32.INSTANCE.SetFileInformationByHandle(hFile, WinBase.FileBasicInfo, fbi.getPointer(), new DWORD(FILE_BASIC_INFO.sizeOf())))
            fail("GetFileInformationByHandleEx failed with " + Kernel32.INSTANCE.GetLastError());
        if (false == Kernel32.INSTANCE.GetFileInformationByHandleEx(hFile, WinBase.FileBasicInfo, p, new DWORD(p.size())))
            fail("GetFileInformationByHandleEx failed with " + Kernel32.INSTANCE.GetLastError());
        fbi = new FILE_BASIC_INFO(p);
        assertTrue((fbi.FileAttributes & WinNT.FILE_ATTRIBUTE_TEMPORARY) != 0);
    } finally {
        Kernel32.INSTANCE.CloseHandle(hFile);
    }
}
Also used : Memory(com.sun.jna.Memory) DWORD(com.sun.jna.platform.win32.WinDef.DWORD) File(java.io.File) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE) FILE_BASIC_INFO(com.sun.jna.platform.win32.WinBase.FILE_BASIC_INFO)

Aggregations

Memory (com.sun.jna.Memory)3 FILE_BASIC_INFO (com.sun.jna.platform.win32.WinBase.FILE_BASIC_INFO)3 DWORD (com.sun.jna.platform.win32.WinDef.DWORD)3 HANDLE (com.sun.jna.platform.win32.WinNT.HANDLE)3 File (java.io.File)3 FILETIME (com.sun.jna.platform.win32.WinBase.FILETIME)1 FILE_ATTRIBUTE_TAG_INFO (com.sun.jna.platform.win32.WinBase.FILE_ATTRIBUTE_TAG_INFO)1 FILE_COMPRESSION_INFO (com.sun.jna.platform.win32.WinBase.FILE_COMPRESSION_INFO)1 FILE_ID_INFO (com.sun.jna.platform.win32.WinBase.FILE_ID_INFO)1 FILE_STANDARD_INFO (com.sun.jna.platform.win32.WinBase.FILE_STANDARD_INFO)1 SYSTEMTIME (com.sun.jna.platform.win32.WinBase.SYSTEMTIME)1 FileTime (java.nio.file.attribute.FileTime)1