Skip to content

Commit 90f3f80

Browse files
committed
use the filesystem abstraction to check if a file/directory exists
1 parent 640df0e commit 90f3f80

5 files changed

Lines changed: 42 additions & 9 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
- Requires PHP `8.4`
88
- `Innmind\OperatingSystem\Remote::sql()` now returns an `Innmind\Immutable\Attempt`
9+
- `Innmind\OperatingSystem\Filesystem::mount()` now longer automatically create the directory
910

1011
## 6.2.0 - 2025-08-10
1112

src/Config.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Innmind\Filesystem\{
1111
Adapter as Filesystem,
1212
CaseSensitivity,
13+
Exception\RecoverMount,
1314
};
1415
use Innmind\FileWatch\Watch;
1516
use Innmind\Server\Status\EnvironmentPath;
@@ -595,9 +596,21 @@ public function clock(): Clock
595596
#[\NoDiscard]
596597
public function filesystem(Path $path): Attempt
597598
{
598-
return ($this->filesystem)($path, $this)->map(
599-
fn($adapter) => ($this->mapFilesystem)($adapter, $this),
599+
$map = fn(Filesystem $adapter): Filesystem => ($this->mapFilesystem)(
600+
$adapter,
601+
$this,
600602
);
603+
604+
return ($this->filesystem)($path, $this)
605+
->mapError(static fn($e) => match (true) {
606+
$e instanceof RecoverMount => new RecoverMount(
607+
static fn() => $e
608+
->recover()
609+
->map($map),
610+
),
611+
default => $e,
612+
})
613+
->map($map);
601614
}
602615

603616
/**

src/Filesystem.php

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
use Innmind\Filesystem\{
77
Adapter,
88
File\Content,
9+
Directory,
10+
Name,
911
};
1012
use Innmind\Url\Path;
1113
use Innmind\Server\Control\Server\Processes;
@@ -76,16 +78,28 @@ public function mount(Path $path): Attempt
7678
#[\NoDiscard]
7779
public function contains(Path $path): bool
7880
{
79-
// todo find a way to not directly access the filesystem
80-
if (!\file_exists($path->toString())) {
81-
return false;
82-
}
81+
$dir = \rtrim(\dirname($path->toString()), '/').'/';
82+
$name = \basename($path->toString());
8383

84-
if ($path->directory() && !\is_dir($path->toString())) {
85-
return false;
84+
$exists = $this
85+
->config
86+
->filesystem(Path::of($dir))
87+
->maybe();
88+
89+
// empty when the path === '/'
90+
if ($name !== '') {
91+
$exists = $exists
92+
->flatMap(static fn($adapter) => $adapter->get(Name::of($name)))
93+
->filter(static fn($file) => match (true) {
94+
$path->directory() && !($file instanceof Directory) => false,
95+
default => true,
96+
});
8697
}
8798

88-
return true;
99+
return $exists->match(
100+
static fn() => true,
101+
static fn() => false,
102+
);
89103
}
90104

91105
/**

tests/FactoryTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
File\Content,
1616
Directory,
1717
CaseSensitivity,
18+
Recover,
1819
};
1920
use Innmind\Url\Path;
2021
use Symfony\Component\Filesystem\Filesystem as FS;
@@ -62,6 +63,7 @@ public function testPersistingFileOnCaseInsensitiveFilesystem()
6263
$adapter = $os
6364
->filesystem()
6465
->mount(Path::of($path))
66+
->recover(Recover::mount(...))
6567
->unwrap();
6668
$adapter->add(
6769
$directory = Directory::named('0')

tests/FilesystemTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ public function testContainsDirectory()
7979
\mkdir('/tmp/some-dir/');
8080
$this->assertTrue($filesystem->contains(Path::of('/tmp/some-dir/')));
8181
\rmdir('/tmp/some-dir/');
82+
\file_put_contents('/tmp/foo', 'data');
83+
$this->assertFalse($filesystem->contains(Path::of('/tmp/foo/')));
84+
\unlink('/tmp/foo');
8285
}
8386

8487
public function testWatch()

0 commit comments

Comments
 (0)