-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.rs
573 lines (516 loc) · 18.9 KB
/
utils.rs
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
use crate::{
command_executor::execute_command,
idf_config::{IdfConfig, IdfInstallation},
idf_tools::read_and_parse_tools_file,
single_version_post_install,
version_manager::get_default_config_path,
};
use anyhow::{anyhow, Result};
use log::debug;
use rust_search::SearchBuilder;
use serde::{Deserialize, Serialize};
#[cfg(not(windows))]
use std::os::unix::fs::MetadataExt;
use std::{
collections::{HashMap, HashSet},
fs::{self},
io,
path::{Path, PathBuf},
};
/// This function retrieves the path to the git executable.
///
/// # Purpose
///
/// The function attempts to locate the git executable by checking the system's PATH environment variable.
/// It uses the appropriate command ("where" on Windows, "which" on Unix-like systems) to find the git executable.
///
/// # Parameters
///
/// There are no parameters for this function.
///
/// # Return Value
///
/// - `Ok(String)`: If the git executable is found, the function returns a `Result` containing the path to the git executable as a `String`.
/// - `Err(String)`: If the git executable is not found or an error occurs during the process of locating the git executable, the function returns a `Result` containing an error message as a `String`.
pub fn get_git_path() -> Result<String, String> {
let cmd = match std::env::consts::OS {
"windows" => "where",
_ => "which",
};
let output = execute_command(cmd, &["git"]).expect("failed to execute process");
if output.status.success() {
let stdout = String::from_utf8_lossy(&output.stdout);
Ok(stdout.trim().to_string())
} else {
let stderr = String::from_utf8_lossy(&output.stderr);
Err(stderr.trim().to_string())
}
}
// Finds all directories in the specified path that match the given name.
// The function recursively searches subdirectories and collects matching paths in a vector.
// Returns a vector of PathBuf containing the paths of matching directories.
pub fn find_directories_by_name(path: &Path, name: &str) -> Vec<String> {
let search: Vec<String> = SearchBuilder::default()
.location(path)
.search_input(name)
// .limit(1000) // results to return
.strict()
// .depth(1)
.ignore_case()
.hidden()
.build()
.collect();
filter_subpaths(search)
}
/// Checks if the given path is a valid ESP-IDF directory.
///
/// # Purpose
///
/// This function verifies if the specified directory contains a valid ESP-IDF setup by checking for the existence of the "tools.json" file in the "tools" subdirectory.
///
/// # Parameters
///
/// - `path`: A reference to a string representing the path to be checked.
///
/// # Return Value
///
/// - `bool`: Returns `true` if the specified path is a valid ESP-IDF directory, and `false` otherwise.
pub fn is_valid_idf_directory(path: &str) -> bool {
let path = PathBuf::from(path);
let tools_path = path.join("tools");
let tools_json_path = tools_path.join("tools.json");
if !tools_json_path.exists() {
return false;
}
match read_and_parse_tools_file(tools_json_path.to_str().unwrap()) {
Ok(_) => true,
Err(_) => false,
}
}
/// Filters out duplicate paths from a vector of strings.
///
/// This function checks for duplicate paths in the input vector and removes them.
/// It uses different strategies based on the operating system:
/// - On Windows, it compares the modification time and size of each file to identify duplicates.
/// - On Unix-like systems, it uses the device ID and inode number to identify duplicates.
///
/// # Parameters
///
/// - `paths`: A vector of strings representing file paths.
///
/// # Return Value
///
/// - A vector of strings containing the unique paths from the input vector.
pub fn filter_duplicate_paths(paths: Vec<String>) -> Vec<String> {
let mut result = Vec::new();
match std::env::consts::OS {
"windows" => {
let mut seen = HashSet::new();
for path in paths {
if let Ok(metadata) = fs::metadata(&path) {
let key = format!("{:?}-{:?}", metadata.modified().ok(), metadata.len());
if seen.insert(key) {
result.push(path);
}
} else {
result.push(path);
}
}
}
_ => {
#[cfg(not(windows))]
let mut seen = HashSet::new();
#[cfg(not(windows))]
for path in paths {
// Get the metadata for the path
if let Ok(metadata) = fs::metadata(&path) {
// Create a tuple of device ID and inode number
let file_id = (metadata.dev(), metadata.ino());
// Only keep the path if we haven't seen this file_id before
if seen.insert(file_id) {
result.push(path);
}
} else {
// If we can't get metadata, keep the original path
result.push(path);
}
}
}
}
result
}
/// Filters out subpaths from a vector of strings.
///
/// This function checks for subpaths in the input vector and removes them.
/// It ensures that only the highest-level paths are retained.
///
/// # Parameters
///
/// - `paths`: A vector of strings representing file paths.
///
/// # Return Value
///
/// - A vector of strings containing the highest-level paths from the input vector.
/// Subpaths are removed, and only the highest-level paths are retained.
fn filter_subpaths(paths: Vec<String>) -> Vec<String> {
let mut filtered = Vec::new();
'outer: for path in paths {
// Check if this path is a subpath of any already filtered path
for other in &filtered {
if path.starts_with(other) {
continue 'outer;
}
}
// Remove any previously added paths that are subpaths of this one
filtered.retain(|other: &String| !other.starts_with(&path));
// Add this path
filtered.push(path);
}
filtered
}
/// Removes a directory and all its contents recursively.
///
/// This function attempts to remove a directory and all its contents, including subdirectories and files.
/// It handles cases where the directory or files are read-only on Windows.
///
/// # Parameters
///
/// - `path`: A reference to a type that implements the `AsRef<Path>` trait, representing the path to the directory to be removed.
///
/// # Return Value
///
/// - `io::Result<()>`: If the directory and its contents are successfully removed, the function returns `Ok(())`.
/// If an error occurs during the process, the function returns an `io::Error` containing the specific error details.
pub fn remove_directory_all<P: AsRef<Path>>(path: P) -> io::Result<()> {
let path = path.as_ref();
if !path.exists() {
return Ok(());
}
// First ensure all contents are writable to handle readonly files
if path.is_dir() {
for entry in fs::read_dir(path)? {
let entry = entry?;
let path = entry.path();
if path.is_dir() {
fs::remove_dir_all(&path)?;
} else {
// On Windows, we need to ensure the file is writable before removal
#[cfg(windows)]
{
let metadata = fs::metadata(&path)?;
let mut permissions = metadata.permissions();
permissions.set_readonly(false);
fs::set_permissions(&path, permissions)?;
}
fs::remove_file(&path)?;
}
}
}
// Now remove the directory itself
if path.is_dir() {
fs::remove_dir_all(path)?;
} else {
fs::remove_file(path)?;
}
Ok(())
}
/// Retry wrapper function that takes a closure and retries it according to the configuration
pub fn with_retry<F, T, E>(f: F, max_retries: usize) -> Result<T, E>
where
F: Fn() -> Result<T, E>,
E: std::fmt::Debug,
{
let mut attempt = 0;
loop {
match f() {
Ok(value) => return Ok(value),
Err(e) => {
attempt += 1;
if attempt >= max_retries {
return Err(e);
}
debug!("Attempt {} failed with error: {:?}", attempt, e);
}
}
}
}
#[derive(Debug, Serialize, Deserialize)]
pub struct IdfToolsConfig {
pub id: i64,
#[serde(rename = "idfLocation")]
pub idf_location: String,
#[serde(rename = "idfVersion")]
pub idf_version: String,
pub active: bool,
#[serde(rename = "systemGitExecutablePath")]
pub system_git_executable_path: String,
#[serde(rename = "systemPythonExecutablePath")]
pub system_python_executable_path: String,
#[serde(rename = "envVars")]
pub env_vars: HashMap<String, String>,
}
fn extract_tools_path_from_python_env_path(path: &str) -> Option<PathBuf> {
let path = PathBuf::from(path);
path.ancestors()
.find(|p| p.file_name().map_or(false, |name| name == "python_env"))
.and_then(|p| p.parent().map(|parent| parent.to_path_buf()))
}
/// Parses and processes a configuration file for IDF tools.
///
/// # Purpose
///
/// This function reads a JSON configuration file containing information about different IDF tool sets.
/// It then processes this information to update the IDF installation configuration.
///
/// # Parameters
///
/// - `config_path`: A string representing the path to the configuration file.
///
/// # Return Value
///
/// This function does not return a value.
///
/// # Errors
///
/// This function logs errors to the console if the configuration file cannot be read or parsed.
/// It also logs errors if the IDF installation configuration cannot be updated.
pub fn parse_tool_set_config(config_path: &str) -> Result<()> {
let config_path = Path::new(config_path);
let json_str = std::fs::read_to_string(config_path).unwrap();
let config: Vec<IdfToolsConfig> = match serde_json::from_str(&json_str) {
Ok(config) => config,
Err(e) => return Err(anyhow!("Failed to parse config file: {}", e)),
};
let config_path = get_default_config_path();
let mut current_config = match IdfConfig::from_file(&config_path) {
Ok(config) => config,
Err(e) => {
return Err(anyhow!("Config file not found: {}", e));
}
};
for tool_set in config {
if current_config
.idf_installed
.iter()
.any(|i| i.path == tool_set.idf_location)
{
continue;
}
let new_idf_tools_path = extract_tools_path_from_python_env_path(
tool_set.env_vars.get("IDF_PYTHON_ENV_PATH").unwrap(),
)
.unwrap()
.to_str()
.unwrap()
.to_string();
let new_export_paths = vec![tool_set.env_vars.get("PATH").unwrap().to_string()];
let tmp = PathBuf::from(tool_set.idf_location.clone());
let version_path = tmp.parent().unwrap();
single_version_post_install(
version_path.to_str().unwrap(),
&tool_set.idf_location,
&tool_set.idf_version,
&new_idf_tools_path,
new_export_paths,
);
let new_activation_script = match std::env::consts::OS {
"windows" => format!(
"{}\\Microsoft.PowerShell_profile.ps1",
version_path.to_str().unwrap()
),
_ => format!(
"{}/{}",
version_path.to_str().unwrap(),
format!("activate_idf_{}.sh", tool_set.idf_version)
),
};
let installation = IdfInstallation {
id: tool_set.id.to_string(),
activation_script: new_activation_script,
path: tool_set.idf_location,
name: tool_set.idf_version,
python: tool_set.system_python_executable_path,
idf_tools_path: new_idf_tools_path,
};
current_config.idf_installed.push(installation);
}
match current_config.to_file(config_path, true) {
Ok(_) => {
debug!("Updated config file with new tool set");
return Ok(());
}
Err(e) => {
return Err(anyhow!("Failed to update config file: {}", e));
}
}
}
/// Converts a path to a long path compatible with Windows.
///
/// This function takes a string representing a path and returns a new string.
/// If the input path is on a Windows system and does not already start with `\\?\`,
/// the function converts the path to a long path by canonicalizing the path,
/// and then adding the `\\?\` prefix.
/// If the input path is not on a Windows system or already starts with `\\?\`,
/// the function returns the input path unchanged.
///
/// # Parameters
///
/// * `path`: A string representing the path to be converted.
///
/// # Return Value
///
/// A string representing the converted path.
/// If the input path is on a Windows system and does not already start with `\\?\`,
/// the returned string will be a long path with the `\\?\` prefix.
/// If the input path is not on a Windows system or already starts with `\\?\`,
/// the returned string will be the same as the input path.
pub fn make_long_path_compatible(path: &str) -> String {
if std::env::consts::OS == "windows" && !path.starts_with(r"\\?\") {
// Convert to absolute path and add \\?\ prefix
let absolute_path = std::fs::canonicalize(path).unwrap_or_else(|_| PathBuf::from(path));
let mut long_path = PathBuf::from(r"\\?\");
long_path.push(absolute_path);
long_path.to_str().unwrap().to_string()
} else {
path.to_string()
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::fs::{self, File};
use std::io::Write;
use std::sync::atomic::{AtomicU32, Ordering};
use tempfile::TempDir;
#[test]
fn test_find_directories_by_name() {
let temp_dir = TempDir::new().unwrap();
let base_path = temp_dir.path();
// Create test directory structure
let test_dir1 = base_path.join("test_dir");
let test_dir2 = base_path.join("subdir").join("test_dir");
fs::create_dir_all(&test_dir1).unwrap();
fs::create_dir_all(&test_dir2).unwrap();
let results = find_directories_by_name(base_path, "test_dir");
assert_eq!(results.len(), 2);
assert!(results
.iter()
.any(|p| p.contains(test_dir1.to_str().unwrap())));
assert!(results
.iter()
.any(|p| p.contains(test_dir2.to_str().unwrap())));
}
#[test]
fn test_is_valid_idf_directory() {
let temp_dir = TempDir::new().unwrap();
let base_path = temp_dir.path();
// Create invalid directory (no tools.json)
assert!(!is_valid_idf_directory(base_path.to_str().unwrap()));
// Create valid IDF directory structure
let tools_dir = base_path.join("tools");
fs::create_dir_all(&tools_dir).unwrap();
let tools_json_path = tools_dir.join("tools.json");
let mut file = File::create(tools_json_path).unwrap();
write!(file, r#"{{"tools": [], "version": 1}}"#).unwrap();
assert!(is_valid_idf_directory(base_path.to_str().unwrap()));
}
#[test]
fn test_filter_duplicate_paths() {
let temp_dir = TempDir::new().unwrap();
let base_path = temp_dir.path();
// Create test files with different content
let file1_path = base_path.join("file1.txt");
let file2_path = base_path.join("file2.txt");
fs::write(&file1_path, "content1").unwrap();
let duration = std::time::Duration::from_millis(1000); // Sleep for 1 second
std::thread::sleep(duration); // because on windows we use the modified time to identify duplicates
fs::write(&file2_path, "content2").unwrap();
let paths = vec![
file1_path.to_string_lossy().to_string(),
file1_path.to_string_lossy().to_string(), // Duplicate
file2_path.to_string_lossy().to_string(),
];
let filtered = filter_duplicate_paths(paths);
assert_eq!(filtered.len(), 2);
}
#[test]
fn test_filter_subpaths() {
let paths = vec![
"/path/to/dir".to_string(),
"/path/to/dir/subdir".to_string(),
"/path/to/another".to_string(),
];
let filtered = filter_subpaths(paths);
assert_eq!(filtered.len(), 2);
assert!(filtered.contains(&"/path/to/dir".to_string()));
assert!(filtered.contains(&"/path/to/another".to_string()));
assert!(!filtered.contains(&"/path/to/dir/subdir".to_string()));
}
#[test]
fn test_remove_directory_all() {
let temp_dir = TempDir::new().unwrap();
let base_path = temp_dir.path();
// Create test directory structure
let test_dir = base_path.join("test_dir");
let test_subdir = test_dir.join("subdir");
let test_file = test_dir.join("test.txt");
fs::create_dir_all(&test_subdir).unwrap();
fs::write(&test_file, "test content").unwrap();
// Test removal
assert!(remove_directory_all(&test_dir).is_ok());
assert!(!test_dir.exists());
}
#[test]
fn test_remove_directory_all_nonexistent() {
let temp_dir = TempDir::new().unwrap();
let non_existent = temp_dir.path().join("non_existent");
assert!(remove_directory_all(&non_existent).is_ok());
}
#[test]
fn test_remove_directory_all_readonly() {
let temp_dir = TempDir::new().unwrap();
let test_dir = temp_dir.path().join("readonly_dir");
let test_file = test_dir.join("readonly.txt");
fs::create_dir_all(&test_dir).unwrap();
fs::write(&test_file, "readonly content").unwrap();
#[cfg(windows)]
{
let metadata = fs::metadata(&test_file).unwrap();
let mut permissions = metadata.permissions();
permissions.set_readonly(true);
fs::set_permissions(&test_file, permissions).unwrap();
}
assert!(remove_directory_all(&test_dir).is_ok());
assert!(!test_dir.exists());
}
#[test]
fn test_retry_success_after_failure() {
let counter = AtomicU32::new(0);
let result = with_retry(
|| {
let current = counter.fetch_add(1, Ordering::SeqCst);
if current < 2 {
Err("Not ready yet")
} else {
Ok("Success!")
}
},
3,
);
assert!(result.is_ok());
assert_eq!(counter.load(Ordering::SeqCst), 3);
}
#[test]
fn test_retry_all_attempts_failed() {
let counter = AtomicU32::new(0);
let result: Result<&str, &str> = with_retry(
|| {
counter.fetch_add(1, Ordering::SeqCst);
Err("Always fails")
},
3,
);
assert!(result.is_err());
assert_eq!(counter.load(Ordering::SeqCst), 3);
}
}