🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: news.software.readers
1 messages
1 total messages Started by Thomas Koenig Fri, 29 Jul 1994 07:48
backing up .newsrc with tin
#10452
Author: 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.
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads