aboutsummaryrefslogtreecommitdiff
path: root/perl/example.pl
blob: 257f2c9cb1d0d0959457787377fdc9094bf5d2fb (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
#!/usr/bin/perl

use Fuse;
use POSIX qw(ENOENT EISDIR EINVAL);

my (%files) = (
	'.' => {
		type => 0040,
		mode => 0755,
		ctime => time()-1000
	},
	a => {
		cont => "File 'a'.\n",
		type => 0100,
		mode => 0755,
		ctime => time()-2000
	},
	b => {
		cont => "This is file 'b'.\n",
		type => 0100,
		mode => 0644,
		ctime => time()-1000
	},
);

sub filename_fixup {
	my ($file) = shift;
	$file =~ s,^/,,;
	$file = '.' unless length($file);
	return $file;
}

sub e_getattr {
	my ($file) = filename_fixup(shift);
	$file =~ s,^/,,;
	$file = '.' unless length($file);
	return -ENOENT() unless exists($files{$file});
	my ($size) = exists($files{$file}{cont}) ? length($files{$file}{cont}) : 0;
	my ($modes) = ($files{$file}{type}<<9) + $files{$file}{mode};
	my ($blocks, $gid, $uid, $nlink) = (1,0,0,1);
	# 4 possible return values:
	#return -ENOENT(); # or any other error you care to
	return ($blocks,$size,$gid,$uid,$nlink,$modes,$files{$file}{ctime});
	# return ($errno,$blocks,$size,$gid,$uid,$nlink,$modes,$time);
	# return ($errno,$blksize,$blocks,$size,$gid,$uid,$nlink,$modes,$time);
	# if omitted, errno defaults to 0, and blksize defaults to 1024.
}

sub e_getdir {
	# return as many text filenames as you like, followed by the retval.
	return (keys %files),0;
}

sub e_open {
	# VFS sanity check; it keeps all the necessary state, not much to do here.
	my ($file) = filename_fixup(shift);
	return -ENOENT() unless exists($files{$file});
	return -EISDIR() unless exists($files{$file}{cont});
	return 0;
}

sub e_read {
	# return an error numeric, or binary/text string.  (note: 0 means EOF, "0" will
	# give a byte (ascii "0") to the reading program)
	my ($file) = filename_fixup(shift);
	my ($buf,$off) = @_;
	return -ENOENT() unless exists($files{$file});
	return -EINVAL() if $off > length($files{$file}{cont});
	return 0 if $off == length($files{$file}{cont});
	return substr($files{$file}{cont},$off,$buf);
}

# If you run the script directly, it will run fusermount, which will in turn
# re-run this script.  Hence the funky semantics.
my ($mountpoint) = "";
$mountpoint = shift(@ARGV) if @ARGV;
Fuse::main(
	mountpoint=>$mountpoint,
	getattr=>\&e_getattr,
	getdir=>\&e_getdir,
	open=>\&e_open,
	read=>\&e_read,
);