How to find out the age of a file in ASM?

Viewed 80

Greetings to all the geniuses of the digital age

Yesterday's task is still on the agenda: "Use the GetOpenFileName function to select a file. Check if the file is less than 3 days old, execute it. Otherwise, display a dialog asking to delete the file. If the user needs it, wipe." (Note: If a task is told to use a structure, it must be placed in dynamically allocated memory)"

I figured out how to open the file and now everything works like a Swiss watch. But another trouble arose. How to determine the age of a file?

In general, I first thought to use GetFileTime to extract all data about the file time, then use FileTimeToLocalFileTime to convert it to local time, and using FileTimeToSystemTime - to system time. Then subtract one from the other using sub, and so on as per the task.

Here, the FileTimeToLocalFileTime function requires a FILETIME structure with the following parameters:

DWORD dwLowDateTime;
DWORD dwHighDateTime;

And the FileTimeToSystemTime function requires a SYSTEMTIME structure with the following parameters:

WORD wYear;
WORD wMonth;
WORD wDayOfWeek;
WORD wDay;
WORD wHour;
WORD wMinute;
WORD wSecond;
WORD wMilliseconds;

And how to be then? How to find the age of a file? Is there any alternative way? And if my method works, then what exactly and from what should I take it?

File.inc

include WINDOWS.inc

include user32.inc
include kernel32.inc
include comdlg32.inc

includelib user32.lib
includelib kernel32.lib
includelib comdlg32.lib

.data

    Time_title  db ' Lab_3',0
    format db 'More than 3 days. Delete file?', 0
    buf db 255 dup(0)   
    hFile dd 0
    readed dd 0
    hmem dd 0

File.asm

.386
.model flat,STDCALL
option casemap :none  ;case sensitive
include Third.inc
include RADbg.inc

Mem_Alloc PROC Buf_Size:DWORD
    add Buf_Size,4  
    invoke GlobalAlloc,GMEM_MOVEABLE or GMEM_ZEROINIT,Buf_Size
    push eax 
    invoke GlobalLock,eax 
    pop [eax]
    add eax,4
Mem_Alloc endp


Mem_Free PROC DATA:DWORD
    mov eax,DATA
    sub eax,4   
    mov eax,[eax]   
    push eax        
    push eax
    call GlobalUnlock   
    call GlobalFree 
Mem_Free endp


.code
Begin:
    call main
    invoke ExitProcess,NULL

main proc
    LOCAL ftCreate, ftLocale: FILETIME;
    LOCAL stUTC, stLocal: SYSTEMTIME;

    invoke Mem_Alloc, 1000h
    mov hmem, eax
    invoke Mem_Alloc, sizeof OPENFILENAME
    mov edi, eax
    assume edi: ptr OPENFILENAME
    xor eax, eax
    mov [edi].lStructSize, sizeof OPENFILENAME
    mov [edi].lpstrFile, offset buf
    mov [edi].nMaxFile, 255
    invoke GetOpenFileName, edi
    invoke CreateFile, [edi].lpstrFile, GENERIC_READ,\
        FILE_SHARE_READ, NULL, OPEN_EXISTING,\
        FILE_ATTRIBUTE_NORMAL, NULL
    mov hFile,eax           ;
    invoke GetFileTime, hFile, addr ftCreate, NULL, NULL
    invoke FileTimeToLocalFileTime, addr ftCreate, addr ftLocale
    invoke FileTimeToSystemTime, addr ftCreate, addr stUTC

    cmp eax, 1
    jz l1
    invoke ReadFile, hFile, hmem, 1000h, addr readed, 0
    invoke MessageBox, 0, hmem, addr Time_title, MB_OKCANCEL

    jmp l2

l1:
    invoke MessageBox, 0, addr format, addr Time_title, MB_OKCANCEL
    cmp eax, IDOK
    jne l2
    invoke DeleteFile, addr [edi].lpstrFile

l2:

    assume edi: dword
    invoke CloseHandle, hFile
    invoke Mem_Free, hmem
    invoke Mem_Free, edi

    ret

main endp   
end Begin
1 Answers

As FILETIME and RtlTimeToSecondsSince1970 said, You should copy the low- and high-order parts of the file time to a ULARGE_INTEGER structure, perform 64-bit arithmetic on the QuadPart member.
So, subtract the 64-bit value in the ULARGE_INTEGER structure initialized with the file time from the 64-bit value of the ULARGE_INTEGER structure initialized with the current system time.

Related