… | |
… | |
190 | use strict 'vars'; |
190 | use strict 'vars'; |
191 | |
191 | |
192 | use base 'Exporter'; |
192 | use base 'Exporter'; |
193 | |
193 | |
194 | BEGIN { |
194 | BEGIN { |
195 | our $VERSION = '2.3'; |
195 | our $VERSION = '2.32'; |
196 | |
196 | |
197 | our @AIO_REQ = qw(aio_sendfile aio_read aio_write aio_open aio_close aio_stat |
197 | our @AIO_REQ = qw(aio_sendfile aio_read aio_write aio_open aio_close aio_stat |
198 | aio_lstat aio_unlink aio_rmdir aio_readdir aio_scandir aio_symlink |
198 | aio_lstat aio_unlink aio_rmdir aio_readdir aio_scandir aio_symlink |
199 | aio_readlink aio_fsync aio_fdatasync aio_readahead aio_rename aio_link |
199 | aio_readlink aio_fsync aio_fdatasync aio_readahead aio_rename aio_link |
200 | aio_move aio_copy aio_group aio_nop aio_mknod); |
200 | aio_move aio_copy aio_group aio_nop aio_mknod aio_load aio_rmtree aio_mkdir); |
201 | our @EXPORT = (@AIO_REQ, qw(aioreq_pri aioreq_nice aio_block)); |
201 | our @EXPORT = (@AIO_REQ, qw(aioreq_pri aioreq_nice aio_block)); |
202 | our @EXPORT_OK = qw(poll_fileno poll_cb poll_wait flush |
202 | our @EXPORT_OK = qw(poll_fileno poll_cb poll_wait flush |
203 | min_parallel max_parallel max_idle |
203 | min_parallel max_parallel max_idle |
204 | nreqs nready npending nthreads |
204 | nreqs nready npending nthreads |
205 | max_poll_time max_poll_reqs); |
205 | max_poll_time max_poll_reqs); |
… | |
… | |
290 | list. They are the same as used by C<sysopen>. |
290 | list. They are the same as used by C<sysopen>. |
291 | |
291 | |
292 | Likewise, C<$mode> specifies the mode of the newly created file, if it |
292 | Likewise, C<$mode> specifies the mode of the newly created file, if it |
293 | didn't exist and C<O_CREAT> has been given, just like perl's C<sysopen>, |
293 | didn't exist and C<O_CREAT> has been given, just like perl's C<sysopen>, |
294 | except that it is mandatory (i.e. use C<0> if you don't create new files, |
294 | except that it is mandatory (i.e. use C<0> if you don't create new files, |
295 | and C<0666> or C<0777> if you do). |
295 | and C<0666> or C<0777> if you do). Note that the C<$mode> will be modified |
|
|
296 | by the umask in effect then the request is being executed, so better never |
|
|
297 | change the umask. |
296 | |
298 | |
297 | Example: |
299 | Example: |
298 | |
300 | |
299 | aio_open "/etc/passwd", O_RDONLY, 0, sub { |
301 | aio_open "/etc/passwd", O_RDONLY, 0, sub { |
300 | if ($_[0]) { |
302 | if ($_[0]) { |
… | |
… | |
430 | =item aio_rename $srcpath, $dstpath, $callback->($status) |
432 | =item aio_rename $srcpath, $dstpath, $callback->($status) |
431 | |
433 | |
432 | Asynchronously rename the object at C<$srcpath> to C<$dstpath>, just as |
434 | Asynchronously rename the object at C<$srcpath> to C<$dstpath>, just as |
433 | rename(2) and call the callback with the result code. |
435 | rename(2) and call the callback with the result code. |
434 | |
436 | |
|
|
437 | =item aio_mkdir $pathname, $mode, $callback->($status) |
|
|
438 | |
|
|
439 | Asynchronously mkdir (create) a directory and call the callback with |
|
|
440 | the result code. C<$mode> will be modified by the umask at the time the |
|
|
441 | request is executed, so do not change your umask. |
|
|
442 | |
435 | =item aio_rmdir $pathname, $callback->($status) |
443 | =item aio_rmdir $pathname, $callback->($status) |
436 | |
444 | |
437 | Asynchronously rmdir (delete) a directory and call the callback with the |
445 | Asynchronously rmdir (delete) a directory and call the callback with the |
438 | result code. |
446 | result code. |
439 | |
447 | |
… | |
… | |
443 | directory (i.e. opendir + readdir + closedir). The entries will not be |
451 | directory (i.e. opendir + readdir + closedir). The entries will not be |
444 | sorted, and will B<NOT> include the C<.> and C<..> entries. |
452 | sorted, and will B<NOT> include the C<.> and C<..> entries. |
445 | |
453 | |
446 | The callback a single argument which is either C<undef> or an array-ref |
454 | The callback a single argument which is either C<undef> or an array-ref |
447 | with the filenames. |
455 | with the filenames. |
|
|
456 | |
|
|
457 | =item aio_load $path, $data, $callback->($status) |
|
|
458 | |
|
|
459 | This is a composite request that tries to fully load the given file into |
|
|
460 | memory. Status is the same as with aio_read. |
|
|
461 | |
|
|
462 | =cut |
|
|
463 | |
|
|
464 | sub aio_load($$;$) { |
|
|
465 | aio_block { |
|
|
466 | my ($path, undef, $cb) = @_; |
|
|
467 | my $data = \$_[1]; |
|
|
468 | |
|
|
469 | my $pri = aioreq_pri; |
|
|
470 | my $grp = aio_group $cb; |
|
|
471 | |
|
|
472 | aioreq_pri $pri; |
|
|
473 | add $grp aio_open $path, O_RDONLY, 0, sub { |
|
|
474 | my ($fh) = @_ |
|
|
475 | or return $grp->result (-1); |
|
|
476 | |
|
|
477 | aioreq_pri $pri; |
|
|
478 | add $grp aio_read $fh, 0, (-s $fh), $$data, 0, sub { |
|
|
479 | $grp->result ($_[0]); |
|
|
480 | }; |
|
|
481 | }; |
|
|
482 | |
|
|
483 | $grp |
|
|
484 | } |
|
|
485 | } |
448 | |
486 | |
449 | =item aio_copy $srcpath, $dstpath, $callback->($status) |
487 | =item aio_copy $srcpath, $dstpath, $callback->($status) |
450 | |
488 | |
451 | Try to copy the I<file> (directories not supported as either source or |
489 | Try to copy the I<file> (directories not supported as either source or |
452 | destination) from C<$srcpath> to C<$dstpath> and call the callback with |
490 | destination) from C<$srcpath> to C<$dstpath> and call the callback with |
… | |
… | |
604 | as those tend to return 0 or 1 as link counts, which disables the |
642 | as those tend to return 0 or 1 as link counts, which disables the |
605 | directory counting heuristic. |
643 | directory counting heuristic. |
606 | |
644 | |
607 | =cut |
645 | =cut |
608 | |
646 | |
609 | sub aio_scandir($$$) { |
647 | sub aio_scandir($$;$) { |
610 | aio_block { |
648 | aio_block { |
611 | my ($path, $maxreq, $cb) = @_; |
649 | my ($path, $maxreq, $cb) = @_; |
612 | |
650 | |
613 | my $pri = aioreq_pri; |
651 | my $pri = aioreq_pri; |
614 | |
652 | |
… | |
… | |
692 | |
730 | |
693 | $grp |
731 | $grp |
694 | } |
732 | } |
695 | } |
733 | } |
696 | |
734 | |
|
|
735 | =item aio_rmtree $path, $callback->($status) |
|
|
736 | |
|
|
737 | Delete a directory tree starting (and including) C<$path>, return the |
|
|
738 | status of the final C<rmdir> only. This is a composite request that |
|
|
739 | uses C<aio_scandir> to recurse into and rmdir directories, and unlink |
|
|
740 | everything else. |
|
|
741 | |
|
|
742 | =cut |
|
|
743 | |
|
|
744 | sub aio_rmtree; |
|
|
745 | sub aio_rmtree($;$) { |
|
|
746 | aio_block { |
|
|
747 | my ($path, $cb) = @_; |
|
|
748 | |
|
|
749 | my $pri = aioreq_pri; |
|
|
750 | my $grp = aio_group $cb; |
|
|
751 | |
|
|
752 | aioreq_pri $pri; |
|
|
753 | add $grp aio_scandir $path, 0, sub { |
|
|
754 | my ($dirs, $nondirs) = @_; |
|
|
755 | |
|
|
756 | my $dirgrp = aio_group sub { |
|
|
757 | add $grp aio_rmdir $path, sub { |
|
|
758 | $grp->result ($_[0]); |
|
|
759 | }; |
|
|
760 | }; |
|
|
761 | |
|
|
762 | (aioreq_pri $pri), add $dirgrp aio_rmtree "$path/$_" for @$dirs; |
|
|
763 | (aioreq_pri $pri), add $dirgrp aio_unlink "$path/$_" for @$nondirs; |
|
|
764 | |
|
|
765 | add $grp $dirgrp; |
|
|
766 | }; |
|
|
767 | |
|
|
768 | $grp |
|
|
769 | } |
|
|
770 | } |
|
|
771 | |
697 | =item aio_fsync $fh, $callback->($status) |
772 | =item aio_fsync $fh, $callback->($status) |
698 | |
773 | |
699 | Asynchronously call fsync on the given filehandle and call the callback |
774 | Asynchronously call fsync on the given filehandle and call the callback |
700 | with the fsync result code. |
775 | with the fsync result code. |
701 | |
776 | |