File: windows.lisp

package info (click to toggle)
acl2 8.6%2Bdfsg-3
  • links: PTS
  • area: main
  • in suites: sid
  • size: 1,138,276 kB
  • sloc: lisp: 17,818,294; java: 125,359; python: 28,122; javascript: 23,458; cpp: 18,851; ansic: 11,569; perl: 7,678; xml: 5,591; sh: 3,978; makefile: 3,840; ruby: 2,633; yacc: 1,126; ml: 763; awk: 295; csh: 233; lex: 197; php: 178; tcl: 49; asm: 23; haskell: 17
file content (268 lines) | stat: -rw-r--r-- 9,552 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
(in-package #:org.shirakumo.file-attributes)

(defconstant CP-UTF8 65001)
(defconstant GENERIC-READ   #x80000000)
(defconstant GENERIC-WRITE  #x40000000)
(defconstant FILE-SHARE-ALL #x00000007)
(defconstant OPEN-EXISTING  3)
(defconstant FILE-ATTRIBUTE-NORMAL #x80)
(defconstant FILE-OBJECT #x1)
(defconstant OWNER-SECURITY-INFORMATION #x1)
(defconstant GROUP-SECURITY-INFORMATION #x2)

(cffi:defctype hfile :int)
(cffi:defctype byte :uint8)
(cffi:defctype word :uint16)
(cffi:defctype dword :uint32)

(cffi:defcstruct (filetime :conc-name filetime-)
  (low-date-time dword)
  (high-date-time dword))

(cffi:defcstruct (systemtime :conc-name systemtime-)
  (year word)
  (month word)
  (day-of-week word)
  (day word)
  (hour word)
  (minute word)
  (second word)
  (milliseconds word))

(cffi:defcstruct (sid :conc-name sid-)
  (revision byte)
  (authority-count byte)
  (identifier byte :count 6)
  (sub-authority dword))

(cffi:defcfun (filetime-to-systemtime "FileTimeToSystemTime") :bool
  (filetime :pointer)
  (systemtime :pointer))

(cffi:defcfun (systemtime-to-filetime "SystemTimeToFileTime") :bool
  (systemtime :pointer)
  (filetime :pointer))

(cffi:defcfun (get-file-time "GetFileTime") :bool
  (file hfile)
  (ctime :pointer)
  (atime :pointer)
  (mtime :pointer))

(cffi:defcfun (set-file-time "SetFileTime") :bool
  (file hfile)
  (ctime :pointer)
  (atime :pointer)
  (mtime :pointer))

(cffi:defcfun (create-file "CreateFileW") hfile
  (filename :pointer)
  (access dword)
  (share-mode dword)
  (security-attributes :pointer)
  (creation-disposition dword)
  (flags-and-attributes dword)
  (template hfile))

(cffi:defcfun (allocate-sid "AllocateAndInitializeSid") :bool
  (authority :pointer)
  (sub-authorities byte)
  (authority-0 dword)
  (authority-1 dword)
  (authority-2 dword)
  (authority-3 dword)
  (authority-4 dword)
  (authority-5 dword)
  (authority-6 dword)
  (authority-7 dword)
  (sid :pointer))

(cffi:defcfun (get-security-info "GetSecurityInfo") dword
  (handle hfile)
  (object-type :int)
  (security-info :int)
  (owner :pointer)
  (group :pointer)
  (dacl :pointer)
  (sacl :pointer)
  (security-descriptor :pointer))

(cffi:defcfun (set-security-info "SetSecurityInfo") dword
  (handle hfile)
  (object-type :int)
  (security-info :int)
  (owner :pointer)
  (group :pointer)
  (dacl :pointer)
  (sacl :pointer))

(cffi:defcfun (close-file "CloseHandle") :bool
  (object hfile))

(cffi:defcfun (local-free "LocalFree") :pointer
  (object :pointer))

(cffi:defcfun (get-file-attributes "GetFileAttributesW") dword
  (filename :pointer))

(cffi:defcfun (set-file-attributes "SetFileAttributesW") :bool
  (filename :pointer)
  (attributes dword))

(cffi:defcfun (wide-char-to-multi-byte "WideCharToMultiByte") :int
  (code-page :uint)
  (flags dword)
  (wide-char-str :pointer)
  (wide-char :int)
  (multi-byte-str :pointer)
  (multi-byte :int)
  (default-char :pointer)
  (used-default-char :pointer))

(cffi:defcfun (multi-byte-to-wide-char "MultiByteToWideChar") :int
  (code-page :uint)
  (flags dword)
  (multi-byte-str :pointer)
  (multi-byte :int)
  (wide-char-str :pointer)
  (wide-char :int))

(defun wstring->string (pointer &optional (chars -1))
  (let ((bytes (wide-char-to-multi-byte CP-UTF8 0 pointer chars (cffi:null-pointer) 0 (cffi:null-pointer) (cffi:null-pointer))))
    (cffi:with-foreign-object (string :uchar bytes)
      (wide-char-to-multi-byte CP-UTF8 0 pointer chars string bytes (cffi:null-pointer) (cffi:null-pointer))
      (cffi:foreign-string-to-lisp string :encoding :utf-8))))

(defun string->wstring (string)
  (cffi:with-foreign-string (string string)
    (let* ((chars (multi-byte-to-wide-char CP-UTF8 0 string -1 (cffi:null-pointer) 0))
           (pointer (cffi:foreign-alloc :uint16 :count chars)))
      (multi-byte-to-wide-char CP-UTF8 0 string -1 pointer chars)
      pointer)))

(defun open-file (path mode)
  (let ((string (string->wstring (enpath path))))
    (unwind-protect
         (let ((handle (create-file string mode FILE-SHARE-ALL (cffi:null-pointer)
                                    OPEN-EXISTING FILE-ATTRIBUTE-NORMAL 0)))
           (if (/= -1 handle)
               handle
               (error "CreateFile failed.")))
      (cffi:foreign-free string))))

(defmacro with-file ((file path mode) &body body)
  `(let ((,file (open-file ,path ,mode)))
     (unwind-protect (progn ,@body)
       (close-file ,file))))

(defun filetime->universal (filetime)
  (cffi:with-foreign-object (systemtime '(:struct systemtime))
    (if (filetime-to-systemtime filetime systemtime)
        (encode-universal-time
         (systemtime-second systemtime)
         (systemtime-minute systemtime)
         (systemtime-hour systemtime)
         (systemtime-day systemtime)
         (systemtime-month systemtime)
         (systemtime-year systemtime) 0)
        (error "Failed to convert filetime."))))

(defun universal->filetime (universal filetime)
  (cffi:with-foreign-object (systemtime '(:struct systemtime))
    (multiple-value-bind (s m h dd mm yy) (decode-universal-time universal 0)
      (setf (systemtime-second systemtime) s)
      (setf (systemtime-minute systemtime) m)
      (setf (systemtime-hour systemtime) h)
      (setf (systemtime-day systemtime) dd)
      (setf (systemtime-month systemtime) mm)
      (setf (systemtime-year systemtime) yy))
    (if (systemtime-to-filetime systemtime filetime)
        filetime
        (error "Failed to convert filetime."))))

(defmacro define-time-reader (name args)
  `(define-implementation ,name (file)
     (with-file (file file GENERIC-READ)
       (cffi:with-foreign-object (filetime '(:struct filetime))
         (if (get-file-time file ,@args)
             (filetime->universal filetime)
             (error "GetFileTime failed."))))))

(defmacro define-time-writer (name args)
  `(define-implementation (setf ,name) (value file)
     (with-file (file file GENERIC-WRITE)
       (cffi:with-foreign-object (filetime '(:struct filetime))
         (universal->filetime value filetime)
         (if (set-file-time file ,@args)
             value
             (error "SetFileTime failed."))))))

(define-time-reader access-time ((cffi:null-pointer) filetime (cffi:null-pointer)))
(define-time-writer access-time ((cffi:null-pointer) filetime (cffi:null-pointer)))
(define-time-reader modification-time ((cffi:null-pointer) (cffi:null-pointer) filetime))
(define-time-writer modification-time ((cffi:null-pointer) (cffi:null-pointer) filetime))
(define-time-reader creation-time (filetime (cffi:null-pointer) (cffi:null-pointer)))
(define-time-writer creation-time (filetime (cffi:null-pointer) (cffi:null-pointer)))

(define-implementation user (file)
  (with-file (file file GENERIC-READ)
    (cffi:with-foreign-objects ((user '(:struct sid))
                                (attribs :pointer))
      (cond ((= 0 (get-security-info file FILE-OBJECT OWNER-SECURITY-INFORMATION user (cffi:null-pointer)
                                     (cffi:null-pointer) (cffi:null-pointer) attribs))
             (local-free (cffi:mem-ref attribs :pointer))
             (sid-sub-authority user))
            (T
             (error "GetSecurityInfo failed."))))))

(define-implementation (setf user) (user file)
  (with-file (file file GENERIC-WRITE)
    (cffi:with-foreign-objects ((user '(:struct sid))
                                (authority byte 6))
      (setf (cffi:mem-aref authority 'byte 6) 5)
      (unless (allocate-sid authority 1 user 0 0 0 0 0 0 0 user)
        (error "AllocateAndInitializeSid failed."))
      (unless (= 0 (set-security-info file FILE-OBJECT USER-SECURITY-INFORMATION user (cffi:null-pointer)
                                      (cffi:null-pointer) (cffi:null-pointer)))
        (error "SetSecurityInfo failed."))
      user)))

(define-implementation group (file)
  (with-file (file file GENERIC-READ)
    (cffi:with-foreign-objects ((group '(:struct sid))
                                (attribs :pointer))
      (cond ((= 0 (get-security-info file FILE-OBJECT GROUP-SECURITY-INFORMATION (cffi:null-pointer) group
                                     (cffi:null-pointer) (cffi:null-pointer) attribs))
             (local-free (cffi:mem-ref attribs :pointer))
             (sid-sub-authority group))
            (T
             (error "GetSecurityInfo failed."))))))

(define-implementation (setf group) (group file)
  (with-file (file file GENERIC-WRITE)
    (cffi:with-foreign-objects ((group '(:struct sid))
                                (authority byte 6))
      (setf (cffi:mem-aref authority 'byte 6) 5)
      (unless (allocate-sid authority 1 group 0 0 0 0 0 0 0 group)
        (error "AllocateAndInitializeSid failed."))
      (unless (= 0 (set-security-info file FILE-OBJECT GROUP-SECURITY-INFORMATION (cffi:null-pointer) group
                                      (cffi:null-pointer) (cffi:null-pointer)))
        (error "SetSecurityInfo failed."))
      group)))

(define-implementation attributes (file)
  (let ((string (string->wstring (enpath file))))
    (unwind-protect
         (let ((attributes (get-file-attributes string)))
           (if (= attributes #xFFFFFFFF)
               (error "GetFileAttributes failed.")
               attributes))
      (cffi:foreign-free string))))

(define-implementation (setf attributes) (value file)
  (let ((string (string->wstring (enpath file))))
    (unwind-protect
         (if (set-file-attributes string value)
             value
             (error "SetFileAttributes failed."))
      (cffi:foreign-free string))))