Filesys-POSIX

 view release on metacpan or  search on metacpan

t/file.t  view on Meta::CPAN


    throws_errno_ok {
        $fs->open( 'foo', $O_CREAT | $O_WRONLY | $O_EXCL );
    }
    &Errno::EEXIST, "Filesys::POSIX->open() prevents clobbering existing inodes with \$O_CREAT | \$O_EXCL";

    throws_errno_ok {
        $fs->rename( 'meow', 'foo' );
    }
    &Errno::ENOTDIR, "Filesys::POSIX->rename() prevents replacing directories with non-directories";

    throws_errno_ok {
        $fs->rename( 'foo', 'meow' );
    }
    &Errno::EISDIR, "Filesys::POSIX->rename() prevents replacing non-directories with directories";
}

{
    my $fs    = Filesys::POSIX->new( Filesys::POSIX::Mem->new );
    my $fd    = $fs->open( 'foo', $O_CREAT | $O_WRONLY, 0644 );
    my $inode = $fs->fstat($fd);
    $fs->close($fd);

    $fs->mkpath('eins/zwei/drei');
    $fs->symlink( 'zwei', 'eins/foo' );

    ok(
        $fs->stat('eins/zwei/drei') eq $fs->lstat('eins/foo/drei'),
        "Filesys::POSIX->lstat() resolves symlinks in tree"
    );

    throws_errno_ok {
        $fs->readlink('foo');
    }
    &Errno::EINVAL, "Filesys::POSIX->readlink() fails on non-symlink inodes";

    $fs->symlink( 'foo', 'bar' );
    my $link = $fs->lstat('bar');

    ok(
        $inode eq $fs->stat('bar'),
        "Filesys::POSIX->stat() works on symlinks"
    );
    ok(
        $fs->readlink('bar') eq 'foo',
        "Filesys::POSIX->readlink() returns expected result"
    );
}

{
    my $fs    = Filesys::POSIX->new( Filesys::POSIX::Mem->new );
    my $fd    = $fs->open( '/foo', $O_CREAT, $S_IFDIR | 0755 );
    my $inode = $fs->fstat($fd);

    $fs->fchdir($fd);
    ok(
        $fs->getcwd eq '/foo',
        "Filesys::POSIX->fchdir() changes current directory when passed a directory fd"
    );

    $fs->fchown( $fd, 500, 500 );
    ok(
        $inode->{'uid'} == 500,
        "Filesys::POSIX->fchown() updates inode's uid properly"
    );
    ok(
        $inode->{'gid'} == 500,
        "Filesys::POSIX->fchown() updates inode's gid properly"
    );

    $fs->fchmod( $fd, 0700 );
    ok(
        ( $inode->{'mode'} & $S_IPERM ) == 0700,
        "Filesys::POSIX->fchmod() updates inode's permissions properly"
    );
}

{
    my $fs = Filesys::POSIX->new( Filesys::POSIX::Mem->new );

    foreach my $dir (qw(dev tmp var)) {
        $fs->mkdir($dir);
    }

    {
        my $inode = $fs->mknod( '/dev/null', $S_IFCHR | 0666, ( 1 << 16 ) | 3 );

        ok(
            $inode->char,
            'Filesys::POSIX->mknod() creates character devices approrpiately'
        );
        is(
            $inode->major, 1,
            'Filesys::POSIX::Inode->major() returns correct value on char devices'
        );
        is(
            $inode->minor, 3,
            'Filesys::POSIX::Inode->minor() returns correct value on char devices'
        );
    }

    {
        my $inode = $fs->mknod( '/dev/mem', $S_IFBLK | 0644, ( 1 << 16 ) | 1 );

        ok(
            $inode->block,
            'Filesys::POSIX->mknod() creates block devices appropriately'
        );
        is(
            $inode->major, 1,
            'Filesys::POSIX::Inode->major() returns correct value on block devices'
        );
        is(
            $inode->minor, 1,
            'Filesys::POSIX::Inode->minor() returns correct value on block devices'
        );
    }

    {
        my $inode = $fs->mknod( '/tmp/foo', $S_IFREG | 0644, ( 1 << 16 ) | 4 );

        throws_errno_ok {
            $inode->major;
        }
        &Errno::EINVAL, 'Filesys::POSIX::Inode->major() dies on non-char, non-block inodes';

        throws_errno_ok {
            $inode->minor;



( run in 0.803 second using v1.01-cache-2.11-cpan-71847e10f99 )