~ubuntu-branches/ubuntu/lucid/system-tools-backends/lucid

« back to all changes in this revision

Viewing changes to Utils/File.pm

  • Committer: Bazaar Package Importer
  • Author(s): Emilio Pozuelo Monfort
  • Date: 2009-10-08 11:42:26 UTC
  • mfrom: (1.3.2 upstream)
  • mto: This revision was merged to the branch mainline in revision 65.
  • Revision ID: james.westby@ubuntu.com-20091008114226-phvqz0hulmw9k4zh
Tags: upstream-2.8.2
ImportĀ upstreamĀ versionĀ 2.8.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env perl
 
1
#!/usr/bin/perl
2
2
#-*- Mode: perl; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
3
3
 
4
4
# Functions for file manipulation. Find, open, read, write, backup, etc.
55
55
  return (&get_base_path () . "/backup");
56
56
}
57
57
 
58
 
# Give a command, and it will put in C locale, some sane PATH values and find
 
58
# Give a command, and it will put in C locale and find
59
59
# the program to run in the path. Redirects stderr to null.
60
 
sub do_get_cmd_path
61
 
{
62
 
  my ($cmd) = @_;
63
 
  my ($tool_name, @argline, $command, $tool_path);
64
 
  
65
 
  ($tool_name, @argline) = split("[ \t]+", $cmd);
66
 
 
67
 
  $tool_path = &locate_tool ($tool_name);
68
 
  return -1 if ($tool_path eq "");
69
 
 
70
 
  $command = "$tool_path @argline";
71
 
  # Do not escape args, it's reasonable
72
 
  # to assume they're already escaped
73
 
  #$command =~ s/\"/\\\"/g;
74
 
 
75
 
  return $command;
76
 
}
77
 
 
78
60
sub get_cmd_path
79
61
{
80
62
   my ($cmd) = @_;
81
63
 
82
 
   my $command = &do_get_cmd_path ($cmd);
 
64
   my ($tool_name, @argline) = split("[ \t]+", $cmd);
 
65
   my $tool_path = &locate_tool ($tool_name);
 
66
   return -1 if ($tool_path eq "");
83
67
 
84
 
   return -1 if ($command == -1);
85
 
   return ("LC_ALL=C PATH=\$PATH:/sbin:/usr/sbin $command 2> /dev/null");
 
68
   $command = "$tool_path @argline";
 
69
   return ("LC_ALL=C $command 2> /dev/null");
86
70
}
87
71
 
88
72
# necessary for some programs that output info through stderr
91
75
   my ($cmd) = @_;
92
76
 
93
77
   my $command = &get_cmd_path ($cmd);
94
 
   return ("LC_ALL=C PATH=\$PATH:/sbin:/usr/sbin $command 2>&1");
 
78
   return ("LC_ALL=C $command 2>&1");
95
79
}
96
80
 
97
81
 
460
444
  $command .= " |" if $mode_mask & $FILE_READ;
461
445
  $command = "| $command > /dev/null" if $mode_mask & $FILE_WRITE;
462
446
 
463
 
  open PIPE, $command;
 
447
  open *PIPE, $command;
464
448
  &Utils::Report::do_report ("file_run_pipe_success", $command);
465
449
  &Utils::Report::leave ();
466
450
  return *PIPE;
764
748
# --- Command-line utilities --- #
765
749
 
766
750
 
767
 
# &run (<command line>)
 
751
# &run_full (<in background>, <command>, <array of arguments>)
768
752
#
769
 
# Assumes the first word on the command line is the command-line utility
 
753
# Takes a boolean indicating whether to run the program in the background,
 
754
# an array containing a command to run and the arguments to pass.
 
755
# Assumes the first word in the array is the command-line utility
770
756
# to run, and tries to locate it, replacing it with its full path. The path
771
757
# is cached in a hash, to avoid searching for it repeatedly. Output
772
758
# redirection is appended, to make the utility perfectly silent. The
773
 
# preprocessed command line is run, and its exit value is returned.
 
759
# preprocessed command line is run, and its exit value is returned,
 
760
# or -1 on failure. When run in the background, 0 is returned if
 
761
# the fork did succeed, disregarding possible failure of exec().
774
762
#
775
 
# Example: "mkswap /dev/hda3" -> 'PATH=$PATH:/sbin:/usr/sbin /sbin/mkswap /dev/hda3 2>/dev/null >/dev/null'.
776
763
 
777
 
sub run
 
764
sub run_full
778
765
{
779
 
  my ($cmd, $background) = @_;
780
 
  my ($command, $tool_name, $tool_path, @argline);
 
766
  my ($background, $cmd, @arguments) = @_;
 
767
  my ($command, $tool_name, $tool_path, $pid);
781
768
 
782
769
  &Utils::Report::enter ();
783
770
 
784
 
  $command = &get_cmd_path ($cmd);
 
771
  $tool_path = &locate_tool ($cmd);
 
772
  return -1 if ($tool_path eq "");
785
773
  return -1 if $command == -1;
786
 
  $command .= " > /dev/null";
787
 
  $command .= " &" if $background;
788
774
 
789
 
  &Utils::Report::do_report ("file_run", $command);
 
775
  $command = join (" ", ($tool_path, @arguments));
 
776
  &Utils::Report::do_report ("file_run_full", $command);
790
777
  &Utils::Report::leave ();
791
778
 
 
779
  my $pid = fork();
 
780
 
 
781
  return -1 if (!defined $pid);
 
782
 
 
783
  if ($pid == 0)
 
784
  {
 
785
    $ENV{"LC_ALL"} = "C";
 
786
    open (STDOUT, "/dev/null");
 
787
    open (STDERR, "/dev/null");
 
788
    exec ($tool_path, @arguments);
 
789
    exit (0);
 
790
  }
 
791
 
 
792
  # If no error has occurred so far, assume success,
 
793
  # ignoring the future return value
 
794
  return 0 if ($background);
 
795
 
 
796
  waitpid ($pid, 0);
 
797
 
792
798
  # As documented in perlfunc, divide by 256.
793
 
  return (system ($command) / 256);
 
799
  return ($? / 256);
794
800
}
795
801
 
 
802
# Simple wrappers calling &run_full() with the right background parameter
796
803
sub run_bg
797
804
{
798
 
  my ($cmd) = @_;
 
805
  return &run_full (1, @_);
 
806
}
799
807
 
800
 
  return &run ($cmd, 1);
 
808
sub run
 
809
{
 
810
  return &run_full (0, @_);
801
811
}
802
812
 
803
813
# &gst_file_locate_tool