1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561
|
# Paranoid::IO -- Paranoid IO support
#
# $Id: lib/Paranoid/IO.pm, 2.10 2022/03/08 00:01:04 acorliss Exp $
#
# This software is free software. Similar to Perl, you can redistribute it
# and/or modify it under the terms of either:
#
# a) the GNU General Public License
# <https://www.gnu.org/licenses/gpl-1.0.html> as published by the
# Free Software Foundation <http://www.fsf.org/>; either version 1
# <https://www.gnu.org/licenses/gpl-1.0.html>, or any later version
# <https://www.gnu.org/licenses/license-list.html#GNUGPL>, or
# b) the Artistic License 2.0
# <https://opensource.org/licenses/Artistic-2.0>,
#
# subject to the following additional term: No trademark rights to
# "Paranoid" have been or are conveyed under any of the above licenses.
# However, "Paranoid" may be used fairly to describe this unmodified
# software, in good faith, but not as a trademark.
#
# (c) 2005 - 2021, Arthur Corliss (corliss@digitalmages.com)
# (tm) 2008 - 2021, Paranoid Inc. (www.paranoid.com)
#
#####################################################################
#####################################################################
#
# Environment definitions
#
#####################################################################
package Paranoid::IO;
use 5.008;
use strict;
use warnings;
use vars qw($VERSION @EXPORT @EXPORT_OK %EXPORT_TAGS);
use base qw(Exporter);
use Cwd qw(realpath);
use Fcntl qw(:DEFAULT :flock :mode :seek);
use Paranoid;
use Paranoid::Debug qw(:all);
use Paranoid::Input;
use IO::Handle;
($VERSION) = ( q$Revision: 2.10 $ =~ /(\d+(?:\.\d+)+)/sm );
@EXPORT = qw(pclose pcloseAll popen preopen ptell pseek pflock pread
pnlread pwrite pnlwrite pappend pnlappend ptruncate pnltruncate);
@EXPORT_OK = ( @EXPORT, qw(PIOBLKSIZE PIOMAXFSIZE PIOLOCKSTACK) );
%EXPORT_TAGS = ( all => [@EXPORT_OK] );
use constant PDEFPERM => 0666;
use constant PDEFMODE => O_CREAT | O_RDWR;
use constant PDEFBLKSZ => 4096;
use constant PDEFFILESZ => 65536;
use constant PFLMASK => LOCK_SH | LOCK_EX | LOCK_UN;
use constant PIGNMFLAGS => O_TRUNC | O_CREAT | O_EXCL;
#####################################################################
#
# Module code follows
#
#####################################################################
{
my $mblksz = PDEFBLKSZ;
sub PIOBLKSIZE : lvalue {
# Purpose: Gets/sets default block size for I/O
# Returns: $mblksz
# Usage: PIOBLKSIZE = $bytes;
$mblksz;
}
my $mfsz = PDEFFILESZ;
sub PIOMAXFSIZE : lvalue {
# Purpose: Gets/sets default max file size for I/O
# Returns: $mfsz
# Usage: PIOMAXFSIZE = bytes;
$mfsz;
}
my %lstack;
my $lsflag = 0;
sub PIOLOCKSTACK : lvalue {
# Purpose: Enables/disables the flock lock stack
# Returns: $lsflag
# Usage: PIOLOCKSTACK = 1;
$lsflag;
}
# %files: {name} => {
# pid => $pid,
# mode => $mode,
# perms => $perms,
# fh => $fh,
# real => $realpath,
# ltype => $lock,
# }
my %files;
sub _pfFhind ($) {
# Purpose: Searches for a filename based on the
# current file handle
# Returns: String/undefined
# Usage: $rv = _pfFhind($fh);
my $fh = shift;
my $rv;
subPreamble( PDLEVEL4, '$', $fh );
if ( defined $fh and ref $fh eq 'GLOB' ) {
foreach ( keys %files ) {
if ( $files{$_}{fh} eq $fh ) {
$rv = $_ and last;
}
}
}
subPostamble( PDLEVEL4, '$', $rv );
return $rv;
}
sub pclose ($) {
# Purpose: Closes a cached file handle
# Returns: Boolean
# Usage: $rv = plcose($filename)
# Usage: $rv = plcose($fh)
my $filename = shift;
my $rv = 1;
my $fh;
subPreamble( PDLEVEL1, '$', $filename );
if ( defined $filename ) {
# Get the missing variable
if ( ref $filename eq 'GLOB' ) {
$fh = $filename;
$filename = _pfFhind($fh);
} else {
$fh = $files{$filename}{fh} if exists $files{$filename};
}
# Close the filehandle
if ( defined $fh and fileno $fh ) {
flock $fh, LOCK_UN;
$rv = close $fh;
}
# Clean up internal data structures
if ( defined $filename ) {
delete $files{$filename};
delete $lstack{$filename};
}
Paranoid::ERROR =
pdebug( 'error closing file handle: %s', PDLEVEL1, $! )
unless $rv;
}
subPostamble( PDLEVEL1, '$', $rv );
return $rv;
}
sub pcloseAll {
# Purpose: Closes all filehandles
# Returns: Boolean
# Usage: $rv = pcloseAll();
my @files = @_;
my $rv = 1;
subPreamble( PDLEVEL1, '@', @files );
@files = keys %files unless @files;
foreach (@files) {
$rv = 0 unless pclose($_);
}
subPostamble( PDLEVEL1, '$', $rv );
return $rv;
}
sub _open {
# Purpose: Performs the sysopen call
# Returns: rv of sysopen
# Usage: $rv = _open($filename);
# Usage: $rv = _open($filename, $mode);
# Usage: $rv = _open($filename, $mode, $perms);
my $filename = shift;
my $mode = shift;
my $perms = shift;
my ( %tmp, $f, $fh, $rv );
subPreamble( PDLEVEL3, '$;$$', $filename, $mode, $perms );
if ( defined $filename ) {
# Detaint mode/perms
$rv = 1;
$mode = PDEFMODE unless defined $mode;
$perms = PDEFPERM unless defined $perms;
unless ( detaint( $mode, 'int' ) ) {
$rv = 0;
Paranoid::ERROR =
pdebug( 'invalid mode passed: %s', PDLEVEL1, $mode );
}
unless ( detaint( $perms, 'int' ) ) {
$rv = 0;
Paranoid::ERROR =
pdebug( 'invalid perm passed: %s', PDLEVEL1, $perms );
}
# Prep file record
%tmp = (
mode => $mode,
perms => $perms,
pid => $$,
ltype => LOCK_UN,
);
# Detaint filename
if ($rv) {
if ( detaint( $filename, 'filename', $f ) ) {
# Attempt to open the fila
$rv =
( $tmp{mode} & O_CREAT )
? sysopen $fh, $f, $tmp{mode}, $tmp{perms}
: sysopen $fh,
$f, $tmp{mode};
if ($rv) {
$tmp{fh} = $fh;
$tmp{real} = realpath($filename);
$files{$filename} = {%tmp};
} else {
Paranoid::ERROR = pdebug( 'failed to open %s: %s',
PDLEVEL1, $filename, $! );
}
} else {
Paranoid::ERROR =
pdebug( 'failed to detaint %s', PDLEVEL1, $filename );
}
}
}
subPostamble( PDLEVEL3, '$', $rv );
return $rv;
}
sub _reopen {
# Purpose: Reopens an open file handle
# Returns: rv of _open
# Usage: $rv = _reopen($filename);
# Usage: $rv = _reopen($fh);
my $filename = shift;
my ( %tmp, $fh, $pos, $rv, $af );
subPreamble( PDLEVEL3, '$', $filename );
if ( defined $filename and exists $files{$filename} ) {
# Get a copy of the file record
%tmp = %{ $files{$filename} };
$fh = $tmp{fh};
# Get the current cursor position
$pos = fileno $fh ? sysseek $fh, 0, SEEK_CUR : 0;
$af = $fh->autoflush;
# Close the old file handle
$tmp{fh} = $fh = undef;
if ( pclose($filename) ) {
# Reopen should ignore O_TRUNC, O_CREAT, and O_EXCL on reopens
$tmp{mode} &= ~PIGNMFLAGS if $tmp{mode} & PIGNMFLAGS;
# Open the file and move the cursor back where it was
$rv = _open( @tmp{qw(real mode perms)} );
if ($rv) {
# Move the cursor back to where it was
$fh = $files{ $tmp{real} }{fh};
$fh->autoflush($af);
$rv = sysseek $fh, $pos, SEEK_SET;
# Move the record over to the original file name
$files{$filename} = { %{ $files{ $tmp{real} } } };
delete $files{ $tmp{real} } if $filename ne $tmp{real};
# Delete any existing lock stack
delete $lstack{$filename};
}
}
}
subPostamble( PDLEVEL3, '$', $rv );
return $rv;
}
sub popen {
# Purpose: Performs a sysopen with file descriptor caching
# Returns: file handle
# Usage: $fh = popen($filename, $mode, $perms);
my $filename = shift;
my $mode = shift;
my $perms = shift;
my ( %tmp, $fh, $f, $pos, $rv );
subPreamble( PDLEVEL2, '$;$$', $filename, $mode, $perms );
# Make sure we weren't passed a file handle, but if we
# were attempt to find the actual filename
if ( defined $filename ) {
if ( ref $filename eq 'GLOB' ) {
$fh = $filename;
$filename = _pfFhind($filename);
} else {
$fh = $files{$filename}{fh} if exists $files{$filename};
}
}
if ( defined $filename and exists $files{$filename} ) {
# Make sure pid is the same
if ( $files{$filename}{pid} == $$ ) {
if ( fileno $fh ) {
# Return existing filehandle
pdebug( 'returning cached file handle', PDLEVEL2 );
$rv = $fh;
} else {
# Reopen a filehandle that was closed outside
# of this module
pdebug( 'reopening closed file handle', PDLEVEL2 );
$rv = $files{$filename}{fh} if _reopen($filename);
}
} else {
pdebug( 'reopening inherited file handle in child',
PDLEVEL2 );
$rv = $files{$filename}{fh} if _reopen($filename);
}
} elsif ( defined $filename ) {
pdebug( 'opening new file handle', PDLEVEL2 );
$rv = $files{$filename}{fh} if _open( $filename, $mode, $perms );
} elsif ( !defined $filename and defined $fh ) {
Paranoid::ERROR =
pdebug( 'popen called with an unmanaged file handle',
PDLEVEL1 );
$rv = fileno $fh ? $fh : undef;
} else {
Paranoid::ERROR =
pdebug( 'attempted to open a file with an undefined name',
PDLEVEL1 );
}
subPostamble( PDLEVEL2, '$', $rv );
return $rv;
}
sub preopen {
# Purpose: Reopens either the named files or all
# Returns: Boolean
# Usage: $rv = preopen();
# Usage: $rv = preopen(@filenames);
my @files = @_;
my $rv = 1;
subPreamble( PDLEVEL2, '@', @files );
@files = keys %files unless @files;
foreach (@files) { $rv = 0 unless _reopen($_) }
subPostamble( PDLEVEL2, '$', $rv );
return $rv;
}
sub _pflock {
# Purpose: Performs file-locking operations on the passed filename
# Returns: Boolean
# Usage: $rv = _pflock($filename, LOCK_EX);
my $filename = shift;
my $lock = shift;
my ( $rv, $fh, $rl );
local $!;
subPreamble( PDLEVEL3, '$$', $filename, $lock );
if ( defined $filename ) {
# Get the missing variable
if ( ref $filename eq 'GLOB' ) {
$fh = $filename;
$filename = _pfFhind($fh);
} else {
$fh = $files{$filename}{fh} if exists $files{$filename};
}
if ( defined $fh ) {
# Apply the lock
$rl = $lock & PFLMASK;
$rv = flock $fh, $lock;
# Record change to internal state if we're tracking this file
if ($rv) {
if ( defined $filename and exists $files{$filename} ) {
$files{$filename}{ltype} = $rl;
} else {
pdebug(
'flock succeeded on file opened outside of the'
. ' Paranoid::IO framework (%s)',
PDLEVEL1, $filename
);
}
} else {
pdebug(
( ( $lock & LOCK_NB ) ? 'non-blocking' : '' )
. 'flock attempt failed on %s',
PDLEVEL1, $filename
);
}
}
}
subPostamble( PDLEVEL3, '$', $rv );
return $rv;
}
sub _plsflock {
my $filename = shift;
my $lock = shift;
my ( $fh, $stack, $rl, $ll, $lsl, $rv );
subPreamble( PDLEVEL3, '$$', $filename, $lock );
# Var Key:
# lock: lock passed to function (can include LOCK_NB)
# rl: real lock (stripping LOCK_NB)
# ll: last lock (as performed by last _pflock()
# lsl: last lock recorded in the lock stack
# Translate glob to filename for lock stack tracking purposes
$fh = $filename;
$filename = _pfFhind($filename) if ref $filename eq 'GLOB';
# Get the current lock state
$ll = $files{$filename}{ltype}
if defined $filename and exists $files{$filename};
if ( defined $ll ) {
# Get the real lock level for comparison
$rl = $lock & PFLMASK;
# File has been opened, at least, with popen, and has a locktype
# entry
$lstack{$filename} = [] unless exists $lstack{$filename};
$stack = $lstack{$filename};
$lsl = $$stack[-1];
#warn "lock: $lock\nrl: $rl\nll: $ll\nlsl: $lsl\n";
pdebug(
'something has gone awry during lock tracking.'
. 'll: %s lsl: %s',
PDLEVEL1, $ll, $lsl
)
if defined $lsl
and $lsl != $ll;
# Adjust as necessary
if ( $rl == LOCK_UN ) {
# Remove a lock from the stack
pop @$stack;
if ( scalar @$stack ) {
# Still have locks in the stack that must not be degraded
$rv = 1;
if ( $ll != $$stack[-1] ) {
# Apply the new level
$rv = _pflock( $filename, $$stack[-1] );
}
} else {
# No locks in the stack to preserve, so go ahead and
# release the lock
$rv = _pflock( $filename, LOCK_UN );
}
} elsif ( $rl == LOCK_SH ) {
# Upgrade lock to preserve previous exclusive lock on the
# stack, if necessary
if ( defined $lsl and $lsl == LOCK_EX ) {
$lock = ( LOCK_EX | ( $lock & LOCK_NB ) );
$rl = LOCK_EX;
}
$rv = $ll == $rl ? 1 : _pflock( $filename, $lock );
push @$stack, $rl if $rv;
} elsif ( $rl == LOCK_EX ) {
push @$stack, $rl;
$rv = $ll == $rl ? 1 : _pflock( $filename, $lock );
} else {
pdebug( 'unknown lock type: %x', PDLEVEL1, $lock );
}
# Report some diagnostics
if ( scalar @$stack ) {
pdebug( 'lock stack depth: %s', PDLEVEL4, scalar @$stack );
if ( $ll == $$stack[-1] ) {
pdebug( 'preserved lock at %s', PDLEVEL4, $ll );
} else {
pdebug( 'switched lock from %s to %s',
PDLEVEL4, $ll, $$stack[-1] );
}
} else {
pdebug( 'no locks remaining', PDLEVEL4 );
}
# Delete empty stacks to avoid memory leaks
delete $lstack{$filename} unless scalar @$stack;
} else {
if ( defined $fh and !defined $filename ) {
$rv = _pflock( $fh, $lock );
} else {
pdebug( 'file %s is unknown to Paranoid::IO so far',
PDLEVEL1, $filename );
}
}
subPostamble( PDLEVEL3, '$', $rv );
return $rv;
}
sub pflock {
# Purpose: Performs file-locking operations on the passed filename
# Returns: Boolean
# Usage: $rv = pflock($filename, LOCK_EX);
my $filename = shift;
my $lock = shift;
my ( $rv, $fh );
subPreamble( PDLEVEL2, '$$', $filename, $lock );
# NOTE: retrieving the file handle might seem silly, but if a process
# is forked, and the first thing they do on a file is apply an flock,
# the first I/O operation will close and reopen the file to avoid
# confusion with the parent process and, therefore, losing the lock.
#
# End sum, this is a necessary evil in order to preserve locks a
# before any effective I/O is done in the child.
if ( defined $filename ) {
$fh = popen($filename);
$rv =
PIOLOCKSTACK()
? _plsflock( $filename, $lock )
: _pflock( $filename, $lock );
}
subPostamble( PDLEVEL2, '$', $rv );
return $rv;
}
sub plockstat {
# Purpose: Returns the the status of the last lock applied via
# pflock()
# Returns: LOCK_*
# Usage: $lock = plockstat($filename);
my $filename = shift;
my $rv;
subPreamble( PDLEVEL2, '$', $filename );
if ( defined $filename ) {
# Get the missing variable
$filename = _pfFhind($filename) if ref $filename eq 'GLOB';
if ( defined $filename and exists $files{$filename} ) {
$rv = $files{$filename}{ltype};
} else {
pdebug(
'attempted to retrieve lock status for file not opened'
. ' with the Paranoid::IO framework (%s)',
PDLEVEL1, $filename
);
}
}
subPostamble( PDLEVEL2, '$', $rv );
return $rv;
}
}
sub ptell {
# Purpose: Returns the cursor position in the file handle
# Returns: Integer
# Usage: $pos = ptell($filename);
my $filename = shift;
my ( $rv, $fh );
local $!;
subPreamble( PDLEVEL2, '$', $filename );
if ( defined $filename ) {
$fh = popen( $filename, O_RDWR );
if ( defined $fh ) {
$rv = sysseek $fh, 0, SEEK_CUR;
Paranoid::ERROR =
pdebug( 'error attempting to ptell: %s', PDLEVEL1, $! )
unless $rv;
}
}
subPostamble( PDLEVEL2, '$', $rv );
return $rv;
}
sub pseek {
# Purpose: Performs a sysseek
# Returns: Integer/undef
# Usage: $cur = pseek($filename, $curpos, $whence);
my $filename = shift;
my $setpos = shift;
my $whence = shift;
my ( $rv, $fh );
local $!;
subPreamble( PDLEVEL2, '$$;$', $filename, $setpos, $whence );
if ( defined $filename ) {
$fh = popen( $filename, O_RDWR );
if ( defined $fh ) {
$whence = SEEK_SET unless defined $whence;
$rv = sysseek $fh, $setpos, $whence;
Paranoid::ERROR =
pdebug( 'error attempting to pseek: %s', PDLEVEL1, $! )
unless $rv;
}
}
subPostamble( PDLEVEL2, '$', $rv );
return $rv;
}
sub pwrite {
# Purpose: Performs a syswrite w/locking
# Returns: Integer/undef
# Usage: $bytes = pwrite($filename, $text);
# Usage: $bytes = pwrite($filename, $text, $length);
# Usage: $bytes = pwrite($filename, $text, $length, $offset);
# Usage: $bytes = pwrite($filename, $text, $length, $offset, $nolock);
my $filename = shift;
my $out = shift;
my $wlen = shift;
my $offset = shift;
my $nolock = shift;
my $bytes = defined $out ? length $out : 0;
my ( $fh, $rv );
subPreamble( PDLEVEL2, '$$;$$$', $filename, $bytes, $wlen, $offset,
$nolock );
if ( defined $filename and defined $out and length $out ) {
# Opportunistically open a file handle if needed,
# otherwise, just retrieve the existing file handle
$fh = popen( $filename, O_WRONLY | O_CREAT );
# Smoke 'em if you got'em...
if ( defined $fh ) {
if ( $nolock or pflock( $filename, LOCK_EX ) ) {
$wlen = length $out unless defined $wlen;
$offset = 0 unless defined $offset;
$rv = syswrite $fh, $out, $wlen, $offset;
if ( defined $rv ) {
pdebug( 'wrote %d bytes', PDLEVEL2, $rv );
} else {
Paranoid::ERROR =
pdebug( 'failed to write to file handle: %s',
PDLEVEL1, $! );
}
pflock( $filename, LOCK_UN ) unless $nolock;
}
}
}
subPostamble( PDLEVEL2, '$', $rv );
return $rv;
}
sub pnlwrite {
# Purpose: Wrapper for pwrite w/o internal flocking
# Returns: RV of pwrite
# Usage: $bytes = pnlwrite($filename, $text, $length);
# Usage: $bytes = pnlwrite($filename, $text, $length, $offset);
my $filename = shift;
my $out = shift;
my $wlen = shift;
my $offset = shift;
return pwrite( $filename, $out, $wlen, $offset, 1 );
}
sub pappend {
# Purpose: Appends the data to the end of the file,
# but does not move the file cursor
# Returns: Integer/undef
# Usage: $rv = pappend($filename, $content);
# Usage: $rv = pappend($filename, $content, $length);
# Usage: $rv = pappend($filename, $content, $length, $offset);
my $filename = shift;
my $out = shift;
my $wlen = shift;
my $offset = shift;
my $nolock = shift;
my ( $fh, $pos, $rv );
subPreamble( PDLEVEL2, '$$;$$', $filename, $out, $wlen, $offset,
$nolock );
if ( defined $filename and defined $out and length $out ) {
# Opportunistically open a file handle in append mode
$fh = popen( $filename, O_WRONLY | O_CREAT | O_APPEND );
# Smoke 'em if you got'em...
if ( defined $fh ) {
# Lock the file
if ( $nolock or pflock( $filename, LOCK_EX ) ) {
# Save the current position
$pos = sysseek $fh, 0, SEEK_CUR;
# Seek to the end of the file
if ( $pos and sysseek $fh, 0, SEEK_END ) {
# write the content
$wlen = length $out unless defined $wlen;
$offset = 0 unless defined $offset;
$rv = syswrite $fh, $out, $wlen, $offset;
if ( defined $rv ) {
pdebug( 'wrote %d bytes', PDLEVEL2, $rv );
} else {
Paranoid::ERROR =
pdebug( 'failed to write to file handle: %s',
PDLEVEL1, $! );
}
}
# Seek back to original position
sysseek $fh, $pos, SEEK_SET;
# Unlock the file handle
pflock( $filename, LOCK_UN ) unless $nolock;
}
}
}
subPostamble( PDLEVEL2, '$', $rv );
return $rv;
}
sub pnlappend {
# Purpose: Wrapper for pappend w/o internal flocking
# Returns: RV of pappend
# Usage: $bytes = pnlappend($filename, $text, $length);
# Usage: $bytes = pnlappend($filename, $text, $length, $offset);
my $filename = shift;
my $out = shift;
my $wlen = shift;
my $offset = shift;
return pappend( $filename, $out, $wlen, $offset, 1 );
}
sub pread ($\$;@) {
# Purpose: Performs a sysread w/locking
# Returns: Integer/undef
# Usage: $bytes = pread($filename, $text, $length);
# Usage: $bytes = pread($filename, $text, $length, $offset);
my $filename = shift;
my $sref = shift;
my $rlen = shift;
my $offset = shift;
my $nolock = shift;
my ( $fh, $rv );
subPreamble( PDLEVEL2, '$\$;$$$', $filename, $sref, $rlen, $offset,
$nolock );
if ( defined $filename ) {
# Opportunistically open a file handle if needed,
# otherwise, just retrieve the existing file handle
$fh = popen( $filename, O_RDONLY );
# Smoke 'em if you got'em...
if ( defined $fh ) {
if ( $nolock or pflock( $filename, LOCK_SH ) ) {
$rlen = PIOBLKSIZE unless defined $rlen;
$offset = 0 unless defined $offset;
$rv = sysread $fh, $$sref, $rlen, $offset;
if ( defined $rv ) {
pdebug( 'read %d bytes', PDLEVEL2, $rv );
} else {
Paranoid::ERROR =
pdebug( 'failed to read from file handle: %s',
PDLEVEL1, $! );
}
pflock( $filename, LOCK_UN ) unless $nolock;
}
}
}
subPostamble( PDLEVEL2, '$', $rv );
return $rv;
}
sub pnlread ($\$;@) {
# Purpose: Wrapper for pread w/o internal flocking
# Returns: RV of pread
# Usage: $bytes = pnlread($filename, $text, $length);
# Usage: $bytes = pnlread($filename, $text, $length, $offset);
my $filename = shift;
my $sref = shift;
my $rlen = shift;
my $offset = shift;
return pread( $filename, $$sref, $rlen, $offset, 1 );
}
sub ptruncate {
# Purpose: Truncates the specified file
# Returns: RV of truncate
# Usage: $rv = ptruncate($filename);
# Usage: $rv = ptruncate($filename, $pos);
# Usage: $rv = ptruncate($filename, $pos, 1);
my $filename = shift;
my $pos = shift;
my $nolock = shift;
my ( $rv, $fh, $cpos );
subPreamble( PDLEVEL2, '$;$$', $filename, $pos, $nolock );
if ( defined $filename ) {
$pos = 0 unless defined $pos;
$fh = popen( $filename, O_RDWR | O_CREAT );
# Smoke 'em if you got'em...
if ( defined $fh ) {
if ( $nolock or pflock( $filename, LOCK_EX ) ) {
$cpos = sysseek $fh, 0, SEEK_CUR;
$rv = truncate $fh, $pos;
if ($rv) {
sysseek $fh, $pos, SEEK_SET if $cpos > $pos;
} else {
Paranoid::ERROR =
pdebug( 'failed to truncate file: %s', PDLEVEL1, $! );
}
pflock( $filename, LOCK_UN ) unless $nolock;
}
}
}
subPostamble( PDLEVEL2, '$', $rv );
return $rv;
}
END {
# Attempt to clean close all filehandles
pcloseAll();
}
1;
__END__
=head1 NAME
Paranoid::IO - Paranoid IO support
=head1 VERSION
$Id: lib/Paranoid/IO.pm, 2.10 2022/03/08 00:01:04 acorliss Exp $
=head1 SYNOPSIS
use Fcntl qw(:DEFAULT :flock :mode :seek);
use Paranoid::IO qw(:all);
# Implicit open
$chars = pread("./foo.log", $in, 2048);
# Implcit write/append
$chars = pwrite("./bar.log", $out);
$chars = pappend("./bar.log", $out);
# Adjust block read size
PIOBLKSIZE = 8192;
# Adjust max file size for file scans
PIOMAXFSIZE = 65536;
# Enable flock lock stack
PIOLOCKSTACK = 1;
# Explicit open with explicit locking
$fh = popen($filename, O_RDWR | O_CREAT | O_TRUNC, 0600);
$rv = pseek($filename, 0, SEEK_END);
$rv = pflock($filename, LOCK_EX);
if ($rv > 0) {
pseek($filename, 0, SEEK_SET) && ptruncate($filename);
}
$rv = pwrite($filename, $text)
$rv = ptell($filename);
$rv = plockstat($filename);
# Calls that ignore file locks
$rv = pnlwrite($filename, $text)
$rv = pnlappend($filename, $text)
$rv = pnlread($filename, $text)
# After fork
$rv = preopen();
$rv = pclose($filename);
$rv = pcloseAll();
=head1 DESCRIPTION
B<Paranoid::IO> is intended to make basic file I/O access easier while
keeping with the tenets of paranoid programming. Most of these calls are
essentially wrappers for the basic system calls (exposed as L<sysopen>,
L<syswrite>, etc.) with some additional logic to reduce the need to explicitly
code every step of normal safe access rules, such as file locking. In the
most basic of usage patterns, even explicitly opening files isn't necessary.
For the most part the system calls that are wrapped here act identically as
the underlying calls, both in the arguments they take and the values they
return. The one notable difference, however, is the I<popen> function itself.
A glob variable isn't passed for assignation since this module stores those
references internally along with some meta data, so I<popen> returns file
handles directly.
That semantic, however, is what gives the rest of the functions the
flexibility of accepting either a file name or a file handle to work on. In
the case of file names some of these functions can open files automatically,
and the rest of the features are granted automatically.
In the case of passing file handles the full feature set of this module is
only available if the file handle was originally opened with I<popen>. The
calls will still work even if it wasn't, but some of the safety features, like
being fork-safe, won't have the meta data to work properly.
The features provided by this module are:
=over
=item * Opportunistic file access
=item * File handle caching
=item * Fork-safe file access
=item * Inherent file locking
=item * O_APPEND access patterns where needed even for files not opened with O_APPEND
=item * Intelligent file tracking
=item * Optional flock lock stack for transactional I/O patterns
=back
The following sections will explain each feature in more depth:
=head2 Opportunistic file access
Opportunistic file access is defined here as not needing the explicit I/O
handling for what can be implied. For instance, to read content from a file
one can simply use the I<pread> function without having to open and apply a
shared file lock. In a similar manner one should be able to write or append
to a file. Files are automatically opened (with the file mode being intuited
by the type of call) as needed. Only where more complicated access patterns
(such as read/write file handles) should an explicit I<popen> call be needed.
Opportunism is limited to where it makes sense, however. Files are not
opportunistically opened if the first I/O call is to I<pseek> or I<ptell>.
The intent of the file I/O (in regards to read/write file modes)
is impossible to tell within those calls.
=head2 File handle caching
This module provides a replacement for Perl's internal L<sysopen>, which
should be used even where read/write file access is necessary. One key
benefit for doing so is that it provides internal file handle caching based on
the file name. All the additional functions provided by this module use it
internally to retrieve that cached file handle to avoid the overhead of
repetitive opening and closing of files.
=head2 Fork-safe file access
A greater benefit of I<popen>, however, is in it's fork-safe behavior. Every
call checks to see if the file handle it has was inherited from its parent,
and if so, transparently closed and reopened so I/O can continue without both
processes conflicting over cursor positions and buffers. After files are
reopened read cursors are placed at the same location they were prior to the
first I/O access in the child.
File modes are preserved without the obvious conflicts of intent as well.
Files opened in the parent with B<O_TRUNC> are reopened without that flag
to prevent content from being clobbered.
=head2 Inherent file locking
Except where explicitly ignored (like for I<pnlread>) all read, write, and
append operations use locking internally, alleviating the need for the
developer to do so explicitly. Locks are applied and removed as quickly as
possible to facilitate concurrent access.
If you're managing flocks directly, however, all of the read/write functions
in this module not only support an option boolean argument to disable internal
flocking, but also have I<pnl*> wrapper functions that set that argument for
you.
=head2 O_APPEND access patterns
I<pappend> allows you to mimic B<O_APPEND> access patterns even for files that
weren't explicitly opened with B<O_APPEND>. If you open a file with B<O_RDWR>
you can still call I<pappend> and the content will be appended to the end of
the file, without moving the file's cursor position for regular reads and
writes.
=head2 Intelligent file tracking
I<popen> caches file handles by file name. If files are opened with relative
paths this has the potential to cause some confusion if the process or
children are changing their working directories. In anticipation of this
I<popen> also tracks the real path (as resolved by the L<realpath> system
call) and file name. This way you can still access the same file regardless
of the process or its children's movements on the file system.
This could be, however, a double-edged sword if your program intends to open
identically named files in multiple locations. If that is your intent you
would be cautioned to avoid using relative paths with I<popen>.
=head2 Optional flock lock stack for transactional I/O patterns
Complex I/O patterns on file I/O can sometimes extensive nested function calls
that each manipulate flocks independently. Those nested calls can come into
conflict when one call degrades a needed lock applied by a previous call.
For instance, a pattern where a new block needs to be allocated to an opened
file, but an index of blocks must be maintained within the same file. One
might have a function which retrieves the list of block addresses from the
index, and that function rationally applies a shared flock before reading, and
removes it afterwards. One might try to get an exclusive lock on the file,
then retrieve the index using the existing function. That function, however,
would end up replacing your exclusive lock with the shared lock, potentially
making it impossible to reacquire that exclusive lock depending on other
processes and their I/O.
The lock stack attempts to solve those kinds of problems by maintaining a
stack of flocks, and making sure that no new locks degrade the previous locks.
In previous example, it would notice that the stack was opened with an
exclusive lock, and when the index retrieval function attempts to apply the
shared lock, it would simply upgrade that lock to preserve the exclusive lock.
Since a stack tracks each call to L<pflock()>, once that function attempts to
release the shared lock, the lock stack would simply pop off it's upgraded
call from the stack, and make sure the preceding lock stays in place.
Another way to describe this in psuedo code:
# Enable the lock stack
PIOLOCKSTACK = 1;
sub readIdx {
pflock($file, LOCK_SH);
# ... read data
pflock($file, LOCK_UN);
# ... return data
}
sub writeIdx {
pflock($file, LOCK_EX);
# ... write data
pflock($file, LOCK_UN);
}
sub writeData {
pflock($file, LOCK_EX);
# ... write data
pflock($file, LOCK_UN);
}
sub writeTx {
pflock($file, LOCK_EX);
readIdx();
writeData();
writeIdx();
pflock($file, LOCK_UN);
}
# Execute the transaction
writeTx();
Without the lock stack, executing the transaction function would cause the
following to happen:
writeTx:
# apply LOCK_EX
# readIdx:
# apply LOCK_SH
# read data
# release all locks w/LOCK_UN
# writeData:
# apply LOCK_EX
# ERROR: any write decisions at this point based on the previous
# ERROR: index read may cause file corruption because the index
# ERROR: may have changed while this process was waiting to
# ERROR: reacquire the exclusive lock!
With the lock stack in place, however, it goes like this:
writeTx:
# apply LOCK_EX
# lock stack: (LOCK_EX)
# readIdx:
# asks for LOCK_SH, but maintains LOCK_EX
# lock stack: (LOCK_EX, LOCK_EX)
# read data
# deletes its lock from the stack, but preserves the previous lock
# lock stack: (LOCK_EX)
# writeData:
# asks for LOCK_EX
# lock stack: (LOCK_EX, LOCK_EX)
# writes data
# deletes its lock from the stack, but preserves the previous lock
# lock stack: (LOCK_EX)
# writeIdx:
# asks for LOCK_EX
# lock stack: (LOCK_EX, LOCK_EX)
# writes data
# deletes its lock from the stack, but preserves the previous lock
# lock stack: (LOCK_EX)
# release lock
# lock stack: ()
At no point was the advisory lock lost, and hence, transactional integrity
was preserved for all compliant processes.
The lock stack is off by default to allow the developer complete control over
locking and I/O patterns, but it's there to make functions easier to write
without having to worry about any locks applied outside of their code scope.
One downside of the lock stack is that affects all I/O performed via the
L<Paranoid::IO> framework, it is not locallized to specific file handles. For
that reason, one must be confident that flocks are applied as atomically as
possible throughout the code space leveraging it.
=head1 IMPORT LISTS
This module exports the following symbols by default:
pclose pcloseAll popen preopen ptell pseek pflock
plockstat pread pnlread pwrite pappend ptruncate
The following specialized import lists also exist:
List Members
---------------------------------------------------------
all @defaults PIOBLKSIZE PIOMAXFSIZE PIOLOCKSTACK
=head1 SUBROUTINES/METHODS
=head2 PIOBLKSIZE
PIOBLKSIZE = 65536;
This lvalue function is not exported by default. It is used to determine the
default block size to read when a size is not explicitly passed. Depending on
hardware and file system parameters there might be performance gains to be
had when doing default-sized reads.
The default is 4096, which is generally a safe size for most applications.
=head2 PIOMAXFSIZE
PIOMAXFSIZE = 131072;
This lvalue function is not exported by default. It is used to determine the
maximum file size that will be read. This is not used in this module, but
provided for use in dependent modules that may want to impose file size
limits, such as L<Paranoid::IO::Line> and others.
The default is 65536.
=head2 PIOLOCKSTACK
PIOLOCKSTACK = 1
This lvalue function is not exported by default. It is used to enable the
flock lock stack functionality in order to support transactional I/O patterns.
It is disabled by default.
=head2 popen
$fh = popen($filename);
$fh = popen($filename, $mode);
$fh = popen($filename, $mode, $perms);
$fh = popen($fh);
Returns a file handle if the file could be opened. If the mode is omitted the
default is B<O_CREAT | O_RDWR>. File permissions (for newly created files)
default to B<0666 ^ UMASK>.
Failures to open a file will result in an undef return value, with a text
description of the fault stored in B<Paranoid::ERROR>.
If a file handle is passed to I<popen> it will attempt to match it to a
tracked file handle and, if identified, take the appropriate action. If it
doesn't match any tracked file handles it will just return that file handle
back to the caller.
=head2 pclose
$rv = pclose($filename);
$rv = pclose($fh);
Returns the value from L<close>. Attempts to close a file that's already
closed is considered a success, and true value is returned. Handing it a
stale file handle, however, will be handed to the internal B<close>, with all
the expected results.
=head2 preopen
$rv = preopen();
$rv = preopen(@filenames);
$rv = preopen(@filehandles);
This checks each tracked file handle (i.e., file handles that were opened by
I<popen>) and reopens them if necessary. This is typically only useful after
a fork. It is also not striclty necessary since every call to a function in
this module does that with every invocation, but if you have several file
handles that you may not access immediately you run the risk of the parent
moving the current file position before the child gets back to those files.
You may or may not care. If you do, use this function immediately after a
fork.
Called with a list of file names means that only those files are examined and
reopened. Any failure to reopen any single file handle will result in a false
return value. That said, any failures will not interrupt the function from
trying every file in the list.
=head2 pcloseAll
$rv = pcloseAll();
$rv = pcloseAll(@filenames);
$rv = pcloseAll(@filehandles);
This function returns a boolean value denoting any errors while trying to
close every tracked file handle. This function is also not strictly necessary
for all the normal Perl I/O reasons, but it's here for those that want to be
explicit.
=head2 ptell
$pos = ptell($filename);
$pos = ptell($fh);
Returns the current position of the file cursor. Returns the results of
L<sysseek>, which means that any successful seek is true, even if the cursor
is at the beginning of the file. In that instance it returns "0 but true"
which is boolean true while converting to an integer appropriately.
Any failures are returned as false or undef.
=head2 pseek
$rv = pseek($filename, $pos, $whence);
$rv = pseek($fh, $pos, $whence);
This returns the return value from L<sysseek>. The appropriate whence values
sould be one of the B<SEEK_*> constants as exported by L<Fcntl>.
=head2 pflock
$rv = pflock($filename, $locktype);
$rv = pflock($fh, $locktype);
This returns the return value from L<flock>. The appropriate lock type values
should be one of the B<LOCK_*> constants as exported by L<Fcntl>.
B<NOTE:> This function essentially acts like a pass-through to the native
L<flock> function for any file handle not opened via this module's functions.
=head2 plockstat
$lock = plockstat($filename);
This returns the last flock applied via L<pflock>.
=head2 pread
$bytes = pread($filename, $text, $length);
$bytes = pread($filename, $text, $length, $offset);
$bytes = pread($fh, $text, $length);
$bytes = pread($fh, $text, $length, $offset);
This returns the number of bytes read, or undef on errors. If this is called
prior to an explicit I<popen> it will default to a mode of B<O_RDONLY>. Length
defaults to B<PIOBLKSIZE>.
=head2 pnlread
$bytes = pnlread($filename, $text, $length);
$bytes = pnlread($filename, $text, $length, $offset);
$bytes = pnlread($fh, $text, $length);
$bytes = pnlread($fh, $text, $length, $offset);
This is a wrapper function for B<pread> that calls it with inherent file
locking disabled. It is assumed that the dev is either managing flocks
directly, or they're not needed for this application.
=head2 pwrite
$bytes = pwrite($filename, $text);
$bytes = pwrite($filename, $text, $length);
$bytes = pwrite($filename, $text, $length, $offset);
$bytes = pwrite($filename, $text, $length, $nolock);
$bytes = pwrite($fh, $text);
$bytes = pwrite($fj, $text, $length);
$bytes = pwrite($fh, $text, $length, $offset);
$bytes = pwrite($fh, $text, $length, $offset, $nolock);
This returns the number of bytes written, or undef for any critical failures.
If this is called prior to an explicit I<popen> it uses a default mode of
B<O_WRONLY | O_CREAT | O_TRUNC>.
The optional boolean fifth argument (I<nolock>) will bypass automatic flocks
since it assumes you're managing the lock directly.
=head2 pnlwrite
$bytes = pnlwrite($filename, $text);
$bytes = pnlwrite($filename, $text, $length);
$bytes = pnlwrite($filename, $text, $length, $offset);
$bytes = pnlwrite($fh, $text);
$bytes = pnlwrite($fj, $text, $length);
$bytes = pnlwrite($fh, $text, $length, $offset);
This is a wrapper function for B<pwrite> that calls it with inherent file
locking disabled. It is assumed that the dev is either managing flocks
directly, or they're not needed for this application.
=head2 pappend
$bytes = pappend($filename, $text);
$bytes = pappend($filename, $text, $length);
$bytes = pappend($filename, $text, $length, $offset);
$bytes = pappend($filename, $text, $length, $offset, $nolock);
$bytes = pappend($fh, $text);
$bytes = pappend($fh, $text, $length);
$bytes = pappend($fh, $text, $length, $offset, $nolock);
This behaves identically to I<pwrite> with the sole exception that this
preserves the file position after explicitly seeking and writing to the end of
the file. The default mode here, however, would be B<O_WRONLY | O_CREAT |
O_APPEND> for those files not explicitly opened.
The optional boolean fifth argument (I<nolock>) will bypass automatic flocks
since it assumes you're managing the lock directly.
=head2 pnlappend
$bytes = pnlappend($filename, $text);
$bytes = pnlappend($filename, $text, $length);
$bytes = pnlappend($filename, $text, $length, $offset);
$bytes = pnlappend($fh, $text);
$bytes = pnlappend($fj, $text, $length);
$bytes = pnlappend($fh, $text, $length, $offset);
This is a wrapper function for B<pappend> that calls it with inherent file
locking disabled. It is assumed that the dev is either managing flocks
directly, or they're not needed for this application.
=head2 ptruncate
$rv = ptruncate($filename);
$rv = ptruncate($filename, $pos, $nolock);
$rv = ptruncate($fh);
$rv = ptruncate($fh, $pos, $nolock);
This returns the result of the internal L<truncate> call. If called without
an explicit I<popen> it will open the named file with the default mode of
B<O_RDWR | O_CREAT>. Omitting the position to truncate from will result in
the file being truncated at the beginning of the file.
The optional boolean third argument (I<nolock>) will bypass automatic flocks
since it assumes you're managing the lock directly.
=head2 pnltruncate
$rv = pnltruncate($filename);
$rv = pnltruncate($fh);
This is a wrapper function for B<pnltruncate> that calls it with inherent file
locking disabled. It is assumed that the dev is either managing flocks
directly, or they're not needed for this application.
=head1 DEPENDENCIES
=over
=item o
L<Cwd>
=item o
L<Fcntl>
=item o
L<IO::Handle>
=item o
L<Paranoid>
=item o
L<Paranoid::Debug>
=item o
L<Paranoid::Input>
=back
=head1 BUGS AND LIMITATIONS
It may not always be benficial to cache file handles. You must explicitly
I<pclose> file handles to avoid that. That said, with straight Perl you'd
have to either explicitly close the file handles or use lexical scoping,
anyway. From that perspective I don't find it onerous to do so, especially
with all of the other code-saving features this module provides.
=head1 AUTHOR
Arthur Corliss (corliss@digitalmages.com)
=head1 LICENSE AND COPYRIGHT
This software is free software. Similar to Perl, you can redistribute it
and/or modify it under the terms of either:
a) the GNU General Public License
<https://www.gnu.org/licenses/gpl-1.0.html> as published by the
Free Software Foundation <http://www.fsf.org/>; either version 1
<https://www.gnu.org/licenses/gpl-1.0.html>, or any later version
<https://www.gnu.org/licenses/license-list.html#GNUGPL>, or
b) the Artistic License 2.0
<https://opensource.org/licenses/Artistic-2.0>,
subject to the following additional term: No trademark rights to
"Paranoid" have been or are conveyed under any of the above licenses.
However, "Paranoid" may be used fairly to describe this unmodified
software, in good faith, but not as a trademark.
(c) 2005 - 2021, Arthur Corliss (corliss@digitalmages.com)
(tm) 2008 - 2021, Paranoid Inc. (www.paranoid.com)
|