copy.cc 2.5 KB

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