copy.cc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. // For gzip indexes we need to look inside the gzip for the hash
  32. // We can not use the extension here as its not used in partial
  33. // on a IMS hit
  34. FileFd::OpenMode OpenMode = FileFd::ReadOnly;
  35. if (_config->FindB("Acquire::GzipIndexes", false) == true)
  36. OpenMode = FileFd::ReadOnlyGzip;
  37. Hashes Hash;
  38. FileFd Fd(Res.Filename, OpenMode);
  39. Hash.AddFD(Fd);
  40. Res.TakeHashes(Hash);
  41. }
  42. // CopyMethod::Fetch - Fetch a file /*{{{*/
  43. // ---------------------------------------------------------------------
  44. /* */
  45. bool CopyMethod::Fetch(FetchItem *Itm)
  46. {
  47. // this ensures that relative paths work in copy
  48. std::string File = Itm->Uri.substr(Itm->Uri.find(':')+1);
  49. // Stat the file and send a start message
  50. struct stat Buf;
  51. if (stat(File.c_str(),&Buf) != 0)
  52. return _error->Errno("stat",_("Failed to stat"));
  53. // Forumulate a result and send a start message
  54. FetchResult Res;
  55. Res.Size = Buf.st_size;
  56. Res.Filename = Itm->DestFile;
  57. Res.LastModified = Buf.st_mtime;
  58. Res.IMSHit = false;
  59. URIStart(Res);
  60. // just calc the hashes if the source and destination are identical
  61. if (File == Itm->DestFile)
  62. {
  63. CalculateHashes(Res);
  64. URIDone(Res);
  65. return true;
  66. }
  67. // See if the file exists
  68. FileFd From(File,FileFd::ReadOnly);
  69. FileFd To(Itm->DestFile,FileFd::WriteAtomic);
  70. To.EraseOnFailure();
  71. if (_error->PendingError() == true)
  72. {
  73. To.OpFail();
  74. return false;
  75. }
  76. // Copy the file
  77. if (CopyFile(From,To) == false)
  78. {
  79. To.OpFail();
  80. return false;
  81. }
  82. From.Close();
  83. To.Close();
  84. // Transfer the modification times
  85. struct timeval times[2];
  86. times[0].tv_sec = Buf.st_atime;
  87. times[1].tv_sec = Buf.st_mtime;
  88. times[0].tv_usec = times[1].tv_usec = 0;
  89. if (utimes(Res.Filename.c_str(), times) != 0)
  90. return _error->Errno("utimes",_("Failed to set modification time"));
  91. CalculateHashes(Res);
  92. URIDone(Res);
  93. return true;
  94. }
  95. /*}}}*/
  96. int main()
  97. {
  98. setlocale(LC_ALL, "");
  99. CopyMethod Mth;
  100. return Mth.Run();
  101. }