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.
 
 
 
 
 
 

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