dirstream.cc 3.2 KB

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