Skip to content

Commit 030e3af

Browse files
committed
Apply review suggestions
1 parent f733c80 commit 030e3af

File tree

3 files changed

+15
-57
lines changed

3 files changed

+15
-57
lines changed

System/File/OsPath/Internal.hs

Lines changed: 5 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module System.File.OsPath.Internal where
99

1010
import qualified System.File.Platform as P
1111

12-
import Prelude ((.), ($), String, IO, ioError, pure, either, const, flip, Maybe(..), fmap, (<$>), id, Bool(..), FilePath, (++), return, show, (>>=), (==), otherwise, errorWithoutStackTrace, userError, mempty)
12+
import Prelude ((.), ($), String, IO, ioError, pure, either, const, flip, Maybe(..), fmap, (<$>), id, Bool(..), FilePath, (++), return, show, (>>=), (==), otherwise, userError)
1313
import GHC.IO (catchException)
1414
import GHC.IO.Exception (IOException(..))
1515
import GHC.IO.Handle (hClose_help)
@@ -26,9 +26,10 @@ import System.OsString.Internal.Types
2626

2727
import qualified Data.ByteString as BS
2828
import qualified Data.ByteString.Lazy as BSL
29-
import qualified System.OsString as OSS
3029
import System.Posix.Types (CMode)
31-
#if !MIN_VERSION_filepath(1, 5, 0)
30+
#if MIN_VERSION_filepath(1, 5, 0)
31+
import qualified System.OsString as OSS
32+
#else
3233
import Data.Coerce
3334
#endif
3435

@@ -245,46 +246,15 @@ openTempFile' loc (OsString tmp_dir) template@(OsString tmpl) binary mode
245246
-- We split off the last extension, so we can use .foo.ext files
246247
-- for temporary files (hidden on Unix OSes). Unfortunately we're
247248
-- below filepath in the hierarchy here.
248-
(OsString prefix, OsString suffix) =
249-
case break_ (== OSS.unsafeFromChar '.') $ reverse_ template of
250-
-- First case: template contains no '.'s. Just re-reverse it.
251-
(rev_suffix, xs)
252-
| xs == mempty -> (reverse_ rev_suffix, mempty)
253-
-- Second case: template contains at least one '.'. Strip the
254-
-- dot from the prefix and prepend it to the suffix (if we don't
255-
-- do this, the unique number will get added after the '.' and
256-
-- thus be part of the extension, which is wrong.)
257-
(rev_suffix, xs)
258-
| (h:rest) <- OSS.unpack xs
259-
, h == unsafeFromChar '.' -> (reverse_ (OSS.pack rest), cons_ (unsafeFromChar '.') $ reverse_ rev_suffix)
260-
-- Otherwise, something is wrong, because (break (== '.')) should
261-
-- always return a pair with either the empty string or a string
262-
-- beginning with '.' as the second component.
263-
_ -> errorWithoutStackTrace "bug in System.IO.openTempFile"
249+
(OsString prefix, OsString suffix) = OSP.splitExtension template
264250

265251
#if MIN_VERSION_filepath(1, 5, 0)
266252
any_ :: (OsChar -> Bool) -> OsString -> Bool
267253
any_ = OSS.any
268254

269-
cons_ :: OsChar -> OsString -> OsString
270-
cons_ = OSS.cons
271-
272-
break_ :: (OsChar -> Bool) -> OsString -> (OsString, OsString)
273-
break_ = OSS.break
274-
275-
reverse_ :: OsString -> OsString
276-
reverse_ = OSS.reverse
277255
#else
278256
any_ :: (OsChar -> Bool) -> OsString -> Bool
279257
any_ = coerce P.any_
280258

281-
cons_ :: OsChar -> OsString -> OsString
282-
cons_ = coerce P.cons_
283-
284-
break_ :: (OsChar -> Bool) -> OsString -> (OsString, OsString)
285-
break_ = coerce P.break_
286-
287-
reverse_ :: OsString -> OsString
288-
reverse_ = coerce P.reverse_
289259
#endif
290260

posix/System/File/Platform.hs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ import Data.Coerce (coerce)
3434
import "filepath" System.OsString.Internal.Types (PosixString(..), PosixChar(..))
3535
import qualified "filepath" System.OsPath.Data.ByteString.Short as BC
3636
#endif
37+
import System.CPUTime (cpuTimePrecision, getCPUTime)
38+
import Text.Printf (printf)
3739

3840
-- | Open a file and return the 'Handle'.
3941
openFile :: PosixPath -> IOMode -> IO Handle
@@ -110,9 +112,10 @@ tempCounter = unsafePerformIO $ newIORef 0
110112
-- build large digit-alike number
111113
rand_string :: IO PosixString
112114
rand_string = do
113-
r1 <- c_getpid
115+
r1 <- fromIntegral @_ @Int <$> c_getpid
114116
(r2, _) <- atomicModifyIORef'_ tempCounter (+1)
115-
return $ PS.pack $ fmap (PS.unsafeFromChar) (show r1 ++ "-" ++ show r2)
117+
r3 <- (`quot` cpuTimePrecision) <$> getCPUTime
118+
return $ PS.pack $ fmap (PS.unsafeFromChar) (printf "%x-%x-%x" r1 r2 r3)
116119

117120
lenientDecode :: PosixString -> String
118121
lenientDecode ps = let utf8' = PS.decodeWith utf8 ps
@@ -124,17 +127,8 @@ lenientDecode ps = let utf8' = PS.decodeWith utf8 ps
124127

125128
#if !MIN_VERSION_filepath(1, 5, 0)
126129

127-
break_ :: (PosixChar -> Bool) -> PosixString -> (PosixString, PosixString)
128-
break_ = coerce BC.break
129-
130-
reverse_ :: PosixString -> PosixString
131-
reverse_ = coerce BC.reverse
132-
133130
any_ :: (PosixChar -> Bool) -> PosixString -> Bool
134131
any_ = coerce BC.any
135132

136-
cons_ :: PosixChar -> PosixString -> PosixString
137-
cons_ = coerce BC.cons
138-
139133
#endif
140134

windows/System/File/Platform.hsc

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,11 @@ import Foreign.Ptr
3535
import Foreign.Marshal.Alloc
3636
import Foreign.Marshal.Utils (with)
3737
import Foreign.Storable
38+
import System.CPUTime (cpuTimePrecision, getCPUTime)
3839
import System.Posix.Types (CMode)
3940
import System.IO.Unsafe (unsafePerformIO)
4041
import System.Posix.Internals (c_getpid, o_EXCL)
42+
import Text.Printf (printf)
4143

4244
#if MIN_VERSION_filepath(1, 5, 0)
4345
import System.OsString.Encoding
@@ -193,9 +195,10 @@ tempCounter = unsafePerformIO $ newIORef 0
193195
-- build large digit-alike number
194196
rand_string :: IO WindowsPath
195197
rand_string = do
196-
r1 <- c_getpid
198+
r1 <- fromIntegral @_ @Int <$> c_getpid
197199
(r2, _) <- atomicModifyIORef'_ tempCounter (+1)
198-
return $ WS.pack $ fmap (WS.unsafeFromChar) (show r1 ++ "-" ++ show r2)
200+
r3 <- (`quot` cpuTimePrecision) <$> getCPUTime
201+
return $ WS.pack $ fmap (WS.unsafeFromChar) (printf "%x-%x-%x" r1 r2 r3)
199202

200203
lenientDecode :: WindowsString -> String
201204
lenientDecode ws = let utf16le' = WS.decodeWith utf16le_b ws
@@ -221,17 +224,8 @@ toHandle fp iomode h = (`onException` Win32.closeHandle h) $ do
221224

222225
#if !MIN_VERSION_filepath(1, 5, 0)
223226

224-
break_ :: (WindowsChar -> Bool) -> WindowsString -> (WindowsString, WindowsString)
225-
break_ = coerce BC.break
226-
227-
reverse_ :: WindowsString -> WindowsString
228-
reverse_ = coerce BC.reverse
229-
230227
any_ :: (WindowsChar -> Bool) -> WindowsString -> Bool
231228
any_ = coerce BC.any
232229

233-
cons_ :: WindowsChar -> WindowsString -> WindowsString
234-
cons_ = coerce BC.cons
235-
236230
#endif
237231

0 commit comments

Comments
 (0)