• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2023 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 //! Inspection and manipulation of the system environment.
16 
17 use std::env;
18 use std::path::PathBuf;
19 
20 /// Get or create the netsimd temporary directory.
21 ///
22 /// This is based on emu System.cpp android::base::getTempDir()
23 ///
24 /// Under Forge temp directory is `$ANDROID_TMP/android-$USER/netsimd`,
25 /// otherwise it is `$TMP/android-$USER/netsimd`
26 ///
netsimd_temp_dir() -> PathBuf27 pub fn netsimd_temp_dir() -> PathBuf {
28     let path = netsimd_temp_dir_pathbuf();
29     if !path.is_dir() {
30         std::fs::create_dir_all(&path).unwrap();
31     }
32     path
33 }
34 
35 /// Helper function for netsimd_temp_dir() to allow Read Only
36 /// Unit tests.
netsimd_temp_dir_pathbuf() -> PathBuf37 fn netsimd_temp_dir_pathbuf() -> PathBuf {
38     // allow Forge to override the system temp
39     let mut path = match env::var("ANDROID_TMP") {
40         Ok(var) => PathBuf::from(var),
41         _ => env::temp_dir(),
42     };
43     // On Windows the GetTempPath() is user-dependent so we don't need
44     // to append $USER to the result -- otherwise allow multiple users
45     // to co-exist on a system.
46     #[cfg(not(target_os = "windows"))]
47     {
48         let user = match env::var("USER") {
49             Ok(var) => format!("android-{}", var),
50             _ => "android".to_string(),
51         };
52         path.push(user);
53     };
54     // netsimd files are stored in their own directory
55     path.push("netsimd");
56     path
57 }
58 
59 #[cfg(not(target_os = "windows"))]
60 #[cfg(test)]
61 mod tests {
62     use super::netsimd_temp_dir_pathbuf;
63     use std::env;
64     use std::sync::Mutex;
65 
66     static ENV_MUTEX: Mutex<i32> = Mutex::new(0);
67 
68     #[test]
test_forge()69     fn test_forge() {
70         let _locked = ENV_MUTEX.lock();
71         env::set_var("ANDROID_TMP", "/tmp/forge");
72         env::set_var("USER", "ryle");
73         let tmp_dir = netsimd_temp_dir_pathbuf();
74         assert_eq!(tmp_dir.to_str().unwrap(), "/tmp/forge/android-ryle/netsimd");
75     }
76 
77     #[test]
test_non_forge()78     fn test_non_forge() {
79         let _locked = ENV_MUTEX.lock();
80         let temp_dir = env::temp_dir();
81         env::remove_var("ANDROID_TMP");
82         env::set_var("USER", "ryle");
83         let netsimd_temp_dir = netsimd_temp_dir_pathbuf();
84         assert_eq!(
85             netsimd_temp_dir.to_str().unwrap(),
86             temp_dir.join("android-ryle/netsimd").to_str().unwrap()
87         );
88     }
89 }
90