aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/world/file.lux
blob: 4793f2fa2a83b3376766ea0dca1a75afe34fa32a (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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
(.module:
  [lux #*
   [control
    ["." monad (#+ Monad do)]
    ["ex" exception (#+ Exception exception:)]
    [concurrency
     ["." promise (#+ Promise)]]
    [security
     ["." integrity (#+ Dirty)]
     ["." capability (#+ Capability)]]]
   [data
    ["." maybe]
    ["." error (#+ Error) ("error/." Functor<Error>)]
    ["." text
     format]
    [collection
     ["." array (#+ Array)]
     ["." list ("list/." Functor<List>)]]]
   [time
    ["." instant (#+ Instant)]
    ["." duration]]
   [world
    ["." binary (#+ Binary)]]
   ["." io (#+ IO) ("io/." Functor<IO>)]
   [host (#+ import:)]
   [platform
    [compiler
     ["." host]]]])

(type: #export Path Text)

(type: #export (Can-Open ! capability)
  (Capability Path (! (Error (capability !)))))

(do-template [<capability> <input> <output>]
  [(type: #export (<capability> !)
     (Capability <input> (! (Error <output>))))]

  [Can-Edit   [Binary] Any]
  [Can-Delete []       Any]
  )

(type: #export (Can-Query ! o)
  (Capability [] (! (Error o))))

(type: #export (Can-Modify ! i)
  (Capability [i] (! (Error Any))))

(`` (signature: #export (File !)
      (~~ (do-template [<name> <output>]
            [(: (Can-Query ! <output>)
                <name>)]

            [size          Nat]
            [last-modified Instant]
            [can-execute?  Bit]
            [content       (Dirty Binary)]
            ))

      (: (Can-Open ! File)
         move)

      (~~ (do-template [<name> <input>]
            [(: (Can-Modify ! <input>)
                <name>)]

            [modify     Instant]
            [over-write Binary]
            ))

      (: (Can-Edit !)
         append)

      (: (Can-Delete !)
         delete)
      ))

(signature: #export (Directory !)
  (: (Can-Query ! (List (File !)))
     files)

  (: (Can-Query ! (List (Directory !)))
     directories)

  (: (Can-Delete !)
     discard))

(`` (signature: #export (System !)
      (~~ (do-template [<name> <capability>]
            [(: (Can-Open ! <capability>)
                <name>)]

            [file             File]
            [create-file      File]
            [directory        Directory]
            [create-directory Directory]
            ))

      (: Text
         separator)
      ))

(def: (async-file file)
  (-> (File IO) (File Promise))
  (`` (structure
       (~~ (do-template [<name>]
             [(def: <name> (|>> (:: file <name>) promise.future))]

             [size] [last-modified] [can-execute?] [content]
             [modify] [over-write]
             [append]
             [delete]))

       (def: move (|>> (:: file move) (io/map (error/map async-file)) promise.future)))))

(def: (async-directory directory)
  (-> (Directory IO) (Directory Promise))
  (`` (structure (~~ (do-template [<name> <async>]
                       [(def: <name> (|>> (:: directory <name>)
                                          (io/map (error/map (list/map <async>)))
                                          promise.future))]

                       [files       async-file]
                       [directories async-directory]))

                 (def: discard (|>> (:: directory discard) promise.future)))))

(def: #export (async system)
  (-> (System IO) (System Promise))
  (`` (structure
       (~~ (do-template [<name> <async>]
             [(def: <name> (|>> (:: system <name>) (io/map (error/map <async>)) promise.future))]

             [file             async-file]
             [create-file      async-file]
             [directory        async-directory]
             [create-directory async-directory]))

       (def: separator (:: system separator)))))

(def: #export (un-nest System<!> file)
  (All [!] (-> (System !) Path (Maybe [Path Text])))
  (case (text.last-index-of (:: System<!> separator) file)
    #.None
    #.None
    
    (#.Some last-separator)
    (let [[parent temp] (maybe.assume (text.split last-separator file))
          [_ child] (maybe.assume (text.split (text.size (:: System<!> separator)) temp))]
      (#.Some [parent child]))))

(def: #export (nest System<!> [parent child])
  (All [!] (-> (System !) [Path Text] Path))
  (format parent (:: System<!> separator) child))

(do-template [<name>]
  [(exception: #export (<name> {file Path})
     (ex.report ["Path" file]))]

  [cannot-create-file]
  [cannot-find-file]
  [cannot-delete-file]

  [cannot-create-directory]
  [cannot-find-directory]
  [cannot-discard-directory]
  
  [cannot-read-all-data]
  [not-a-directory]
  )

(exception: #export (cannot-move {target Path} {source Path})
  (ex.report ["Source" source]
             ["Target" target]))

(exception: #export (cannot-modify {instant Instant} {file Path})
  (ex.report ["Instant" (%instant instant)]
             ["Path" file]))

(template: (!delete path exception)
  (do io.Monad<IO>
    [outcome (java/io/File::delete (java/io/File::new path))]
    (case outcome
      (#error.Success #1)
      (wrap (#error.Success []))

      _
      (io.throw exception [path]))))

(`` (for {(~~ (static host.jvm))
          (as-is (import: #long java/io/File
                   (new [String])
                   (~~ (do-template [<name>]
                         [(<name> [] #io #try boolean)]

                         [createNewFile] [mkdir]
                         [exists] [delete]
                         [isFile] [isDirectory]
                         [canRead] [canWrite] [canExecute]))
                   
                   (length [] #io #try long)
                   (listFiles [] #io #try #? (Array java/io/File))
                   (getAbsolutePath [] #io #try String)
                   (renameTo [java/io/File] #io #try boolean)
                   (lastModified [] #io #try long)
                   (setLastModified [long] #io #try boolean)
                   (#static separator String))

                 (import: java/lang/AutoCloseable
                   (close [] #io #try void))

                 (import: java/io/OutputStream
                   (write [(Array byte)] #io #try void)
                   (flush [] #io #try void))

                 (import: java/io/FileOutputStream
                   (new [java/io/File boolean] #io #try))

                 (import: java/io/InputStream
                   (read [(Array byte)] #io #try int))

                 (import: java/io/FileInputStream
                   (new [java/io/File] #io #try))

                 (structure: (File<IO> path)
                   (-> Path (File IO))

                   (~~ (do-template [<name> <flag>]
                         [(def: (<name> data)
                            (do io.Monad<Process>
                              [stream (FileOutputStream::new (java/io/File::new path) <flag>)
                               _ (OutputStream::write data stream)
                               _ (OutputStream::flush stream)]
                              (AutoCloseable::close stream)))]

                         [over-write  #0]
                         [append      #1]
                         ))

                   (def: (content _)
                     (do io.Monad<Process>
                       [#let [file (java/io/File::new path)]
                        size (java/io/File::length file)
                        #let [data (binary.create (.nat size))]
                        stream (FileInputStream::new file)
                        bytes-read (InputStream::read data stream)
                        _ (AutoCloseable::close stream)]
                       (if (i/= size bytes-read)
                         (wrap (integrity.taint data))
                         (io.io (ex.throw cannot-read-all-data path)))))

                   (def: (size _)
                     (|> path
                         java/io/File::new
                         java/io/File::length
                         (:: io.Monad<Process> map .nat)))

                   (def: (last-modified _)
                     (|> path
                         java/io/File::new
                         (java/io/File::lastModified)
                         (:: io.Monad<Process> map (|>> duration.from-millis instant.absolute))))

                   (def: (can-execute? _)
                     (|> path
                         java/io/File::new
                         java/io/File::canExecute))

                   (def: (move destination)
                     (do io.Monad<IO>
                       [outcome (java/io/File::renameTo (java/io/File::new destination)
                                                        (java/io/File::new path))]
                       (case outcome
                         (#error.Success #1)
                         (wrap (#error.Success (File<IO> destination)))

                         _
                         (io.throw cannot-move [destination path]))))

                   (def: (modify time-stamp)
                     (do io.Monad<IO>
                       [outcome (java/io/File::setLastModified (|> time-stamp instant.relative duration.to-millis)
                                                               (java/io/File::new path))]
                       (case outcome
                         (#error.Success #1)
                         (wrap (#error.Success []))

                         _
                         (io.throw cannot-modify [time-stamp path]))))

                   (def: (delete _)
                     (!delete path cannot-delete-file)))

                 (structure: (Directory<IO> path)
                   (-> Path (Directory IO))

                   (~~ (do-template [<name> <method> <capability>]
                         [(def: (<name> _)
                            (do io.Monad<Process>
                              [?children (java/io/File::listFiles (java/io/File::new path))]
                              (case ?children
                                (#.Some children)
                                (|> children
                                    array.to-list
                                    (monad.filter @ (|>> <method>))
                                    (:: @ map (monad.map @ (|>> java/io/File::getAbsolutePath (:: @ map  <capability>))))
                                    (:: @ join))

                                #.None
                                (io.throw not-a-directory [path]))))]

                         [files       java/io/File::isFile      File<IO>]
                         [directories java/io/File::isDirectory Directory<IO>]
                         ))

                   (def: (discard _)
                     (!delete path cannot-discard-directory)))

                 (structure: #export _ (System IO)
                   (~~ (do-template [<name> <method> <capability> <exception>]
                         [(def: (<name> path)
                            (do io.Monad<IO>
                              [#let [file (java/io/File::new path)]
                               outcome (<method> file)]
                              (case outcome
                                (#error.Success #1)
                                (wrap (#error.Success (<capability> path)))

                                _
                                (wrap (ex.throw <exception> [path])))))]

                         [file             java/io/File::isFile        ..File<IO>      cannot-find-file]
                         [create-file      java/io/File::createNewFile ..File<IO>      cannot-create-file]
                         [directory        java/io/File::isDirectory   ..Directory<IO> cannot-find-directory]
                         [create-directory java/io/File::mkdir         ..Directory<IO> cannot-create-directory]
                         ))

                   (def: separator (java/io/File::separator))
                   ))
          }))

(do-template [<get> <signature> <create> <find> <exception>]
  [(def: #export (<get> Monad<!> System<!> path)
     (All [!] (-> (Monad !) (System !) Path (! (Error (<signature> !)))))
     (do Monad<!>
       [outcome (:: System<!> <create> path)]
       (case outcome
         (#error.Success file)
         (wrap (#error.Success file))
         
         (#error.Failure error)
         (if (ex.match? <exception> error)
           (:: System<!> <find> path)
           (wrap (#error.Failure error))))))]

  [get-file      File      create-file      file      ..cannot-create-file]
  [get-directory Directory create-directory directory ..cannot-create-directory]
  )

(def: #export (exists? Monad<!> System<!> path)
  (All [!] (-> (Monad !) (System !) Path (! Bit)))
  (do Monad<!>
    [?file (:: System<!> file path)]
    (case ?file
      (#error.Success file)
      (wrap true)

      (#error.Failure _)
      (do Monad<!>
        [?directory (:: System<!> directory path)]
        (case ?directory
          (#error.Success directory)
          (wrap true)

          (#error.Failure _)
          (wrap false))))))