diff options
Diffstat (limited to 'python/tar.cc')
| -rw-r--r-- | python/tar.cc | 165 |
1 files changed, 165 insertions, 0 deletions
diff --git a/python/tar.cc b/python/tar.cc new file mode 100644 index 00000000..09f6b680 --- /dev/null +++ b/python/tar.cc @@ -0,0 +1,165 @@ +// -*- mode: cpp; mode: fold -*- +// Description /*{{{*/ +// $Id: tar.cc,v 1.1 2001/09/30 03:52:58 jgg Exp $ +/* ###################################################################### + + Tar Inteface + + ##################################################################### */ + /*}}}*/ +// Include Files /*{{{*/ +#include "generic.h" + +#include <apt-pkg/extracttar.h> +#include <apt-pkg/error.h> +#include <apt-pkg/debfile.h> +#include "apt_instmodule.h" + +#include <python/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; + } + + 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" +"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,"O!O!s",&PyFile_Type,&File,&PyFunction_Type, + &Function,&Comp) == 0) + return 0; + + { + // Open the file and associate the tar + FileFd Fd(fileno(PyFile_AsFile(File)),false); + ExtractTar Tar(Fd,0xFFFFFFFF); + 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" +"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; + + if (PyArg_ParseTuple(Args,"O!O!s",&PyFile_Type,&File,&PyFunction_Type, + &Function,&Chunk) == 0) + return 0; + + { + // Open the file and associate the tar + // Open the file and associate the .deb + FileFd Fd(fileno(PyFile_AsFile(File)),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 fund chunk %s",Chunk); + return HandleErrors(); + } + + // Extract it. + ExtractTar Tar(Deb.GetFile(),Member->Size); + ProcessTar Proc(Function); + if (Tar.Go(Proc) == false) + return HandleErrors(); + } + + Py_INCREF(Py_None); + return HandleErrors(Py_None); +} + /*}}}*/ |
