gzip.cc 2.8 KB

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