aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe-php-shim/src/fs.rs
blob: a97433a9f27dca8db6bc49d68e077ca8c774d0b2 (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
use crate::PhpMixed;
use crate::PhpResource;
use crate::UnexpectedValueException;
use indexmap::IndexMap;

pub const PHP_EOL: &str = "\n";

pub const FILE_APPEND: i64 = 8;

pub const STDIN: PhpResource = PhpResource::Stdin;

pub const PATHINFO_FILENAME: i64 = 64;
pub const PATHINFO_EXTENSION: i64 = 4;
pub const PATHINFO_DIRNAME: i64 = 1;
pub const PATHINFO_BASENAME: i64 = 2;

pub const PATH_SEPARATOR: &str = ":";
pub const DIRECTORY_SEPARATOR: &str = "/";

pub const FILE_IGNORE_NEW_LINES: i64 = 2;

pub const SEEK_SET: i64 = 0;
pub const SEEK_CUR: i64 = 1;
pub const SEEK_END: i64 = 2;

pub const SKIP_DOTS: i64 = 4096;
pub const CHILD_FIRST: i64 = 16;
pub const SELF_FIRST: i64 = 0;

pub struct FilesystemIterator;

impl FilesystemIterator {
    pub const KEY_AS_PATHNAME: i64 = 256;
    pub const CURRENT_AS_FILEINFO: i64 = 0;
}

#[derive(Debug)]
pub struct DirectoryIteratorEntry;
impl DirectoryIteratorEntry {
    // TODO(phase-d): DirectoryIterator is a unit struct carrying no entry data; giving it real
    // behavior requires the same field redesign as RecursiveIteratorFileInfo. It has no callers, so
    // it is left unimplemented.
    pub fn get_basename(&self) -> String {
        todo!()
    }
    pub fn is_file(&self) -> bool {
        todo!()
    }
    pub fn get_extension(&self) -> String {
        todo!()
    }
}

#[derive(Debug)]
pub struct RecursiveDirectoryIterator {
    root: std::path::PathBuf,
    flags: i64,
}

impl RecursiveDirectoryIterator {
    pub const SKIP_DOTS: i64 = 4096;
    pub const FOLLOW_SYMLINKS: i64 = 512;
}

#[derive(Debug)]
pub struct RecursiveIteratorIterator {
    entries: Vec<RecursiveIteratorFileInfo>,
    // Index of the entry the iteration is currently on, so get_sub_pathname() can report it.
    cursor: std::cell::Cell<usize>,
}

impl RecursiveIteratorIterator {
    pub const SELF_FIRST: i64 = 0;
    pub const CHILD_FIRST: i64 = 16;

    pub fn get_sub_pathname(&self) -> String {
        self.entries[self.cursor.get()].sub_pathname()
    }
}

pub struct RecursiveIteratorIter<'a> {
    inner: &'a RecursiveIteratorIterator,
    index: usize,
}

impl Iterator for RecursiveIteratorIter<'_> {
    type Item = RecursiveIteratorFileInfo;

    fn next(&mut self) -> Option<Self::Item> {
        if self.index < self.inner.entries.len() {
            // Publish the current position so get_sub_pathname() called inside the loop sees it.
            self.inner.cursor.set(self.index);
            let item = self.inner.entries[self.index].clone();
            self.index += 1;
            Some(item)
        } else {
            None
        }
    }
}

impl<'a> IntoIterator for &'a RecursiveIteratorIterator {
    type Item = RecursiveIteratorFileInfo;
    type IntoIter = RecursiveIteratorIter<'a>;

    fn into_iter(self) -> Self::IntoIter {
        RecursiveIteratorIter {
            inner: self,
            index: 0,
        }
    }
}

#[derive(Debug, Clone)]
pub struct RecursiveIteratorFileInfo {
    path: std::path::PathBuf,
    root: std::path::PathBuf,
}

impl RecursiveIteratorFileInfo {
    pub fn is_dir(&self) -> bool {
        // SplFileInfo::isDir() follows symlinks.
        std::fs::metadata(&self.path)
            .map(|m| m.is_dir())
            .unwrap_or(false)
    }

    pub fn is_file(&self) -> bool {
        std::fs::metadata(&self.path)
            .map(|m| m.is_file())
            .unwrap_or(false)
    }

    pub fn is_link(&self) -> bool {
        std::fs::symlink_metadata(&self.path)
            .map(|m| m.file_type().is_symlink())
            .unwrap_or(false)
    }

    pub fn get_pathname(&self) -> String {
        self.path.to_string_lossy().into_owned()
    }

    pub fn get_size(&self) -> i64 {
        std::fs::metadata(&self.path)
            .map(|m| m.len() as i64)
            .unwrap_or(0)
    }

    fn sub_pathname(&self) -> String {
        self.path
            .strip_prefix(&self.root)
            .map(|p| p.to_string_lossy().into_owned())
            .unwrap_or_else(|_| self.get_pathname())
    }
}

pub fn recursive_directory_iterator(
    _path: impl AsRef<std::path::Path>,
    _flags: i64,
) -> Result<RecursiveDirectoryIterator, UnexpectedValueException> {
    let root = _path.as_ref().to_path_buf();
    if !root.is_dir() {
        return Err(UnexpectedValueException {
            message: format!(
                "RecursiveDirectoryIterator::__construct({}): Failed to open directory",
                root.to_string_lossy()
            ),
            code: 0,
        });
    }
    Ok(RecursiveDirectoryIterator {
        root,
        flags: _flags,
    })
}

pub fn recursive_iterator_iterator(
    _iter: RecursiveDirectoryIterator,
    _mode: i64,
) -> RecursiveIteratorIterator {
    let mut entries = Vec::new();
    rii_walk(&_iter.root, &_iter.root, _iter.flags, _mode, &mut entries);
    RecursiveIteratorIterator {
        entries,
        cursor: std::cell::Cell::new(0),
    }
}

// Recursively collects directory entries in filesystem order, matching SplFileInfo recursion:
// real subdirectories are descended into (also symlinked dirs when FOLLOW_SYMLINKS is set), with the
// directory itself yielded before its children for SELF_FIRST and after them for CHILD_FIRST.
fn rii_walk(
    dir: &std::path::Path,
    root: &std::path::Path,
    flags: i64,
    mode: i64,
    out: &mut Vec<RecursiveIteratorFileInfo>,
) {
    let rd = match std::fs::read_dir(dir) {
        Ok(rd) => rd,
        Err(_) => return,
    };
    for entry in rd.flatten() {
        let path = entry.path();
        let is_real_dir = std::fs::symlink_metadata(&path)
            .map(|m| m.is_dir())
            .unwrap_or(false);
        let follows_symlink_dir = (flags & RecursiveDirectoryIterator::FOLLOW_SYMLINKS != 0)
            && std::fs::metadata(&path)
                .map(|m| m.is_dir())
                .unwrap_or(false);
        let info = RecursiveIteratorFileInfo {
            path: path.clone(),
            root: root.to_path_buf(),
        };
        if is_real_dir || follows_symlink_dir {
            if mode == RecursiveIteratorIterator::CHILD_FIRST {
                rii_walk(&path, root, flags, mode, out);
                out.push(info);
            } else {
                out.push(info);
                rii_walk(&path, root, flags, mode, out);
            }
        } else {
            out.push(info);
        }
    }
}

pub fn directory_iterator(_path: &str) -> Vec<DirectoryIteratorEntry> {
    // TODO(phase-d): see DirectoryIteratorEntry; the entry type carries no data yet and there are no
    // callers.
    todo!()
}

// TODO(phase-d): the fopen-family stream API is keyed on PhpMixed, but PhpMixed has no stream/
// resource variant, so an opened stream cannot be represented or threaded through fread/fwrite/etc.
// Wiring a stream representation into PhpMixed is Phase C type-design work.
pub fn fopen(_file: &str, _mode: &str) -> PhpMixed {
    todo!()
}

pub fn fwrite(_file: PhpMixed, _data: &str, _length: i64) -> Option<i64> {
    // TODO(phase-d): see fopen; no PhpMixed stream representation exists.
    todo!()
}

pub fn fread(_handle: PhpMixed, _length: i64) -> Option<String> {
    // TODO(phase-d): see fopen; no PhpMixed stream representation exists.
    todo!()
}

pub fn feof(_stream: PhpMixed) -> bool {
    // TODO(phase-d): see fopen; no PhpMixed stream representation exists.
    todo!()
}

pub fn fclose(_file: PhpMixed) {
    // TODO(phase-d): see fopen; no PhpMixed stream representation exists.
    todo!()
}

pub fn fgets(_handle: PhpMixed) -> Option<String> {
    // TODO(phase-d): see fopen; no PhpMixed stream representation exists.
    todo!()
}

pub fn fgetc(_resource: &PhpResource) -> Option<String> {
    // TODO(phase-d): PhpResource models stdio/file write sinks (see fwrite_resource) but not
    // buffered reads with a tracked position; fgetc needs a readable, seekable stream wrapper.
    todo!()
}

pub fn ftell(_resource: &PhpResource) -> i64 {
    // TODO(phase-d): PhpResource does not track a stream position; see fgetc.
    todo!()
}

pub fn fseek(_stream: PhpMixed, _offset: i64) -> i64 {
    // TODO(phase-d): see fopen; no PhpMixed stream representation exists.
    todo!()
}

pub fn rewind(_stream: PhpMixed) -> bool {
    // TODO(phase-d): see fopen; no PhpMixed stream representation exists.
    todo!()
}

pub fn fstat(_stream: PhpResource) -> PhpMixed {
    // TODO(phase-d): PhpResource::File holds a File, but the stdio variants have no fd to stat; a
    // faithful fstat needs a uniform stream handle.
    todo!()
}

pub fn lstat(_filename: &str) -> Option<IndexMap<String, PhpMixed>> {
    use std::os::unix::fs::MetadataExt;
    let m = std::fs::symlink_metadata(_filename).ok()?;
    // PHP stat/lstat return the 13 fields both by numeric index (0..12) and by name.
    let fields: [(&str, i64); 13] = [
        ("dev", m.dev() as i64),
        ("ino", m.ino() as i64),
        ("mode", m.mode() as i64),
        ("nlink", m.nlink() as i64),
        ("uid", m.uid() as i64),
        ("gid", m.gid() as i64),
        ("rdev", m.rdev() as i64),
        ("size", m.size() as i64),
        ("atime", m.atime()),
        ("mtime", m.mtime()),
        ("ctime", m.ctime()),
        ("blksize", m.blksize() as i64),
        ("blocks", m.blocks() as i64),
    ];
    let mut map = IndexMap::new();
    for (i, (_, v)) in fields.iter().enumerate() {
        map.insert(i.to_string(), PhpMixed::Int(*v));
    }
    for (name, v) in &fields {
        map.insert(name.to_string(), PhpMixed::Int(*v));
    }
    Some(map)
}

/// PHP `ftell()` over a PhpMixed stream resource. (`ftell` itself is already defined for the
/// `PhpResource`-typed stream API used elsewhere.)
pub fn ftell_stream(_stream: &PhpMixed) -> i64 {
    // TODO(phase-d): see fopen; no PhpMixed stream representation exists.
    todo!()
}

pub fn fseek3(_stream: PhpMixed, _offset: i64, _whence: i64) -> i64 {
    // TODO(phase-d): see fopen; no PhpMixed stream representation exists.
    todo!()
}

pub fn touch(_path: &str) -> bool {
    // TODO(phase-d): for an existing file PHP also bumps its mtime/atime to now; std exposes no
    // portable utime, so only the create-if-absent case is handled here.
    std::fs::OpenOptions::new()
        .write(true)
        .create(true)
        .truncate(false)
        .open(_path)
        .is_ok()
}

pub fn fflush_resource(resource: &PhpResource) {
    use std::io::Write;
    match resource {
        PhpResource::Stdin => {}
        PhpResource::Stdout => {
            let _ = std::io::stdout().flush();
        }
        PhpResource::Stderr => {
            let _ = std::io::stderr().flush();
        }
        PhpResource::File(file) => {
            let _ = file.borrow_mut().flush();
        }
    }
}

pub fn fwrite_resource(resource: &PhpResource, data: &str) {
    use std::io::Write;
    let bytes = data.as_bytes();
    match resource {
        PhpResource::Stdin => {}
        PhpResource::Stdout => {
            let _ = std::io::stdout().write_all(bytes);
        }
        PhpResource::Stderr => {
            let _ = std::io::stderr().write_all(bytes);
        }
        PhpResource::File(file) => {
            let _ = file.borrow_mut().write_all(bytes);
        }
    }
}

pub fn touch2(_path: &str, _mtime: i64) -> bool {
    // TODO(phase-d): setting an explicit mtime needs utimensat(2), not exposed by std (no
    // libc/filetime crate available).
    todo!()
}

pub fn touch3(_path: &str, _mtime: i64, _atime: i64) -> bool {
    // TODO(phase-d): setting explicit mtime/atime needs utimensat(2); see touch2.
    todo!()
}

pub fn chmod(_path: &str, _mode: u32) -> bool {
    use std::os::unix::fs::PermissionsExt;
    std::fs::set_permissions(_path, std::fs::Permissions::from_mode(_mode)).is_ok()
}

pub fn fileperms(_path: &str) -> i64 {
    use std::os::unix::fs::MetadataExt;
    // PHP returns the full st_mode (file type bits included).
    // TODO(phase-d): PHP returns false on error; this i64 signature reports 0 instead.
    std::fs::metadata(_path)
        .map(|m| m.mode() as i64)
        .unwrap_or(0)
}

pub fn filesize(path: impl AsRef<std::path::Path>) -> Option<i64> {
    std::fs::metadata(path).ok().map(|m| m.len() as i64)
}

pub fn file_exists(path: impl AsRef<std::path::Path>) -> bool {
    path.as_ref().exists()
}

// TODO(phase-c): PHP's is_writable() resolves to access(2) with W_OK, honoring the effective
// user/group and ACLs. This std-only approximation only inspects the permission bits, so it can
// diverge for files the current user does not own. Refine with a syscall (libc/rustix) crate later.
pub fn is_writable(_path: &str) -> bool {
    match std::fs::metadata(_path) {
        Ok(meta) => !meta.permissions().readonly(),
        Err(_) => false,
    }
}

pub fn is_readable(_path: &str) -> bool {
    let path = std::path::Path::new(_path);
    match std::fs::metadata(path) {
        Ok(meta) => {
            if meta.is_dir() {
                std::fs::read_dir(path).is_ok()
            } else {
                std::fs::File::open(path).is_ok()
            }
        }
        Err(_) => false,
    }
}

pub fn is_executable(_path: &str) -> bool {
    use std::os::unix::fs::PermissionsExt;
    // TODO(phase-d): like is_writable, this only inspects the permission bits and ignores the
    // effective user/group, so it can diverge from PHP's access(2, X_OK) check.
    match std::fs::metadata(_path) {
        Ok(m) => (m.permissions().mode() & 0o111) != 0,
        Err(_) => false,
    }
}

pub fn is_file(path: impl AsRef<std::path::Path>) -> bool {
    path.as_ref().is_file()
}

pub fn is_link(path: impl AsRef<std::path::Path>) -> bool {
    std::fs::symlink_metadata(path)
        .map(|m| m.file_type().is_symlink())
        .unwrap_or(false)
}

pub fn is_dir(path: impl AsRef<std::path::Path>) -> bool {
    path.as_ref().is_dir()
}

pub fn fileatime(_filename: &str) -> Option<i64> {
    std::fs::metadata(_filename)
        .ok()
        .and_then(|m| m.accessed().ok())
        .and_then(|t| t.duration_since(std::time::UNIX_EPOCH).ok())
        .map(|d| d.as_secs() as i64)
}

pub fn filemtime(_filename: &str) -> Option<i64> {
    std::fs::metadata(_filename)
        .ok()
        .and_then(|m| m.modified().ok())
        .and_then(|t| t.duration_since(std::time::UNIX_EPOCH).ok())
        .map(|d| d.as_secs() as i64)
}

pub fn fileowner(_filename: &str) -> Option<i64> {
    use std::os::unix::fs::MetadataExt;
    std::fs::metadata(_filename).ok().map(|m| m.uid() as i64)
}

pub fn unlink(path: impl AsRef<std::path::Path>) -> bool {
    std::fs::remove_file(path).is_ok()
}

pub fn unlink_silent(_path: &str) -> bool {
    // PHP's `@unlink`: delete the file, suppressing any warning.
    std::fs::remove_file(_path).is_ok()
}

pub fn file_put_contents(_path: &str, _data: &[u8]) -> Option<i64> {
    std::fs::write(_path, _data)
        .ok()
        .map(|_| _data.len() as i64)
}

pub fn file_put_contents3(_filename: &str, _data: &str, _flags: i64) -> Option<i64> {
    use std::io::Write;
    // TODO(phase-d): the LOCK_EX and FILE_USE_INCLUDE_PATH flags are ignored; only FILE_APPEND is
    // honored.
    let append = _flags & FILE_APPEND != 0;
    let mut opts = std::fs::OpenOptions::new();
    opts.write(true).create(true);
    if append {
        opts.append(true);
    } else {
        opts.truncate(true);
    }
    let mut file = opts.open(_filename).ok()?;
    file.write_all(_data.as_bytes()).ok()?;
    Some(_data.len() as i64)
}

pub fn file_get_contents(_path: &str) -> Option<String> {
    std::fs::read(_path)
        .ok()
        .map(|bytes| String::from_utf8_lossy(&bytes).into_owned())
}

pub fn file_get_contents5(
    _path: &str,
    _use_include_path: bool,
    _context: PhpMixed,
    _offset: i64,
    _length: Option<i64>,
) -> Option<String> {
    // TODO(phase-d): the stream $context and FILE_USE_INCLUDE_PATH are ignored; only $offset and
    // $length are applied (to the file read from the local filesystem).
    let bytes = std::fs::read(_path).ok()?;
    let len = bytes.len() as i64;
    let start = if _offset < 0 {
        (len + _offset).max(0)
    } else {
        _offset.min(len)
    } as usize;
    let slice = &bytes[start..];
    let slice = match _length {
        Some(l) if l >= 0 => &slice[..(l as usize).min(slice.len())],
        _ => slice,
    };
    Some(String::from_utf8_lossy(slice).into_owned())
}

pub fn getcwd() -> Option<String> {
    std::env::current_dir()
        .ok()
        .map(|p| p.to_string_lossy().into_owned())
}

pub fn chdir(_path: &str) -> anyhow::Result<()> {
    Ok(std::env::set_current_dir(_path)?)
}

pub fn glob(_pattern: &str) -> Vec<String> {
    glob_with_flags(_pattern, 0)
}

pub const FILE_SKIP_EMPTY_LINES: i64 = 4;

pub fn file(_filename: &str, _flags: i64) -> Option<Vec<String>> {
    let content = std::fs::read(_filename).ok()?;
    let s = String::from_utf8_lossy(&content);
    let ignore_newlines = _flags & FILE_IGNORE_NEW_LINES != 0;
    let skip_empty = _flags & FILE_SKIP_EMPTY_LINES != 0;
    let mut lines = Vec::new();
    // PHP keeps the trailing newline on each element unless FILE_IGNORE_NEW_LINES is set.
    for line in s.split_inclusive('\n') {
        let mut l = line.to_string();
        if ignore_newlines {
            if l.ends_with('\n') {
                l.pop();
            }
            if l.ends_with('\r') {
                l.pop();
            }
        }
        if skip_empty && l.is_empty() {
            continue;
        }
        lines.push(l);
    }
    Some(lines)
}

pub fn umask() -> u32 {
    // Linux exposes the current umask via /proc/self/status.
    // TODO(phase-d): other platforms have no /proc; reading the umask there needs the
    // read-modify-write umask(2), which std does not expose (no libc/syscall crate available).
    std::fs::read_to_string("/proc/self/status")
        .ok()
        .and_then(|status| {
            status.lines().find_map(|line| {
                line.strip_prefix("Umask:")
                    .and_then(|v| u32::from_str_radix(v.trim(), 8).ok())
            })
        })
        .unwrap_or(0o022)
}

pub fn mkdir(_pathname: &str, _mode: u32, _recursive: bool) -> bool {
    use std::os::unix::fs::DirBuilderExt;
    // DirBuilder::mode passes the mode to mkdir(2), which applies the process umask, matching PHP.
    let mut builder = std::fs::DirBuilder::new();
    builder.mode(_mode).recursive(_recursive);
    builder.create(_pathname).is_ok()
}

pub fn rmdir(dir: impl AsRef<std::path::Path>) -> bool {
    std::fs::remove_dir(dir).is_ok()
}

pub fn rename(
    old_name: impl AsRef<std::path::Path>,
    new_name: impl AsRef<std::path::Path>,
) -> bool {
    std::fs::rename(old_name, new_name).is_ok()
}

pub fn copy(_source: &str, _dest: &str) -> bool {
    std::fs::copy(_source, _dest).is_ok()
}

pub fn ftruncate(_stream: &PhpMixed, _size: i64) -> bool {
    // TODO(phase-d): see fopen; no PhpMixed stream representation exists to truncate.
    todo!()
}

pub fn symlink(_target: &str, _link: &str) -> bool {
    std::os::unix::fs::symlink(_target, _link).is_ok()
}

pub fn sys_get_temp_dir() -> String {
    std::env::temp_dir().to_string_lossy().into_owned()
}

pub fn tempnam(_dir: &str, _prefix: &str) -> Option<String> {
    use std::os::unix::fs::PermissionsExt;
    // TODO(phase-d): PHP falls back to the system temp dir when $dir is not writable; that fallback
    // is not implemented here.
    for _ in 0..1000 {
        let name = format!("{}{:08x}", _prefix, fastrand::u32(..));
        let path = std::path::Path::new(_dir).join(name);
        match std::fs::OpenOptions::new()
            .write(true)
            .create_new(true)
            .open(&path)
        {
            Ok(_) => {
                // PHP creates the file with 0600 permissions.
                let _ = std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o600));
                return path.to_str().map(ToOwned::to_owned);
            }
            Err(_) => continue,
        }
    }
    None
}

pub fn opendir(_path: &str) -> Option<PhpMixed> {
    // TODO(phase-d): opendir returns a directory-handle resource consumed by readdir/closedir, but
    // PhpMixed has no resource variant to carry it (see fopen).
    todo!()
}

pub fn pathinfo(path: PhpMixed, option: i64) -> PhpMixed {
    let path = path.as_string().unwrap_or("");
    let component = match option {
        PATHINFO_DIRNAME => dirname(path),
        PATHINFO_BASENAME => basename(path),
        PATHINFO_EXTENSION => {
            let base = basename(path);
            match base.rfind('.') {
                Some(index) => base[index + 1..].to_string(),
                None => String::new(),
            }
        }
        PATHINFO_FILENAME => {
            let base = basename(path);
            match base.rfind('.') {
                Some(index) => base[..index].to_string(),
                None => base,
            }
        }
        _ => unreachable!("pathinfo called with an unsupported single-component option"),
    };
    PhpMixed::String(component)
}

// TODO(phase-c): takes &Path and returns Option<PathBuf>
pub fn realpath(path: &str) -> Option<String> {
    std::path::Path::new(path)
        .canonicalize()
        .ok()
        .and_then(|p| p.to_str().map(ToOwned::to_owned))
}

pub fn dirname(path: &str) -> String {
    if path.is_empty() {
        return String::new();
    }
    match std::path::Path::new(path).parent() {
        // No parent: the root itself, or a path made up solely of slashes.
        None => "/".to_string(),
        // Path::parent yields an empty path where PHP's dirname returns ".".
        Some(parent) if parent.as_os_str().is_empty() => ".".to_string(),
        Some(parent) => parent.to_str().expect("input was valid UTF-8").to_string(),
    }
}

pub fn dirname_levels(path: &str, levels: i64) -> String {
    let mut result = path.to_string();
    for _ in 0..levels {
        result = dirname(&result);
    }
    result
}

pub fn basename(path: &str) -> String {
    // PHP basename(): the trailing name component, after stripping trailing directory separators.
    let trimmed = path.trim_end_matches(['/', '\\']);
    match trimmed.rfind(['/', '\\']) {
        Some(index) => trimmed[index + 1..].to_string(),
        None => trimmed.to_string(),
    }
}

pub fn basename_with_suffix(path: &str, suffix: &str) -> String {
    let base = basename(path);
    // PHP strips the suffix only when it is a proper trailing part of the name,
    // never when it equals the whole basename.
    if base != suffix && base.ends_with(suffix) {
        base[..base.len() - suffix.len()].to_string()
    } else {
        base
    }
}

pub fn clearstatcache() {
    // Rust performs a fresh syscall for every metadata query; there is no stat
    // cache to invalidate.
}

pub fn clearstatcache2(_clear_realpath_cache: bool, _filename: &str) {
    // Rust performs a fresh syscall for every metadata query; there is no stat
    // cache to invalidate.
}

pub fn disk_free_space(_directory: &str) -> Option<f64> {
    // TODO(phase-d): reading free space for an arbitrary path requires statvfs(3); std exposes no
    // equivalent and no /proc file gives per-path free space (no libc/syscall crate available).
    todo!()
}

pub const GLOB_MARK: i64 = 8;
pub const GLOB_ONLYDIR: i64 = 1024;
pub const GLOB_BRACE: i64 = 4096;

pub fn glob_with_flags(_pattern: &str, _flags: i64) -> Vec<String> {
    let patterns = if _flags & GLOB_BRACE != 0 {
        glob_expand_braces(_pattern)
    } else {
        vec![_pattern.to_string()]
    };
    let mut results: Vec<String> = Vec::new();
    for pattern in patterns {
        glob_collect(&pattern, _flags, &mut results);
    }
    // PHP sorts the result set by default (GLOB_NOSORT is not modeled here).
    results.sort();
    results.dedup();
    results
}

fn glob_collect(pattern: &str, flags: i64, out: &mut Vec<String>) {
    let (mut current, rest) = match pattern.strip_prefix('/') {
        Some(rest) => (vec!["/".to_string()], rest),
        None => (vec![String::new()], pattern),
    };
    let segments: Vec<&str> = rest.split('/').collect();
    for (idx, seg) in segments.iter().enumerate() {
        let is_last = idx == segments.len() - 1;
        let mut next: Vec<String> = Vec::new();
        for base in &current {
            if seg.is_empty() {
                next.push(base.clone());
                continue;
            }
            if glob_has_wildcard(seg) {
                let read_base = if base.is_empty() { "." } else { base.as_str() };
                if let Ok(rd) = std::fs::read_dir(read_base) {
                    for entry in rd.flatten() {
                        let name = entry.file_name().to_string_lossy().into_owned();
                        if glob_fnmatch(seg, &name) {
                            let path = glob_join(base, &name);
                            if is_last || std::path::Path::new(&path).is_dir() {
                                next.push(path);
                            }
                        }
                    }
                }
            } else {
                let path = glob_join(base, seg);
                let p = std::path::Path::new(&path);
                if (is_last && p.exists()) || (!is_last && p.is_dir()) {
                    next.push(path);
                }
            }
        }
        current = next;
    }
    for mut path in current {
        let is_dir = std::path::Path::new(&path).is_dir();
        if flags & GLOB_ONLYDIR != 0 && !is_dir {
            continue;
        }
        if flags & GLOB_MARK != 0 && is_dir && !path.ends_with('/') {
            path.push('/');
        }
        out.push(path);
    }
}

fn glob_join(base: &str, seg: &str) -> String {
    if base.is_empty() {
        seg.to_string()
    } else if base == "/" {
        format!("/{}", seg)
    } else {
        format!("{}/{}", base, seg)
    }
}

fn glob_has_wildcard(seg: &str) -> bool {
    seg.bytes().any(|b| matches!(b, b'*' | b'?' | b'['))
}

fn glob_fnmatch(pattern: &str, name: &str) -> bool {
    // A leading '.' is only matched by an explicit leading '.' in the pattern.
    if name.starts_with('.') && !pattern.starts_with('.') {
        return false;
    }
    glob_fnmatch_bytes(pattern.as_bytes(), name.as_bytes())
}

fn glob_fnmatch_bytes(p: &[u8], s: &[u8]) -> bool {
    let mut pi = 0;
    let mut si = 0;
    let mut star: Option<usize> = None;
    let mut star_s = 0;
    while si < s.len() {
        if pi < p.len() {
            match p[pi] {
                b'*' => {
                    star = Some(pi);
                    star_s = si;
                    pi += 1;
                    continue;
                }
                b'?' => {
                    pi += 1;
                    si += 1;
                    continue;
                }
                b'[' => {
                    if let Some((matched, next_pi)) = glob_match_bracket(p, pi, s[si]) {
                        if matched {
                            pi = next_pi;
                            si += 1;
                            continue;
                        }
                    } else if p[pi] == s[si] {
                        // Unterminated '[' is treated as a literal.
                        pi += 1;
                        si += 1;
                        continue;
                    }
                }
                c => {
                    if c == s[si] {
                        pi += 1;
                        si += 1;
                        continue;
                    }
                }
            }
        }
        if let Some(sp) = star {
            pi = sp + 1;
            star_s += 1;
            si = star_s;
        } else {
            return false;
        }
    }
    while pi < p.len() && p[pi] == b'*' {
        pi += 1;
    }
    pi == p.len()
}

// Returns (matched, index-after-']') for a `[...]` class, or None when the bracket is unterminated.
fn glob_match_bracket(p: &[u8], start: usize, c: u8) -> Option<(bool, usize)> {
    let mut i = start + 1;
    if i >= p.len() {
        return None;
    }
    let negate = p[i] == b'!' || p[i] == b'^';
    if negate {
        i += 1;
    }
    let mut matched = false;
    let mut first = true;
    while i < p.len() {
        if p[i] == b']' && !first {
            return Some((matched ^ negate, i + 1));
        }
        first = false;
        if i + 2 < p.len() && p[i + 1] == b'-' && p[i + 2] != b']' {
            if p[i] <= c && c <= p[i + 2] {
                matched = true;
            }
            i += 3;
        } else {
            if p[i] == c {
                matched = true;
            }
            i += 1;
        }
    }
    None
}

fn glob_expand_braces(pattern: &str) -> Vec<String> {
    let bytes = pattern.as_bytes();
    let Some(open) = pattern.find('{') else {
        return vec![pattern.to_string()];
    };
    // Find the matching '}'.
    let mut depth = 0;
    let mut close = None;
    for (i, &b) in bytes.iter().enumerate().skip(open) {
        match b {
            b'{' => depth += 1,
            b'}' => {
                depth -= 1;
                if depth == 0 {
                    close = Some(i);
                    break;
                }
            }
            _ => {}
        }
    }
    let Some(close) = close else {
        return vec![pattern.to_string()];
    };
    let prefix = &pattern[..open];
    let suffix = &pattern[close + 1..];
    let inner = &pattern[open + 1..close];
    let mut result = Vec::new();
    for alt in glob_split_top_commas(inner) {
        let combined = format!("{}{}{}", prefix, alt, suffix);
        result.extend(glob_expand_braces(&combined));
    }
    result
}

fn glob_split_top_commas(inner: &str) -> Vec<String> {
    let mut parts = Vec::new();
    let mut depth = 0;
    let mut start = 0;
    let bytes = inner.as_bytes();
    for (i, &b) in bytes.iter().enumerate() {
        match b {
            b'{' => depth += 1,
            b'}' => depth -= 1,
            b',' if depth == 0 => {
                parts.push(inner[start..i].to_string());
                start = i + 1;
            }
            _ => {}
        }
    }
    parts.push(inner[start..].to_string());
    parts
}