copy.cc 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: copy.cc,v 1.5 1998/11/01 05:27:40 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. // See if the file exists
  31. FileFd From(File,FileFd::ReadOnly);
  32. FileFd To(Itm->DestFile,FileFd::WriteEmpty);
  33. To.EraseOnFailure();
  34. if (_error->PendingError() == true)
  35. return false;
  36. // Copy the file
  37. if (CopyFile(From,To) == false)
  38. return false;
  39. From.Close();
  40. To.Close();
  41. // Transfer the modification times
  42. struct stat Buf;
  43. if (stat(File.c_str(),&Buf) != 0)
  44. {
  45. To.OpFail();
  46. return _error->Errno("stat","Failed to stat");
  47. }
  48. struct utimbuf TimeBuf;
  49. TimeBuf.actime = Buf.st_atime;
  50. TimeBuf.modtime = Buf.st_mtime;
  51. if (utime(Itm->DestFile.c_str(),&TimeBuf) != 0)
  52. {
  53. To.OpFail();
  54. return _error->Errno("utime","Failed to set modification time");
  55. }
  56. // Forumulate a result
  57. FetchResult Res;
  58. Res.Size = Buf.st_size;
  59. Res.Filename = Itm->DestFile;
  60. Res.LastModified = Buf.st_mtime;
  61. Res.IMSHit = false;
  62. URIDone(Res);
  63. return true;
  64. }
  65. /*}}}*/
  66. int main()
  67. {
  68. CopyMethod Mth;
  69. return Mth.Run();
  70. }