1
1
use crate :: dspfs:: builder:: DspfsBuilder ;
2
2
use crate :: dspfs:: client:: Client ;
3
3
use crate :: dspfs:: server:: { Server , ServerHandle } ;
4
+ use crate :: fs:: file:: File ;
5
+ use crate :: fs:: file:: SimpleFile ;
4
6
use crate :: fs:: group:: StoredGroup ;
7
+ use crate :: fs:: hash:: Hash ;
5
8
use crate :: global_store:: { SharedStore , Store } ;
6
9
use crate :: user:: { PrivateUser , PublicUser } ;
7
- use anyhow:: { Result , Context } ;
10
+ use anyhow:: { Context , Result } ;
11
+ use api:: Api ;
12
+ use api:: LocalFile ;
13
+ use async_trait:: async_trait;
8
14
use log:: * ;
9
- use std:: collections:: { HashMap , HashSet , BTreeSet } ;
15
+ use std:: collections:: { BTreeSet , HashMap , HashSet } ;
10
16
use std:: fs;
11
17
use std:: mem;
12
18
use std:: path:: Path ;
13
19
use uuid:: Uuid ;
14
- use crate :: fs:: file:: SimpleFile ;
15
- use crate :: fs:: file:: File ;
16
- use crate :: fs:: hash:: Hash ;
17
- use async_trait:: async_trait;
18
- use api:: LocalFile ;
19
- use api:: Api ;
20
20
21
+ pub mod api;
21
22
pub mod builder;
22
23
pub mod client;
23
24
pub mod server;
24
- pub mod api;
25
25
26
26
pub struct Dspfs < S : Store + ' static > {
27
27
pub ( self ) store : SharedStore < S > ,
@@ -63,7 +63,6 @@ impl<S: Store> Dspfs<S> {
63
63
}
64
64
}
65
65
66
-
67
66
#[ async_trait]
68
67
impl < S : Store > Api for Dspfs < S > {
69
68
async fn init_group ( & self , path : & Path ) -> Result < Uuid > {
@@ -72,7 +71,7 @@ impl<S: Store> Api for Dspfs<S> {
72
71
if !group. dspfs_folder ( ) . exists ( ) {
73
72
// New folder
74
73
fs:: create_dir_all ( group. dspfs_folder ( ) ) ?;
75
- let uuid = group. uuid . clone ( ) ;
74
+ let uuid = group. uuid ;
76
75
self . store . write ( ) . await . add_group ( group) ?;
77
76
78
77
Ok ( uuid)
@@ -82,81 +81,109 @@ impl<S: Store> Api for Dspfs<S> {
82
81
}
83
82
84
83
async fn add_file ( & self , guuid : Uuid , path : & Path ) -> Result < SimpleFile > {
85
- let mut group = self . store . read ( ) . await . get_group ( guuid) ?
86
- . context ( "There is no group with this uuid." ) ?. reload ( self . store . clone ( ) ) ?;
84
+ let mut group = self
85
+ . store
86
+ . read ( )
87
+ . await
88
+ . get_group ( guuid) ?
89
+ . context ( "There is no group with this uuid." ) ?
90
+ . reload ( self . store . clone ( ) ) ?;
87
91
88
92
let us = self . store . read ( ) . await . get_self_user ( ) ?. context ( "" ) ?;
89
93
90
94
let mut location = group. dspfs_root ( ) . to_path_buf ( ) ;
91
95
location. push ( path) ;
92
96
93
97
if !location. is_file ( ) {
94
- return Err ( anyhow:: anyhow!( "Path does not point to a file" ) )
98
+ return Err ( anyhow:: anyhow!( "Path does not point to a file" ) ) ;
95
99
}
96
100
97
- let file = File :: new ( location) . await
98
- . context ( "Indexing file failed" ) ?;
101
+ let file = File :: new ( location) . await . context ( "Indexing file failed" ) ?;
99
102
100
103
let simple_file = file. simplify ( ) ;
101
104
102
- group. add_file ( & us, file) . await . context ( "Adding file to group failed" ) ?;
105
+ group
106
+ . add_file ( & us, file)
107
+ . await
108
+ . context ( "Adding file to group failed" ) ?;
103
109
104
110
Ok ( simple_file)
105
111
}
106
112
107
- async fn join_group ( & self , guuid : Uuid , bootstrap_user : & PublicUser ) -> Result < ( ) > {
113
+ async fn join_group ( & self , _guuid : Uuid , _bootstrap_user : & PublicUser ) -> Result < ( ) > {
108
114
unimplemented ! ( "procrastination is life" )
109
115
}
110
116
111
117
async fn list_files ( & self , guuid : Uuid ) -> Result < HashSet < SimpleFile > > {
112
- let group = self . store . read ( ) . await . get_group ( guuid) ?
113
- . context ( "There is no group with this uuid." ) ?. reload ( self . store . clone ( ) ) ?;
118
+ let group = self
119
+ . store
120
+ . read ( )
121
+ . await
122
+ . get_group ( guuid) ?
123
+ . context ( "There is no group with this uuid." ) ?
124
+ . reload ( self . store . clone ( ) ) ?;
114
125
115
- let f = group. list_files ( ) . await ?
116
- . into_iter ( )
117
- . map ( |f| f. simplify ( ) ) ;
126
+ let f = group. list_files ( ) . await ?. into_iter ( ) . map ( |f| f. simplify ( ) ) ;
118
127
119
128
Ok ( f. collect ( ) )
120
129
}
121
130
122
131
async fn get_users ( & self , guuid : Uuid ) -> Result < BTreeSet < PublicUser > > {
123
- let group = self . store . read ( ) . await . get_group ( guuid) ?
132
+ let group = self
133
+ . store
134
+ . read ( )
135
+ . await
136
+ . get_group ( guuid) ?
124
137
. context ( "There is no group with this uuid." ) ?;
125
138
126
139
Ok ( group. users )
127
140
}
128
141
129
142
async fn get_available_files ( & self , guuid : Uuid , path : & Path ) -> Result < HashSet < LocalFile > > {
130
- let group = self . store . read ( ) . await . get_group ( guuid) ?
143
+ let group = self
144
+ . store
145
+ . read ( )
146
+ . await
147
+ . get_group ( guuid) ?
131
148
. context ( "There is no group with this uuid." ) ?;
132
149
133
150
let mut location = group. dspfs_root ( ) . to_path_buf ( ) ;
134
151
location. push ( path) ;
135
152
136
- let set = location. read_dir ( ) . context ( "Reading directory failed" ) ?
137
- . map ( |i| i. map ( |direntry| {
138
- LocalFile :: from_direntry ( direntry)
139
- } ) ) . flatten ( ) . collect :: < Result < _ > > ( ) ?;
153
+ let set = location
154
+ . read_dir ( )
155
+ . context ( "Reading directory failed" ) ?
156
+ . map ( |i| i. map ( LocalFile :: from_direntry) )
157
+ . flatten ( )
158
+ . collect :: < Result < _ > > ( ) ?;
140
159
141
160
Ok ( set)
142
161
}
143
162
144
- async fn get_files ( & self , guuid : Uuid , user : & PublicUser , path : & Path ) -> Result < HashSet < SimpleFile > > {
145
- let group = self . store . read ( ) . await . get_group ( guuid) ?
146
- . context ( "There is no group with this uuid." ) ?. reload ( self . store . clone ( ) ) ?;
147
-
148
- let files = group. get_files_from_user ( user, path) . await ;
163
+ async fn get_files (
164
+ & self ,
165
+ guuid : Uuid ,
166
+ user : & PublicUser ,
167
+ path : & Path ,
168
+ ) -> Result < HashSet < SimpleFile > > {
169
+ let group = self
170
+ . store
171
+ . read ( )
172
+ . await
173
+ . get_group ( guuid) ?
174
+ . context ( "There is no group with this uuid." ) ?
175
+ . reload ( self . store . clone ( ) ) ?;
176
+
177
+ let _files = group. get_files_from_user ( user, path) . await ;
149
178
150
179
todo ! ( )
151
180
}
152
181
153
- async fn request_file ( & self , hash : Hash , to : & Path ) -> Result < ( ) > {
182
+ async fn request_file ( & self , _hash : Hash , _to : & Path ) -> Result < ( ) > {
154
183
unimplemented ! ( )
155
184
}
156
185
157
186
async fn refresh ( & self ) {
158
187
unimplemented ! ( )
159
188
}
160
189
}
161
-
162
-
0 commit comments