umask.rkt

#386
Raw
Author
winny
Created
Sept. 20, 2021, 3:02 a.m.
Expires
Never
Size
916 bytes
Hits
242
Syntax
Racket
Private
No
#|

I didn't see a way to set umask in racket, so here is a quick and dirty
solution.

|#

#lang racket

(provide umask
         with-umask)

(require ffi/unsafe
         ffi/unsafe/define)

(define-ffi-definer define-libc (ffi-lib "libc" "6"))
(define-libc libc-umask (_fun _uint32 -> _uint32) #:c-id umask)

(define/match (umask [mask #f])
  [(#f)
   (define last-mask (libc-umask 0))
   (libc-umask last-mask)
   last-mask]
  [(new-mask)
   (libc-umask new-mask)
   (void)])

#|
Usage:
(with-umask #0o077
  (define temporary-file (make-temporary-file))
  ;; Do something with a temporary file that only your user can read.
  )
|#
(define-syntax-rule (with-umask new-umask body body-rest ...)
  ;; Using the version of umask that is not provided out of this module.
  (let ([old-umask (libc-umask new-umask)])
    (begin0
        (begin
          body
          body-rest ...)
      (umask old-umask))))