File:  [LON-CAPA] / loncom / debugging_tools / modify_config_files.pl
Revision 1.20: download - view: text, annotated - select for diffs
Thu Mar 25 13:23:07 2021 UTC (3 years, 2 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- By default, Ubuntu 16LTS and later include "ONLY_FULL_GROUP_BY" in the
  global sql_mode. Remove that so DoDisc can be calculated in Statistics.

    1: #!/usr/bin/perl -w
    2: #
    3: # The LearningOnline Network
    4: #
    5: # $Id: modify_config_files.pl,v 1.20 2021/03/25 13:23:07 raeburn Exp $
    6: #
    7: # Copyright Michigan State University Board of Trustees
    8: #
    9: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
   10: #
   11: # LON-CAPA is free software; you can redistribute it and/or modify
   12: # it under the terms of the GNU General Public License as published by
   13: # the Free Software Foundation; either version 2 of the License, or
   14: # (at your option) any later version.
   15: #
   16: # LON-CAPA is distributed in the hope that it will be useful,
   17: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   18: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   19: # GNU General Public License for more details.
   20: #
   21: # You should have received a copy of the GNU General Public License
   22: # along with LON-CAPA; if not, write to the Free Software
   23: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   24: #
   25: # /home/httpd/html/adm/gpl.txt
   26: #
   27: # http://www.lon-capa.org/
   28: #
   29: ###
   30: 
   31: =pod
   32: 
   33: =head1 NAME
   34: 
   35: B<modify_config_files.pl>
   36: 
   37: =head1 SYNOPSIS
   38: 
   39: This script modifies /etc/my.cnf and one of: /etc/yum.conf 
   40: (for CentOS/Scientific Linux/RHEL >=5 and <8), /etc/apt/sources.list
   41: (for Debian/Ubuntu), /etc/sysconfig/rhn/sources (for RHEL4),
   42: and /etc/yum.repos.d/loncapa.repo (Fedora >= 21; Oracle Linux; 
   43: CentOS/RHEL >= 8).
   44: 
   45: =head1 DESCRIPTION
   46: 
   47: This script modifies /etc/my.cnf, /etc/yum.conf, /etc/yum.repos.d/loncapa.repo,
   48: /etc/apt/sources, or /etc/sysconfig/rhn/sources to ensure certain
   49: parameters are set properly.
   50: 
   51: The LON-CAPA yum repositories are added to /etc/yum.conf, 
   52: /etc/yum.repos.d/loncapa.repo, /etc/sysconfig/rhn/sources
   53: and the LON-CAPA apt repositories are added to 
   54: /etc/apt/sources.list.
   55: 
   56: The /etc/my.cnf file is modified to set the wait_timeout to 1 year.  Backup
   57: copies of each file are made in /etc, /etc/apt, and /etc/sysconfig/rhn, as 
   58: appropriate.
   59: 
   60: =cut
   61: 
   62: use strict;
   63: use File::Copy;
   64: use lib '/home/httpd/lib/perl/';
   65: use LONCAPA::Configuration;
   66: my $loncapa_config=LONCAPA::Configuration::read_conf('loncapa.conf');
   67: 
   68: open(DSH,"$$loncapa_config{'lonDaemons'}/distprobe |");
   69: my $dist = <DSH>;
   70: chomp($dist);
   71: close(DSH);
   72: 
   73: my $yum_status;
   74: my $loninst = 'http://install.loncapa.org';
   75: my $loninst_re = 'http://install\.loncapa\.org';
   76: if ($dist =~ /^fedora(\d+)$/) {
   77:     my $file = '/etc/yum.conf';
   78:     my $ver = $1;
   79:     my $gpgchk = '0';
   80:     my $gpg = "$loninst/versions/fedora/RPM-GPG-KEY-loncapa"; 
   81:     my $nobackup;
   82:     if ($ver > 6) {
   83:         $gpgchk = '1';
   84:     }
   85:     if ($ver >= 21) {
   86:         $file = '/etc/yum.repos.d/loncapa.repo';
   87:         $nobackup = 1;
   88:     }
   89:     $yum_status =  
   90:         &update_file($file,
   91:              [{section => 'loncapa-updates-basearch',
   92:                key     => 'name=',
   93:                value   => 'Fedora Core $releasever LON-CAPA $basearch Updates',
   94:            }, {section => 'loncapa-updates-basearch',
   95:                key     => 'baseurl=',
   96:                value   => $loninst.'/fedora/linux/loncapa/$releasever/$basearch',
   97:            }, {section => 'loncapa-updates-basearch',
   98:                key     => 'gpgcheck=',
   99:                value   =>  $gpgchk,
  100:            }, {section => 'loncapa-updates-basearch',
  101:                key     => 'gpgkey=',
  102:                value   => $gpg,
  103:            }, {section => 'loncapa-updates-noarch',
  104:                key     => 'name=',
  105:                value   => 'Fedora Core $releasever LON-CAPA noarch Updates',
  106:            }, {section => 'loncapa-updates-noarch',
  107:                key     => 'baseurl=',
  108:                value   => $loninst.'/fedora/linux/loncapa/$releasever/noarch',
  109:            }, {section => 'loncapa-updates-noarch',
  110:                key     => 'gpgcheck=',
  111:                value   => $gpgchk,
  112:            }, {section => 'loncapa-updates-noarch',
  113:                key     => 'gpgkey=',
  114:                value   => $gpg,
  115:            }],$nobackup);
  116: } elsif ($dist =~ /^(rhes|centos|scientific|oracle)(\d+)$/) {
  117:     my $type = $1;
  118:     my $ver = $2;
  119:     my $longver = $ver;
  120:     my $nobackup;
  121:     if ($type eq 'rhes') {
  122:         if ($ver == 4) {
  123:             $longver = '4ES';
  124:         } elsif ($ver == 5) {
  125:             $longver = '5Server';
  126:         }
  127:     }
  128:     my %info = (
  129:                  rhes => {
  130:                            title => 'RHEL',
  131:                            path => 'redhat/linux/enterprise/loncapa',
  132:                            gpg => 'versions/redhat/RPM-GPG-KEY-loncapa',
  133:                            gpgchk => 1,
  134:                          },
  135:                  centos => {
  136:                              title => 'CentOS',
  137:                              path => 'centos/loncapa',
  138:                              gpg => 'versions/centos/RPM-GPG-KEY-loncapa',
  139:                              gpgchk => 1,
  140:                            },
  141:                  scientific => {
  142:                                  title => 'Scientific Linux',
  143:                                  path => 'scientific/loncapa',
  144:                                  gpg => 'versions/scientific/RPM-GPG-KEY-loncapa',
  145:                                  gpgchk => 1,
  146:                                },
  147:                  oracle => {
  148:                              title => 'Oracle Linux',
  149:                              path => 'oracle/loncapa',
  150:                              gpg => 'versions/oracle/RPM-GPG-KEY-loncapa',
  151:                              gpgchk => 1,
  152:                            },
  153:                );
  154:     if (ref($info{$type}) eq 'HASH') {
  155:         if ($ver > 4) {
  156:             my $file = '/etc/yum.conf';
  157:             if (($ver > 7) || ($type eq 'oracle')) {
  158:                 $file = '/etc/yum.repos.d/loncapa.repo';
  159:                 $nobackup = 1;
  160:             }
  161:             $yum_status =
  162:                 &update_file($file,
  163:                      [{section => 'loncapa-updates-basearch',
  164:                        key     => 'name=',
  165:                        value   => $info{$type}{title}.' $releasever LON-CAPA $basearch Updates',
  166:                       }, {section => "loncapa-updates-basearch",
  167:                           key     => 'baseurl=',
  168:                           value   => "$loninst/$info{$type}{path}/".'$releasever/$basearch',
  169:                       }, {section => 'loncapa-updates-basearch',
  170:                           key     => 'gpgcheck=',
  171:                           value   => $info{$type}{gpgchk},
  172:                       }, {section => 'loncapa-updates-basearch',
  173:                           key     => 'gpgkey=',
  174:                           value   => "$loninst/$info{$type}{gpg}",
  175:                       }, {section => 'loncapa-updates-noarch',
  176:                           key     => 'name=',
  177:                           value   => $info{$type}{title}.' $releasever LON-CAPA noarch Updates',
  178:                       }, {section => 'loncapa-updates-noarch',
  179:                           key     => 'baseurl=',
  180:                           value   => "$loninst/$info{$type}{path}/".'$releasever/noarch',
  181:                       }, {section => 'loncapa-updates-noarch',
  182:                           key     => 'gpgcheck=',
  183:                           value   => $info{$type}{gpgchk},
  184:                       }, {section => 'loncapa-updates-noarch',
  185:                           key     => 'gpgkey=',
  186:                           value   => "$loninst/$info{$type}{gpg}",
  187:                       }],$nobackup);
  188:         } elsif (($type eq 'rhes') && ($ver == 4)) {
  189:             my %rhn = (
  190:                         basearch => { 
  191:                             regexp => '\s*yum\s+loncapa\-updates\-basearch\s+'.$loninst_re.'/'.$info{$type}{path}.'/'.$longver.'/\$ARCH',
  192:                             text => "yum loncapa-updates-basearch $loninst/$info{$type}{path}/$longver/".'$ARCH',
  193:                                     },
  194:                         noarch =>  {
  195:                             regexp => '\s*yum\s+loncapa\-updates\-noarch\s+'.$loninst_re.'/'.$info{$type}{path}.'/'.$longver.'/noarch',
  196:                             text => "yum loncapa-updates-noarch $loninst/$info{$type}{path}/$longver/noarch",
  197:                                    },
  198:                       );
  199:             $yum_status = &update_rhn_source(\%rhn); 
  200:         }
  201:     }
  202: } elsif ($dist =~ /^(debian|ubuntu)\d+$/) {
  203:     my %apt_get_source = (
  204:                            debian5 => {
  205:                                         regexp => '\s*deb\s+'.$loninst_re.'/debian/?\s+lenny\s+main',
  206:                                         text   => "deb $loninst/debian lenny main",
  207:                                       },
  208:                            ubuntu6 => {
  209:                                         regexp => '\s*deb\s+'.$loninst_re.'/ubuntu/?\s+dapper\s+main',
  210:                                         text   => "deb $loninst/ubuntu dapper main",
  211:                                       },
  212:                            ubuntu8 => {
  213:                                         regexp => '\s*deb\s+'.$loninst_re.'/ubuntu/?\s+hardy\s+main',
  214:                                         text   => "deb $loninst/ubuntu hardy main",
  215:                                       },
  216:                            ubuntu10 => {
  217:                                         regexp => '\s*deb\s+'.$loninst_re.'/ubuntu/?\s+lucid\s+main',
  218:                                         text   => "deb $loninst/ubuntu lucid main",
  219:                                        },
  220:                            ubuntu12 => {
  221:                                         regexp => '\s*deb\s+'.$loninst_re.'/ubuntu/?\s+precise\s+main',
  222:                                         text   => "deb $loninst/ubuntu precise main",
  223:                                        },
  224:                            ubuntu14 => {
  225:                                         regexp => '\s*deb\s+'.$loninst_re.'/ubuntu/?\s+trusty\s+main',
  226:                                         text   => "deb $loninst/ubuntu trusty main",
  227:                                        },
  228:                            ubuntu16 => {
  229:                                         regexp => '\s*deb\s+'.$loninst_re.'/ubuntu/?\s+xenial\s+main',
  230:                                         text   => "deb $loninst/ubuntu xenial main",
  231:                                        },
  232:                            ubuntu18 => {
  233:                                         regexp => '\s*deb\s+'.$loninst_re.'/ubuntu/?\s+bionic\s+main',
  234:                                         text   => "deb $loninst/ubuntu bionic main",
  235:                                        },
  236:                            ubuntu20 => {
  237:                                         regexp => '\s*deb\s+'.$loninst_re.'/ubuntu/?\s+focal\s+main',
  238:                                         text   => "deb $loninst/ubuntu focal main",
  239:                                        },
  240:                          );
  241:     my $apt_status;
  242:     if (defined($apt_get_source{$dist})) {
  243:         $apt_status = &update_apt_source($apt_get_source{$dist},);
  244:     }
  245: }
  246: 
  247: my $mysqlfile = '/etc/my.cnf';
  248: my $mysqlconf = [{section =>'mysqld',
  249:                   key     =>'wait_timeout=',
  250:                   value   =>'31536000'}];
  251: if ($dist =~ /^ubuntu(\d+)$/) {
  252:     my $version = $1;
  253:     $mysqlfile = '/etc/mysql/my.cnf';
  254:     if ($version > 14) {
  255:         $mysqlfile = '/etc/mysql/mysql.conf.d/mysqld.cnf';
  256:         if ($version < 20) {
  257:             push(@{$mysqlconf},
  258:                  {section =>'mysqld',
  259:                   key     =>'sql_mode=',
  260:                   value   =>'"STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"'});
  261:         } else {
  262:             push(@{$mysqlconf},
  263:                  {section =>'mysqld',
  264:                   key     =>'sql_mode=',
  265:                   value   =>'"STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION"'});
  266:         }
  267:     }
  268: }
  269: 
  270: my $mysql_global_status = &update_file($mysqlfile,$mysqlconf);
  271: 
  272: my $local_my_cnf = '/home/www/.my.cnf';
  273: if (! -e $local_my_cnf) {
  274:     # Create a file so we can do something with it...
  275:     system("touch $local_my_cnf");
  276: }
  277: my $mysql_www_status =
  278:     &update_file($local_my_cnf,
  279:              [{section =>'client',
  280:                key     =>'user=',
  281:                value   =>'www',},
  282:               {section =>'client',
  283:                key     =>'password=',
  284:                value   =>$loncapa_config->{'lonSqlAccess'}},]);
  285: 
  286: my $exitvalue = 0;
  287: 
  288: if ($mysql_global_status) { $exitvalue = 1; }
  289: 
  290: exit $exitvalue;
  291: 
  292: 
  293: sub update_file {
  294:     my ($file,$newdata,$nobackup) = @_;
  295:     return 1 if (! -e $file);
  296:     unless ($nobackup) {
  297:         my $backup = $file.'.backup';
  298:         if (! copy($file,$backup)) {
  299:             warn "**** Error: Unable to make backup of $file";
  300:             return 0;
  301:         }
  302:     }
  303:     my ($filedata) = &parse_config_file($file);
  304:     if (! ref($filedata)) { warn "**** Error: $filedata"; return 0;}
  305:     my $modified = 0;
  306:     foreach my $data (@$newdata) {
  307:         my $section = $data->{'section'};
  308:         my $key = $data->{'key'};
  309:         my $value = $data->{'value'};
  310:         my $result = &modify_config_file($filedata,$section,$key,$value);
  311:         if ($result) { $modified = 1; }
  312:     }
  313:     if ($modified) {
  314:         my $result = &write_config_file($file,$filedata);
  315:         if (defined($result)) { warn 'Error:'.$result; return 0; }
  316:     }
  317:     return $modified;
  318: }
  319: 
  320: #################################################################
  321: #################################################################
  322: 
  323: =pod
  324: 
  325: =over 4
  326: 
  327: =item &parse_config_file()
  328: 
  329: Read a configuration file in and parse it into an internal data structure.
  330: 
  331: Input: filename
  332: 
  333: Output: array ref $filedata  OR  scalar error message
  334: 
  335: =back 
  336: 
  337: =cut
  338: 
  339: #################################################################
  340: #################################################################
  341: sub parse_config_file {
  342:     my ($file) = @_;
  343:     open(INFILE,$file) || return ('Unable to open '.$file.' for reading');
  344:     my @Input = <INFILE>;
  345:     close(INFILE);
  346:     my @Structure;
  347:     my %Sections;
  348:     while (my $line = shift(@Input)) {
  349:         chomp($line);
  350:         if ($line =~ /^\[([^\]]*)\]/) {
  351:             my $section_id = $1;
  352:             push(@Structure,'__section__'.$section_id);
  353:             while ($line = shift(@Input)) {
  354:                 chomp($line);
  355:                 if ($line =~ /^\[([^\]]*)\]/) {
  356:                     unshift(@Input,$line);
  357:                     last;
  358:                 } else {
  359:                     push(@{$Sections{$section_id}},$line);
  360:                 }
  361:             }
  362:         } else {
  363:             push(@Structure,$line);
  364:         }
  365:     }
  366:     my $filedata = [\@Structure,\%Sections];
  367:     return $filedata;
  368: }
  369: 
  370: #################################################################
  371: #################################################################
  372: 
  373: =pod
  374: 
  375: =over 4
  376: 
  377: =item
  378: 
  379: Write a configuration file out based on the internal data structure returned
  380: by &parse_config_file
  381: 
  382: Inputs: filename, $filedata (the return value of &parse_config_file
  383: 
  384: Returns: undef on success, scalar error message on failure.
  385: 
  386: =back
  387: 
  388: =cut
  389: 
  390: #################################################################
  391: #################################################################
  392: sub write_config_file {
  393:     my ($file,$filedata) = @_;
  394:     my ($structure,$sections) = @$filedata;
  395:     if (! defined($structure) || ! ref($structure)) {
  396:         return 'Bad subroutine inputs';
  397:     }
  398:     open(OUTPUT,'>',$file) || return('Unable to open '.$file.' for writing');
  399:     for (my $i=0;$i<scalar(@$structure);$i++) {
  400:         my $line = $structure->[$i];
  401:         chomp($line);
  402:         if ($line =~ /^__section__(.*)$/) {
  403:             my $section_id = $1;
  404:             print OUTPUT ('['.$section_id.']'.$/);
  405:             foreach my $section_line (@{$sections->{$section_id}}) {
  406:                 chomp($section_line);
  407:                 print OUTPUT $section_line.$/;
  408:             }
  409:             # Deal with blank lines
  410:             if ($sections->{$section_id}->[-1] =~ /^\s*$/) {
  411:                 # No need to output a blank line at the end if there is one 
  412:                 # already
  413:             } else {
  414:                 print OUTPUT $/;
  415:             }
  416:         } else {
  417:             print OUTPUT $line.$/;
  418:         }
  419:     }
  420:     close OUTPUT;
  421:     return undef;
  422: }
  423: 
  424: #################################################################
  425: #################################################################
  426: 
  427: =pod
  428: 
  429: =over 4
  430: 
  431: =item &modify_config_file()
  432: 
  433: Modifies the internal data structure of a configuration file to include new
  434: sections and/or new configuration directives.
  435: 
  436: Inputs: $filedata (see &parse_config_file
  437: $section, the [section] the new entry is to reside in.  A value of undef will
  438: cause the "outer" section (as in yum.conf) to be updated (or have the new
  439: value prepended).
  440: $newkey: A line which matches this will be replaced with $newkey.$newvalue
  441: $newvalue: The new value to be placed with the new key.
  442: 
  443: Returns: 0 or 1, indicating if the file was modified(1) or not(0).
  444: 
  445: =back 
  446: 
  447: =cut
  448: 
  449: #################################################################
  450: #################################################################
  451: sub modify_config_file {
  452:     my ($filedata,$section,$newkey,$newvalue)=@_;
  453:     my $modified = 0;    # returned value - set to true if the file is modified
  454:     my ($structure,$sections) = @$filedata;
  455:     if (! defined($newvalue)) {
  456:         $newvalue = '';
  457:     }
  458:     my $newline = $newkey.$newvalue;
  459:     #
  460:     # Determine which array ref gets the item
  461:     my $target;
  462:     if (defined($section)) {
  463:         if (! exists($sections->{$section})) {
  464:             push(@$structure,'__section__'.$section);
  465:             $sections->{$section}=[];
  466:         }
  467:         $target = $sections->{$section};
  468:     } else {
  469:         $target = $structure;
  470:     }
  471:     #
  472:     # Put the item in or update it.
  473:     my $key_is_new = 1;
  474:     for (my $i=0;$i<scalar(@$target);$i++) {
  475:         if ($target->[$i] =~/^$newkey/) {
  476:             if ($target->[$i] ne $newline) {
  477:                 $target->[$i]=$newline;
  478:                 $modified = 1;
  479:             }
  480:             $key_is_new = 0;
  481:             last;
  482:         }
  483:     }
  484:     if ($key_is_new) {
  485:         if (! defined($section)) {
  486:             unshift(@$target,$newline);
  487:         } else {
  488:             # No need to put things after a blank line.
  489:             if (defined($target->[-1]) && $target->[-1] =~ /^\s*$/) {
  490:                 $target->[-1] = $newline;
  491:                 $modified = 1;
  492:             } else {
  493:                 push(@$target,$newline);
  494:                 $modified = 1;
  495:             }
  496:         }
  497:     }
  498:     return $modified;
  499: }
  500: 
  501: #################################################################
  502: #################################################################
  503: 
  504: =pod
  505: 
  506: =over 4
  507: 
  508: =item &update_rhn_source()
  509: 
  510: Modifies the Red Hat 4 sources file which includes repositories used by up2date 
  511: 
  512: Inputs: 
  513: $rhn_items - a reference to hash of a hash containing the regular expression
  514: to test for, and the text string to append to the file, if an entry for the 
  515: LON-CAPA RHEL repository is missing for two cases:
  516: 
  517: (a) basearch
  518: (b) noarch 
  519: 
  520: Returns: 0 or 1, indicating if the file was modified(1) or not(0).
  521: 
  522: =back
  523: 
  524: =cut
  525: 
  526: #################################################################
  527: #################################################################
  528: sub update_rhn_source {
  529:     my ($rhn_items) = @_;
  530:     return 0 if (ref($rhn_items) ne 'HASH');
  531:     return 0 if ((ref($rhn_items->{basearch}) ne 'HASH') || (ref($rhn_items->{noarch}) ne 'HASH'));
  532:     my $file = '/etc/sysconfig/rhn/sources';
  533:     return 0 if (! -e $file);
  534:     my $backup = $file.'.backup';
  535:     if (! copy($file,$backup)) {
  536:         warn "**** Error: Unable to make backup of $file";
  537:         return 0;
  538:     }
  539:     my $result = 0;
  540:     my $fh;
  541:     if (open($fh,'<',$file)) {
  542:         my $total = 0;
  543:         my %found;
  544:         foreach my $item (keys(%{$rhn_items})) {
  545:             $found{$item} = 0;
  546:         }
  547:         while(<$fh>) {
  548:             foreach my $item (keys(%{$rhn_items})) {
  549:                 if (ref($rhn_items->{$item}) eq 'HASH') {
  550:                     my $pattern = $rhn_items->{$item}->{regexp};
  551:                     if ($pattern ne '') { 
  552:                         if (m{^$pattern}) {
  553:                             $found{$item} = 1;
  554:                             $total ++;
  555:                         }
  556:                     }
  557:                 }
  558:             }
  559:             last if $total == 2;
  560:         }
  561:         close($fh);
  562:         if ($total < 2) {
  563:             if (open($fh,'>>',$file)) {
  564:                 foreach my $item (keys(%{$rhn_items})) {
  565:                     unless ($found{$item}) {
  566:                         if (ref($rhn_items->{$item}) eq 'HASH') {
  567:                             if ($rhn_items->{$item}->{text} ne '') {
  568:                                 print $fh "\n".$rhn_items->{$item}->{text}."\n";
  569:                                 $result = 1;
  570:                             }
  571:                         }
  572:                     }
  573:                 }
  574:                 close($fh);
  575:             }
  576:         }
  577:     }
  578:     return $result;
  579: }
  580: 
  581: #################################################################
  582: #################################################################
  583: 
  584: =pod
  585: 
  586: =over 4
  587: 
  588: =item &update_apt_source()
  589: 
  590: Modifies the source.list file which includes repositories used by apt-get
  591: 
  592: Inputs:
  593: $deb_row - a reference to containing the regular expression
  594: to test for, and the text string to append to the file, if an entry for the
  595: LON-CAPA Debian/ or Ubuntu repository is missing.
  596: 
  597: Returns: 0 or 1, indicating if the file was modified(1) or not(0).
  598: 
  599: =back
  600: 
  601: =cut
  602: 
  603: #################################################################
  604: #################################################################
  605: sub update_apt_source {
  606:     my ($deb_row) = @_;
  607:     return 0 if (ref($deb_row) ne 'HASH');
  608:     return 0 if (($deb_row->{regexp} eq '') || ($deb_row->{text} eq ''));
  609:     my $file = '/etc/apt/sources.list';
  610:     return 0 if (! -e $file);
  611:     my $backup = $file.'.backup';
  612:     if (! copy($file,$backup)) {
  613:         warn "**** Error: Unable to make backup of $file";
  614:         return 0;
  615:     }
  616:     my $result = 0;
  617:     my $fh;
  618:     if (open($fh,'<',$file)) {
  619:         my $found = 0;
  620:         my $pattern = $deb_row->{regexp};
  621:         while(<$fh>) {
  622:             if (m{^$pattern}) {
  623:                 $found = 1;
  624:                 last;
  625:             }
  626:         }
  627:         close($fh);
  628:         if (!$found) {
  629:             if (open($fh,'>>',$file)) {
  630:                 print $fh "\n".$deb_row->{text}."\n";
  631:                 close($fh);
  632:                 $result = 1;
  633:             }
  634:         }
  635:     }
  636:     return $result;
  637: }
  638: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>