home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / share / system-tools-backends-2.0 / scripts / Shares / NFS.pm next >
Encoding:
Perl POD Document  |  2006-08-14  |  4.4 KB  |  213 lines

  1. #-*- Mode: perl; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2. #
  3. # Copyright (C) 2000-2001 Ximian, Inc.
  4. #
  5. # Authors: Hans Petter Jansson <hpj@ximian.com>
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU Library General Public License as published
  9. # by the Free Software Foundation; either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. # GNU Library General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Library General Public License
  18. # along with this program; if not, write to the Free Software
  19. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  20.  
  21. package Shares::NFS;
  22.  
  23. use Utils::Parse;
  24.  
  25. sub get_distro_nfs_file
  26. {
  27.   # This is quite generic
  28.   return "/etc/exports";
  29. }
  30.  
  31. sub get_share_client_info
  32. {
  33.   my ($client) = @_;
  34.   my ($pattern, $options_str, @options, $option);
  35.   my ($rw);
  36.  
  37.   $client =~ /^([a-zA-Z0-9.-_*?@\/]+)/;
  38.   $pattern = $1;
  39.   $pattern = "0.0.0.0/0" if $pattern eq "";
  40.   $rw = 0;
  41.  
  42.   if ($client =~ /\((.+)\)/)
  43.   {
  44.     $option_str = $1;
  45.     @options = ($option_str =~ /([a-zA-Z0-9_=-]+),?/mg);
  46.  
  47.     for $option (@options)
  48.     {
  49.       $rw = ($option eq "rw") ? 1 : 0;
  50.       # Add supported NFS export options here. Some might have to be split on '='.
  51.     }
  52.   }
  53.  
  54.   return [ $pattern, $rw ];
  55. }
  56.  
  57. sub get_share_info
  58. {
  59.   my ($clients) = @_;
  60.   my (@share_info, $client);
  61.  
  62.   foreach $client (@$clients)
  63.   {
  64.     push @share_info, &get_share_client_info ($client);
  65.   }
  66.  
  67.   return \@share_info;
  68. }
  69.  
  70. sub get_export_line
  71. {
  72.   my ($share) = @_;
  73.   my ($str);
  74.  
  75.   $str = sprintf ("%-15s ", $$share[0]);
  76.  
  77.   foreach $i (@{$$share[1]})
  78.   {
  79.     $str .= $$i[0];
  80.     $str .= "(rw)" if (!$$i[1]);
  81.     $str .= " ";
  82.   }
  83.  
  84.   $str .= "\n";
  85.   return $str;
  86. }
  87.  
  88. sub add_entry
  89. {
  90.   my ($share, $file) = @_;
  91.   my ($buff);
  92.  
  93.   $buff = &Utils::File::load_buffer ($file);
  94.   push @$buff, &get_export_line ($share);
  95.  
  96.   &Utils::File::save_buffer ($buff, $file);
  97. }
  98.  
  99. sub delete_entry
  100. {
  101.   my ($share, $file) = @_;
  102.   my ($buff, $i, $line, @arr);
  103.  
  104.   $buff = &Utils::File::load_buffer ($file);
  105.   $i = 0;
  106.  
  107.   while ($$buff[$i])
  108.   {
  109.     if (!&Utils::Util::ignore_line ($$buff[$i]))
  110.     {
  111.       @arr = split /[ \t]+/, $$buff[$i];
  112.       delete $$buff[$i] if ($arr[0] eq $$share[0]);
  113.     }
  114.  
  115.     $i++;
  116.   }
  117.  
  118.   &Utils::File::clean_buffer ($buff);
  119.   &Utils::File::save_buffer  ($buff, $file);
  120. }
  121.  
  122. sub change_entry
  123. {
  124.   my ($old_share, $share, $file) = @_;
  125.   my ($buff, $i, $line, @arr);
  126.  
  127.   $buff = &Utils::File::load_buffer ($file);
  128.   $i = 0;
  129.  
  130.   while ($$buff[$i])
  131.   {
  132.     if (!&Utils::Util::ignore_line ($$buff[$i]))
  133.     {
  134.       @arr = split /[ \t]+/, $$buff[$i];
  135.       $$buff[$i] = &get_export_line ($share) if ($arr[0] eq $$old_share[0]);
  136.     }
  137.  
  138.     $i++;
  139.   }
  140.  
  141.   &Utils::File::clean_buffer ($buff);
  142.   &Utils::File::save_buffer  ($buff, $file);
  143. }
  144.  
  145. sub get
  146. {
  147.   my ($nfs_exports_name);
  148.   my (@sections, @table, $entries);
  149.   my $point, $share_info;
  150.  
  151.   $nfs_exports_name = &get_distro_nfs_file ();
  152.  
  153.   $entries = &Utils::Parse::split_hash_with_continuation ($nfs_exports_name, "[ \t]+", "[ \t]+");
  154.  
  155.   foreach $point (keys %$entries)
  156.   {
  157.     my $clients = $$entries{$point};
  158.  
  159.     $share_info = &get_share_info ($clients);
  160.     push @table, [ $point, $share_info ];
  161.   }
  162.  
  163.   return \@table;
  164. }
  165.  
  166. sub set
  167. {
  168.   my ($config) = @_;
  169.   my ($nfs_exports_file);
  170.   my ($old_config, %shares);
  171.   my (%config_hash, %old_config_hash);
  172.   my ($state, $i);
  173.  
  174.   $nfs_exports_name = &get_distro_nfs_file ();
  175.   $old_config = &get ();
  176.  
  177.   foreach $i (@$config)
  178.   {
  179.     $shares{$$i[0]} |= 1;
  180.     $config_hash{$$i[0]} = $i;
  181.   }
  182.  
  183.   foreach $i (@$old_config)
  184.   {
  185.     $shares{$$i[0]} |= 2;
  186.     $old_config_hash{$$i[0]} = $i;
  187.   }
  188.  
  189.   foreach $i (sort keys (%shares))
  190.   {
  191.     $state = $shares{$i};
  192.  
  193.     if ($state == 1)
  194.     {
  195.       # These entries have been added
  196.       &add_entry ($config_hash{$i}, $nfs_exports_name);
  197.     }
  198.     elsif ($state == 2)
  199.     {
  200.       # These entries have been deleted
  201.       &delete_entry ($old_config_hash{$i}, $nfs_exports_name);
  202.     }
  203.     elsif (($state == 3) &&
  204.            (!Utils::Util::struct_eq ($config_hash{$i}, $old_config_hash{$i})))
  205.     {
  206.       # These entries have been modified
  207.       &change_entry ($old_config_hash{$i}, $config_hash{$i}, $nfs_exports_name);
  208.     }
  209.   }
  210. }
  211.  
  212. 1;
  213.