🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: news.software.readers
Article #10452

backing up .newsrc with tin

#10452
From: Thomas Koenig
Date: Fri, 29 Jul 1994 07:48
91 lines
3507 bytes
Here's a Perl wrapper script I wrote for use with tin because I had
some problems with corrupted .newsrc files with tin (especially
if output errors occured).  I works for me, but no guarantees at
all.  Maybe this can be of use for somebody.

#! /usr/bin/perl

require 'syscall.ph';
require 'fcntl.ph';
require 'errno.ph';

$newsreader = '/usr/local/bin/tin';

$lockfile  = $ENV{'HOME'} . '/.tin/lockfile';
$newsrc    = $ENV{'HOME'} . '/.newsrc';
$newsrcbak = $ENV{'HOME'} . '/.newsrc.bak';

$fd = syscall (&SYS_open, $lockfile, &O_RDWR | &O_CREAT | &O_EXCL, 0600);
if ($fd < 0) {
    if ($! == &EEXIST +0) {
	open (LOCKFILE, $lockfile) || die "Failed to lock $lockfile\n";
	while (<LOCKFILE>) {
	    print STDERR
	}
	exit 1;
    }
    else {
	die "Failed to lock $lockfile: $!\n";
    }
}    

$SIG{'HUP'} = $SIG{'INT'} = $SIG{'QUIT'} = $SIG{'TERM'} = 'cleanup';

$ret = syscall (&SYS_close, $fd);

open (LOCKFILE, ">" . $lockfile) || die "Cannot open $lockfile: $!\n";
printf LOCKFILE "$newsreader running as PID %s on host %s",$$, `hostname`;
print LOCKFILE "If this process is no longer running, check $newsrc and
$newsrcbak for consistency; there may be a problem.  If not, you
can remove $lockfile.\n";
close LOCKFILE;

if (-w $newsrc) {
    if (!-s $newsrc) {
	die "Zero - length $newsrc - aborting\n";
    }
    else {
	&backup_newsrc;
	die "unequal size of $newsrc and $newsrcbak - aborting.\n"
	    if (-s $newsrc) != (-s $newsrcbak);
        system ($newsreader,@ARGV);
	$rcsize = -s $newsrc;
	$backsize = -s $newsrcbak;
	if ($rcsize == 0) {
	    rename ($newsrcbak, $newsrc) ||
		die "Failed to restore $newsrc - check $newsrcbak:$!\n";
	    warn "$newsrc corrupted ... restoring old version\n";
	    &backup_newsrc;
	}
	elsif ($rcsize < $backsize && ($backsize - $rcsize) / $backsize > 0.5) {
	    die "$newsrc has shrunk a lot - please check it and $newsrcbak.\n";
	}
    }
}
else {
    system ($newsreader,@ARGV);
}

&cleanup;

sub cleanup {
    unlink $lockfile || die "failed to unlink $lockfile : $!\n";
}

sub backup_newsrc {
    local ($_);

    open (NEWSRC,    '<' . $newsrc) || die "Cannot open $newsrc: $!\n";
    open (NEWSRCBAK, '>' . $newsrcbak) || die "Cannot open $newsrcbak : $!\n";
    while (<NEWSRC>) {
	print NEWSRCBAK;
    }
    close NEWSRC;
    close NEWSRCBAK;
}
--
Thomas Koenig, Thomas.Koenig@ciw.uni-karlsruhe.de, ig25@dkauni2.bitnet.
The joy of engineering is to find a straight line on a double
logarithmic diagram.
Technical Details
Message-ID: <31ac7o$bef@nz12.rz.uni-karlsruhe.de>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!i2pn2.pugleaf.net!archive.newsdeef.eu!mbox2nntp-comp.lang.perl.mbox.zip!bga.com!news.sprintlink.net!hookup!yeshua.marcam.com!usc!howland.reston.ans.net!EU.net!Germany.EU.net!news.dfn.de!news.belwue.de!news.uni-stuttgart.de!rz.uni-karlsruhe.de!fg70.rz.uni-karlsruhe.de!ig25

Show Full Headers
Complete Article Headers
Message-ID: <31ac7o$bef@nz12.rz.uni-karlsruhe.de>
Subject: backing up .newsrc with tin
Date: Fri, 29 Jul 1994 07:48:08 +0000
From: ig25@fg70.rz.uni-karlsruhe.de (Thomas Koenig)
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!i2pn2.pugleaf.net!archive.newsdeef.eu!mbox2nntp-comp.lang.perl.mbox.zip!bga.com!news.sprintlink.net!hookup!yeshua.marcam.com!usc!howland.reston.ans.net!EU.net!Germany.EU.net!news.dfn.de!news.belwue.de!news.uni-stuttgart.de!rz.uni-karlsruhe.de!fg70.rz.uni-karlsruhe.de!ig25
Newsgroups: news.software.readers,comp.lang.perl
Organization: University of Karlsruhe, Germany
Lines: 90
Reply-To: Thomas.Koenig@ciw.uni-karlsruhe.de
NNTP-Posting-Host: fg70.rz.uni-karlsruhe.de
Mime-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: 8bit
X-Newsreader: TIN [version 1.2 PL0]