/
wipe.factor
44 lines (34 loc) · 1.43 KB
/
wipe.factor
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
! Copyright (C) 2017-2019, 2023 Alexander Ilin.
! See https://factorcode.org/license.txt for BSD license.
USING: accessors io io.directories io.encodings.binary io.files
io.files.info io.files.unique io.streams.limited
io.streams.random kernel namespaces system vocabs ;
IN: wipe
HOOK: remove-read-only os ( file-name -- )
M: object remove-read-only drop ;
! Load a Windows-specific implementation of remove-read-only.
os windows? [ "wipe.windows" require ] when
: overwrite-with-random-bytes ( file-name -- )
[ remove-read-only ] [ file-info size>> ] [ ] tri binary [
<random-stream> swap limit-stream
0 seek-absolute output-stream get
[ stream-seek ] keep stream-copy
] with-file-appender ;
: make-file-empty ( file-name -- )
binary [ ] with-file-writer ;
: wipe-file ( file-name -- )
[ overwrite-with-random-bytes ] [ make-file-empty ] [ delete-file ] tri ;
: wipe-all ( directory -- )
[ dup directory? [ drop ] [ name>> wipe-file ] if ] each-directory-entry ;
: wipe ( path -- )
dup file-info regular-file? [ wipe-file ] [ wipe-all ] if ;
: with-temp-directory-at ( path quot -- )
[ cleanup-unique-directory ] curry with-directory ; inline
: wipe-free-space ( path -- )
dup [
file-system-info free-space>>
"" "" unique-file binary [
<random-stream> swap limit-stream
output-stream get stream-copy
] with-file-writer
] with-temp-directory-at ;