Added a private flag

This commit is contained in:
2026-08-09 14:16:18 +02:00
parent a9f1f9b7b6
commit f8349e9a11
5 changed files with 52 additions and 9 deletions
+9 -2
View File
@@ -15,7 +15,7 @@
(define ini% (define ini%
(class object% (class object%
(init-field [file #f] [fail #f]) (init-field [file #f] [fail #f] [private? #f])
(define content #f) (define content #f)
@@ -33,6 +33,13 @@
(define/public (get-fail) (define/public (get-fail)
fail) fail)
(define/public (get-private?)
private?)
(define/public (set-private! p)
(i-set! private? p)
this)
(define/public (get-contents) (define/public (get-contents)
content) content)
@@ -53,7 +60,7 @@
(if (eq? file #f) (if (eq? file #f)
(when fail (when fail
(error "ini: No filename set, cannot write ini after set!")) (error "ini: No filename set, cannot write ini after set!"))
(ini->file content file)) (ini->file content file #:private? private?))
this)) this))
(define/public (get section key . default-value) (define/public (get section key . default-value)
+2 -2
View File
@@ -1,7 +1,7 @@
#lang info #lang info
(define pkg-authors '(hnmdijkema)) (define pkg-authors '(hnmdijkema))
(define version "0.3.2") (define version "0.3.3")
(define license 'Apache-2.0) (define license 'Apache-2.0)
(define pkg-desc "A Simple .ini file reader/writer for racket") (define pkg-desc "A Simple .ini file reader/writer for racket")
@@ -12,7 +12,7 @@
) )
(define deps (define deps
'("base" "roos")) '(("base" #:version "8.7.0.10") "roos"))
(define build-deps (define build-deps
'("racket-doc" '("racket-doc"
+9 -3
View File
@@ -41,8 +41,15 @@
) )
) )
(define (ini->file ini file) (define (ini->file ini file #:private? [private? #f])
(let ((out (open-output-file (get-ini-file file) #:exists 'replace))) (let* ((file (get-ini-file file))
(out (if (and private?
(memq (system-type 'os) '(unix macosx)))
(open-output-file file
#:exists 'replace
#:permissions #o600
#:replace-permissions? #t)
(open-output-file file #:exists 'replace))))
(let ((last-is-newline #f)) (let ((last-is-newline #f))
(for-each (lambda (section) (for-each (lambda (section)
(let ((section-name (car section))) (let ((section-name (car section)))
@@ -77,7 +84,6 @@
lines)))) lines))))
(mcdr ini))) (mcdr ini)))
(close-output-port out))) (close-output-port out)))
(define (make-ini) (define (make-ini)
(mcons 'ini (list))) (mcons 'ini (list)))
+12 -1
View File
@@ -45,9 +45,10 @@
] ]
} }
@defproc[(ini->file [ini mc-pair?] [file path-string?]) void?]{ @defproc[(ini->file [ini mc-pair?] [file path-string?] [#:private? private? boolean? #f]) void?]{
Writes an INI structure (as produced by @racket[file->ini] or @racket[make-ini]) to the specified file. Writes an INI structure (as produced by @racket[file->ini] or @racket[make-ini]) to the specified file.
If file is a symbol?, the file will be constructed from the prefs-dir, the symbol and a suffix ".ini". If file is a symbol?, the file will be constructed from the prefs-dir, the symbol and a suffix ".ini".
If @racket[private?] is @racket[#t], Unix-like systems restrict the file to mode @tt{0600} before the INI contents are written.
The output preserves: The output preserves:
@itemlist[ @itemlist[
@@ -89,6 +90,7 @@ An OO wrapper around the ini functions.
@defconstructor[([file (or/c symbol? string? path? boolean?) #f] @defconstructor[([file (or/c symbol? string? path? boolean?) #f]
[fail (or/c boolean?) #f] [fail (or/c boolean?) #f]
[private? (or/c boolean?) #f]
)]{ )]{
Creates the ini from the given file. Creates the ini from the given file.
* If (eq? file #f), an empty ini will be made. * If (eq? file #f), an empty ini will be made.
@@ -115,6 +117,15 @@ An OO wrapper around the ini functions.
Sets the value of the 'fail' flag. See constructor for more information. Sets the value of the 'fail' flag. See constructor for more information.
} }
@defmethod*[([(get-private?) boolean?])]{
Returns whether private file permissions are enabled.
}
@defmethod*[([(set-private! [private? boolean?]) this])]{
Enables or disables private file permissions for subsequent writes.
}
@defmethod*[([(get-contents) list?])]{ @defmethod*[([(get-contents) list?])]{
Gets the contents of the ini structure as stored in memory. Gets the contents of the ini structure as stored in memory.
} }
+19
View File
@@ -0,0 +1,19 @@
#lang racket/base
(require rackunit
racket/file
"../main.rkt")
(define tmp (make-temporary-file "simple-ini-private-~a.ini"))
(dynamic-wind
void
(lambda ()
(define ini (make-ini))
(ini-set! ini 'secret 'token "abc")
(ini->file ini tmp #:private? #t)
(when (memq (system-type 'os) '(unix macosx))
(check-equal? (bitwise-and (file-or-directory-permissions tmp 'bits) #o777)
#o600)))
(lambda ()
(when (file-exists? tmp)
(delete-file tmp))))