summaryrefslogtreecommitdiff
path: root/source/Felix/OutputPlan.hs
blob: 78d7e7af9fc6f46f3151864740a9ab18e3b3d86e (plain)
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE NoImplicitPrelude #-}

-- | Authority-free reservation of verification output namespaces.
module Felix.OutputPlan
    ( DumpOutputPlan
    , dumpOutputPath
    , VerificationOutputPlan
    , verificationDumpOutput
    , verificationHtmlRoutes
    , OutputNamespaceOwner(..)
    , OutputNamespaceCollision(..)
    , OutputPlanError(..)
    , renderOutputPlanError
    , planVerificationOutputs
    ) where

import Base
import Felix.Source
import Felix.Store
import Felix.Render.Html.Output qualified as Html

import Control.Exception (displayException)
import Data.List qualified as List
import Data.Text qualified as Text
import System.Directory qualified as Directory
import System.FilePath.Posix qualified as Posix


newtype DumpOutputPlan = DumpOutputPlan FilePath
    deriving stock (Show, Eq)

dumpOutputPath :: DumpOutputPlan -> FilePath
dumpOutputPath (DumpOutputPlan path) =
    path

data VerificationOutputPlan = VerificationOutputPlan
    !(Maybe DumpOutputPlan)
    !(Maybe Html.HtmlRoutePlan)

verificationDumpOutput
    :: VerificationOutputPlan
    -> Maybe DumpOutputPlan
verificationDumpOutput
        (VerificationOutputPlan dump _html) =
    dump

verificationHtmlRoutes
    :: VerificationOutputPlan
    -> Maybe Html.HtmlRoutePlan
verificationHtmlRoutes
        (VerificationOutputPlan _dump html) =
    html

data OutputNamespaceOwner
    = StoreOutputNamespace
    | StoreJournalOutputNamespace
    | DumpOutputNamespace
    | HtmlOutputArtifact !SafeRelativePath
    deriving stock (Show, Eq, Ord)

data OutputNamespaceCollision = OutputNamespaceCollision
    !OutputNamespaceOwner
    !FilePath
    !OutputNamespaceOwner
    !FilePath
    deriving stock (Show, Eq)

data OutputPlanError
    = EmptyDumpDestination
    | OutputPathInspectionFailed !FilePath !Text
    | DumpDestinationNotDirectory !FilePath
    | DumpDestinationNotEmpty !FilePath ![FilePath]
    | HtmlRoutePlanFailed !Html.HtmlOutputError
    | CollidingOutputNamespaces
        !(NonEmpty OutputNamespaceCollision)
    deriving stock (Show, Eq)

renderOutputPlanError :: OutputPlanError -> Text
renderOutputPlanError = \case
    EmptyDumpDestination ->
        "--dump requires a nonempty destination"
    OutputPathInspectionFailed path reason ->
        "could not inspect output path " <> quotePath path <> ": " <> reason
    DumpDestinationNotDirectory path ->
        "dump destination is not a directory: " <> quotePath path
    DumpDestinationNotEmpty path contents ->
        "dump destination " <> quotePath path
            <> " is not empty; choose an absent or empty directory"
            <> case contents of
                [] -> ""
                _ -> "; found "
                    <> Text.intercalate ", " (quotePath <$> contents)
    HtmlRoutePlanFailed failure ->
        Html.renderHtmlOutputError failure
    CollidingOutputNamespaces collisions ->
        "verification output namespaces collide: "
            <> Text.intercalate "; "
                (renderCollision <$> toList collisions)
  where
    renderCollision
        (OutputNamespaceCollision leftOwner leftPath rightOwner rightPath) =
            renderOwner leftOwner <> " " <> quotePath leftPath
                <> " conflicts with " <> renderOwner rightOwner
                <> " " <> quotePath rightPath

    renderOwner = \case
        StoreOutputNamespace -> "store"
        StoreJournalOutputNamespace -> "store rollback journal"
        DumpOutputNamespace -> "dump directory"
        HtmlOutputArtifact relative ->
            "HTML artifact "
                <> quotePath (safeRelativePathFilePath relative)

quotePath :: FilePath -> Text
quotePath = Text.pack . show

planVerificationOutputs
    :: StorePath
    -> Maybe FilePath
    -> Maybe (FilePath, [SafeRelativePath])
    -> IO (Either OutputPlanError VerificationOutputPlan)
planVerificationOutputs storePath requestedDump requestedHtml = do
    dumpResult <- traverse planDumpOutput requestedDump
    case sequence dumpResult of
        Left failure ->
            pure (Left failure)
        Right dump -> do
            htmlResult <- traverse
                (uncurry Html.planHtmlRoutes)
                requestedHtml
            case sequence htmlResult of
                Left failure ->
                    pure (Left (HtmlRoutePlanFailed failure))
                Right html ->
                    pure do
                        rejectCollisions storePath dump html
                        Right
                            (VerificationOutputPlan
                                dump html)

planDumpOutput
    :: FilePath
    -> IO (Either OutputPlanError DumpOutputPlan)
planDumpOutput requested
    | null requested =
        pure (Left EmptyDumpDestination)
    | otherwise = do
        absoluteResult <- inspectPath requested
            (Posix.normalise
                <$> Directory.makeAbsolute requested)
        case absoluteResult of
            Left failure ->
                pure (Left failure)
            Right absolute -> do
                existsResult <- inspectPath absolute
                    (Directory.doesPathExist absolute)
                case existsResult of
                    Left failure ->
                        pure (Left failure)
                    Right False ->
                        pure (Right (DumpOutputPlan absolute))
                    Right True -> do
                        directoryResult <- inspectPath absolute
                            (Directory.doesDirectoryExist absolute)
                        case directoryResult of
                            Left failure ->
                                pure (Left failure)
                            Right False ->
                                pure
                                    (Left
                                        (DumpDestinationNotDirectory
                                            absolute))
                            Right True -> do
                                contentsResult <- inspectPath absolute
                                    (List.sort
                                        <$> Directory.listDirectory
                                            absolute)
                                pure
                                    (case contentsResult of
                                        Left failure ->
                                            Left failure
                                        Right [] ->
                                            Right
                                                (DumpOutputPlan absolute)
                                        Right contents ->
                                            Left
                                                (DumpDestinationNotEmpty
                                                    absolute
                                                    contents))

rejectCollisions
    :: StorePath
    -> Maybe DumpOutputPlan
    -> Maybe Html.HtmlRoutePlan
    -> Either OutputPlanError ()
rejectCollisions storePath dump html =
    case collisions of
        [] ->
            Right ()
        firstCollision : remaining ->
            Left
                (CollidingOutputNamespaces
                    (firstCollision :| remaining))
  where
    stores =
        [ ( StoreOutputNamespace
          , storePathFilePath storePath
          )
        , ( StoreJournalOutputNamespace
          , storeRollbackJournalPath storePath
          )
        ]
    dumpNamespace =
        (\planned ->
            ( DumpOutputNamespace
            , dumpOutputPath planned
            ))
            <$> dump
    htmlArtifacts =
        case html of
            Nothing ->
                []
            Just routes ->
                [ (HtmlOutputArtifact relative, destination)
                | (relative, destination) <-
                    Html.htmlRoutePlanDestinations routes
                ]
    pairs =
        maybe
            []
            (\dumpOwner ->
                [(storeOwner, dumpOwner) | storeOwner <- stores])
            dumpNamespace
            <> [ (storeOwner, htmlOwner)
               | storeOwner <- stores
               , htmlOwner <- htmlArtifacts
               ]
            <> maybe
                []
                (\dumpOwner ->
                    [(dumpOwner, htmlOwner) | htmlOwner <- htmlArtifacts])
                dumpNamespace
    collisions =
        [ OutputNamespaceCollision
            leftOwner leftPath rightOwner rightPath
        | ( (leftOwner, leftPath)
          , (rightOwner, rightPath)
          ) <- pairs
        , namespacesOverlap leftPath rightPath
        ]

namespacesOverlap :: FilePath -> FilePath -> Bool
namespacesOverlap left right =
    components left `List.isPrefixOf` components right
        || components right `List.isPrefixOf` components left
  where
    components =
        Posix.splitDirectories
            . Posix.dropTrailingPathSeparator
            . Posix.normalise

inspectPath
    :: FilePath
    -> IO value
    -> IO (Either OutputPlanError value)
inspectPath path action = do
    result <- tryIOError action
    pure
        (case result of
            Left failure ->
                Left
                    (OutputPathInspectionFailed
                        path
                        (Text.pack
                            (displayException failure)))
            Right value ->
                Right value)