dirstream.cc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: dirstream.cc,v 1.3.2.1 2004/01/16 18:58:50 mdz Exp $
  4. /* ######################################################################
  5. Directory Stream
  6. This class provides a simple basic extractor that can be used for
  7. a number of purposes.
  8. ##################################################################### */
  9. /*}}}*/
  10. // Include Files /*{{{*/
  11. #include <apt-pkg/dirstream.h>
  12. #include <apt-pkg/error.h>
  13. #include <fcntl.h>
  14. #include <sys/stat.h>
  15. #include <sys/types.h>
  16. #include <errno.h>
  17. #include <utime.h>
  18. #include <unistd.h>
  19. #include <apti18n.h>
  20. /*}}}*/
  21. // DirStream::DoItem - Process an item /*{{{*/
  22. // ---------------------------------------------------------------------
  23. /* This is a very simple extractor, it does not deal with things like
  24. overwriting directories with files and so on. */
  25. bool pkgDirStream::DoItem(Item &Itm,int &Fd)
  26. {
  27. switch (Itm.Type)
  28. {
  29. case Item::File:
  30. {
  31. /* Open the output file, NDELAY is used to prevent this from
  32. blowing up on device special files.. */
  33. int iFd = open(Itm.Name,O_NDELAY|O_WRONLY|O_CREAT|O_TRUNC|O_APPEND,
  34. Itm.Mode);
  35. if (iFd < 0)
  36. return _error->Errno("open",_("Failed to write file %s"),
  37. Itm.Name);
  38. // fchmod deals with umask and fchown sets the ownership
  39. if (fchmod(iFd,Itm.Mode) != 0)
  40. return _error->Errno("fchmod",_("Failed to write file %s"),
  41. Itm.Name);
  42. if (fchown(iFd,Itm.UID,Itm.GID) != 0 && errno != EPERM)
  43. return _error->Errno("fchown",_("Failed to write file %s"),
  44. Itm.Name);
  45. Fd = iFd;
  46. return true;
  47. }
  48. case Item::HardLink:
  49. case Item::SymbolicLink:
  50. case Item::CharDevice:
  51. case Item::BlockDevice:
  52. case Item::Directory:
  53. {
  54. struct stat Buf;
  55. // check if the dir is already there, if so return true
  56. if (stat(Itm.Name,&Buf) == 0)
  57. {
  58. if(S_ISDIR(Buf.st_mode))
  59. return true;
  60. // something else is there already, return false
  61. return false;
  62. }
  63. // nothing here, create the dir
  64. if(mkdir(Itm.Name,Itm.Mode) < 0)
  65. return false;
  66. return true;
  67. break;
  68. }
  69. case Item::FIFO:
  70. break;
  71. }
  72. return true;
  73. }
  74. /*}}}*/
  75. // DirStream::FinishedFile - Finished processing a file /*{{{*/
  76. // ---------------------------------------------------------------------
  77. /* */
  78. bool pkgDirStream::FinishedFile(Item &Itm,int Fd)
  79. {
  80. if (Fd < 0)
  81. return true;
  82. if (close(Fd) != 0)
  83. return _error->Errno("close",_("Failed to close file %s"),Itm.Name);
  84. /* Set the modification times. The only way it can fail is if someone
  85. has futzed with our file, which is intolerable :> */
  86. struct utimbuf Time;
  87. Time.actime = Itm.MTime;
  88. Time.modtime = Itm.MTime;
  89. if (utime(Itm.Name,&Time) != 0)
  90. _error->Errno("utime",_("Failed to close file %s"),Itm.Name);
  91. return true;
  92. }
  93. /*}}}*/
  94. // DirStream::Fail - Failed processing a file /*{{{*/
  95. // ---------------------------------------------------------------------
  96. /* */
  97. bool pkgDirStream::Fail(Item &Itm,int Fd)
  98. {
  99. if (Fd < 0)
  100. return true;
  101. close(Fd);
  102. return false;
  103. }
  104. /*}}}*/