copy.cc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: copy.cc,v 1.7.2.1 2004/01/16 18:58:50 mdz Exp $
  4. /* ######################################################################
  5. Copy URI - This method takes a uri like a file: uri and copies it
  6. to the destination file.
  7. ##################################################################### */
  8. /*}}}*/
  9. // Include Files /*{{{*/
  10. #include <config.h>
  11. #include <apt-pkg/fileutl.h>
  12. #include <apt-pkg/strutl.h>
  13. #include <apt-pkg/acquire-method.h>
  14. #include <apt-pkg/error.h>
  15. #include <apt-pkg/hashes.h>
  16. #include <apt-pkg/configuration.h>
  17. #include <string>
  18. #include <sys/stat.h>
  19. #include <sys/time.h>
  20. #include <apti18n.h>
  21. /*}}}*/
  22. class CopyMethod : public pkgAcqMethod
  23. {
  24. virtual bool Fetch(FetchItem *Itm);
  25. void CalculateHashes(FetchResult &Res);
  26. public:
  27. CopyMethod() : pkgAcqMethod("1.0",SingleInstance | SendConfig) {};
  28. };
  29. void CopyMethod::CalculateHashes(FetchResult &Res)
  30. {
  31. Hashes Hash;
  32. FileFd::CompressMode CompressMode = FileFd::None;
  33. if (_config->FindB("Acquire::GzipIndexes", false) == true)
  34. CompressMode = FileFd::Extension;
  35. FileFd Fd(Res.Filename, FileFd::ReadOnly, CompressMode);
  36. Hash.AddFD(Fd);
  37. Res.TakeHashes(Hash);
  38. }
  39. // CopyMethod::Fetch - Fetch a file /*{{{*/
  40. // ---------------------------------------------------------------------
  41. /* */
  42. bool CopyMethod::Fetch(FetchItem *Itm)
  43. {
  44. // this ensures that relative paths work in copy
  45. std::string File = Itm->Uri.substr(Itm->Uri.find(':')+1);
  46. // Stat the file and send a start message
  47. struct stat Buf;
  48. if (stat(File.c_str(),&Buf) != 0)
  49. return _error->Errno("stat",_("Failed to stat"));
  50. // Forumulate a result and send a start message
  51. FetchResult Res;
  52. Res.Size = Buf.st_size;
  53. Res.Filename = Itm->DestFile;
  54. Res.LastModified = Buf.st_mtime;
  55. Res.IMSHit = false;
  56. URIStart(Res);
  57. // when the files are identical, just compute the hashes
  58. if(File == Itm->DestFile)
  59. {
  60. CalculateHashes(Res);
  61. URIDone(Res);
  62. return true;
  63. }
  64. // just calc the hashes if the source and destination are identical
  65. if (File == Itm->DestFile)
  66. {
  67. CalculateHashes(Res);
  68. URIDone(Res);
  69. return true;
  70. }
  71. // See if the file exists
  72. FileFd From(File,FileFd::ReadOnly);
  73. FileFd To(Itm->DestFile,FileFd::WriteAtomic);
  74. To.EraseOnFailure();
  75. if (_error->PendingError() == true)
  76. {
  77. To.OpFail();
  78. return false;
  79. }
  80. // Copy the file
  81. if (CopyFile(From,To) == false)
  82. {
  83. To.OpFail();
  84. return false;
  85. }
  86. From.Close();
  87. To.Close();
  88. // Transfer the modification times
  89. struct timeval times[2];
  90. times[0].tv_sec = Buf.st_atime;
  91. times[1].tv_sec = Buf.st_mtime;
  92. times[0].tv_usec = times[1].tv_usec = 0;
  93. if (utimes(Res.Filename.c_str(), times) != 0)
  94. return _error->Errno("utimes",_("Failed to set modification time"));
  95. CalculateHashes(Res);
  96. URIDone(Res);
  97. return true;
  98. }
  99. /*}}}*/
  100. int main()
  101. {
  102. setlocale(LC_ALL, "");
  103. CopyMethod Mth;
  104. return Mth.Run();
  105. }