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
//! File I/O operations for session persistence
//!
//! Provides async functions for loading and saving session files
//! using atomic write patterns for data integrity.
use Path;
use Arc;
use crate;
use crateRequestGroup;
use SessionEntry;
use deserialize;
use serialize_groups;
/// Loads and deserializes session entries from a file
///
/// Reads the specified session file and parses its contents into
/// a vector of SessionEntry objects using atomic read operations.
///
/// # Arguments
///
/// * `path` - Path to the session file to load
///
/// # Returns
///
/// Result containing a Vec of SessionEntry objects or an IO error
///
/// # Errors
///
/// Returns an error if:
/// - The file cannot be read (permission denied, not found, etc.)
/// - The file contains invalid UTF-8
///
/// # Example
///
/// ```rust,no_run
/// use aria2_core::session::session_serializer::load_from_file;
/// use std::path::Path;
///
/// #[tokio::main]
/// async fn main() {
/// let path = Path::new("aria2.session");
/// let _entries = load_from_file(path).await.unwrap();
/// }
/// ```
pub async
/// Saves multiple RequestGroups to a session file using atomic write
///
/// Serializes all provided RequestGroups and writes them to the specified
/// file using an atomic write pattern (write to temp file + rename).
/// This ensures session file integrity even if the process crashes during write.
///
/// # Arguments
///
/// * `path` - Target path for the session file
/// * `groups` - Slice of Arc<std::sync::RwLock<RequestGroup>> references to serialize
///
/// # Returns
///
/// Result indicating success or an IO error
///
/// # Atomic Write Strategy
///
/// 1. Serialize all groups to memory
/// 2. Write to `{path}.sess.tmp` temporary file
/// 3. Rename temp file to target path (atomic on most filesystems)
///
/// # Errors
///
/// Returns an error if:
/// - Temporary file cannot be written
/// - Rename operation fails
///
/// # Example
///
/// ```rust,no_run
/// use aria2_core::session::session_serializer::save_to_file;
/// use std::path::Path;
/// use std::sync::Arc;
/// use std::sync::RwLock;
///
/// #[tokio::main]
/// async fn main() {
/// let path = Path::new("aria2.session");
/// let groups: Vec<Arc<RwLock<aria2_core::request::request_group::RequestGroup>>> = vec![];
/// save_to_file(path, &groups).await.unwrap();
/// }
/// ```
pub async
/// Saves pre-serialized SessionEntry list directly to file (bypasses RequestGroup conversion)
///
/// Useful when you already have SessionEntry objects and want to save them
/// without converting through RequestGroup. Uses atomic write pattern for safety.
///
/// # Arguments
///
/// * `path` - Target path for the session file
/// * `entries` - Slice of SessionEntry objects to serialize and save
///
/// # Returns
///
/// Result indicating success or an IO error
///
/// # When to Use
///
/// - Testing session persistence without full RequestGroup setup
/// - Migrating sessions from another source
/// - Manual session manipulation tools
///
/// # Atomic Write Strategy
///
/// Same as [`save_to_file()`]: write to `.sess.tmp` then rename atomically.
///
/// # Example
///
/// ```rust,no_run
/// use aria2_core::session::session_entry::SessionEntry;
/// use aria2_core::session::session_serializer::save_to_file_with_entries;
/// use std::path::Path;
///
/// #[tokio::main]
/// async fn main() {
/// let path = Path::new("custom.session");
/// let entries = vec![
/// SessionEntry::new(1, vec!["http://example.com/f".to_string()]),
/// ];
/// save_to_file_with_entries(path, &entries).await.unwrap();
/// }
/// ```
pub async