aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe/src/util/filesystem.rs
blob: 73f21d66330d43a5eeb5374852979dcd0a1e197d (plain)
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
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
//! ref: composer/src/Composer/Util/Filesystem.php

use shirabe_external_packages::composer::pcre::Preg;
use shirabe_external_packages::symfony::component::filesystem::exception::IOException;
use shirabe_external_packages::symfony::component::finder::Finder;
use shirabe_php_shim::{
    DIRECTORY_SEPARATOR, ErrorException, InvalidArgumentException, LogicException, PhpMixed,
    RuntimeException, UnexpectedValueException, array_pop, basename, chdir, clearstatcache,
    clearstatcache2, copy, count, dirname, end, error_get_last, explode, fclose, feof, file_exists,
    file_get_contents, file_put_contents, fileatime, filemtime, filesize, fopen, fread,
    function_exists, fwrite, implode, is_array, is_dir, is_file, is_link, is_readable, lstat,
    mkdir, react_promise_resolve, rename, rmdir, rtrim, sprintf, str_contains, str_repeat,
    str_replace, str_starts_with, strlen, strpos, strtolower, strtoupper, strtr, substr,
    substr_count, symlink, touch, unlink, usleep, var_export,
};

use crate::util::Platform;
use crate::util::ProcessExecutor;
use crate::util::Silencer;

#[derive(Debug)]
pub struct Filesystem {
    process_executor: Option<std::rc::Rc<std::cell::RefCell<ProcessExecutor>>>,
}

impl Filesystem {
    pub fn new(executor: Option<std::rc::Rc<std::cell::RefCell<ProcessExecutor>>>) -> Self {
        Self {
            process_executor: executor,
        }
    }

    pub fn remove(&mut self, file: &str) -> anyhow::Result<bool> {
        if is_dir(file) {
            return self.remove_directory(file);
        }

        if file_exists(file) {
            return self.unlink(file);
        }

        Ok(false)
    }

    /// Checks if a directory is empty
    pub fn is_dir_empty(&self, dir: &str) -> bool {
        let mut finder = Finder::create();
        finder
            .ignore_vcs(false)
            .ignore_dot_files(false)
            .depth(0)
            .r#in(dir);

        finder.len() == 0
    }

    pub fn empty_directory(
        &mut self,
        dir: &str,
        ensure_directory_exists: bool,
    ) -> anyhow::Result<()> {
        if is_link(dir) && file_exists(dir) {
            self.unlink(dir)?;
        }

        if ensure_directory_exists {
            self.ensure_directory_exists(dir)?;
        }

        if is_dir(dir) {
            let mut finder = Finder::create();
            finder
                .ignore_vcs(false)
                .ignore_dot_files(false)
                .depth(0)
                .r#in(dir);

            for path in finder.iter() {
                self.remove(&path.get_pathname())?;
            }
        }
        Ok(())
    }

    /// Recursively remove a directory
    ///
    /// Uses the process component if proc_open is enabled on the PHP
    /// installation.
    pub fn remove_directory(&mut self, directory: &str) -> anyhow::Result<bool> {
        let edge_case_result = self.remove_edge_cases(directory, true)?;
        if let Some(r) = edge_case_result {
            return Ok(r);
        }

        let cmd: Vec<String> = if Platform::is_windows() {
            vec![
                "rmdir".to_string(),
                "/S".to_string(),
                "/Q".to_string(),
                Platform::realpath(directory),
            ]
        } else {
            vec!["rm".to_string(), "-rf".to_string(), directory.to_string()]
        };

        let mut output = PhpMixed::Null;
        let result = self
            .get_process()
            .execute(
                PhpMixed::List(
                    cmd.iter()
                        .map(|s| Box::new(PhpMixed::String(s.clone())))
                        .collect(),
                ),
                Some(&mut output),
                (),
            )
            .map(|n| n == 0)
            .unwrap_or(false);

        // clear stat cache because external processes aren't tracked by the php stat cache
        clearstatcache2(false, "");

        if result && !is_dir(directory) {
            return Ok(true);
        }

        self.remove_directory_php(directory)
    }

    /// Recursively remove a directory asynchronously
    ///
    /// Uses the process component if proc_open is enabled on the PHP
    /// installation.
    pub async fn remove_directory_async(&mut self, directory: &str) -> anyhow::Result<bool> {
        let edge_case_result = self.remove_edge_cases(directory, true)?;
        if let Some(r) = edge_case_result {
            return Ok(r);
        }

        let cmd: Vec<String> = if Platform::is_windows() {
            vec![
                "rmdir".to_string(),
                "/S".to_string(),
                "/Q".to_string(),
                Platform::realpath(directory),
            ]
        } else {
            vec!["rm".to_string(), "-rf".to_string(), directory.to_string()]
        };

        let process = self
            .get_process()
            .execute_async(
                PhpMixed::List(
                    cmd.iter()
                        .map(|s| Box::new(PhpMixed::String(s.clone())))
                        .collect(),
                ),
                (),
            )
            .await?;

        // clear stat cache because external processes aren't tracked by the php stat cache
        clearstatcache2(false, "");

        if process.is_successful() && !is_dir(directory) {
            return Ok(true);
        }

        self.remove_directory_php(directory)
    }

    /// Returns null when no edge case was hit. Otherwise a bool whether removal was successful
    fn remove_edge_cases(
        &mut self,
        directory: &str,
        fallback_to_php: bool,
    ) -> anyhow::Result<Option<bool>> {
        if self.is_symlinked_directory(directory) {
            return Ok(Some(self.unlink_symlinked_directory(directory)?));
        }

        if self.is_junction(directory) {
            return Ok(Some(self.remove_junction(directory)?));
        }

        if is_link(directory) {
            return Ok(Some(unlink(directory)));
        }

        if !is_dir(directory) || !file_exists(directory) {
            return Ok(Some(true));
        }

        if Preg::is_match3("{^(?:[a-z]:)?[/\\\\]+$}i", directory, None).unwrap_or(false) {
            return Err(RuntimeException {
                message: format!("Aborting an attempted deletion of {}, this was probably not intended, if it is a real use case please report it.", directory),
                code: 0,
            }
            .into());
        }

        if !function_exists("proc_open") && fallback_to_php {
            return Ok(Some(self.remove_directory_php(directory)?));
        }

        Ok(None)
    }

    /// Recursively delete directory using PHP iterators.
    ///
    /// Uses a CHILD_FIRST RecursiveIteratorIterator to sort files
    /// before directories, creating a single non-recursive loop
    /// to delete files/directories in the correct order.
    pub fn remove_directory_php(&mut self, directory: &str) -> anyhow::Result<bool> {
        let edge_case_result = self.remove_edge_cases(directory, false)?;
        if let Some(r) = edge_case_result {
            return Ok(r);
        }

        // PHP: $it = new RecursiveDirectoryIterator($directory, RecursiveDirectoryIterator::SKIP_DOTS);
        // TODO(phase-b): PHP throws UnexpectedValueException on iterator creation failure;
        // shim signature does not yet model this. Skipping the retry/clearstatcache branch.
        let it =
            shirabe_php_shim::recursive_directory_iterator(directory, shirabe_php_shim::SKIP_DOTS);
        let ri = shirabe_php_shim::recursive_iterator_iterator(it, shirabe_php_shim::CHILD_FIRST);

        for file in &ri {
            if file.is_dir() {
                self.rmdir(&file.get_pathname())?;
            } else {
                self.unlink(&file.get_pathname())?;
            }
        }

        // release locks on the directory, see https://github.com/composer/composer/issues/9945
        drop(ri);

        self.rmdir(directory)
    }

    pub fn ensure_directory_exists(&mut self, directory: &str) -> anyhow::Result<()> {
        if !is_dir(directory) {
            if file_exists(directory) {
                return Err(RuntimeException {
                    message: format!("{} exists and is not a directory.", directory),
                    code: 0,
                }
                .into());
            }

            if is_link(directory) && !self.unlink_implementation(directory) {
                return Err(RuntimeException {
                    message: format!(
                        "Could not delete symbolic link {}: {}",
                        directory,
                        error_get_last()
                            .as_ref()
                            .and_then(|m| m.get("message"))
                            .and_then(|v| v.as_string())
                            .unwrap_or("")
                    ),
                    code: 0,
                }
                .into());
            }

            if !mkdir(directory, 0o777, true) {
                let e = RuntimeException {
                    message: format!(
                        "{} does not exist and could not be created: {}",
                        directory,
                        error_get_last()
                            .as_ref()
                            .and_then(|m| m.get("message"))
                            .and_then(|v| v.as_string())
                            .unwrap_or("")
                    ),
                    code: 0,
                };

                // in pathological cases with paths like path/to/broken-symlink/../foo is_dir will fail to detect path/to/foo
                // but normalizing the ../ away first makes it work so we attempt this just in case, and if it still fails we
                // report the initial error we had with the original path, and ignore the normalized path exception
                // see https://github.com/composer/composer/issues/11864
                let normalized = self.normalize_path(directory);
                if normalized != directory {
                    let _ = self.ensure_directory_exists(&normalized);
                    if is_dir(&normalized) {
                        return Ok(());
                    }
                }

                return Err(e.into());
            }
        }
        Ok(())
    }

    /// Attempts to unlink a file and in case of failure retries after 350ms on windows
    pub fn unlink(&self, path: &str) -> anyhow::Result<bool> {
        let mut unlinked = self.unlink_implementation(path);
        if !unlinked {
            // retry after a bit on windows since it tends to be touchy with mass removals
            if Platform::is_windows() {
                usleep(350000);
                unlinked = self.unlink_implementation(path);
            }

            if !unlinked {
                let error = error_get_last();
                let mut message = format!(
                    "Could not delete {}: {}",
                    path,
                    error
                        .as_ref()
                        .and_then(|m| m.get("message"))
                        .and_then(|v| v.as_string())
                        .unwrap_or("")
                );
                if Platform::is_windows() {
                    message.push_str("\nThis can be due to an antivirus or the Windows Search Indexer locking the file while they are analyzed");
                }

                return Err(RuntimeException { message, code: 0 }.into());
            }
        }

        Ok(true)
    }

    /// Attempts to rmdir a file and in case of failure retries after 350ms on windows
    pub fn rmdir(&self, path: &str) -> anyhow::Result<bool> {
        let mut deleted = rmdir(path);
        if !deleted {
            // retry after a bit on windows since it tends to be touchy with mass removals
            if Platform::is_windows() {
                usleep(350000);
                deleted = rmdir(path);
            }

            if !deleted {
                let error = error_get_last();
                let mut message = format!(
                    "Could not delete {}: {}",
                    path,
                    error
                        .as_ref()
                        .and_then(|m| m.get("message"))
                        .and_then(|v| v.as_string())
                        .unwrap_or("")
                );
                if Platform::is_windows() {
                    message.push_str("\nThis can be due to an antivirus or the Windows Search Indexer locking the file while they are analyzed");
                }

                return Err(RuntimeException { message, code: 0 }.into());
            }
        }

        Ok(true)
    }

    /// Copy then delete is a non-atomic version of rename.
    ///
    /// Some systems can't rename and also don't have proc_open,
    /// which requires this solution.
    pub fn copy_then_remove(&mut self, source: &str, target: &str) -> anyhow::Result<()> {
        self.copy(source, target)?;
        if !is_dir(source) {
            self.unlink(source)?;

            return Ok(());
        }

        self.remove_directory_php(source)?;
        Ok(())
    }

    /// Copies a file or directory from $source to $target.
    pub fn copy(&mut self, source: &str, target: &str) -> anyhow::Result<bool> {
        // refs https://github.com/composer/composer/issues/11864
        let target = self.normalize_path(target);

        if !is_dir(source) {
            let result =
                std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| copy(source, &target)));
            match result {
                Ok(b) => return Ok(b),
                Err(payload) => {
                    let e: ErrorException = match payload.downcast::<ErrorException>() {
                        Ok(boxed) => *boxed,
                        Err(_) => return Err(anyhow::anyhow!("Copy panicked")),
                    };

                    // if copy fails we attempt to copy it manually as this can help bypass issues with VirtualBox shared folders
                    // see https://github.com/composer/composer/issues/12057
                    if str_contains(&e.message, "Bad address") {
                        let source_handle = fopen(source, "r");
                        let target_handle = fopen(&target, "w");
                        if source_handle.is_null() || target_handle.is_null() {
                            return Err(e.into());
                        }
                        while !feof(source_handle.clone()) {
                            let chunk =
                                fread(source_handle.clone(), 1024 * 1024).unwrap_or_default();
                            // TODO(phase-b): PHP fwrite returns int|false; shim currently returns ();
                            // assume success here.
                            fwrite(target_handle.clone(), &chunk, chunk.len() as i64);
                        }
                        fclose(source_handle);
                        fclose(target_handle);

                        return Ok(true);
                    }
                    return Err(e.into());
                }
            }
        }

        let it =
            shirabe_php_shim::recursive_directory_iterator(source, shirabe_php_shim::SKIP_DOTS);
        let ri = shirabe_php_shim::recursive_iterator_iterator(it, shirabe_php_shim::SELF_FIRST);
        self.ensure_directory_exists(&target)?;

        let mut result = true;
        for file in &ri {
            let target_path = format!("{}{}{}", target, DIRECTORY_SEPARATOR, ri.get_sub_pathname());
            if file.is_dir() {
                self.ensure_directory_exists(&target_path)?;
            } else {
                result = result && copy(&file.get_pathname(), &target_path);
            }
        }

        Ok(result)
    }

    pub fn rename(&mut self, source: &str, target: &str) -> anyhow::Result<()> {
        if rename(source, target) {
            return Ok(());
        }

        if !function_exists("proc_open") {
            return self.copy_then_remove(source, target);
        }

        if Platform::is_windows() {
            // Try to copy & delete - this is a workaround for random "Access denied" errors.
            let mut output = String::new();
            let result = self.get_process().execute_args(
                &[
                    "xcopy".to_string(),
                    source.to_string(),
                    target.to_string(),
                    "/E".to_string(),
                    "/I".to_string(),
                    "/Q".to_string(),
                    "/Y".to_string(),
                ],
                &mut output,
                (),
            );

            // clear stat cache because external processes aren't tracked by the php stat cache
            clearstatcache2(false, "");

            if 0 == result {
                self.remove(source)?;

                return Ok(());
            }
        } else {
            // We do not use PHP's "rename" function here since it does not support
            // the case where $source, and $target are located on different partitions.
            let mut output = String::new();
            let result = self.get_process().execute_args(
                &["mv".to_string(), source.to_string(), target.to_string()],
                &mut output,
                (),
            );

            // clear stat cache because external processes aren't tracked by the php stat cache
            clearstatcache2(false, "");

            if 0 == result {
                return Ok(());
            }
        }

        self.copy_then_remove(source, target)
    }

    /// Returns the shortest path from $from to $to
    pub fn find_shortest_path(
        &self,
        from: &str,
        to: &str,
        directories: bool,
        prefer_relative: bool,
    ) -> String {
        if !self.is_absolute_path(from) || !self.is_absolute_path(to) {
            // PHP throws InvalidArgumentException
            // Returning early-formatted Result is not possible without changing signature; panic to surface in tests.
            panic!(
                "{}",
                sprintf(
                    "$from (%s) and $to (%s) must be absolute paths.",
                    &[from.to_string().into(), to.to_string().into()]
                )
            );
        }

        let mut from = self.normalize_path(from);
        let to = self.normalize_path(to);

        if directories {
            from = format!("{}/dummy_file", rtrim(&from, Some("/")));
        }

        if dirname(&from) == dirname(&to) {
            return format!("./{}", basename(&to));
        }

        let mut common_path = to.clone();
        while strpos(&format!("{}/", from), &format!("{}/", common_path)) != Some(0)
            && "/" != common_path
            && !Preg::is_match3("{^[A-Z]:/?$}i", &common_path, None).unwrap_or(false)
        {
            common_path = strtr(&dirname(&common_path), "\\", "/");
        }

        // no commonality at all
        if Some(0) != strpos(&from, &common_path) {
            return to;
        }

        common_path = format!("{}/", rtrim(&common_path, Some("/")));
        let source_path_depth = substr_count(&substr(&from, strlen(&common_path), None), "/");
        let common_path_code = str_repeat("../", source_path_depth as usize);

        // allow top level /foo & /bar dirs to be addressed relatively as this is common in Docker setups
        if !prefer_relative && "/" == common_path && source_path_depth > 1 {
            return to;
        }

        let result = format!(
            "{}{}",
            common_path_code,
            substr(&to, strlen(&common_path), None)
        );
        if strlen(&result) == 0 {
            return "./".to_string();
        }

        result
    }

    /// Returns PHP code that, when executed in $from, will return the path to $to
    pub fn find_shortest_path_code(
        &self,
        from: &str,
        to: &str,
        directories: bool,
        static_code: bool,
        prefer_relative: bool,
    ) -> String {
        if !self.is_absolute_path(from) || !self.is_absolute_path(to) {
            panic!(
                "{}",
                sprintf(
                    "$from (%s) and $to (%s) must be absolute paths.",
                    &[from.to_string().into(), to.to_string().into()]
                )
            );
        }

        let from = self.normalize_path(from);
        let to = self.normalize_path(to);

        if from == to {
            return (if directories { "__DIR__" } else { "__FILE__" }).to_string();
        }

        let mut common_path = to.clone();
        while strpos(&format!("{}/", from), &format!("{}/", common_path)) != Some(0)
            && "/" != common_path
            && !Preg::is_match3("{^[A-Z]:/?$}i", &common_path, None).unwrap_or(false)
            && "." != common_path
        {
            common_path = strtr(&dirname(&common_path), "\\", "/");
        }

        // no commonality at all
        if Some(0) != strpos(&from, &common_path) || "." == common_path {
            return var_export(&PhpMixed::String(to), true);
        }

        common_path = format!("{}/", rtrim(&common_path, Some("/")));
        if str_starts_with(&to, &format!("{}/", from)) {
            return format!(
                "__DIR__ . {}",
                var_export(&PhpMixed::String(substr(&to, strlen(&from), None)), true)
            );
        }
        let source_path_depth = (substr_count(&substr(&from, strlen(&common_path), None), "/")
            as i64)
            + (if directories { 1 } else { 0 });

        // allow top level /foo & /bar dirs to be addressed relatively as this is common in Docker setups
        if !prefer_relative && "/" == common_path && source_path_depth > 1 {
            return var_export(&PhpMixed::String(to), true);
        }

        let common_path_code = if static_code {
            format!(
                "__DIR__ . '{}'",
                str_repeat("/..", source_path_depth as usize)
            )
        } else {
            format!(
                "{}{}{}",
                str_repeat("dirname(", source_path_depth as usize),
                "__DIR__",
                str_repeat(")", source_path_depth as usize)
            )
        };
        let rel_target = substr(&to, strlen(&common_path), None);

        format!(
            "{}{}",
            common_path_code,
            if strlen(&rel_target) > 0 {
                format!(
                    ".{}",
                    var_export(&PhpMixed::String(format!("/{}", rel_target)), true)
                )
            } else {
                String::new()
            }
        )
    }

    /// Checks if the given path is absolute
    pub fn is_absolute_path(&self, path: &str) -> bool {
        strpos(path, "/") == Some(0)
            || substr(path, 1, Some(1)) == ":"
            || strpos(path, "\\\\") == Some(0)
    }

    /// Returns size of a file or directory specified by path. If a directory is
    /// given, its size will be computed recursively.
    pub fn size(&self, path: &str) -> anyhow::Result<i64> {
        if !file_exists(path) {
            return Err(RuntimeException {
                message: format!("{} does not exist.", path),
                code: 0,
            }
            .into());
        }
        if is_dir(path) {
            return Ok(self.directory_size(path));
        }

        Ok(filesize(path).unwrap_or(0))
    }

    /// Normalize a path. This replaces backslashes with slashes, removes ending
    /// slash and collapses redundant separators and up-level references.
    pub fn normalize_path(&self, path: &str) -> String {
        let mut parts: Vec<String> = vec![];
        let mut path = strtr(path, "\\", "/");
        let mut prefix = String::new();
        let mut absolute = String::new();

        // extract windows UNC paths e.g. \\foo\bar
        if strpos(&path, "//") == Some(0) && strlen(&path) > 2 {
            absolute = "//".to_string();
            path = substr(&path, 2, None);
        }

        // extract a prefix being a protocol://, protocol:, protocol://drive: or simply drive:
        let mut prefix_match: indexmap::IndexMap<
            shirabe_external_packages::composer::pcre::CaptureKey,
            String,
        > = indexmap::IndexMap::new();
        if Preg::is_match_strict_groups3(
            "{^( [0-9a-z]{2,}+: (?: // (?: [a-z]: )? )? | [a-z]: )}ix",
            &path,
            Some(&mut prefix_match),
        )
        .unwrap_or(false)
        {
            prefix = prefix_match
                .get(&shirabe_external_packages::composer::pcre::CaptureKey::ByIndex(1))
                .cloned()
                .unwrap_or_default();
            path = substr(&path, strlen(&prefix), None);
        }

        if strpos(&path, "/") == Some(0) {
            absolute = "/".to_string();
            path = substr(&path, 1, None);
        }

        let mut up = false;
        for chunk in explode("/", &path) {
            if ".." == chunk && (strlen(&absolute) > 0 || up) {
                array_pop(&mut parts);
                up = !(parts.len() == 0 || ".." == end(&parts).unwrap_or_default());
            } else if "." != chunk && "" != chunk {
                parts.push(chunk.clone());
                up = ".." != chunk;
            }
        }

        // ensure c: is normalized to C:
        prefix = Preg::replace_callback(
            "{(^|://)[a-z]:$}i",
            |m: &indexmap::IndexMap<
                shirabe_external_packages::composer::pcre::CaptureKey,
                String,
            >|
             -> String {
                let s = m
                    .get(&shirabe_external_packages::composer::pcre::CaptureKey::ByIndex(0))
                    .cloned()
                    .unwrap_or_default();
                strtoupper(&s)
            },
            &prefix,
        )
        .unwrap_or_default();

        format!("{}{}{}", prefix, absolute, implode("/", &parts))
    }

    /// Remove trailing slashes if present to avoid issues with symlinks
    ///
    /// And other possible unforeseen disasters, see https://github.com/composer/composer/pull/9422
    pub fn trim_trailing_slash(path: &str) -> String {
        let mut path = path.to_string();
        if !Preg::is_match3("{^[/\\\\]+$}", &path, None).unwrap_or(false) {
            path = rtrim(&path, Some("/\\"));
        }

        path
    }

    /// Return if the given path is local
    pub fn is_local_path(path: &str) -> bool {
        // on windows, \\foo indicates network paths so we exclude those from local paths, however it is unsafe
        // on linux as file:////foo (which would be a network path \\foo on windows) will resolve to /foo which could be a local path
        if Platform::is_windows() {
            return Preg::is_match3(
                "{^(file://(?!//)|/(?!/)|/?[a-z]:[\\\\/]|\\.\\.[\\\\/]|[a-z0-9_.-]+[\\\\/])}i",
                path,
                None,
            )
            .unwrap_or(false);
        }

        Preg::is_match3(
            "{^(file://|/|/?[a-z]:[\\\\/]|\\.\\.[\\\\/]|[a-z0-9_.-]+[\\\\/])}i",
            path,
            None,
        )
        .unwrap_or(false)
    }

    pub fn get_platform_path(path: &str) -> String {
        let mut path = path.to_string();
        if Platform::is_windows() {
            path = Preg::replace("{^(?:file:///([a-z]):?/)}i", "file://$1:/", &path)
                .unwrap_or_default();
        }

        Preg::replace("{^file://}i", "", &path).unwrap_or_default()
    }

    /// Cross-platform safe version of is_readable()
    ///
    /// This will also check for readability by reading the file as is_readable can not be trusted on network-mounts
    /// and \\\\wsl$ paths. See https://github.com/composer/composer/issues/8231 and https://bugs.php.net/bug.php?id=68926
    pub fn is_readable(path: &str) -> bool {
        if is_readable(path) {
            return true;
        }

        if is_file(path) {
            return Silencer::call(|| Ok(file_get_contents(path).is_some())).unwrap_or(false);
        }

        if is_dir(path) {
            return Silencer::call(|| Ok(shirabe_php_shim::opendir(path).is_some()))
                .unwrap_or(false);
        }

        // assume false otherwise
        false
    }

    pub(crate) fn directory_size(&self, directory: &str) -> i64 {
        let it =
            shirabe_php_shim::recursive_directory_iterator(directory, shirabe_php_shim::SKIP_DOTS);
        let ri = shirabe_php_shim::recursive_iterator_iterator(it, shirabe_php_shim::CHILD_FIRST);

        let mut size: i64 = 0;
        for file in &ri {
            if file.is_file() {
                size += file.get_size();
            }
        }

        size
    }

    pub(crate) fn get_process(&mut self) -> std::cell::RefMut<'_, ProcessExecutor> {
        if self.process_executor.is_none() {
            self.process_executor = Some(std::rc::Rc::new(std::cell::RefCell::new(
                ProcessExecutor::new(()),
            )));
        }

        self.process_executor.as_ref().unwrap().borrow_mut()
    }

    /// delete symbolic link implementation (commonly known as "unlink()")
    ///
    /// symbolic links on windows which link to directories need rmdir instead of unlink
    fn unlink_implementation(&self, path: &str) -> bool {
        if Platform::is_windows() && is_dir(path) && is_link(path) {
            return rmdir(path);
        }

        unlink(path)
    }

    /// Creates a relative symlink from $link to $target
    pub fn relative_symlink(&self, target: &str, link: &str) -> bool {
        if !function_exists("symlink") {
            return false;
        }

        let cwd = Platform::get_cwd(false).unwrap_or_default();

        let relative_path = self.find_shortest_path(link, target, false, false);
        chdir(&dirname(link));
        let result = symlink(&relative_path, link);

        chdir(&cwd);

        result
    }

    /// return true if that directory is a symlink.
    pub fn is_symlinked_directory(&self, directory: &str) -> bool {
        if !is_dir(directory) {
            return false;
        }

        let resolved = self.resolve_symlinked_directory_symlink(directory);

        is_link(&resolved)
    }

    fn unlink_symlinked_directory(&self, directory: &str) -> anyhow::Result<bool> {
        let resolved = self.resolve_symlinked_directory_symlink(directory);

        self.unlink(&resolved)
    }

    /// resolve pathname to symbolic link of a directory
    fn resolve_symlinked_directory_symlink(&self, pathname: &str) -> String {
        if !is_dir(pathname) {
            return pathname.to_string();
        }

        let resolved = rtrim(pathname, Some("/"));

        if 0 == strlen(&resolved) {
            return pathname.to_string();
        }

        resolved
    }

    /// Creates an NTFS junction.
    pub fn junction(&mut self, target: &str, junction: &str) -> anyhow::Result<()> {
        if !Platform::is_windows() {
            return Err(LogicException {
                message: format!(
                    "Function {} is not available on non-Windows platform",
                    "Composer\\Util\\Filesystem"
                ),
                code: 0,
            }
            .into());
        }
        if !is_dir(target) {
            return Err(IOException::new(
                format!(
                    "Cannot junction to \"{}\" as it is not a directory.",
                    target
                ),
                0,
                None,
                Some(target.to_string()),
            )
            .into());
        }

        // Removing any previously junction to ensure clean execution.
        if !is_dir(junction) || self.is_junction(junction) {
            let _ = rmdir(junction);
        }

        let cmd = vec![
            "mklink".to_string(),
            "/J".to_string(),
            str_replace("/", DIRECTORY_SEPARATOR, junction),
            Platform::realpath(target),
        ];
        let mut output = String::new();
        if self.get_process().execute_args(&cmd, &mut output, ()) != 0 {
            return Err(IOException::new(
                format!(
                    "Failed to create junction to \"{}\" at \"{}\".",
                    target, junction
                ),
                0,
                None,
                Some(target.to_string()),
            )
            .into());
        }
        clearstatcache2(true, junction);
        Ok(())
    }

    /// Returns whether the target directory is a Windows NTFS Junction.
    ///
    /// We test if the path is a directory and not an ordinary link, then check
    /// that the mode value returned from lstat (which gives the status of the
    /// link itself) is not a directory, by replicating the POSIX S_ISDIR test.
    ///
    /// This logic works because PHP does not set the mode value for a junction,
    /// since there is no universal file type flag for it. Unfortunately an
    /// uninitialized variable in PHP prior to 7.2.16 and 7.3.3 may cause a
    /// random value to be returned. See https://bugs.php.net/bug.php?id=77552
    ///
    /// If this random value passes the S_ISDIR test, then a junction will not be
    /// detected and a recursive delete operation could lead to loss of data in
    /// the target directory. Note that Windows rmdir can handle this situation
    /// and will only delete the junction (from Windows 7 onwards).
    pub fn is_junction(&self, junction: &str) -> bool {
        if !Platform::is_windows() {
            return false;
        }

        // Important to clear all caches first
        clearstatcache2(true, junction);

        if !is_dir(junction) || is_link(junction) {
            return false;
        }

        let stat = lstat(junction);

        // S_ISDIR test (S_IFDIR is 0x4000, S_IFMT is 0xF000 bitmask)
        if let Some(arr) = stat {
            let mode = arr.get("mode").and_then(|v| v.as_int()).unwrap_or(0);
            return 0x4000 != (mode & 0xF000);
        }
        false
    }

    /// Removes a Windows NTFS junction.
    pub fn remove_junction(&mut self, junction: &str) -> anyhow::Result<bool> {
        if !Platform::is_windows() {
            return Ok(false);
        }
        let junction = rtrim(
            &str_replace("/", DIRECTORY_SEPARATOR, junction),
            Some(DIRECTORY_SEPARATOR),
        );
        if !self.is_junction(&junction) {
            return Err(IOException::new(
                format!(
                    "{} is not a junction and thus cannot be removed as one",
                    junction
                ),
                0,
                None,
                None,
            )
            .into());
        }

        self.rmdir(&junction)
    }

    pub fn file_put_contents_if_modified(&self, path: &str, content: &str) -> anyhow::Result<i64> {
        let current_content =
            Silencer::call(|| Ok(file_get_contents(path).unwrap_or_default())).unwrap_or_default();
        if current_content.is_empty() || current_content != content {
            return Ok(file_put_contents(path, content.as_bytes()).unwrap_or(0));
        }

        Ok(0)
    }

    /// Copy file using stream_copy_to_stream to work around https://bugs.php.net/bug.php?id=6463
    pub fn safe_copy(&self, source: &str, target: &str) -> anyhow::Result<()> {
        if !file_exists(target) || !file_exists(source) || !self.files_are_equal(source, target) {
            let source_handle = fopen(source, "r");
            if source_handle.is_null() {
                return Err(anyhow::anyhow!(
                    "Could not open \"{}\" for reading.",
                    source
                ));
            }
            let target_handle = fopen(target, "w+");
            if target_handle.is_null() {
                return Err(anyhow::anyhow!(
                    "Could not open \"{}\" for writing.",
                    target
                ));
            }

            shirabe_php_shim::stream_copy_to_stream(source_handle.clone(), target_handle.clone());
            fclose(source_handle);
            fclose(target_handle);

            touch(target);
            // PHP also passes filemtime/fileatime — skipping detailed timestamp restore here.
            let _ = (filemtime(source), fileatime(source));
        }
        Ok(())
    }

    /// compare 2 files
    /// https://stackoverflow.com/questions/3060125/can-i-use-file-get-contents-to-compare-two-files
    fn files_are_equal(&self, a: &str, b: &str) -> bool {
        // Check if filesize is different
        if filesize(a) != filesize(b) {
            return false;
        }

        // Check if content is different
        let a_handle = fopen(a, "rb");
        if a_handle.is_null() {
            return false;
        }
        let b_handle = fopen(b, "rb");
        if b_handle.is_null() {
            return false;
        }

        let mut result = true;
        while !feof(a_handle.clone()) {
            if fread(a_handle.clone(), 8192) != fread(b_handle.clone(), 8192) {
                result = false;
                break;
            }
        }

        fclose(a_handle);
        fclose(b_handle);

        result
    }
}