Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 15 of 15
  1. #1
    New Coder
    Join Date
    Jul 2009
    Location
    Scotland
    Posts
    44
    Thanks
    2
    Thanked 1 Time in 1 Post

    Problems Creating TAR File

    I am trying to compress a list of selected files into a tar file. The previous time it was done it worked perfectly but for some reason it is now throwing an exception.

    The line to compress the file is:
    my $result = `cd $tempdir && tar zcvf $archive_filename *`;
    When the variable $archive_filename is read in it is correctly taken as /tmp/2009_07_23config.tgz but when the line above is called to create the tar file it is changed for some bizzare reason to /tmp/2009_07_23config.tgztar: *:

    The following errors are thrown afterwards as well:
    tar: *: Cannot stat: No such file or directory
    tar: Error exit delayed from previous errors


    Any ideas of the problem? Any help is greatly appreciated

    Cheers
    Chris

  • #2
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,840
    Thanks
    2
    Thanked 160 Times in 155 Posts
    The starting point would be to use Data::Dumper (just prior to the backticks command) to to dump both of those vars to see exactly what they hold.

    Next would be to break that up into 2 separate commands. I'd use Perl's built-in chdir function to change directories.

    And for portablility, I'd use the Archive::Tar module instead of the shell.
    http://search.cpan.org/~kane/Archive...Archive/Tar.pm

  • #3
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,840
    Thanks
    2
    Thanked 160 Times in 155 Posts
    It also appears that the tar command is missing something.

    shouldn't
    tar zcvf

    be
    tar -zcvf

  • #4
    New Coder
    Join Date
    Jul 2009
    Location
    Scotland
    Posts
    44
    Thanks
    2
    Thanked 1 Time in 1 Post
    I did change it to -zcvf just missed they hyphen when typing. The original code that I have inherited didn't have the hyphen in when I looked back over it and it seems to work ok. The only problem I can detect from the code is that the original file uses a filter to remove the path from the filename whereas the newer one I have created doesn't. Would this be the cause of the problems perhaps?

    Cheers
    Chris

  • #5
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,840
    Thanks
    2
    Thanked 160 Times in 155 Posts
    Yes, it sounds like the filter is causing the problem.

    Can you post a more complete sample of the script that includes the filter code?

  • #6
    New Coder
    Join Date
    Jul 2009
    Location
    Scotland
    Posts
    44
    Thanks
    2
    Thanked 1 Time in 1 Post
    The filter script that:
    # $1 The file to filter.
    #$2 The filtered file to save as.
    grep -i '^\$nix' $1 | grep -iv ', tx[a-z]' | grep -iv ', rangepulse,' > $2
    exit 0


    And the code that calls it:
    foreach $file (@files) {
    debug ($file);
    my $command = "./filter.sh ".$file." ".$tempdir."/".remove_path ($file);
    debug($command);
    my $result = `$command`;
    if (!find_in_list ($file, @previous_archived_logs)) {
    addto_file ($config{'logfiles_archived'}, $file."\n");
    }
    my $result = `cd $tempdir && tar -zcvf $archive_filename *`;


    Chris

  • #7
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,840
    Thanks
    2
    Thanked 160 Times in 155 Posts
    Why spawn 4 external processes to filter the file?

    It appears that a good portion of your Perl script just shells out commands. You really should decide on doing either a shell script or a Perl script. You current code, at least the part you posted, is obfuscated and IMO needlessly spawns extra processes, which all of those extra processes could be done directly in Perl.

    Where is $archive_filename being assigned?

  • #8
    New Coder
    Join Date
    Jul 2009
    Location
    Scotland
    Posts
    44
    Thanks
    2
    Thanked 1 Time in 1 Post
    $archive_filename is pulled in from another class, index.cgi, where the user enters the filename into a textbox and it is submitted when they click the archive button.

    I have narrowed the error down to a few parts, the first is the combination of -zcvf which is wrong as there is no file to compress firstly and changing it to -cfz seems to have eradicated this error.

    Secondly I have a command, $tempdir = tempdir (CLEANUP=>1);, which is supposed to wipe the temp directory but for some reason is taking the temp directory as /tmp/some-other-file and assigning itself a different name at each archive process. Is the command incorrectly structured.

    The final error it is giving is which reference to the '*' at the end of the archive line (my $result = `cd $tempdir && tar -cfz $archive_filename *' where the system is looking for files in the wrong directory. Is there a way of replacing the * with the acutal directory it should be looking in?

    Thanks for the advice so far, the code is a bit of a 'mare since the person who coded it decided comments weren't of any use and left before I started taking the sparse documentation that was available with him

  • #9
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,840
    Thanks
    2
    Thanked 160 Times in 155 Posts
    Since you haven't shown the code for the temdir() sub, I can't say how or what you should be passing to it or why it's not working as expected, but your calling of it is certainly wrong. Try changing it to this:
    Code:
    tempdir( { CLEANUP => 1 } );  # passes an anonymous hash
    or this:
    Code:
    my %options = ( CLEANUP => 1 );
    tempdir( %options );  #  passes a hash
    or
    Code:
    my %options = ( CLEANUP => 1 );
    tempdir( \%options );  #  passes a reference to a hash
    Have you tested your tar command from the command line? It would be my expectation that it would fail and if it fails on the command line, it will certainly fail in the script.

    Do you understand how the tar command works? Have you read its man page?

    The -f option expects/requires a filename parameter. Putting another option between the f option and the filename parameter is invalid.

    Try this:
    Code:
    chdir $tempdir;
    my $result = `tar -czf $archive_filename *`;

  • Users who have thanked FishMonger for this post:

    soper87 (07-24-2009)

  • #10
    New Coder
    Join Date
    Jul 2009
    Location
    Scotland
    Posts
    44
    Thanks
    2
    Thanked 1 Time in 1 Post
    That seems to work, thanks .

    Haven't been able to run tar from the command line because the system at the moment is worked on in Windows on run as a virtual system through a virtual machine acting as an Ubuntu OS.

    Thanks for the help
    Chris

  • #11
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,840
    Thanks
    2
    Thanked 160 Times in 155 Posts
    You're welcome.

    When you have the time, you should look into rewriting that script. The portions of code you posted are poorly written and based on that, I'd say that the entire script is poorly written.

  • #12
    New Coder
    Join Date
    Jul 2009
    Location
    Scotland
    Posts
    44
    Thanks
    2
    Thanked 1 Time in 1 Post
    Think that is my job for the first few months. The joys of inheriting other people's work I suppose

  • #13
    New Coder
    Join Date
    Jul 2009
    Location
    Scotland
    Posts
    44
    Thanks
    2
    Thanked 1 Time in 1 Post
    My system creates a tar file, and seems to be all ok except it prints out:

    tar: Removing leading `/' from member names

    in the error log file and can't seem to find what the actual error is. Anybody have any ideas?

    Cheers
    Chris

  • #14
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,840
    Thanks
    2
    Thanked 160 Times in 155 Posts
    That's not an error, it's a normal operation/message from tar while it's doing its work. It's replacing the leading '/' with a '.' to make the paths relative instead of absolute so that you can specify the destination when you untar it.

    If you want to retain the absolute paths, then use the -P option (see the man page fore tar).

  • #15
    New Coder
    Join Date
    Jul 2009
    Location
    Scotland
    Posts
    44
    Thanks
    2
    Thanked 1 Time in 1 Post
    Found that out afterwards, was being told to investigate it as an error as it was printing to the error log that the system was running.

    Thanks for all the help though
    Chris


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •