1 | #!env perl |
---|
2 | |
---|
3 | # |
---|
4 | # The contents of this file are subject to the Mozilla Public |
---|
5 | # License Version 1.1 (the "License"); you may not use this file |
---|
6 | # except in compliance with the License. You may obtain a copy of |
---|
7 | # the License at http://www.mozilla.org/MPL/ |
---|
8 | # |
---|
9 | # Software distributed under the License is distributed on an "AS |
---|
10 | # IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or |
---|
11 | # implied. See the License for the specific language governing |
---|
12 | # rights and limitations under the License. |
---|
13 | # |
---|
14 | # The Original Code is mozilla.org code. |
---|
15 | # |
---|
16 | # The Initial Developer of the Original Code is Netscape |
---|
17 | # Communications Corporation. Portions created by Netscape are |
---|
18 | # Copyright (C) 2001 Netscape Communications Corporation. All |
---|
19 | # Rights Reserved. |
---|
20 | # |
---|
21 | # Contributor(s): |
---|
22 | # Christopher Seawood <cls@seawood.org> |
---|
23 | |
---|
24 | # |
---|
25 | # A generic script to add entries to a file |
---|
26 | # if the entry does not already exist |
---|
27 | # |
---|
28 | # Usage: $0 [-l] <filename> <entry> [<entry> <entry>] |
---|
29 | # |
---|
30 | # -l do not attempt flock the file. |
---|
31 | |
---|
32 | use Fcntl qw(:DEFAULT :flock); |
---|
33 | use Getopt::Std; |
---|
34 | use mozLock; |
---|
35 | |
---|
36 | sub usage() { |
---|
37 | print "$0 [-l] <filename> <entry>\n"; |
---|
38 | exit(1); |
---|
39 | } |
---|
40 | |
---|
41 | $nofilelocks = 0; |
---|
42 | |
---|
43 | getopts("l"); |
---|
44 | |
---|
45 | $nofilelocks = 1 if defined($::opt_l); |
---|
46 | |
---|
47 | $file = shift; |
---|
48 | |
---|
49 | undef @entrylist; |
---|
50 | while ($entry = shift) { |
---|
51 | push @entrylist, $entry; |
---|
52 | } |
---|
53 | |
---|
54 | $lockfile = $file . ".lck"; |
---|
55 | |
---|
56 | # touch the file if it doesn't exist |
---|
57 | if ( ! -e "$file") { |
---|
58 | $now = time; |
---|
59 | utime $now, $now, $file; |
---|
60 | } |
---|
61 | |
---|
62 | # This needs to be atomic |
---|
63 | mozLock($lockfile) unless $nofilelocks; |
---|
64 | |
---|
65 | # Read entire file into mem |
---|
66 | undef @inbuf; |
---|
67 | if ( -e "$file" ) { |
---|
68 | open(IN, "$file") || die ("$file: $!\n"); |
---|
69 | binmode(IN); |
---|
70 | while ($tmp = <IN>) { |
---|
71 | chomp($tmp); |
---|
72 | push @inbuf, $tmp; |
---|
73 | } |
---|
74 | close(IN); |
---|
75 | } |
---|
76 | |
---|
77 | undef @outbuf; |
---|
78 | # Add each entry to file if it's not already there |
---|
79 | foreach $entry (@entrylist) { |
---|
80 | push @outbuf, $entry if (!grep(/^$entry$/, @inbuf)); |
---|
81 | } |
---|
82 | |
---|
83 | $count = $#outbuf + 1; |
---|
84 | |
---|
85 | # Append new entry to file |
---|
86 | if ($count) { |
---|
87 | open(OUT, ">>$file") || die ("$file: $!\n"); |
---|
88 | binmode(OUT); |
---|
89 | foreach $entry (@outbuf) { |
---|
90 | print OUT "$entry\n"; |
---|
91 | } |
---|
92 | close(OUT); |
---|
93 | } |
---|
94 | |
---|
95 | mozUnlock($lockfile) unless $nofilelocks; |
---|
96 | |
---|
97 | exit(0); |
---|