/
file.rb
585 lines (487 loc) · 15.6 KB
/
file.rb
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
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
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
require_relative 'file/constants'
require_relative 'file/structs'
require_relative 'file/functions'
require 'ffi/win32/extensions'
require 'win32/file/stat'
class File
include Windows::File::Constants
include Windows::File::Structs
extend Windows::File::Functions
# The version of the win32-file library
WIN32_FILE_VERSION = '0.8.2'
class << self
alias_method :join_orig, :join
alias_method :realpath_orig, :realpath
alias_method :realdirpath_orig, :realdirpath
remove_method :atime, :basename, :blockdev?, :chardev?, :ctime, :dirname
remove_method :directory?, :executable?, :executable_real?, :file?
remove_method :ftype, :grpowned?, :join, :lstat, :owned?, :pipe?, :socket?
remove_method :readable?, :readable_real?, :readlink, :realpath
remove_method :realdirpath
remove_method :split, :stat
remove_method :symlink
remove_method :symlink?
remove_method :world_readable?, :world_writable?
remove_method :writable?, :writable_real?
end
## Path methods
# Returns the last component of the filename given in +filename+. If
# +suffix+ is given and present at the end of +filename+, it is removed.
# Any extension can be removed by giving an extension of ".*".
#
# This was reimplemented because the current version does not handle UNC
# paths properly, i.e. it should not return anything less than the root.
# In most other respects it is identical to the current implementation,
# except that it does not strip the drive letter on a root path.
#
# Unlike MRI, this version will convert all forward slashes to
# backslashes automatically.
#
# Examples:
#
# File.basename("C:\\foo\\bar.txt") -> "bar.txt"
# File.basename("C:\\foo\\bar.txt", ".txt") -> "bar"
# File.basename("\\\\foo\\bar") -> "\\\\foo\\bar"
#
def self.basename(file, suffix = nil)
file = string_check(file)
suffix = string_check(suffix) if suffix
return file if file.empty? # Return an empty path as-is.
encoding = file.encoding
wfile = file.wincode
# Return a root path as-is.
if PathIsRootW(wfile)
return file.tr(File::ALT_SEPARATOR, File::SEPARATOR)
end
ptr = FFI::MemoryPointer.from_string(wfile)
PathStripPathW(ptr) # Gives us the basename
if suffix
if suffix == '.*'
PathRemoveExtensionW(ptr)
else
ext = PathFindExtensionW(ptr).read_string(suffix.length * 2).wstrip
if ext == suffix
PathRemoveExtensionW(ptr)
end
end
end
wfile = ptr.read_bytes(wfile.size * 2).split("\000\000").first.tr(0.chr, '')
file = wfile.encode(encoding)[/^[^\0]*/]
file.sub!(/\\+\z/, '') # Trim trailing slashes
ptr.free
file
end
# Returns all components of the filename given in +filename+ except the
# last one.
#
# This was reimplemented because the current version does not handle UNC
# paths properly, i.e. it should not return anything less than the root.
# In all other respects it is identical to the current implementation.
#
# Also, this method will convert all forward slashes to backslashes.
#
# Examples:
#
# File.dirname("C:\\foo\\bar\\baz.txt") -> "C:\\foo\\bar"
# File.dirname("\\\\foo\\bar") -> "\\\\foo\\bar"
#
def self.dirname(file)
file = string_check(file)
# Short circuit for empty paths
return '.' if file.empty?
# Store original encoding, restore it later
encoding = file.encoding
# Convert to UTF-16LE
wfile = file.wincode
# Return a root path as-is.
if PathIsRootW(wfile)
return file.tr(File::SEPARATOR, File::ALT_SEPARATOR)
end
ptr = FFI::MemoryPointer.from_string(wfile)
# Remove trailing slashes if present
while result = PathRemoveBackslashW(ptr)
break unless result.empty?
end
# Remove trailing file name if present
unless PathRemoveFileSpecW(ptr)
raise SystemCallError.new("PathRemoveFileSpec", FFI.errno)
end
wfile = ptr.read_bytes(wfile.size * 2).split("\000\000").first
# Empty paths, short relative paths
if wfile.nil? or wfile.empty?
return '.'
end
# Return to original encoding
file = wfile.tr(0.chr, '').encode(encoding)
ptr.free
file
end
# Join path string components together into a single string.
#
# This method was reimplemented so that it automatically converts
# forward slashes to backslashes. It is otherwise identical to
# the core File.join method.
#
# Examples:
#
# File.join("C:", "foo", "bar") # => C:\foo\bar
# File.join("foo", "bar") # => foo\bar
#
def self.join(*args)
return join_orig(*args).tr("/", "\\")
end
# Splits the given string into a directory and a file component and
# returns them in a two element array. This was reimplemented because
# the current version does not handle UNC paths properly.
#
def self.split(file)
file = string_check(file)
array = []
if file.empty? || PathIsRootW(file.wincode)
array.push(file, '')
else
array.push(File.dirname(file), File.basename(file))
end
array
end
# Returns +path+ in long format. For example, if 'SOMEFI~1.TXT'
# was the argument provided, and the short representation for
# 'somefile.txt', then this method would return 'somefile.txt'.
#
# Note that certain file system optimizations may prevent this method
# from working as expected. In that case, you will get back the file
# name in 8.3 format.
#
def self.long_path(file)
buffer = FFI::Buffer.new(:wint_t, 1024, true)
wfile = string_check(file).wincode
length = GetLongPathNameW(wfile, buffer, buffer.size)
if length == 0 || length > buffer.size / 2
raise SystemCallError.new('GetLongPathName', FFI.errno)
end
buffer.read_bytes(length * 2).wstrip
end
# Returns +path+ in 8.3 format. For example, 'c:\documentation.doc'
# would be returned as 'c:\docume~1.doc'.
#
def self.short_path(file)
buffer = FFI::Buffer.new(:wint_t, 1024, true)
wfile = string_check(file).wincode
length = GetShortPathNameW(wfile, buffer, buffer.size)
if length == 0 || length > buffer.size / 2
raise SystemCallError.new('GetShortPathName', FFI.errno)
end
buffer.read_bytes(length * 2).wstrip
end
# Creates a symbolic link called +new_name+ for the file or directory
# +old_name+.
#
# This method requires Windows Vista or later to work. Otherwise, it
# returns nil as per MRI.
#
def self.symlink(target, link)
target = string_check(target)
link = string_check(link)
flags = File.directory?(target) ? 1 : 0
wlink = link.wincode
wtarget = target.wincode
unless CreateSymbolicLinkW(wlink, wtarget, flags)
raise SystemCallError.new('CreateSymbolicLink', FFI.errno)
end
0 # Comply with spec
end
# Returns whether or not +file+ is a symlink.
#
def self.symlink?(file)
return false unless File.exist?(file)
bool = false
wfile = string_check(file).wincode
attrib = GetFileAttributesW(wfile)
if attrib == INVALID_FILE_ATTRIBUTES
raise SystemCallError.new('GetFileAttributes', FFI.errno)
end
if attrib & FILE_ATTRIBUTE_REPARSE_POINT > 0
begin
find_data = WIN32_FIND_DATA.new
handle = FindFirstFileW(wfile, find_data)
if handle == INVALID_HANDLE_VALUE
raise SystemCallError.new('FindFirstFile', FFI.errno)
end
if find_data[:dwReserved0] == IO_REPARSE_TAG_SYMLINK
bool = true
end
ensure
CloseHandle(handle)
end
end
bool
end
# Converts path to a full file path, with all symlinks resolved and relative
# paths made absolute. If a second parameter if present, it is used as the
# base for resolving leading relative path segments.
#
# Unlike File.realpath, an error is not raised if the final path created
# using a relative path argument doesn't exist.
#--
# On Windows we only modify the realpath method if the file is a symlink.
#
def self.realdirpath(file, relative_to = nil)
file = string_check(file)
if symlink?(file)
if relative_to
File.join(relative_to, File.basename(readlink(file)))
else
readlink(file)
end
else
realdirpath_orig(file, relative_to)
end
end
# Converts path to a full file path, with all symlinks resolved and relative
# paths made absolute. If a second parameter if present, it is used as the
# base for resolving leading relative path segments.
#--
# On Windows we only modify the realpath method if the file is a symlink.
#
def self.realpath(file, relative_to = nil)
file = string_check(file)
relative_to = string_check(relative_to) if relative_to
if symlink?(file)
if relative_to
result = File.join(relative_to, File.basename(readlink(file)))
if File.exist?(result)
result
else
raise SystemCallError.new(result, 2) # Errno::ENOENT
end
else
readlink(file)
end
else
realpath_orig(file, relative_to)
end
end
# Returns the path of the of the symbolic link referred to by +file+.
#
# Unlike unixy platforms, this will raise an error if the link is stale.
#
def self.readlink(file)
file = string_check(file)
if exist?(file) && !symlink?(file)
raise SystemCallError.new(22) # EINVAL, match the spec
end
wfile = file.wincode
path = 0.chr * 1024
if File.directory?(file)
flags = FILE_FLAG_BACKUP_SEMANTICS
else
flags = FILE_ATTRIBUTE_NORMAL
end
begin
handle = CreateFileW(
wfile,
GENERIC_READ,
FILE_SHARE_READ,
nil,
OPEN_EXISTING,
flags,
0
)
if handle == INVALID_HANDLE_VALUE
raise SystemCallError.new('CreateFile', FFI.errno)
end
if GetFinalPathNameByHandleW(handle, path, path.size/2, 0) == 0
raise SystemCallError.new('GetFinalPathNameByHandle', FFI.errno)
end
ensure
CloseHandle(handle)
end
path.wstrip[4..-1] # Remove leading backslashes + question mark
end
## STAT METHODS
# Returns the file's last access time. If a +time+ argument is provided, it
# sets the file's access time. The +time+ argument may be a Time object or
# a numeric value.
#
def self.atime(file, time = nil)
set_filetime(file, nil, time) if time
File::Stat.new(file).atime
end
# Returns the filesystem's block size.
#
def self.blksize(file)
File::Stat.new(file).blksize
end
# Returns whether or not the file is a block device. For MS Windows a
# block device is a removable drive, cdrom or ramdisk.
#
def self.blockdev?(file)
return false unless File.exist?(file)
File::Stat.new(file).blockdev?
end
# Returns whether or not the file is a character device.
#
def self.chardev?(file)
return false unless File.exist?(file)
File::Stat.new(file).chardev?
end
# Returns the file's creation time. If a +time+ argument is provided, it
# sets the file's creation time. The +time+ argument may be a Time object or
# a numeric value.
#
def self.ctime(file, time = nil)
set_filetime(file, time) if time
File::Stat.new(file).ctime
end
# Returns whether or not the file is a directory.
#
def self.directory?(file)
return false unless File.exist?(file)
File::Stat.new(file).directory?
end
# Returns whether or not the file is executable.
#
def self.executable?(file)
return false unless File.exist?(file)
File::Stat.new(file).executable?
end
# Returns whether or not the file is a regular file.
#
def self.file?(file)
return false unless File.exist?(file)
File::Stat.new(file).file?
end
# Identifies the type of file. The return string is one of 'file',
# 'directory', 'characterSpecial', 'socket' or 'unknown'.
#
def self.ftype(file)
File::Stat.new(file).ftype
end
# Returns true if the process owner's ID is the same as one of the file's groups.
#
def self.grpowned?(file)
return false unless File.exist?(file)
File::Stat.new(file).grpowned?
end
# Returns the file's creation time. If a +time+ argument is provided, it
# sets the file's creation time. The +time+ argument may be a Time object or
# a numeric value.
#
def self.mtime(file, time = nil)
set_filetime(file, nil, nil, time) if time
File::Stat.new(file).mtime
end
# Returns whether or not the current process owner is the owner of the file.
#
def self.owned?(file)
return false unless File.exist?(file)
File::Stat.new(file).owned?
end
# Returns whether or not the file is a pipe.
#
def self.pipe?(file)
return false unless File.exist?(file)
File::Stat.new(file).pipe?
end
# Returns whether or not the file is readable by the process owner.
#
def self.readable?(file)
return false unless File.exist?(file)
File::Stat.new(file).readable?
end
# Synonym for File.readable?
#
def self.readable_real?(file)
return false unless File.exist?(file)
File::Stat.new(file).readable_real?
end
# Returns a File::Stat object as defined in the win32-file-stat library.
#
def self.stat(file)
File::Stat.new(file)
end
# Returns whether or not the file is readable by others. Note that this
# merely returns true or false, not permission bits (or nil).
#
def self.world_readable?(file)
return false unless File.exist?(file)
File::Stat.new(file).world_readable?
end
# Returns whether or not the file is writable by others. Note that this
# merely returns true or false, not permission bits (or nil).
#
def self.world_writable?(file)
return false unless File.exist?(file)
File::Stat.new(file).world_writable?
end
# Returns whether or not the file is writable by the current process owner.
#
def self.writable?(file)
return false unless File.exist?(file)
File::Stat.new(file).writable?
end
# Synonym for File.writable?
#
def self.writable_real?(file)
return false unless File.exist?(file)
File::Stat.new(file).writable_real?
end
# Singleton aliases
class << self
alias lstat stat
alias executable_real? executable?
alias socket? pipe?
end
## Instance Methods
# Same as MRI, except it returns a stat object using the win32-file-stat gem.
#
def stat
File::Stat.new(self.path)
end
# Private singleton methods
private
# Simulate Ruby's string checking
def self.string_check(arg)
return arg if arg.is_a?(String)
return arg.send(:to_str) if arg.respond_to?(:to_str, true) # MRI allows private to_str
return arg.to_path if arg.respond_to?(:to_path)
raise TypeError
end
# Internal method for setting the file time.
def self.set_filetime(path, ctime = nil, atime = nil, mtime = nil)
begin
handle = CreateFileW(
path.wincode,
FILE_WRITE_ATTRIBUTES,
FILE_SHARE_READ | FILE_SHARE_WRITE,
nil,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0
)
if handle == INVALID_HANDLE_VALUE
raise SystemCallError.new('CreateFile', FFI.errno)
end
ftimes = [] # 0 = ctime, 1 = atime, 2 = mtime
[ctime, atime, mtime].each{ |time|
if time.nil?
ftimes << nil
next
else
systime = SYSTEMTIME.new(time)
ftime = FILETIME.new
if SystemTimeToFileTime(systime, ftime)
ftimes << ftime
else
raise SystemCallError.new('SystemTimetoFileTime', FFI.errno)
end
end
}
unless SetFileTime(handle, ftimes[0], ftimes[1], ftimes[2])
raise SystemCallError.new('SetFileTime', FFI.errno)
end
ensure
CloseHandle(handle) if handle
end
end
end