| 1 | package Fuse::PerlSSH::RemoteFunctions; | ||
| 2 | |||
| 3 | # init read seek tell write truncate close copied from PerlSSH::Library::IO, v0.16, (C) Paul Evans, 2009-2011 -- leonerd@leonerd.org.uk | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | |||
| 8 | use IPC::PerlSSH::Library; | ||
| 9 | |||
| 10 | |||
| 11 | init q[ | ||
| 12 | use IO::Handle; | ||
| 13 | |||
| 14 | our %handles; | ||
| 15 | |||
| 16 | sub store_handle { | ||
| 17 | my $fh = shift; | ||
| 18 | my $fd = $fh->fileno; | ||
| 19 | $handles{$fd} = $fh; | ||
| 20 | return $fd; | ||
| 21 | } | ||
| 22 | |||
| 23 | sub get_handle { | ||
| 24 | my $fd = shift; | ||
| 25 | $fd > 2 or die "Cannot operate on STD{IN|OUT|ERR}\n"; | ||
| 26 | return $handles{$fd} || die "No handle on fileno $fd\n"; | ||
| 27 | } | ||
| 28 | ]; | ||
| 29 | |||
| 30 | # because FUSE gives us sysopen-style numeric modes, and PerlSSH's open would | ||
| 31 | # require us to map back to symbols, we implement a sysopen | ||
| 32 | func sysopen => q{ | ||
| 33 | my ( $mode, $path ) = @_; | ||
| 34 | sysopen( my $fh, $path, $mode ) or die "Cannot sysopen() - $!\n"; | ||
| 35 | $fh->autoflush; | ||
| 36 | store_handle( $fh ); | ||
| 37 | }; | ||
| 38 | |||
| 39 | func close => q{ | ||
| 40 | our %handles; | ||
| 41 | undef $handles{shift()}; | ||
| 42 | }; | ||
| 43 | |||
| 44 | # second arg to seek is WHENCE, 0 is for SEEK_SET | ||
| 45 | func read => q{ | ||
| 46 | my $fh = get_handle( shift ); | ||
| 47 | $fh->seek($_[1], 0) or die "Cannot seek() in read() - $!\n"; | ||
| 48 | defined( $fh->read( my $buf, $_[0] ) ) or die "Cannot read() - $!\n"; | ||
| 49 | return $buf; | ||
| 50 | }; | ||
| 51 | |||
| 52 | func write => q{ | ||
| 53 | my $fh = get_handle( shift ); | ||
| 54 | $fh->seek($_[1], 0) or die "Cannot seek() in write() - $!\n"; | ||
| 55 | defined( $fh->print( $_[0] ) ) or die "Cannot write() - $!\n"; | ||
| 56 | }; | ||
| 57 | |||
| 58 | func tell => q{ | ||
| 59 | my $fh = get_handle( shift ); | ||
| 60 | return tell($fh); | ||
| 61 | }; | ||
| 62 | |||
| 63 | func truncate => q{ | ||
| 64 | my $fh = get_handle( shift ); | ||
| 65 | $fh->truncate( $_[0] ) or die "Cannot truncate() - $!\n"; | ||
| 66 | }; | ||
| 67 | |||
| 68 | func fstat => q{ | ||
| 69 | my $fh = get_handle( shift ); | ||
| 70 | my @s = stat( $fh ) or die "Cannot stat() - $!\n"; | ||
| 71 | @s; | ||
| 72 | }; | ||
| 73 | |||
| 74 | 1; |