File: tar.cc

package info (click to toggle)
python-apt 0.7.100.1%2Bsqueeze1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 2,748 kB
  • ctags: 1,919
  • sloc: cpp: 8,937; python: 5,750; makefile: 89; sh: 9
file content (195 lines) | stat: -rw-r--r-- 4,831 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// -*- mode: cpp; mode: fold -*-
// Description								/*{{{*/
// $Id: tar.cc,v 1.4 2004/12/12 17:55:54 mdz Exp $
/* ######################################################################

   Tar Inteface
   * THIS FILE IS COMPLETELY DEPRECATED, AND NOT USED ANYMORE IF BUILT *
   * WITHOUT COMPATIBILITY. *

   ##################################################################### */
									/*}}}*/
// Include Files							/*{{{*/
#ifdef COMPAT_0_7
#include "generic.h"

#include <apt-pkg/extracttar.h>
#include <apt-pkg/error.h>
#include <apt-pkg/debfile.h>
#include "apt_instmodule.h"

#include <Python.h>
									/*}}}*/

class ProcessTar : public pkgDirStream
{
   public:

   PyObject *Function;

   virtual bool DoItem(Item &Itm,int &Fd);

   ProcessTar(PyObject *Function) : Function(Function)
   {
      Py_INCREF(Function);
   }
   virtual ~ProcessTar()
   {
      Py_DECREF(Function);
   }
};

// ProcessTar::DoItem - Feed an item to a python function		/*{{{*/
// ---------------------------------------------------------------------
/* The function is called with a tuple that has:
   (FileName,Link,Mode,UID,GID,Size,MTime,Major,Minor) */
bool ProcessTar::DoItem(Item &Itm,int &Fd)
{
   const char *Type;
   switch (Itm.Type)
   {
      case Item::File:
      Type = "FILE";
      break;

      case Item::HardLink:
      Type = "HARDLINK";
      break;

      case Item::SymbolicLink:
      Type = "SYMLINK";
      break;

      case Item::CharDevice:
      Type = "CHARDEV";
      break;

      case Item::BlockDevice:
      Type = "BLKDEV";
      break;

      case Item::Directory:
      Type = "DIR";
      break;

      case Item::FIFO:
      Type = "FIFO";
      break;

      default:
      return false;
   }


   if (PyObject_CallFunction(Function,"sssiiiiiii",Type,Itm.Name,
			     Itm.LinkTarget,Itm.Mode,Itm.UID,Itm.GID,Itm.Size,
			     Itm.MTime,Itm.Major,Itm.Minor) == 0)
      return false;

   Fd = -1;
   return true;
}
									/*}}}*/

// tarExtract - Examine files from a tar				/*{{{*/
// ---------------------------------------------------------------------
/* */
char *doc_tarExtract =
"tarExtract(File,Func,Comp) -> None\n"
"The tar file referenced by the file object File, Func called for each\n"
"Tar member. Comp must be the string \"gzip\" (gzip is automatically invoked) \n";
PyObject *tarExtract(PyObject *Self,PyObject *Args)
{
   PyObject *File;
   PyObject *Function;
   char *Comp;

   if (PyArg_ParseTuple(Args,"OOs",&File, &Function,&Comp) == 0)
      return 0;

   if (PyCallable_Check(Function) == 0)
   {
      PyErr_SetString(PyExc_TypeError,"argument 2: expected something callable.");
      return 0;
   }

   {
      // Open the file and associate the tar
      int fileno = PyObject_AsFileDescriptor(File);
      if (fileno == -1)
        return 0;

      FileFd Fd(fileno,false);
      ExtractTar Tar(Fd,0xFFFFFFFF,Comp);
      if (_error->PendingError() == true)
	 return HandleErrors();

      ProcessTar Proc(Function);
      if (Tar.Go(Proc) == false)
	 return HandleErrors();
   }

   Py_INCREF(Py_None);
   return HandleErrors(Py_None);
}
									/*}}}*/

// debExtract - Examine files from a deb				/*{{{*/
// ---------------------------------------------------------------------
/* */
char *doc_debExtract =
"debExtract(File,Func,Chunk) -> None\n"
"The deb referenced by the file object File is examined. The AR member\n"
"given by Chunk is treated as a tar.gz and fed through Func like\n"
"tarExtract\n";
PyObject *debExtract(PyObject *Self,PyObject *Args)
{
   PyObject *File;
   PyObject *Function;
   char *Chunk;
   const char *Comp = "gzip";

   if (PyArg_ParseTuple(Args,"OOs",&File,&Function,&Chunk) == 0)
      return 0;

   if (PyCallable_Check(Function) == 0)
   {
      PyErr_SetString(PyExc_TypeError,"argument 2: expected something callable.");
      return 0;
   }

   int fileno = PyObject_AsFileDescriptor(File);
   if (fileno == -1)
      return 0;
   {
      // Open the file and associate the tar
      // Open the file and associate the .deb
      FileFd Fd(fileno,false);
      debDebFile Deb(Fd);
      if (_error->PendingError() == true)
	 return HandleErrors();

      // Get the archive member and positition the file
      const ARArchive::Member *Member = Deb.GotoMember(Chunk);
      if (Member == 0)
      {
	 _error->Error("Cannot find chunk %s",Chunk);
	 return HandleErrors();
      }

      // Extract it.
      if (strcmp(".bz2", &Chunk[strlen(Chunk)-4]) == 0)
	      Comp = "bzip2";
      else if(strcmp(".lzma", &Chunk[strlen(Chunk)-5]) == 0)
	      Comp = "lzma";
      ExtractTar Tar(Deb.GetFile(),Member->Size,Comp);
      ProcessTar Proc(Function);
      if (Tar.Go(Proc) == false)
	 return HandleErrors();
   }

   Py_INCREF(Py_None);
   return HandleErrors(Py_None);
}
									/*}}}*/
#endif // defined(COMPAT_0_7)