Read file as bytestring and write this bytestring to a file: issue on a network drive
Consider the following simple Haskell program, which reads a file as a bytestring and writes the file
tmp.tmp from this bytestring:
module Main where import System.Environment import qualified Data.ByteString.Lazy as B main :: IO () main = do [file] <- getArgs bs <- B.readFile file action <- B.writeFile "tmp.tmp" bs putStrLn "done"
It is compiled to an executable named
I have two hard drives on my computer: the
C drive and the
U drive, and this one is a network drive, and this network drive is offline.
Now, let's try
When I run it from
C, there's no problem; I run it two times below, the first time with a file on
C and the second time with a file on
C:\HaskellProjects\imagelength> tmptmp LICENSE done C:\HaskellProjects\imagelength> tmptmp U:\Data\ztemp\test.xlsx done
Now I run it from
U, with a file on the
C drive, no problem:
U:\Data\ztemp> tmptmp C:\HaskellProjects\imagelength\LICENSE done
The problem occurs when I run it from
U with a file on the
U:\Data\ztemp> tmptmp test.xlsx tmptmp: tmp.tmp: openBinaryFile: resource busy (file is locked)
If in my program I use strict bytestrings instead of lazy bytestrings (by replacing
Data.ByteString), this problem does not occur anymore.
I'd like to understand that. Any explanation? (I would particularly like to know how to solve this issue but still using lazy bytestrings)
To be perhaps more precise, the problem still occurs with this program:
import qualified Data.ByteString as SB import qualified Data.ByteString.Lazy as LB main :: IO () main = do [file] <- getArgs bs <- LB.readFile file action <- SB.writeFile "tmp.tmp" (LB.toStrict bs) putStrLn "done"
while the problem disappears with:
bs <- SB.readFile file action <- LB.writeFile "tmp.tmp" (LB.fromStrict bs)
It looks like the point causing the problem is the laziness of