gramps/src/TarFile.py

163 lines
4.6 KiB
Python
Raw Normal View History

2002-10-20 19:55:16 +05:30
#
# Gramps - a GTK+/GNOME based genealogy program
#
2005-05-24 18:38:06 +05:30
# Copyright (C) 2000-2005 Donald N. Allingham
2002-10-20 19:55:16 +05:30
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
2005-05-24 18:38:06 +05:30
# $Id$
2002-10-20 19:55:16 +05:30
import gzip
import cStringIO
_BLKSIZE=512
nul = '\0'
2003-02-24 10:21:57 +05:30
#------------------------------------------------------------------------
#
# TarFile
#
#------------------------------------------------------------------------
2002-10-20 19:55:16 +05:30
class TarFile:
def __init__(self,name):
self.name = name
self.f = gzip.open(name,"wb")
self.pos = 0
def add_file(self,filename,mtime,iobuf):
iobuf.seek(0,2)
length = iobuf.tell()
iobuf.seek(0)
buf = filename
buf = buf + '\0'*(100-len(filename))
buf = buf + "0100664" + nul
buf = buf + "0000764" + nul
buf = buf + "0000764" + nul
buf = buf + "%011o" % length + nul
buf = buf + "%011o" % mtime + nul
buf = buf + "%s"
buf = buf + "0" + '\0'*100 + 'ustar \0'
buf = buf + '\0'*32
buf = buf + '\0'*32
buf = buf + '\0'*183
chksum = 0
blank = " "
temp = buf % (blank)
for c in temp:
chksum = chksum + ord(c)
2004-05-14 04:15:51 +05:30
vsum = "%06o " % chksum
vsum = vsum + nul
buf = buf % vsum
2002-10-20 19:55:16 +05:30
self.pos = self.pos + len(buf)
self.f.write(buf)
buf = iobuf.read(length)
self.f.write(buf)
self.pos = self.pos + length
rem = _BLKSIZE - (self.pos % _BLKSIZE)
if rem != 0:
self.f.write('\0' * rem)
self.pos = self.pos + rem
def close(self):
rem = (_BLKSIZE*20) - (self.pos % (_BLKSIZE*20))
if rem != 0:
self.f.write('\0' * rem)
self.f.close()
2003-02-24 10:21:57 +05:30
#------------------------------------------------------------------------
#
# ReadTarFile
#
#------------------------------------------------------------------------
2002-10-20 19:55:16 +05:30
class ReadTarFile:
def __init__(self,name,wd="/tmp"):
2002-10-20 19:55:16 +05:30
self.name = name
2005-05-24 18:38:06 +05:30
self.wd = wd
2002-10-20 19:55:16 +05:30
self.f = gzip.open(name,"rb")
self.pos = 0
def extract_files(self):
data = {}
2005-05-24 18:38:06 +05:30
while 1:
buf = self.f.read(100)
2002-10-20 19:55:16 +05:30
if buf == '':
2005-05-24 18:38:06 +05:30
return
2002-10-20 19:55:16 +05:30
index = 0
2005-05-24 18:38:06 +05:30
for b in buf:
if b != nul:
index = index + 1
else:
if index == 0:
return data
2005-05-24 18:38:06 +05:30
continue
filename = buf[0:index]
2002-10-20 19:55:16 +05:30
if filename == None:
return data
2005-05-24 18:38:06 +05:30
self.f.read(24) # modes
l = self.f.read(12)(chr(0),' ')
2003-01-12 10:52:21 +05:30
length = int(l,8)
2005-05-24 18:38:06 +05:30
self.f.read(12)
self.f.read(6)
self.f.read(111)
2002-10-20 19:55:16 +05:30
2005-05-24 18:38:06 +05:30
self.f.read(64)
self.f.read(183)
2002-10-20 19:55:16 +05:30
foo = cStringIO.StringIO()
data[filename] = foo
2005-05-24 18:38:06 +05:30
foo.write(self.f.read(length))
foo.reset()
self.f.read(_BLKSIZE-(length%_BLKSIZE))
return data
2002-10-20 19:55:16 +05:30
def extract(self):
2005-05-24 18:38:06 +05:30
while 1:
buf = self.f.read(100)
2002-10-20 19:55:16 +05:30
if buf == '':
2005-05-24 18:38:06 +05:30
return
2002-10-20 19:55:16 +05:30
index = 0
2005-05-24 18:38:06 +05:30
for b in buf:
if b != nul:
index = index + 1
else:
if index == 0:
2002-10-20 19:55:16 +05:30
return
2005-05-24 18:38:06 +05:30
continue
filename = buf[0:index]
self.f.read(24) # modes
2002-10-20 19:55:16 +05:30
l = self.f.read(12)
length_string = "";
for char in l:
if ord(char) != 0:
length_string = length_string + char
2003-01-12 10:52:21 +05:30
length = int(length_string,8)
2005-05-24 18:38:06 +05:30
self.f.read(12)
self.f.read(6)
self.f.read(111)
2002-10-20 19:55:16 +05:30
2005-05-24 18:38:06 +05:30
self.f.read(64)
self.f.read(183)
2003-02-24 10:21:57 +05:30
foo = open("%s/%s" % (self.wd,filename),"wb")
2005-05-24 18:38:06 +05:30
foo.write(self.f.read(length))
foo.close()
self.f.read(_BLKSIZE-(length%_BLKSIZE))
2002-10-20 19:55:16 +05:30
def close(self):
self.f.close()