gzip.cc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: gzip.cc,v 1.17.2.1 2004/01/16 18:58:50 mdz Exp $
  4. /* ######################################################################
  5. GZip method - Take a file URI in and decompress it into the target
  6. file.
  7. ##################################################################### */
  8. /*}}}*/
  9. // Include Files /*{{{*/
  10. #include <apt-pkg/fileutl.h>
  11. #include <apt-pkg/error.h>
  12. #include <apt-pkg/acquire-method.h>
  13. #include <apt-pkg/strutl.h>
  14. #include <apt-pkg/hashes.h>
  15. #include <sys/stat.h>
  16. #include <unistd.h>
  17. #include <utime.h>
  18. #include <stdio.h>
  19. #include <errno.h>
  20. #include <apti18n.h>
  21. /*}}}*/
  22. class GzipMethod : public pkgAcqMethod
  23. {
  24. virtual bool Fetch(FetchItem *Itm);
  25. public:
  26. GzipMethod() : pkgAcqMethod("1.1",SingleInstance | SendConfig) {};
  27. };
  28. // GzipMethod::Fetch - Decompress the passed URI /*{{{*/
  29. // ---------------------------------------------------------------------
  30. /* */
  31. bool GzipMethod::Fetch(FetchItem *Itm)
  32. {
  33. URI Get = Itm->Uri;
  34. string Path = Get.Host + Get.Path; // To account for relative paths
  35. FetchResult Res;
  36. Res.Filename = Itm->DestFile;
  37. URIStart(Res);
  38. // Open the source and destination files
  39. FileFd From(Path,FileFd::ReadOnlyGzip);
  40. if(From.FileSize() == 0)
  41. return _error->Error(_("Empty files can't be valid archives"));
  42. FileFd To(Itm->DestFile,FileFd::WriteAtomic);
  43. To.EraseOnFailure();
  44. if (_error->PendingError() == true)
  45. return false;
  46. // Read data from source, generate checksums and write
  47. Hashes Hash;
  48. bool Failed = false;
  49. while (1)
  50. {
  51. unsigned char Buffer[4*1024];
  52. unsigned long Count;
  53. if (!From.Read(Buffer,sizeof(Buffer),&Count))
  54. {
  55. To.OpFail();
  56. return false;
  57. }
  58. if (Count == 0)
  59. break;
  60. Hash.Add(Buffer,Count);
  61. if (To.Write(Buffer,Count) == false)
  62. {
  63. Failed = true;
  64. break;
  65. }
  66. }
  67. From.Close();
  68. To.Close();
  69. if (Failed == true)
  70. return false;
  71. // Transfer the modification times
  72. struct stat Buf;
  73. if (stat(Path.c_str(),&Buf) != 0)
  74. return _error->Errno("stat",_("Failed to stat"));
  75. struct utimbuf TimeBuf;
  76. TimeBuf.actime = Buf.st_atime;
  77. TimeBuf.modtime = Buf.st_mtime;
  78. if (utime(Itm->DestFile.c_str(),&TimeBuf) != 0)
  79. return _error->Errno("utime",_("Failed to set modification time"));
  80. if (stat(Itm->DestFile.c_str(),&Buf) != 0)
  81. return _error->Errno("stat",_("Failed to stat"));
  82. // Return a Done response
  83. Res.LastModified = Buf.st_mtime;
  84. Res.Size = Buf.st_size;
  85. Res.TakeHashes(Hash);
  86. URIDone(Res);
  87. return true;
  88. }
  89. /*}}}*/
  90. int main(int argc, char *argv[])
  91. {
  92. setlocale(LC_ALL, "");
  93. GzipMethod Mth;
  94. return Mth.Run();
  95. }