DummyFs::getattr()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 15
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 22
rs 9.7666
1
<?php
2
3
/**
4
 * This file is part of the sj-i/php-fuse package.
5
 *
6
 * (c) sji <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
use Fuse\FilesystemDefaultImplementationTrait;
15
use Fuse\FilesystemInterface;
16
use Fuse\Libc\Errno\Errno;
17
use Fuse\Libc\Fuse\FuseFileInfo;
18
use Fuse\Libc\Fuse\FuseFillDir;
19
use Fuse\Libc\Fuse\FuseReadDirBuffer;
20
use Fuse\Libc\String\CBytesBuffer;
21
use Fuse\Libc\Sys\Stat\Stat;
22
23
class DummyFs implements FilesystemInterface
24
{
25
    use FilesystemDefaultImplementationTrait;
26
27
    const FILE_PATH = '/example';
28
    const FILE_NAME = 'example';
29
    const FILE_CONTENT = 'hello FUSE from PHP' . PHP_EOL;
30
31
    public function getattr(string $path, Stat $stat): int
32
    {
33
        echo "attr read {$path}" . PHP_EOL;
34
35
        if ($path === '/') {
36
            $stat->st_mode = Stat::S_IFDIR | 0755;
37
            $stat->st_nlink = 2;
38
            $stat->st_uid = getmyuid();
39
            $stat->st_gid = getmygid();
40
            return 0;
41
        }
42
43
        if ($path === self::FILE_PATH) {
44
            $stat->st_mode = Stat::S_IFREG | 0777;
45
            $stat->st_nlink = 1;
46
            $stat->st_size = strlen(self::FILE_CONTENT);
47
            $stat->st_uid = getmyuid();
48
            $stat->st_gid = getmygid();
49
            return 0;
50
        }
51
52
        return -Errno::ENOENT;
53
    }
54
55
    public function readdir(
56
        string $path,
57
        FuseReadDirBuffer $buf,
58
        FuseFillDir $filler,
59
        int $offset,
60
        FuseFileInfo $fuse_file_info
61
    ): int {
62
        $filler($buf, '.', null, 0);
63
        $filler($buf, '..', null, 0);
64
        $filler($buf, self::FILE_NAME, null, 0);
65
66
        return 0;
67
    }
68
69
    public function open(string $path, FuseFileInfo $fuse_file_info): int
70
    {
71
        echo "open {$path}" . PHP_EOL;
72
73
        if ($path !== self::FILE_PATH) {
74
            return -Errno::ENOENT;
75
        }
76
77
        return 0;
78
    }
79
80
    public function read(string $path, CBytesBuffer $buffer, int $size, int $offset, FuseFileInfo $fuse_file_info): int
81
    {
82
        echo "read {$path}" . PHP_EOL;
83
84
        $len = strlen(self::FILE_CONTENT);
85
86
        if ($offset + $size > $len) {
87
            $size = ($len - $offset);
88
        }
89
90
        $content = substr(self::FILE_CONTENT, $offset, $size);
91
        $buffer->write($content, $size);
92
93
        return $size;
94
    }
95
}
96