1 ! Copyright (C) 2008 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: system io.directories alien.strings
4 io.pathnames io.backend io.files.windows destructors
5 kernel accessors calendar windows windows.errors
6 windows.kernel32 alien.c-types sequences splitting
7 fry continuations classes.struct ;
8 IN: io.directories.windows
10 M: windows touch-file ( path -- )
13 maybe-create-file [ &dispose ] dip
14 [ drop ] [ handle>> f now dup (set-file-times) ] if
17 M: windows move-file ( from to -- )
18 [ normalize-path ] bi@ MoveFile win32-error=0/f ;
20 ERROR: file-delete-failed path error ;
22 : delete-file-throws ( path -- )
23 DeleteFile win32-error=0/f ;
25 : delete-read-only-file ( path -- )
26 [ set-file-normal-attribute ] [ delete-file-throws ] bi ;
28 : (delete-file) ( path -- )
30 GetLastError ERROR_ACCESS_DENIED =
31 [ delete-read-only-file ] [ throw-win32-error ] if
34 M: windows delete-file ( path -- )
37 [ \ file-delete-failed boa rethrow ] recover ;
39 M: windows make-directory ( path -- )
41 f CreateDirectory win32-error=0/f ;
43 M: windows delete-directory ( path -- )
45 RemoveDirectory win32-error=0/f ;
47 : find-first-file ( path WIN32_FIND_DATA -- WIN32_FIND_DATA HANDLE )
48 [ nip ] [ FindFirstFile ] 2bi
49 [ INVALID_HANDLE_VALUE = [ win32-error-string throw ] when ] keep ;
51 : find-next-file ( HANDLE WIN32_FIND_DATA -- WIN32_FIND_DATA/f )
52 [ nip ] [ FindNextFile ] 2bi 0 = [
53 GetLastError ERROR_NO_MORE_FILES = [
58 TUPLE: windows-directory-entry < directory-entry attributes ;
60 C: <windows-directory-entry> windows-directory-entry
62 : >windows-directory-entry ( WIN32_FIND_DATA -- directory-entry )
63 [ cFileName>> alien>native-string ]
66 [ win32-file-type ] [ win32-file-attributes ] bi
68 dupd remove <windows-directory-entry> ; inline
70 M: windows (directory-entries) ( path -- seq )
71 "\\" ?tail drop "\\*" append
72 WIN32_FIND_DATA <struct>
74 [ >windows-directory-entry ] 2dip
77 [ _ _ find-next-file dup ]
78 [ >windows-directory-entry ]
80 over name>> "." = [ nip ] [ swap prefix ] if
82 ] [ drop '[ _ FindClose win32-error=0/f ] ] 2bi [ ] cleanup ;