copy.cc 2.1 KB

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