Skip to content
Daniel Berger edited this page Jun 11, 2016 · 8 revisions

The win32-file gem

The win32-file gem reopens the core File class, adding several new methods, while also redefining certain existing methods.

File.basename

This was redefined because, in our opinion, the MRI version did not handle root paths properly. Our contention is that the basename of a root path is itself, i.e. getting less than the //server/share path doesn't make sense because //server (or even just /) is not a valid path.

In addition, our version automatically converts all forward slashes to back slashes, whereas MRI will simply mix and match depending on what the arguments were.

# MRI
File.basename("C:/")       # => /
File.basename("//foo/bar") # => //foo

# win32-file
File.basename("C:/")       # => "C:\\"
File.basename("//foo/bar") # => "\\\\foo\\bar"

File.dirname

This was originally redefined for the same reasons that File.basename was redefined. However, at some point in the Ruby 2.x lifecycle it was modified, and now behaves nearly the same as the win32-file gem. The only exception is that the win32-file gem converts all forward slashes to backslashes.

This is somewhat confusing for us, since it would now seem that MRI is handling root paths between File.basename and File.dirname differently. To wit:

# MRI
File.dirname("//foo/bar/baz") # => "//foo/bar"
File.dirname("//foo/bar")     # => "//foo/bar"

File.join

Identical to the MRI method except that it converts all forward slashes to backslashes.

File.split

This was modified because of the change to File.basename above, and root path handling in general.

# MRI
File.split("C:/")       # => ["C:/", "/"]
File.split("//foo/bar") # => ["//foo/bar", "/"]

# win32-file
File.split("C:/")       # => ["C:/", ""]
File.split("//foo/bar") # => ["/foo/bar", ""]

File.long_path

This converts a file in 8.3 format into the full path name.

File.long_path("C:/SOMEFI~1.txt") # => "C:/somefile.txt"

File.short_path

This converts a long file name into 8.3 format.

File.short_path("C:/documentation.doc" # => "C:/docume~1.doc"

File.symlink

This was redefined because Windows did not support symlinks until Windows Vista (released in 2006), and core Ruby did not support symlinks until Ruby 2.3.x, which was released in 2016.

This may be removed in a future release.

File.symlink?

This was redefined for the same reasons as File.symlink above.

Note that Ruby 2.3.x currently has a bug where it treats all reparse points as symlinks, and so we have left our own version in the gem for now. See https://bugs.ruby-lang.org/issues/11478 for details.

File.realdirpath

This was redefined to handle our added symlink support.

File.realpath

This was redefined to handle our added symlink support.

File.readlink

This was redefined to handle our added symlink support.

File.stat

This was redefined so that it returns a File::Stat struct as defined by the win32-file-stat gem. For all stat methods, e.g. File.ctime, File.blksize, etc, please see the documentation for the win32-file-stat gem.