Create netsimd_temp_dir() based on android::base::GetTempDir(). Use
this for the packet capture directory. Files will be included in
sponge.
BUG: 279279040
Change-Id: If24882b6a70b0c7be8dfaa6285a40b1bcd8887b1
diff --git a/rust/netsim-cxx/src/captures/capture.rs b/rust/netsim-cxx/src/captures/capture.rs
index a04cbea..bd73d77 100644
--- a/rust/netsim-cxx/src/captures/capture.rs
+++ b/rust/netsim-cxx/src/captures/capture.rs
@@ -32,6 +32,7 @@
use protobuf::well_known_types::timestamp::Timestamp;
use crate::ffi::get_facade_id;
+use crate::system;
use super::pcap_util::write_pcap_header;
@@ -82,13 +83,13 @@
// Creates a pcap file with headers and store it under temp directory
// The lifecycle of the file is NOT tied to the lifecycle of the struct
- // Format: /tmp/netsim-pcaps/{chip_id}-{device_name}-{chip_kind}.pcap
+ // Format: /tmp/netsimd/$USER/pcaps/{chip_id}-{device_name}-{chip_kind}.pcap
pub fn start_capture(&mut self) -> Result<()> {
if self.file.is_some() {
return Ok(());
}
- let mut filename = std::env::temp_dir();
- filename.push("netsim-pcaps");
+ let mut filename = system::netsimd_temp_dir();
+ filename.push("pcaps");
std::fs::create_dir_all(&filename)?;
filename.push(format!("{:?}-{:}-{:?}.pcap", self.id, self.device_name, self.chip_kind));
let mut file = OpenOptions::new().write(true).truncate(true).create(true).open(filename)?;
diff --git a/rust/netsim-cxx/src/lib.rs b/rust/netsim-cxx/src/lib.rs
index 4fda715..3c665cc 100644
--- a/rust/netsim-cxx/src/lib.rs
+++ b/rust/netsim-cxx/src/lib.rs
@@ -20,6 +20,7 @@
mod devices;
mod http_server;
mod ranging;
+mod system;
mod transport;
mod uwb;
mod version;
diff --git a/rust/netsim-cxx/src/system/mod.rs b/rust/netsim-cxx/src/system/mod.rs
new file mode 100644
index 0000000..3b62549
--- /dev/null
+++ b/rust/netsim-cxx/src/system/mod.rs
@@ -0,0 +1,75 @@
+// Copyright 2023 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+//! Inspection and manipulation of the system environment.
+
+use std::env;
+use std::path::PathBuf;
+
+/// Get the netsimd temporary directory.
+///
+/// This is based on emu System.cpp android::base::getTempDir()
+///
+/// Under Forge temp directory is `$ANDROID_TMP/android-$USER/netsimd`,
+/// otherwise it is `$TMP/android-$USER/netsimd`
+///
+pub fn netsimd_temp_dir() -> PathBuf {
+ // allow Forge to override the system temp
+ let mut path = match env::var("ANDROID_TMP") {
+ Ok(var) => PathBuf::from(var),
+ _ => env::temp_dir(),
+ };
+ // On Windows the GetTempPath() is user-dependent so we don't need
+ // to append $USER to the result -- otherwise allow multiple users
+ // to co-exist on a system.
+ #[cfg(not(target_os = "windows"))]
+ {
+ let user = match env::var("USER") {
+ Ok(var) => format!("android-{}", var),
+ _ => "android".to_string(),
+ };
+ path.push(user);
+ };
+ // netsimd files are stored in their own directory
+ path.push("netsimd");
+ path
+}
+
+#[cfg(test)]
+mod tests {
+ use super::netsimd_temp_dir;
+ use std::env;
+ use std::sync::Mutex;
+
+ static ENV_MUTEX: Mutex<i32> = Mutex::new(0);
+
+ #[test]
+ fn test_forge() {
+ let _locked = ENV_MUTEX.lock();
+ env::set_var("ANDROID_TMP", "/tmp/forge");
+ env::set_var("USER", "ryle");
+ let tmp_dir = netsimd_temp_dir();
+ assert_eq!(tmp_dir.to_str().unwrap(), "/tmp/forge/android-ryle/netsimd");
+ }
+
+ #[cfg(not(target_os = "windows"))]
+ #[test]
+ fn test_non_forge() {
+ let _locked = ENV_MUTEX.lock();
+ env::remove_var("ANDROID_TMP");
+ env::set_var("USER", "ryle");
+ let tmp_dir = netsimd_temp_dir();
+ assert_eq!(tmp_dir.to_str().unwrap(), "/tmp/android-ryle/netsimd");
+ }
+}