AnyEvent
view release on metacpan or search on metacpan
lib/AnyEvent/IO.pm view on Meta::CPAN
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)
=item aio_lstat $path, $cb->($success)
Calls C<stat> or C<lstat> on the path or perl file handle and passes a
true value to the callback on success.
The stat data will be available by C<stat>'ing the C<_> file handle
(e.g. C<-x _>, C<stat _> and so on).
Example: see if we can find the number of subdirectories of F</etc>.
aio_stat "/etc", sub {
@_
or return AE::log error => "/etc: $!";
(stat _)[3] >= 2
or return AE::log warn => "/etc has low link count - non-POSIX filesystem?";
print "/etc has ", (stat _)[3] - 2, " subdirectories.\n";
};
=item aio_link $oldpath, $newpath, $cb->($success)
Calls C<link> on the paths and passes a true value to the callback on
success.
Example: link "F<file> to F<file.bak>, then rename F<file.new> over F<file>,
to atomically replace it.
aio_link "file", "file.bak", sub {
@_
or return AE::log error => "file: $!";
aio_rename "file.new", "file", sub {
@_
( run in 0.594 second using v1.01-cache-2.11-cpan-ceb78f64989 )