eoka 0.14.2

Stealth browser automation for Rust. Puppeteer/Playwright alternative with anti-bot bypass.
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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
//! Persistent, verified patch cache and atomic publication (non-macOS).

use std::fs;
#[cfg(not(target_os = "macos"))]
use std::fs::{File, OpenOptions};
use std::path::{Path, PathBuf};

#[cfg(not(target_os = "macos"))]
use memmap2::{Mmap, MmapMut};

#[cfg(not(target_os = "macos"))]
use super::sections::{apply_patches, count_patchable_in_file};
use super::PATCHER_SCHEMA_VERSION;
#[cfg(not(target_os = "macos"))]
use super::{random_string, ChromePatcher};
#[cfg(target_os = "linux")]
use crate::browser::discovery::{is_elf, resolve_to_elf};
use crate::error::{Error, Result};

/// Manifest file inside each published cache entry.
#[cfg(not(target_os = "macos"))]
const MANIFEST_NAME: &str = "eoka-patch.manifest";

/// Staging and trash leftovers from crashed processes older than this are
/// removed when another entry is published.
#[cfg(not(target_os = "macos"))]
const LEFTOVER_MAX_AGE: std::time::Duration = std::time::Duration::from_secs(60 * 60);

fn fnv1a(state: u64, bytes: &[u8]) -> u64 {
    bytes.iter().fold(state, |hash, byte| {
        (hash ^ u64::from(*byte)).wrapping_mul(0x0000_0100_0000_01b3)
    })
}

#[cfg(not(target_os = "macos"))]
/// Fast, stable 64-bit content hash used to detect truncated or corrupted
/// cache entries. Not a cryptographic digest: entries live in a private
/// directory owned by the current user.
fn content_hash(data: &[u8]) -> u64 {
    const MULTIPLIER: u64 = 0x9e37_79b9_7f4a_7c15;
    let mut hash = 0xcbf2_9ce4_8422_2325 ^ data.len() as u64;
    let mut words = data.chunks_exact(8);
    for word in &mut words {
        let word = u64::from_le_bytes(word.try_into().expect("8-byte chunk"));
        hash = (hash ^ word).wrapping_mul(MULTIPLIER).rotate_left(31);
    }
    for byte in words.remainder() {
        hash = (hash ^ u64::from(*byte)).wrapping_mul(MULTIPLIER);
    }
    hash ^ (hash >> 29)
}

fn path_bytes(path: &Path) -> Vec<u8> {
    #[cfg(unix)]
    {
        use std::os::unix::ffi::OsStrExt;
        path.as_os_str().as_bytes().to_vec()
    }
    #[cfg(not(unix))]
    {
        path.to_string_lossy().as_bytes().to_vec()
    }
}

#[cfg(not(target_os = "macos"))]
fn hex(bytes: &[u8]) -> String {
    bytes.iter().map(|byte| format!("{byte:02x}")).collect()
}

/// Identity of the source executable that a cache entry was derived from.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(super) struct SourceIdentity {
    pub(super) path: PathBuf,
    pub(super) len: u64,
    pub(super) mtime_ns: u128,
}

impl SourceIdentity {
    pub(super) fn of(path: &Path) -> Result<Self> {
        let path = fs::canonicalize(path)?;
        let metadata = fs::metadata(&path)?;
        let mtime_ns = metadata
            .modified()?
            .duration_since(std::time::UNIX_EPOCH)
            .map_or(0, |duration| duration.as_nanos());
        Ok(Self {
            path,
            len: metadata.len(),
            mtime_ns,
        })
    }

    /// Hex of the canonical source path, as stored in manifests.
    #[cfg(not(target_os = "macos"))]
    fn path_hex(&self) -> String {
        hex(&path_bytes(&self.path))
    }

    pub(super) fn cache_key(&self) -> String {
        let mut hash = fnv1a(0xcbf2_9ce4_8422_2325, &PATCHER_SCHEMA_VERSION.to_le_bytes());
        hash = fnv1a(hash, &path_bytes(&self.path));
        hash = fnv1a(hash, &[0]);
        hash = fnv1a(hash, &self.len.to_le_bytes());
        hash = fnv1a(hash, &self.mtime_ns.to_le_bytes());
        format!("chrome-{hash:016x}")
    }
}

/// Text manifest stored with every published entry and clean marker.
#[cfg(not(target_os = "macos"))]
#[derive(Debug, Clone, PartialEq, Eq)]
pub(super) struct Manifest {
    schema: u64,
    source: String,
    source_len: u64,
    source_mtime_ns: u128,
    patched_len: u64,
    patched_hash: u64,
    patches: usize,
}

#[cfg(not(target_os = "macos"))]
impl Manifest {
    pub(super) fn for_source(identity: &SourceIdentity, patched_hash: u64, patches: usize) -> Self {
        Self {
            schema: PATCHER_SCHEMA_VERSION,
            source: identity.path_hex(),
            source_len: identity.len,
            source_mtime_ns: identity.mtime_ns,
            patched_len: identity.len,
            patched_hash,
            patches,
        }
    }

    pub(super) fn render(&self) -> String {
        format!(
            "schema={}\nsource={}\nsource_len={}\nsource_mtime_ns={}\npatched_len={}\npatched_hash={:016x}\npatches={}\n",
            self.schema,
            self.source,
            self.source_len,
            self.source_mtime_ns,
            self.patched_len,
            self.patched_hash,
            self.patches
        )
    }

    pub(super) fn parse(text: &str) -> Option<Self> {
        let mut fields = std::collections::HashMap::new();
        for line in text.lines() {
            let (key, value) = line.split_once('=')?;
            if fields.insert(key, value).is_some() {
                return None;
            }
        }
        if fields.len() != 7 {
            return None;
        }
        Some(Self {
            schema: fields.get("schema")?.parse().ok()?,
            source: fields.get("source")?.to_string(),
            source_len: fields.get("source_len")?.parse().ok()?,
            source_mtime_ns: fields.get("source_mtime_ns")?.parse().ok()?,
            patched_len: fields.get("patched_len")?.parse().ok()?,
            patched_hash: u64::from_str_radix(fields.get("patched_hash")?, 16).ok()?,
            patches: fields.get("patches")?.parse().ok()?,
        })
    }

    fn read(path: &Path) -> Option<Self> {
        let metadata = fs::symlink_metadata(path).ok()?;
        if !metadata.is_file() || metadata.len() > 64 * 1024 {
            return None;
        }
        Self::parse(&fs::read_to_string(path).ok()?)
    }

    pub(super) fn describes(&self, identity: &SourceIdentity) -> bool {
        self.schema == PATCHER_SCHEMA_VERSION
            && self.source == identity.path_hex()
            && self.source_len == identity.len
            && self.source_mtime_ns == identity.mtime_ns
    }
}

/// Persistent cache root from `XDG_CACHE_HOME` (absolute) or `HOME`.
/// Pure so tests never consult or mutate the real environment.
#[cfg(all(unix, not(target_os = "macos")))]
pub(super) fn persistent_cache_root_from(
    xdg_cache_home: Option<std::ffi::OsString>,
    home: Option<std::ffi::OsString>,
) -> Option<PathBuf> {
    let base = xdg_cache_home
        .map(PathBuf::from)
        .filter(|path| path.is_absolute())
        .or_else(|| {
            home.map(PathBuf::from)
                .filter(|path| path.is_absolute())
                .map(|home| home.join(".cache"))
        })?;
    Some(base.join("eoka").join("patched-chrome"))
}

/// The persistent cache root, or `None` where patching is per-runtime only
/// (macOS bundles, Windows) or no home directory is known.
pub(super) fn persistent_cache_root() -> Option<PathBuf> {
    #[cfg(all(unix, not(target_os = "macos")))]
    {
        persistent_cache_root_from(std::env::var_os("XDG_CACHE_HOME"), std::env::var_os("HOME"))
    }
    #[cfg(not(all(unix, not(target_os = "macos"))))]
    {
        None
    }
}

/// Create `path` (and missing parents) private to the current user.
pub(super) fn create_cache_root(path: &Path) -> Result<()> {
    #[cfg(unix)]
    {
        use std::os::unix::fs::DirBuilderExt;
        fs::DirBuilder::new()
            .recursive(true)
            .mode(0o700)
            .create(path)?;
    }
    #[cfg(not(unix))]
    fs::create_dir_all(path)?;
    Ok(())
}

#[cfg(unix)]
pub(super) fn validate_cache_root(path: &Path) -> Result<()> {
    use std::os::unix::fs::MetadataExt;

    // SAFETY: geteuid has no arguments or memory-safety preconditions.
    let uid = unsafe { libc::geteuid() };
    // Constructors retain the canonical root. Check every component without
    // following replacement symlinks: a private leaf is not protected when an
    // ancestor lets another user rename it.
    for ancestor in path.ancestors() {
        let metadata = fs::symlink_metadata(ancestor)?;
        if !metadata.is_dir()
            || (metadata.uid() != uid && metadata.uid() != 0)
            || (metadata.mode() & 0o022 != 0 && metadata.mode() & 0o1000 == 0)
        {
            return Err(Error::patching(
                "cache",
                format!("Untrusted Chrome cache ancestor: {}", ancestor.display()),
            ));
        }
    }
    Ok(())
}

#[cfg(unix)]
pub(super) fn validate_private_cache_directory(path: &Path) -> Result<()> {
    use std::os::unix::fs::MetadataExt;

    let metadata = fs::symlink_metadata(path)?;
    // SAFETY: geteuid has no arguments or memory-safety preconditions.
    let uid = unsafe { libc::geteuid() };
    if !metadata.is_dir() || metadata.uid() != uid || metadata.mode() & 0o077 != 0 {
        return Err(Error::patching(
            "cache",
            format!(
                "Chrome cache directory is not private and owned: {}",
                path.display()
            ),
        ));
    }
    Ok(())
}

#[cfg(unix)]
pub(super) fn create_private_cache_directory(path: &Path) -> Result<()> {
    use std::os::unix::fs::DirBuilderExt;

    let parent = path
        .parent()
        .ok_or_else(|| Error::patching("cache", "Chrome cache directory has no parent"))?;
    validate_cache_root(parent)?;
    match fs::DirBuilder::new().mode(0o700).create(path) {
        Ok(()) => {}
        Err(error) if error.kind() == std::io::ErrorKind::AlreadyExists => {}
        Err(error) => return Err(error.into()),
    }
    validate_private_cache_directory(path)
}

#[cfg(not(target_os = "macos"))]
impl ChromePatcher {
    pub(super) fn entry_directory(&self) -> &Path {
        self.patched_path
            .parent()
            .expect("patched path is always inside its entry directory")
    }

    pub(super) fn entry_name(&self) -> &std::ffi::OsStr {
        self.entry_directory()
            .file_name()
            .expect("entry directory always has a name")
    }

    fn clean_marker_path(&self) -> PathBuf {
        let mut name = self.entry_name().to_os_string();
        name.push(".clean");
        self.root.join(name)
    }

    /// A fresh name for a staging directory or temporary file in the root.
    fn staging_path(&self) -> PathBuf {
        self.root.join(format!(
            ".staging-{}-{}",
            std::process::id(),
            random_string(12)
        ))
    }

    /// Move an entry aside and delete it. Never delete in place: a running
    /// browser may still use the entry.
    fn discard_entry(&self, entry: &Path) -> std::io::Result<()> {
        let trash = self.root.join(format!(".trash-{}", random_string(12)));
        fs::rename(entry, &trash)?;
        let _ = fs::remove_dir_all(&trash);
        Ok(())
    }

    #[cfg(all(test, unix))]
    pub(super) fn cache_directory(&self) -> Result<&Path> {
        Ok(self.entry_directory())
    }

    pub(super) fn cached_path(&self) -> Option<PathBuf> {
        // The key was derived when the patcher was built; refuse to answer
        // for a source that has since changed.
        if SourceIdentity::of(&self.original_path).ok()? != self.identity {
            return None;
        }
        #[cfg(unix)]
        validate_cache_root(&self.root).ok()?;
        if Manifest::read(&self.clean_marker_path())
            .is_some_and(|manifest| manifest.describes(&self.identity) && manifest.patches == 0)
        {
            return Some(self.original_path.clone());
        }
        self.validate_entry(self.entry_directory())
            .ok()
            .map(|()| self.patched_path.clone())
    }

    /// Check a published (or staged) entry: private directory, manifest for
    /// this exact source, full file length and full content hash.
    fn validate_entry(&self, directory: &Path) -> Result<()> {
        #[cfg(unix)]
        validate_private_cache_directory(directory)?;
        let invalid = |reason: &str| {
            Error::patching(
                "cache",
                format!("Invalid cache entry {}: {reason}", directory.display()),
            )
        };
        let manifest =
            Manifest::read(&directory.join(MANIFEST_NAME)).ok_or_else(|| invalid("manifest"))?;
        if !manifest.describes(&self.identity) || manifest.patches == 0 {
            return Err(invalid("manifest describes a different source"));
        }
        let binary = directory.join(self.patched_path.file_name().expect("binary file name"));
        verify_binary(&binary, manifest.patched_len, manifest.patched_hash)
    }

    pub(super) fn patch_and_resolve(&self) -> Result<PathBuf> {
        tracing::info!(
            "Scanning Chrome binary for patching: {:?}",
            self.original_path
        );
        #[cfg(unix)]
        validate_cache_root(&self.root)?;
        if count_patchable_in_file(&self.original_path)? == 0 {
            // Nothing to hide: launch the original and remember the result.
            // Failing to record that only costs a rescan next launch.
            match self.write_clean_marker() {
                Ok(()) => self.prune_stale_entries(),
                Err(error) => tracing::warn!("Could not record clean Chrome scan: {}", error),
            }
            tracing::info!("Chrome binary has no automation markers; using it unpatched");
            return Ok(self.original_path.clone());
        }
        for _ in 0..2 {
            let staging = self.staging_path();
            #[cfg(unix)]
            create_private_cache_directory(&staging)?;
            #[cfg(not(unix))]
            fs::create_dir(&staging)?;
            let built = self.build_entry(&staging);
            let published = built.and_then(|patches| {
                tracing::info!("Patched {patches} occurrences; publishing cache entry");
                self.publish(&staging)
            });
            let _ = fs::remove_dir_all(&staging);
            if let Some(path) = published? {
                self.prune_stale_entries();
                return Ok(path);
            }
        }
        Err(Error::patching(
            "publish",
            "Chrome cache entry kept changing while it was being published",
        ))
    }

    /// Write the patched copy, its companions and manifest into `staging`.
    fn build_entry(&self, staging: &Path) -> Result<usize> {
        let binary = staging.join(self.patched_path.file_name().expect("binary file name"));
        let mut source = File::open(&self.original_path)?;
        let mut destination = OpenOptions::new()
            .read(true)
            .write(true)
            .create_new(true)
            .open(&binary)?;
        std::io::copy(&mut source, &mut destination)?;
        drop(source);
        if destination.metadata()?.len() != self.identity.len
            || SourceIdentity::of(&self.original_path)? != self.identity
        {
            return Err(Error::patching(
                "copy",
                "Chrome binary changed while it was being copied",
            ));
        }

        // SAFETY: `destination` is a regular file this process created with
        // create_new inside its own private staging directory; nothing else
        // maps or writes it while the map is alive.
        let mut map = unsafe { MmapMut::map_mut(&destination)? };
        let patches = apply_patches(&mut map)?;
        if patches == 0 {
            return Err(Error::patching(
                "patch",
                "Chrome binary changed between scanning and patching",
            ));
        }
        map.flush()?;
        let hash = content_hash(&map);
        drop(map);
        destination.sync_all()?;
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            destination.set_permissions(fs::Permissions::from_mode(0o755))?;
        }
        // Close the writable descriptor before anything can exec the file.
        drop(destination);
        verify_binary(&binary, self.identity.len, hash)?;

        self.link_companion_files(staging)?;
        let manifest = Manifest::for_source(&self.identity, hash, patches);
        write_synced(&staging.join(MANIFEST_NAME), manifest.render().as_bytes())?;
        sync_directory(staging);
        Ok(patches)
    }

    /// Atomically move `staging` into place. `Ok(None)` asks the caller to
    /// rebuild after a corrupt entry was moved aside.
    fn publish(&self, staging: &Path) -> Result<Option<PathBuf>> {
        let entry = self.entry_directory();
        match fs::rename(staging, entry) {
            Ok(()) => {
                sync_directory(&self.root);
                return Ok(Some(self.patched_path.clone()));
            }
            Err(error) if fs::symlink_metadata(entry).is_err() => return Err(error.into()),
            Err(_) => {}
        }
        // Another launch published first, or an entry is corrupt.
        if self.validate_entry(entry).is_ok() {
            return Ok(Some(self.patched_path.clone()));
        }
        // Only ever replace a private directory this user owns; anything else
        // (a symlink, a shared directory) is left untouched and fails closed.
        #[cfg(unix)]
        validate_private_cache_directory(entry)?;
        tracing::warn!("Replacing corrupt Chrome cache entry {:?}", entry);
        self.discard_entry(entry)?;
        Ok(None)
    }

    fn write_clean_marker(&self) -> Result<()> {
        let marker = self.clean_marker_path();
        let temporary = self.staging_path();
        let manifest = Manifest::for_source(&self.identity, 0, 0);
        let written = write_synced(&temporary, manifest.render().as_bytes()).and_then(|()| {
            match fs::symlink_metadata(&marker) {
                Ok(metadata) if !metadata.is_file() => Err(Error::patching(
                    "cache",
                    format!("Chrome cache marker is not a file: {}", marker.display()),
                )),
                _ => fs::rename(&temporary, &marker).map_err(Into::into),
            }
        });
        if written.is_err() {
            let _ = fs::remove_file(&temporary);
        }
        written?;
        sync_directory(&self.root);
        Ok(())
    }

    /// Remove entries and markers for this source with another key (an older
    /// Chrome build or schema), and leftovers from crashed processes.
    fn prune_stale_entries(&self) {
        let Ok(entries) = fs::read_dir(&self.root) else {
            return;
        };
        let own_entry = self.entry_name().to_os_string();
        let own_marker = self.clean_marker_path();
        let source = self.identity.path_hex();
        for entry in entries.flatten() {
            let path = entry.path();
            let Ok(metadata) = fs::symlink_metadata(&path) else {
                continue;
            };
            let name = entry.file_name();
            let name_text = name.to_string_lossy();
            let stale = if name_text.starts_with(".staging-") || name_text.starts_with(".trash-") {
                metadata
                    .modified()
                    .ok()
                    .and_then(|modified| modified.elapsed().ok())
                    .is_some_and(|age| age > LEFTOVER_MAX_AGE)
            } else if name_text.starts_with("chrome-") && name != own_entry && path != own_marker {
                let manifest = if metadata.is_dir() {
                    Manifest::read(&path.join(MANIFEST_NAME))
                } else {
                    Manifest::read(&path)
                };
                manifest.is_some_and(|manifest| manifest.source == source)
            } else {
                false
            };
            if !stale {
                continue;
            }
            if metadata.is_dir() {
                tracing::debug!("Pruning stale Chrome cache entry {:?}", path);
                let _ = self.discard_entry(&path);
            } else {
                let _ = fs::remove_file(&path);
            }
        }
    }

    /// Link (Unix) or copy (Windows) every sibling of the original binary into
    /// `directory`. Chromium resolves shared libraries and data files
    /// (icudtl.dat, V8 snapshots, locale paks) relative to its executable.
    fn link_companion_files(&self, directory: &Path) -> Result<()> {
        let source_directory = self.original_path.parent().ok_or_else(|| {
            Error::patching("dependencies", "Chrome binary has no parent directory")
        })?;
        let binary_name = self.original_path.file_name();
        for entry in fs::read_dir(source_directory)? {
            let entry = entry?;
            if Some(entry.file_name().as_os_str()) == binary_name {
                continue;
            }
            let destination = directory.join(entry.file_name());
            if fs::symlink_metadata(&destination).is_ok() {
                continue;
            }
            #[cfg(unix)]
            std::os::unix::fs::symlink(entry.path(), &destination)?;
            #[cfg(windows)]
            copy_dependency(&entry.path(), &destination)?;
        }
        Ok(())
    }
}

/// Verify the complete file: exact length and content hash.
#[cfg(not(target_os = "macos"))]
fn verify_binary(path: &Path, expected_len: u64, expected_hash: u64) -> Result<()> {
    let invalid = |reason: &str| {
        Error::patching(
            "verify",
            format!(
                "Patched Chrome {} failed verification: {reason}",
                path.display()
            ),
        )
    };
    let metadata = fs::symlink_metadata(path)?;
    if !metadata.is_file() {
        return Err(invalid("not a regular file"));
    }
    if metadata.len() != expected_len {
        return Err(invalid("length mismatch"));
    }
    if expected_len == 0 {
        return Err(invalid("empty file"));
    }
    let file = File::open(path)?;
    // SAFETY: read-only map of a file inside a private cache directory owned
    // by this user; published entries are never modified in place.
    let map = unsafe { Mmap::map(&file)? };
    if map.len() as u64 != expected_len || content_hash(&map) != expected_hash {
        return Err(invalid("content hash mismatch"));
    }
    Ok(())
}

#[cfg(not(target_os = "macos"))]
fn write_synced(path: &Path, contents: &[u8]) -> Result<()> {
    use std::io::Write;
    let mut file = OpenOptions::new().write(true).create_new(true).open(path)?;
    file.write_all(contents)?;
    file.sync_all()?;
    Ok(())
}

/// Best-effort fsync of a directory so a rename into it survives a crash.
#[cfg(not(target_os = "macos"))]
fn sync_directory(directory: &Path) {
    #[cfg(unix)]
    if let Ok(directory) = File::open(directory) {
        let _ = directory.sync_all();
    }
    #[cfg(not(unix))]
    let _ = directory;
}

#[cfg(target_os = "linux")]
pub(super) fn resolve_elf_for_patching(chrome_path: &Path) -> Result<PathBuf> {
    let resolved = resolve_to_elf(chrome_path, true);
    if !is_elf(&resolved) {
        return Err(Error::patching(
            "new",
            "Cannot resolve the requested Chrome wrapper to its ELF binary",
        ));
    }
    Ok(resolved)
}

#[cfg(windows)]
fn copy_dependency(source: &Path, destination: &Path) -> Result<()> {
    if source.is_dir() {
        fs::create_dir_all(destination)?;
        for entry in fs::read_dir(source)? {
            let entry = entry?;
            copy_dependency(&entry.path(), &destination.join(entry.file_name()))?;
        }
    } else {
        fs::copy(source, destination)?;
    }
    Ok(())
}