| 1 | use strict; | ||
| 2 | use 5.010; | ||
| 3 | |||
| 4 | package FusqlFS; | ||
| 5 | |||
| 6 | =head1 NAME | ||
| 7 | |||
| 8 | FusqlFS - FUSE filesystem to work with database via DBI interface | ||
| 9 | |||
| 10 | =head1 SYNOPSIS | ||
| 11 | |||
| 12 | use FusqlFS; | ||
| 13 | |||
| 14 | FusqlFS->init( | ||
| 15 | engine => 'PgSQL', | ||
| 16 | host => 'localhost', | ||
| 17 | database => 'postgres', | ||
| 18 | user => 'postgres', | ||
| 19 | password => 'pas$w0rd', | ||
| 20 | limit => 100, | ||
| 21 | debug => 2, | ||
| 22 | threaded => 0, | ||
| 23 | ); | ||
| 24 | |||
| 25 | FusqlFS->mount( | ||
| 26 | '/path/to/mount/point', | ||
| 27 | 'allow_other', | ||
| 28 | ); | ||
| 29 | |||
| 30 | =head1 DESCRIPTION | ||
| 31 | |||
| 32 | This package provides basic interface to FusqlFS to initialize L<DBI> backend | ||
| 33 | and mount it with L<Fuse>. | ||
| 34 | |||
| 35 | =head1 SUBROUTINES | ||
| 36 | |||
| 37 | =over | ||
| 38 | |||
| 39 | =cut | ||
| 40 | |||
| 41 | use POSIX qw(:fcntl_h :errno_h mktime SIGQUIT SIGTERM SIGINT); | ||
| 42 | use File::Path qw(make_path remove_tree); | ||
| 43 | use Fcntl qw(:mode); | ||
| 44 | use Carp; | ||
| 45 | use Fuse; | ||
| 46 | |||
| 47 | use FusqlFS::Cache; | ||
| 48 | use FusqlFS::Backend; | ||
| 49 | |||
| 50 | our $fusqlh; | ||
| 51 | our $def_time; | ||
| 52 | our $debug; | ||
| 53 | our $threaded; | ||
| 54 | our %cache; | ||
| 55 | our %inbuffer; | ||
| 56 | |||
| 57 | use FusqlFS::Version; | ||
| 58 | our $VERSION = $FusqlFS::Version::VERSION; | ||
| 59 | |||
| 60 | =item init | ||
| 61 | |||
| 62 | Initializes L<FusqlFS::Backend> and L<FusqlFS::Cache> subsystems, preparing | ||
| 63 | all resources to be actually used by L<Fuse> interface. | ||
| 64 | |||
| 65 | Input: %options. | ||
| 66 | Output: $fusqlfs_instance. | ||
| 67 | |||
| 68 | =cut | ||
| 69 | sub init | ||
| 70 | { | ||
| 71 | my $class = shift; | ||
| 72 | my %options = @_; | ||
| 73 | |||
| 74 | $threaded = $options{threaded}||0; | ||
| 75 | $debug = $options{debug}||0; | ||
| 76 | $def_time = mktime(localtime()); | ||
| 77 | |||
| 78 | $fusqlh = FusqlFS::Backend->new(@_); | ||
| 79 | croak "Unable to initialize database backend" unless defined $fusqlh; | ||
| 80 | |||
| 81 | if ($threaded) | ||
| 82 | { | ||
| 83 | use threads; | ||
| 84 | use threads::shared; | ||
| 85 | } | ||
| 86 | |||
| 87 | FusqlFS::Cache->init(\%cache, @options{qw(cache_strategy cache_threshold)}); | ||
| 88 | $SIG{USR1} = sub () { %cache = (); }; | ||
| 89 | $SIG{HUP} = sub () { %cache = (); $fusqlh->reconnect(); }; | ||
| 90 | |||
| 91 | return $class; | ||
| 92 | } | ||
| 93 | |||
| 94 | =item mount | ||
| 95 | |||
| 96 | Runs fuse main loop, configured with all required hooks and options to | ||
| 97 | interface with L<FusqlFS::Backend>. | ||
| 98 | |||
| 99 | Input: $mountpoint, $mountopts. | ||
| 100 | |||
| 101 | =cut | ||
| 102 | sub mount | ||
| 103 | { | ||
| 104 | my $class = shift; | ||
| 105 | my $mountpoint = shift; | ||
| 106 | my %mountopts = @_; | ||
| 107 | |||
| 108 | if (!-e $mountpoint) { | ||
| 109 | if ($mountopts{mkdir}) { | ||
| 110 | make_path($mountpoint) or croak "Unable to create mountpoint `$mountpoint'"; | ||
| 111 | } else { | ||
| 112 | croak "Mountpoint `$mountpoint' does not exist"; | ||
| 113 | } | ||
| 114 | |||
| 115 | } elsif (!-d $mountpoint) { | ||
| 116 | croak "Mountpoint `$mountpoint' is not a directory"; | ||
| 117 | } | ||
| 118 | |||
| 119 | my $fusermount = (grep { -f "$_/fusermount" } split /:/, $ENV{PATH})[0] . '/fusermount'; | ||
| 120 | if (-x $fusermount) { | ||
| 121 | carp "fusermount found at `$fusermount' and will be used to autounmount when daemon terminated."; | ||
| 122 | } else { | ||
| 123 | carp "fusermount is not found or non-executable, won't autounmount when daemon terminated!"; | ||
| 124 | $fusermount = undef; | ||
| 125 | } | ||
| 126 | |||
| 127 | my $rmdir = $mountopts{rmdir} || 0; | ||
| 128 | if ($fusermount || $rmdir) { | ||
| 129 | my $quithandler = POSIX::SigAction->new(sub { | ||
| 130 | if ($fusermount) { | ||
| 131 | carp "Running `$fusermount' to unmount `$mountpoint'..."; | ||
| 132 | chdir '/'; | ||
| 133 | $mountpoint =~ s/(['\\])/\\$1/g; | ||
| 134 | system(sprintf(q{fusermount -u -z '%s'}, $mountpoint)); | ||
| 135 | } | ||
| 136 | |||
| 137 | remove_tree($mountpoint) if $rmdir; | ||
| 138 | |||
| 139 | exit(0); | ||
| 140 | }); | ||
| 141 | |||
| 142 | POSIX::sigaction(SIGQUIT, $quithandler); | ||
| 143 | POSIX::sigaction(SIGTERM, $quithandler); | ||
| 144 | POSIX::sigaction(SIGINT, $quithandler); | ||
| 145 | } | ||
| 146 | |||
| 147 | Fuse::main( | ||
| 148 | mountpoint => $mountpoint, | ||
| 149 | mountopts => $mountopts{options}||'', | ||
| 150 | threaded => $threaded, | ||
| 151 | debug => $debug > 4, | ||
| 152 | |||
| 153 | getdir => \&getdir, | ||
| 154 | getattr => \&getattr, | ||
| 155 | readlink => \&readlink, | ||
| 156 | open => \&open, | ||
| 157 | read => \&read, | ||
| 158 | |||
| 159 | write => \&write, | ||
| 160 | truncate => \&truncate, | ||
| 161 | flush => \&flush, | ||
| 162 | rename => \&rename, | ||
| 163 | |||
| 164 | mkdir => \&mkdir, | ||
| 165 | mknod => \&mknod, | ||
| 166 | symlink => \&symlink, | ||
| 167 | |||
| 168 | unlink => \&unlink, | ||
| 169 | rmdir => \&rmdir, | ||
| 170 | |||
| 171 | fsync => \&fsync, | ||
| 172 | utime => \&utime, | ||
| 173 | ); | ||
| 174 | |||
| 175 | remove_tree($mountpoint) if $rmdir; | ||
| 176 | } | ||
| 177 | |||
| 178 | =item Fuse hooks | ||
| 179 | |||
| 180 | L<Fuse/getdir>, L<Fuse/getattr>, L<Fuse/readlink>, L<Fuse/read>, L<Fuse/write>, | ||
| 181 | L<Fuse/flush>, L<Fuse/open>, L<Fuse/truncate>, L<Fuse/symlink>, L<Fuse/unlink>, | ||
| 182 | L<Fuse/mkdir>, L<Fuse/rmdir>, L<Fuse/mknod>, L<Fuse/rename>, L<Fuse/fsync>, | ||
| 183 | L<Fuse/utime>. | ||
| 184 | |||
| 185 | See L<Fuse> for details. | ||
| 186 | |||
| 187 | =cut | ||
| 188 | sub getdir | ||
| 189 | { | ||
| 190 | |||
| 191 | my ($path) = @_; | ||
| 192 | my $entry = by_path($path); | ||
| 193 | return - ENOENT unless $entry; | ||
| 194 | return - ENOTDIR unless $entry->isdir(); | ||
| 195 | return ('.', '..', @{$entry->list()}, 0); | ||
| 196 | } | ||
| 197 | |||
| 198 | sub getattr | ||
| 199 | { | ||
| 200 | my ($path) = @_; | ||
| 201 | my $entry = by_path($path); | ||
| 202 | return - ENOENT unless $entry; | ||
| 203 | return file_struct($entry); | ||
| 204 | } | ||
| 205 | |||
| 206 | sub readlink | ||
| 207 | { | ||
| 208 | my ($path) = @_; | ||
| 209 | my $entry = by_path($path); | ||
| 210 | return - ENOENT unless $entry; | ||
| 211 | return - EINVAL unless $entry->islink(); | ||
| 212 | return $entry->read(); | ||
| 213 | } | ||
| 214 | |||
| 215 | sub read | ||
| 216 | { | ||
| 217 | my ($path, $size, $offset) = @_; | ||
| 218 | my $entry = by_path($path); | ||
| 219 | return - ENOENT unless $entry; | ||
| 220 | return - EINVAL unless $entry->isfile(); | ||
| 221 | |||
| 222 | return $entry->read($offset, $size); | ||
| 223 | } | ||
| 224 | |||
| 225 | sub write | ||
| 226 | { | ||
| 227 | my ($path, $buffer, $offset) = @_; | ||
| 228 | my $entry = by_path($path); | ||
| 229 | return - ENOENT unless $entry; | ||
| 230 | return - EISDIR if $entry->isdir(); | ||
| 231 | return - EINVAL unless $entry->isfile(); | ||
| 232 | return - EACCES unless $entry->writable(); | ||
| 233 | |||
| 234 | $inbuffer{$path} ||= $entry->get(); | ||
| 235 | substr($inbuffer{$path}, $offset, length($buffer)) = $buffer; | ||
| 236 | return length($buffer); | ||
| 237 | } | ||
| 238 | |||
| 239 | sub flush | ||
| 240 | { | ||
| 241 | my ($path) = @_; | ||
| 242 | my $entry = by_path($path); | ||
| 243 | return - ENOENT unless $entry; | ||
| 244 | |||
| 245 | flush_inbuffer($path, $entry); | ||
| 246 | clear_cache($path, $entry->depth()) unless $entry->ispipe(); | ||
| 247 | return 0; | ||
| 248 | } | ||
| 249 | |||
| 250 | sub open | ||
| 251 | { | ||
| 252 | my ($path, $mode) = @_; | ||
| 253 | my $entry = by_path($path); | ||
| 254 | return - ENOENT unless $entry; | ||
| 255 | return - EISDIR if $entry->isdir(); | ||
| 256 | return 0; | ||
| 257 | } | ||
| 258 | |||
| 259 | sub truncate | ||
| 260 | { | ||
| 261 | my ($path, $offset) = @_; | ||
| 262 | my $entry = by_path($path); | ||
| 263 | return - ENOENT unless $entry; | ||
| 264 | return - EINVAL unless $entry->isfile(); | ||
| 265 | return - EACCES unless $entry->writable(); | ||
| 266 | |||
| 267 | $entry->write($offset); | ||
| 268 | clear_cache($path, $entry->depth()) unless $entry->ispipe(); | ||
| 269 | return 0; | ||
| 270 | } | ||
| 271 | |||
| 272 | sub symlink | ||
| 273 | { | ||
| 274 | my ($path, $symlink) = @_; | ||
| 275 | return - EOPNOTSUPP if $path =~ /^\//; | ||
| 276 | |||
| 277 | $path = fold_path($symlink, '..', $path); | ||
| 278 | my $origin = by_path($path); | ||
| 279 | return - ENOENT unless $origin; | ||
| 280 | |||
| 281 | $path =~ s{^/}{}; | ||
| 282 | my $entry = $fusqlh->by_path($symlink, \$path); | ||
| 283 | return - EEXIST unless $entry->get() == \$path; | ||
| 284 | |||
| 285 | $entry->store(); | ||
| 286 | say STDERR "depth = ",$entry->depth(), "; height = ", $entry->height(); | ||
| 287 | clear_cache($symlink, $entry->depth()); | ||
| 288 | return 0; | ||
| 289 | } | ||
| 290 | |||
| 291 | sub unlink | ||
| 292 | { | ||
| 293 | my ($path) = @_; | ||
| 294 | my $entry = by_path($path); | ||
| 295 | return - ENOENT unless $entry; | ||
| 296 | return - EACCES unless $entry->writable(); | ||
| 297 | return - EISDIR if $entry->isdir(); | ||
| 298 | |||
| 299 | clear_cache($path, $entry->depth() + 1); | ||
| 300 | $entry->drop(); | ||
| 301 | return 0; | ||
| 302 | } | ||
| 303 | |||
| 304 | sub mkdir | ||
| 305 | { | ||
| 306 | my ($path, $mode) = @_; | ||
| 307 | my $newdir = {}; | ||
| 308 | my $entry = $fusqlh->by_path($path, $newdir); | ||
| 309 | return - ENOENT unless $entry; | ||
| 310 | return - EEXIST unless $entry->get() == $newdir; | ||
| 311 | |||
| 312 | $entry->create(); | ||
| 313 | clear_cache($path, $entry->depth() + 1); | ||
| 314 | return 0; | ||
| 315 | } | ||
| 316 | |||
| 317 | sub rmdir | ||
| 318 | { | ||
| 319 | my ($path) = @_; | ||
| 320 | my $entry = by_path($path); | ||
| 321 | return - ENOENT unless $entry; | ||
| 322 | return - EACCES unless $entry->writable(); | ||
| 323 | return - ENOTDIR unless $entry->isdir(); | ||
| 324 | |||
| 325 | $entry->drop(); | ||
| 326 | clear_cache($path, $entry->depth() + 1); | ||
| 327 | return 0; | ||
| 328 | } | ||
| 329 | |||
| 330 | sub mknod | ||
| 331 | { | ||
| 332 | my ($path, $mode, $dev) = @_; | ||
| 333 | my $entry = $fusqlh->by_path($path, ''); | ||
| 334 | return - ENOENT unless $entry; | ||
| 335 | return - EEXIST unless $entry->get() eq ''; | ||
| 336 | |||
| 337 | $entry->create(); | ||
| 338 | clear_cache($path, $entry->depth()); | ||
| 339 | return 0; | ||
| 340 | } | ||
| 341 | |||
| 342 | sub rename | ||
| 343 | { | ||
| 344 | my ($path, $name) = @_; | ||
| 345 | my $entry = by_path($path); | ||
| 346 | return - ENOENT unless $entry; | ||
| 347 | return - EACCES unless $entry->writable(); | ||
| 348 | |||
| 349 | my $target = $fusqlh->by_path($name, $entry->get()); | ||
| 350 | return - ENOENT unless $target; | ||
| 351 | return - EEXIST unless $entry->get() == $target->get(); | ||
| 352 | return - EACCES unless $target->writable(); | ||
| 353 | return - EOPNOTSUPP unless $entry->pkg() == $target->pkg() | ||
| 354 | && $entry->depth() == $target->depth() | ||
| 355 | && $entry->height() == $target->height(); | ||
| 356 | |||
| 357 | $entry->move($target); | ||
| 358 | clear_cache($path, $entry->depth()); | ||
| 359 | return 0; | ||
| 360 | } | ||
| 361 | |||
| 362 | sub fsync | ||
| 363 | { | ||
| 364 | my ($path, $flags) = @_; | ||
| 365 | my $entry = by_path($path); | ||
| 366 | return - ENOENT unless $entry; | ||
| 367 | |||
| 368 | flush_inbuffer($path, $entry); | ||
| 369 | clear_cache($path, $flags? $entry->depth(): undef); | ||
| 370 | return 0; | ||
| 371 | } | ||
| 372 | |||
| 373 | sub utime | ||
| 374 | { | ||
| 375 | my ($path, $atime, $mtime) = @_; | ||
| 376 | my $entry = by_path($path); | ||
| 377 | return - ENOENT unless $entry; | ||
| 378 | return 0; | ||
| 379 | } | ||
| 380 | |||
| 381 | =item fold_path | ||
| 382 | |||
| 383 | Folds path by removing "..", "." and other special sequences from it. | ||
| 384 | |||
| 385 | Input: $path. | ||
| 386 | Output $folded_path. | ||
| 387 | |||
| 388 | =cut | ||
| 389 | sub fold_path | ||
| 390 | { | ||
| 391 | local $/ = '/'; | ||
| 392 | my $path = join '/', @_; | ||
| 393 | $path =~ s{//+}{/}g; | ||
| 394 | while ($path =~ s{/\./}{/}g) {} | ||
| 395 | while ($path =~ s{[^/]+/\.\./}{}) {} | ||
| 396 | #$path =~ s{^/\.\./}{/}; | ||
| 397 | chomp $path; | ||
| 398 | return $path; | ||
| 399 | } | ||
| 400 | |||
| 401 | =item file_struct | ||
| 402 | |||
| 403 | Creates FUSE-exportable file structure for given entry. | ||
| 404 | |||
| 405 | Input: $entry. | ||
| 406 | Output: @file_struct. | ||
| 407 | |||
| 408 | =cut | ||
| 409 | sub file_struct | ||
| 410 | { | ||
| 411 | my ($entry) = @_; | ||
| 412 | my @fileinfo = ( | ||
| 413 | undef, # 0 dev | ||
| 414 | undef, # 1 ino | ||
| 415 | 0644, # 2 mode | ||
| 416 | 1, # 3 nlink | ||
| 417 | $>, # 4 uid | ||
| 418 | $), # 5 gid | ||
| 419 | undef, # 6 rdev | ||
| 420 | 0, # 7 size | ||
| 421 | $def_time, # 8 atime | ||
| 422 | $def_time, # 9 mtime | ||
| 423 | $def_time, # 10 ctime | ||
| 424 | 512, # 11 blksize | ||
| 425 | undef, # 12 blocks | ||
| 426 | ); | ||
| 427 | if ($entry->isdir()) | ||
| 428 | { | ||
| 429 | $fileinfo[2] |= (S_IFDIR|0111); | ||
| 430 | $fileinfo[3] = 2 + $entry->size(); | ||
| 431 | } | ||
| 432 | elsif ($entry->islink()) | ||
| 433 | { | ||
| 434 | $fileinfo[2] |= S_IFLNK; | ||
| 435 | $fileinfo[7] = $entry->size(); | ||
| 436 | } | ||
| 437 | else | ||
| 438 | { | ||
| 439 | $fileinfo[2] |= S_IFREG; | ||
| 440 | $fileinfo[2] |= S_ISVTX if $entry->ispipe(); | ||
| 441 | $fileinfo[7] = $entry->size(); | ||
| 442 | } | ||
| 443 | |||
| 444 | unless ($entry->writable()) | ||
| 445 | { | ||
| 446 | $fileinfo[2] &= ~0222; | ||
| 447 | } | ||
| 448 | |||
| 449 | return @fileinfo; | ||
| 450 | } | ||
| 451 | |||
| 452 | =item by_path | ||
| 453 | |||
| 454 | Gets entry by path, uses cache. | ||
| 455 | |||
| 456 | Input: $path. | ||
| 457 | |||
| 458 | =cut | ||
| 459 | sub by_path | ||
| 460 | { | ||
| 461 | my ($path) = @_; | ||
| 462 | return $cache{$path} if exists $cache{$path}; | ||
| 463 | my $entry = $fusqlh->by_path(@_); | ||
| 464 | $cache{$path} = $entry if $entry; | ||
| 465 | return $entry; | ||
| 466 | } | ||
| 467 | |||
| 468 | =item clear_cache | ||
| 469 | |||
| 470 | Invalidates entries cache for given path, optionally recursively invalidating | ||
| 471 | all paths up to given depth. | ||
| 472 | |||
| 473 | Input: $path, $depth=undef. | ||
| 474 | |||
| 475 | =cut | ||
| 476 | sub clear_cache | ||
| 477 | { | ||
| 478 | delete $cache{$_[0]}; | ||
| 479 | if (defined $_[1]) | ||
| 480 | { | ||
| 481 | my $key = $_[0]; | ||
| 482 | my $re = "/[^/]+" x $_[1]; | ||
| 483 | $key =~ s{$re$}{} if $re; | ||
| 484 | my $lk = length($key); | ||
| 485 | while (my $_ = each %cache) | ||
| 486 | { | ||
| 487 | next unless substr($_, 0, $lk) eq $key; | ||
| 488 | delete $cache{$_}; | ||
| 489 | } | ||
| 490 | } | ||
| 491 | } | ||
| 492 | |||
| 493 | =item flush_inbuffer | ||
| 494 | |||
| 495 | Flushes input buffer for given $entry by given $path. | ||
| 496 | |||
| 497 | Input: $path, $entry. | ||
| 498 | |||
| 499 | =cut | ||
| 500 | sub flush_inbuffer | ||
| 501 | { | ||
| 502 | my ($path, $entry) = @_; | ||
| 503 | if (exists $inbuffer{$path}) | ||
| 504 | { | ||
| 505 | $entry->write(0, $inbuffer{$path}); | ||
| 506 | delete $inbuffer{$path}; | ||
| 507 | } | ||
| 508 | } | ||
| 509 | |||
| 510 | 1; | ||
| 511 | |||
| 512 | __END__ |