~nova/todosrht-fuse

ref: 4b8efcb3c3ba13ecd3a839ea7fa693234bf0afe9 todosrht-fuse/main.pl -rw-r--r-- 6.9 KiB
4b8efcb3 — Novalinium Initial commit, ls only, no read implemented 2 years ago
                                                                                
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
#!/usr/bin/perl -w
use strict;
use warnings;

use local::lib '.';

use Data::Dumper;

#use blib;
use Fuse qw(fuse_get_context);
use Mojo::DOM;
use POSIX qw(ENOENT EISDIR EINVAL);
use Time::Piece;
use User::pwent;
use WWW::Mechanize;

# TODO config file
my $CACHE_TTL = 1000;
my $BASE_URL = "https://todo.sr.ht";
my $TRACKER_USER = "nova";
my $TRACKER_NAME = "fletcher";

my $agent = WWW::Mechanize->new( agent => 'Yssaringintinka/1.0' );

my (%tracker_cache) = (
    '0' => {
        title => 'No tickets loaded',
        content => 'See title.',
        labels => [],
        submitter => 'System',
		ctime => time()-$CACHE_TTL-1,
        status => 'OPEN',
		type => 0100,
		mode => 0755,
    },
);

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
	},
	me => {
		size => 45,
		type => 0100,
		mode => 0644,
		ctime => time()-1000
	},
);

sub refresh_tracker_cache() {
    if ((time() - $CACHE_TTL) > $tracker_cache{0}{ctime}) {
        # refresh cache
        $tracker_cache{0}{ctime} = time();
        foreach my $status (qw(open closed)) {
            my $page = 1;
            my $content;
            do {
                my $page_url = sprintf("%s/~%s/%s?search=status:%s&page=%d", $BASE_URL, $TRACKER_USER, $TRACKER_NAME, $status, $page++);
                $agent->get( $page_url );
                $content = Mojo::DOM->new($agent->content());
                $content->find("div.ticket-list div.id")->each(sub {
                        my ($ticket, $offset) = @_;
                        my $ticket_id = substr($ticket->children->first->text, 1);
                        $tracker_cache{$ticket_id} = ();
                        $tracker_cache{$ticket_id}{title} = $ticket->following->[0]->text;
                        if ($ticket->following->[0]->children->first->children) {
                            $tracker_cache{$ticket_id}{labels} = $ticket->following->[0]->children->first->children->map('text')->each;
                        } else {
                            $tracker_cache{$ticket_id}{labels} = [];
                        }
                        $tracker_cache{$ticket_id}{mtime} = Time::Piece->strptime($ticket->following->[1]->children->first->attr->{title}, '%Y-%m-%d %H:%M:%S UTC')->epoch;
                        $tracker_cache{$ticket_id}{ctime} = $tracker_cache{$ticket_id}{mtime}; # TODO support mtime in e_getattr;
                        $tracker_cache{$ticket_id}{submitter} = $ticket->following->[2]->children->first->text;
                        $tracker_cache{$ticket_id}{submitter} =~ s/^\s+~(\S*)\s*$/$1/;
                        $tracker_cache{$ticket_id}{comments} = $ticket->following->[3]->text;
                        $tracker_cache{$ticket_id}{type} = 0100; # regular file nodes
                        $tracker_cache{$ticket_id}{mode} = 0700; # owner only
                });
            } while ($content->at("i.fa-caret-right"));
        }
    }
    return \%tracker_cache;
}
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);
    print "getattr $file\n";
    refresh_tracker_cache();
    if (exists($files{$file})) {
        my ($size) = exists($files{$file}{cont}) ? length($files{$file}{cont}) : 0;
        $size = $files{$file}{size} if exists $files{$file}{size};
        my ($modes) = ($files{$file}{type}<<9) + $files{$file}{mode};
        my ($dev, $ino, $rdev, $blocks, $gid, $uid, $nlink, $blksize) = (0,0,0,1,0,0,1,1024);
        my ($atime, $ctime, $mtime);
        $atime = $ctime = $mtime = $files{$file}{ctime};
        # 2 possible types of return values:
        #return -ENOENT(); # or any other error you care to
        #print(join(",",($dev,$ino,$modes,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks)),"\n");
        return ($dev,$ino,$modes,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks);
    } elsif (exists($tracker_cache{$file})) {
        my ($size) = exists($tracker_cache{$file}{cont}) ? length($tracker_cache{$file}{cont}) : 0;
        $size = $tracker_cache{$file}{comments} if exists $tracker_cache{$file}{comments};
        my ($modes) = ($tracker_cache{$file}{type}<<9) + $tracker_cache{$file}{mode};
        my $submitter = $tracker_cache{$file}{submitter};
        print $submitter."\n";
        my $uid = getpwnam($submitter);
        $uid = $uid ? $uid->uid : 0;
        my ($dev, $ino, $rdev, $blocks, $gid, $nlink, $blksize) = (0,0,0,1,0,1,1024);
        my ($atime, $ctime, $mtime);
        $atime = $ctime = $mtime = $tracker_cache{$file}{ctime};
        # 2 possible types of return values:
        #return -ENOENT(); # or any other error you care to
        #print(join(",",($dev,$ino,$modes,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks)),"\n");
        return ($dev,$ino,$modes,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks);
    } else {
        return -ENOENT() unless exists($files{$file});
    }
}

sub e_getdir {
	# return as many text filenames as you like, followed by the retval.
    print "getdir @_\n";
    refresh_tracker_cache();
	print((scalar keys %tracker_cache)."\n");
	return (keys %tracker_cache),0;
}

sub e_open {
	# VFS sanity check; it keeps all the necessary state, not much to do here.
    print "open @_\n";
    my $file = filename_fixup(shift);
    my ($flags, $fileinfo) = @_;
    print("open called $file, $flags, $fileinfo\n");
	return -ENOENT() unless exists($files{$file}) || exists($tracker_cache{$file});
	return -EISDIR() if $files{$file}{type} & 0040;
    
    my $fh = [ rand() ];
    
    print("open ok (handle $fh)\n");
    return (0, $fh);
}

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, $fh) = @_;
    print "read from $file, $buf \@ $off\n";
    print "file handle:\n", Dumper($fh);
	return -ENOENT() unless exists($files{$file});
	if(!exists($files{$file}{cont})) {
		return -EINVAL() if $off > 0;
		my $context = fuse_get_context();
		return sprintf("pid=0x%08x uid=0x%08x gid=0x%08x\n",@$context{'pid','uid','gid'});
	}
	return -EINVAL() if $off > length($files{$file}{cont});
	return 0 if $off == length($files{$file}{cont});
	return substr($files{$file}{cont},$off,$buf);
}

sub e_statfs { return 255, 1, 1, 1, 1, 2 }

# 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=>"main::e_getattr",
	getdir =>"main::e_getdir",
	open   =>"main::e_open",
	statfs =>"main::e_statfs",
	read   =>"main::e_read",
	threaded=>0
);