Skip to main content

pacsea/sources/
pkgbuild.rs

1//! PKGBUILD fetching with rate limiting and caching.
2
3use crate::integrations::arch_toolkit::ToolkitContext;
4use crate::logic::files::get_pkgbuild_from_cache;
5use crate::state::{PackageItem, Source};
6use crate::util::percent_encode;
7use std::sync::Mutex;
8use std::time::{Duration, Instant};
9
10/// Result type alias for PKGBUILD fetching operations.
11type Result<T> = super::Result<T>;
12
13/// Rate limiter for PKGBUILD requests to avoid overwhelming AUR servers.
14///
15/// Tracks the timestamp of the last PKGBUILD request to enforce minimum intervals.
16static PKGBUILD_RATE_LIMITER: Mutex<Option<Instant>> = Mutex::new(None);
17/// Minimum interval between PKGBUILD requests in milliseconds.
18///
19/// Reduced from 500ms to 200ms for faster preview operations.
20const PKGBUILD_MIN_INTERVAL_MS: u64 = 200;
21/// Extra curl flags for PKGBUILD HTTP fetches (override base connect timeout; cap total time).
22const PKGBUILD_CURL_EXTRA: &[&str] = &["--connect-timeout", "8", "--max-time", "10"];
23
24/// What: Fetch PKGBUILD content for a package from AUR or official Git packaging repos.
25///
26/// Inputs:
27/// - `item`: Package whose PKGBUILD should be retrieved.
28///
29/// Output:
30/// - `Ok(String)` with PKGBUILD text when available; `Err` on network or lookup failure.
31///
32/// # Errors
33/// - Returns `Err` when network request fails (curl execution error)
34/// - Returns `Err` when PKGBUILD cannot be fetched from AUR or official GitLab repositories
35/// - Returns `Err` when rate limiting mutex is poisoned
36/// - Returns `Err` when task spawn fails
37///
38/// # Panics
39/// - Panics if the rate limiting mutex is poisoned
40///
41/// Details:
42/// - First tries offline methods (yay/paru cache) for fast loading.
43/// - Then tries network with rate limiting and timeout (10s).
44/// - Uses curl with timeout to prevent hanging on slow servers.
45/// - Passes a short `--connect-timeout` after the base `curl_args` flags so it overrides the
46///   default 30s connect wait (important when official GitLab is unreachable).
47pub async fn fetch_pkgbuild_fast(item: &PackageItem) -> Result<String> {
48    let context = ToolkitContext::new()?;
49    fetch_pkgbuild_fast_with_context(&context, item).await
50}
51
52/// What: Fetch PKGBUILD content with a caller-owned shared toolkit context.
53///
54/// Inputs:
55/// - `context`: Runtime integration context.
56/// - `item`: Package whose PKGBUILD should be retrieved.
57///
58/// Output:
59/// - Cached or bounded network PKGBUILD text.
60///
61/// Details:
62/// - Pacsea keeps helper caches and official GitLab fallback; arch-toolkit owns the bounded AUR path.
63pub async fn fetch_pkgbuild_fast_with_context(
64    context: &ToolkitContext,
65    item: &PackageItem,
66) -> Result<String> {
67    let name = item.name.clone();
68
69    // 1. Try offline methods first (yay/paru cache) - this is fast!
70    if let Some(cached) = tokio::task::spawn_blocking({
71        let name = name.clone();
72        move || get_pkgbuild_from_cache(&name)
73    })
74    .await?
75    {
76        tracing::debug!("Using cached PKGBUILD for {} (offline)", name);
77        return Ok(cached);
78    }
79
80    // 2. Rate limiting: ensure minimum interval between requests
81    let delay = {
82        let mut last_request = PKGBUILD_RATE_LIMITER
83            .lock()
84            .expect("PKGBUILD rate limiter mutex poisoned");
85        if let Some(last) = *last_request {
86            let elapsed = last.elapsed();
87            if elapsed < Duration::from_millis(PKGBUILD_MIN_INTERVAL_MS) {
88                let delay = Duration::from_millis(PKGBUILD_MIN_INTERVAL_MS)
89                    .checked_sub(elapsed)
90                    .expect("elapsed should be less than PKGBUILD_MIN_INTERVAL_MS");
91                tracing::debug!(
92                    "Rate limiting PKGBUILD request for {}: waiting {:?}",
93                    name,
94                    delay
95                );
96                // Drop the guard before await
97                *last_request = Some(Instant::now());
98                Some(delay)
99            } else {
100                *last_request = Some(Instant::now());
101                None
102            }
103        } else {
104            *last_request = Some(Instant::now());
105            None
106        }
107    };
108    if let Some(delay) = delay {
109        tokio::time::sleep(delay).await;
110    }
111
112    // 3. Fetch from network with timeout
113    match &item.source {
114        Source::Aur => crate::integrations::arch_toolkit::aur::pkgbuild(context, &name)
115            .await
116            .map_err(Into::into),
117        Source::Official { .. } => {
118            let url_main = format!(
119                "https://gitlab.archlinux.org/archlinux/packaging/packages/{}/-/raw/main/PKGBUILD",
120                percent_encode(&name)
121            );
122            let main_result = tokio::task::spawn_blocking({
123                let u = url_main.clone();
124                move || crate::util::curl::curl_text_with_args(&u, PKGBUILD_CURL_EXTRA)
125            })
126            .await;
127            if let Ok(Ok(txt)) = main_result {
128                return Ok(txt);
129            }
130            let url_master = format!(
131                "https://gitlab.archlinux.org/archlinux/packaging/packages/{}/-/raw/master/PKGBUILD",
132                percent_encode(&name)
133            );
134            let txt = tokio::task::spawn_blocking({
135                let u = url_master;
136                move || crate::util::curl::curl_text_with_args(&u, PKGBUILD_CURL_EXTRA)
137            })
138            .await??;
139            Ok(txt)
140        }
141    }
142}
143
144#[cfg(not(target_os = "windows"))]
145#[cfg(test)]
146mod tests {
147    use super::*;
148
149    #[tokio::test]
150    #[ignore = "Only run when explicitly mentioned"]
151    #[allow(clippy::await_holding_lock)]
152    async fn pkgbuild_fetches_aur_via_curl_text() {
153        let _guard = crate::sources::test_mutex()
154            .lock()
155            .expect("Test mutex poisoned");
156        // Shim PATH with fake curl
157        let old_path = std::env::var("PATH").unwrap_or_default();
158        let mut root = std::env::temp_dir();
159        root.push(format!(
160            "pacsea_fake_curl_pkgbuild_{}_{}",
161            std::process::id(),
162            std::time::SystemTime::now()
163                .duration_since(std::time::UNIX_EPOCH)
164                .expect("System time is before UNIX epoch")
165                .as_nanos()
166        ));
167        std::fs::create_dir_all(&root).expect("Failed to create test root directory");
168        let mut bin = root.clone();
169        bin.push("bin");
170        std::fs::create_dir_all(&bin).expect("Failed to create test bin directory");
171        let mut curl = bin.clone();
172        curl.push("curl");
173        let script = "#!/bin/sh\necho 'pkgver=1'\n";
174        std::fs::write(&curl, script.as_bytes()).expect("Failed to write test curl script");
175        #[cfg(unix)]
176        {
177            use std::os::unix::fs::PermissionsExt;
178            let mut perm = std::fs::metadata(&curl)
179                .expect("Failed to read test curl script metadata")
180                .permissions();
181            perm.set_mode(0o755);
182            std::fs::set_permissions(&curl, perm)
183                .expect("Failed to set test curl script permissions");
184        }
185        let new_path = format!("{}:{old_path}", bin.to_string_lossy());
186        unsafe { std::env::set_var("PATH", &new_path) };
187
188        let item = PackageItem {
189            name: "yay-bin".into(),
190            version: String::new(),
191            description: String::new(),
192            source: Source::Aur,
193            popularity: None,
194            out_of_date: None,
195            orphaned: false,
196        };
197        let txt = super::fetch_pkgbuild_fast(&item)
198            .await
199            .expect("Failed to fetch PKGBUILD in test");
200        assert!(txt.contains("pkgver=1"));
201
202        unsafe { std::env::set_var("PATH", &old_path) };
203        let _ = std::fs::remove_dir_all(&root);
204    }
205
206    #[test]
207    #[allow(clippy::await_holding_lock)]
208    fn pkgbuild_fetches_official_main_then_master() {
209        let _guard = crate::global_test_mutex_lock();
210        let old_path = std::env::var("PATH").unwrap_or_default();
211        let mut root = std::env::temp_dir();
212        root.push(format!(
213            "pacsea_fake_curl_pkgbuild_official_{}_{}",
214            std::process::id(),
215            std::time::SystemTime::now()
216                .duration_since(std::time::UNIX_EPOCH)
217                .expect("System time is before UNIX epoch")
218                .as_nanos()
219        ));
220        std::fs::create_dir_all(&root).expect("Failed to create test root directory");
221        let mut bin = root.clone();
222        bin.push("bin");
223        std::fs::create_dir_all(&bin).expect("Failed to create test bin directory");
224        let mut curl = bin.clone();
225        curl.push("curl");
226        // Fail when URL contains '/-/raw/main/' and succeed when '/-/raw/master/'
227        // curl_args creates defaults like:
228        // ["-sSLf", "--connect-timeout", "30", "--max-time", "90", "--max-filesize", "10485760", ...]
229        // Get the last argument by looping through all arguments
230        // Use printf instead of echo to avoid trailing newline that confuses the HTTP header parser
231        let script = "#!/bin/sh\nfor arg; do :; done\nurl=\"$arg\"\nif echo \"$url\" | grep -q '/-/raw/main/'; then exit 22; fi\nprintf 'pkgrel=2'\n";
232        std::fs::write(&curl, script.as_bytes()).expect("Failed to write test curl script");
233        #[cfg(unix)]
234        {
235            use std::os::unix::fs::PermissionsExt;
236            let mut perm = std::fs::metadata(&curl)
237                .expect("Failed to read test curl script metadata")
238                .permissions();
239            perm.set_mode(0o755);
240            std::fs::set_permissions(&curl, perm)
241                .expect("Failed to set test curl script permissions");
242        }
243
244        // Create fake paru and yay that fail (to prevent get_pkgbuild_from_cache from fetching real data)
245        let mut paru = bin.clone();
246        paru.push("paru");
247        std::fs::write(&paru, b"#!/bin/sh\nexit 1\n").expect("Failed to write test paru script");
248        #[cfg(unix)]
249        {
250            use std::os::unix::fs::PermissionsExt;
251            let mut perm = std::fs::metadata(&paru)
252                .expect("Failed to read test paru script metadata")
253                .permissions();
254            perm.set_mode(0o755);
255            std::fs::set_permissions(&paru, perm)
256                .expect("Failed to set test paru script permissions");
257        }
258
259        let mut yay = bin.clone();
260        yay.push("yay");
261        std::fs::write(&yay, b"#!/bin/sh\nexit 1\n").expect("Failed to write test yay script");
262        #[cfg(unix)]
263        {
264            use std::os::unix::fs::PermissionsExt;
265            let mut perm = std::fs::metadata(&yay)
266                .expect("Failed to read test yay script metadata")
267                .permissions();
268            perm.set_mode(0o755);
269            std::fs::set_permissions(&yay, perm)
270                .expect("Failed to set test yay script permissions");
271        }
272        let new_path = format!("{}:{old_path}", bin.to_string_lossy());
273        unsafe { std::env::set_var("PATH", &new_path) };
274        // Enable curl PATH lookup override so our fake curl is used instead of /usr/bin/curl
275        unsafe { std::env::set_var("PACSEA_CURL_PATH", "1") };
276
277        // Set HOME to empty directory to avoid finding cached PKGBUILDs
278        let old_home = std::env::var("HOME").unwrap_or_default();
279        unsafe { std::env::set_var("HOME", root.to_string_lossy().as_ref()) };
280
281        // Create a new tokio runtime AFTER setting PATH and HOME so worker threads inherit them
282        let rt = tokio::runtime::Runtime::new().expect("Failed to create tokio runtime for test");
283        let txt = rt.block_on(async {
284            let item = PackageItem {
285                name: "ripgrep".into(),
286                version: String::new(),
287                description: String::new(),
288                source: Source::Official {
289                    repo: "extra".into(),
290                    arch: "x86_64".into(),
291                },
292                popularity: None,
293                out_of_date: None,
294                orphaned: false,
295            };
296            super::fetch_pkgbuild_fast(&item)
297                .await
298                .expect("Failed to fetch PKGBUILD in test")
299        });
300
301        assert!(txt.contains("pkgrel=2"));
302
303        unsafe { std::env::set_var("PATH", &old_path) };
304        unsafe { std::env::set_var("HOME", &old_home) };
305        unsafe { std::env::remove_var("PACSEA_CURL_PATH") };
306        let _ = std::fs::remove_dir_all(&root);
307    }
308}