AnyEvent
view release on metacpan or search on metacpan
lib/AnyEvent/IO.pm view on Meta::CPAN
=item aio_open $path, $flags, $mode, $cb->($fh)
Tries to open the file specified by C<$path> with the O_XXX-flags
C<$flags> (from the Fcntl module, or see below) and the mode C<$mode> (a
good value is 0666 for C<O_CREAT>, and C<0> otherwise).
The (normal, standard, perl) file handle associated with the opened file
is then passed to the callback.
This works very much like Perl's C<sysopen> function.
Changing the C<umask> while this request executes results in undefined
behaviour - likewise changing anything else that would change the outcome,
such as your effective user or group ID.
To avoid having to load L<Fcntl>, this module provides constants
for C<O_RDONLY>, C<O_WRONLY>, C<O_RDWR>, C<O_CREAT>, C<O_EXCL>,
C<O_TRUNC> and C<O_APPEND> - you can either access them directly
(C<AnyEvent::IO::O_RDONLY>) or import them by specifying the C<:flags>
import tag (see SYNOPSIS).
Example: securely open a file in F</var/tmp>, fail if it exists or is a symlink.
use AnyEvent::IO qw(:flags);
aio_open "/var/tmp/mytmp$$", O_CREAT | O_EXCL | O_RDWR, 0600, sub {
my ($fh) = @_
or return AE::log error => "$! - denial of service attack?";
# now we have $fh
};
=item aio_close $fh, $cb->($success)
Closes the file handle (yes, close can block your process indefinitely)
and passes a true value to the callback on success.
Due to idiosyncrasies in perl, instead of calling C<close>, the file
handle might get closed by C<dup2>'ing another file descriptor over
it, that is, the C<$fh> might still be open, but can be closed safely
afterwards and must not be used for anything.
Example: close a file handle, and dirty as we are, do not even bother
to check for errors.
aio_close $fh, sub { };
=item aio_read $fh, $length, $cb->($data)
Tries to read C<$length> octets from the current position from C<$fh> and
passes these bytes to C<$cb>. Otherwise the semantics are very much like
those of Perl's C<sysread>.
If less than C<$length> octets have been read, C<$data> will contain
only those bytes actually read. At EOF, C<$data> will be a zero-length
string. If an error occurs, then nothing is passed to the callback.
Obviously, multiple C<aio_read>'s or C<aio_write>'s at the same time on file
handles sharing the underlying open file description results in undefined
behaviour, due to sharing of the current file offset (and less obviously
so, because OS X is not thread safe and corrupts data when you try).
Example: read 128 octets from a file.
aio_read $fh, 128, sub {
my ($data) = @_
or return AE::log error "read from fh: $!";
if (length $data) {
print "read ", length $data, " octets.\n";
} else {
print "EOF\n";
}
};
=item aio_seek $fh, $offset, $whence, $callback->($offs)
Seeks the filehandle to the new C<$offset>, similarly to Perl's
C<sysseek>. The C<$whence> are the traditional values (C<0> to count from
start, C<1> to count from the current position and C<2> to count from the
end).
The resulting absolute offset will be passed to the callback on success.
Example: measure the size of the file in the old-fashioned way using seek.
aio_seek $fh, 0, 2, sub {
my ($size) = @_
or return AE::log error => "seek to end failed: $!";
# maybe we need to seek to the beginning again?
aio_seek $fh, 0, 0, sub {
# now we are hopefully at the beginning
};
};
=item aio_write $fh, $data, $cb->($length)
Tries to write the octets in C<$data> to the current position of C<$fh>
and passes the actual number of bytes written to the C<$cb>. Otherwise the
semantics are very much like those of Perl's C<syswrite>.
If less than C<length $data> octets have been written, C<$length> will
reflect that. If an error occurs, then nothing is passed to the callback.
Obviously, multiple C<aio_read>'s or C<aio_write>'s at the same time on file
handles sharing the underlying open file description results in undefined
behaviour, due to sharing of the current file offset (and less obviously
so, because OS X is not thread safe and corrupts data when you try).
=item aio_truncate $fh_or_path, $new_length, $cb->($success)
Calls C<truncate> on the path or perl file handle and passes a true value
to the callback on success.
Example: truncate F</etc/passwd> to zero length - this only works on
systems that support C<truncate>, should not be tried out for obvious
reasons and debian will probably open yte another security bug about this
example.
aio_truncate "/etc/passwd", sub {
@_
or return AE::log error => "/etc/passwd: $! - are you root enough?";
};
=item aio_utime $fh_or_path, $atime, $mtime, $cb->($success)
Calls C<utime> on the path or perl file handle and passes a true value to
the callback on success.
The special case of both C<$atime> and C<$mtime> being C<undef> sets the
times to the current time, on systems that support this.
Example: try to touch F<file>.
aio_utime "file", undef, undef, sub { };
=item aio_chown $fh_or_path, $uid, $gid, $cb->($success)
Calls C<chown> on the path or perl file handle and passes a true value to
the callback on success.
If C<$uid> or C<$gid> can be specified as C<undef>, in which case the
uid or gid of the file is not changed. This differs from Perl's C<chown>
built-in, which wants C<-1> for this.
Example: update the group of F<file> to 0 (root), but leave the owner alone.
aio_chown "file", undef, 0, sub {
@_
or return AE::log error => "chown 'file': $!";
};
=item aio_chmod $fh_or_path, $perms, $cb->($success)
Calls C<chmod> on the path or perl file handle and passes a true value to
the callback on success.
Example: change F<file> to be user/group/world-readable, but leave the other flags
alone.
aio_stat "file", sub {
@_
or return AE::log error => "file: $!";
aio_chmod "file", (stat _)[2] & 07777 | 00444, sub { };
};
=item aio_stat $fh_or_path, $cb->($success)
( run in 1.441 second using v1.01-cache-2.11-cpan-140bd7fdf52 )