frakt 0.1.0

Ergonomic platform HTTP client bindings for Rust
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
//! Background download implementations for reqwest backend

use crate::{Error, Result};
use futures_util::StreamExt;
use std::path::PathBuf;
use url::Url;

/// Generate a unique session identifier
fn generate_session_id(prefix: &str) -> String {
    format!(
        "frakt-{}-{}-{}",
        prefix,
        std::process::id(),
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs()
    )
}

/// Create and return the state directory path
fn ensure_state_dir() -> Result<PathBuf> {
    let state_dir = std::env::temp_dir().join("frakt");
    std::fs::create_dir_all(&state_dir)
        .map_err(|e| Error::Internal(format!("Failed to create state directory: {}", e)))?;
    Ok(state_dir)
}

/// Execute background download on Unix using double-fork
#[cfg(unix)]
pub async fn execute_unix_background_download(
    client: &reqwest::Client,
    url: Url,
    file_path: PathBuf,
    session_identifier: Option<String>,
    progress_callback: Option<Box<dyn Fn(u64, Option<u64>) + Send + Sync + 'static>>,
) -> Result<crate::client::download::DownloadResponse> {
    let session_id = session_identifier.unwrap_or_else(|| generate_session_id("unix"));
    let state_dir = ensure_state_dir()?;
    let state_file = state_dir.join(format!("{}.state", session_id));
    let has_progress_callback = progress_callback.is_some();

    // Clone client for daemon process
    let client = client.clone();

    // Double-fork to create a truly detached process
    unsafe {
        let pid = libc::fork();
        if pid < 0 {
            return Err(Error::Internal("First fork failed".to_string()));
        } else if pid == 0 {
            // First child - create new session
            if libc::setsid() < 0 {
                std::process::exit(1);
            }

            // Second fork
            let pid2 = libc::fork();
            if pid2 < 0 {
                std::process::exit(1);
            } else if pid2 == 0 {
                // Second child - the daemon

                // Close all file descriptors
                for fd in 3..256 {
                    libc::close(fd);
                }

                // Redirect stdin/stdout/stderr to /dev/null
                let devnull = libc::open(b"/dev/null\0".as_ptr() as *const i8, libc::O_RDWR);
                if devnull >= 0 {
                    libc::dup2(devnull, 0);
                    libc::dup2(devnull, 1);
                    libc::dup2(devnull, 2);
                    if devnull > 2 {
                        libc::close(devnull);
                    }
                }

                // Run the download in the daemon process
                run_daemon_download(url, file_path, state_file, client, has_progress_callback);

                // Exit when download completes
                std::process::exit(0);
            } else {
                // First child exits immediately
                std::process::exit(0);
            }
        } else {
            // Parent process - wait for first child to exit
            let mut status = 0;
            libc::waitpid(pid, &mut status, 0);
        }
    }

    // Wait a moment for the download to start
    tokio::time::sleep(std::time::Duration::from_millis(100)).await;

    // Monitor the state file for completion and call progress callback if provided
    monitor_background_download_with_progress(state_file, file_path, progress_callback).await
}

/// Monitor state file for completion with progress callback support
async fn monitor_background_download_with_progress(
    state_file: PathBuf,
    file_path: PathBuf,
    progress_callback: Option<Box<dyn Fn(u64, Option<u64>) + Send + Sync + 'static>>,
) -> Result<crate::client::download::DownloadResponse> {
    let start_time = std::time::Instant::now();
    let timeout = std::time::Duration::from_secs(300); // 5 minute timeout

    loop {
        if start_time.elapsed() > timeout {
            return Err(Error::Internal("Background download timeout".to_string()));
        }

        if let Ok(state_content) = std::fs::read_to_string(&state_file) {
            let mut status = None;
            let mut bytes_downloaded = 0u64;
            let mut error_msg = None;

            for line in state_content.lines() {
                if let Some((key, value)) = line.split_once(':') {
                    match key {
                        "status" => status = Some(value.to_string()),
                        "bytes_downloaded" => {
                            bytes_downloaded = value.parse().unwrap_or(0);
                        }
                        "error" => error_msg = Some(value.to_string()),
                        _ => {}
                    }
                }
            }

            // Call progress callback if we have one and we're downloading
            if let (Some(callback), Some("downloading")) = (&progress_callback, status.as_deref()) {
                // For Unix daemon downloads, we don't know the total size easily,
                // so we'll pass None for total_bytes
                callback(bytes_downloaded, None);
            }

            match status.as_deref() {
                Some("completed") => {
                    // Call final progress callback if provided
                    if let Some(callback) = &progress_callback {
                        callback(bytes_downloaded, Some(bytes_downloaded));
                    }

                    // Clean up state file
                    let _ = std::fs::remove_file(&state_file);

                    return Ok(crate::client::download::DownloadResponse {
                        file_path,
                        bytes_downloaded,
                    });
                }
                Some("failed") => {
                    // Clean up state file
                    let _ = std::fs::remove_file(&state_file);

                    return Err(Error::Internal(
                        error_msg.unwrap_or_else(|| "Download failed".to_string()),
                    ));
                }
                _ => {
                    // Still downloading or unknown status
                }
            }
        }

        tokio::time::sleep(std::time::Duration::from_millis(500)).await;
    }
}

/// Execute background download on non-Unix platforms using resumable downloads
#[cfg(not(unix))]
pub async fn execute_resumable_background_download(
    client: &reqwest::Client,
    url: Url,
    file_path: PathBuf,
    session_identifier: Option<String>,
    progress_callback: Option<Box<dyn Fn(u64, Option<u64>) + Send + Sync + 'static>>,
) -> Result<crate::client::download::DownloadResponse> {
    let session_id = session_identifier.unwrap_or_else(|| generate_session_id("resumable"));
    let state_dir = ensure_state_dir()?;
    let state_file = state_dir.join(format!("{}.state", session_id));

    // Check if we're resuming an existing download
    let mut bytes_downloaded = 0u64;
    if file_path.exists() {
        bytes_downloaded = std::fs::metadata(&file_path)
            .map_err(|e| Error::Internal(format!("Failed to read file metadata: {}", e)))?
            .len();
    }

    // Write initial state
    let state_content = format!(
        "status:downloading\nbytes_downloaded:{}\nlast_update:{}\n",
        bytes_downloaded,
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs()
    );
    std::fs::write(&state_file, state_content)
        .map_err(|e| Error::Internal(format!("Failed to write state file: {}", e)))?;

    // Perform the download with retry logic
    let mut retry_count = 0;
    const MAX_RETRIES: u32 = 5;
    const RETRY_DELAY: std::time::Duration = std::time::Duration::from_secs(2);

    loop {
        match try_download_with_resume(
            client,
            &url,
            &file_path,
            bytes_downloaded,
            progress_callback.as_deref(),
        )
        .await
        {
            Ok(total_bytes) => {
                // Download completed successfully
                let final_state = format!(
                    "status:completed\nbytes_downloaded:{}\nlast_update:{}\n",
                    total_bytes,
                    std::time::SystemTime::now()
                        .duration_since(std::time::UNIX_EPOCH)
                        .unwrap_or_default()
                        .as_secs()
                );
                let _ = std::fs::write(&state_file, final_state);

                return Ok(crate::client::download::DownloadResponse {
                    file_path,
                    bytes_downloaded: total_bytes,
                });
            }
            Err(e) => {
                retry_count += 1;
                if retry_count >= MAX_RETRIES {
                    let failed_state = format!(
                        "status:failed\nerror:{}\nlast_update:{}\n",
                        e,
                        std::time::SystemTime::now()
                            .duration_since(std::time::UNIX_EPOCH)
                            .unwrap_or_default()
                            .as_secs()
                    );
                    let _ = std::fs::write(&state_file, failed_state);
                    return Err(e);
                }

                // Update bytes downloaded for next retry
                if file_path.exists() {
                    bytes_downloaded = std::fs::metadata(&file_path)
                        .map_err(|e| {
                            Error::Internal(format!("Failed to read file metadata: {}", e))
                        })?
                        .len();
                }

                // Exponential backoff
                let delay = RETRY_DELAY * 2_u32.pow(retry_count - 1);
                tokio::time::sleep(delay).await;
            }
        }
    }
}

/// Run download in daemon process using reqwest
fn run_daemon_download(
    url: Url,
    file_path: PathBuf,
    state_file: PathBuf,
    client: reqwest::Client,
    has_progress_callback: bool,
) {
    // Helper function to write state
    let write_state = |status: &str, bytes_downloaded: u64, error: Option<&str>| {
        let mut content = format!(
            "status:{}\nbytes_downloaded:{}\nlast_update:{}\n",
            status,
            bytes_downloaded,
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap_or_default()
                .as_secs()
        );
        if let Some(err) = error {
            content.push_str(&format!("error:{}\n", err));
        }
        let _ = std::fs::write(&state_file, content);
    };

    // Create a new tokio runtime in the forked process
    let runtime = match tokio::runtime::Builder::new_current_thread()
        .enable_all()
        .build()
    {
        Ok(rt) => rt,
        Err(e) => {
            write_state(
                "failed",
                0,
                Some(&format!("Failed to create runtime: {}", e)),
            );
            return;
        }
    };

    // Run the download
    runtime.block_on(async {
        if let Err(e) = daemon_download_async(
            url,
            file_path,
            &state_file,
            client,
            has_progress_callback,
            write_state,
        )
        .await
        {
            write_state("failed", 0, Some(&format!("Download failed: {}", e)));
        }
    });
}

/// Attempt download with resume capability
#[cfg(not(unix))]
async fn try_download_with_resume(
    client: &reqwest::Client,
    url: &Url,
    file_path: &std::path::Path,
    start_byte: u64,
    progress_callback: Option<&(dyn Fn(u64, Option<u64>) + Send + Sync)>,
) -> Result<u64> {
    // Create request with Range header for resume if needed
    let mut request_builder = client.get(url.clone());
    if start_byte > 0 {
        request_builder = request_builder.header("Range", format!("bytes={}-", start_byte));
    }

    // Send the request
    let response = request_builder
        .send()
        .await
        .map_err(|e| Error::Internal(format!("Request failed: {}", e)))?;

    // Check status
    if !response.status().is_success() && response.status() != 206 {
        if response.status() == 416 && start_byte > 0 {
            // Range not satisfiable - file is already complete
            return Ok(start_byte);
        }
        return Err(Error::Internal(format!(
            "HTTP error: {}",
            response.status()
        )));
    }

    // Get total size if available
    let total_size = response.content_length().map(|len| len + start_byte);

    // Open file for writing (append if resuming)
    let mut file = std::fs::OpenOptions::new()
        .create(true)
        .append(start_byte > 0)
        .write(true)
        .truncate(start_byte == 0)
        .open(file_path)
        .map_err(|e| Error::Internal(format!("Failed to open file: {}", e)))?;

    // Stream the response body
    let mut stream = response.bytes_stream();
    let mut bytes_downloaded = start_byte;

    while let Some(chunk_result) = stream.next().await {
        let chunk = chunk_result.map_err(|e| Error::Internal(format!("Stream error: {}", e)))?;

        // Write chunk to file
        std::io::Write::write_all(&mut file, &chunk)
            .map_err(|e| Error::Internal(format!("Failed to write to file: {}", e)))?;

        bytes_downloaded += chunk.len() as u64;

        // Call progress callback if provided
        if let Some(callback) = progress_callback {
            callback(bytes_downloaded, total_size);
        }
    }

    // Ensure file is flushed
    std::io::Write::flush(&mut file)
        .map_err(|e| Error::Internal(format!("Failed to flush file: {}", e)))?;

    Ok(bytes_downloaded)
}

/// Async download logic for daemon process
async fn daemon_download_async(
    url: Url,
    file_path: PathBuf,
    _state_file: &std::path::Path,
    client: reqwest::Client,
    has_progress_callback: bool,
    write_state: impl Fn(&str, u64, Option<&str>),
) -> std::result::Result<(), String> {
    // Check if file already exists (for resume)
    let initial_size = if file_path.exists() {
        std::fs::metadata(&file_path)
            .map_err(|e| format!("Failed to check existing file: {}", e))?
            .len()
    } else {
        0
    };

    // Create request with Range header for resume if needed
    let mut request_builder = client.get(url);
    if initial_size > 0 {
        request_builder = request_builder.header("Range", format!("bytes={}-", initial_size));
    }

    // Send the request
    let response = request_builder
        .send()
        .await
        .map_err(|e| format!("Request failed: {}", e))?;

    // Check status
    if !response.status().is_success() && response.status() != 206 {
        if response.status() == 416 && initial_size > 0 {
            // Range not satisfiable - file is already complete
            write_state("completed", initial_size, None);
            return Ok(());
        }
        return Err(format!("HTTP error: {}", response.status()));
    }

    // Open file for writing (append if resuming)
    let mut file = std::fs::OpenOptions::new()
        .create(true)
        .append(initial_size > 0)
        .write(true)
        .truncate(initial_size == 0)
        .open(&file_path)
        .map_err(|e| format!("Failed to open file: {}", e))?;

    // Stream the response body
    let mut stream = response.bytes_stream();
    let mut bytes_downloaded = initial_size;
    let mut last_progress_update = std::time::Instant::now();

    write_state("downloading", bytes_downloaded, None);

    while let Some(chunk_result) = stream.next().await {
        let chunk = chunk_result.map_err(|e| format!("Stream error: {}", e))?;

        // Write chunk to file
        std::io::Write::write_all(&mut file, &chunk)
            .map_err(|e| format!("Failed to write to file: {}", e))?;

        bytes_downloaded += chunk.len() as u64;

        // Update progress periodically
        if has_progress_callback
            && last_progress_update.elapsed() > std::time::Duration::from_millis(500)
        {
            write_state("downloading", bytes_downloaded, None);
            last_progress_update = std::time::Instant::now();
        }
    }

    // Ensure file is flushed
    std::io::Write::flush(&mut file).map_err(|e| format!("Failed to flush file: {}", e))?;

    // Mark as completed
    write_state("completed", bytes_downloaded, None);
    Ok(())
}