@@ -3,6 +3,20 @@ use std::io;
3
3
use std:: path;
4
4
use tokio:: fs as afs;
5
5
6
+ /// Creates a writeable and persistent temporary file in the path provided, returning the path and
7
+ /// file handle.
8
+ async fn tempfile ( dir : & path:: Path ) -> Result < ( afs:: File , path:: PathBuf ) > {
9
+ let tmppath = crate :: tmp:: tmppath_in ( dir) ;
10
+ let tmp = afs:: OpenOptions :: new ( )
11
+ . write ( true )
12
+ . create ( true )
13
+ . truncate ( true )
14
+ . open ( & tmppath)
15
+ . await
16
+ . map_err ( ForcepError :: Io ) ?;
17
+ Ok ( ( tmp, tmppath) )
18
+ }
19
+
6
20
/// The main component of `forceps`, acts as the API for interacting with the on-disk API.
7
21
///
8
22
/// This structure exposes `read`, `write`, and misc metadata operations. `read` and `write` are
@@ -43,6 +57,7 @@ pub struct CacheBuilder {
43
57
path : path:: PathBuf ,
44
58
}
45
59
60
+
46
61
impl Cache {
47
62
/// Creates a new Cache instance based on the CacheBuilder
48
63
async fn new ( builder : CacheBuilder ) -> Result < Self > {
@@ -78,29 +93,6 @@ impl Cache {
78
93
buf
79
94
}
80
95
81
- /// Creates a temporary file in the `self.path` directory of the cache, returning the file
82
- /// handle and the path of the file created.
83
- async fn open_tempfile ( & self ) -> Result < ( afs:: File , path:: PathBuf ) > {
84
- let tmp_in = self . path . clone ( ) ;
85
-
86
- // spawn a blocking task to create the temporary file
87
- tokio:: task:: spawn_blocking ( move || {
88
- // create a new named temporary file and persist it to capture the file handle and path
89
- tempfile:: NamedTempFile :: new_in ( & tmp_in)
90
- . map_err ( ForcepError :: Io )
91
- . and_then ( |x| x. keep ( ) . map_err ( |e| ForcepError :: Io ( e. into ( ) ) ) )
92
- . map ( |( file, path) | ( afs:: File :: from_std ( file) , path) )
93
- } )
94
- . await
95
- . map_err ( |_| {
96
- ForcepError :: Io ( io:: Error :: new (
97
- io:: ErrorKind :: Other ,
98
- "cannot join blocking task" ,
99
- ) )
100
- } )
101
- . and_then ( |x| x)
102
- }
103
-
104
96
/// Reads an entry from the database, returning a vector of bytes that represent the entry.
105
97
///
106
98
/// # Not Found
@@ -175,7 +167,7 @@ impl Cache {
175
167
let key = key. as_ref ( ) ;
176
168
let value = value. as_ref ( ) ;
177
169
178
- let ( tmp, tmp_path) = self . open_tempfile ( ) . await ?;
170
+ let ( tmp, tmp_path) = tempfile ( & self . path ) . await ?;
179
171
// write all data to a temporary file
180
172
{
181
173
let mut writer = tokio:: io:: BufWriter :: new ( tmp) ;
0 commit comments