ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/lib/cf.pm
(Generate patch)

Comparing deliantra/server/lib/cf.pm (file contents):
Revision 1.580 by root, Wed Jan 4 03:22:48 2012 UTC vs.
Revision 1.581 by root, Fri Feb 3 02:04:11 2012 UTC

372 $d =~ s/([\x00-\x07\x09\x0b\x0c\x0e-\x1f])/sprintf "\\x%02x", ord($1)/ge; 372 $d =~ s/([\x00-\x07\x09\x0b\x0c\x0e-\x1f])/sprintf "\\x%02x", ord($1)/ge;
373 $d 373 $d
374 } || "[unable to dump $_[0]: '$@']"; 374 } || "[unable to dump $_[0]: '$@']";
375} 375}
376 376
377=item $scalar = load_file $path 377=item $scalar = cf::load_file $path
378 378
379Loads the given file from path and returns its contents. Croaks on error 379Loads the given file from path and returns its contents. Croaks on error
380and can block. 380and can block.
381 381
382=cut 382=cut
384sub load_file($) { 384sub load_file($) {
385 0 <= aio_load $_[0], my $data 385 0 <= aio_load $_[0], my $data
386 or Carp::croak "$_[0]: $!"; 386 or Carp::croak "$_[0]: $!";
387 387
388 $data 388 $data
389}
390
391=item $success = cf::replace_file $path, $data, $sync
392
393Atomically replaces the file at the given $path with new $data, and
394optionally $sync the data to disk before replacing the file.
395
396=cut
397
398sub replace_file($$;$) {
399 my ($path, $data, $sync) = @_;
400
401 my $lock = cf::lock_acquire ("replace_file:$path");
402
403 my $fh = aio_open "$path~", Fcntl::O_WRONLY | Fcntl::O_CREAT | Fcntl::O_TRUNC, 0644
404 or return;
405
406 $data = $data->() if ref $data;
407
408 length $data == aio_write $fh, 0, (length $data), $data, 0
409 or return;
410
411 !$sync
412 or !aio_fsync $fh
413 or return;
414
415 aio_close $fh
416 and return;
417
418 aio_rename "$path~", $path
419 and return;
420
421 if ($sync) {
422 $path =~ s%/[^/]*$%%;
423 aio_pathsync $path;
424 }
425
426 1
389} 427}
390 428
391=item $ref = cf::decode_json $json 429=item $ref = cf::decode_json $json
392 430
393Converts a JSON string into the corresponding perl data structure. 431Converts a JSON string into the corresponding perl data structure.

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines