Remove some debug output that's not needed anymore.
[perl-fuse.git] / examples / fioc.pl
1 #!/usr/bin/env perl
2
3 use strict;
4 no strict qw(refs);
5
6 use Carp ();
7 local $SIG{'__WARN__'} = \&Carp::cluck;
8
9 use Fuse;
10 use Fcntl qw(:mode);
11 use Errno qw(:POSIX);
12 use POSIX;
13
14 my $fioc_size = 0;
15 use constant FIOC_NAME => 'fioc';
16 my $fioc_buf = '';
17 use constant FIOC_NONE  => 0;
18 use constant FIOC_ROOT  => 1;
19 use constant FIOC_FILE  => 2;
20
21 require 'asm/ioctl.ph';
22
23 our %sizeof = ('int' => 4);
24 sub FIOC_GET_SIZE { _IOR(ord 'E', 0, 'int'); }
25 sub FIOC_SET_SIZE { _IOW(ord 'E', 1, 'int'); }
26
27 sub fioc_resize {
28     my ($size) = @_;
29     print 'called ', (caller(0))[3], "\n";
30     return 0 if $size == $fioc_size;
31     
32     if ($size < $fioc_size) {
33         $fioc_buf = substr($fioc_buf, 0, $size);
34     }
35     else {
36         $fioc_buf .= "\0" x ($size - $fioc_size);
37     }
38     $fioc_size = $size;
39     return 0;
40 }
41
42 sub fioc_expand {
43     my ($size) = @_;
44     print 'called ', (caller(0))[3], "\n";
45     if ($size > $fioc_size) {
46         return fioc_resize($size);
47     }
48     return 0;
49 }
50
51 sub fioc_file_type {
52     my ($path) = @_;
53     print 'called ', (caller(0))[3], "\n";
54     return FIOC_ROOT if $path eq '/';
55     return FIOC_FILE if $path eq '/' . FIOC_NAME;
56     return FIOC_NONE;
57 }
58
59 sub fioc_getattr {
60     my ($path) = @_;
61     print 'called ', (caller(0))[3], "\n";
62     my @stbuf = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
63
64     $stbuf[4] = $<;
65     $stbuf[5] = (split(/\s+/, $())[0];
66     $stbuf[8] = $stbuf[9] = time();
67
68     my $type = fioc_file_type($path);
69     if ($type == FIOC_ROOT) {
70         $stbuf[2] = S_IFDIR | 0755;
71         $stbuf[3] = 2;
72     }
73     elsif ($type == FIOC_FILE) {
74         $stbuf[2] = S_IFREG | 0644;
75         $stbuf[3] = 1;
76         $stbuf[7] = $fioc_size;
77     }
78     else {
79         return -&ENOENT;
80     }
81     return @stbuf;
82 }
83
84 sub fioc_open {
85     my ($path, $flags, $info) = @_;
86     print 'called ', (caller(0))[3], "\n";
87
88     if (fioc_file_type($path) != FIOC_NONE) {
89         return 0;
90     }
91     return -&ENOENT;
92 }
93
94 sub fioc_read {
95     my ($path, $size, $offset) = @_;
96     print 'called ', (caller(0))[3], "\n";
97
98     return -&EINVAL if fioc_file_type($path) != FIOC_FILE;
99
100     if ($offset > $fioc_size) {
101         return q{};
102     }
103
104     if ($size > $fioc_size - $offset) {
105         $size - $fioc_size - $offset;
106     }
107
108     return substr($fioc_buf, $offset, $size);
109 }
110
111 sub fioc_write {
112     my ($path, $data, $offset) = @_;
113     print 'called ', (caller(0))[3], "\n";
114
115     return -&EINVAL if fioc_file_type($path) != FIOC_FILE;
116
117     if (fioc_expand($offset + length($data))) {
118         return -&ENOMEM;
119     }
120
121     substr($fioc_buf, $offset, length($data), $data);
122     return length($data);
123 }
124
125 sub fioc_truncate {
126     my ($path, $size) = @_;
127     print 'called ', (caller(0))[3], "\n";
128
129     return -&EINVAL if fioc_file_type($path) != FIOC_FILE;
130
131     return fioc_resize($size);
132 }
133
134 sub fioc_readdir {
135     my ($path, $offset) = @_;
136     print 'called ', (caller(0))[3], "\n";
137
138     return -&EINVAL if fioc_file_type($path) != FIOC_ROOT;
139
140     return ('.', '..', FIOC_NAME, 0);
141 }
142
143 sub fioc_ioctl {
144     my ($path, $cmd, $flags, $data) = @_;
145     print 'called ', (caller(0))[3], "\n";
146     $cmd = unpack('L', pack('l', $cmd));
147
148     return -&EINVAL if fioc_file_type($path) != FIOC_FILE;
149
150     return -&ENOSYS if $flags & 0x1;
151
152     if ($cmd == FIOC_GET_SIZE) {
153         return(0, pack('L', $fioc_size));
154     }
155     elsif ($cmd == FIOC_SET_SIZE) {
156         fioc_resize(unpack('L', $data));
157         return 0;
158     }
159
160     return -&EINVAL;
161 }
162
163 croak("Fuse doesn't have ioctl") unless Fuse::fuse_version() >= 2.8;
164
165 Fuse::main(
166     'mountpoint' => $ARGV[0],
167     'getattr'   => 'main::fioc_getattr',
168     'readdir'   => 'main::fioc_readdir',
169     'truncate'  => 'main::fioc_truncate',
170     'open'      => 'main::fioc_open',
171     'read'      => 'main::fioc_read',
172     'write'     => 'main::fioc_write',
173     'ioctl'     => 'main::fioc_ioctl');