fixed file permission error with a 'truncate option, used racket-native file permission-changing function

main
m455 2019-03-12 15:00:03 -04:00
parent 4c1089e459
commit d06d952cdd
3 changed files with 41 additions and 72 deletions

View File

@ -15,27 +15,6 @@ Now the default directory and todo list file have better default permissions:
# Todos # Todos
- **Really weird bug to solve**: `~/.rodo/todo.txt`'s file permissions change from `rw-------` to `rw-rw-r--` (if on Ubuntu) or `rw-rw-rw` (if on Windows Subsystem for Linux) after being modified
- For example:
1. Create a `test.txt` file somewhere
2. Open a Racket REPL
3. Run the following to set `600` file permissions on the file:
```racket
(file-or-directory-permissions "test.txt" #o600)
```
4. Run `ls -l` to check the file permissions
5. Run the following to modify the file:
```racket
(display-to-file "this is a test" "test.txt" #:mode 'text #:exists 'replace)
```
6. Run `ls -l` to check the file permissions to see that they have changed to either `rw-rw-r--` or `rw-rw-rw`
- So far, the only solution I know of would be to do:
```racket
(begin
(display-to-file "this is a test" "test.txt" #:mode 'text #:exists 'replace)
(file-or-directory-permissions "test.txt" #o600))
```
- Unfortunately the above solution leaves the file readable for a split second, which would be enough for another program to read information in the files
- Add color option to `config.rkt` file - Add color option to `config.rkt` file
- Encrypt `todo.txt` file - Encrypt `todo.txt` file
- Add note on `.bash_history` about items being added here before going into the `todo.txt` file - Add note on `.bash_history` about items being added here before going into the `todo.txt` file

View File

@ -2,7 +2,7 @@
(provide (all-defined-out)) (provide (all-defined-out))
(define program-name "rodo") (define program-name "rodo")
(define program-directory "~/.rodo/") (define program-directory (path->string (expand-user-path "~/.rodo/")))
(define program-file "todo.txt") (define program-file "todo.txt")
(define remove-command "rm") (define remove-command "rm")
(define add-command "add") (define add-command "add")
@ -10,7 +10,6 @@
(define initialize-command "init") (define initialize-command "init")
(define help-command '("-h" "--help")) (define help-command '("-h" "--help"))
(define path (define path
(expand-user-path
(string-append (string-append
program-directory program-directory
program-file))) program-file))

View File

@ -3,41 +3,32 @@
(require (prefix-in list: racket/list) (require (prefix-in list: racket/list)
(prefix-in file: racket/file) (prefix-in file: racket/file)
(prefix-in string: racket/string) (prefix-in string: racket/string)
(prefix-in system: racket/system)
(prefix-in config: "config.rkt") (prefix-in config: "config.rkt")
(prefix-in messages: "messages.rkt")) (prefix-in messages: "messages.rkt"))
(provide (all-defined-out)) (provide (all-defined-out))
(define (set-permissions permissions file-or-directory)
(let* ([converted-permissions (number->string permissions)]
[converted-file-or-directory
(cond [(path? file-or-directory) (path->string file-or-directory)]
[else file-or-directory])])
(system:system
(string-append "chmod" " " converted-permissions " " converted-file-or-directory))))
(define (check-for-file) (define (check-for-file)
(file-exists? config:path)) (file-exists? config:path))
(define (create-file) (define (create-file)
(let ([opened-file (let ([opened-file
(open-output-file config:path (open-output-file config:path
#:mode 'text #:mode 'text
#:exists 'can-update)]) #:exists 'truncate)])
(close-output-port opened-file)) (close-output-port opened-file))
(set-permissions 600 config:path)) (file-or-directory-permissions config:path #o600))
(define (check-for-directory) (define (check-for-directory)
(directory-exists? (expand-user-path (directory-exists? (expand-user-path
(string-append (string-append
config:program-directory)))) config:program-directory))))
(define (create-directory) (define (create-directory)
(make-directory (expand-user-path (make-directory (expand-user-path
(string-append (string-append
config:program-directory))) config:program-directory)))
(set-permissions 700 config:program-directory)) (file-or-directory-permissions config:program-directory #o700))
(define (display-hash-ref hash-list key) (define (display-hash-ref hash-list key)
(display (hash-ref hash-list key))) (display (hash-ref hash-list key)))
@ -77,10 +68,10 @@
(define (display-prettified-list) (define (display-prettified-list)
(display (display
(string:string-join (string:string-join
(prefix-with-number (file->string-list config:path)) (prefix-with-number (file->string-list config:path))
"\n" "\n"
#:after-last "\n"))) #:after-last "\n")))
;; This is a bit of ugly scheme sorcery ;; This is a bit of ugly scheme sorcery
(define (append-to-end args lst) (define (append-to-end args lst)
@ -98,56 +89,56 @@
(define (show-list) (define (show-list)
(cond [(and (cond [(and
(check-for-directory) (check-for-directory)
(check-for-file)) (check-for-file))
(if (if
;; If file exists, see if it's empty, if so ;; If file exists, see if it's empty, if so
;; tell the user ;; tell the user
(list-empty? config:path) (list-empty? config:path)
(display-hash-ref messages:messages 'empty-todo-list) (display-hash-ref messages:messages 'empty-todo-list)
;; If file isn't empty, display a pretty list ;; If file isn't empty, display a pretty list
(display-prettified-list))] (display-prettified-list))]
;; If file doesn't exist, tell the user ;; If file doesn't exist, tell the user
[else [else
(display-hash-ref messages:messages 'file-not-found) (display-hash-ref messages:messages 'file-not-found)
(display-hash-ref messages:messages 'try-init)])) (display-hash-ref messages:messages 'try-init)]))
(define (add-item-to-file args) (define (add-item-to-file args)
;; Add item to end of list and write to file ;; Add item to end of list and write to file
(let ([new-list (append-to-end args config:path)]) (let ([new-list (append-to-end args config:path)])
(file:display-to-file (file:display-to-file
(string:string-join new-list "\n") (string:string-join new-list "\n")
config:path config:path
#:mode 'text #:mode 'text
#:exists 'replace) #:exists 'truncate)
(display-item-added args))) (display-item-added args)))
(define (add-item args) (define (add-item args)
(if (and (if (and
(check-for-directory) (check-for-directory)
(check-for-file)) (check-for-file))
(add-item-to-file (vector-ref args 1)) (add-item-to-file (vector-ref args 1))
(begin (begin
(display-hash-ref messages:messages 'file-not-found) (display-hash-ref messages:messages 'file-not-found)
(display-hash-ref messages:messages 'try-init)))) (display-hash-ref messages:messages 'try-init))))
(define (remove-item-from-file args) (define (remove-item-from-file args)
(let* ([removed-item (get-removed-item config:path args)] (let* ([removed-item (get-removed-item config:path args)]
[new-list (remove removed-item (file->string-list config:path))]) [new-list (remove removed-item (file->string-list config:path))])
(file:display-to-file (file:display-to-file
(string:string-join new-list "\n") (string:string-join new-list "\n")
config:path config:path
#:mode 'text #:mode 'text
#:exists 'replace) #:exists 'truncate)
(display-item-removed removed-item))) (display-item-removed removed-item)))
(define (remove-item args) (define (remove-item args)
(cond [(list-empty? config:path) (cond [(list-empty? config:path)
(display-hash-ref messages:messages 'empty-todo-list)] (display-hash-ref messages:messages 'empty-todo-list)]
[(and [(and
(check-for-directory) (check-for-directory)
(check-for-file)) (check-for-file))
(remove-item-from-file (vector-ref args 1))] (remove-item-from-file (vector-ref args 1))]
[(and (not (check-for-directory)) (not (check-for-file))) [(and (not (check-for-directory)) (not (check-for-file)))
(begin (begin