copy.cc 1.9 KB

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