File: archivegz.in

package info (click to toggle)
inn2 2.5.4-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 11,720 kB
  • ctags: 8,983
  • sloc: ansic: 92,499; sh: 13,509; perl: 12,921; makefile: 2,985; yacc: 842; python: 342; lex: 255
file content (334 lines) | stat: -rw-r--r-- 8,870 bytes parent folder | download | duplicates (8)
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
#!/usr/bin/perl
# Copyright 1999 Stephen M. Benoit, Service Providers of America.
#  See notice at end of this file.
#
# Filename: archivegz.pl
# Author: Stephen M. Benoit (benoits@servicepro.com)
# Created: Wed Apr 14 13:56:01 1999
# Version: $Id: archivegz.in 4329 2001-01-14 13:47:52Z rra $
#
$RCSID='$Id: archivegz.in 4329 2001-01-14 13:47:52Z rra $ ';

# Specify command line options, and decode the command line.

require 'newgetopt.pl';
require 'newusage.pl';
@opts =
  (
   "help|usage;;print this message",
   "version;;print version",
   "a=s;;directory to archive in instead of the default",
   "f;;directory names will be flattened out",
   "i=s;;append one line to the index file for each article (Destination name, Message ID, Subject)",
   "m;; Files are copied by making a link.  Not applicable, ignored",
   "r;;Suppress stderr redirection to /var/log/news/errlog",
   "n=s;;the news spool (source) directory (default=/var/spool/news/)",
   "t=i;;timeout that separates batches (default 10 seconds)",
   ";;input",
   # Examples. 
   # 
   # "OPT;;Option without an argument",
   # "OPT!;;Negatable option without an argument",
   # "VAR=T;;Option with mandatory argumet T = s(tring),i(nteger), or f(loat).
   # "VAR:T;;Option with optional argument.
   # "OPT|AAA|BBB";;AAA and BBB are aliases for OPT",
   # "VAR=T@";;Push option argument onto array @opt_VAR"
  );
$ignorecase = 0;
$badopt = !&NGetOpt(&NMkOpts(@opts));
# $badarg = (@ARGV != 0);
if ($badarg || $badopt || $opt_help)
  {
    &NUsage($0,0,'',@opts);
    exit ($badopt||$badarg);
  } 
if ($opt_version) {print STDERR "$RCSID\n"; exit 0}

# --------------------------------------------------------------------

# --- constants and defaults ---
$NEWS_ROOT = "/var/spool/news/";
$NEWS_ERR = "/var/log/news/errlog";
$NEWS_ARCHIVE = $NEWS_ROOT . "news.archive/";
$timeout = 10;
if ($opt_t)
  { $timeout = $opt_t;}
if ($timeout<1) {$timeout=1;}

# --------------------------------------------------------------------

sub regexp_escape
  {
    local($data)=@_;

    $data =~ s+\\+\\\\+gi; # replace \ with \\
    $data =~ s+\/+\\\/+gi; # replace / with \/

    $data =~ s/([\+\*\?\[\]\(\)\{\}\.\|])/\\$1/gi; # replace +*?[](){}.|

    return $data;
  }

sub fhbits {
  local(@fhlist) = split(' ',$_[0]);
  local($bits);
  for (@fhlist) {
    vec($bits,fileno($_),1) = 1;
  }
  $bits;
}

sub timed_getline
  {
    my ($fileh,$timeout)=@_;
    my $filehandle = (ref($fileh)
		      ? (ref($fileh) eq 'GLOB'
			 || UNIVERSAL::isa($fileh, 'GLOB')
			 || UNIVERSAL::isa($fileh, 'IO::Handle'))
		      : (ref(\$fileh) eq 'GLOB'));
    local(*FILEH) = *$fileh{FILEHANDLE};

    local($rin,$win,$ein);
    local($rout,$wout,$eout);
    $rin = $win = $ein = '';
    $rin = fhbits('FILEH');
    $ein = $rin | $win;
    local($nfound);
    local($offset)=0;
    local($accum)='';
    local($done)=0;
    local($result);

    $nfound = select($rout=$rin, $wout=$win, $eout=$ein, $timeout);

    if ($nfound>0)
      {
	
	# use sysread() to get characters up to end-of-line (incl.)
	while (!$done)
	  {
	    $result = sysread(FILEH, $accum, 1, $offset);
	    if ($result<=0)
	      {
		$done=1;
		return undef;
	      }

	    if (substr($accum,$offset,1) eq "\n")
	      {
		$done=1;
	      }
	    else
	      {
		$offset+=$result;
	      }
	  }
      }
    return $accum;
  }

# --------------------------------------------------------------------

# --- source spool directory ---
if ($opt_n)
  {
    if ($opt_n !~ /^\//) # absolute path?
      { $opt_n = $NEWS_ROOT . $opt_n; }
    if ($opt_n !~ /\/$/) # must end with /
      { $opt_n .= '/'; }
    $NEWS_ROOT = $opt_n;
  }

# --- archive directory ---
if ($opt_a)
  {
    if ($opt_a !~ /^\//) # absolute path?
      { $opt_a = $NEWS_ROOT . $opt_a; }
    if ($opt_a !~ /\/$/) # must end with /
      { $opt_a .= '/'; }
    $NEWS_ARCHIVE = $opt_a;
  }

# --- redirect stderr ---
if (!$opt_r)
  {
    open(SAVEERR, ">&STDERR");
    open(STDERR, ">>$NEWS_ERR") || die "Can't redirect stderr";
  }

# --- get input file opened ---
if ($infilename=shift(@ARGV))
  {
    if ($infilename !~ /^\//) # absolute filename? 
      {
	$infilename = $NEWS_ROOT . $infilename;
      }

  }
else
  {
    $infilename="-";
  }
open(INFILE,"<$infilename");

$done=0;
while (!$done)
  {
    %sourcefile=();
    %destfile=();
    %destname=();

    
    # --- loop over each line in infile ---
    # comments start with '#', ignore blank lines, each line is a filename
    while ($srcfile = &timed_getline(INFILE,$timeout))
    {
      if ($srcfile =~ /\#/) {$srcfile = $`;}
      if ($srcfile =~ /^\s*/) {$srcfile = $';}
      if ($srcfile =~ /\s*$/) {$srcfile = $`;}
      if ($srcfile)  # if a filename survived all that...
	{
	  if ($srcfile !~ /^\//) # absolute filename?
	    {
	      $srcfile = $NEWS_ROOT . $srcfile;
	    }
	  # $srcfile is now a valid, absolute filename
	  # split filename into news directory, newsgroup and article number
	  $artnum=-1;
	  $remaining=$srcfile;
	  if ($remaining =~ /\/(\d*)$/) # remove / and article number
	    { $artnum = $1; $remaining=$`;}
	  $regex = &regexp_escape($NEWS_ROOT);
	  if ($remaining =~ /^$regex/) # split off news dir
	    { $newsdir = $&; $grpdir = $';}
	  else
	    { $newsdir = ''; $grpdir = $remaining; } # ... otherwise, grp = dir
	  $newsgrp = $grpdir;
	  $newsgrp =~ s/\//\./g; # replace slash (/) with dot (.)
	  if ($opt_f)
	    {
	      $grpdir = "$newsgrp.gz";
	    }
	  else
	    { $grpdir .= "/archive.gz"; }
	  $destfile = $NEWS_ARCHIVE . $grpdir;

	  # print STDERR "$srcfile --> $newsgrp --> $destfile\n";
	  if ($sourcefile{$newsgrp}) {$sourcefile{$newsgrp} .= " ";}
	  $sourcefile{$newsgrp} .= $srcfile;
	  $destfile{$newsgrp} = $destfile;
	  $destname{$newsgrp} = $grpdir;
	}
    }

    # --- is there anything to do at this time? ---
    if (%destfile)
      {

	# --- open INDEX ---
	if ($opt_i)
	  {
	    # make sure directory exists
	    if ($opt_i =~ /\/[^\/]*$/)
	      {
		$dirbase=$`;
		system("mkdir -p $dirbase");
	      }
	    open(INDEX,">>$opt_i");
	  }

	# --- make sure that archive file can be written (make parent dirs) ---
	if ($destfile{$group} =~ /\/[^\/]*$/)
	  {
	    $dirbase=$`;
	    system("mkdir -p $dirbase");
	  }

	# --- process each article ---
	foreach $group (keys(%destfile))
	  {
	    # --- gzip the concatenated document, appending archive file ---
	    open(GZIP, "|gzip -c >> $destfile{$group}") || die "Can't open gzip";
	    
	    # --- concatenate the articles, keeping header info if needed ---
	    @accum_headers=();
	    foreach $srcfile (split(/\s+/, $sourcefile{$group}))
	      {
		# print STDERR "reading $srcfile...\n";
		$this_doc='';
		open(DOC, "<$srcfile");
		while ($line=<DOC>)
		  {
		    $this_doc .= $line;
		  }
		close(DOC);
		print GZIP $this_doc;
		if ($opt_i)
		  {
		    # --- get header information and store it in index
		    $subject=''; $mesageid=''; $destname='';
		    if ($this_doc =~ /Subject:\s*(.*)/)
		      { $subject = $1; }
		    if ($subject =~ /^\s*/) {$subject = $';}
		    if ($subject =~ /\s*$/) {$subject = $`;}
		    if ($this_doc =~ /Message-ID:\s*(.*)/)
		      {$messageid = $1; }
		    if ($messageid =~ /^\s*/) {$messageid = $';}
		    if ($messageid =~ /\s*$/) {$messageid = $`;}
		    
		    print INDEX "$destname{$group} $messageid $subject\n";
		  }
	      }
	    
	    close(GZIP);
	  }

	# --- close index file ---
	if ($opt_i)
	  {
	    close(INDEX);
	  }
      }

    if (!defined($srcfile)) # file was closed
      {
	$done=1;
	last;  # "break"
      }
    
  }

# --- restore stderr ---
if (!$opt_r)
  {
    close(STDERR);
    open(STDERR,">>&SAVEERR");
  }

# --- close input file ---
close(INFILE);


__END__
# Local Variables:
# mode: perl
# End:

# Copyright 1999 Stephen M. Benoit, Service Providers of America (SPA).
#
# Permission to use, copy, modify, and distribute this software and its
# documentation for any purpose without fee is hereby granted without fee,
# provided that the above copyright notice appear in all copies and that both
# that copyright notice and this permission notice appear in supporting
# documentation, and that the name of SPA not be used in advertising or
# publicity pertaining to distribution of the software without specific,
# written prior permission.  SPA makes no representations about the
# suitability of this software for any purpose.  It is provided "as is"
# without express or implied warranty.
# 
# SPA DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL
# SPA BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
# DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
# AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.