Bilal Syed Hussain
Bilal Syed Hussain

Reputation: 9224

Check if two directories are on the same filesystem in haskell

If I have two directories A and B How do I tell if they are on the same filesystem (e.g on same hardrive) in Haskell on OS X and linux ?

I checked System.Directory and System.FilePath.Posix which don't seem to have any thing for doing this.

Upvotes: 1

Views: 163

Answers (2)

fjh
fjh

Reputation: 13091

The getFileStatus and deviceID functions from the unix package should help you with that.

Upvotes: 3

Sibi
Sibi

Reputation: 48734

One way would be to exploit the stat utility and write a wrapper for it yourself. stat has the ability to give device number for your file. I tested this following code in Linux and it works for different disks (but I'm not sure for Mac OS):

import Control.Applicative ((<$>))
import System.Process

statDeviceID :: FilePath -> IO String
statDeviceID fp = readProcess "stat" ["--printf=%d", fp] ""
-- for mac which has a different version of stat
-- statDeviceID fp = readProcess "stat" ["-f", "%d", fp] ""

checkSameDevice :: [FilePath] -> IO Bool
checkSameDevice xs = (\x -> all (== head x) x) <$> (sequence $ map statDeviceID xs)

paths = ["/mnt/Books", "/home/sibi"]

main = checkSameDevice paths >>= print

In ghci:

λ> main
False   -- False since /mnt is a different hard disk

Upvotes: 2

Related Questions