Search code examples
rubywindowsfilesystemsntfs

How to determine the filesystem for a path or file in Ruby?


I need to create safe filenames on Windows (with support for foreign language chars). Some of the development environments are Mac, others Windows. Our automated tests run on a Windows machine and the application is installed on Windows machines. Filesystems impose their own rules on what constitutes a legal filename (specifically characters like <, >, " and path seperators are illegal on NTFS).

I'd like to write some tests that actually test the filesystem itself. I'm trying not to mock the tests because then I'm not really testing anything in the real system and just verifying that the code handles mocked exceptions. The tests need to at least run on Mac (it's okay if they just skip the test when Mac is present), but I would also like to know which type of filesystem the tests are running on. NTFS is different than Fat32. NOTE: I am not asking for an implementation of the safe filename method, I already have that. I am asking if there is any way in Ruby to determine which filesystem I am currently in, for testing purposes. Multiple searches and the docs for File, FileUtils, Dir, and even ENV haven't turned up anything.


Solution

  • You can try sys-filesystem gem:

    This is an interface for obtaining filesytem information ala 'df' on Unix, but also works on Windows.

    irb(main):001:0> require 'sys/filesystem'
    => true
    irb(main):006:0> Sys::Filesystem.stat("D:\\").base_type
    => "NTFS"