You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

118 lines
2.8 KiB

  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(FetchItem const * const Itm, FetchResult &Res);
  26. public:
  27. CopyMethod() : pkgAcqMethod("1.0",SingleInstance | SendConfig) {};
  28. };
  29. void CopyMethod::CalculateHashes(FetchItem const * const Itm, FetchResult &Res)
  30. {
  31. Hashes Hash(Itm->ExpectedHashes);
  32. FileFd Fd(Res.Filename, FileFd::ReadOnly, FileFd::Extension);
  33. Hash.AddFD(Fd);
  34. Res.TakeHashes(Hash);
  35. }
  36. // CopyMethod::Fetch - Fetch a file /*{{{*/
  37. // ---------------------------------------------------------------------
  38. /* */
  39. bool CopyMethod::Fetch(FetchItem *Itm)
  40. {
  41. // this ensures that relative paths work in copy
  42. std::string const File = Itm->Uri.substr(Itm->Uri.find(':')+1);
  43. // Stat the file and send a start message
  44. struct stat Buf;
  45. if (stat(File.c_str(),&Buf) != 0)
  46. return _error->Errno("stat",_("Failed to stat"));
  47. // Forumulate a result and send a start message
  48. FetchResult Res;
  49. Res.Size = Buf.st_size;
  50. Res.Filename = Itm->DestFile;
  51. Res.LastModified = Buf.st_mtime;
  52. Res.IMSHit = false;
  53. URIStart(Res);
  54. // just calc the hashes if the source and destination are identical
  55. if (File == Itm->DestFile)
  56. {
  57. CalculateHashes(Itm, Res);
  58. URIDone(Res);
  59. return true;
  60. }
  61. // See if the file exists
  62. FileFd From(File,FileFd::ReadOnly);
  63. FileFd To(Itm->DestFile,FileFd::WriteAtomic);
  64. To.EraseOnFailure();
  65. if (_error->PendingError() == true)
  66. {
  67. To.OpFail();
  68. return false;
  69. }
  70. // Copy the file
  71. if (CopyFile(From,To) == false)
  72. {
  73. To.OpFail();
  74. return false;
  75. }
  76. From.Close();
  77. To.Close();
  78. // Transfer the modification times
  79. struct timeval times[2];
  80. times[0].tv_sec = Buf.st_atime;
  81. times[1].tv_sec = Buf.st_mtime;
  82. times[0].tv_usec = times[1].tv_usec = 0;
  83. if (utimes(Res.Filename.c_str(), times) != 0)
  84. return _error->Errno("utimes",_("Failed to set modification time"));
  85. CalculateHashes(Itm, Res);
  86. URIDone(Res);
  87. return true;
  88. }
  89. /*}}}*/
  90. int main()
  91. {
  92. setlocale(LC_ALL, "");
  93. CopyMethod Mth;
  94. return Mth.Run();
  95. }