File: OpenFile.hs

package info (click to toggle)
git-annex 10.20251029-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 75,300 kB
  • sloc: haskell: 91,492; javascript: 9,103; sh: 1,593; makefile: 216; perl: 137; ansic: 44
file content (38 lines) | stat: -rw-r--r-- 1,043 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
{- Opening files
 -
 - Copyright 2024 Joey Hess <id@joeyh.name>
 -
 - License: BSD-2-clause
 -}

{-# LANGUAGE CPP #-}

module Utility.OpenFile where

#ifndef mingw32_HOST_OS

import System.IO
import System.Posix.IO
import GHC.IO.FD
import GHC.IO.Handle.FD
import GHC.IO.Device

import Utility.OpenFd
import Utility.RawFilePath
import Utility.FileSystemEncoding

{- Usually, opening a Handle to a file that another thread also has open
 - for write is prevented, which avoids a lot of concurrency bugs especially
 - with lazy IO.
 - 
 - However, sometimes one thread is writing and another thread really wants
 - to read from the same file. This bypasses the usual locking, by claiming
 - that an opened FD is a Stream.
 -}
openFileBeingWritten :: RawFilePath -> IO Handle
openFileBeingWritten f = do
	fd <- openFdWithMode f ReadOnly Nothing defaultFileFlags (CloseOnExecFlag True)
	(fd', fdtype) <- mkFD (fromIntegral fd) ReadMode (Just (Stream, 0, 0)) False False
	mkHandleFromFD fd' fdtype (fromRawFilePath f) ReadMode False Nothing

#endif