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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
crate::ix!();

/**
  | Windows: C:\Users\Username\AppData\Roaming\Bitcoin
  |
  | macOS: ~/Library/Application Support/Bitcoin
  | 
  | Unix-like: ~/.bitcoin
  |
  */
#[cfg(target_os = "windows")]
pub fn get_default_data_dir() -> PathBuf {

    let mut path: PathBuf = get_special_folder_path(csidl_appdata);
    path.push("Bitcoin");
    path
}

#[cfg(target_os = "macos")]
pub fn get_default_data_dir() -> PathBuf {

    let mut path_ret = get_home_dir();

    path_ret.push("Library/Application Support/Bitcoin");

    path_ret
}

#[cfg(not(any(target_os = "windows", target_os = "macos")))]
pub fn get_default_data_dir() -> PathBuf {

    let mut path_ret = get_home_dir();

    //  Unix-like
    path_ret.push(".bitcoin");

    path_ret
}

#[cfg(not(target_os = "windows"))]
pub fn get_home_dir() -> PathBuf {

    let psz_home = env::var("HOME");

    if psz_home.is_err() || psz_home.as_ref().unwrap().is_empty() {
        PathBuf::from("/")
    } else {
        PathBuf::from(psz_home.unwrap())
    }
}

impl ArgsManagerInner {

    /**
      | Get data directory path
      | 
      | 
      | -----------
      | @return
      | 
      | Absolute path on success, otherwise
      | an empty path when a non-directory path
      | would be returned @post Returned directory
      | path is created unless it is empty
      |
      */
    pub fn get_data_dir_base(&self) -> PathBuf {
        self.get_data_dir(false)
    }

    /**
      | Get data directory path with appended
      | network identifier
      | 
      | -----------
      | @return
      | 
      | Absolute path on success, otherwise
      | an empty path when a non-directory path
      | would be returned @post Returned directory
      | path is created unless it is empty
      |
      */
    pub fn get_data_dir_net(&self) -> PathBuf {
        self.get_data_dir(true)
    }

    /**
      | Get data directory path
      | 
      | -----------
      | @param net_specific
      | 
      | Append network identifier to the returned
      | path
      | 
      | -----------
      | @return
      | 
      | Absolute path on success, otherwise
      | an empty path when a non-directory path
      | would be returned @post Returned directory
      | path is created unless it is empty
      |
      */
    pub fn get_data_dir(&self, net_specific: bool) -> PathBuf {
        
        let maybe_cached = match net_specific {
            true   => self.cached_network_datadir_path.as_ref(),
            false  => self.cached_datadir_path.as_ref()
        };

        // Cache the path to avoid calling
        // create_directories on every call of
        // this function
        if let Some(path) = maybe_cached {
            return path.to_path_buf();
        }

        let mut buf: PathBuf = PathBuf::new();

        let datadir: String = self.get_arg("-datadir","");

        if datadir.len() != 0 {

            let arg_path = std::fs::canonicalize(Path::new(&datadir)).unwrap();

            buf.push(arg_path);

            if !buf.as_path().is_dir() {
                buf.clear();
                return buf.to_path_buf();
            }

        } else {
            buf.push(get_default_data_dir());
        }

        if net_specific {
            buf.push(base_params().data_dir());
        }

        if let Ok(result) = std::fs::create_dir_all(buf.as_path()) {

            let mut subdir = buf.clone();

            subdir.push("wallets");

            // This is the first run, create
            // wallets subdirectory too
            std::fs::create_dir_all(subdir);
        }

        strip_redundant_last_elements_of_path(&mut buf);

        buf
    }
}