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
//! Offline mode handling for storage
//!
//! Provides functionality to detect when storage is unavailable and operate
//! in read-only mode with cached data.
use crate::error::{StorageError, StorageResult};
use crate::types::StorageState;
use std::path::Path;
use std::time::{SystemTime, UNIX_EPOCH};
use tracing::{debug, warn};
/// Offline mode handler
pub struct OfflineModeHandler;
impl OfflineModeHandler {
/// Check if storage is available
///
/// # Arguments
///
/// * `storage_path` - Path to storage directory
///
/// # Returns
///
/// Returns the storage state (Available, Unavailable, or ReadOnly)
pub fn check_storage_availability(storage_path: &Path) -> StorageState {
// Check if path exists
if !storage_path.exists() {
warn!(
"Storage unavailable: path does not exist: {}",
storage_path.display()
);
return StorageState::Unavailable {
reason: "Storage path does not exist".to_string(),
};
}
// Check if path is accessible (try to read directory)
match std::fs::read_dir(storage_path) {
Ok(_) => {
debug!("Storage is available: {}", storage_path.display());
StorageState::Available
}
Err(e) => {
warn!(
"Storage unavailable: cannot read directory {}: {}",
storage_path.display(),
e
);
StorageState::Unavailable {
reason: format!("Cannot read directory: {}", e),
}
}
}
}
/// Check if storage is on external or network drive
///
/// # Arguments
///
/// * `storage_path` - Path to storage directory
///
/// # Returns
///
/// Returns true if storage appears to be on external/network storage
pub fn is_external_storage(storage_path: &Path) -> bool {
let path_str = storage_path.to_string_lossy();
// Check for common network/external indicators
#[cfg(target_os = "windows")]
{
// Check for UNC paths (network drives)
if path_str.starts_with("\\\\") {
return true;
}
// Check for mapped drives (typically Z:, Y:, etc.)
if let Some(drive) = path_str.chars().next() {
if drive.is_alphabetic() {
let drive_letter = drive.to_ascii_uppercase();
// Assume drives beyond D: might be external/network
if drive_letter > 'D' {
return true;
}
}
}
}
#[cfg(target_os = "macos")]
{
// Check for mounted volumes
if path_str.starts_with("/Volumes/") {
return true;
}
}
#[cfg(target_os = "linux")]
{
// Check for mounted filesystems
if path_str.starts_with("/mnt/") || path_str.starts_with("/media/") {
return true;
}
}
false
}
/// Transition to offline mode
///
/// # Arguments
///
/// * `storage_path` - Path to storage directory
/// * `cache_available` - Whether cached data is available
///
/// # Returns
///
/// Returns the new storage state
pub fn enter_offline_mode(storage_path: &Path, cache_available: bool) -> StorageState {
let cached_at = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap_or_default()
.as_secs()
.to_string();
if cache_available {
warn!(
"Entering offline mode for storage: {}. Using cached data.",
storage_path.display()
);
StorageState::ReadOnly { cached_at }
} else {
warn!(
"Entering offline mode for storage: {}. No cached data available.",
storage_path.display()
);
StorageState::Unavailable {
reason: "Storage unavailable and no cached data available".to_string(),
}
}
}
/// Check if we should retry storage access
///
/// # Arguments
///
/// * `storage_path` - Path to storage directory
///
/// # Returns
///
/// Returns true if storage is now available
pub fn retry_storage_access(storage_path: &Path) -> bool {
match Self::check_storage_availability(storage_path) {
StorageState::Available => {
debug!("Storage is now available: {}", storage_path.display());
true
}
_ => {
debug!("Storage is still unavailable: {}", storage_path.display());
false
}
}
}
/// Log offline mode warning
///
/// # Arguments
///
/// * `storage_path` - Path to storage directory
/// * `reason` - Reason for offline mode
pub fn log_offline_warning(storage_path: &Path, reason: &str) {
warn!(
"Storage offline mode activated for {}: {}",
storage_path.display(),
reason
);
}
/// Validate that we can operate in offline mode
///
/// # Arguments
///
/// * `cache_available` - Whether cached data is available
///
/// # Returns
///
/// Returns error if offline mode cannot be used
pub fn validate_offline_mode(cache_available: bool) -> StorageResult<()> {
if !cache_available {
return Err(StorageError::internal(
"Cannot enter offline mode: no cached data available",
));
}
debug!("Offline mode validation passed");
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
use tempfile::TempDir;
#[test]
fn test_check_storage_availability_exists() {
let temp_dir = TempDir::new().unwrap();
let state = OfflineModeHandler::check_storage_availability(temp_dir.path());
assert_eq!(state, StorageState::Available);
}
#[test]
fn test_check_storage_availability_not_exists() {
let path = std::path::PathBuf::from("/nonexistent/path/that/does/not/exist");
let state = OfflineModeHandler::check_storage_availability(&path);
match state {
StorageState::Unavailable { .. } => {
// Expected
}
_ => panic!("Expected Unavailable state"),
}
}
#[test]
fn test_enter_offline_mode_with_cache() {
let temp_dir = TempDir::new().unwrap();
let state = OfflineModeHandler::enter_offline_mode(temp_dir.path(), true);
match state {
StorageState::ReadOnly { .. } => {
// Expected
}
_ => panic!("Expected ReadOnly state"),
}
}
#[test]
fn test_enter_offline_mode_without_cache() {
let temp_dir = TempDir::new().unwrap();
let state = OfflineModeHandler::enter_offline_mode(temp_dir.path(), false);
match state {
StorageState::Unavailable { .. } => {
// Expected
}
_ => panic!("Expected Unavailable state"),
}
}
#[test]
fn test_retry_storage_access_available() {
let temp_dir = TempDir::new().unwrap();
let result = OfflineModeHandler::retry_storage_access(temp_dir.path());
assert!(result);
}
#[test]
fn test_retry_storage_access_unavailable() {
let path = std::path::PathBuf::from("/nonexistent/path");
let result = OfflineModeHandler::retry_storage_access(&path);
assert!(!result);
}
#[test]
fn test_validate_offline_mode_with_cache() {
let result = OfflineModeHandler::validate_offline_mode(true);
assert!(result.is_ok());
}
#[test]
fn test_validate_offline_mode_without_cache() {
let result = OfflineModeHandler::validate_offline_mode(false);
assert!(result.is_err());
}
#[test]
fn test_is_external_storage() {
// This test is platform-specific
#[cfg(target_os = "windows")]
{
// UNC paths are external
let unc_path = std::path::PathBuf::from("\\\\server\\share");
assert!(OfflineModeHandler::is_external_storage(&unc_path));
}
#[cfg(target_os = "macos")]
{
// /Volumes paths are external
let volume_path = std::path::PathBuf::from("/Volumes/ExternalDrive");
assert!(OfflineModeHandler::is_external_storage(&volume_path));
}
#[cfg(target_os = "linux")]
{
// /mnt and /media paths are external
let mnt_path = std::path::PathBuf::from("/mnt/external");
assert!(OfflineModeHandler::is_external_storage(&mnt_path));
let media_path = std::path::PathBuf::from("/media/user/external");
assert!(OfflineModeHandler::is_external_storage(&media_path));
}
}
}