#!/usr/bin/perl -w # This was taken almost wholesale from one of the Fuse.pm examples :) use strict; use Fuse; use IO::File; use POSIX qw(ENOENT ENOSYS EEXIST EPERM O_RDONLY O_RDWR O_APPEND O_CREAT); use Fcntl qw(S_ISBLK S_ISCHR S_ISFIFO SEEK_SET); my $can_syscall = eval { require 'syscall.ph'; # for SYS_mknod and SYS_lchown }; if (!$can_syscall && open my $fh, '<', '/usr/include/sys/syscall.h') { local $/ = undef; my %sys = do { local $/ = undef; <$fh> =~ m/\#define \s+ (\w+) \s+ (\d+)/gxms; }; close $fh; if ($sys{SYS_mknod} && $sys{SYS_lchown}) { *SYS_mknod = sub { $sys{SYS_mknod} }; *SYS_lchown = sub { $sys{SYS_lchown} }; $can_syscall = 1; } } my $mountpoint; my $target_path; my $cache_path; sub fixup { # print STDERR "*** fixup $_[0] from @{[caller]}\n"; my ($path) = @_; my $new_path; $new_path = $target_path if $path eq '/'; $new_path = $target_path . $path; if(! -e $new_path) { $new_path = $cache_path if $path eq '/'; $new_path = $cache_path . $path; } print STDERR "PATH: $new_path\n"; return $new_path; } sub x_getattr { my ($file) = fixup(shift); my (@list) = lstat($file); return -$! unless @list; return @list; } sub x_getdir { my ($dirname) = fixup(shift); unless(opendir(DIRHANDLE,$dirname)) { return -ENOENT(); } my (@files) = readdir(DIRHANDLE); closedir(DIRHANDLE); return (@files, 0); } my $already_cached = {}; sub cache_file { my ($bare_file) = @_; return if $already_cached->{$bare_file}; $already_cached->{$bare_file} = 1; my $cache_file = $cache_path . $bare_file; if($cache_file =~ /^(.*)\/.*$/) { my $cache_file_dir = $1; `mkdir -p "$cache_file_dir"`; } print STDERR qq{*** Copying "$target_path$bare_file" -> "$cache_path$bare_file"\n}; if(!fork) { #`nice cp "$target_path$bare_file" "$cache_path$bare_file"`; `rsync -aP --bwlimit=100 "$target_path$bare_file" "$cache_path$bare_file"`; exit; } } my $read_action = {}; sub queue_file_for_cache { my ($bare_file) = @_; if(-e "$target_path$bare_file" && -f _ && -r _) { if($bare_file =~ /\.(mp3|ogg)$/i) { $read_action->{$bare_file} = sub { my ($file,$bufsize,$off) = @_; if($off > 500000 && $off < 1000000) { print STDERR "Read at offset $off triggering action...\n"; cache_file($bare_file); delete $read_action->{$bare_file}; } }; } else { cache_file($bare_file); } } } sub x_open { my ($bare_file) = shift; queue_file_for_cache($bare_file); my ($file) = fixup($bare_file); my ($mode) = shift; return -$! unless sysopen(FILE,$file,$mode); close(FILE); return 0; } # for mp3 files, look for > 100000 and < 200000 reads to decide if we want to # copy this file or not. It reads around zero and then around the end of the # file for the id3 stuff, so this should only catch the ones that are actually # being played. sub x_read { my ($file,$bufsize,$off) = @_; print STDERR "Read: $file, $bufsize at $off\n"; if($read_action->{$file}) { $read_action->{$file}->($file, $bufsize, $off); } my ($rv) = -ENOSYS(); my ($handle) = new IO::File; return -ENOENT() unless -e ($file = fixup($file)); my ($fsize) = -s $file; return -ENOSYS() unless open($handle,$file); if(seek($handle,$off,SEEK_SET)) { read($handle,$rv,$bufsize); } return $rv; } sub x_write { my ($file,$buf,$off) = @_; my ($rv); return -ENOENT() unless -e ($file = fixup($file)); my ($fsize) = -s $file; return -ENOSYS() unless open(FILE,'+<',$file); if($rv = seek(FILE,$off,SEEK_SET)) { $rv = print(FILE $buf); } $rv = -ENOSYS() unless $rv; close(FILE); return length($buf); } sub err { return (-shift || -$!) } sub x_readlink { return readlink(fixup(shift)); } sub x_unlink { return unlink(fixup(shift)) ? 0 : -$!; } sub x_symlink { #print "symlink\n"; return symlink(shift,fixup(shift)) ? 0 : -$!; } sub x_rename { my ($old) = fixup(shift); my ($new) = fixup(shift); my ($err) = rename($old,$new) ? 0 : -ENOENT(); return $err; } sub x_link { return link(fixup(shift),fixup(shift)) ? 0 : -$! } sub x_chown { return -ENOSYS() if ! $can_syscall; my ($fn) = fixup(shift); #print "nonexistent $fn\n" unless -e $fn; my ($uid,$gid) = @_; # perl's chown() does not chown symlinks, it chowns the symlink's # target. it fails when the link's target doesn't exist, because # the stat64() syscall fails. # this causes error messages when unpacking symlinks in tarballs. my ($err) = syscall(&SYS_lchown,$fn,$uid,$gid,$fn) ? -$! : 0; return $err; } sub x_chmod { my ($fn) = fixup(shift); my ($mode) = shift; my ($err) = chmod($mode,$fn) ? 0 : -$!; return $err; } sub x_truncate { return truncate(fixup(shift),shift) ? 0 : -$! ; } sub x_utime { return utime($_[1],$_[2],fixup($_[0])) ? 0:-$!; } sub x_mkdir { my ($name, $perm) = @_; return 0 if mkdir(fixup($name),$perm); return -$!; } sub x_rmdir { return 0 if rmdir fixup(shift); return -$!; } sub x_mknod { return -ENOSYS() if ! $can_syscall; # since this is called for ALL files, not just devices, I'll do some checks # and possibly run the real mknod command. my ($file, $modes, $dev) = @_; $file = fixup($file); $! = 0; syscall(&SYS_mknod,$file,$modes,$dev); return -$!; } # kludge sub x_statfs {return 255,1000000,500000,1000000,500000,4096} if(!@ARGV) { print " Usage: mobilefs.pl "; exit; } ($target_path, $mountpoint, $cache_path) = @ARGV; # my $tmp = -d '/private' ? '/private/tmp' : '/tmp'; # my $target_path = "$tmp/fusetest-" . $ENV{LOGNAME}; # if (! -e $target_path) { # mkdir($target_path) || die "can't create $target_path: $!"; # } # my ($mountpoint) = ""; # $mountpoint = shift(@ARGV) if @ARGV; Fuse::main( mountpoint=>$mountpoint, getattr =>"main::x_getattr", readlink=>"main::x_readlink", getdir =>"main::x_getdir", mknod =>"main::x_mknod", mkdir =>"main::x_mkdir", unlink =>"main::x_unlink", rmdir =>"main::x_rmdir", symlink =>"main::x_symlink", rename =>"main::x_rename", link =>"main::x_link", chmod =>"main::x_chmod", chown =>"main::x_chown", truncate=>"main::x_truncate", utime =>"main::x_utime", open =>"main::x_open", read =>"main::x_read", write =>"main::x_write", statfs =>"main::x_statfs", threaded=>0, debug => 1, );