~rosco2/ubuntu/wily/gramps/bug-1492304

« back to all changes in this revision

Viewing changes to src/TarFile.py

  • Committer: Bazaar Package Importer
  • Author(s): James A. Treacy
  • Date: 2004-06-16 16:53:36 UTC
  • Revision ID: james.westby@ubuntu.com-20040616165336-kjzczqef4gnxrn2b
Tags: upstream-1.0.4
ImportĀ upstreamĀ versionĀ 1.0.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# Gramps - a GTK+/GNOME based genealogy program
 
3
#
 
4
# Copyright (C) 2000  Donald N. Allingham
 
5
#
 
6
# This program is free software; you can redistribute it and/or modify
 
7
# it under the terms of the GNU General Public License as published by
 
8
# the Free Software Foundation; either version 2 of the License, or
 
9
# (at your option) any later version.
 
10
#
 
11
# This program is distributed in the hope that it will be useful,
 
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
# GNU General Public License for more details.
 
15
#
 
16
# You should have received a copy of the GNU General Public License
 
17
# along with this program; if not, write to the Free Software
 
18
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
19
#
 
20
import gzip
 
21
import cStringIO
 
22
import string
 
23
 
 
24
_BLKSIZE=512
 
25
nul = '\0'
 
26
 
 
27
#------------------------------------------------------------------------
 
28
#
 
29
# TarFile
 
30
#
 
31
#------------------------------------------------------------------------
 
32
class TarFile:
 
33
    def __init__(self,name):
 
34
        self.name = name
 
35
        self.f = gzip.open(name,"wb")
 
36
        self.pos = 0
 
37
        
 
38
    def add_file(self,filename,mtime,iobuf):
 
39
        iobuf.seek(0,2)
 
40
        length = iobuf.tell()
 
41
        iobuf.seek(0)
 
42
 
 
43
        buf = filename
 
44
        buf = buf + '\0'*(100-len(filename))
 
45
        buf = buf + "0100664" + nul
 
46
        buf = buf + "0000764" + nul
 
47
        buf = buf + "0000764" + nul
 
48
        buf = buf + "%011o" % length + nul
 
49
        buf = buf + "%011o" % mtime + nul
 
50
        buf = buf + "%s"
 
51
        buf = buf + "0" + '\0'*100 + 'ustar  \0'
 
52
        buf = buf + '\0'*32
 
53
        buf = buf + '\0'*32
 
54
        buf = buf + '\0'*183
 
55
 
 
56
        chksum = 0
 
57
        blank = "        "
 
58
        temp = buf % (blank)
 
59
        for c in temp:
 
60
            chksum = chksum + ord(c)
 
61
        sum = "%06o " % chksum
 
62
        sum = sum + nul
 
63
        buf = buf % sum
 
64
 
 
65
        self.pos = self.pos + len(buf)
 
66
        self.f.write(buf)
 
67
 
 
68
        buf = iobuf.read(length)
 
69
        self.f.write(buf)
 
70
        self.pos = self.pos + length
 
71
        rem = _BLKSIZE - (self.pos % _BLKSIZE)
 
72
        if rem != 0:
 
73
            self.f.write('\0' * rem)
 
74
        self.pos = self.pos + rem
 
75
 
 
76
    def close(self):
 
77
        rem = (_BLKSIZE*20) - (self.pos % (_BLKSIZE*20))
 
78
        if rem != 0:
 
79
            self.f.write('\0' * rem)
 
80
        self.f.close()
 
81
 
 
82
#------------------------------------------------------------------------
 
83
#
 
84
# ReadTarFile
 
85
#
 
86
#------------------------------------------------------------------------
 
87
class ReadTarFile:
 
88
    def __init__(self,name,wd="/tmp"):
 
89
        self.name = name
 
90
        self.wd = wd
 
91
        self.f = gzip.open(name,"rb")
 
92
        self.pos = 0
 
93
 
 
94
    def extract_files(self):
 
95
        map = {}
 
96
        while 1:
 
97
            buf = self.f.read(100)
 
98
            if buf == '':
 
99
                return
 
100
            index = 0
 
101
            for b in buf:
 
102
                if b != nul:
 
103
                    index = index + 1
 
104
                else:
 
105
                    if index == 0:
 
106
                        return map
 
107
                    continue
 
108
            filename = buf[0:index]
 
109
            if filename == None:
 
110
                return map
 
111
            self.f.read(24) # modes
 
112
            l = string.replace(self.f.read(12),chr(0),' ')
 
113
            length = int(l,8) 
 
114
            self.f.read(12)
 
115
            self.f.read(6)
 
116
            self.f.read(111)
 
117
 
 
118
            self.f.read(64)
 
119
            self.f.read(183)
 
120
            foo = cStringIO.StringIO()
 
121
            map[filename] = foo
 
122
            foo.write(self.f.read(length))
 
123
            foo.reset()
 
124
            self.f.read(_BLKSIZE-(length%_BLKSIZE))
 
125
    
 
126
    def extract(self):
 
127
        while 1:
 
128
            buf = self.f.read(100)
 
129
            if buf == '':
 
130
                return
 
131
            index = 0
 
132
            for b in buf:
 
133
                if b != nul:
 
134
                    index = index + 1
 
135
                else:
 
136
                    if index == 0:
 
137
                        return
 
138
                    continue
 
139
            filename = buf[0:index]
 
140
            self.f.read(24) # modes
 
141
            l = self.f.read(12)
 
142
            length_string = "";
 
143
            for char in l:
 
144
                if ord(char) != 0:
 
145
                    length_string = length_string + char
 
146
            length = int(length_string,8) 
 
147
            self.f.read(12)
 
148
            self.f.read(6)
 
149
            self.f.read(111)
 
150
 
 
151
            self.f.read(64)
 
152
            self.f.read(183)
 
153
            foo = open("%s/%s" % (self.wd,filename),"wb")
 
154
            foo.write(self.f.read(length))
 
155
            foo.close()
 
156
            self.f.read(_BLKSIZE-(length%_BLKSIZE))
 
157
 
 
158
    def close(self):
 
159
        self.f.close()
 
160