win32-file-stat 1.3.6 → 1.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGES +126 -103
- data/MANIFEST +10 -7
- data/README +85 -84
- data/Rakefile +31 -26
- data/lib/win32/file/stat.rb +934 -755
- data/lib/win32/file/windows/constants.rb +92 -0
- data/lib/win32/file/windows/functions.rb +57 -0
- data/lib/win32/file/windows/helper.rb +13 -0
- data/lib/win32/file/windows/structs.rb +165 -0
- data/test/test_file_stat.rb +647 -354
- data/win32-file-stat.gemspec +28 -26
- metadata +63 -26
data/lib/win32/file/stat.rb
CHANGED
@@ -1,755 +1,934 @@
|
|
1
|
-
require 'windows
|
2
|
-
require 'windows
|
3
|
-
require 'windows
|
4
|
-
require 'windows
|
5
|
-
require '
|
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
|
-
# Returns whether or not the file is a
|
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
|
-
#
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
#
|
311
|
-
#
|
312
|
-
#
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
#
|
318
|
-
#
|
319
|
-
def
|
320
|
-
@
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
#
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
#
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
#
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
#
|
357
|
-
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
#
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
#
|
381
|
-
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
#
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
|
394
|
-
#
|
395
|
-
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
#
|
402
|
-
|
403
|
-
|
404
|
-
|
405
|
-
|
406
|
-
|
407
|
-
|
408
|
-
#
|
409
|
-
|
410
|
-
|
411
|
-
|
412
|
-
|
413
|
-
|
414
|
-
#
|
415
|
-
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
|
420
|
-
|
421
|
-
|
422
|
-
|
423
|
-
|
424
|
-
|
425
|
-
|
426
|
-
|
427
|
-
|
428
|
-
#
|
429
|
-
|
430
|
-
|
431
|
-
|
432
|
-
|
433
|
-
|
434
|
-
|
435
|
-
#
|
436
|
-
|
437
|
-
|
438
|
-
|
439
|
-
|
440
|
-
|
441
|
-
|
442
|
-
|
443
|
-
|
444
|
-
|
445
|
-
|
446
|
-
|
447
|
-
|
448
|
-
#
|
449
|
-
#
|
450
|
-
|
451
|
-
|
452
|
-
|
453
|
-
|
454
|
-
|
455
|
-
|
456
|
-
|
457
|
-
|
458
|
-
#
|
459
|
-
|
460
|
-
|
461
|
-
|
462
|
-
|
463
|
-
|
464
|
-
|
465
|
-
|
466
|
-
|
467
|
-
|
468
|
-
|
469
|
-
|
470
|
-
|
471
|
-
|
472
|
-
|
473
|
-
|
474
|
-
|
475
|
-
|
476
|
-
|
477
|
-
|
478
|
-
|
479
|
-
|
480
|
-
|
481
|
-
|
482
|
-
|
483
|
-
|
484
|
-
|
485
|
-
|
486
|
-
|
487
|
-
|
488
|
-
|
489
|
-
|
490
|
-
|
491
|
-
|
492
|
-
|
493
|
-
|
494
|
-
|
495
|
-
|
496
|
-
|
497
|
-
#
|
498
|
-
|
499
|
-
|
500
|
-
|
501
|
-
|
502
|
-
|
503
|
-
|
504
|
-
|
505
|
-
|
506
|
-
|
507
|
-
|
508
|
-
|
509
|
-
|
510
|
-
|
511
|
-
|
512
|
-
|
513
|
-
|
514
|
-
|
515
|
-
|
516
|
-
|
517
|
-
|
518
|
-
|
519
|
-
|
520
|
-
|
521
|
-
|
522
|
-
|
523
|
-
|
524
|
-
|
525
|
-
|
526
|
-
|
527
|
-
|
528
|
-
|
529
|
-
|
530
|
-
|
531
|
-
|
532
|
-
|
533
|
-
|
534
|
-
|
535
|
-
|
536
|
-
|
537
|
-
|
538
|
-
|
539
|
-
|
540
|
-
|
541
|
-
|
542
|
-
|
543
|
-
|
544
|
-
|
545
|
-
|
546
|
-
|
547
|
-
|
548
|
-
|
549
|
-
|
550
|
-
|
551
|
-
|
552
|
-
}
|
553
|
-
|
554
|
-
|
555
|
-
|
556
|
-
|
557
|
-
#
|
558
|
-
#
|
559
|
-
|
560
|
-
|
561
|
-
|
562
|
-
|
563
|
-
|
564
|
-
|
565
|
-
|
566
|
-
|
567
|
-
|
568
|
-
|
569
|
-
|
570
|
-
|
571
|
-
|
572
|
-
|
573
|
-
|
574
|
-
|
575
|
-
|
576
|
-
|
577
|
-
|
578
|
-
|
579
|
-
|
580
|
-
|
581
|
-
|
582
|
-
|
583
|
-
|
584
|
-
|
585
|
-
|
586
|
-
|
587
|
-
|
588
|
-
end
|
589
|
-
|
590
|
-
#
|
591
|
-
|
592
|
-
|
593
|
-
|
594
|
-
|
595
|
-
|
596
|
-
|
597
|
-
|
598
|
-
|
599
|
-
|
600
|
-
|
601
|
-
|
602
|
-
|
603
|
-
|
604
|
-
|
605
|
-
|
606
|
-
|
607
|
-
|
608
|
-
|
609
|
-
|
610
|
-
|
611
|
-
|
612
|
-
|
613
|
-
|
614
|
-
|
615
|
-
|
616
|
-
|
617
|
-
|
618
|
-
|
619
|
-
|
620
|
-
|
621
|
-
|
622
|
-
|
623
|
-
|
624
|
-
|
625
|
-
|
626
|
-
|
627
|
-
|
628
|
-
|
629
|
-
|
630
|
-
|
631
|
-
|
632
|
-
|
633
|
-
|
634
|
-
|
635
|
-
|
636
|
-
|
637
|
-
|
638
|
-
|
639
|
-
|
640
|
-
|
641
|
-
|
642
|
-
|
643
|
-
|
644
|
-
|
645
|
-
|
646
|
-
|
647
|
-
|
648
|
-
|
649
|
-
|
650
|
-
|
651
|
-
|
652
|
-
|
653
|
-
|
654
|
-
|
655
|
-
|
656
|
-
|
657
|
-
|
658
|
-
|
659
|
-
|
660
|
-
|
661
|
-
|
662
|
-
|
663
|
-
|
664
|
-
|
665
|
-
|
666
|
-
|
667
|
-
|
668
|
-
|
669
|
-
|
670
|
-
|
671
|
-
|
672
|
-
|
673
|
-
|
674
|
-
|
675
|
-
|
676
|
-
|
677
|
-
|
678
|
-
|
679
|
-
|
680
|
-
|
681
|
-
|
682
|
-
|
683
|
-
|
684
|
-
|
685
|
-
|
686
|
-
|
687
|
-
|
688
|
-
|
689
|
-
|
690
|
-
|
691
|
-
|
692
|
-
|
693
|
-
|
694
|
-
|
695
|
-
|
696
|
-
|
697
|
-
|
698
|
-
|
699
|
-
|
700
|
-
|
701
|
-
|
702
|
-
|
703
|
-
|
704
|
-
|
705
|
-
|
706
|
-
|
707
|
-
|
708
|
-
|
709
|
-
|
710
|
-
|
711
|
-
|
712
|
-
|
713
|
-
|
714
|
-
|
715
|
-
|
716
|
-
|
717
|
-
|
718
|
-
|
719
|
-
|
720
|
-
|
721
|
-
|
722
|
-
|
723
|
-
|
724
|
-
|
725
|
-
|
726
|
-
|
727
|
-
|
728
|
-
|
729
|
-
|
730
|
-
|
731
|
-
|
732
|
-
|
733
|
-
|
734
|
-
|
735
|
-
|
736
|
-
|
737
|
-
|
738
|
-
|
739
|
-
|
740
|
-
end
|
741
|
-
|
742
|
-
|
743
|
-
|
744
|
-
|
745
|
-
|
746
|
-
|
747
|
-
|
748
|
-
|
749
|
-
|
750
|
-
|
751
|
-
|
752
|
-
|
753
|
-
|
754
|
-
|
755
|
-
end
|
1
|
+
require File.join(File.dirname(__FILE__), 'windows', 'helper')
|
2
|
+
require File.join(File.dirname(__FILE__), 'windows', 'constants')
|
3
|
+
require File.join(File.dirname(__FILE__), 'windows', 'structs')
|
4
|
+
require File.join(File.dirname(__FILE__), 'windows', 'functions')
|
5
|
+
require 'pp'
|
6
|
+
|
7
|
+
class File::Stat
|
8
|
+
include Windows::Stat::Constants
|
9
|
+
include Windows::Stat::Structs
|
10
|
+
include Windows::Stat::Functions
|
11
|
+
include Comparable
|
12
|
+
|
13
|
+
# We have to undefine these first in order to avoid redefinition warnings.
|
14
|
+
undef_method :atime, :ctime, :mtime, :blksize, :blockdev?, :blocks, :chardev?
|
15
|
+
undef_method :dev, :dev_major, :dev_minor, :directory?, :executable?
|
16
|
+
undef_method :executable_real?, :file?
|
17
|
+
undef_method :ftype, :gid, :grpowned?, :ino, :mode, :nlink, :owned?
|
18
|
+
undef_method :pipe?, :readable?, :readable_real?, :rdev, :rdev_major
|
19
|
+
undef_method :rdev_minor, :setuid?, :setgid?
|
20
|
+
undef_method :size, :size?, :socket?, :sticky?, :symlink?, :uid
|
21
|
+
undef_method :world_readable?, :world_writable?, :writable?, :writable_real?
|
22
|
+
undef_method :<=>, :inspect, :pretty_print, :zero?
|
23
|
+
|
24
|
+
# A Time object containing the last access time.
|
25
|
+
attr_reader :atime
|
26
|
+
|
27
|
+
# A Time object indicating when the file was last changed.
|
28
|
+
attr_reader :ctime
|
29
|
+
|
30
|
+
# A Time object containing the last modification time.
|
31
|
+
attr_reader :mtime
|
32
|
+
|
33
|
+
# The native filesystems' block size.
|
34
|
+
attr_reader :blksize
|
35
|
+
|
36
|
+
# The number of native filesystem blocks allocated for this file.
|
37
|
+
attr_reader :blocks
|
38
|
+
|
39
|
+
# The serial number of the file's volume.
|
40
|
+
attr_reader :rdev
|
41
|
+
|
42
|
+
# The file's unique identifier. Only valid for regular files.
|
43
|
+
attr_reader :ino
|
44
|
+
|
45
|
+
# Integer representing the permission bits of the file.
|
46
|
+
attr_reader :mode
|
47
|
+
|
48
|
+
# The number of hard links to the file.
|
49
|
+
attr_reader :nlink
|
50
|
+
|
51
|
+
# The size of the file in bytes.
|
52
|
+
attr_reader :size
|
53
|
+
|
54
|
+
# Nil on Windows
|
55
|
+
attr_reader :dev_major, :dev_minor, :rdev_major, :rdev_minor
|
56
|
+
|
57
|
+
# The version of the win32-file-stat library
|
58
|
+
WIN32_FILE_STAT_VERSION = '1.4.0'
|
59
|
+
|
60
|
+
# Creates and returns a File::Stat object, which encapsulate common status
|
61
|
+
# information for File objects on MS Windows sytems. The information is
|
62
|
+
# recorded at the moment the File::Stat object is created; changes made to
|
63
|
+
# the file after that point will not be reflected.
|
64
|
+
#
|
65
|
+
def initialize(file)
|
66
|
+
raise TypeError unless file.is_a?(String)
|
67
|
+
|
68
|
+
path = file.tr('/', "\\")
|
69
|
+
@path = path
|
70
|
+
|
71
|
+
@user_sid = get_file_sid(file, OWNER_SECURITY_INFORMATION)
|
72
|
+
@grp_sid = get_file_sid(file, GROUP_SECURITY_INFORMATION)
|
73
|
+
|
74
|
+
@uid = @user_sid.split('-').last.to_i
|
75
|
+
@gid = @grp_sid.split('-').last.to_i
|
76
|
+
|
77
|
+
@owned = @user_sid == get_current_process_sid(TokenUser)
|
78
|
+
@grpowned = @grp_sid == get_current_process_sid(TokenGroups)
|
79
|
+
|
80
|
+
begin
|
81
|
+
# The handle returned will be used by other functions
|
82
|
+
handle = get_handle(path)
|
83
|
+
|
84
|
+
@blockdev = get_blockdev(path)
|
85
|
+
@blksize = get_blksize(path)
|
86
|
+
|
87
|
+
if handle
|
88
|
+
@filetype = get_filetype(handle)
|
89
|
+
@chardev = @filetype == FILE_TYPE_CHAR
|
90
|
+
@regular = @filetype == FILE_TYPE_DISK
|
91
|
+
@pipe = @filetype == FILE_TYPE_PIPE
|
92
|
+
else
|
93
|
+
@chardev = false
|
94
|
+
@regular = false
|
95
|
+
@pipe = false
|
96
|
+
end
|
97
|
+
|
98
|
+
fpath = path.wincode
|
99
|
+
|
100
|
+
if handle == nil || ((@blockdev || @chardev || @pipe) && GetDriveType(fpath) != DRIVE_REMOVABLE)
|
101
|
+
data = WIN32_FIND_DATA.new
|
102
|
+
CloseHandle(handle) if handle
|
103
|
+
|
104
|
+
handle = FindFirstFile(fpath, data)
|
105
|
+
|
106
|
+
if handle == INVALID_HANDLE_VALUE
|
107
|
+
raise SystemCallError.new('FindFirstFile', FFI.errno)
|
108
|
+
end
|
109
|
+
|
110
|
+
FindClose(handle)
|
111
|
+
handle = nil
|
112
|
+
|
113
|
+
@nlink = 1 # Default from stat/wstat function.
|
114
|
+
@ino = nil
|
115
|
+
@rdev = nil
|
116
|
+
else
|
117
|
+
data = BY_HANDLE_FILE_INFORMATION.new
|
118
|
+
|
119
|
+
unless GetFileInformationByHandle(handle, data)
|
120
|
+
raise SystemCallError.new('GetFileInformationByHandle', FFI.errno)
|
121
|
+
end
|
122
|
+
|
123
|
+
@nlink = data[:nNumberOfLinks]
|
124
|
+
@ino = (data[:nFileIndexHigh] << 32) | data[:nFileIndexLow]
|
125
|
+
@rdev = data[:dwVolumeSerialNumber]
|
126
|
+
end
|
127
|
+
|
128
|
+
@readable = access_check(path, GENERIC_READ)
|
129
|
+
@readable_real = @readable
|
130
|
+
|
131
|
+
@writable = access_check(path, GENERIC_WRITE)
|
132
|
+
@writable_real = @writable
|
133
|
+
|
134
|
+
@world_readable = access_check_world(path, FILE_READ_DATA)
|
135
|
+
@world_writable = access_check_world(path, FILE_WRITE_DATA)
|
136
|
+
|
137
|
+
# Not supported and/or meaningless on MS Windows
|
138
|
+
@dev_major = nil
|
139
|
+
@dev_minor = nil
|
140
|
+
@rdev_major = nil
|
141
|
+
@rdev_minor = nil
|
142
|
+
@setgid = false
|
143
|
+
@setuid = false
|
144
|
+
@sticky = false
|
145
|
+
|
146
|
+
# Originally used GetBinaryType, but it only worked
|
147
|
+
# for .exe files, and it could return false positives.
|
148
|
+
@executable = %w[.bat .cmd .com .exe].include?(File.extname(@path).downcase)
|
149
|
+
|
150
|
+
# Set blocks equal to size / blksize, rounded up
|
151
|
+
case @blksize
|
152
|
+
when nil
|
153
|
+
@blocks = nil
|
154
|
+
when 0
|
155
|
+
@blocks = 0
|
156
|
+
else
|
157
|
+
@blocks = (data.size.to_f / @blksize.to_f).ceil
|
158
|
+
end
|
159
|
+
|
160
|
+
@attr = data[:dwFileAttributes]
|
161
|
+
@atime = Time.at(data.atime)
|
162
|
+
@ctime = Time.at(data.ctime)
|
163
|
+
@mtime = Time.at(data.mtime)
|
164
|
+
@size = data.size
|
165
|
+
|
166
|
+
@archive = @attr & FILE_ATTRIBUTE_ARCHIVE > 0
|
167
|
+
@compressed = @attr & FILE_ATTRIBUTE_COMPRESSED > 0
|
168
|
+
@directory = @attr & FILE_ATTRIBUTE_DIRECTORY > 0
|
169
|
+
@encrypted = @attr & FILE_ATTRIBUTE_ENCRYPTED > 0
|
170
|
+
@hidden = @attr & FILE_ATTRIBUTE_HIDDEN > 0
|
171
|
+
@indexed = @attr & ~FILE_ATTRIBUTE_NOT_CONTENT_INDEXED > 0
|
172
|
+
@normal = @attr & FILE_ATTRIBUTE_NORMAL > 0
|
173
|
+
@offline = @attr & FILE_ATTRIBUTE_OFFLINE > 0
|
174
|
+
@readonly = @attr & FILE_ATTRIBUTE_READONLY > 0
|
175
|
+
@reparse_point = @attr & FILE_ATTRIBUTE_REPARSE_POINT > 0
|
176
|
+
@sparse = @attr & FILE_ATTRIBUTE_SPARSE_FILE > 0
|
177
|
+
@system = @attr & FILE_ATTRIBUTE_SYSTEM > 0
|
178
|
+
@temporary = @attr & FILE_ATTRIBUTE_TEMPORARY > 0
|
179
|
+
|
180
|
+
@mode = get_mode
|
181
|
+
|
182
|
+
if @reparse_point
|
183
|
+
@symlink = get_symlink(path)
|
184
|
+
else
|
185
|
+
@symlink = false
|
186
|
+
end
|
187
|
+
ensure
|
188
|
+
CloseHandle(handle) if handle
|
189
|
+
end
|
190
|
+
end
|
191
|
+
|
192
|
+
## Comparable
|
193
|
+
|
194
|
+
# Compares two File::Stat objects using modification time.
|
195
|
+
#--
|
196
|
+
# Custom implementation necessary since we altered File::Stat.
|
197
|
+
#
|
198
|
+
def <=>(other)
|
199
|
+
@mtime.to_i <=> other.mtime.to_i
|
200
|
+
end
|
201
|
+
|
202
|
+
## Other
|
203
|
+
|
204
|
+
# Returns whether or not the file is an archive file.
|
205
|
+
#
|
206
|
+
def archive?
|
207
|
+
@archive
|
208
|
+
end
|
209
|
+
|
210
|
+
# Returns whether or not the file is a block device. For MS Windows a
|
211
|
+
# block device is a removable drive, cdrom or ramdisk.
|
212
|
+
#
|
213
|
+
def blockdev?
|
214
|
+
@blockdev
|
215
|
+
end
|
216
|
+
|
217
|
+
# Returns whether or not the file is a character device.
|
218
|
+
#
|
219
|
+
def chardev?
|
220
|
+
@chardev
|
221
|
+
end
|
222
|
+
|
223
|
+
# Returns whether or not the file is compressed.
|
224
|
+
#
|
225
|
+
def compressed?
|
226
|
+
@compressed
|
227
|
+
end
|
228
|
+
|
229
|
+
# Returns whether or not the file is a directory.
|
230
|
+
#
|
231
|
+
def directory?
|
232
|
+
@directory
|
233
|
+
end
|
234
|
+
|
235
|
+
# Returns whether or not the file in encrypted.
|
236
|
+
#
|
237
|
+
def encrypted?
|
238
|
+
@encrypted
|
239
|
+
end
|
240
|
+
|
241
|
+
# Returns whether or not the file is executable. Generally speaking, this
|
242
|
+
# means .bat, .cmd, .com, and .exe files.
|
243
|
+
#
|
244
|
+
def executable?
|
245
|
+
@executable
|
246
|
+
end
|
247
|
+
|
248
|
+
alias executable_real? executable?
|
249
|
+
|
250
|
+
# Returns whether or not the file is a regular file, as opposed to a pipe,
|
251
|
+
# socket, etc.
|
252
|
+
#
|
253
|
+
def file?
|
254
|
+
@regular
|
255
|
+
end
|
256
|
+
|
257
|
+
# Returns the user ID of the file. If full_sid is true, then the full
|
258
|
+
# string sid is returned instead.
|
259
|
+
#--
|
260
|
+
# The user id is the RID of the SID.
|
261
|
+
#
|
262
|
+
def gid(full_sid = false)
|
263
|
+
full_sid ? @grp_sid : @gid
|
264
|
+
end
|
265
|
+
|
266
|
+
# Returns true if the process owner's ID is the same as one of the file's groups.
|
267
|
+
#--
|
268
|
+
# Internally we're checking the process sid against the TokenGroups sid.
|
269
|
+
#
|
270
|
+
def grpowned?
|
271
|
+
@grpowned
|
272
|
+
end
|
273
|
+
|
274
|
+
# Returns whether or not the file is hidden.
|
275
|
+
#
|
276
|
+
def hidden?
|
277
|
+
@hidden
|
278
|
+
end
|
279
|
+
|
280
|
+
# Returns whether or not the file is content indexed.
|
281
|
+
#
|
282
|
+
def indexed?
|
283
|
+
@indexed
|
284
|
+
end
|
285
|
+
|
286
|
+
alias content_indexed? indexed?
|
287
|
+
|
288
|
+
# Returns whether or not the file is 'normal'. This is only true if
|
289
|
+
# virtually all other attributes are false.
|
290
|
+
#
|
291
|
+
def normal?
|
292
|
+
@normal
|
293
|
+
end
|
294
|
+
|
295
|
+
# Returns whether or not the file is offline.
|
296
|
+
#
|
297
|
+
def offline?
|
298
|
+
@offline
|
299
|
+
end
|
300
|
+
|
301
|
+
# Returns whether or not the current process owner is the owner of the file.
|
302
|
+
#--
|
303
|
+
# Internally we're checking the process sid against the owner's sid.
|
304
|
+
def owned?
|
305
|
+
@owned
|
306
|
+
end
|
307
|
+
|
308
|
+
# Returns the drive number of the disk containing the file, or -1 if there
|
309
|
+
# is no associated drive number.
|
310
|
+
#
|
311
|
+
# If the +letter+ option is true, returns the drive letter instead. If there
|
312
|
+
# is no drive letter, it will return nil.
|
313
|
+
#--
|
314
|
+
# This differs slightly from MRI in that it will return -1 if the path
|
315
|
+
# does not have a drive letter.
|
316
|
+
#
|
317
|
+
# Note: Bug in JRuby as of JRuby 1.7.8, which does not expand NUL properly.
|
318
|
+
#
|
319
|
+
def dev(letter = false)
|
320
|
+
fpath = File.expand_path(@path).wincode
|
321
|
+
num = PathGetDriveNumber(fpath)
|
322
|
+
|
323
|
+
if letter
|
324
|
+
if num == -1
|
325
|
+
nil
|
326
|
+
else
|
327
|
+
(num + 'A'.ord).chr + ':'
|
328
|
+
end
|
329
|
+
else
|
330
|
+
num
|
331
|
+
end
|
332
|
+
end
|
333
|
+
|
334
|
+
# Returns whether or not the file is readable by the process owner.
|
335
|
+
#--
|
336
|
+
# In Windows terms, we're checking for GENERIC_READ privileges.
|
337
|
+
#
|
338
|
+
def readable?
|
339
|
+
@readable
|
340
|
+
end
|
341
|
+
|
342
|
+
# A synonym for File::Stat#readable?
|
343
|
+
#
|
344
|
+
def readable_real?
|
345
|
+
@readable_real
|
346
|
+
end
|
347
|
+
|
348
|
+
# Returns whether or not the file is readonly.
|
349
|
+
#
|
350
|
+
def readonly?
|
351
|
+
@readonly
|
352
|
+
end
|
353
|
+
|
354
|
+
alias read_only? readonly?
|
355
|
+
|
356
|
+
# Returns whether or not the file is a pipe.
|
357
|
+
#
|
358
|
+
def pipe?
|
359
|
+
@pipe
|
360
|
+
end
|
361
|
+
|
362
|
+
alias socket? pipe?
|
363
|
+
|
364
|
+
# Returns whether or not the file is a reparse point.
|
365
|
+
#
|
366
|
+
def reparse_point?
|
367
|
+
@reparse_point
|
368
|
+
end
|
369
|
+
|
370
|
+
# Returns false on MS Windows.
|
371
|
+
#--
|
372
|
+
# I had to explicitly define this because of a bug in JRuby.
|
373
|
+
#
|
374
|
+
def setgid?
|
375
|
+
@setgid
|
376
|
+
end
|
377
|
+
|
378
|
+
# Returns false on MS Windows.
|
379
|
+
#--
|
380
|
+
# I had to explicitly define this because of a bug in JRuby.
|
381
|
+
#
|
382
|
+
def setuid?
|
383
|
+
@setuid
|
384
|
+
end
|
385
|
+
|
386
|
+
# Returns whether or not the file size is zero.
|
387
|
+
#
|
388
|
+
def size?
|
389
|
+
@size > 0 ? @size : nil
|
390
|
+
end
|
391
|
+
|
392
|
+
# Returns whether or not the file is a sparse file. In most cases a sparse
|
393
|
+
# file is an image file.
|
394
|
+
#
|
395
|
+
def sparse?
|
396
|
+
@sparse
|
397
|
+
end
|
398
|
+
|
399
|
+
# Returns false on MS Windows.
|
400
|
+
#--
|
401
|
+
# I had to explicitly define this because of a bug in JRuby.
|
402
|
+
#
|
403
|
+
def sticky?
|
404
|
+
@sticky
|
405
|
+
end
|
406
|
+
|
407
|
+
# Returns whether or not the file is a symlink.
|
408
|
+
#
|
409
|
+
def symlink?
|
410
|
+
@symlink
|
411
|
+
end
|
412
|
+
|
413
|
+
# Returns whether or not the file is a system file.
|
414
|
+
#
|
415
|
+
def system?
|
416
|
+
@system
|
417
|
+
end
|
418
|
+
|
419
|
+
# Returns whether or not the file is being used for temporary storage.
|
420
|
+
#
|
421
|
+
def temporary?
|
422
|
+
@temporary
|
423
|
+
end
|
424
|
+
|
425
|
+
# Returns the user ID of the file. If the +full_sid+ is true, then the
|
426
|
+
# full string sid is returned instead.
|
427
|
+
#--
|
428
|
+
# The user id is the RID of the SID.
|
429
|
+
#
|
430
|
+
def uid(full_sid = false)
|
431
|
+
full_sid ? @user_sid : @uid
|
432
|
+
end
|
433
|
+
|
434
|
+
# Returns whether or not the file is readable by others. Note that this
|
435
|
+
# merely returns true or false, not permission bits (or nil).
|
436
|
+
#--
|
437
|
+
# In Windows terms, this is checking the access right FILE_READ_DATA against
|
438
|
+
# the well-known SID "S-1-1-0", aka "Everyone".
|
439
|
+
#
|
440
|
+
#
|
441
|
+
def world_readable?
|
442
|
+
@world_readable
|
443
|
+
end
|
444
|
+
|
445
|
+
# Returns whether or not the file is writable by others. Note that this
|
446
|
+
# merely returns true or false, not permission bits (or nil).
|
447
|
+
#--
|
448
|
+
# In Windows terms, this is checking the access right FILE_WRITE_DATA against
|
449
|
+
# the well-known SID "S-1-1-0", aka "Everyone".
|
450
|
+
#
|
451
|
+
def world_writable?
|
452
|
+
@world_writable
|
453
|
+
end
|
454
|
+
|
455
|
+
# Returns whether or not the file is writable by the current process owner.
|
456
|
+
#--
|
457
|
+
# In Windows terms, we're checking for GENERIC_WRITE privileges.
|
458
|
+
#
|
459
|
+
def writable?
|
460
|
+
@writable
|
461
|
+
end
|
462
|
+
|
463
|
+
# A synonym for File::Stat#readable?
|
464
|
+
#
|
465
|
+
def writable_real?
|
466
|
+
@writable_real
|
467
|
+
end
|
468
|
+
|
469
|
+
# Returns whether or not the file size is zero.
|
470
|
+
#
|
471
|
+
def zero?
|
472
|
+
@size == 0
|
473
|
+
end
|
474
|
+
|
475
|
+
# Identifies the type of file. The return string is one of 'file',
|
476
|
+
# 'directory', 'characterSpecial', 'socket' or 'unknown'.
|
477
|
+
#
|
478
|
+
def ftype
|
479
|
+
return 'directory' if @directory
|
480
|
+
|
481
|
+
case @filetype
|
482
|
+
when FILE_TYPE_CHAR
|
483
|
+
'characterSpecial'
|
484
|
+
when FILE_TYPE_DISK
|
485
|
+
'file'
|
486
|
+
when FILE_TYPE_PIPE
|
487
|
+
'socket'
|
488
|
+
else
|
489
|
+
if blockdev?
|
490
|
+
'blockSpecial'
|
491
|
+
else
|
492
|
+
'unknown'
|
493
|
+
end
|
494
|
+
end
|
495
|
+
end
|
496
|
+
|
497
|
+
# Returns a stringified version of a File::Stat object.
|
498
|
+
#
|
499
|
+
def inspect
|
500
|
+
members = %w[
|
501
|
+
archive? atime blksize blockdev? blocks compressed? ctime dev
|
502
|
+
encrypted? gid hidden? indexed? ino mode mtime rdev nlink normal?
|
503
|
+
offline? readonly? reparse_point? size sparse? system? temporary?
|
504
|
+
uid
|
505
|
+
]
|
506
|
+
|
507
|
+
str = "#<#{self.class}"
|
508
|
+
|
509
|
+
members.sort.each{ |mem|
|
510
|
+
if mem == 'mode'
|
511
|
+
str << " #{mem}=" << sprintf("0%o", send(mem.intern))
|
512
|
+
elsif mem[-1].chr == '?' # boolean methods
|
513
|
+
str << " #{mem.chop}=" << send(mem.intern).to_s
|
514
|
+
else
|
515
|
+
str << " #{mem}=" << send(mem.intern).to_s
|
516
|
+
end
|
517
|
+
}
|
518
|
+
|
519
|
+
str
|
520
|
+
end
|
521
|
+
|
522
|
+
# A custom pretty print method. This was necessary not only to handle
|
523
|
+
# the additional attributes, but to work around an error caused by the
|
524
|
+
# builtin method for the current File::Stat class (see pp.rb).
|
525
|
+
#
|
526
|
+
def pretty_print(q)
|
527
|
+
members = %w[
|
528
|
+
archive? atime blksize blockdev? blocks compressed? ctime dev
|
529
|
+
encrypted? gid hidden? indexed? ino mode mtime rdev nlink normal?
|
530
|
+
offline? readonly? reparse_point? size sparse? system? temporary?
|
531
|
+
uid
|
532
|
+
]
|
533
|
+
|
534
|
+
q.object_group(self){
|
535
|
+
q.breakable
|
536
|
+
members.each{ |mem|
|
537
|
+
q.group{
|
538
|
+
q.text("#{mem}".ljust(15) + "=> ")
|
539
|
+
if mem == 'mode'
|
540
|
+
q.text(sprintf("0%o", send(mem.intern)))
|
541
|
+
else
|
542
|
+
val = self.send(mem.intern)
|
543
|
+
if val.nil?
|
544
|
+
q.text('nil')
|
545
|
+
else
|
546
|
+
q.text(val.to_s)
|
547
|
+
end
|
548
|
+
end
|
549
|
+
}
|
550
|
+
q.comma_breakable unless mem == members.last
|
551
|
+
}
|
552
|
+
}
|
553
|
+
end
|
554
|
+
|
555
|
+
private
|
556
|
+
|
557
|
+
# This is based on fileattr_to_unixmode in win32.c
|
558
|
+
#
|
559
|
+
def get_mode
|
560
|
+
mode = 0
|
561
|
+
|
562
|
+
s_iread = 0x0100; s_iwrite = 0x0080; s_iexec = 0x0040
|
563
|
+
s_ifreg = 0x8000; s_ifdir = 0x4000; s_iwusr = 0200
|
564
|
+
s_iwgrp = 0020; s_iwoth = 0002;
|
565
|
+
|
566
|
+
if @readonly
|
567
|
+
mode |= s_iread
|
568
|
+
else
|
569
|
+
mode |= s_iread | s_iwrite | s_iwusr
|
570
|
+
end
|
571
|
+
|
572
|
+
if @directory
|
573
|
+
mode |= s_ifdir | s_iexec
|
574
|
+
else
|
575
|
+
mode |= s_ifreg
|
576
|
+
end
|
577
|
+
|
578
|
+
if @executable
|
579
|
+
mode |= s_iexec
|
580
|
+
end
|
581
|
+
|
582
|
+
mode |= (mode & 0700) >> 3;
|
583
|
+
mode |= (mode & 0700) >> 6;
|
584
|
+
|
585
|
+
mode &= ~(s_iwgrp | s_iwoth)
|
586
|
+
|
587
|
+
mode
|
588
|
+
end
|
589
|
+
|
590
|
+
# Returns whether or not +path+ is a block device.
|
591
|
+
#
|
592
|
+
def get_blockdev(path)
|
593
|
+
ptr = FFI::MemoryPointer.from_string(path.wincode)
|
594
|
+
|
595
|
+
if PathStripToRoot(ptr)
|
596
|
+
fpath = ptr.read_bytes(path.size * 2).split("\000\000").first
|
597
|
+
else
|
598
|
+
fpath = nil
|
599
|
+
end
|
600
|
+
|
601
|
+
case GetDriveType(fpath)
|
602
|
+
when DRIVE_REMOVABLE, DRIVE_CDROM, DRIVE_RAMDISK
|
603
|
+
true
|
604
|
+
else
|
605
|
+
false
|
606
|
+
end
|
607
|
+
end
|
608
|
+
|
609
|
+
# Returns the blksize for +path+.
|
610
|
+
#---
|
611
|
+
# The jruby-ffi gem (as of 1.9.3) reports a failure here where it shouldn't.
|
612
|
+
# Consequently, this method returns 4096 automatically for now on JRuby.
|
613
|
+
#
|
614
|
+
def get_blksize(path)
|
615
|
+
return 4096 if RUBY_PLATFORM == 'java' # Bug in jruby-ffi
|
616
|
+
|
617
|
+
ptr = FFI::MemoryPointer.from_string(path.wincode)
|
618
|
+
|
619
|
+
if PathStripToRoot(ptr)
|
620
|
+
fpath = ptr.read_bytes(path.size * 2).split("\000\000").first
|
621
|
+
else
|
622
|
+
fpath = nil
|
623
|
+
end
|
624
|
+
|
625
|
+
size = nil
|
626
|
+
|
627
|
+
sectors = FFI::MemoryPointer.new(:ulong)
|
628
|
+
bytes = FFI::MemoryPointer.new(:ulong)
|
629
|
+
free = FFI::MemoryPointer.new(:ulong)
|
630
|
+
total = FFI::MemoryPointer.new(:ulong)
|
631
|
+
|
632
|
+
if GetDiskFreeSpace(fpath, sectors, bytes, free, total)
|
633
|
+
size = sectors.read_ulong * bytes.read_ulong
|
634
|
+
else
|
635
|
+
unless PathIsUNC(fpath)
|
636
|
+
raise SystemCallError.new('GetDiskFreeSpace', FFI.errno)
|
637
|
+
end
|
638
|
+
end
|
639
|
+
|
640
|
+
size
|
641
|
+
end
|
642
|
+
|
643
|
+
# Generic method for retrieving a handle.
|
644
|
+
#
|
645
|
+
def get_handle(path)
|
646
|
+
fpath = path.wincode
|
647
|
+
|
648
|
+
handle = CreateFile(
|
649
|
+
fpath,
|
650
|
+
GENERIC_READ,
|
651
|
+
FILE_SHARE_READ,
|
652
|
+
nil,
|
653
|
+
OPEN_EXISTING,
|
654
|
+
FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT,
|
655
|
+
0
|
656
|
+
)
|
657
|
+
|
658
|
+
if handle == INVALID_HANDLE_VALUE
|
659
|
+
return nil if FFI.errno == 32 # ERROR_SHARING_VIOLATION. Locked files.
|
660
|
+
raise SystemCallError.new('CreateFile', FFI.errno)
|
661
|
+
end
|
662
|
+
|
663
|
+
handle
|
664
|
+
end
|
665
|
+
|
666
|
+
# Determines whether or not +file+ is a symlink.
|
667
|
+
#
|
668
|
+
def get_symlink(file)
|
669
|
+
bool = false
|
670
|
+
fpath = File.expand_path(file).wincode
|
671
|
+
|
672
|
+
begin
|
673
|
+
data = WIN32_FIND_DATA.new
|
674
|
+
handle = FindFirstFile(fpath, data)
|
675
|
+
|
676
|
+
if handle == INVALID_HANDLE_VALUE
|
677
|
+
raise SystemCallError.new('FindFirstFile', FFI.errno)
|
678
|
+
end
|
679
|
+
|
680
|
+
if data[:dwReserved0] == IO_REPARSE_TAG_SYMLINK
|
681
|
+
bool = true
|
682
|
+
end
|
683
|
+
ensure
|
684
|
+
FindClose(handle) if handle
|
685
|
+
end
|
686
|
+
|
687
|
+
bool
|
688
|
+
end
|
689
|
+
|
690
|
+
# Returns the filetype for the given +handle+.
|
691
|
+
#
|
692
|
+
def get_filetype(handle)
|
693
|
+
file_type = GetFileType(handle)
|
694
|
+
|
695
|
+
if file_type == FILE_TYPE_UNKNOWN && FFI.errno != NO_ERROR
|
696
|
+
raise SystemCallError.new('GetFileType', FFI.errno)
|
697
|
+
end
|
698
|
+
|
699
|
+
file_type
|
700
|
+
end
|
701
|
+
|
702
|
+
# Return a sid of the file's owner.
|
703
|
+
#
|
704
|
+
def get_file_sid(file, info)
|
705
|
+
wfile = file.wincode
|
706
|
+
size_needed_ptr = FFI::MemoryPointer.new(:ulong)
|
707
|
+
|
708
|
+
# First pass, get the size needed
|
709
|
+
bool = GetFileSecurity(wfile, info, nil, 0, size_needed_ptr)
|
710
|
+
|
711
|
+
size_needed = size_needed_ptr.read_ulong
|
712
|
+
security_ptr = FFI::MemoryPointer.new(size_needed)
|
713
|
+
|
714
|
+
# Second pass, this time with the appropriately sized security pointer
|
715
|
+
bool = GetFileSecurity(wfile, info, security_ptr, security_ptr.size, size_needed_ptr)
|
716
|
+
|
717
|
+
unless bool
|
718
|
+
error = FFI.errno
|
719
|
+
return "S-1-5-80-0" if error == 32 # ERROR_SHARING_VIOLATION. Locked files, etc.
|
720
|
+
raise SystemCallError.new("GetFileSecurity", error)
|
721
|
+
end
|
722
|
+
|
723
|
+
sid_ptr = FFI::MemoryPointer.new(:pointer)
|
724
|
+
defaulted = FFI::MemoryPointer.new(:bool)
|
725
|
+
|
726
|
+
if info == OWNER_SECURITY_INFORMATION
|
727
|
+
bool = GetSecurityDescriptorOwner(security_ptr, sid_ptr, defaulted)
|
728
|
+
meth = "GetSecurityDescriptorOwner"
|
729
|
+
else
|
730
|
+
bool = GetSecurityDescriptorGroup(security_ptr, sid_ptr, defaulted)
|
731
|
+
meth = "GetSecurityDescriptorGroup"
|
732
|
+
end
|
733
|
+
|
734
|
+
raise SystemCallError.new(meth, FFI.errno) unless bool
|
735
|
+
|
736
|
+
ptr = FFI::MemoryPointer.new(:string)
|
737
|
+
|
738
|
+
unless ConvertSidToStringSid(sid_ptr.read_pointer, ptr)
|
739
|
+
raise SystemCallError.new("ConvertSidToStringSid")
|
740
|
+
end
|
741
|
+
|
742
|
+
ptr.read_pointer.read_string
|
743
|
+
end
|
744
|
+
|
745
|
+
# Return the sid of the current process.
|
746
|
+
#
|
747
|
+
def get_current_process_sid(token_type)
|
748
|
+
token = FFI::MemoryPointer.new(:uintptr_t)
|
749
|
+
sid = nil
|
750
|
+
|
751
|
+
begin
|
752
|
+
# Get the current process sid
|
753
|
+
unless OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, token)
|
754
|
+
raise SystemCallError.new("OpenProcessToken", FFI.errno)
|
755
|
+
end
|
756
|
+
|
757
|
+
token = token.read_pointer.to_i
|
758
|
+
rlength = FFI::MemoryPointer.new(:pointer)
|
759
|
+
|
760
|
+
if token_type == TokenUser
|
761
|
+
buf = 0.chr * 512
|
762
|
+
else
|
763
|
+
buf = TOKEN_GROUP.new
|
764
|
+
end
|
765
|
+
|
766
|
+
unless GetTokenInformation(token, token_type, buf, buf.size, rlength)
|
767
|
+
raise SystemCallError.new("GetTokenInformation", FFI.errno)
|
768
|
+
end
|
769
|
+
|
770
|
+
if token_type == TokenUser
|
771
|
+
tsid = buf[FFI.type_size(:pointer)*2, (rlength.read_ulong - FFI.type_size(:pointer)*2)]
|
772
|
+
else
|
773
|
+
tsid = buf[:Groups][0][:Sid]
|
774
|
+
end
|
775
|
+
|
776
|
+
ptr = FFI::MemoryPointer.new(:string)
|
777
|
+
|
778
|
+
unless ConvertSidToStringSid(tsid, ptr)
|
779
|
+
raise SystemCallError.new("ConvertSidToStringSid")
|
780
|
+
end
|
781
|
+
|
782
|
+
sid = ptr.read_pointer.read_string
|
783
|
+
ensure
|
784
|
+
CloseHandle(token) if token
|
785
|
+
end
|
786
|
+
|
787
|
+
sid
|
788
|
+
end
|
789
|
+
|
790
|
+
# Returns whether or not the current process has given access rights for +path+.
|
791
|
+
#
|
792
|
+
def access_check(path, access_rights)
|
793
|
+
wfile = path.wincode
|
794
|
+
check = false
|
795
|
+
size_needed_ptr = FFI::MemoryPointer.new(:ulong)
|
796
|
+
|
797
|
+
flags = OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION |
|
798
|
+
DACL_SECURITY_INFORMATION
|
799
|
+
|
800
|
+
# First attempt, get the size needed
|
801
|
+
bool = GetFileSecurity(wfile, flags, nil, 0, size_needed_ptr)
|
802
|
+
|
803
|
+
# If it fails horribly here, assume the answer is no.
|
804
|
+
if !bool && FFI.errno != ERROR_INSUFFICIENT_BUFFER
|
805
|
+
return false
|
806
|
+
end
|
807
|
+
|
808
|
+
size_needed = size_needed_ptr.read_ulong
|
809
|
+
security_ptr = FFI::MemoryPointer.new(size_needed)
|
810
|
+
|
811
|
+
# Second attempt, now with the needed size
|
812
|
+
if GetFileSecurity(wfile, flags, security_ptr, size_needed, size_needed_ptr)
|
813
|
+
token = FFI::MemoryPointer.new(:uintptr_t)
|
814
|
+
|
815
|
+
pflags = TOKEN_IMPERSONATE | TOKEN_QUERY | TOKEN_DUPLICATE | STANDARD_RIGHTS_READ
|
816
|
+
|
817
|
+
if OpenProcessToken(GetCurrentProcess(), pflags, token)
|
818
|
+
begin
|
819
|
+
token = token.read_pointer.to_i
|
820
|
+
token2 = FFI::MemoryPointer.new(:uintptr_t)
|
821
|
+
|
822
|
+
if DuplicateToken(token, SecurityImpersonation, token2)
|
823
|
+
begin
|
824
|
+
token2 = token2.read_pointer.to_i
|
825
|
+
mapping = GENERIC_MAPPING.new
|
826
|
+
privileges = PRIVILEGE_SET.new
|
827
|
+
privileges[:PrivilegeCount] = 0
|
828
|
+
privileges_length = privileges.size
|
829
|
+
|
830
|
+
mapping[:GenericRead] = FILE_GENERIC_READ
|
831
|
+
mapping[:GenericWrite] = FILE_GENERIC_WRITE
|
832
|
+
mapping[:GenericExecute] = FILE_GENERIC_EXECUTE
|
833
|
+
mapping[:GenericAll] = FILE_ALL_ACCESS
|
834
|
+
|
835
|
+
rights_ptr = FFI::MemoryPointer.new(:ulong)
|
836
|
+
rights_ptr.write_ulong(access_rights)
|
837
|
+
|
838
|
+
MapGenericMask(rights_ptr, mapping)
|
839
|
+
rights = rights_ptr.read_ulong
|
840
|
+
|
841
|
+
result_ptr = FFI::MemoryPointer.new(:ulong)
|
842
|
+
privileges_length_ptr = FFI::MemoryPointer.new(:ulong)
|
843
|
+
privileges_length_ptr.write_ulong(privileges_length)
|
844
|
+
granted_access_ptr = FFI::MemoryPointer.new(:ulong)
|
845
|
+
|
846
|
+
bool = AccessCheck(
|
847
|
+
security_ptr,
|
848
|
+
token2,
|
849
|
+
rights,
|
850
|
+
mapping,
|
851
|
+
privileges,
|
852
|
+
privileges_length_ptr,
|
853
|
+
granted_access_ptr,
|
854
|
+
result_ptr
|
855
|
+
)
|
856
|
+
|
857
|
+
if bool
|
858
|
+
check = result_ptr.read_ulong == 1
|
859
|
+
else
|
860
|
+
raise SystemCallError.new('AccessCheck', FFI.errno)
|
861
|
+
end
|
862
|
+
ensure
|
863
|
+
CloseHandle(token2)
|
864
|
+
end
|
865
|
+
end
|
866
|
+
ensure
|
867
|
+
CloseHandle(token)
|
868
|
+
end
|
869
|
+
end
|
870
|
+
end
|
871
|
+
|
872
|
+
check
|
873
|
+
end
|
874
|
+
|
875
|
+
# Returns whether or not the Everyone has given access rights for +path+.
|
876
|
+
#
|
877
|
+
def access_check_world(path, access_rights)
|
878
|
+
wfile = path.wincode
|
879
|
+
check = false
|
880
|
+
size_needed_ptr = FFI::MemoryPointer.new(:ulong)
|
881
|
+
|
882
|
+
flags = DACL_SECURITY_INFORMATION
|
883
|
+
|
884
|
+
# First attempt, get the size needed
|
885
|
+
bool = GetFileSecurity(wfile, flags, nil, 0, size_needed_ptr)
|
886
|
+
|
887
|
+
# If it fails horribly here, assume the answer is no.
|
888
|
+
if !bool && FFI.errno != ERROR_INSUFFICIENT_BUFFER
|
889
|
+
return false
|
890
|
+
end
|
891
|
+
|
892
|
+
size_needed = size_needed_ptr.read_ulong
|
893
|
+
security_ptr = FFI::MemoryPointer.new(size_needed)
|
894
|
+
|
895
|
+
# Second attempt, now with the needed size
|
896
|
+
if GetFileSecurity(wfile, flags, security_ptr, size_needed, size_needed_ptr)
|
897
|
+
present_ptr = FFI::MemoryPointer.new(:ulong)
|
898
|
+
pdacl_ptr = FFI::MemoryPointer.new(:pointer)
|
899
|
+
defaulted_ptr = FFI::MemoryPointer.new(:ulong)
|
900
|
+
|
901
|
+
bool = GetSecurityDescriptorDacl(
|
902
|
+
security_ptr,
|
903
|
+
present_ptr,
|
904
|
+
pdacl_ptr,
|
905
|
+
defaulted_ptr
|
906
|
+
)
|
907
|
+
|
908
|
+
# If it fails, or the dacl isn't present, return false.
|
909
|
+
if !bool || present_ptr.read_ulong == 0
|
910
|
+
return false
|
911
|
+
end
|
912
|
+
|
913
|
+
pdacl = pdacl_ptr.read_pointer
|
914
|
+
psid_ptr = FFI::MemoryPointer.new(:pointer)
|
915
|
+
|
916
|
+
# S-1-1-0 is the well known SID for "Everyone".
|
917
|
+
ConvertStringSidToSid('S-1-1-0', psid_ptr)
|
918
|
+
|
919
|
+
psid = psid_ptr.read_pointer
|
920
|
+
trustee_ptr = FFI::MemoryPointer.new(TRUSTEE)
|
921
|
+
|
922
|
+
BuildTrusteeWithSid(trustee_ptr, psid)
|
923
|
+
|
924
|
+
rights_ptr = FFI::MemoryPointer.new(:ulong)
|
925
|
+
|
926
|
+
if GetEffectiveRightsFromAcl(pdacl, trustee_ptr, rights_ptr) == NO_ERROR
|
927
|
+
rights = rights_ptr.read_ulong
|
928
|
+
check = (rights & access_rights) == access_rights
|
929
|
+
end
|
930
|
+
end
|
931
|
+
|
932
|
+
check
|
933
|
+
end
|
934
|
+
end
|