fixed file permission error with a 'truncate option, used racket-native file permission-changing function
parent
4c1089e459
commit
d06d952cdd
21
README.md
21
README.md
|
@ -15,27 +15,6 @@ Now the default directory and todo list file have better default permissions:
|
|||
|
||||
# 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
|
||||
- Encrypt `todo.txt` file
|
||||
- Add note on `.bash_history` about items being added here before going into the `todo.txt` file
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
(provide (all-defined-out))
|
||||
|
||||
(define program-name "rodo")
|
||||
(define program-directory "~/.rodo/")
|
||||
(define program-directory (path->string (expand-user-path "~/.rodo/")))
|
||||
(define program-file "todo.txt")
|
||||
(define remove-command "rm")
|
||||
(define add-command "add")
|
||||
|
@ -10,7 +10,6 @@
|
|||
(define initialize-command "init")
|
||||
(define help-command '("-h" "--help"))
|
||||
(define path
|
||||
(expand-user-path
|
||||
(string-append
|
||||
program-directory
|
||||
program-file)))
|
||||
program-file))
|
||||
|
|
87
util.rkt
87
util.rkt
|
@ -3,41 +3,32 @@
|
|||
(require (prefix-in list: racket/list)
|
||||
(prefix-in file: racket/file)
|
||||
(prefix-in string: racket/string)
|
||||
(prefix-in system: racket/system)
|
||||
(prefix-in config: "config.rkt")
|
||||
(prefix-in messages: "messages.rkt"))
|
||||
|
||||
(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)
|
||||
(file-exists? config:path))
|
||||
|
||||
(define (create-file)
|
||||
(let ([opened-file
|
||||
(open-output-file config:path
|
||||
#:mode 'text
|
||||
#:exists 'can-update)])
|
||||
(open-output-file config:path
|
||||
#:mode 'text
|
||||
#:exists 'truncate)])
|
||||
(close-output-port opened-file))
|
||||
(set-permissions 600 config:path))
|
||||
(file-or-directory-permissions config:path #o600))
|
||||
|
||||
(define (check-for-directory)
|
||||
(directory-exists? (expand-user-path
|
||||
(string-append
|
||||
config:program-directory))))
|
||||
(string-append
|
||||
config:program-directory))))
|
||||
|
||||
(define (create-directory)
|
||||
(make-directory (expand-user-path
|
||||
(string-append
|
||||
config:program-directory)))
|
||||
(set-permissions 700 config:program-directory))
|
||||
(string-append
|
||||
config:program-directory)))
|
||||
(file-or-directory-permissions config:program-directory #o700))
|
||||
|
||||
(define (display-hash-ref hash-list key)
|
||||
(display (hash-ref hash-list key)))
|
||||
|
@ -77,10 +68,10 @@
|
|||
|
||||
(define (display-prettified-list)
|
||||
(display
|
||||
(string:string-join
|
||||
(prefix-with-number (file->string-list config:path))
|
||||
"\n"
|
||||
#:after-last "\n")))
|
||||
(string:string-join
|
||||
(prefix-with-number (file->string-list config:path))
|
||||
"\n"
|
||||
#:after-last "\n")))
|
||||
|
||||
;; This is a bit of ugly scheme sorcery
|
||||
(define (append-to-end args lst)
|
||||
|
@ -98,56 +89,56 @@
|
|||
|
||||
(define (show-list)
|
||||
(cond [(and
|
||||
(check-for-directory)
|
||||
(check-for-file))
|
||||
(check-for-directory)
|
||||
(check-for-file))
|
||||
(if
|
||||
;; If file exists, see if it's empty, if so
|
||||
;; tell the user
|
||||
(list-empty? config:path)
|
||||
(display-hash-ref messages:messages 'empty-todo-list)
|
||||
;; If file isn't empty, display a pretty list
|
||||
(display-prettified-list))]
|
||||
;; If file exists, see if it's empty, if so
|
||||
;; tell the user
|
||||
(list-empty? config:path)
|
||||
(display-hash-ref messages:messages 'empty-todo-list)
|
||||
;; If file isn't empty, display a pretty list
|
||||
(display-prettified-list))]
|
||||
;; If file doesn't exist, tell the user
|
||||
[else
|
||||
(display-hash-ref messages:messages 'file-not-found)
|
||||
(display-hash-ref messages:messages 'try-init)]))
|
||||
(display-hash-ref messages:messages 'file-not-found)
|
||||
(display-hash-ref messages:messages 'try-init)]))
|
||||
|
||||
(define (add-item-to-file args)
|
||||
;; Add item to end of list and write to file
|
||||
(let ([new-list (append-to-end args config:path)])
|
||||
(file:display-to-file
|
||||
(string:string-join new-list "\n")
|
||||
config:path
|
||||
#:mode 'text
|
||||
#:exists 'replace)
|
||||
(string:string-join new-list "\n")
|
||||
config:path
|
||||
#:mode 'text
|
||||
#:exists 'truncate)
|
||||
(display-item-added args)))
|
||||
|
||||
(define (add-item args)
|
||||
(if (and
|
||||
(check-for-directory)
|
||||
(check-for-file))
|
||||
(add-item-to-file (vector-ref args 1))
|
||||
(begin
|
||||
(display-hash-ref messages:messages 'file-not-found)
|
||||
(display-hash-ref messages:messages 'try-init))))
|
||||
(check-for-directory)
|
||||
(check-for-file))
|
||||
(add-item-to-file (vector-ref args 1))
|
||||
(begin
|
||||
(display-hash-ref messages:messages 'file-not-found)
|
||||
(display-hash-ref messages:messages 'try-init))))
|
||||
|
||||
(define (remove-item-from-file args)
|
||||
(let* ([removed-item (get-removed-item config:path args)]
|
||||
[new-list (remove removed-item (file->string-list config:path))])
|
||||
|
||||
(file:display-to-file
|
||||
(string:string-join new-list "\n")
|
||||
config:path
|
||||
#:mode 'text
|
||||
#:exists 'replace)
|
||||
(string:string-join new-list "\n")
|
||||
config:path
|
||||
#:mode 'text
|
||||
#:exists 'truncate)
|
||||
(display-item-removed removed-item)))
|
||||
|
||||
(define (remove-item args)
|
||||
(cond [(list-empty? config:path)
|
||||
(display-hash-ref messages:messages 'empty-todo-list)]
|
||||
[(and
|
||||
(check-for-directory)
|
||||
(check-for-file))
|
||||
(check-for-directory)
|
||||
(check-for-file))
|
||||
(remove-item-from-file (vector-ref args 1))]
|
||||
[(and (not (check-for-directory)) (not (check-for-file)))
|
||||
(begin
|
||||
|
|
Loading…
Reference in New Issue