| 252 | | op1 "-r" = fileTestIO fileTestIsReadable |
| 253 | | op1 "-w" = fileTestIO fileTestIsWritable |
| 254 | | op1 "-x" = fileTestIO fileTestIsExecutable |
| 255 | | op1 "-e" = fileTestIO fileTestExists |
| 256 | | op1 "-z" = fileTestIO fileTestSizeIsZero |
| 257 | | op1 "-s" = fileTestIO fileTestFileSize |
| 258 | | op1 "-f" = fileTestIO fileTestIsFile |
| 259 | | op1 "-d" = fileTestIO fileTestIsDirectory |
| | 253 | op1 "-r" = FileTest.isReadable |
| | 254 | op1 "-w" = FileTest.isWritable |
| | 255 | op1 "-x" = FileTest.isExecutable |
| | 256 | op1 "-e" = FileTest.exists |
| | 257 | op1 "-z" = FileTest.sizeIsZero |
| | 258 | op1 "-s" = FileTest.fileSize |
| | 259 | op1 "-f" = FileTest.isFile |
| | 260 | op1 "-d" = FileTest.isDirectory |
| 1234 | | |
| 1235 | | -- filetest operators -- |
| 1236 | | |
| 1237 | | -- Officially, these should return a stat object, which sometimes pretends |
| 1238 | | -- to be a boolean, and may(?) return the filename in string context. |
| 1239 | | -- DARCS was working on stat, and we should perhaps grab their work: |
| 1240 | | -- http://www.abridgegame.org/pipermail/darcs-users/2005-February/005499.html |
| 1241 | | -- They currently (2004-04-05) seem to be using: |
| 1242 | | -- http://abridgegame.org/cgi-bin/darcs.cgi/darcs/win32/System/Posix.hs |
| 1243 | | -- For the moment, these return filename and false or undef. |
| 1244 | | -- Known Bugs: multiple stat()s are done, and filename isnt a boolean. |
| 1245 | | |
| 1246 | | fileTestIO :: (Value n) => (n -> IO Val) -> Val -> Eval Val |
| 1247 | | fileTestIO f v = do |
| 1248 | | str <- fromVal =<< fromVal' v |
| 1249 | | tryIO undef $ f str |
| 1250 | | |
| 1251 | | fileTestIsReadable :: FilePath -> IO Val |
| 1252 | | fileTestIsReadable f = do |
| 1253 | | p <- getPermissions f |
| 1254 | | let b = readable p |
| 1255 | | return $ if b then castV f else VBool False |
| 1256 | | |
| 1257 | | fileTestIsWritable :: FilePath -> IO Val |
| 1258 | | fileTestIsWritable f = do |
| 1259 | | p <- getPermissions f |
| 1260 | | let b = writable p |
| 1261 | | return $ if b then castV f else VBool False |
| 1262 | | |
| 1263 | | fileTestIsExecutable :: FilePath -> IO Val |
| 1264 | | fileTestIsExecutable f = do |
| 1265 | | p <- getPermissions f |
| 1266 | | let b = executable p || searchable p |
| 1267 | | return $ if b then castV f else VBool False |
| 1268 | | |
| 1269 | | fileTestExists :: FilePath -> IO Val |
| 1270 | | fileTestExists f = do |
| 1271 | | b1 <- doesFileExist f |
| 1272 | | b2 <- doesDirectoryExist f |
| 1273 | | return $ if b1 || b2 then castV f else VBool False |
| 1274 | | |
| 1275 | | fileTestIsFile :: FilePath -> IO Val |
| 1276 | | fileTestIsFile f = do |
| 1277 | | b <- doesFileExist f |
| 1278 | | return $ if b then castV f else VBool False |
| 1279 | | |
| 1280 | | fileTestIsDirectory :: FilePath -> IO Val |
| 1281 | | fileTestIsDirectory f = do |
| 1282 | | b <- doesDirectoryExist f |
| 1283 | | return $ if b then castV f else VBool False |
| 1284 | | |
| 1285 | | fileTestFileSize :: FilePath -> IO Val |
| 1286 | | fileTestFileSize f = do |
| 1287 | | n <- statFileSize f |
| 1288 | | return $ VInt n |
| 1289 | | |
| 1290 | | fileTestSizeIsZero :: FilePath -> IO Val |
| 1291 | | fileTestSizeIsZero f = do |
| 1292 | | n <- statFileSize f |
| 1293 | | return $ if n == 0 then VBool True else VBool False |