~nova/todosrht-fuse

f54e69224c0c1cd580c555b9c82f298e2e3e5315 — Novalinium 2 years ago 1dae3ae master
Improvements to non-existent file checking
2 files changed, 55 insertions(+), 26 deletions(-)

M main.pl
M requirements.txt
M main.pl => main.pl +54 -26
@@ 10,12 10,13 @@ use Data::Dumper;
use Fuse qw(fuse_get_context);
use Mojo::DOM;
use POSIX qw(ENOENT EISDIR EINVAL);
use Scalar::Util::Numeric qw(isint);
use Time::Piece;
use User::pwent;
use WWW::Mechanize;

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


@@ 91,12 92,17 @@ sub refresh_ticket_cache {
        my $page_url = sprintf("%s/~%s/%s/%d", $BASE_URL, $TRACKER_USER, $TRACKER_NAME, $ticket_id);
        $agent->get( $page_url );
        my $content = Mojo::DOM->new($agent->content());
        if ($content->at('div.container > h2')) {
            delete $tracker_cache{$ticket_id};
            return;
        }
        $tracker_cache{$ticket_id}{title} = $content->find('div.container-fluid h2 span')->last->text;
        $tracker_cache{$ticket_id}{title} =~ s/^\s+(\S*)\s*$/$1/ms;
        $tracker_cache{$ticket_id}{description} = '';
        my $description_pointer = $content->at('div.header-tabbed ~ div.container div.row div.col-md-6')->children->first;
        my $description_pointer = $content->at('div.header-tabbed ~ div.container > div.row > div.col-md-6')->children->first;
        if ($description_pointer->tag eq "style") {
            $description_pointer = $description_pointer->next_node;
            while ($description_pointer->tag ne "dl") {
            while (!defined($description_pointer->tag) || $description_pointer->tag ne "dl") {
                $tracker_cache{$ticket_id}{description} .= $description_pointer->all_text . "\n";
                $description_pointer = $description_pointer->next_node;
            }


@@ 147,22 153,29 @@ sub e_getattr {
        #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})) {
        # refresh_ticket_cache($file);
        my ($size) = exists($tracker_cache{$file}{cont}) ? length($tracker_cache{$file}{cont}) : 0;
        $size = $tracker_cache{$file}{comments} if $size == 0 && 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);
    } elsif (isint $file) {
        if (!exists($tracker_cache{$file})) {
            refresh_tracker_cache();
            if (exists($tracker_cache{$file})) {
                refresh_ticket_cache($file);
            }
        }
        if (exists($tracker_cache{$file})) {
            # refresh_ticket_cache($file);
            my ($size) = exists($tracker_cache{$file}{cont}) ? length($tracker_cache{$file}{cont}) : 0;
            $size = $tracker_cache{$file}{comments} if $size == 0 && exists $tracker_cache{$file}{comments};
            my ($modes) = ($tracker_cache{$file}{type}<<9) + $tracker_cache{$file}{mode};
            my $submitter = $tracker_cache{$file}{submitter};
            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});
    }


@@ 180,13 193,23 @@ 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);
    if (int($file)) {
        refresh_ticket_cache($file);
    }
    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 $fbackobj;
    if (exists($files{$file})) {
        $fbackobj = $files{$file};
    } elsif (exists($tracker_cache{$file})) {
        $fbackobj = $tracker_cache{$file};
        refresh_ticket_cache($file);
    } elsif (!exists($tracker_cache{$file}) && int($file)) {
        refresh_tracker_cache();
        if (exists($tracker_cache{$file})) {
            refresh_ticket_cache($file);
            $fbackobj = $tracker_cache{$file};
        }
    }
	return -ENOENT() unless $fbackobj;
	return -EISDIR() if $fbackobj->{type} & 0040;
    
    my $fh = [ rand() ];
    


@@ 198,8 221,13 @@ 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);
    if (int($file)) {
        refresh_ticket_cache($file);
    if (isint $file) {
        if (!exists($tracker_cache{$file})) {
            refresh_tracker_cache();
        }
        if (exists($tracker_cache{$file})) {
            refresh_ticket_cache($file);
        }
    }
    my ($buf, $off, $fh) = @_;
    print "read from $file, $buf \@ $off\n";

M requirements.txt => requirements.txt +1 -0
@@ 1,3 1,4 @@
Fuse (Dependes on bundled patched Unix::Mknod)
WWW::Mechanize
Mojo::DOM
Scalar::Util::Numeric