Class: LogTwuncator::Win32File

Inherits:
Object
  • Object
show all
Defined in:
lib/log_twuncator/win32_file.rb

Constant Summary collapse

GENERIC_WRITE =
0x40000000
GENERIC_EXECUTE =
0x20000000
GENERIC_ALL =
0x10000000
FILE_SHARE_WRITE =
2
OPEN_EXISTING =
3
GetLocaltime =
Win32API.new("kernel32", "GetLocalTime", "P", 'V')
SystemTimeToFileTime =
Win32API.new("kernel32", "SystemTimeToFileTime", "PP", 'I')
CreateFile =
Win32API.new("kernel32", "CreateFileA", "PLLLLLL", "L")
SetFileTime =
Win32API.new("kernel32", "SetFileTime", "LPPP", "I")
CloseHandle =
Win32API.new("kernel32", "CloseHandle", "L", "I")

Class Method Summary collapse

Class Method Details

.get_file_time(time) ⇒ Object

Convert Ruby time into win32 FileTime



20
21
22
23
24
25
26
27
28
29
# File 'lib/log_twuncator/win32_file.rb', line 20

def self.get_file_time(time)
  pSYSTEMTIME = ' ' * 2 * 8 # 2byte x 8
  pFILETIME   = ' ' * 2 * 8 # 2byte x 8
  GetLocaltime.call(pSYSTEMTIME)
  time_arr = pSYSTEMTIME.unpack("S8")
  time_arr[0..1] = time.year, time.month
  time_arr[3..6] = time.day, time.hour, time.min, time.sec
  SystemTimeToFileTime.call(time_arr.pack("S8"), pFILETIME) 
  pFILETIME
end

.set_file_time(file, ctime = nil, atime = nil, mtime = nil) ⇒ Object

Set created, accessed and modified dates for a file



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/log_twuncator/win32_file.rb', line 32

def self.set_file_time(file, ctime=nil, atime=nil, mtime=nil)      
  hFile = 0
            
  times = [ctime, atime, mtime].collect do |time|        
    next( 0 ) if time.nil?        
    get_file_time(time.dup.utc)
  end
  
  hFile = CreateFile.call(file.dup, GENERIC_WRITE, FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0)
  res = SetFileTime.call(hFile, times[0], times[1], times[2])
  CloseHandle.call(hFile)
  return res
end