Просмотр исходного кода

* apt-pkg/contrib/fileutl.{h,cc}:
- Add support for transparent reading of gzipped files.
- Link against zlib (in apt-pkg/makefile) and add zlib build dependency.

martin@piware.de лет назад: 16
Родитель
Сommit
a3a03f5d74
5 измененных файлов с 55 добавлено и 11 удалено
  1. 41 6
      apt-pkg/contrib/fileutl.cc
  2. 6 3
      apt-pkg/contrib/fileutl.h
  3. 1 1
      apt-pkg/makefile
  4. 6 0
      debian/changelog
  5. 1 1
      debian/control

+ 41 - 6
apt-pkg/contrib/fileutl.cc

@@ -11,6 +11,7 @@
    Most of this source is placed in the Public Domain, do with it what 
    Most of this source is placed in the Public Domain, do with it what 
    you will
    you will
    It was originally written by Jason Gunthorpe <jgg@debian.org>.
    It was originally written by Jason Gunthorpe <jgg@debian.org>.
+   FileFd gzip support added by Martin Pitt <martin.pitt@canonical.com>
    
    
    The exception is RunScripts() it is under the GPLv2
    The exception is RunScripts() it is under the GPLv2
 
 
@@ -603,6 +604,13 @@ bool FileFd::Open(string FileName,OpenMode Mode, unsigned long Perms)
    {
    {
       case ReadOnly:
       case ReadOnly:
       iFd = open(FileName.c_str(),O_RDONLY);
       iFd = open(FileName.c_str(),O_RDONLY);
+      if (iFd > 0 && FileName.compare(FileName.size()-3, 3, ".gz") == 0) {
+	  gz = gzdopen (iFd, "r");
+	  if (gz == NULL) {
+	      close (iFd);
+	      iFd = -1;
+	  }
+      }
       break;
       break;
       
       
       case WriteEmpty:
       case WriteEmpty:
@@ -658,7 +666,10 @@ bool FileFd::Read(void *To,unsigned long Size,unsigned long *Actual)
    
    
    do
    do
    {
    {
-      Res = read(iFd,To,Size);
+      if (gz != NULL)
+         Res = gzread(gz,To,Size);
+      else
+         Res = read(iFd,To,Size);
       if (Res < 0 && errno == EINTR)
       if (Res < 0 && errno == EINTR)
 	 continue;
 	 continue;
       if (Res < 0)
       if (Res < 0)
@@ -697,7 +708,10 @@ bool FileFd::Write(const void *From,unsigned long Size)
    errno = 0;
    errno = 0;
    do
    do
    {
    {
-      Res = write(iFd,From,Size);
+      if (gz != NULL)
+         Res = gzwrite(gz,From,Size);
+      else
+         Res = write(iFd,From,Size);
       if (Res < 0 && errno == EINTR)
       if (Res < 0 && errno == EINTR)
 	 continue;
 	 continue;
       if (Res < 0)
       if (Res < 0)
@@ -723,7 +737,12 @@ bool FileFd::Write(const void *From,unsigned long Size)
 /* */
 /* */
 bool FileFd::Seek(unsigned long To)
 bool FileFd::Seek(unsigned long To)
 {
 {
-   if (lseek(iFd,To,SEEK_SET) != (signed)To)
+   int res;
+   if (gz)
+      res = gzseek(gz,To,SEEK_SET);
+   else
+      res = lseek(iFd,To,SEEK_SET);
+   if (res != (signed)To)
    {
    {
       Flags |= Fail;
       Flags |= Fail;
       return _error->Error("Unable to seek to %lu",To);
       return _error->Error("Unable to seek to %lu",To);
@@ -737,7 +756,12 @@ bool FileFd::Seek(unsigned long To)
 /* */
 /* */
 bool FileFd::Skip(unsigned long Over)
 bool FileFd::Skip(unsigned long Over)
 {
 {
-   if (lseek(iFd,Over,SEEK_CUR) < 0)
+   int res;
+   if (gz)
+      res = gzseek(gz,Over,SEEK_CUR);
+   else
+      res = lseek(iFd,Over,SEEK_CUR);
+   if (res < 0)
    {
    {
       Flags |= Fail;
       Flags |= Fail;
       return _error->Error("Unable to seek ahead %lu",Over);
       return _error->Error("Unable to seek ahead %lu",Over);
@@ -751,6 +775,11 @@ bool FileFd::Skip(unsigned long Over)
 /* */
 /* */
 bool FileFd::Truncate(unsigned long To)
 bool FileFd::Truncate(unsigned long To)
 {
 {
+   if (gz)
+   {
+      Flags |= Fail;
+      return _error->Error("Truncating gzipped files is not implemented (%s)", FileName.c_str());
+   }
    if (ftruncate(iFd,To) != 0)
    if (ftruncate(iFd,To) != 0)
    {
    {
       Flags |= Fail;
       Flags |= Fail;
@@ -765,7 +794,11 @@ bool FileFd::Truncate(unsigned long To)
 /* */
 /* */
 unsigned long FileFd::Tell()
 unsigned long FileFd::Tell()
 {
 {
-   off_t Res = lseek(iFd,0,SEEK_CUR);
+   off_t Res;
+   if (gz)
+     Res = gztell(gz);
+   else
+     Res = lseek(iFd,0,SEEK_CUR);
    if (Res == (off_t)-1)
    if (Res == (off_t)-1)
       _error->Errno("lseek","Failed to determine the current file position");
       _error->Errno("lseek","Failed to determine the current file position");
    return Res;
    return Res;
@@ -776,6 +809,7 @@ unsigned long FileFd::Tell()
 /* */
 /* */
 unsigned long FileFd::Size()
 unsigned long FileFd::Size()
 {
 {
+   //TODO: For gz, do we need the actual file size here or the uncompressed length?
    struct stat Buf;
    struct stat Buf;
    if (fstat(iFd,&Buf) != 0)
    if (fstat(iFd,&Buf) != 0)
       return _error->Errno("fstat","Unable to determine the file size");
       return _error->Errno("fstat","Unable to determine the file size");
@@ -789,9 +823,10 @@ bool FileFd::Close()
 {
 {
    bool Res = true;
    bool Res = true;
    if ((Flags & AutoClose) == AutoClose)
    if ((Flags & AutoClose) == AutoClose)
-      if (iFd >= 0 && close(iFd) != 0)
+      if ((gz != NULL && gzclose(gz) != 0) || (gz == NULL && iFd > 0 && close(iFd) != 0))
 	 Res &= _error->Errno("close",_("Problem closing the file"));
 	 Res &= _error->Errno("close",_("Problem closing the file"));
    iFd = -1;
    iFd = -1;
+   gz = NULL;
    
    
    if ((Flags & Fail) == Fail && (Flags & DelOnFail) == DelOnFail &&
    if ((Flags & Fail) == Fail && (Flags & DelOnFail) == DelOnFail &&
        FileName.empty() == false)
        FileName.empty() == false)

+ 6 - 3
apt-pkg/contrib/fileutl.h

@@ -25,6 +25,8 @@
 #include <string>
 #include <string>
 #include <vector>
 #include <vector>
 
 
+#include <zlib.h>
+
 using std::string;
 using std::string;
 
 
 class FileFd
 class FileFd
@@ -36,6 +38,7 @@ class FileFd
                     HitEof = (1<<3)};
                     HitEof = (1<<3)};
    unsigned long Flags;
    unsigned long Flags;
    string FileName;
    string FileName;
+   gzFile gz;
    
    
    public:
    public:
    enum OpenMode {ReadOnly,WriteEmpty,WriteExists,WriteAny,WriteTemp};
    enum OpenMode {ReadOnly,WriteEmpty,WriteExists,WriteAny,WriteTemp};
@@ -69,12 +72,12 @@ class FileFd
    inline string &Name() {return FileName;};
    inline string &Name() {return FileName;};
    
    
    FileFd(string FileName,OpenMode Mode,unsigned long Perms = 0666) : iFd(-1), 
    FileFd(string FileName,OpenMode Mode,unsigned long Perms = 0666) : iFd(-1), 
-            Flags(0) 
+            Flags(0), gz(NULL)
    {
    {
       Open(FileName,Mode,Perms);
       Open(FileName,Mode,Perms);
    };
    };
-   FileFd(int Fd = -1) : iFd(Fd), Flags(AutoClose) {};
-   FileFd(int Fd,bool) : iFd(Fd), Flags(0) {};
+   FileFd(int Fd = -1) : iFd(Fd), Flags(AutoClose), gz(NULL) {};
+   FileFd(int Fd,bool) : iFd(Fd), Flags(0), gz(NULL) {};
    virtual ~FileFd();
    virtual ~FileFd();
 };
 };
 
 

+ 1 - 1
apt-pkg/makefile

@@ -14,7 +14,7 @@ include ../buildlib/libversion.mak
 LIBRARY=apt-pkg
 LIBRARY=apt-pkg
 MAJOR=$(LIBAPTPKG_MAJOR)
 MAJOR=$(LIBAPTPKG_MAJOR)
 MINOR=$(LIBAPTPKG_RELEASE)
 MINOR=$(LIBAPTPKG_RELEASE)
-SLIBS=$(PTHREADLIB) $(INTLLIBS) -lutil -ldl
+SLIBS=$(PTHREADLIB) $(INTLLIBS) -lutil -ldl -lz
 APT_DOMAIN:=libapt-pkg$(LIBAPTPKG_MAJOR)
 APT_DOMAIN:=libapt-pkg$(LIBAPTPKG_MAJOR)
 
 
 # Source code for the contributed non-core things
 # Source code for the contributed non-core things

+ 6 - 0
debian/changelog

@@ -1,8 +1,14 @@
 apt (0.7.26~exp5) UNRELEASED; urgency=low
 apt (0.7.26~exp5) UNRELEASED; urgency=low
 
 
+  [ Christian Perrier ]
   * Slovak translation update. Closes: #581159
   * Slovak translation update. Closes: #581159
   * Italian translation update. Closes: #581742
   * Italian translation update. Closes: #581742
 
 
+  [ Martin Pitt ]
+  * apt-pkg/contrib/fileutl.{h,cc}:
+    - Add support for transparent reading of gzipped files.
+    - Link against zlib (in apt-pkg/makefile) and add zlib build dependency.
+
  -- Christian Perrier <bubulle@debian.org>  Tue, 11 May 2010 19:52:00 +0200
  -- Christian Perrier <bubulle@debian.org>  Tue, 11 May 2010 19:52:00 +0200
 
 
 apt (0.7.26~exp4) unstable; urgency=low
 apt (0.7.26~exp4) unstable; urgency=low

+ 1 - 1
debian/control

@@ -6,7 +6,7 @@ Uploaders: Michael Vogt <mvo@debian.org>, Otavio Salvador <otavio@debian.org>,
  Christian Perrier <bubulle@debian.org>, Daniel Burrows <dburrows@debian.org>,
  Christian Perrier <bubulle@debian.org>, Daniel Burrows <dburrows@debian.org>,
  Luca Bruno <lethalman88@gmail.com>, Julian Andres Klode <jak@debian.org>
  Luca Bruno <lethalman88@gmail.com>, Julian Andres Klode <jak@debian.org>
 Standards-Version: 3.8.4
 Standards-Version: 3.8.4
-Build-Depends: debhelper (>= 5.0), libdb-dev, gettext (>= 0.12), libcurl4-gnutls-dev | libcurl3-gnutls-dev (>= 7.15.5), debiandoc-sgml, xsltproc, docbook-xsl, po4a (>= 0.34-2), autotools-dev
+Build-Depends: debhelper (>= 5.0), libdb-dev, gettext (>= 0.12), libcurl4-gnutls-dev | libcurl3-gnutls-dev (>= 7.15.5), zlib1g-dev | libz-dev, debiandoc-sgml, xsltproc, docbook-xsl, po4a (>= 0.34-2), autotools-dev
 Vcs-Bzr: http://bzr.debian.org/apt/debian-sid/
 Vcs-Bzr: http://bzr.debian.org/apt/debian-sid/
 
 
 Package: apt
 Package: apt