copy.cc 2.2 KB

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