Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is_tarfile() returns True for a blank file

Tags:

python

EDIT 1

Hmm, I accept the answers that tar respects an empty file... but on my system:

$ touch emptytar
$ tar -tf emptytar 
tar: This does not look like a tar archive
tar: Exiting with failure status due to previous errors

Maybe I have a non-canonical version?

$ tar --version
tar (GNU tar) 1.22
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by John Gilmore and Jay Fenlason.

Hello all,

I am testing some logic to handle a user uploading a TAR file. When I feed a blank file to tarfile.is_tarfile() it returns True, which is not what I am expecting:

$ touch tartest
$ cat tartest
$ python -c "import tarfile; print tarfile.is_tarfile('tartest')"
True

If I add some text to the file, it returns False, which I am expecting:

$ echo "not a tar" > tartest
$ python -c "import tarfile; print tarfile.is_tarfile('tartest')"
False

I could add a check at the beginning to check for a zero-length file, but based on the documentation for tarfile.is_tarfile(name) I think this is unecessary:

Return True if name is a tar archive file, that the tarfile module can read.

I went so far as to check the source, tarfile.py, and I can see that it is checking header blocks but I do not fully understand how it is evaluating those blocks.

Am I misreading the documentation and therefore setting unfair expectations?

Thank you,
Zachary

like image 801
Zach Young Avatar asked Jun 17 '10 01:06

Zach Young


People also ask

How can I tell if a tar file is empty?

You probably can just use tarfile. open and check if the descriptor contains anything.

How do I read a tar file in Python?

You can use the tarfile module to read and write tar files. To extract a tar file, you need to first open the file and then use the extract method of the tarfile module.


1 Answers

An empty tar file is a perfectly valid, and empty, tar file. Consider, at any Unix shell prompt:

$ touch foo.tar
$ ls -l foo.tar
-rw-r--r--  1 aleax  staff  0 Jun 16 18:49 foo.tar
$ tar tvf foo.tar 
$ tar xvf foo.tar

See? The empty foo.tar is a perfectly valid tar file for the Unix tar command -- it just has nothing to show or to unpack. It would be truly problematic if Python's tar handling differed so drastically from that of tar itself! What sentence in the docs led you to believe that such a problematic, headache-inducing incompatibility is part of the specs?

like image 62
Alex Martelli Avatar answered Sep 28 '22 08:09

Alex Martelli