如果我有两个目录-- A和B --如何判断它们是否位于OS和linux中的同一个文件系统(例如相同的硬盘上)?
我检查了System.Directory和System.FilePath.Posix,它们似乎没有做这个的任何事情。
发布于 2014-08-15 21:48:25
unix包中的getFileStatus和deviceID函数应该可以帮助您实现这一点。
发布于 2014-08-15 21:57:42
一种方法是利用stat实用程序并自己为它编写一个包装器。stat能够为您的文件提供设备编号。我在Linux中测试了以下代码,它适用于不同的磁盘(但我不确定它是否适用于Mac ):
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在ghci:
λ> main
False -- False since /mnt is a different hard diskhttps://stackoverflow.com/questions/25334127
复制相似问题