pathkit 1.2.0

Similar to the Path structure provided by python's pathlib, it provides various async/sync versions of file manipulation methods in addition to some of the std::Path built-in methods.
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
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
//! Synchronous file system operations module
//!
//! This module provides the `SyncFsOps` trait for synchronous file system operations.
//! Implement this trait on any type to provide blocking file operations.
//!
//! # Example
//!
//! ```rust,ignore
//! use pathkit::{Path, SyncFsOps};
//!
//! let path = Path::new("/tmp/test.txt");
//! path.write_sync(b"Hello!")?;
//! let content = path.read_sync()?;
//! ```

use std::{
    fs::{
        self,
        File,
        Metadata,
        OpenOptions,
        Permissions,
        ReadDir,
    },
    time::SystemTime,
};

use anyhow::Result;
use filetime::{
    set_file_mtime,
    FileTime,
};
use serde::{
    de::DeserializeOwned,
    Serialize,
};
use serde_json::{
    from_slice,
    to_vec_pretty,
};

use super::core::Path;

/// Trait for synchronous file system operations.
///
/// This trait provides blocking file system operations similar to Python's pathlib.
/// It is implemented for `Path` but can be implemented for other types as well.
///
/// # Example
///
/// ```rust,ignore
/// use pathkit::{Path, SyncFsOps};
///
/// let path = Path::new("/tmp/test.txt");
///
/// // Check if file exists
/// if path.exists_sync()? {
///     // Read file contents
///     let content = path.read_sync()?;
/// }
///
/// // Write to file
/// path.write_sync(b"Hello, world!")?;
///
/// // Get file size
/// let size = path.get_file_size_sync()?;
/// ```
pub trait SyncFsOps {
    #[cfg(unix)]
    fn chmod_sync(&self, mode: u32) -> Result<()>;
    #[cfg(unix)]
    fn chown_sync(&self, uid: Option<u32>, gid: Option<u32>) -> Result<()>;
    fn copy_file_sync(&self, dest: impl AsRef<Path>) -> Result<u64>;
    fn create_dir_all_sync(&self) -> Result<()>;
    fn create_dir_sync(&self) -> Result<()>;
    fn create_parent_dir_all_sync(&self) -> Result<bool>;
    fn create_parent_dir_sync(&self) -> Result<bool>;
    fn empty_dir_sync(&self) -> Result<()>;
    fn exists_sync(&self) -> Result<bool>;
    fn get_file_size_sync(&self) -> Result<u64>;
    fn hard_link_sync(&self, link: impl AsRef<Path>) -> Result<()>;
    #[cfg(unix)]
    fn is_block_device_sync(&self) -> Result<bool>;
    #[cfg(unix)]
    fn is_char_device_sync(&self) -> Result<bool>;
    fn is_dir_sync(&self) -> Result<bool>;
    #[cfg(unix)]
    fn is_fifo_sync(&self) -> Result<bool>;
    fn is_file_sync(&self) -> Result<bool>;
    #[cfg(unix)]
    fn is_socket_sync(&self) -> Result<bool>;
    fn is_symlink_sync(&self) -> Result<bool>;
    fn metadata_sync(&self) -> Result<Metadata>;
    fn read_dir_names_sync(&self) -> Result<Vec<String>>;
    fn read_dir_paths_sync(&self) -> Result<Vec<Path>>;
    fn read_dir_sync(&self) -> Result<ReadDir>;
    fn read_json_sync<T: DeserializeOwned>(&self) -> Result<T>;
    #[cfg(unix)]
    fn read_link_sync(&self) -> Result<Path>;
    fn read_sync(&self) -> Result<Vec<u8>>;
    fn read_to_string_sync(&self) -> Result<String>;
    fn remove_dir_all_sync(&self) -> Result<()>;
    fn remove_dir_sync(&self) -> Result<()>;
    fn remove_file_sync(&self) -> Result<()>;
    fn set_permissions_sync(&self, permissions: Permissions) -> Result<()>;
    #[cfg(unix)]
    fn soft_link_sync(&self, link: impl AsRef<Path>) -> Result<()>;
    fn symlink_metadata_sync(&self) -> Result<Metadata>;
    fn touch_sync(&self) -> Result<()>;
    fn truncate_sync(&self, len: Option<u64>) -> Result<()>;
    fn write_json_sync<T: Serialize>(&self, data: T) -> Result<()>;
    fn write_sync(&self, contents: impl AsRef<[u8]>) -> Result<()>;
}

impl SyncFsOps for Path {
    #[cfg(unix)]
    fn chmod_sync(&self, mode: u32) -> Result<()> {
        use std::os::unix::fs::PermissionsExt;

        Ok(fs::set_permissions(self, Permissions::from_mode(mode))?)
    }

    #[cfg(unix)]
    fn chown_sync(&self, uid: Option<u32>, gid: Option<u32>) -> Result<()> {
        Ok(std::os::unix::fs::chown(self, uid, gid)?)
    }

    fn copy_file_sync(&self, dest: impl AsRef<Path>) -> Result<u64> {
        Ok(fs::copy(self, dest.as_ref())?)
    }

    fn create_dir_all_sync(&self) -> Result<()> {
        Ok(fs::create_dir_all(self)?)
    }

    fn create_dir_sync(&self) -> Result<()> {
        Ok(fs::create_dir(self)?)
    }

    fn create_parent_dir_all_sync(&self) -> Result<bool> {
        if let Some(parent) = self.parent() {
            parent.create_dir_all_sync()?;
            return Ok(true);
        }

        Ok(false)
    }

    fn create_parent_dir_sync(&self) -> Result<bool> {
        if let Some(parent) = self.parent() {
            parent.create_dir_sync()?;
            return Ok(true);
        }

        Ok(false)
    }

    fn empty_dir_sync(&self) -> Result<()> {
        if !self.exists_sync()? {
            return self.create_dir_all_sync();
        }

        for entry in fs::read_dir(self)? {
            let entry_path = entry?.path();
            if entry_path.is_dir() {
                fs::remove_dir_all(entry_path)?;
            } else {
                fs::remove_file(entry_path)?;
            }
        }

        Ok(())
    }

    fn exists_sync(&self) -> Result<bool> {
        Ok(self.try_exists()?)
    }

    fn get_file_size_sync(&self) -> Result<u64> {
        Ok(self.metadata_sync()?.len())
    }

    fn hard_link_sync(&self, link: impl AsRef<Path>) -> Result<()> {
        Ok(fs::hard_link(self, link.as_ref())?)
    }

    #[cfg(unix)]
    fn is_block_device_sync(&self) -> Result<bool> {
        use std::os::unix::fs::FileTypeExt;

        Ok(self.metadata_sync()?.file_type().is_block_device())
    }

    #[cfg(unix)]
    fn is_char_device_sync(&self) -> Result<bool> {
        use std::os::unix::fs::FileTypeExt;

        Ok(self.metadata_sync()?.file_type().is_char_device())
    }

    fn is_dir_sync(&self) -> Result<bool> {
        Ok(self.metadata_sync()?.is_dir())
    }

    #[cfg(unix)]
    fn is_fifo_sync(&self) -> Result<bool> {
        use std::os::unix::fs::FileTypeExt;

        Ok(self.metadata_sync()?.file_type().is_fifo())
    }

    fn is_file_sync(&self) -> Result<bool> {
        Ok(self.metadata_sync()?.is_file())
    }

    #[cfg(unix)]
    fn is_socket_sync(&self) -> Result<bool> {
        use std::os::unix::fs::FileTypeExt;

        Ok(self.metadata_sync()?.file_type().is_socket())
    }

    fn is_symlink_sync(&self) -> Result<bool> {
        Ok(fs::symlink_metadata(self)?.file_type().is_symlink())
    }

    fn metadata_sync(&self) -> Result<Metadata> {
        Ok(fs::metadata(self)?)
    }

    fn read_dir_names_sync(&self) -> Result<Vec<String>> {
        let mut names = Vec::new();
        for entry in fs::read_dir(self)? {
            names.push(entry?.file_name().to_string_lossy().into());
        }

        Ok(names)
    }

    fn read_dir_paths_sync(&self) -> Result<Vec<Path>> {
        let mut paths = Vec::new();
        for entry in fs::read_dir(self)? {
            paths.push(Self::new(entry?.path()));
        }

        Ok(paths)
    }

    fn read_dir_sync(&self) -> Result<ReadDir> {
        Ok(fs::read_dir(self)?)
    }

    fn read_json_sync<T: DeserializeOwned>(&self) -> Result<T> {
        Ok(from_slice::<T>(&self.read_sync()?)?)
    }

    #[cfg(unix)]
    fn read_link_sync(&self) -> Result<Path> {
        Ok(Self::new(fs::read_link(self)?))
    }

    fn read_sync(&self) -> Result<Vec<u8>> {
        Ok(fs::read(self)?)
    }

    fn read_to_string_sync(&self) -> Result<String> {
        Ok(fs::read_to_string(self)?)
    }

    fn remove_dir_all_sync(&self) -> Result<()> {
        Ok(fs::remove_dir_all(self)?)
    }

    fn remove_dir_sync(&self) -> Result<()> {
        Ok(fs::remove_dir(self)?)
    }

    fn remove_file_sync(&self) -> Result<()> {
        Ok(fs::remove_file(self)?)
    }

    fn set_permissions_sync(&self, permissions: Permissions) -> Result<()> {
        Ok(fs::set_permissions(self, permissions)?)
    }

    #[cfg(unix)]
    fn soft_link_sync(&self, link: impl AsRef<Path>) -> Result<()> {
        use std::os::unix::fs::symlink;

        Ok(symlink(self, link.as_ref())?)
    }

    fn symlink_metadata_sync(&self) -> Result<Metadata> {
        Ok(fs::symlink_metadata(self)?)
    }

    fn touch_sync(&self) -> Result<()> {
        if self.exists_sync()? {
            let t = SystemTime::now();
            set_file_mtime(self, FileTime::from_system_time(t))?;
        } else {
            File::create(self)?;
        }

        Ok(())
    }

    fn truncate_sync(&self, len: Option<u64>) -> Result<()> {
        Ok(OpenOptions::new().write(true).open(self)?.set_len(len.unwrap_or(0))?)
    }

    fn write_json_sync<T: Serialize>(&self, data: T) -> Result<()> {
        self.write_sync(to_vec_pretty(&data)?)
    }

    fn write_sync(&self, contents: impl AsRef<[u8]>) -> Result<()> {
        Ok(fs::write(self, contents)?)
    }
}

#[cfg(test)]
mod tests {
    use serde::Deserialize;
    use tempfile::{
        tempdir,
        NamedTempFile,
    };

    use super::*;

    // Test exists_sync
    #[test]
    fn test_exists_sync() -> Result<()> {
        let temp_file = NamedTempFile::new()?;
        let file_path = Path::new(temp_file.path());

        assert!(file_path.exists_sync()?);
        Ok(())
    }

    #[test]
    fn test_exists_sync_false() -> Result<()> {
        let temp_dir = tempdir()?;
        let non_existent = temp_dir.path().join("non_existent_file.txt");
        let path = Path::new(&non_existent);

        assert!(!path.exists_sync()?);
        Ok(())
    }

    // Test is_file_sync
    #[test]
    fn test_is_file_sync() -> Result<()> {
        let temp_file = NamedTempFile::new()?;
        let file_path = Path::new(temp_file.path());

        assert!(file_path.is_file_sync()?);
        Ok(())
    }

    #[test]
    fn test_is_file_sync_false() -> Result<()> {
        let temp_dir = tempdir()?;
        let dir_path = Path::new(temp_dir.path());

        assert!(!dir_path.is_file_sync()?);
        Ok(())
    }

    // Test is_dir_sync
    #[test]
    fn test_is_dir_sync() -> Result<()> {
        let temp_dir = tempdir()?;
        let dir_path = Path::new(temp_dir.path());

        assert!(dir_path.is_dir_sync()?);
        Ok(())
    }

    #[test]
    fn test_is_dir_sync_false() -> Result<()> {
        let temp_file = NamedTempFile::new()?;
        let file_path = Path::new(temp_file.path());

        assert!(!file_path.is_dir_sync()?);
        Ok(())
    }

    // Test is_symlink_sync
    #[cfg(unix)]
    #[test]
    fn test_is_symlink_sync() -> Result<()> {
        let temp_dir = tempdir()?;
        let target = temp_dir.path().join("target.txt");
        fs::write(&target, "test")?;

        let link = temp_dir.path().join("link.txt");
        #[cfg(unix)]
        std::os::unix::fs::symlink(&target, &link)?;

        let link_path = Path::new(&link);
        assert!(link_path.is_symlink_sync()?);
        Ok(())
    }

    // Test metadata_sync
    #[test]
    fn test_metadata_sync() -> Result<()> {
        let temp_file = NamedTempFile::new()?;
        let file_path = Path::new(temp_file.path());

        let metadata = file_path.metadata_sync()?;
        assert!(metadata.is_file());
        Ok(())
    }

    // Test read_sync and write_sync
    #[test]
    fn test_read_write_sync() -> Result<()> {
        let temp_file = NamedTempFile::new()?;
        let file_path = Path::new(temp_file.path());

        let test_content = b"Hello, World!";
        file_path.write_sync(test_content)?;

        let read_content = file_path.read_sync()?;
        assert_eq!(read_content, test_content);
        Ok(())
    }

    // Test read_to_string_sync
    #[test]
    fn test_read_to_string_sync() -> Result<()> {
        let temp_file = NamedTempFile::new()?;
        let file_path = Path::new(temp_file.path());

        let test_content = "Hello, World!";
        file_path.write_sync(test_content)?;

        let read_content = file_path.read_to_string_sync()?;
        assert_eq!(read_content, test_content);
        Ok(())
    }

    // Test create_dir_sync
    #[test]
    fn test_create_dir_sync() -> Result<()> {
        let temp_dir = tempdir()?;
        let new_dir = temp_dir.path().join("new_dir");
        let dir_path = Path::new(&new_dir);

        dir_path.create_dir_sync()?;

        assert!(dir_path.is_dir_sync()?);
        Ok(())
    }

    // Test create_dir_all_sync
    #[test]
    fn test_create_dir_all_sync() -> Result<()> {
        let temp_dir = tempdir()?;
        let new_dir = temp_dir.path().join("parent/child/grandchild");
        let dir_path = Path::new(&new_dir);

        dir_path.create_dir_all_sync()?;

        assert!(dir_path.is_dir_sync()?);
        Ok(())
    }

    // Test remove_dir_sync
    #[test]
    fn test_remove_dir_sync() -> Result<()> {
        let temp_dir = tempdir()?;
        let new_dir = temp_dir.path().join("to_remove");
        fs::create_dir(&new_dir)?;
        let dir_path = Path::new(&new_dir);

        assert!(dir_path.exists_sync()?);
        dir_path.remove_dir_sync()?;
        assert!(!dir_path.exists_sync()?);
        Ok(())
    }

    // Test remove_file_sync
    #[test]
    fn test_remove_file_sync() -> Result<()> {
        let temp_file = NamedTempFile::new()?;
        let file_path = Path::new(temp_file.path());

        assert!(file_path.exists_sync()?);
        file_path.remove_file_sync()?;
        assert!(!file_path.exists_sync()?);
        Ok(())
    }

    // Test remove_dir_all_sync
    #[test]
    fn test_remove_dir_all_sync() -> Result<()> {
        let temp_dir = tempdir()?;
        let parent = temp_dir.path().join("parent");
        fs::create_dir(&parent)?;
        fs::write(parent.join("file1.txt"), "content1")?;
        fs::write(parent.join("file2.txt"), "content2")?;

        let dir_path = Path::new(&parent);
        assert!(dir_path.exists_sync()?);
        dir_path.remove_dir_all_sync()?;
        assert!(!dir_path.exists_sync()?);
        Ok(())
    }

    // Test get_file_size_sync
    #[test]
    fn test_get_file_size_sync() -> Result<()> {
        let temp_file = NamedTempFile::new()?;
        let file_path = Path::new(temp_file.path());

        let test_content = b"Hello, World!";
        file_path.write_sync(test_content)?;

        let size = file_path.get_file_size_sync()?;
        assert_eq!(size, test_content.len() as u64);
        Ok(())
    }

    // Test truncate_sync
    #[test]
    fn test_truncate_sync() -> Result<()> {
        let temp_file = NamedTempFile::new()?;
        let file_path = Path::new(temp_file.path());

        let test_content = b"Hello, World!";
        file_path.write_sync(test_content)?;

        // Truncate to 5 bytes
        file_path.truncate_sync(Some(5))?;

        let size = file_path.get_file_size_sync()?;
        assert_eq!(size, 5);
        Ok(())
    }

    // Test read_json_sync and write_json_sync
    #[test]
    fn test_read_write_json_sync() -> Result<()> {
        #[derive(Debug, Serialize, Deserialize, PartialEq)]
        struct TestData {
            name: String,
            value: i32,
        }

        let temp_file = NamedTempFile::new()?;
        let file_path = Path::new(temp_file.path());

        let original = TestData {
            name: "test".to_string(),
            value: 42,
        };

        file_path.write_json_sync(&original)?;

        let loaded: TestData = file_path.read_json_sync()?;
        assert_eq!(loaded, original);
        Ok(())
    }

    // Test read_dir_sync
    #[test]
    fn test_read_dir_sync() -> Result<()> {
        let temp_dir = tempdir()?;
        fs::write(temp_dir.path().join("file1.txt"), "content1")?;
        fs::write(temp_dir.path().join("file2.txt"), "content2")?;
        fs::create_dir(temp_dir.path().join("subdir"))?;

        let dir_path = Path::new(temp_dir.path());
        let entries: Vec<_> = dir_path.read_dir_sync()?.collect();

        // Should have 3 entries: 2 files + 1 directory
        assert_eq!(entries.len(), 3);
        Ok(())
    }

    // Test empty_dir_sync
    #[test]
    fn test_empty_dir_sync() -> Result<()> {
        let temp_dir = tempdir()?;
        fs::write(temp_dir.path().join("file1.txt"), "content1")?;
        fs::write(temp_dir.path().join("file2.txt"), "content2")?;
        fs::create_dir(temp_dir.path().join("subdir"))?;

        let dir_path = Path::new(temp_dir.path());
        dir_path.empty_dir_sync()?;

        // Directory should be empty now
        let entries: Vec<_> = dir_path.read_dir_sync()?.collect();
        assert_eq!(entries.len(), 0);
        Ok(())
    }

    // Test set_permissions_sync
    #[cfg(unix)]
    #[test]
    fn test_set_permissions_sync() -> Result<()> {
        use std::os::unix::fs::PermissionsExt;

        let temp_file = NamedTempFile::new()?;
        let file_path = Path::new(temp_file.path());

        // Read current permissions
        let metadata = fs::metadata(temp_file.path())?;
        let original_mode = metadata.permissions().mode();

        // Set new permissions
        file_path.set_permissions_sync(fs::Permissions::from_mode(0o644))?;

        let new_metadata = fs::metadata(temp_file.path())?;
        assert_eq!(new_metadata.permissions().mode() & 0o777, 0o644);

        // Restore original
        file_path.set_permissions_sync(fs::Permissions::from_mode(original_mode))?;
        Ok(())
    }

    #[cfg(unix)]
    // Test chmod_sync
    #[test]
    fn test_chmod_sync() -> Result<()> {
        use std::os::unix::fs::PermissionsExt;

        let temp_file = NamedTempFile::new()?;
        let file_path = Path::new(temp_file.path());

        file_path.chmod_sync(0o744)?;
        let metadata = fs::metadata(temp_file.path())?;
        assert_eq!(metadata.permissions().mode() & 0o777, 0o744);

        file_path.chmod_sync(0o700)?;
        let metadata = fs::metadata(temp_file.path())?;
        assert_eq!(metadata.permissions().mode() & 0o777, 0o700);

        Ok(())
    }

    // Test chown_sync - requires root, skip if not root
    #[cfg(unix)]
    #[test]
    fn test_chown_sync() -> Result<()> {
        use std::os::unix::fs::PermissionsExt;

        // Skip if not root (chown requires root privileges)
        if unsafe { libc::geteuid() } != 0 {
            return Ok(());
        }

        let temp_file = NamedTempFile::new()?;
        let file_path = Path::new(temp_file.path());

        // Get current uid/gid
        let metadata = fs::metadata(temp_file.path())?;
        let original_mode = metadata.permissions().mode();

        // chown to same uid/gid (no-op but should work)
        file_path.chown_sync(Some(0), Some(0))?;

        // Restore permissions
        file_path.set_permissions_sync(fs::Permissions::from_mode(original_mode))?;
        Ok(())
    }

    #[cfg(unix)]
    // Test is_block_device_sync
    #[test]
    fn test_is_block_device_sync() -> Result<()> {
        let path = Path::new("/dev/sda"); // Common block device
        if path.exists_sync()? {
            // May fail if not root or device doesn't exist
            let _ = path.is_block_device_sync();
        }
        Ok(())
    }

    #[cfg(unix)]
    // Test is_char_device_sync
    #[test]
    fn test_is_char_device_sync() -> Result<()> {
        let path = Path::new("/dev/zero"); // Common char device
        if path.exists_sync()? {
            assert!(path.is_char_device_sync()?);
        }
        Ok(())
    }

    #[cfg(unix)]
    // Test is_fifo_sync - simplified
    #[test]
    fn test_is_fifo_sync() -> Result<()> {
        // FIFOs require special permissions to create
        // Just test that non-fifo returns false
        let path = Path::new("/tmp"); // This is not a fifo
        assert!(!path.is_fifo_sync()?);
        Ok(())
    }

    #[cfg(unix)]
    // Test is_socket_sync - simplified
    #[test]
    fn test_is_socket_sync() -> Result<()> {
        // Unix socket files are tricky to create and test
        let path = Path::new("/tmp"); // This is not a socket
        assert!(!path.is_socket_sync()?);
        Ok(())
    }

    // ----------------------------------------------------------------
    // Tests for previously uncovered sync_fs_ops functions
    // ----------------------------------------------------------------

    #[test]
    fn test_copy_file_sync() -> Result<()> {
        let temp_src = NamedTempFile::new()?;
        let temp_dst = NamedTempFile::new()?;
        let src = Path::new(temp_src.path());
        let dst = Path::new(temp_dst.path());

        src.write_sync(b"hello world")?;

        let bytes = src.copy_file_sync(&dst)?;
        assert_eq!(bytes, 11);

        let content = dst.read_sync()?;
        assert_eq!(content, b"hello world");
        Ok(())
    }

    #[test]
    fn test_hard_link_sync() -> Result<()> {
        let temp_file = NamedTempFile::new()?;
        let src = Path::new(temp_file.path());
        let link_path = Path::new(temp_file.path().with_extension("link"));

        src.write_sync(b"link test")?;
        src.hard_link_sync(&link_path)?;

        // Both files should have same content
        let content = fs::read(link_path.as_path())?;
        assert_eq!(content, b"link test");

        // And same inode (hard link)
        let src_meta = fs::metadata(src.as_path())?;
        let link_meta = fs::metadata(link_path.as_path())?;
        #[cfg(unix)]
        {
            use std::os::unix::fs::MetadataExt;
            assert_eq!(src_meta.ino(), link_meta.ino());
        }
        #[cfg(not(unix))]
        let _ = (src_meta, link_meta);
        Ok(())
    }

    #[cfg(unix)]
    #[test]
    fn test_soft_link_sync() -> Result<()> {
        let temp_file = NamedTempFile::new()?;
        let src = Path::new(temp_file.path());
        let link_path = Path::new(temp_file.path().with_extension("sym"));

        src.write_sync(b"symlink test")?;
        src.soft_link_sync(&link_path)?;

        // Read through symlink
        let content = fs::read(link_path.as_path())?;
        assert_eq!(content, b"symlink test");

        // Verify link_path is a symlink
        assert!(link_path.is_symlink_sync()?);
        Ok(())
    }

    #[cfg(unix)]
    #[test]
    fn test_read_link_sync() -> Result<()> {
        let temp_file = NamedTempFile::new()?;
        let src = Path::new(temp_file.path());
        let link_path = Path::new(temp_file.path().with_extension("readlink"));

        src.write_sync(b"readlink test")?;
        src.soft_link_sync(&link_path)?;

        let link_target = link_path.read_link_sync()?;
        assert_eq!(link_target.to_str(), src.to_str());

        Ok(())
    }

    #[cfg(unix)]
    #[test]
    fn test_symlink_metadata_sync() -> Result<()> {
        let temp_file = NamedTempFile::new()?;
        let src = Path::new(temp_file.path());
        let link_path = Path::new(temp_file.path().with_extension("meta"));

        src.write_sync(b"meta test")?;
        src.soft_link_sync(&link_path)?;

        // symlink_metadata gets metadata of the link itself (not the target)
        let meta = link_path.symlink_metadata_sync()?;
        assert!(meta.file_type().is_symlink());
        Ok(())
    }

    #[test]
    fn test_touch_sync_creates_new_file() -> Result<()> {
        let temp_dir = tempdir()?;
        let new_file = temp_dir.path().join("touched.txt");
        let path = Path::new(&new_file);

        assert!(!path.exists_sync()?);
        path.touch_sync()?;
        assert!(path.exists_sync()?);
        Ok(())
    }

    #[test]
    fn test_touch_sync_updates_existing() -> Result<()> {
        let temp_file = NamedTempFile::new()?;
        let path = Path::new(temp_file.path());

        // Record original mtime
        let meta_before = fs::metadata(temp_file.path())?;
        let mtime_before = meta_before.modified()?;

        // Wait a bit so mtime actually changes
        std::thread::sleep(std::time::Duration::from_millis(10));

        path.touch_sync()?;

        let meta_after = fs::metadata(temp_file.path())?;
        let mtime_after = meta_after.modified()?;
        assert!(mtime_after > mtime_before);
        Ok(())
    }

    #[test]
    fn test_read_dir_names_sync() -> Result<()> {
        let temp_dir = tempdir()?;
        fs::write(temp_dir.path().join("a.txt"), "")?;
        fs::write(temp_dir.path().join("b.txt"), "")?;
        fs::create_dir(temp_dir.path().join("subdir"))?;

        let dir = Path::new(temp_dir.path());
        let names = dir.read_dir_names_sync()?;
        assert_eq!(names.len(), 3);
        assert!(names.contains(&String::from("a.txt")));
        assert!(names.contains(&String::from("b.txt")));
        assert!(names.contains(&String::from("subdir")));
        Ok(())
    }

    #[test]
    fn test_read_dir_paths_sync() -> Result<()> {
        let temp_dir = tempdir()?;
        let file_a = temp_dir.path().join("file_a.txt");
        let file_b = temp_dir.path().join("file_b.txt");
        fs::write(&file_a, "")?;
        fs::write(&file_b, "")?;

        let dir = Path::new(temp_dir.path());
        let paths = dir.read_dir_paths_sync()?;
        assert_eq!(paths.len(), 2);
        // All returned paths should be absolute
        for p in &paths {
            assert!(p.is_absolute());
        }
        Ok(())
    }

    #[test]
    fn test_empty_dir_sync_creates_dir_if_missing() -> Result<()> {
        let temp_dir = tempdir()?;
        let new_dir = temp_dir.path().join("brand_new_dir");
        let path = Path::new(&new_dir);

        // Directory doesn't exist
        assert!(!path.exists_sync()?);

        // empty_dir_sync should create it (via create_dir_all_sync)
        path.empty_dir_sync()?;

        assert!(path.is_dir_sync()?);
        Ok(())
    }
}