aria2-core 0.2.1

High-performance download engine core: multi-protocol segmented downloads, rate limiting, config management, 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
//! File locking to prevent concurrent aria2 instances from writing to same file.
//!
//! This module provides platform-appropriate file locking mechanisms:
//!
//! - **Unix**: Uses `flock(LOCK_EX | LOCK_NB)` for advisory exclusive locking.
//! - **Windows**: Uses a lock marker file (`.lock` extension) as a simple
//!   exclusive access mechanism.
//! - **Other**: No-op fallback (no locking).
//!
//! # Example
//!
//! ```rust,no_run
//! use aria2_core::filesystem::file_lock::{FileLock, DownloadPathLock};
//! use std::path::Path;
//!
//! // Acquire exclusive lock on a specific file
//! let lock = FileLock::acquire(Path::new("/data/file.zip")).unwrap();
///
/// // ... perform download while holding lock ...
///
/// // Lock is released automatically when `lock` goes out of scope
/// ```
use std::path::{Path, PathBuf};

// ---------------------------------------------------------------------------
// FileLock -- platform-adaptive exclusive file lock
// ---------------------------------------------------------------------------

/// Exclusive file lock to prevent concurrent aria2 instances from writing to
/// the same file simultaneously.
///
/// Uses platform-appropriate locking mechanism:
/// - On **Unix**: `flock(LOCK_EX | LOCK_NB)` via libc (non-blocking).
/// - On **Windows**: Creates a `.lock` marker file with exclusive semantics.
/// - On **other platforms**: No-op placeholder.
///
/// The lock is automatically released when this struct is dropped.
pub struct FileLock {
    path: PathBuf,
    #[cfg(unix)]
    file: Option<std::fs::File>,
    #[cfg(windows)]
    _handle: Option<()>,
    #[cfg(not(any(unix, windows)))]
    _handle: Option<()>,
}

impl FileLock {
    /// Acquire an exclusive lock on the given file path.
    ///
    /// # Unix Behavior
    ///
    /// Opens/creates the file and calls `flock(fd, LOCK_EX | LOCK_NB)`.
    /// If another process already holds the lock, returns an error immediately
    /// (non-blocking). The lock is **advisory** -- cooperative processes must
    /// all use `flock` for it to be effective. The lock is released when the
    /// returned `FileLock` is dropped (or when [`release`] is called).
    ///
    /// # Windows Behavior
    ///
    /// Creates a sibling `.lock` file in the same directory. This is a simple
    /// marker-based approach; a production implementation would use
    /// `LockFileEx` or a named mutex for true exclusivity.
    ///
    /// # Errors
    ///
    /// Returns `Err` if:
    /// - The file cannot be created/opened (permission denied, disk full, etc.)
    /// - Another process already holds the lock (Unix only)
    pub fn acquire(path: &Path) -> Result<Self, String> {
        #[cfg(unix)]
        {
            Self::acquire_unix(path)
        }
        #[cfg(windows)]
        {
            Self::acquire_windows(path)
        }
        #[cfg(not(any(unix, windows)))]
        {
            Self::acquire_fallback(path)
        }
    }

    /// Unix implementation: flock(LOCK_EX | LOCK_NB).
    #[cfg(unix)]
    fn acquire_unix(path: &Path) -> Result<Self, String> {
        use std::os::unix::io::AsRawFd;

        // Create or open the target file
        let f = std::fs::File::create(path)
            .map_err(|e| format!("Failed to create '{}': {}", path.display(), e))?;

        let fd = f.as_raw_fd();

        // Attempt non-blocking exclusive lock (flock LOCK_EX | LOCK_NB)
        // Returns 0 on success, -1 on error (EAGAIN/EWOULDBLOCK if locked)
        let ret = unsafe { libc::flock(fd, libc::LOCK_EX | libc::LOCK_NB) };

        if ret != 0 {
            let err = std::io::Error::last_os_error();
            return Err(format!(
                "Cannot acquire lock on '{}': {} (already in use by another process?)",
                path.display(),
                err
            ));
        }

        tracing::debug!(path = %path.display(), "File lock acquired (Unix flock)");

        Ok(Self {
            path: path.to_path_buf(),
            file: Some(f),
        })
    }

    /// Windows implementation: .lock marker file with exclusive create.
    #[cfg(windows)]
    fn acquire_windows(path: &Path) -> Result<Self, String> {
        use std::io::Write;
        // Use a clearly separate lock file name to avoid ambiguity with
        // with_extension() behavior on dotted filenames like ".aria2.lock"
        let lock_path = if let Some(parent) = path.parent() {
            if let Some(stem) = path.file_stem() {
                parent.join(format!("{}.lock", stem.to_string_lossy()))
            } else {
                path.with_extension(".lock")
            }
        } else {
            path.with_extension(".lock")
        };

        // Try to create the lock file exclusively (fails if exists)
        match std::fs::OpenOptions::new()
            .write(true)
            .create_new(true)
            .open(&lock_path)
        {
            Ok(mut f) => {
                // Write our PID as lock owner identifier
                let pid = std::process::id();
                let _ = write!(f, "{}", pid);
                // Keep file handle open to maintain lock (dropped = released)
                drop(f);
                Ok(Self {
                    path: path.to_path_buf(),
                    _handle: Some(()),
                })
            }
            Err(e) => {
                if e.kind() == std::io::ErrorKind::AlreadyExists {
                    Err(format!(
                        "Cannot acquire lock on '{}': lock file '{}' already exists",
                        path.display(),
                        lock_path.display()
                    ))
                } else {
                    Err(format!(
                        "Failed to create lock file '{}': {}",
                        lock_path.display(),
                        e
                    ))
                }
            }
        }
    }

    /// Fallback for unsupported platforms: no-op.
    #[cfg(not(any(unix, windows)))]
    fn acquire_fallback(path: &Path) -> Result<Self, String> {
        tracing::warn!(
            path = %path.display(),
            "File locking not supported on this platform; proceeding without lock"
        );

        Ok(Self {
            path: path.to_path_buf(),
            _handle: None,
        })
    }

    /// Explicitly release the lock.
    ///
    /// After calling this method, the lock is released and the `FileLock`
    /// should no longer be used. Dropping the `FileLock` also releases the
    /// lock, so explicit release is optional.
    pub fn release(self) {
        #[cfg(unix)]
        {
            use std::os::fd::AsRawFd;
            if let Some(f) = &self.file {
                let fd = f.as_raw_fd();
                let _ = unsafe { libc::flock(fd, libc::LOCK_UN) };
                tracing::debug!(
                    path = %self.path.display(),
                    "File lock released (Unix flock)"
                );
            }
        }
        #[cfg(windows)]
        {
            let lock_path = self.lock_file_path();
            let _ = std::fs::remove_file(&lock_path);
            tracing::debug!(
                path = %self.path.display(),
                lock_file = %lock_path.display(),
                "Lock file removed (Windows)"
            );
        }
        #[cfg(not(any(unix, windows)))]
        {
            let _ = self; // suppress unused warning
        }
    }

    /// Check whether we currently hold the lock.
    ///
    /// Always returns `true` if [`acquire`] succeeded (the lock is held until
    /// dropped or explicitly released). Returns `false` only if the lock was
    /// never acquired.
    pub fn is_held(&self) -> bool {
        #[cfg(unix)]
        {
            self.file.is_some()
        }
        #[cfg(windows)]
        {
            self._handle.is_some()
        }
        #[cfg(not(any(unix, windows)))]
        {
            false
        }
    }

    /// Return the path that this lock guards.
    pub fn path(&self) -> &Path {
        &self.path
    }

    /// Return the actual platform-specific lock file path used for locking.
    ///
    /// - **Unix**: Same as `path()` (the file itself is locked via `flock`).
    /// - **Windows**: The `.lock` marker file (e.g., `foo.lock` for target `foo`).
    pub fn lock_file_path(&self) -> PathBuf {
        #[cfg(unix)]
        {
            self.path.clone()
        }
        #[cfg(windows)]
        {
            // Match the same logic as acquire_windows()
            if let Some(parent) = self.path.parent() {
                if let Some(stem) = self.path.file_stem() {
                    parent.join(format!("{}.lock", stem.to_string_lossy()))
                } else {
                    self.path.with_extension(".lock")
                }
            } else {
                self.path.with_extension(".lock")
            }
        }
        #[cfg(not(any(unix, windows)))]
        {
            self.path.clone()
        }
    }
}

impl Drop for FileLock {
    fn drop(&mut self) {
        // Release lock on drop (same logic as release but consuming self is not possible in Drop)
        #[cfg(unix)]
        {
            use std::os::fd::AsRawFd;
            if let Some(ref f) = self.file {
                let fd = f.as_raw_fd();
                let _ = unsafe { libc::flock(fd, libc::LOCK_UN) };
                tracing::debug!(
                    path = %self.path.display(),
                    "File lock auto-released on drop (Unix)"
                );
            }
        }
        #[cfg(windows)]
        {
            let lock_path = self.lock_file_path();
            let _ = std::fs::remove_file(&lock_path);
            tracing::debug!(
                path = %self.path.display(),
                lock_file = %lock_path.display(),
                "Lock file auto-removed on drop (Windows)"
            );
        }
    }
}

impl std::fmt::Debug for FileLock {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("FileLock")
            .field("path", &self.path)
            .field("is_held", &self.is_held())
            .finish()
    }
}

// ---------------------------------------------------------------------------
// DownloadPathLock -- manages locks for multi-file downloads
// ---------------------------------------------------------------------------

/// Manages file locks for all files in a download directory.
///
/// Creates a `.aria2.lock` file in the output directory to indicate that an
/// aria2 download is active. This prevents multiple aria2 instances from
/// writing to the same output location simultaneously.
///
/// The lock is held for the lifetime of the `DownloadPathLock` struct and
/// is automatically released when it is dropped.
pub struct DownloadPathLock {
    base_lock: Option<FileLock>,
    dir: PathBuf,
}

impl DownloadPathLock {
    /// Acquire a download lock for the given output directory.
    ///
    /// Creates (or locks) a `.aria2.lock` file inside `output_dir`. If another
    /// aria2 instance already holds a lock on this directory, returns an error.
    ///
    /// # Arguments
    ///
    /// * `output_dir` - The directory where downloaded files will be written.
    ///
    /// # Errors
    ///
    /// Returns `Err` if:
    /// - The directory does not exist and cannot be created.
    /// - Another process holds the lock on `.aria2.lock` in this directory.
    pub fn acquire_for_download(output_dir: &Path) -> Result<Self, String> {
        // Ensure output directory exists
        if !output_dir.exists() {
            std::fs::create_dir_all(output_dir).map_err(|e| {
                format!(
                    "Failed to create output directory '{}': {}",
                    output_dir.display(),
                    e
                )
            })?;
        }

        let lock_path = output_dir.join(".aria2.lock");
        let base = FileLock::acquire(&lock_path)?;

        tracing::info!(
            dir = %output_dir.display(),
            lock = %lock_path.display(),
            "Download path lock acquired"
        );

        Ok(Self {
            base_lock: Some(base),
            dir: output_dir.to_path_buf(),
        })
    }

    /// Release the download lock early (also happens on drop).
    pub fn release(self) {
        if let Some(lock) = self.base_lock {
            lock.release();
            tracing::info!(dir = %self.dir.display(), "Download path lock released");
        }
    }

    /// Return the output directory this lock protects.
    pub fn dir(&self) -> &Path {
        &self.dir
    }

    /// Return the underlying FileLock if it exists.
    pub fn file_lock(&self) -> Option<&FileLock> {
        self.base_lock.as_ref()
    }

    /// Check whether the lock is currently held.
    pub fn is_held(&self) -> bool {
        self.base_lock.as_ref().is_some_and(|l| l.is_held())
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;

    /// Generate a unique suffix for test file names to avoid collisions.
    fn unique_suffix() -> String {
        format!(
            "_t{}_p{}",
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .map_or_else(|_| 0, |d| d.as_nanos()),
            std::process::id()
        )
    }

    #[test]
    fn test_file_lock_acquire_release() {
        let tmp_dir = std::env::temp_dir();
        let lock_path = tmp_dir.join(format!("aria2_test_lock{}.tmp", unique_suffix()));

        // Acquire lock should succeed
        let lock = FileLock::acquire(&lock_path).expect("First acquire should succeed");
        assert!(lock.is_held());
        assert_eq!(lock.path(), lock_path);

        // Explicit release
        lock.release();

        // After release, acquiring again should succeed (lock is freed)
        let lock2 = FileLock::acquire(&lock_path).expect("Re-acquire after release should succeed");
        assert!(lock2.is_held());

        // Cleanup
        let _ = std::fs::remove_file(&lock_path);
        #[cfg(windows)]
        let _ = std::fs::remove_file(lock_path.with_extension(".lock"));
    }

    #[test]
    #[cfg(unix)]
    fn test_file_lock_double_acquire_fails() {
        let tmp_dir = std::env::temp_dir();
        let lock_path = tmp_dir.join(format!("aria2_test_double_lock{}.tmp", unique_suffix()));

        // First acquire succeeds
        let lock1 = FileLock::acquire(&lock_path).expect("First acquire should succeed");

        // Second acquire on same path should FAIL (non-blocking flock returns EAGAIN)
        let result = FileLock::acquire(&lock_path);
        assert!(
            result.is_err(),
            "Second acquire on same path should fail, got: {:?}",
            result
        );

        // Drop first lock
        drop(lock1);

        // Now acquire should succeed again
        let lock3 = FileLock::acquire(&lock_path).expect("Acquire after drop should succeed");
        assert!(lock3.is_held());

        // Cleanup
        let _ = std::fs::remove_file(&lock_path);
    }

    #[test]
    fn test_download_path_lock_creates_marker() {
        let tmp_dir = std::env::temp_dir();
        let test_dir = tmp_dir.join(format!("aria2_test_dl_lock{}", unique_suffix()));

        // Ensure clean state
        let _ = std::fs::remove_dir_all(&test_dir);

        // Acquire download path lock
        let dl_lock =
            DownloadPathLock::acquire_for_download(&test_dir).expect("Acquire should succeed");

        // Verify lock marker file exists while lock is held (use platform-aware path)
        let lock_file = dl_lock.base_lock.as_ref().unwrap().lock_file_path();
        assert!(
            lock_file.exists(),
            "lock marker file should exist while lock is held"
        );
        assert!(dl_lock.is_held());
        assert_eq!(dl_lock.dir(), test_dir);

        // Verify directory was created
        assert!(test_dir.exists());

        // Release lock
        dl_lock.release();

        // After release, the lock file should be cleaned up (platform dependent)
        #[cfg(windows)]
        assert!(
            !lock_file.exists(),
            ".lock file should be removed after release on Windows"
        );

        // Cleanup
        let _ = std::fs::remove_dir_all(&test_dir);
    }

    #[test]
    fn test_download_path_lock_reacquire_after_drop() {
        let tmp_dir = std::env::temp_dir();
        let test_dir = tmp_dir.join(format!("aria2_test_reacquire{}", unique_suffix()));

        // Clean up
        let _ = std::fs::remove_dir_all(&test_dir);

        {
            // Scope 1: acquire and hold
            let _lock1 =
                DownloadPathLock::acquire_for_download(&test_dir).expect("First acquire OK");
            let lock_path = _lock1.file_lock().unwrap().lock_file_path();
            assert!(lock_path.exists());
        }
        // _lock1 dropped here -> lock released

        {
            // Scope 2: re-acquire should succeed
            let _lock2 = DownloadPathLock::acquire_for_download(&test_dir)
                .expect("Re-acquire after drop OK");
            assert!(_lock2.is_held());
        }

        // Cleanup
        let _ = std::fs::remove_dir_all(&test_dir);
    }
}