Multiple changes for better *BSD compatibility, including:
[perl-fuse.git] / examples / loopback.pl
index 81ee2e1..2dea378 100755 (executable)
@@ -5,12 +5,11 @@ use blib;
 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);
+use Fcntl qw(S_ISBLK S_ISCHR S_ISFIFO SEEK_SET S_ISREG S_ISFIFO S_IMODE);
 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;
         };
@@ -22,17 +21,7 @@ if (!$can_syscall && open my $fh, '<', '/usr/include/sys/syscall.h') {
        }
 }
 
-my $tmp = -d '/private' ? '/private/tmp' : '/tmp';
-my $tmp_path = "$tmp/fusetest-" . $ENV{LOGNAME};
-if (! -e $tmp_path) {
-       mkdir($tmp_path) || die "can't create $tmp_path: $!";
-}
-
-sub fixup { print STDERR "fixup $_[0] from @{[caller]}\n";
-            my ($path) = @_;
-            return $tmp_path if $path eq '/';
-            return $tmp_path . $path;
-}
+sub fixup { return "/tmp/fusetest-" . $ENV{LOGNAME} . shift }
 
 sub x_getattr {
        my ($file) = fixup(shift);
@@ -130,7 +119,18 @@ sub x_mknod {
        # and possibly run the real mknod command.
        my ($file, $modes, $dev) = @_;
        $file = fixup($file);
-       $! = 0;
+       undef $!;
+       if ($^O eq 'freebsd' || $^O eq 'darwin' || $^O eq 'netbsd') {
+               if (S_ISREG($modes)) {
+                       open(FILE, '>', $file) || return -$!;
+                       print FILE "";
+                       close(FILE);
+                       return 0;
+               } elsif (S_ISFIFO($modes)) {
+                       my ($rv) = POSIX::mkfifo($file, S_IMODE($modes));
+                       return $rv ? 0 : -POSIX::errno();
+               }
+       }
        syscall(&SYS_mknod,$file,$modes,$dev);
        return -$!;
 }