87 lines
11 KiB
HTML
87 lines
11 KiB
HTML
<?xml version="1.0" encoding="UTF-8"?>
|
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
|
|
<html>
|
|
<head>
|
|
<!-- Generated by HsColour, http://code.haskell.org/~malcolm/hscolour/ -->
|
|
<title>src/System/Posix/FD.hs</title>
|
|
<link type='text/css' rel='stylesheet' href='hscolour.css' />
|
|
</head>
|
|
<body>
|
|
<pre><a name="line-1"></a><span class='hs-comment'>-- |</span>
|
|
<a name="line-2"></a><span class='hs-comment'>-- Module : System.Posix.FD</span>
|
|
<a name="line-3"></a><span class='hs-comment'>-- Copyright : © 2016 Julian Ospald</span>
|
|
<a name="line-4"></a><span class='hs-comment'>-- License : BSD3</span>
|
|
<a name="line-5"></a><span class='hs-comment'>--</span>
|
|
<a name="line-6"></a><span class='hs-comment'>-- Maintainer : Julian Ospald <hasufell@posteo.de></span>
|
|
<a name="line-7"></a><span class='hs-comment'>-- Stability : experimental</span>
|
|
<a name="line-8"></a><span class='hs-comment'>-- Portability : portable</span>
|
|
<a name="line-9"></a><span class='hs-comment'>--</span>
|
|
<a name="line-10"></a><span class='hs-comment'>-- Provides an alternative for `System.Posix.IO.ByteString.openFd`</span>
|
|
<a name="line-11"></a><span class='hs-comment'>-- which gives us more control on what status flags to pass to the</span>
|
|
<a name="line-12"></a><span class='hs-comment'>-- low-level @open(2)@ call, in contrast to the unix package.</span>
|
|
<a name="line-13"></a>
|
|
<a name="line-14"></a>
|
|
<a name="line-15"></a><span class='hs-comment'>{-# LANGUAGE ForeignFunctionInterface #-}</span>
|
|
<a name="line-16"></a><span class='hs-comment'>{-# LANGUAGE OverloadedStrings #-}</span>
|
|
<a name="line-17"></a><span class='hs-comment'>{-# LANGUAGE TupleSections #-}</span>
|
|
<a name="line-18"></a>
|
|
<a name="line-19"></a><span class='hs-comment'>{-# OPTIONS_GHC -Wall #-}</span>
|
|
<a name="line-20"></a>
|
|
<a name="line-21"></a>
|
|
<a name="line-22"></a><span class='hs-keyword'>module</span> <span class='hs-conid'>System</span><span class='hs-varop'>.</span><span class='hs-conid'>Posix</span><span class='hs-varop'>.</span><span class='hs-conid'>FD</span> <span class='hs-layout'>(</span>
|
|
<a name="line-23"></a> <span class='hs-varid'>openFd</span>
|
|
<a name="line-24"></a><span class='hs-layout'>)</span> <span class='hs-keyword'>where</span>
|
|
<a name="line-25"></a>
|
|
<a name="line-26"></a>
|
|
<a name="line-27"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Foreign</span><span class='hs-varop'>.</span><span class='hs-conid'>C</span><span class='hs-varop'>.</span><span class='hs-conid'>String</span>
|
|
<a name="line-28"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Foreign</span><span class='hs-varop'>.</span><span class='hs-conid'>C</span><span class='hs-varop'>.</span><span class='hs-conid'>Types</span>
|
|
<a name="line-29"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>System</span><span class='hs-varop'>.</span><span class='hs-conid'>Posix</span><span class='hs-varop'>.</span><span class='hs-conid'>Directory</span><span class='hs-varop'>.</span><span class='hs-conid'>Foreign</span>
|
|
<a name="line-30"></a><span class='hs-keyword'>import</span> <span class='hs-keyword'>qualified</span> <span class='hs-conid'>System</span><span class='hs-varop'>.</span><span class='hs-conid'>Posix</span> <span class='hs-keyword'>as</span> <span class='hs-conid'>Posix</span>
|
|
<a name="line-31"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>System</span><span class='hs-varop'>.</span><span class='hs-conid'>Posix</span><span class='hs-varop'>.</span><span class='hs-conid'>ByteString</span><span class='hs-varop'>.</span><span class='hs-conid'>FilePath</span>
|
|
<a name="line-32"></a>
|
|
<a name="line-33"></a>
|
|
<a name="line-34"></a><span class='hs-keyword'>foreign</span> <span class='hs-keyword'>import</span> <span class='hs-keyword'>ccall</span> <span class='hs-keyword'>unsafe</span> <span class='hs-str'>"open"</span>
|
|
<a name="line-35"></a> <span class='hs-varid'>c_open</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>CString</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>CInt</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Posix</span><span class='hs-varop'>.</span><span class='hs-conid'>CMode</span> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>IO</span> <span class='hs-conid'>CInt</span>
|
|
<a name="line-36"></a>
|
|
<a name="line-37"></a>
|
|
<a name="line-38"></a><a name="open_"></a><span class='hs-definition'>open_</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>CString</span>
|
|
<a name="line-39"></a> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Posix</span><span class='hs-varop'>.</span><span class='hs-conid'>OpenMode</span>
|
|
<a name="line-40"></a> <span class='hs-keyglyph'>-></span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Flags</span><span class='hs-keyglyph'>]</span>
|
|
<a name="line-41"></a> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Maybe</span> <span class='hs-conid'>Posix</span><span class='hs-varop'>.</span><span class='hs-conid'>FileMode</span>
|
|
<a name="line-42"></a> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>IO</span> <span class='hs-conid'>Posix</span><span class='hs-varop'>.</span><span class='hs-conid'>Fd</span>
|
|
<a name="line-43"></a><span class='hs-definition'>open_</span> <span class='hs-varid'>str</span> <span class='hs-varid'>how</span> <span class='hs-varid'>optional_flags</span> <span class='hs-varid'>maybe_mode</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>do</span>
|
|
<a name="line-44"></a> <span class='hs-varid'>fd</span> <span class='hs-keyglyph'><-</span> <span class='hs-varid'>c_open</span> <span class='hs-varid'>str</span> <span class='hs-varid'>all_flags</span> <span class='hs-varid'>mode_w</span>
|
|
<a name="line-45"></a> <span class='hs-varid'>return</span> <span class='hs-layout'>(</span><span class='hs-conid'>Posix</span><span class='hs-varop'>.</span><span class='hs-conid'>Fd</span> <span class='hs-varid'>fd</span><span class='hs-layout'>)</span>
|
|
<a name="line-46"></a> <span class='hs-keyword'>where</span>
|
|
<a name="line-47"></a> <span class='hs-varid'>all_flags</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>unionFlags</span> <span class='hs-varop'>$</span> <span class='hs-varid'>optional_flags</span> <span class='hs-varop'>++</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>open_mode</span><span class='hs-keyglyph'>]</span> <span class='hs-varop'>++</span> <span class='hs-varid'>creat</span>
|
|
<a name="line-48"></a>
|
|
<a name="line-49"></a>
|
|
<a name="line-50"></a> <span class='hs-layout'>(</span><span class='hs-varid'>creat</span><span class='hs-layout'>,</span> <span class='hs-varid'>mode_w</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>case</span> <span class='hs-varid'>maybe_mode</span> <span class='hs-keyword'>of</span>
|
|
<a name="line-51"></a> <span class='hs-conid'>Nothing</span> <span class='hs-keyglyph'>-></span> <span class='hs-layout'>(</span><span class='hs-conid'>[]</span><span class='hs-layout'>,</span><span class='hs-num'>0</span><span class='hs-layout'>)</span>
|
|
<a name="line-52"></a> <span class='hs-conid'>Just</span> <span class='hs-varid'>x</span> <span class='hs-keyglyph'>-></span> <span class='hs-layout'>(</span><span class='hs-keyglyph'>[</span><span class='hs-varid'>oCreat</span><span class='hs-keyglyph'>]</span><span class='hs-layout'>,</span> <span class='hs-varid'>x</span><span class='hs-layout'>)</span>
|
|
<a name="line-53"></a>
|
|
<a name="line-54"></a> <span class='hs-varid'>open_mode</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>case</span> <span class='hs-varid'>how</span> <span class='hs-keyword'>of</span>
|
|
<a name="line-55"></a> <span class='hs-conid'>Posix</span><span class='hs-varop'>.</span><span class='hs-conid'>ReadOnly</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>oRdonly</span>
|
|
<a name="line-56"></a> <span class='hs-conid'>Posix</span><span class='hs-varop'>.</span><span class='hs-conid'>WriteOnly</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>oWronly</span>
|
|
<a name="line-57"></a> <span class='hs-conid'>Posix</span><span class='hs-varop'>.</span><span class='hs-conid'>ReadWrite</span> <span class='hs-keyglyph'>-></span> <span class='hs-varid'>oRdwr</span>
|
|
<a name="line-58"></a>
|
|
<a name="line-59"></a>
|
|
<a name="line-60"></a><a name="openFd"></a><span class='hs-comment'>-- |Open and optionally create this file. See 'System.Posix.Files'</span>
|
|
<a name="line-61"></a><span class='hs-comment'>-- for information on how to use the 'FileMode' type.</span>
|
|
<a name="line-62"></a><span class='hs-comment'>--</span>
|
|
<a name="line-63"></a><span class='hs-comment'>-- Note that passing @Just x@ as the 4th argument triggers the</span>
|
|
<a name="line-64"></a><span class='hs-comment'>-- `oCreat` status flag, which must be set when you pass in `oExcl`</span>
|
|
<a name="line-65"></a><span class='hs-comment'>-- to the status flags. Also see the manpage for @open(2)@.</span>
|
|
<a name="line-66"></a><span class='hs-definition'>openFd</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>RawFilePath</span>
|
|
<a name="line-67"></a> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Posix</span><span class='hs-varop'>.</span><span class='hs-conid'>OpenMode</span>
|
|
<a name="line-68"></a> <span class='hs-keyglyph'>-></span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Flags</span><span class='hs-keyglyph'>]</span> <span class='hs-comment'>-- ^ status flags of @open(2)@</span>
|
|
<a name="line-69"></a> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>Maybe</span> <span class='hs-conid'>Posix</span><span class='hs-varop'>.</span><span class='hs-conid'>FileMode</span> <span class='hs-comment'>-- ^ @Just x@ => creates the file with the given modes, Nothing => the file must exist.</span>
|
|
<a name="line-70"></a> <span class='hs-keyglyph'>-></span> <span class='hs-conid'>IO</span> <span class='hs-conid'>Posix</span><span class='hs-varop'>.</span><span class='hs-conid'>Fd</span>
|
|
<a name="line-71"></a><span class='hs-definition'>openFd</span> <span class='hs-varid'>name</span> <span class='hs-varid'>how</span> <span class='hs-varid'>optional_flags</span> <span class='hs-varid'>maybe_mode</span> <span class='hs-keyglyph'>=</span>
|
|
<a name="line-72"></a> <span class='hs-varid'>withFilePath</span> <span class='hs-varid'>name</span> <span class='hs-varop'>$</span> <span class='hs-keyglyph'>\</span><span class='hs-varid'>str</span> <span class='hs-keyglyph'>-></span>
|
|
<a name="line-73"></a> <span class='hs-varid'>throwErrnoPathIfMinus1Retry</span> <span class='hs-str'>"openFd"</span> <span class='hs-varid'>name</span> <span class='hs-varop'>$</span>
|
|
<a name="line-74"></a> <span class='hs-varid'>open_</span> <span class='hs-varid'>str</span> <span class='hs-varid'>how</span> <span class='hs-varid'>optional_flags</span> <span class='hs-varid'>maybe_mode</span>
|
|
<a name="line-75"></a>
|
|
</pre></body>
|
|
</html>
|