summaryrefslogtreecommitdiff
path: root/fuse.pl
blob: 08b3d3808068144b9ae2fedf927bde8b34629407 (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
#!/usr/bin/perl
use Fuse;
use POSIX qw(ENOENT ENOSYS EEXIST EPERM O_RDONLY O_RDWR O_APPEND O_CREAT);
use local::db;
use config;

my ($mountpoint) = "";
$mountpoint = shift(@ARGV) if @ARGV;
Fuse::main(mountpoint=>$mountpoint, mountopts => "allow_other",getattr=>"main::my_getattr", getdir=>"main::my_getdir", debug=>0, readlink => "main::my_readlink");

exit 0;

sub my_getdir
{
  my ( $filename ) = @_;
  my @dirs = '.';
  if ($filename eq '/')
  {
    my @cat = local::db::category->retrieve_all;
    push @dirs, map {$_->name} @cat;
  } else {
    my ($cat) = $filename =~ m'/(.*)$';
    my @cat = local::db::category->search(name=>$cat);
    my @downloads = (shift @cat)->download; #local::db::download->retrieve_all;
    push @dirs, map {$_->name.'_'.$_->download_id} @downloads;
  }
  return @dirs,0;
#  return -1*ENOENT;
}

sub my_getattr
{
  my ( $path ) = @_;
  my $mode = 0040 <<9| 0755;
  my ($foo,$dirname,$filename) = split '/',$path;
  if ($dirname and $dirname ne '.')
  {
    if ($filename)
    {
      $mode = 0120000 | 0777 if $filename;
      return -ENOENT() if not $filename =~ m/_\d+$/;
    } else {
      local::db::category->search(name=>$dirname) or return -ENOENT();
    }
  }
  my $nlink = 1;
  my $uid = $<;
  my ($gid) = split / /, $(;
  my $size = 0;
  my $rdev = 0;
  my $atime = time;
  my $mtime = $atime;
  my $ctime = $atime;
  my $blksize = 1024;
  my $blocks = 1;
  my $dev = 0;
  my $ino = 0;

  return (
    $dev, $ino, $mode, $nlink,
    $uid, $gid, $rdev, $size,
    $atime, $mtime, $ctime, $blksize,
    $blocks
  );
#  return -1*ENOENT;
}

sub my_readlink
{
  my ( $path ) = @_;
  my ($foo,$cat,$filename) = split '/',$path;
  my ($id) = $filename =~ m/_(\d+)$/;
#  my @download = local::db::download->search(download_id=>$id);
#  my $download = shift @download or return "/dev/null";
  return $config::config{files_dir}.'/'.$cat.'/'.$id;
}