1
1
use std:: {
2
2
cell:: RefCell ,
3
3
collections:: HashMap ,
4
- path:: { Path , PathBuf } ,
4
+ path:: PathBuf ,
5
5
sync:: {
6
6
atomic:: { AtomicUsize , Ordering } ,
7
7
Arc ,
8
8
} ,
9
9
} ;
10
10
11
- use anyhow:: Result ;
11
+ use anyhow:: { anyhow , Result } ;
12
12
use cid:: {
13
13
multihash:: { Code , MultihashDigest } ,
14
14
Cid ,
15
15
} ;
16
16
use config:: { Config , ConfigError , Environment , File , Map , Source , Value , ValueKind } ;
17
- use dirs:: home_dir;
18
17
use tracing:: debug;
19
18
20
- const IROH_DIR : & str = ".iroh" ;
19
+ /// name of directory that wraps all iroh files in a given application directory
20
+ const IROH_DIR : & str = "iroh" ;
21
21
const DEFAULT_NOFILE_LIMIT : u64 = 65536 ;
22
22
const MIN_NOFILE_LIMIT : u64 = 2048 ;
23
23
@@ -44,16 +44,47 @@ pub async fn block_until_sigint() {
44
44
ctrlc_oneshot. await . unwrap ( ) ;
45
45
}
46
46
47
- /// Path to the iroh home directory.
48
- pub fn iroh_home_root ( ) -> Option < PathBuf > {
49
- let home = home_dir ( ) ?;
50
- Some ( Path :: new ( & home) . join ( IROH_DIR ) )
47
+ /// Returns the path to the user's iroh config directory.
48
+ ///
49
+ /// The returned value depends on the operating system and is either a `Some`, containing a value from the following table, or a `None`.
50
+ ///
51
+ /// | Platform | Value | Example |
52
+ /// | -------- | ------------------------------------- | -------------------------------- |
53
+ /// | Linux | `$XDG_CONFIG_HOME` or `$HOME`/.config/iroh | /home/alice/.config/iroh |
54
+ /// | macOS | `$HOME`/Library/Application Support/iroh | /Users/Alice/Library/Application Support/iroh |
55
+ /// | Windows | `{FOLDERID_RoamingAppData}`/iroh | C:\Users\Alice\AppData\Roaming\iroh |
56
+ pub fn iroh_config_root ( ) -> Result < PathBuf > {
57
+ let cfg = dirs_next:: config_dir ( )
58
+ . ok_or_else ( || anyhow ! ( "operating environment provides no directory for configuration" ) ) ?;
59
+ Ok ( cfg. join ( & IROH_DIR ) )
60
+ }
61
+
62
+ // Path that leads to a file in the iroh config directory.
63
+ pub fn iroh_config_path ( file_name : & str ) -> Result < PathBuf > {
64
+ let path = iroh_config_root ( ) ?. join ( file_name) ;
65
+ Ok ( path)
66
+ }
67
+
68
+ /// Returns the path to the user's iroh data directory.
69
+ ///
70
+ /// The returned value depends on the operating system and is either a `Some`, containing a value from the following table, or a `None`.
71
+ ///
72
+ /// | Platform | Value | Example |
73
+ /// | -------- | --------------------------------------------- | ---------------------------------------- |
74
+ /// | Linux | `$XDG_DATA_HOME`/iroh or `$HOME`/.local/share/iroh | /home/alice/.local/share/iroh |
75
+ /// | macOS | `$HOME`/Library/Application Support/iroh | /Users/Alice/Library/Application Support/iroh |
76
+ /// | Windows | `{FOLDERID_RoamingAppData}/iroh` | C:\Users\Alice\AppData\Roaming\iroh |
77
+ pub fn iroh_data_root ( ) -> Result < PathBuf > {
78
+ let path = dirs_next:: data_dir ( ) . ok_or_else ( || {
79
+ anyhow ! ( "operating environment provides no directory for application data" )
80
+ } ) ?;
81
+ Ok ( path. join ( & IROH_DIR ) )
51
82
}
52
83
53
- /// Path that leads to a file in the iroh home directory.
54
- pub fn iroh_home_path ( file_name : & str ) -> Option < PathBuf > {
55
- let path = iroh_home_root ( ) ?. join ( file_name) ;
56
- Some ( path)
84
+ /// Path that leads to a file in the iroh data directory.
85
+ pub fn iroh_data_path ( file_name : & str ) -> Result < PathBuf > {
86
+ let path = iroh_data_root ( ) ?. join ( file_name) ;
87
+ Ok ( path)
57
88
}
58
89
59
90
/// insert a value into a `config::Map`
@@ -174,9 +205,9 @@ pub fn increase_fd_limit() -> std::io::Result<u64> {
174
205
mod tests {
175
206
use super :: * ;
176
207
#[ test]
177
- fn test_iroh_home_path ( ) {
178
- let got = iroh_home_path ( "foo.bar" ) . unwrap ( ) ;
208
+ fn test_iroh_config_path ( ) {
209
+ let got = iroh_config_path ( "foo.bar" ) . unwrap ( ) ;
179
210
let got = got. to_str ( ) . unwrap ( ) . to_string ( ) ;
180
- assert ! ( got. ends_with( "/. iroh/foo.bar" ) ) ;
211
+ assert ! ( got. ends_with( "/iroh/foo.bar" ) ) ;
181
212
}
182
213
}
0 commit comments