gzip.cc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 the file is empty, just rename it and return
  41. if(From.Size() == 0)
  42. {
  43. rename(Path.c_str(), Itm->DestFile.c_str());
  44. return true;
  45. }
  46. FileFd To(Itm->DestFile,FileFd::WriteAtomic);
  47. To.EraseOnFailure();
  48. if (_error->PendingError() == true)
  49. return false;
  50. // Read data from source, generate checksums and write
  51. Hashes Hash;
  52. bool Failed = false;
  53. while (1)
  54. {
  55. unsigned char Buffer[4*1024];
  56. unsigned long Count;
  57. if (!From.Read(Buffer,sizeof(Buffer),&Count))
  58. {
  59. To.OpFail();
  60. return false;
  61. }
  62. if (Count == 0)
  63. break;
  64. Hash.Add(Buffer,Count);
  65. if (To.Write(Buffer,Count) == false)
  66. {
  67. Failed = true;
  68. break;
  69. }
  70. }
  71. From.Close();
  72. To.Close();
  73. if (Failed == true)
  74. return false;
  75. // Transfer the modification times
  76. struct stat Buf;
  77. if (stat(Path.c_str(),&Buf) != 0)
  78. return _error->Errno("stat",_("Failed to stat"));
  79. struct utimbuf TimeBuf;
  80. TimeBuf.actime = Buf.st_atime;
  81. TimeBuf.modtime = Buf.st_mtime;
  82. if (utime(Itm->DestFile.c_str(),&TimeBuf) != 0)
  83. return _error->Errno("utime",_("Failed to set modification time"));
  84. if (stat(Itm->DestFile.c_str(),&Buf) != 0)
  85. return _error->Errno("stat",_("Failed to stat"));
  86. // Return a Done response
  87. Res.LastModified = Buf.st_mtime;
  88. Res.Size = Buf.st_size;
  89. Res.TakeHashes(Hash);
  90. URIDone(Res);
  91. return true;
  92. }
  93. /*}}}*/
  94. int main(int argc, char *argv[])
  95. {
  96. setlocale(LC_ALL, "");
  97. GzipMethod Mth;
  98. return Mth.Run();
  99. }