copy.cc 2.1 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 <apt-pkg/fileutl.h>
  11. #include <apt-pkg/acquire-method.h>
  12. #include <apt-pkg/error.h>
  13. #include <sys/stat.h>
  14. #include <utime.h>
  15. #include <unistd.h>
  16. #include <apti18n.h>
  17. /*}}}*/
  18. class CopyMethod : public pkgAcqMethod
  19. {
  20. virtual bool Fetch(FetchItem *Itm);
  21. public:
  22. CopyMethod() : pkgAcqMethod("1.0",SingleInstance) {};
  23. };
  24. // CopyMethod::Fetch - Fetch a file /*{{{*/
  25. // ---------------------------------------------------------------------
  26. /* */
  27. bool CopyMethod::Fetch(FetchItem *Itm)
  28. {
  29. URI Get = Itm->Uri;
  30. string File = Get.Path;
  31. // Stat the file and send a start message
  32. struct stat Buf;
  33. if (stat(File.c_str(),&Buf) != 0)
  34. return _error->Errno("stat",_("Failed to stat"));
  35. // Forumulate a result and send a start message
  36. FetchResult Res;
  37. Res.Size = Buf.st_size;
  38. Res.Filename = Itm->DestFile;
  39. Res.LastModified = Buf.st_mtime;
  40. Res.IMSHit = false;
  41. URIStart(Res);
  42. // See if the file exists
  43. FileFd From(File,FileFd::ReadOnly);
  44. FileFd To(Itm->DestFile,FileFd::WriteEmpty);
  45. To.EraseOnFailure();
  46. if (_error->PendingError() == true)
  47. {
  48. To.OpFail();
  49. return false;
  50. }
  51. // Copy the file
  52. if (CopyFile(From,To) == false)
  53. {
  54. To.OpFail();
  55. return false;
  56. }
  57. From.Close();
  58. To.Close();
  59. // Transfer the modification times
  60. struct utimbuf TimeBuf;
  61. TimeBuf.actime = Buf.st_atime;
  62. TimeBuf.modtime = Buf.st_mtime;
  63. if (utime(Itm->DestFile.c_str(),&TimeBuf) != 0)
  64. {
  65. To.OpFail();
  66. return _error->Errno("utime",_("Failed to set modification time"));
  67. }
  68. URIDone(Res);
  69. return true;
  70. }
  71. /*}}}*/
  72. int main()
  73. {
  74. setlocale(LC_ALL, "");
  75. CopyMethod Mth;
  76. return Mth.Run();
  77. }