aria2-core 0.3.0

aria2-rust core download engine: multi-protocol segmented downloads, rate limiting, configuration, session persistence, and BitTorrent seeding
Documentation
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
//! Built-in hook implementations
//!
//! Provides standard post-download hooks:
//! - `MoveHook`: Move downloaded files to a target directory
//! - `RenameHook`: Rename files using a template pattern
//! - `TouchHook`: Update file timestamps
//! - `ExecHook`: Execute external commands with download metadata as env vars

use std::collections::HashMap;
use std::path::PathBuf;
use std::time::{SystemTime, UNIX_EPOCH};

use async_trait::async_trait;
use tokio::process::Command;
use tracing::{debug, info, warn};

use crate::error::{Aria2Error, Result};

use super::types::{HookContext, PostDownloadHook};

// ============================================================================
// MoveHook
// ============================================================================

/// File move hook
///
/// Moves the file to a specified directory after download completion.
/// Supports automatic creation of target directory structure.
#[derive(Clone, Debug)]
pub struct MoveHook {
    /// Target directory path
    target_dir: PathBuf,
    /// Whether to automatically create non-existent directories
    create_dirs: bool,
}

impl MoveHook {
    /// Create a new move hook
    ///
    /// # Arguments
    ///
    /// * `target_dir` - Target directory path
    /// * `create_dirs` - Whether to automatically create directories
    pub fn new(target_dir: PathBuf, create_dirs: bool) -> Self {
        Self {
            target_dir,
            create_dirs,
        }
    }
}

#[async_trait]
impl PostDownloadHook for MoveHook {
    /// Execute file move operation
    ///
    /// Verifies the source file exists, then moves it to the target directory.
    /// If `create_dirs` is true, the required directory hierarchy is created automatically.
    async fn on_complete(&self, context: &HookContext) -> Result<()> {
        let source = &context.file_path;

        // Verify source file exists
        if !source.exists() {
            return Err(Aria2Error::Fatal(crate::error::FatalError::FileNotFound {
                path: source.to_string_lossy().to_string(),
            }));
        }

        // Create target directory (if needed)
        if self.create_dirs && !self.target_dir.exists() {
            debug!(
                hook = "MoveHook",
                target_dir = %self.target_dir.display(),
                "Creating target directory"
            );
            tokio::fs::create_dir_all(&self.target_dir)
                .await
                .map_err(|e| {
                    Aria2Error::Io(format!(
                        "Failed to create directory {}: {}",
                        self.target_dir.display(),
                        e
                    ))
                })?;
        }

        // Build destination path
        let filename = context.filename();
        let destination = self.target_dir.join(filename);

        info!(
            hook = "MoveHook",
            source = %source.display(),
            dest = %destination.display(),
            "Moving file"
        );

        // Execute move operation
        tokio::fs::rename(source, &destination).await.map_err(|e| {
            Aria2Error::Io(format!(
                "Failed to move file from {} to {}: {}",
                source.display(),
                destination.display(),
                e
            ))
        })?;

        Ok(())
    }

    /// Move hook does nothing on error
    async fn on_error(&self, _context: &HookContext, _error: &str) -> Result<()> {
        // File move typically does not need to be executed on error
        Ok(())
    }

    fn name(&self) -> &'static str {
        "MoveHook"
    }
}

// ============================================================================
// RenameHook
// ============================================================================

/// File rename hook
///
/// Renames downloaded files using a template pattern.
/// Supported placeholders:
/// - `%d`: Source file directory
/// - `%f`: Original filename
/// - `%e`: File extension
/// - `%i`: Download task GID
/// - `%t`: Current timestamp (Unix timestamp)
#[derive(Clone, Debug)]
pub struct RenameHook {
    /// Rename template pattern
    pattern: String,
}

impl RenameHook {
    /// Create a new rename hook
    ///
    /// # Arguments
    ///
    /// * `pattern` - Rename template supporting placeholder substitution
    pub fn new(pattern: String) -> Self {
        Self { pattern }
    }

    /// Expand placeholders in the template
    ///
    /// Replaces special markers in the template string with actual values.
    ///
    /// # Arguments
    ///
    /// * `context` - Hook context used to obtain replacement values
    ///
    /// # Returns
    ///
    /// The expanded filename with all placeholders replaced
    pub fn expand_pattern(&self, context: &HookContext) -> String {
        let timestamp = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs();

        self.pattern
            .replace("%d", &context.directory().to_string_lossy())
            .replace("%f", context.filename())
            .replace("%e", context.extension())
            .replace("%i", &context.gid.value().to_string())
            .replace("%t", &timestamp.to_string())
    }
}

#[async_trait]
impl PostDownloadHook for RenameHook {
    /// Execute file rename operation
    ///
    /// Generates a new filename based on the template pattern and renames the file.
    async fn on_complete(&self, context: &HookContext) -> Result<()> {
        let source = &context.file_path;

        if !source.exists() {
            return Err(Aria2Error::Fatal(crate::error::FatalError::FileNotFound {
                path: source.to_string_lossy().to_string(),
            }));
        }

        let new_name = self.expand_pattern(context);

        // If the new name contains a path separator, treat it as a full path; otherwise rename in the same directory
        let destination = if new_name.contains(std::path::MAIN_SEPARATOR)
            || (std::path::MAIN_SEPARATOR == '\\' && new_name.contains('/'))
        {
            PathBuf::from(&new_name)
        } else {
            context.directory().join(&new_name)
        };

        info!(
            hook = "RenameHook",
            source = %source.display(),
            dest = %destination.display(),
            pattern = %self.pattern,
            "Renaming file"
        );

        tokio::fs::rename(source, &destination).await.map_err(|e| {
            Aria2Error::Io(format!(
                "Failed to rename file to {}: {}",
                destination.display(),
                e
            ))
        })?;

        Ok(())
    }

    /// Rename hook does nothing on error
    async fn on_error(&self, _context: &HookContext, _error: &str) -> Result<()> {
        Ok(())
    }

    fn name(&self) -> &'static str {
        "RenameHook"
    }
}

// ============================================================================
// TouchHook
// ============================================================================

/// File timestamp update hook
///
/// Updates the file's modification time and access time to the current system time after download completion.
#[derive(Clone, Debug)]
pub struct TouchHook;

impl TouchHook {
    /// Create a new TouchHook instance
    pub fn new() -> Self {
        Self
    }
}

impl Default for TouchHook {
    fn default() -> Self {
        Self::new()
    }
}

#[async_trait]
impl PostDownloadHook for TouchHook {
    /// Update the file's modification time and access time
    ///
    /// Sets both the file's mtime and atime to the current system time.
    async fn on_complete(&self, context: &HookContext) -> Result<()> {
        let path = &context.file_path;

        if !path.exists() {
            return Err(Aria2Error::Fatal(crate::error::FatalError::FileNotFound {
                path: path.to_string_lossy().to_string(),
            }));
        }

        let now = SystemTime::now();

        debug!(
            hook = "TouchHook",
            path = %path.display(),
            "Updating file timestamps"
        );

        // Use filetime crate or standard library to set timestamps
        #[cfg(unix)]
        {
            // Get existing permissions to preserve them
            let _metadata = tokio::fs::metadata(path).await.map_err(|e| {
                Aria2Error::Io(format!(
                    "Failed to get metadata for {}: {}",
                    path.display(),
                    e
                ))
            })?;

            // Use utimensat to set timestamps with nanosecond precision
            let duration = now.duration_since(UNIX_EPOCH).unwrap_or_default();
            let times: [libc::timespec; 2] = [
                libc::timespec {
                    tv_sec: duration.as_secs() as _,
                    tv_nsec: duration.subsec_nanos() as _,
                },
                libc::timespec {
                    tv_sec: duration.as_secs() as _,
                    tv_nsec: duration.subsec_nanos() as _,
                },
            ];

            let c_path = std::ffi::CString::new(path.to_string_lossy().as_bytes())
                .map_err(|e| Aria2Error::Io(format!("Invalid path: {}", e)))?;

            unsafe {
                if libc::utimensat(0, c_path.as_ptr(), times.as_ptr(), 0) != 0 {
                    return Err(Aria2Error::Io(format!(
                        "Failed to update timestamps for {}",
                        path.display()
                    )));
                }
            }
        }

        #[cfg(windows)]
        {
            // On Windows, use the standard library's set_times feature (requires Rust 1.75+)
            // or update timestamps by re-writing the file
            use std::fs;

            // Simple approach: read file metadata and set time
            let file = fs::OpenOptions::new().write(true).open(path).map_err(|e| {
                Aria2Error::Io(format!("Failed to open file {}: {}", path.display(), e))
            })?;

            file.set_modified(now).map_err(|e| {
                Aria2Error::Io(format!(
                    "Failed to set modified time for {}: {}",
                    path.display(),
                    e
                ))
            })?;
        }

        info!(
            hook = "TouchHook",
            path = %path.display(),
            "File timestamps updated"
        );

        Ok(())
    }

    /// TouchHook does nothing on error
    async fn on_error(&self, _context: &HookContext, _error: &str) -> Result<()> {
        Ok(())
    }

    fn name(&self) -> &'static str {
        "TouchHook"
    }
}

// ============================================================================
// ExecHook
// ============================================================================

/// External command execution hook
///
/// Executes a specified external command after download completion, injecting download-related
/// information as environment variables.
/// Supported environment variables:
/// - `ARIA2_GID`: Task group ID
/// - `ARIA2_PATH`: File path
/// - `ARIA2_STATUS`: Download status
/// - `ARIA2_ERROR`: Error message (if any)
/// - `ARIA2_DOWNLOADED_BYTES`: Downloaded bytes
/// - `ARIA2_UPLOADED_BYTES`: Uploaded bytes
/// - `ARIA2_DOWNLOAD_SPEED`: Download speed
/// - `ARIA2_UPLOAD_SPEED`: Upload speed
#[derive(Clone, Debug)]
pub struct ExecHook {
    /// Command to execute
    command: String,
    /// Additional environment variables
    env_vars: HashMap<String, String>,
}

impl ExecHook {
    /// Create a new command execution hook
    ///
    /// # Arguments
    ///
    /// * `command` - Shell command to execute
    /// * `env_vars` - Additional environment variable key-value pairs
    pub fn new(command: String, env_vars: HashMap<String, String>) -> Self {
        Self { command, env_vars }
    }

    /// Build environment variable mapping
    ///
    /// Merges user-defined environment variables with aria2 built-in environment variables.
    pub(crate) fn build_env(
        &self,
        context: &HookContext,
        status_override: Option<&str>,
    ) -> HashMap<String, String> {
        let mut env = HashMap::new();

        // Inject aria2-specific environment variables
        env.insert("ARIA2_GID".to_string(), context.gid.value().to_string());
        env.insert(
            "ARIA2_PATH".to_string(),
            context.file_path.to_string_lossy().to_string(),
        );
        env.insert(
            "ARIA2_STATUS".to_string(),
            status_override
                .unwrap_or(&context.status.to_string())
                .to_string(),
        );
        if let Some(ref err) = context.error {
            env.insert("ARIA2_ERROR".to_string(), err.clone());
        }
        env.insert(
            "ARIA2_DOWNLOADED_BYTES".to_string(),
            context.stats.downloaded_bytes.to_string(),
        );
        env.insert(
            "ARIA2_UPLOADED_BYTES".to_string(),
            context.stats.uploaded_bytes.to_string(),
        );
        env.insert(
            "ARIA2_DOWNLOAD_SPEED".to_string(),
            context.stats.download_speed.to_string(),
        );
        env.insert(
            "ARIA2_UPLOAD_SPEED".to_string(),
            context.stats.upload_speed.to_string(),
        );

        // Merge user-defined environment variables (can override built-in variables)
        for (k, v) in &self.env_vars {
            env.insert(k.clone(), v.clone());
        }

        env
    }
}

#[async_trait]
impl PostDownloadHook for ExecHook {
    /// Execute external command
    ///
    /// Executes the configured command via shell, injecting aria2-related environment variables.
    /// Non-zero exit codes are treated as execution failures.
    async fn on_complete(&self, context: &HookContext) -> Result<()> {
        let env = self.build_env(context, None);

        info!(
            hook = "ExecHook",
            command = %self.command,
            "Executing command on complete"
        );

        let output = Command::new("sh")
            .arg("-c")
            .arg(&self.command)
            .envs(&env)
            .output()
            .await
            .map_err(|e| {
                Aria2Error::Io(format!(
                    "Failed to execute command '{}': {}",
                    self.command, e
                ))
            })?;

        if !output.status.success() {
            let stderr = String::from_utf8_lossy(&output.stderr);
            warn!(
                hook = "ExecHook",
                command = %self.command,
                exit_code = ?output.status.code(),
                stderr = %stderr,
                "Command failed with non-zero exit code"
            );
            return Err(Aria2Error::DownloadFailed(format!(
                "Command '{}' failed with exit code {:?}: {}",
                self.command,
                output.status.code(),
                stderr.trim()
            )));
        }

        debug!(
            hook = "ExecHook",
            stdout = %String::from_utf8_lossy(&output.stdout),
            "Command executed successfully"
        );

        Ok(())
    }

    /// Execute command on download error
    ///
    /// Similar to `on_complete`, but the status is set to "error".
    async fn on_error(&self, context: &HookContext, error: &str) -> Result<()> {
        let mut ctx_with_error = context.clone();
        ctx_with_error.error = Some(error.to_string());
        let env = self.build_env(&ctx_with_error, Some("error"));

        info!(
            hook = "ExecHook",
            command = %self.command,
            "Executing command on error"
        );

        let output = Command::new("sh")
            .arg("-c")
            .arg(&self.command)
            .envs(&env)
            .output()
            .await
            .map_err(|e| {
                Aria2Error::Io(format!(
                    "Failed to execute command '{}': {}",
                    self.command, e
                ))
            })?;

        if !output.status.success() {
            let stderr = String::from_utf8_lossy(&output.stderr);
            return Err(Aria2Error::DownloadFailed(format!(
                "Command '{}' failed with exit code {:?}: {}",
                self.command,
                output.status.code(),
                stderr.trim()
            )));
        }

        Ok(())
    }

    fn name(&self) -> &'static str {
        "ExecHook"
    }
}