… | |
… | |
182 | print "read $_[0] bytes: <$buffer>\n"; |
182 | print "read $_[0] bytes: <$buffer>\n"; |
183 | }; |
183 | }; |
184 | |
184 | |
185 | =item aio_move $srcpath, $dstpath, $callback->($status) |
185 | =item aio_move $srcpath, $dstpath, $callback->($status) |
186 | |
186 | |
|
|
187 | [EXPERIMENTAL due to internal aio_group use] |
|
|
188 | |
187 | Try to move the I<file> (directories not supported as either source or |
189 | Try to move the I<file> (directories not supported as either source or |
188 | destination) from C<$srcpath> to C<$dstpath> and call the callback with |
190 | destination) from C<$srcpath> to C<$dstpath> and call the callback with |
189 | the C<0> (error) or C<-1> ok. |
191 | the C<0> (error) or C<-1> ok. |
190 | |
192 | |
191 | This is a composite request that tries to rename(2) the file first. If |
193 | This is a composite request that tries to rename(2) the file first. If |
… | |
… | |
201 | =cut |
203 | =cut |
202 | |
204 | |
203 | sub aio_move($$$) { |
205 | sub aio_move($$$) { |
204 | my ($src, $dst, $cb) = @_; |
206 | my ($src, $dst, $cb) = @_; |
205 | |
207 | |
206 | my $grp = aio_group; |
208 | my $grp = aio_group $cb; |
207 | |
209 | |
208 | add $grp aio_rename $src, $dst, sub { |
210 | add $grp aio_rename $src, $dst, sub { |
209 | if ($_[0] && $! == EXDEV) { |
211 | if ($_[0] && $! == EXDEV) { |
210 | add $grp aio_open $src, O_RDONLY, 0, sub { |
212 | add $grp aio_open $src, O_RDONLY, 0, sub { |
211 | if (my $src_fh = $_[0]) { |
213 | if (my $src_fh = $_[0]) { |
… | |
… | |
221 | chmod $stat[2] & 07777, $dst_fh; |
223 | chmod $stat[2] & 07777, $dst_fh; |
222 | chown $stat[4], $stat[5], $dst_fh; |
224 | chown $stat[4], $stat[5], $dst_fh; |
223 | close $dst_fh; |
225 | close $dst_fh; |
224 | |
226 | |
225 | add $grp aio_unlink $src, sub { |
227 | add $grp aio_unlink $src, sub { |
226 | $cb->($_[0]); |
228 | $grp->result ($_[0]); |
227 | }; |
229 | }; |
228 | } else { |
230 | } else { |
229 | my $errno = $!; |
231 | my $errno = $!; |
230 | add $grp aio_unlink $dst, sub { |
232 | add $grp aio_unlink $dst, sub { |
231 | $! = $errno; |
233 | $! = $errno; |
232 | $cb->(-1); |
234 | $grp->result (-1); |
233 | }; |
235 | }; |
234 | } |
236 | } |
235 | }; |
237 | }; |
236 | } else { |
238 | } else { |
237 | $cb->(-1); |
239 | $grp->result (-1); |
238 | } |
240 | } |
239 | }, |
241 | }, |
240 | |
242 | |
241 | } else { |
243 | } else { |
242 | $cb->(-1); |
244 | $grp->result (-1); |
243 | } |
245 | } |
244 | }; |
246 | }; |
245 | } else { |
247 | } else { |
246 | $cb->($_[0]); |
248 | $grp->result ($_[0]); |
247 | } |
249 | } |
248 | }; |
250 | }; |
249 | |
251 | |
250 | $grp |
252 | $grp |
251 | } |
253 | } |
… | |
… | |
343 | The callback a single argument which is either C<undef> or an array-ref |
345 | The callback a single argument which is either C<undef> or an array-ref |
344 | with the filenames. |
346 | with the filenames. |
345 | |
347 | |
346 | =item aio_scandir $path, $maxreq, $callback->($dirs, $nondirs) |
348 | =item aio_scandir $path, $maxreq, $callback->($dirs, $nondirs) |
347 | |
349 | |
|
|
350 | [EXPERIMENTAL due to internal aio_group use] |
|
|
351 | |
348 | Scans a directory (similar to C<aio_readdir>) but additionally tries to |
352 | Scans a directory (similar to C<aio_readdir>) but additionally tries to |
349 | separate the entries of directory C<$path> into two sets of names, ones |
353 | separate the entries of directory C<$path> into two sets of names, ones |
350 | you can recurse into (directories or links to them), and ones you cannot |
354 | you can recurse into (directories or links to them), and ones you cannot |
351 | recurse into (everything else). |
355 | recurse into (everything else). |
352 | |
356 | |
… | |
… | |
398 | =cut |
402 | =cut |
399 | |
403 | |
400 | sub aio_scandir($$$) { |
404 | sub aio_scandir($$$) { |
401 | my ($path, $maxreq, $cb) = @_; |
405 | my ($path, $maxreq, $cb) = @_; |
402 | |
406 | |
403 | my $grp = aio_group; |
407 | my $grp = aio_group $cb; |
404 | |
408 | |
405 | $maxreq = 8 if $maxreq <= 0; |
409 | $maxreq = 8 if $maxreq <= 0; |
406 | |
410 | |
407 | # stat once |
411 | # stat once |
408 | add $grp aio_stat $path, sub { |
412 | add $grp aio_stat $path, sub { |
409 | return $cb->() if $_[0]; |
413 | return $grp->result () if $_[0]; |
410 | my $now = time; |
414 | my $now = time; |
411 | my $hash1 = join ":", (stat _)[0,1,3,7,9]; |
415 | my $hash1 = join ":", (stat _)[0,1,3,7,9]; |
412 | |
416 | |
413 | # read the directory entries |
417 | # read the directory entries |
414 | add $grp aio_readdir $path, sub { |
418 | add $grp aio_readdir $path, sub { |
415 | my $entries = shift |
419 | my $entries = shift |
416 | or return $cb->(); |
420 | or return $grp->result (); |
417 | |
421 | |
418 | # stat the dir another time |
422 | # stat the dir another time |
419 | add $grp aio_stat $path, sub { |
423 | add $grp aio_stat $path, sub { |
420 | my $hash2 = join ":", (stat _)[0,1,3,7,9]; |
424 | my $hash2 = join ":", (stat _)[0,1,3,7,9]; |
421 | |
425 | |
… | |
… | |
426 | $ndirs = -1; |
430 | $ndirs = -1; |
427 | } else { |
431 | } else { |
428 | # if nlink == 2, we are finished |
432 | # if nlink == 2, we are finished |
429 | # on non-posix-fs's, we rely on nlink < 2 |
433 | # on non-posix-fs's, we rely on nlink < 2 |
430 | $ndirs = (stat _)[3] - 2 |
434 | $ndirs = (stat _)[3] - 2 |
431 | or return $cb->([], $entries); |
435 | or return $grp->result ([], $entries); |
432 | } |
436 | } |
433 | |
437 | |
434 | # sort into likely dirs and likely nondirs |
438 | # sort into likely dirs and likely nondirs |
435 | # dirs == files without ".", short entries first |
439 | # dirs == files without ".", short entries first |
436 | $entries = [map $_->[0], |
440 | $entries = [map $_->[0], |
… | |
… | |
452 | } |
456 | } |
453 | } elsif (!$nreq) { |
457 | } elsif (!$nreq) { |
454 | # finished |
458 | # finished |
455 | undef $statcb; |
459 | undef $statcb; |
456 | undef $schedcb; |
460 | undef $schedcb; |
457 | $cb->(\@dirs, \@nondirs) if $cb; |
461 | $grp->result (\@dirs, \@nondirs) if $cb; |
458 | undef $cb; |
462 | undef $cb; |
459 | } |
463 | } |
460 | }; |
464 | }; |
461 | $statcb = sub { |
465 | $statcb = sub { |
462 | my ($status, $entry) = @_; |
466 | my ($status, $entry) = @_; |
… | |
… | |
505 | callback with the fdatasync result code. |
509 | callback with the fdatasync result code. |
506 | |
510 | |
507 | If this call isn't available because your OS lacks it or it couldn't be |
511 | If this call isn't available because your OS lacks it or it couldn't be |
508 | detected, it will be emulated by calling C<fsync> instead. |
512 | detected, it will be emulated by calling C<fsync> instead. |
509 | |
513 | |
510 | =item aio_group $callback->() |
514 | =item aio_group $callback->(...) |
511 | |
515 | |
512 | [EXPERIMENTAL] |
516 | [EXPERIMENTAL] |
513 | |
517 | |
514 | This is a very special aio request: Instead of doing something, it is a |
518 | This is a very special aio request: Instead of doing something, it is a |
515 | container for other aio requests, which is useful if you want to bundle |
519 | container for other aio requests, which is useful if you want to bundle |
… | |
… | |
585 | You add requests by calling the C<add> method with one or more |
589 | You add requests by calling the C<add> method with one or more |
586 | C<IO::AIO::REQ> objects: |
590 | C<IO::AIO::REQ> objects: |
587 | |
591 | |
588 | $grp->add (aio_unlink "..."); |
592 | $grp->add (aio_unlink "..."); |
589 | |
593 | |
590 | add $grp aio_stat "...", sub { ... }; |
594 | add $grp aio_stat "...", sub { |
|
|
595 | $_[0] or return $grp->result ("error"); |
|
|
596 | |
|
|
597 | # add another request dynamically, if first succeeded |
|
|
598 | add $grp aio_open "...", sub { |
|
|
599 | $grp->result ("ok"); |
|
|
600 | }; |
|
|
601 | }; |
591 | |
602 | |
592 | This makes it very easy to create composite requests (see the source of |
603 | This makes it very easy to create composite requests (see the source of |
593 | C<aio_move> for an application) that work and feel like simple requests. |
604 | C<aio_move> for an application) that work and feel like simple requests. |
594 | |
605 | |
595 | The IO::AIO::GRP objects will be cleaned up during calls to |
606 | The IO::AIO::GRP objects will be cleaned up during calls to |
… | |
… | |
619 | Add one or more requests to the group. Any type of L<IO::AIO::REQ> can |
630 | Add one or more requests to the group. Any type of L<IO::AIO::REQ> can |
620 | be added, including other groups, as long as you do not create circular |
631 | be added, including other groups, as long as you do not create circular |
621 | dependencies. |
632 | dependencies. |
622 | |
633 | |
623 | Returns all its arguments. |
634 | Returns all its arguments. |
|
|
635 | |
|
|
636 | =item $grp->result (...) |
|
|
637 | |
|
|
638 | Set the result value(s) that will be passed to the group callback when all |
|
|
639 | subrequests have finished. By default, no argument will be passed. |
624 | |
640 | |
625 | =back |
641 | =back |
626 | |
642 | |
627 | =head2 SUPPORT FUNCTIONS |
643 | =head2 SUPPORT FUNCTIONS |
628 | |
644 | |