Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Extracting files Using Archive::Tar?

by heezy (Monk)
on Oct 07, 2002 at 21:46 UTC ( [id://203500]=perlquestion: print w/replies, xml ) Need Help??

heezy has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I need to extract files from a tar that has been submitted via an HTML upload (cgi.pm)

At the moment the file is uploaded to the correct directory with the following persmissions..

drwxr-xr-x   2 nobody   nobody       512 Oct  7 15:40 ./
drwxrwxrwx  46 mf139823 staff       1536 Oct  7 15:40 ../
-rw-r--r--   1 nobody   nobody    461824 Oct  7 15:40 sample.tar

I thought it would be a simple case of creating a tar object and asking it extract all files in the archive. Apparently not? What am I doing thats crazy

open TARFILE, "/.....$submissionID/$user_File_Filename"; $tar = Archive::Tar->new(); $tar->extract_archive(TARFILE);

Any help would be gratefully recieved

Many thanks

M

Replies are listed 'Best First'.
Re: Extracting files Using Archive::Tar?
by Flexx (Pilgrim) on Oct 07, 2002 at 22:35 UTC

    Hi, without much thinking about it, "/.....$submissionID/$user_File_Filename" instantly appears to be quite a strange filename to me. Are you sure you don't mean "../../.$submissionID/$user_File_Filename", or something like that?

    Just a "hipshot", but maybe it hits...

    Apart from that, It's hard to help you, when you're not providing a minimal test case. Reduce your program to a minimal script that exhibits the error and repost (Chances are you'll discover the bug yourself by doing so). It might well be that the bug is not in the three lines you posted.

    For example, it might be that $submissionID or $user_File_Filename might not have the values you'd expect them to hold (print them at some point above the lines you presented to make sure).

    You're also not checking the return code of open, Archive::Tar->new and $tar->extract_archive(TARFILE).

    Do you use strict and warnings?

    So long,
    Flexx

      Ahhh... I see... extract_archive returns something like "Can't find file or directory" sorry I can't remmember exactaly I'm not at work at the mo

      I thank you all for your help with this. If I can't resolve the problem I'll repost to this thread around 10am Mountain Time (-7hGMT)

      Thanks again everyone

      M

      Hmmm... okay I have no checked all the retrun values as you quite rightly suggested and it appears that $tar->error contains...

      "No such file or directory"

      That is not good.. so I checked and double checked that the file is there, it is. So I thought to make sure I would do an exsits file test (-e) The test found the file.

      So why would Archive::Tar report that the file wasn't there when an exsits test says the file is there?

      Here's the revised code

      sub extractTarFile{ print $cgi->header(); ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime( +time); $year = $year + 1900; $mon = &formatDigit($mon ++); $mday = &formatDigit($mday); $hour = &formatDigit($hour); $min = &formatDigit($min); $sec = &formatDigit($sec); $submissionID = "$year-$mon-$mday-$hour-$min-$sec-$user_Name"; # +yyyy-mm-dd-hhmmss mkdir "$submittedFiles/$submissionID", 0777 or warn "Cannot make d +irectory: $!"; copy ("$tmpFilePosition", "$submittedFiles/$submissionID/$user_Fil +e_Filename"); $tarLocation = "$submittedFiles/$submissionID/$user_File_Filename" +; $RC_open = open TARFILE, $tarLocation; if (-e TARFILE){ print $cgi->p("File exsists"); } $RC_new = $tar = Archive::Tar->new(); $RC_extract = $tar->extract_archive("TARFILE"); $tar_errors = $tar->error(); print $cgi->p("Return Val open ($RC_open)"); print $cgi->p("Return Val New tar ($RC_new)"); print $cgi->p("Return Val Extract ($RC_extract)"); print $cgi->p("Tar Location is ($tarLocation)"); print $cgi->p("Tar errors ($tar_errors)"); print $cgi->p("submittedFiles ($submittedFiles)"); print $cgi->p("submissionID ($submissionID)"); print $cgi->p("userFileFilename ($user_File_Filename)"); close TARFILE; }

      And the new output from the browser...

      
      File exsists
      
      Return Val open (1)
      
      Return Val New tar (Archive::Tar=HASH(0x3be9cc))
      
      Return Val Extract ()
      
      Tar Location is (/webroot/service/servicelist_submission/submitted_files/2002-09-08-08-32-25-Mark/sample.tar)
      
      Tar errors (No such file or directory)
      
      submittedFiles (/webroot/service/servicelist_submission/submitted_files)
      
      submissionID (2002-09-08-08-32-25-Mark)
      
      userFileFilename (sample.tar)
      
      

      Whilst I'm at it I'll show you the permissions on the files as well although I don't think it's that...?...

      mf139823@esweb: pwd
      /apps/webroot/service/servicelist_submission/submitted_files/2002-09-08-08-32-25-Mark
      308 mf139823@esweb: ls -l
      total 932
      drwxr-xr-x   2 nobody   nobody       512 Oct  8 08:32 ./
      drwxrwxrwx  23 mf139823 staff       1024 Oct  8 08:32 ../
      -rw-r--r--   1 nobody   nobody    461824 Oct  8 08:32 sample.tar
      mf139823@esweb
      

      Am I doing something crazy?

      Please help!

      M

        I realise

        $RC_extract = $tar->extract_archive("TARFILE");

        Should actually be written without the quotation marks around TARFILE. I have now changed this but $tar->error still maintains it can't find the file??....ideas?..Anyone?

        M

Re: Extracting files Using Archive::Tar?
by valdez (Monsignor) on Oct 07, 2002 at 22:24 UTC

    Hi heezy,

    have you checked the result value of extract_archive? if it returns undef, then you should call `error' method to find the cause of the failure.

    HTH, Valerio

Re: Extracting files Using Archive::Tar?
by valdez (Monsignor) on Oct 08, 2002 at 17:18 UTC

    I rewrote your subroutine, and changed something:

    sub extractTarFile { my ($strftime, $submissionID, $cur_submit_dir, $tarLocation, $old_cwd, $tar, $RC_extract); my ($user_Name, $tmpFilePosition, $user_File_Filename) = @_; # just to save some typing $strftime = POSIX::strftime("%Y-%m-%d-%H-%M-%S", localtime); # note that this ID is not unique $submissionID = "$strftime-$user_Name"; $cur_submit_dir = "$submittedFiles/$submissionID"; $tarLocation = "$cur_submit_dir/$user_File_Filename"; # do you really need to create a world writable directory? mkdir($cur_submit_dir, 0755) or die "Cannot make directory: $! +"; copy("$tmpFilePosition", "$tarLocation") or die "copy failed: +$!"; # store our current working directory $old_cwd = Cwd::cwd; # change to submitted files current dir Cwd::chdir($cur_submit_dir); $tar = Archive::Tar->new(); $RC_extract = $tar->extract_archive($tarLocation); # restore previous working dir Cwd::chdir $old_cwd; return $RC_extract, $tar->error(); }

    Few notes: Archive::Tar extracts archives in current directory, so you need to change your working directory before extracting and possibly restore it when done. I also used strftime, checked return values of file operations instead using -e and removed the open that was unnecessary. All paths must be absolute.

    Ciao, Valerio

    Update:You are welcome heezy :) I couldn't believe my eyes when I realized that I was writing somewhere else...
    Oh, I forgot to do some final cleaning: we should remove submitted files from $tmpFilePosition and $tarLocation... Ciao!

      It works!!! Thats great thank you!! You tidied up my code a lot (thank you) but I think it's more the use of Cwd::cwd that made the difference? Is that right?

      Thanks everyone I spoke to on chaterbox and all the people that replied here, I would be lost without you all!

      M

Re: Extracting files Using Archive::Tar?
by heezy (Monk) on Oct 07, 2002 at 21:51 UTC

    Appologies for not including this..

    No errors are displayed to the screen, no errors are present in the servers logs, and no files are unzipped (or if they are I'm not sure where's it putting them!)

    Thanks

    M

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://203500]
Approved by fglock
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (2)
As of 2024-04-24 22:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found