Class: File
- Inherits:
-
Object
- Object
- File
- Extended by:
- Windows::Error, Windows::File, Windows::Handle, Windows::Limits, Windows::MSVCRT::Buffer, Windows::Path, Windows::Security
- Includes:
- Windows::DeviceIO, Windows::Error, Windows::File, Windows::Handle, Windows::Limits, Windows::Security
- Defined in:
- lib/win32/file.rb
Constant Summary collapse
- WIN32_FILE_VERSION =
The version of the win32-file library
'0.6.7'
- ARCHIVE =
The file or directory is an archive. Typically used to mark files for backup or removal.
FILE_ATTRIBUTE_ARCHIVE
- COMPRESSED =
The file or directory is encrypted. For a file, this means that all data in the file is encrypted. For a directory, this means that encryption is # the default for newly created files and subdirectories.
FILE_ATTRIBUTE_COMPRESSED
- HIDDEN =
The file is hidden. Not included in an ordinary directory listing.
FILE_ATTRIBUTE_HIDDEN
- NORMAL =
A file that does not have any other attributes set.
FILE_ATTRIBUTE_NORMAL
- OFFLINE =
The data of a file is not immediately available. This attribute indicates that file data is physically moved to offline storage.
FILE_ATTRIBUTE_OFFLINE
- READONLY =
The file is read only. Apps can read it, but not write to it or delete it.
FILE_ATTRIBUTE_READONLY
- SYSTEM =
The file is part of or used exclusively by an operating system.
FILE_ATTRIBUTE_SYSTEM
- TEMPORARY =
The file is being used for temporary storage.
FILE_ATTRIBUTE_TEMPORARY
- INDEXED =
The file or directory is to be indexed by the content indexing service. Note that we have inverted the traditional definition.
0x0002000
- CONTENT_INDEXED =
Synonym for File::INDEXED.
INDEXED
- FULL =
Full security rights - read, write, append, execute, and delete.
STANDARD_RIGHTS_ALL | FILE_READ_DATA | FILE_WRITE_DATA | FILE_APPEND_DATA | FILE_READ_EA | FILE_WRITE_EA | FILE_EXECUTE | FILE_DELETE_CHILD | FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES
- CHANGE =
Generic write, generic read, execute and delete privileges
FILE_GENERIC_WRITE | FILE_GENERIC_READ | FILE_EXECUTE | DELETE
- READ =
Read and execute privileges
FILE_GENERIC_READ | FILE_EXECUTE
- ADD =
Add privileges
0x001201bf
- SECURITY_RIGHTS =
:stopdoc:
{ 'FULL' => FULL, 'DELETE' => DELETE, 'READ' => READ, 'CHANGE' => CHANGE, 'ADD' => ADD }
Class Method Summary collapse
-
.archive?(file) ⇒ Boolean
Returns true if the file or directory is an archive file.
-
.attributes(file) ⇒ Object
Returns an array of strings indicating the attributes for that file.
-
.basename(file, suffix = nil) ⇒ Object
Returns the last component of the filename given in
filename
. -
.basename_orig ⇒ Object
Strictly for making this code -w clean.
-
.blksize(file) ⇒ Object
Returns the file system’s block size.
-
.blockdev?(file) ⇒ Boolean
Returns whether or not
file
is a block device. - .blockdev_orig ⇒ Object
-
.chardev?(file) ⇒ Boolean
Returns true if the file is a character device.
- .chardev_orig ⇒ Object
-
.compressed?(file) ⇒ Boolean
Returns true if the file or directory is compressed.
-
.decrypt(file) ⇒ Object
Decrypts an encrypted file or directory.
-
.directory?(file) ⇒ Boolean
Returns true if
file
is a directory, false otherwise. - .directory_orig? ⇒ Object
-
.dirname(file) ⇒ Object
Returns all components of the filename given in
filename
except the last one. - .dirname_orig ⇒ Object
-
.encrypt(file) ⇒ Object
Encrypts a file or directory.
-
.encrypted?(file) ⇒ Boolean
Returns true if the file or directory is encrypted.
-
.get_permissions(file, host = nil) ⇒ Object
Returns a hash describing the current file permissions for the given file.
-
.hidden?(file) ⇒ Boolean
Returns true if the file or directory is hidden.
-
.indexed?(file) ⇒ Boolean
(also: content_indexed?)
Returns true if the file or directory is indexed by the content indexing service.
-
.join(*args) ⇒ Object
Join path string components together into a single string.
- .join_orig ⇒ Object
-
.long_path(path) ⇒ Object
Returns
path
in long format. -
.lstat(file) ⇒ Object
Identical to File.stat on Windows.
- .lstat_orig ⇒ Object
-
.normal?(file) ⇒ Boolean
Returns true if the file or directory has no other attributes set.
-
.offline?(file) ⇒ Boolean
Returns true if the data of the file is not immediately available.
-
.readlink(file) ⇒ Object
Returns the path of the of the symbolic link referred to by
file
. - .readlink_orig ⇒ Object
-
.readonly?(file) ⇒ Boolean
(also: read_only?)
Returns true if The file or directory is read-only.
-
.remove_attributes(file, flags) ⇒ Object
(also: unset_attr)
Removes the file attributes based on the given (numeric)
flags
. -
.reparse_point?(file) ⇒ Boolean
Returns true if the file or directory has an associated reparse point.
-
.securities(mask) ⇒ Object
Returns an array of human-readable strings that correspond to the permission flags.
-
.set_attributes(file, flags) ⇒ Object
(also: set_attr)
Sets the file attributes based on the given (numeric)
flags
. -
.set_permissions(file, perms) ⇒ Object
Sets the file permissions for the given file name.
-
.short_path(path) ⇒ Object
Returns
path
in 8.3 format. -
.size(file) ⇒ Object
Returns the size of the file in bytes.
- .size_orig ⇒ Object
-
.sparse?(file) ⇒ Boolean
Returns true if the file is a sparse file.
-
.split(file) ⇒ Object
Splits the given string into a directory and a file component and returns them in a two element array.
- .split_orig ⇒ Object
-
.stat(file) ⇒ Object
Returns a File::Stat object, as defined in the win32-file-stat package.
- .stat_orig ⇒ Object
-
.symlink(old_name, new_name) ⇒ Object
Creates a symbolic link called
new_name
for the file or directoryold_name
. -
.symlink?(file) ⇒ Boolean
Return true if the named file is a symbolic link, false otherwise.
- .symlink_orig ⇒ Object
- .symlink_orig? ⇒ Object
-
.system?(file) ⇒ Boolean
Returns true if the file or directory is part of the operating system or is used exclusively by the operating system.
-
.temporary?(file) ⇒ Boolean
Returns true if the file is being used for temporary storage.
Instance Method Summary collapse
-
#archive=(bool) ⇒ Object
Sets whether or not the file is an archive file.
-
#compressed=(bool) ⇒ Object
Sets whether or not the file is a compressed file.
-
#hidden=(bool) ⇒ Object
Sets the hidden attribute to true or false.
-
#indexed=(bool) ⇒ Object
(also: #content_indexed=)
Sets the ‘indexed’ attribute to true or false.
-
#normal=(bool) ⇒ Object
Sets the normal attribute.
-
#offline=(bool) ⇒ Object
Applications should not arbitrarily change this attribute.
-
#readonly=(bool) ⇒ Object
Sets the readonly attribute.
-
#sparse=(bool) ⇒ Object
Sets the file to a sparse (usually image) file.
-
#stat ⇒ Object
Instance methods.
-
#system=(bool) ⇒ Object
that is part of the operating system or is used exclusively by it.
-
#temporary=(bool) ⇒ Object
Sets whether or not the file is being used for temporary storage.
Class Method Details
.archive?(file) ⇒ Boolean
Returns true if the file or directory is an archive file. Applications use this attribute to mark files for backup or removal.
754 755 756 |
# File 'lib/win32/file.rb', line 754 def self.archive?(file) File::Stat.new(file).archive? end |
.attributes(file) ⇒ Object
Returns an array of strings indicating the attributes for that file. The possible values are:
archive compressed directory encrypted hidden indexed normal offline readonly reparse_point sparse system temporary
867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 |
# File 'lib/win32/file.rb', line 867 def self.attributes(file) attributes = GetFileAttributesW(multi_to_wide(file)) if attributes == INVALID_FILE_ATTRIBUTES raise ArgumentError, get_last_error end arr = [] arr << 'archive' if attributes & FILE_ATTRIBUTE_ARCHIVE > 0 arr << 'compressed' if attributes & FILE_ATTRIBUTE_COMPRESSED > 0 arr << 'directory' if attributes & FILE_ATTRIBUTE_DIRECTORY > 0 arr << 'encrypted' if attributes & FILE_ATTRIBUTE_ENCRYPTED > 0 arr << 'hidden' if attributes & FILE_ATTRIBUTE_HIDDEN > 0 arr << 'indexed' if attributes & FILE_ATTRIBUTE_NOT_CONTENT_INDEXED == 0 arr << 'normal' if attributes & FILE_ATTRIBUTE_NORMAL > 0 arr << 'offline' if attributes & FILE_ATTRIBUTE_OFFLINE > 0 arr << 'readonly' if attributes & FILE_ATTRIBUTE_READONLY > 0 arr << 'reparse_point' if attributes & FILE_ATTRIBUTE_REPARSE_POINT > 0 arr << 'sparse' if attributes & FILE_ATTRIBUTE_SPARSE_FILE > 0 arr << 'system' if attributes & FILE_ATTRIBUTE_SYSTEM > 0 arr << 'temporary' if attributes & FILE_ATTRIBUTE_TEMPORARY > 0 arr end |
.basename(file, suffix = nil) ⇒ Object
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 all other respects it is identical to the current implementation.
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"
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 |
# File 'lib/win32/file.rb', line 464 def basename(file, suffix = nil) raise TypeError unless file.is_a?(String) raise TypeError unless suffix.is_a?(String) if suffix return file if file.empty? # Return an empty path as-is. file = multi_to_wide(file) # Required for Windows API functions to work properly. file.tr!(File::SEPARATOR, File::ALT_SEPARATOR) # Return a root path as-is. return wide_to_multi(file) if PathIsRootW(file) PathStripPathW(file) # Gives us the basename if suffix if suffix == '.*' PathRemoveExtensionW(file) else if PathFindExtensionA(wide_to_multi(file)) == suffix PathRemoveExtensionW(file) end end end file = wide_to_multi(file) file.sub!(/\\+\z/, '') # Trim trailing slashes file end |
.basename_orig ⇒ Object
Strictly for making this code -w clean. They are removed later.
96 |
# File 'lib/win32/file.rb', line 96 alias basename_orig basename |
.blksize(file) ⇒ Object
Returns the file system’s block size.
715 716 717 |
# File 'lib/win32/file.rb', line 715 def blksize(file) File::Stat.new(file).blksize end |
.blockdev?(file) ⇒ Boolean
Returns whether or not file
is a block device. For MS Windows this means a removable drive, cdrom or ramdisk.
722 723 724 |
# File 'lib/win32/file.rb', line 722 def blockdev?(file) File::Stat.new(file).blockdev? end |
.blockdev_orig ⇒ Object
97 |
# File 'lib/win32/file.rb', line 97 alias blockdev_orig blockdev? |
.chardev?(file) ⇒ Boolean
Returns true if the file is a character device. This replaces the current Ruby implementation which always returns false.
729 730 731 |
# File 'lib/win32/file.rb', line 729 def chardev?(file) File::Stat.new(file).chardev? end |
.chardev_orig ⇒ Object
98 |
# File 'lib/win32/file.rb', line 98 alias chardev_orig chardev? |
.compressed?(file) ⇒ Boolean
Returns true if the file or directory is compressed. For a file, this means that all of the data in the file is compressed. For a directory, this means that compression is the default for newly created files and subdirectories.
763 764 765 |
# File 'lib/win32/file.rb', line 763 def self.compressed?(file) File::Stat.new(file).compressed? end |
.decrypt(file) ⇒ Object
Decrypts an encrypted file or directory.
The caller must have the FILE_READ_DATA, FILE_WRITE_DATA, FILE_READ_ATTRIBUTES, FILE_WRITE_ATTRIBUTES, and SYNCHRONIZE access rights.
Requires exclusive access to the file being decrypted, and will fail if another process is using the file. If the file is not encrypted an error is NOT raised.
Windows 2000 or later only.
438 439 440 441 442 443 |
# File 'lib/win32/file.rb', line 438 def decrypt(file) unless DecryptFileW(multi_to_wide(file), 0) raise ArgumentError, get_last_error end self end |
.directory?(file) ⇒ Boolean
Returns true if file
is a directory, false otherwise. – This method was redefined to handle wide character strings.
499 500 501 502 503 |
# File 'lib/win32/file.rb', line 499 def directory?(file) file = multi_to_wide(file) attributes = GetFileAttributesW(file) (attributes != INVALID_FILE_ATTRIBUTES) && (attributes & FILE_ATTRIBUTE_DIRECTORY > 0) end |
.directory_orig? ⇒ Object
99 |
# File 'lib/win32/file.rb', line 99 alias directory_orig? directory? |
.dirname(file) ⇒ Object
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"
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 |
# File 'lib/win32/file.rb', line 519 def dirname(file) raise TypeError unless file.is_a?(String) # Short circuit for empty paths return '.' if file.empty? # Temporarily convert to wide-byte file = multi_to_wide(file) # Convert slashes to backslashes for the Windows API functions file.tr!(File::SEPARATOR, File::ALT_SEPARATOR) # Return a root path as-is. return wide_to_multi(file) if PathIsRootW(file) # Remove trailing slashes if present while result = PathRemoveBackslashW(file) break unless result.empty? end # Remove trailing file name if present PathRemoveFileSpecW(file) # Return to multi-byte file = wide_to_multi(file) # Empty paths, short relative paths if file.nil? || (file && file.empty?) return '.' end file end |
.dirname_orig ⇒ Object
100 |
# File 'lib/win32/file.rb', line 100 alias dirname_orig dirname |
.encrypt(file) ⇒ Object
Encrypts a file or directory. All data streams in a file are encrypted. All new files created in an encrypted directory are encrypted.
The caller must have the FILE_READ_DATA, FILE_WRITE_DATA, FILE_READ_ATTRIBUTES, FILE_WRITE_ATTRIBUTES, and SYNCHRONIZE access rights.
Requires exclusive access to the file being encrypted, and will fail if another process is using the file. If the file is compressed, EncryptFile will decompress the file before encrypting it.
419 420 421 422 423 424 |
# File 'lib/win32/file.rb', line 419 def encrypt(file) unless EncryptFileW(multi_to_wide(file)) raise ArgumentError, get_last_error end self end |
.encrypted?(file) ⇒ Boolean
Returns true if the file or directory is encrypted. For a file, this means that all data in the file is encrypted. For a directory, this means that encryption is the default for newly created files and subdirectories.
772 773 774 |
# File 'lib/win32/file.rb', line 772 def self.encrypted?(file) File::Stat.new(file).encrypted? end |
.get_permissions(file, host = nil) ⇒ Object
Returns a hash describing the current file permissions for the given file. The account name is the key, and the value is an integer representing an or’d value that corresponds to the security permissions for that file.
To get a human readable version of the permissions, pass the value to the File.securities
method.
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 |
# File 'lib/win32/file.rb', line 302 def (file, host=nil) current_length = 0 length_needed = [0].pack('L') sec_buf = '' loop do bool = GetFileSecurityW( multi_to_wide(file), DACL_SECURITY_INFORMATION, sec_buf, sec_buf.length, length_needed ) if bool == 0 && GetLastError() != ERROR_INSUFFICIENT_BUFFER raise ArgumentError, get_last_error end break if sec_buf.length >= length_needed.unpack('L').first sec_buf += ' ' * length_needed.unpack('L').first end control = [0].pack('L') revision = [0].pack('L') unless GetSecurityDescriptorControl(sec_buf, control, revision) raise ArgumentError, get_last_error end # No DACL exists if (control.unpack('L').first & SE_DACL_PRESENT) == 0 raise ArgumentError, 'No DACL present: explicit deny all' end dacl_present = [0].pack('L') dacl_defaulted = [0].pack('L') dacl_ptr = [0].pack('L') val = GetSecurityDescriptorDacl( sec_buf, dacl_present, dacl_ptr, dacl_defaulted ) if val == 0 raise ArgumentError, get_last_error end acl_buf = 0.chr * 8 # byte, byte, word, word, word (struct ACL) memcpy(acl_buf, dacl_ptr.unpack('L').first, acl_buf.size) if acl_buf.unpack('CCSSS').first == 0 raise ArgumentError, 'DACL is NULL: implicit access grant' end ace_ptr = [0].pack('L') ace_count = acl_buf.unpack('CCSSS')[3] perms_hash = {} 0.upto(ace_count - 1){ |i| unless GetAce(dacl_ptr.unpack('L').first, i, ace_ptr) next end ace_buf = 0.chr * 12 # ACE_HEADER, dword, dword (ACCESS_ALLOWED_ACE) memcpy(ace_buf, ace_ptr.unpack('L').first, ace_buf.size) if ace_buf.unpack('CCS').first == ACCESS_ALLOWED_ACE_TYPE name = 0.chr * MAXPATH name_size = [name.size].pack('L') domain = 0.chr * MAXPATH domain_size = [domain.size].pack('L') snu_ptr = 0.chr * 4 val = LookupAccountSid( host, ace_ptr.unpack('L').first + 8, # address of ace_ptr->SidStart name, name_size, domain, domain_size, snu_ptr ) if val == 0 raise ArgumentError, get_last_error end name = name[0..name_size.unpack('L').first].split(0.chr)[0] domain = domain[0..domain_size.unpack('L').first].split(0.chr)[0] mask = ace_buf.unpack('LLL')[1] unless domain.nil? || domain.empty? name = domain + '\\' + name end perms_hash[name] = mask end } perms_hash end |
.hidden?(file) ⇒ Boolean
Returns true if the file or directory is hidden. It is not included in an ordinary directory listing.
779 780 781 |
# File 'lib/win32/file.rb', line 779 def self.hidden?(file) File::Stat.new(file).hidden? end |
.indexed?(file) ⇒ Boolean Also known as: content_indexed?
Returns true if the file or directory is indexed by the content indexing service.
786 787 788 |
# File 'lib/win32/file.rb', line 786 def self.indexed?(file) File::Stat.new(file).indexed? end |
.join(*args) ⇒ Object
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
564 565 566 |
# File 'lib/win32/file.rb', line 564 def join(*args) return join_orig(*args).tr("/", "\\") end |
.join_orig ⇒ Object
101 |
# File 'lib/win32/file.rb', line 101 alias join_orig join |
.long_path(path) ⇒ Object
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.
576 577 578 579 580 581 582 |
# File 'lib/win32/file.rb', line 576 def long_path(path) buf = 0.chr * MAXPATH if GetLongPathNameW(multi_to_wide(path), buf, buf.size) == 0 raise ArgumentError, get_last_error end wide_to_multi(buf) end |
.lstat(file) ⇒ Object
Identical to File.stat on Windows.
709 710 711 |
# File 'lib/win32/file.rb', line 709 def lstat(file) File::Stat.new(file) end |
.lstat_orig ⇒ Object
102 |
# File 'lib/win32/file.rb', line 102 alias lstat_orig lstat |
.normal?(file) ⇒ Boolean
Returns true if the file or directory has no other attributes set.
792 793 794 |
# File 'lib/win32/file.rb', line 792 def self.normal?(file) File::Stat.new(file).normal? end |
.offline?(file) ⇒ Boolean
Returns true if the data of the file is not immediately available. This attribute indicates that the file data has been physically moved to offline storage. This attribute is used by Remote Storage, the hierarchical storage management software. Applications should not arbitrarily change this attribute.
802 803 804 |
# File 'lib/win32/file.rb', line 802 def self.offline?(file) File::Stat.new(file).offline? end |
.readlink(file) ⇒ Object
Returns the path of the of the symbolic link referred to by file
.
Requires Windows Vista or later. On older versions of Windows it will raise a NotImplementedError, as per MRI.
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 |
# File 'lib/win32/file.rb', line 589 def readlink(file) if defined? GetFinalPathNameByHandle file = multi_to_wide(file) begin handle = CreateFileW( file, GENERIC_READ, FILE_SHARE_READ, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nil ) if handle == INVALID_HANDLE_VALUE raise ArgumentError, get_last_error end path = 0.chr * MAXPATH GetFinalPathNameByHandleW(handle, path, path.size, 0) ensure CloseHandle(handle) end wide_to_multi(path).strip[4..-1] # get rid of prepending "\\?\" else msg = "readlink() function is unimplemented on this machine" raise NotImplementedError, msg end end |
.readlink_orig ⇒ Object
103 |
# File 'lib/win32/file.rb', line 103 alias readlink_orig readlink |
.readonly?(file) ⇒ Boolean Also known as: read_only?
Returns true if The file or directory is read-only. Applications can read the file but cannot write to it or delete it. In the case of a directory, applications cannot delete it.
810 811 812 |
# File 'lib/win32/file.rb', line 810 def self.readonly?(file) File::Stat.new(file).readonly? end |
.remove_attributes(file, flags) ⇒ Object Also known as: unset_attr
Removes the file attributes based on the given (numeric) flags
.
915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 |
# File 'lib/win32/file.rb', line 915 def self.remove_attributes(file, flags) file = multi_to_wide(file) attributes = GetFileAttributesW(file) if attributes == INVALID_FILE_ATTRIBUTES raise ArgumentError, get_last_error end attributes &= ~flags if SetFileAttributesW(file, attributes) == 0 raise ArgumentError, get_last_error end self end |
.reparse_point?(file) ⇒ Boolean
Returns true if the file or directory has an associated reparse point. A reparse point is a collection of user defined data associated with a file or directory. For more on reparse points, search msdn.microsoft.com.
819 820 821 |
# File 'lib/win32/file.rb', line 819 def self.reparse_point?(file) File::Stat.new(file).reparse_point? end |
.securities(mask) ⇒ Object
Returns an array of human-readable strings that correspond to the permission flags.
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 |
# File 'lib/win32/file.rb', line 276 def securities(mask) sec_array = [] if mask == 0 sec_array.push('NONE') else if (mask & FULL) ^ FULL == 0 sec_array.push('FULL') else SECURITY_RIGHTS.each{ |string, numeric| if (numeric & mask) ^ numeric == 0 sec_array.push(string) end } end end sec_array end |
.set_attributes(file, flags) ⇒ Object Also known as: set_attr
Sets the file attributes based on the given (numeric) flags
. This does not remove existing attributes, it merely adds to them.
896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 |
# File 'lib/win32/file.rb', line 896 def self.set_attributes(file, flags) file = multi_to_wide(file) attributes = GetFileAttributesW(file) if attributes == INVALID_FILE_ATTRIBUTES raise ArgumentError, get_last_error end attributes |= flags if SetFileAttributesW(file, attributes) == 0 raise ArgumentError, get_last_error end self end |
.set_permissions(file, perms) ⇒ Object
Sets the file permissions for the given file name. The ‘permissions’ argument is a hash with an account name as the key, and the various permission constants as possible values. The possible constant values are:
-
FILE_READ_DATA
-
FILE_WRITE_DATA
-
FILE_APPEND_DATA
-
FILE_READ_EA
-
FILE_WRITE_EA
-
FILE_EXECUTE
-
FILE_DELETE_CHILD
-
FILE_READ_ATTRIBUTES
-
FILE_WRITE_ATTRIBUTES
-
STANDARD_RIGHTS_ALL
-
FULL
-
READ
-
ADD
-
CHANGE
-
DELETE
-
READ_CONTROL
-
WRITE_DAC
-
WRITE_OWNER
-
SYNCHRONIZE
-
STANDARD_RIGHTS_REQUIRED
-
STANDARD_RIGHTS_READ
-
STANDARD_RIGHTS_WRITE
-
STANDARD_RIGHTS_EXECUTE
-
STANDARD_RIGHTS_ALL
-
SPECIFIC_RIGHTS_ALL
-
ACCESS_SYSTEM_SECURITY
-
MAXIMUM_ALLOWED
-
GENERIC_READ
-
GENERIC_WRITE
-
GENERIC_EXECUTE
-
GENERIC_ALL
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 |
# File 'lib/win32/file.rb', line 151 def (file, perms) raise TypeError unless file.is_a?(String) raise TypeError unless perms.kind_of?(Hash) file = multi_to_wide(file) account_rights = 0 sec_desc = 0.chr * SECURITY_DESCRIPTOR_MIN_LENGTH unless InitializeSecurityDescriptor(sec_desc, 1) raise ArgumentError, get_last_error end cb_acl = 1024 cb_sid = 1024 acl_new = 0.chr * cb_acl unless InitializeAcl(acl_new, cb_acl, ACL_REVISION2) raise ArgumentError, get_last_error end sid = 0.chr * cb_sid snu_type = 0.chr * cb_sid all_ace = 0.chr * ALLOW_ACE_LENGTH all_ace_ptr = memset(all_ace, 0, 0) # address of all_ace # all_ace_ptr->Header.AceType = ACCESS_ALLOWED_ACE_TYPE all_ace[0] = 0.chr perms.each{ |account, mask| next if mask.nil? cch_domain = [80].pack('L') cb_sid = [1024].pack('L') domain_buf = 0.chr * 80 server, account = account.split("\\") if ['BUILTIN', 'NT AUTHORITY'].include?(server.upcase) server = nil end val = LookupAccountName( server, account, sid, cb_sid, domain_buf, cch_domain, snu_type ) if val == 0 raise ArgumentError, get_last_error end size = [0,0,0,0,0].pack('CCSLL').length # sizeof(ACCESS_ALLOWED_ACE) val = CopySid( ALLOW_ACE_LENGTH - size, all_ace_ptr + 8, # address of all_ace_ptr->SidStart sid ) if val == 0 raise ArgumentError, get_last_error end if (GENERIC_ALL & mask).nonzero? account_rights = GENERIC_ALL & mask elsif (GENERIC_RIGHTS_CHK & mask).nonzero? account_rights = GENERIC_RIGHTS_MASK & mask end # all_ace_ptr->Header.AceFlags = INHERIT_ONLY_ACE|OBJECT_INHERIT_ACE all_ace[1] = (INHERIT_ONLY_ACE | OBJECT_INHERIT_ACE).chr # WHY DO I NEED THIS RUBY CORE TEAM? WHY?!?!?!?!?!? all_ace.force_encoding('ASCII-8BIT') if RUBY_VERSION.to_f >= 1.9 2.times{ if account_rights != 0 all_ace[2,2] = [12 - 4 + GetLengthSid(sid)].pack('S') all_ace[4,4] = [account_rights].pack('L') val = AddAce( acl_new, ACL_REVISION2, MAXDWORD, all_ace_ptr, all_ace[2,2].unpack('S').first ) if val == 0 raise ArgumentError, get_last_error end # all_ace_ptr->Header.AceFlags = CONTAINER_INHERIT_ACE all_ace[1] = CONTAINER_INHERIT_ACE.chr else # all_ace_ptr->Header.AceFlags = 0 all_ace[1] = 0.chr end account_rights = REST_RIGHTS_MASK & mask } } unless SetSecurityDescriptorDacl(sec_desc, 1, acl_new, 0) raise ArgumentError, get_last_error end unless SetFileSecurityW(file, DACL_SECURITY_INFORMATION, sec_desc) raise ArgumentError, get_last_error end self end |
.short_path(path) ⇒ Object
Returns path
in 8.3 format. For example, ‘c:documentation.doc’ would be returned as ‘c:docume~1.doc’.
625 626 627 628 629 630 631 |
# File 'lib/win32/file.rb', line 625 def short_path(path) buf = 0.chr * MAXPATH if GetShortPathNameW(multi_to_wide(path), buf, buf.size) == 0 raise ArgumentError, get_last_error end wide_to_multi(buf) end |
.size(file) ⇒ Object
Returns the size of the file in bytes.
This was reimplemented because the current version does not handle file sizes greater than 2gb.
738 739 740 |
# File 'lib/win32/file.rb', line 738 def size(file) File::Stat.new(file).size end |
.size_orig ⇒ Object
104 |
# File 'lib/win32/file.rb', line 104 alias size_orig size |
.sparse?(file) ⇒ Boolean
Returns true if the file is a sparse file. A sparse file is a file in which much of the data is zeros, typically image files. See msdn.microsoft.com for more details.
827 828 829 |
# File 'lib/win32/file.rb', line 827 def self.sparse?(file) File::Stat.new(file).sparse? end |
.split(file) ⇒ Object
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.
637 638 639 640 641 642 643 644 645 646 |
# File 'lib/win32/file.rb', line 637 def split(file) array = [] if file.empty? || PathIsRootW(multi_to_wide(file)) array.push(file, '') else array.push(File.dirname(file), File.basename(file)) end array end |
.split_orig ⇒ Object
105 |
# File 'lib/win32/file.rb', line 105 alias split_orig split |
.stat(file) ⇒ Object
Returns a File::Stat object, as defined in the win32-file-stat package.
703 704 705 |
# File 'lib/win32/file.rb', line 703 def stat(file) File::Stat.new(file) end |
.stat_orig ⇒ Object
106 |
# File 'lib/win32/file.rb', line 106 alias stat_orig stat |
.symlink(old_name, new_name) ⇒ Object
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.
654 655 656 657 658 659 660 661 662 663 664 665 666 |
# File 'lib/win32/file.rb', line 654 def symlink(old_name, new_name) if defined? CreateSymbolicLink old_name = multi_to_wide(old_name) new_name = multi_to_wide(new_name) flags = File.directory?(wide_to_multi(old_name)) ? 1 : 0 unless CreateSymbolicLinkW(new_name, old_name, flags) raise ArgumentError, get_last_error end else nil end end |
.symlink?(file) ⇒ Boolean
Return true if the named file is a symbolic link, false otherwise.
This method requires Windows Vista or later to work. Otherwise, it always returns false as per MRI.
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 |
# File 'lib/win32/file.rb', line 673 def symlink?(file) bool = false file = multi_to_wide(file) attr = GetFileAttributesW(file) # Differentiate between a symlink and other kinds of reparse points if attr & FILE_ATTRIBUTE_REPARSE_POINT > 0 begin buffer = 0.chr * 278 # WIN32_FIND_DATA handle = FindFirstFileW(file, buffer) if handle == INVALID_HANDLE_VALUE raise ArgumentError, get_last_error end if buffer[36,4].unpack('L')[0] == IO_REPARSE_TAG_SYMLINK bool = true end ensure CloseHandle(handle) end end bool end |
.symlink_orig ⇒ Object
107 |
# File 'lib/win32/file.rb', line 107 alias symlink_orig symlink |
.symlink_orig? ⇒ Object
108 |
# File 'lib/win32/file.rb', line 108 alias symlink_orig? symlink? |
.system?(file) ⇒ Boolean
Returns true if the file or directory is part of the operating system or is used exclusively by the operating system.
834 835 836 |
# File 'lib/win32/file.rb', line 834 def self.system?(file) File::Stat.new(file).system? end |
.temporary?(file) ⇒ Boolean
Returns true if the file is being used for temporary storage.
File systems avoid writing data back to mass storage if sufficient cache memory is available, because often the application deletes the temporary file shortly after the handle is closed. In that case, the system can entirely avoid writing the data. Otherwise, the data will be written after the handle is closed.
846 847 848 |
# File 'lib/win32/file.rb', line 846 def self.temporary?(file) File::Stat.new(file).temporary? end |
Instance Method Details
#archive=(bool) ⇒ Object
Sets whether or not the file is an archive file.
940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 |
# File 'lib/win32/file.rb', line 940 def archive=(bool) wide_path = multi_to_wide(self.path) attributes = GetFileAttributesW(wide_path) if attributes == INVALID_FILE_ATTRIBUTES raise ArgumentError, get_last_error end if bool attributes |= FILE_ATTRIBUTE_ARCHIVE; else attributes &= ~FILE_ATTRIBUTE_ARCHIVE; end if SetFileAttributesW(wide_path, attributes) == 0 raise ArgumentError, get_last_error end self end |
#compressed=(bool) ⇒ Object
Sets whether or not the file is a compressed file.
963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 |
# File 'lib/win32/file.rb', line 963 def compressed=(bool) in_buf = bool ? COMPRESSION_FORMAT_DEFAULT : COMPRESSION_FORMAT_NONE in_buf = [in_buf].pack('L') bytes = [0].pack('L') # We can't use get_osfhandle here because we need specific attributes handle = CreateFileW( multi_to_wide(self.path), FILE_READ_DATA | FILE_WRITE_DATA, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0 ) if handle == INVALID_HANDLE_VALUE raise ArgumentError, get_last_error end begin bool = DeviceIoControl( handle, FSCTL_SET_COMPRESSION(), in_buf, in_buf.length, 0, 0, bytes, 0 ) unless bool raise ArgumentError, get_last_error end ensure CloseHandle(handle) end self end |
#hidden=(bool) ⇒ Object
Sets the hidden attribute to true or false. Setting this attribute to true means that the file is not included in an ordinary directory listing.
1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 |
# File 'lib/win32/file.rb', line 1008 def hidden=(bool) wide_path = multi_to_wide(self.path) attributes = GetFileAttributesW(wide_path) if attributes == INVALID_FILE_ATTRIBUTES raise ArgumentError, get_last_error end if bool attributes |= FILE_ATTRIBUTE_HIDDEN; else attributes &= ~FILE_ATTRIBUTE_HIDDEN; end if SetFileAttributesW(wide_path, attributes) == 0 raise ArgumentError, get_last_error end self end |
#indexed=(bool) ⇒ Object Also known as: content_indexed=
Sets the ‘indexed’ attribute to true or false. Setting this to false means that the file will not be indexed by the content indexing service.
1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 |
# File 'lib/win32/file.rb', line 1033 def indexed=(bool) wide_path = multi_to_wide(self.path) attributes = GetFileAttributesW(wide_path) if attributes == INVALID_FILE_ATTRIBUTES raise ArgumentError, get_last_error end if bool attributes &= ~FILE_ATTRIBUTE_NOT_CONTENT_INDEXED; else attributes |= FILE_ATTRIBUTE_NOT_CONTENT_INDEXED; end if SetFileAttributes(wide_path, attributes) == 0 raise ArgumentError, get_last_error end self end |
#normal=(bool) ⇒ Object
Sets the normal attribute. Note that only ‘true’ is a valid argument, which has the effect of removing most other attributes. Attempting to pass any value except true will raise an ArgumentError.
1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 |
# File 'lib/win32/file.rb', line 1060 def normal=(bool) unless bool raise ArgumentError, "only 'true' may be passed as an argument" end if SetFileAttributesW(multi_to_wide(self.path),FILE_ATTRIBUTE_NORMAL) == 0 raise ArgumentError, get_last_error end self end |
#offline=(bool) ⇒ Object
Applications should not arbitrarily change this attribute.
1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 |
# File 'lib/win32/file.rb', line 1080 def offline=(bool) wide_path = multi_to_wide(self.path) attributes = GetFileAttributesW(wide_path) if attributes == INVALID_FILE_ATTRIBUTES raise ArgumentError, get_last_error end if bool attributes |= FILE_ATTRIBUTE_OFFLINE; else attributes &= ~FILE_ATTRIBUTE_OFFLINE; end if SetFileAttributesW(wide_path, attributes) == 0 raise ArgumentError, get_last_error end self end |
#readonly=(bool) ⇒ Object
Sets the readonly attribute. If set to true the the file or directory is readonly. Applications can read the file but cannot write to it or delete it. In the case of a directory, applications cannot delete it.
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 |
# File 'lib/win32/file.rb', line 1105 def readonly=(bool) wide_path = multi_to_wide(self.path) attributes = GetFileAttributesW(wide_path) if attributes == INVALID_FILE_ATTRIBUTES raise ArgumentError, get_last_error end if bool attributes |= FILE_ATTRIBUTE_READONLY; else attributes &= ~FILE_ATTRIBUTE_READONLY; end if SetFileAttributesW(wide_path, attributes) == 0 raise ArgumentError, get_last_error end self end |
#sparse=(bool) ⇒ Object
Sets the file to a sparse (usually image) file. Note that you cannot remove the sparse property from a file.
1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 |
# File 'lib/win32/file.rb', line 1129 def sparse=(bool) unless bool warn 'cannot remove sparse property from a file - operation ignored' return end bytes = [0].pack('L') handle = CreateFileW( multi_to_wide(self.path), FILE_READ_DATA | FILE_WRITE_DATA, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING, FSCTL_SET_SPARSE(), 0 ) if handle == INVALID_HANDLE_VALUE raise ArgumentError, get_last_error end begin bool = DeviceIoControl( handle, FSCTL_SET_SPARSE(), 0, 0, 0, 0, bytes, 0 ) unless bool == 0 raise ArgumentError, get_last_error end ensure CloseHandle(handle) end self end |
#stat ⇒ Object
Instance methods
934 935 936 |
# File 'lib/win32/file.rb', line 934 def stat File::Stat.new(self.path) end |
#system=(bool) ⇒ Object
that is part of the operating system or is used exclusively by it.
1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 |
# File 'lib/win32/file.rb', line 1176 def system=(bool) wide_path = multi_to_wide(self.path) attributes = GetFileAttributesW(wide_path) if attributes == INVALID_FILE_ATTRIBUTES raise ArgumentError, get_last_error end if bool attributes |= FILE_ATTRIBUTE_SYSTEM; else attributes &= ~FILE_ATTRIBUTE_SYSTEM; end if SetFileAttributesW(wide_path, attributes) == 0 raise ArgumentError, get_last_error end self end |
#temporary=(bool) ⇒ Object
Sets whether or not the file is being used for temporary storage.
File systems avoid writing data back to mass storage if sufficient cache memory is available, because often the application deletes the temporary file shortly after the handle is closed. In that case, the system can entirely avoid writing the data. Otherwise, the data will be written after the handle is closed.
1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 |
# File 'lib/win32/file.rb', line 1205 def temporary=(bool) wide_path = multi_to_wide(self.path) attributes = GetFileAttributesW(wide_path) if attributes == INVALID_FILE_ATTRIBUTES raise ArgumentError, get_last_error end if bool attributes |= FILE_ATTRIBUTE_TEMPORARY; else attributes &= ~FILE_ATTRIBUTE_TEMPORARY; end if SetFileAttributesW(wide_path, attributes) == 0 raise ArgumentError, get_last_error end self end |