This file is indexed.

/usr/share/racket/collects/setup/unpack.rkt is in racket-common 6.3-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
#lang racket/base

(require file/gunzip
         racket/file
         racket/list
         racket/port
         racket/bool
         net/base64
         setup/getinfo
         racket/match
         "dirs.rkt")

(provide unpack
         fold-plt-archive)

;; ----------------------------------------

;; Returns a port and a kill thunk
(define (port64gz->port p64gz)
  ;; Inflate in a thread so the whole input isn't read at once
  (let-values ([(base64-out base64-in) (make-pipe 4096)]
               [(guz-out guz-in) (make-pipe 4096)])
    (let ([64t
           (thread (lambda ()
                     (dynamic-wind
                         void
                         (lambda () (base64-decode-stream p64gz base64-in))
                         (lambda () (close-output-port base64-in)))))]
          [gzt
           (thread (lambda ()
                     (dynamic-wind
                         void
                         (lambda () (gunzip-through-ports base64-out guz-in))
                         (lambda () (close-output-port guz-in)))))])
      (values guz-out (lambda () (kill-thread 64t) (kill-thread gzt))))))

;; ------------------------------------------------------------

;; fold-plt-archive : path[to .plt file] (sexpr A -> A) (sexpr input-port A -> A) (path A -> A) (path input-port A -> A) A -> A
(define (fold-plt-archive filename on-config-fn on-setup-unit on-directory on-file initial-value)
  (let*-values ([(fip) (open-input-file filename)]
                [(ip kill) (port64gz->port fip)])
    (dynamic-wind
        void
        (λ () (fold-plt-archive-port ip on-config-fn on-setup-unit on-directory on-file initial-value))
        (λ ()
           (close-input-port fip)
           (kill)))))

;; fold-plt-archive-port : input-port (sexpr A -> A) (sexpr input-port A -> A) (path A -> A) (path input-port A -> A) A -> A
(define (fold-plt-archive-port p on-config-fn on-setup-unit on-directory on-file initial-value)
  
  ;; skip past the initial #"PLT" and two sexprs
  (unless (and (eq? #\P (read-char p))
               (eq? #\L (read-char p))
               (eq? #\T (read-char p)))
    (error "not an unpackable distribution archive"))
  
  (let* ([config-fn-sexpr (read p)]
         [_  (when (eof-object? config-fn-sexpr) (error "malformed input"))]
         [val (on-config-fn config-fn-sexpr initial-value)]
         
         [setup-unit (read p)]
         [_ (when (eof-object? setup-unit) (error "malformed input"))]
         [val (on-setup-unit setup-unit p val)])
    
    ;; read contents of file directly. [on-setup-unit may have consumed all input,
    ;; but if so this loop will just do nothing.]
    (let loop ([val val])
      (let ([kind (read p)])
        (cond
         [(eof-object? kind) val]
         [else
          (case kind
            [(dir) 
             (let* ([v (read p)]
                    [s (expr->path-descriptor v)])
               (unless (relative-path-descriptor? s)
                 (error "expected a directory name relative path string, got" s))
               (let ([next-val (on-directory s val)])
                 (loop next-val)))]
            [(file file-replace)
             (let* ([v (read p)]
                    [s (expr->path-descriptor v)])
               (unless (relative-path-descriptor? s)
                 (error "expected a file name relative path string, got" s))
               (let ([len (read p)])
                 (unless (and (number? len) (integer? len))
                   (error "expected a file name size, got" len))
                 ;; Find starting *
                 (let loop ()
                   (let ([c (read-char p)])
                     (cond [(char=? c #\*) (void)] ; found it
                           [(char-whitespace? c) (loop)]
                           [(eof-object? c) (void)] ; signal the error below
                           [else (error 
                                  (format "unexpected character setting up ~a, looking for *" s)
                                  c)])))
                 (let-values ([(control fp) (protected-limited-input-port p len)])
                   (let ([next-val (if (procedure-arity-includes? on-file 4)
                                       (on-file s fp kind val)
                                       (on-file s fp val))])
                     (exhaust-port control)
                     (loop next-val)))))]
            [else (error "unknown file tag" kind)])])))))

;; path-descriptor ::= 'same | (list location path)
;; location        ::= symbol in '(same collects doc lib include)

;; expr->path-descriptor : sexpr -> path-descriptor
;; extracts a path-descriptor from an sexpr embedded in a .plt file
;; raises an error if the given sexpr can't be converted to a path descriptor
(define (expr->path-descriptor v)
  (cond
   [(null? v) 'same]
   [(and (pair? v) (symbol? (car v)) (symbol=? (car v) 'same))
    (apply build-path 'same (cdr v))]
   [(and (pair? v) (string? (car v)))
    (let ([location (string->loc (car v))])
      (if (eq? location 'relative)
          (apply build-path v)
          (if (null? (cdr v))
              (list location (build-path/convention-type (system-path-convention-type) 'same))
              (list location (apply build-path (cdr v))))))]
   [else (error "malformed path description: " v)]))

;; string->loc : string -> location
;; converts the string into a corresponding location, or raises an error
;; if that is not possible
(define (string->loc str)
  (let ([loc (string->symbol str)])
    (cond
     [(memq loc '(collects doc lib include same)) loc]
     [else 'relative])))

;; relative-path-descriptor? : path-descriptor -> boolean
;; determines if the given path descriptor names a relative file rather
;; than an absolute one
(define (relative-path-descriptor? s)
  (or (eq? s 'same) 
      (and (path? s) (relative-path? s))
      (relative-path? (cadr s))))

;; protected-limited-output-port input-port n -> (values input-port input-port)
;; returns two input ports. the first reads from the given input port, and the second
;; reads from the first.
;; why would you ever want to do this? So that you can hand out the second, and then
;; regardless of whether the user closes it or not you still have a limited input port
;; you can read to exhaustion.
(define (protected-limited-input-port ip limit)
  (let* ([i2 (make-limited-input-port ip limit #f)]
         [i3 (make-limited-input-port i2 limit #f)])
    (values i2 i3)))

;; exhaust-port : input-port -> void
;; consumes all input on the given port
(define exhaust-port
  (let ([nowhere (open-output-nowhere)])
    (λ (ip) (copy-port ip nowhere))))


;; ------------------------------------------------------------


(define (pretty-name f)
  (with-handlers ([void (lambda (x) f)])
    (let-values ([(base name dir?) (split-path f)])
      (format "~a in ~a"
              (path->string name)
              (if (path? base) (path->string base) base)))))

(define (shuffle-path parent-dir get-dir shuffle? v)
  (if shuffle?
      ;; Re-arrange for "collects', etc.
      (let ([v (remq* '(same) v)])
        (if (null? v)
            (values #f 'same)
            (let ([dir
                   (case (string->symbol (car v))
                     [(collects) (get-dir find-collects-dir find-user-collects-dir)]
                     [(doc)     (get-dir find-doc-dir find-user-doc-dir)]
                     [(lib)     (get-dir find-lib-dir find-user-lib-dir)]
                     [(include) (get-dir find-include-dir find-user-include-dir)]
                     [else #f])])
              (if dir
                  (if (null? (cdr v))
                      (values dir 'same)
                      (values dir (apply build-path (cdr v))))
                  (values parent-dir (apply build-path v))))))
      (values parent-dir (if (null? v) 'same (apply build-path v)))))

(define (unmztar p filter parent-dir get-dir shuffle? print-status)
  (define bufsize 4096)
  (define buffer (make-bytes bufsize))
  (let loop ()
    (let ([kind (read p)])
      (unless (eof-object? kind)
        (case kind
          [(dir) (let-values ([(target-dir s)
                               (shuffle-path parent-dir get-dir shuffle? (read p))])
                   (unless (or (eq? s 'same) (relative-path? s))
                     (error "expected a directory name relative path string, got" s))
                   (when (and target-dir
                              (or (eq? s 'same) (filter 'dir s target-dir)))
                     (let ([d (build-path target-dir s)])
                       (unless (directory-exists? d)
                         (print-status
                          (format "  making directory ~a" (pretty-name d)))
                         (make-directory* d)))))]
          [(file file-replace)
           (let-values ([(target-dir s)
                         (shuffle-path parent-dir get-dir shuffle? (read p))])
             (unless (relative-path? s)
               (error "expected a file name relative path string, got" s))
             (let ([len (read p)])
               (unless (and (number? len) (integer? len))
                 (error "expected a file name size, got" len))
               (let* ([write? (filter kind s target-dir)]
                      [path (build-path target-dir s)])
                 (let ([out (and write?
                                 (if (file-exists? path)
                                     (if (eq? kind 'file)
                                         #f
                                         (open-output-file path #:exists 'truncate))
                                     (open-output-file path)))])
                   (when (and write? (not out))
                     (print-status (format "  skipping ~a; already exists" (pretty-name path))))
                   (when out
                     (print-status (format "  unpacking ~a" (pretty-name path))))
                   ;; Find starting *
                   (let loop ()
                     (let ([c (read-char p)])
                       (cond [(char=? c #\*) (void)] ; found it
                             [(char-whitespace? c) (loop)]
                             [(eof-object? c) (void)] ; signal the error below
                             [else (error 
                                    (format "unexpected character setting up ~a, looking for *"
                                            path)
                                    c)])))
                   ;; Copy file data
                   (let loop ([n len])
                     (unless (zero? n)
                       (let ([l (read-bytes! buffer p 0 (min n bufsize))])
                         (when (eof-object? l)
                           (error (format
                                   "unexpected end-of-file while ~a ~a (at ~a of ~a)"
                                   (if out "unpacking" "skipping")
                                   path
                                   (- len n -1) len)))
                         (when out (write-bytes buffer out 0 l))
                         (loop (- n l)))))
                   (when out (close-output-port out))))))]
          [else (error "unknown file tag" kind)])
        (loop)))))

(define (call-info info flag mk-default test)
  (if info
      (let ([v (info flag mk-default)]) (test v) v)
      (mk-default)))

(define unpack
  (lambda (archive 
           [main-collects-parent-dir (current-directory)]
           [print-status (lambda (x) (printf "~a\n" x))]
           [get-target-directory (lambda () (current-directory))]
           [force? #f]
           [get-target-plt-directory
            (lambda (preferred main-collects-parent-dir options)
              preferred)])
    (let*-values ([(p64gz) (open-input-file archive)]
                  [(p kill) (port64gz->port p64gz)])
      (dynamic-wind
       void
       (lambda ()
         (unless (and (eq? #\P (read-char p))
                      (eq? #\L (read-char p))
                      (eq? #\T (read-char p)))
           (error "not an unpackable distribution archive"))
         (let* ([info (let ([v (read p)])
                        (match v
                          [`(lambda (request failure)
                              (case request
                                [(name) ,name]
                                [(unpacker) 'mzscheme]
                                [(requires) ',requires]
                                [(conflicts) ',conflicts]
                                [(plt-relative?) ,plt-relative?]
                                [(plt-home-relative?) ,plt-home-relative?]
                                . ,(or `([(test-plt-dirs) ,test-dirs] ; #f or `(quote ,dirs)
                                         [else (failure)])
                                       `([,(and 'else test-dirs) (failure)]))))
                           (lambda (request failure)
                             (case request
                               [(name) name]
                               [(unpacker) 'mzscheme]
                               [(requires) requires]
                               [(conflicts) conflicts]
                               [(plt-relative?) plt-relative?]
                               [(plt-home-relative?) plt-home-relative?]
                               [(test-plt-dirs) (and test-dirs
                                                     (not (eq? test-dirs 'else))
                                                     (cadr test-dirs))]
                               [else (failure)]))]
                          [else
                           (error "info-procedure S-expression did not have the expected shape: "
                                  v)]))])
           (unless (and (procedure? info)
                        (procedure-arity-includes? info 2))
             (error "expected a procedure of arity 2, got" info))
           (let ([name (call-info info 'name (lambda () #f)
                                  (lambda (n) 
                                    (unless (string? n)
                                      (if n
                                          (error "couldn't find the package name")
                                          (error "expected a string")))))]
                 [unpacker (call-info info 'unpacker (lambda () #f)
                                      (lambda (n) 
                                        (unless (eq? n 'mzscheme)
                                          (error "unpacker isn't mzscheme:" n))))]
                 [target-dir-info
                  (let ([rel? (call-info info 'plt-relative? (lambda () #f) values)]
                        [not-user-rel? (call-info info 'plt-home-relative? (lambda () #f) values)]
                        [test-dirs (call-info info 'test-plt-dirs (lambda () #f) values)])
                    (if rel?
                        ;; Shuffling...
                        (if (and not-user-rel? 
                                 ;; Check for void because old unpacker didn't use
                                 ;;  the failure thunk.
                                 (not (void? not-user-rel?))
                                 ;; Non-user optional if test-dirs are writable
                                 (or (not test-dirs)
                                     (andmap
                                      (lambda (p)
                                        (and (string? p)
                                             (let ([dir (let-values ([(base dir)
                                                                      (shuffle-path main-collects-parent-dir
                                                                                    (lambda (a b) (a))
                                                                                    #t (list p))])
                                                          (build-path base dir))])
                                               (memq 'write
                                                     (with-handlers ([exn:fail:filesystem? (lambda (x) null)])
                                                       (file-or-directory-permissions dir))))))
                                      test-dirs)))
                            ;; Shuffle to main directory always:
                            (let ([dir (get-target-plt-directory main-collects-parent-dir
                                                                 main-collects-parent-dir
                                                                 (list main-collects-parent-dir))])
                              (list dir (lambda (sys user)
                                          (let ([a (sys)])
                                            (get-target-plt-directory a a (list a))))))
                            ;; Prefer to shuffle to user directory:
                            (let ([addons (find-user-collects-dir)])
                              (let ([dir (get-target-plt-directory
                                          addons
                                          main-collects-parent-dir
                                          (list addons main-collects-parent-dir))])
                                (list dir (lambda (sys user)
                                            (let ([a (sys)]
                                                  [b (user)])
                                              (get-target-plt-directory b a (list b a))))))))
                        ;; No shuffling --- install to target directory:
                        (list (get-target-directory))))])

             ;; Stop if no target directory:
             (if (car target-dir-info)

                 ;; Check declared dependencies (none means v103)
                 (begin
                   (call-info 
                    info 'requires (lambda () null)
                    (lambda (l) 
                      (define (bad)
                        (error "`requires' info is corrupt:" l))
                      (when (void? l)
                        (if force?
                            (print-status "warning: archive is for an older version of Racket")
                            (error "cannot install; archive is for an older version of Racket")))
                      (unless (or (list? l) (and force? (void? l)))
                        (bad))
                      ;; Check each dependency:
                      (when (list? l)
                        (for-each
                         (lambda (d)
                           (unless (and (list? d) (= 2 (length d)))
                             (bad))
                           (let ([coll-path (car d)]
                                 [version (cadr d)])
                             (unless (and (pair? coll-path)
                                          (list? coll-path)
                                          (andmap string? coll-path)
                                          (list? version)
                                          (andmap number? version))
                               (bad))
                             (with-handlers ([exn:fail:filesystem?
                                              (lambda (x)
                                                (if force?
                                                    (print-status 
                                                     (format "warning: missing required collection ~s" coll-path))
                                                    (error "cannot install; missing required collection" coll-path)))])
                               (apply collection-path coll-path))
                             (let ([inst-version 
                                    (with-handlers ([void (lambda (x) 
                                                            (if (exn:break? x)
                                                                (raise x)
                                                                null))])
                                      (let ([info (get-info coll-path)])
                                        (info 'version (lambda () null))))])
                               (let loop ([v version][iv inst-version])
                                 (unless (null? v)
                                   (when (or (null? iv)
                                             (not (= (car v) (car iv))))
                                     (let ([msg (format "version ~a of collection ~s is required, but version ~a is installed"
                                                        version coll-path 
                                                        (if (null? inst-version)
                                                            '<unknown>
                                                            inst-version))])
                                       (if force?
                                           (print-status (format "warning: ~a" msg))
                                           (error (format "cannot install; ~a" msg)))))
                                   (loop (cdr v) (cdr iv)))))))
                         l))))

                   ;; Check for conflicts:
                   (call-info
                    info 'conflicts (lambda () null)
                    (lambda (l) 
                      (define (bad)
                        (error "`conflicts' info is corrupt:" l))
                      (unless (or (list? l) (and force? (void? l)))
                        (bad))
                      (when (list? l)
                        (for-each
                         (lambda (coll-path)
                           (unless (and (pair? coll-path)
                                        (list? coll-path)
                                        (andmap string? coll-path))
                             (bad))
                           (when (with-handlers ([exn:fail? (lambda (x) #f)])
                                   (apply collection-path coll-path))
                             (error "cannot install; conflict with installed collection"
                                    coll-path)))
                         l))))
                   
                   (unless (and name unpacker)
                     (error "bad name or unpacker"))
                   (print-status (format "Unpacking ~a from ~a" name archive))
                   (let ([u (read p)])
                     (match u
                       [`(unit (import ,(? symbol?) mzuntar) 
                               (export)
                               (mzuntar void)
                               (quote ,collections))
                        (make-directory* (car target-dir-info))
                        (unmztar p void
                                 (car target-dir-info) 
                                 (lambda (a b)
                                   ((cadr target-dir-info) a b))
                                 ((length target-dir-info) . > . 1)
                                 print-status)
                        collections]
                       [else
                        (error "expected a `unit' pattern, got" u)])))

                 ;; Cancelled: no collections
                 null))))
       (lambda () (kill) (close-input-port p64gz))))))