Use a few more Perl-isms.
[perl-fuse.git] / examples / fioc.pl
index 3336457..1b5282a 100755 (executable)
@@ -3,17 +3,19 @@
 use strict;
 no strict qw(refs);
 
-use Carp ();
+use threads;
+use threads::shared;
+
+use Carp;
 local $SIG{'__WARN__'} = \&Carp::cluck;
 
 use Fuse;
 use Fcntl qw(:mode);
-use Errno qw(:POSIX);
 use POSIX;
 
-my $fioc_size = 0;
+my $fioc_size :shared = 0;
 use constant FIOC_NAME => 'fioc';
-my $fioc_buf = '';
+my $fioc_buf :shared = '';
 use constant FIOC_NONE  => 0;
 use constant FIOC_ROOT  => 1;
 use constant FIOC_FILE  => 2;
@@ -23,12 +25,13 @@ require 'asm/ioctl.ph';
 our %sizeof = ('int' => 4);
 sub FIOC_GET_SIZE { _IOR(ord 'E', 0, 'int'); }
 sub FIOC_SET_SIZE { _IOW(ord 'E', 1, 'int'); }
+sub TCGETS { 0x5401; }
 
 sub fioc_resize {
     my ($size) = @_;
     print 'called ', (caller(0))[3], "\n";
     return 0 if $size == $fioc_size;
-    
+
     if ($size < $fioc_size) {
         $fioc_buf = substr($fioc_buf, 0, $size);
     }
@@ -85,9 +88,7 @@ sub fioc_open {
     my ($path, $flags, $info) = @_;
     print 'called ', (caller(0))[3], "\n";
 
-    if (fioc_file_type($path) != FIOC_NONE) {
-        return 0;
-    }
+    return 0 if fioc_file_type($path) != FIOC_NONE;
     return -&ENOENT;
 }
 
@@ -96,10 +97,7 @@ sub fioc_read {
     print 'called ', (caller(0))[3], "\n";
 
     return -&EINVAL if fioc_file_type($path) != FIOC_FILE;
-
-    if ($offset > $fioc_size) {
-        return q{};
-    }
+    return q{} if $offset > $fioc_size;
 
     if ($size > $fioc_size - $offset) {
         $size - $fioc_size - $offset;
@@ -111,12 +109,10 @@ sub fioc_read {
 sub fioc_write {
     my ($path, $data, $offset) = @_;
     print 'called ', (caller(0))[3], "\n";
+    lock($fioc_buf);
 
     return -&EINVAL if fioc_file_type($path) != FIOC_FILE;
-
-    if (fioc_expand($offset + length($data))) {
-        return -&ENOMEM;
-    }
+    return -&ENOMEM if fioc_expand($offset + length($data));
 
     substr($fioc_buf, $offset, length($data), $data);
     return length($data);
@@ -125,6 +121,7 @@ sub fioc_write {
 sub fioc_truncate {
     my ($path, $size) = @_;
     print 'called ', (caller(0))[3], "\n";
+    lock($fioc_buf);
 
     return -&EINVAL if fioc_file_type($path) != FIOC_FILE;
 
@@ -143,22 +140,23 @@ sub fioc_readdir {
 sub fioc_ioctl {
     my ($path, $cmd, $flags, $data) = @_;
     print 'called ', (caller(0))[3], "\n";
-    $cmd = unpack('L', pack('l', $cmd));
 
-    print("fioc_ioctl(): path is \"$path\", cmd is $cmd, flags is $flags\n");
     return -&EINVAL if fioc_file_type($path) != FIOC_FILE;
-
     return -&ENOSYS if $flags & 0x1;
 
     if ($cmd == FIOC_GET_SIZE) {
-        print "handling FIOC_GET_SIZE\n";
         return(0, pack('L', $fioc_size));
     }
     elsif ($cmd == FIOC_SET_SIZE) {
-        print "handling FIOC_SET_SIZE\n";
+        lock($fioc_buf);
         fioc_resize(unpack('L', $data));
         return 0;
     }
+    elsif ($cmd == TCGETS) {
+        # perl sends TCGETS as part of calling isatty() on opening a file;
+        # this appears to be a more canonical answer
+        return -&ENOTTY;
+    }
 
     return -&EINVAL;
 }
@@ -173,4 +171,5 @@ Fuse::main(
     'open'      => 'main::fioc_open',
     'read'      => 'main::fioc_read',
     'write'     => 'main::fioc_write',
-    'ioctl'     => 'main::fioc_ioctl');
+    'ioctl'     => 'main::fioc_ioctl',
+    'threaded'  => 1);