uowners.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. void ExtractUnixOwner20(Archive &Arc,const wchar *FileName)
  2. {
  3. char NameA[NM];
  4. WideToChar(FileName,NameA,ASIZE(NameA));
  5. if (Arc.BrokenHeader)
  6. {
  7. uiMsg(UIERROR_UOWNERBROKEN,Arc.FileName,FileName);
  8. ErrHandler.SetErrorCode(RARX_CRC);
  9. return;
  10. }
  11. struct passwd *pw;
  12. errno=0; // Required by getpwnam specification if we need to check errno.
  13. if ((pw=getpwnam(Arc.UOHead.OwnerName))==NULL)
  14. {
  15. uiMsg(UIERROR_UOWNERGETOWNERID,Arc.FileName,GetWide(Arc.UOHead.OwnerName));
  16. ErrHandler.SysErrMsg();
  17. ErrHandler.SetErrorCode(RARX_WARNING);
  18. return;
  19. }
  20. uid_t OwnerID=pw->pw_uid;
  21. struct group *gr;
  22. errno=0; // Required by getgrnam specification if we need to check errno.
  23. if ((gr=getgrnam(Arc.UOHead.GroupName))==NULL)
  24. {
  25. uiMsg(UIERROR_UOWNERGETGROUPID,Arc.FileName,GetWide(Arc.UOHead.GroupName));
  26. ErrHandler.SysErrMsg();
  27. ErrHandler.SetErrorCode(RARX_CRC);
  28. return;
  29. }
  30. uint Attr=GetFileAttr(FileName);
  31. gid_t GroupID=gr->gr_gid;
  32. #if defined(SAVE_LINKS) && !defined(_APPLE)
  33. if (lchown(NameA,OwnerID,GroupID)!=0)
  34. #else
  35. if (chown(NameA,OwnerID,GroupID)!=0)
  36. #endif
  37. {
  38. uiMsg(UIERROR_UOWNERSET,Arc.FileName,FileName);
  39. ErrHandler.SetErrorCode(RARX_CREATE);
  40. }
  41. SetFileAttr(FileName,Attr);
  42. }
  43. void ExtractUnixOwner30(Archive &Arc,const wchar *FileName)
  44. {
  45. char NameA[NM];
  46. WideToChar(FileName,NameA,ASIZE(NameA));
  47. char *OwnerName=(char *)&Arc.SubHead.SubData[0];
  48. int OwnerSize=strlen(OwnerName)+1;
  49. int GroupSize=Arc.SubHead.SubData.Size()-OwnerSize;
  50. char GroupName[NM];
  51. strncpy(GroupName,(char *)&Arc.SubHead.SubData[OwnerSize],GroupSize);
  52. GroupName[GroupSize]=0;
  53. struct passwd *pw;
  54. if ((pw=getpwnam(OwnerName))==NULL)
  55. {
  56. uiMsg(UIERROR_UOWNERGETOWNERID,Arc.FileName,GetWide(OwnerName));
  57. ErrHandler.SetErrorCode(RARX_WARNING);
  58. return;
  59. }
  60. uid_t OwnerID=pw->pw_uid;
  61. struct group *gr;
  62. if ((gr=getgrnam(GroupName))==NULL)
  63. {
  64. uiMsg(UIERROR_UOWNERGETGROUPID,Arc.FileName,GetWide(GroupName));
  65. ErrHandler.SetErrorCode(RARX_WARNING);
  66. return;
  67. }
  68. uint Attr=GetFileAttr(FileName);
  69. gid_t GroupID=gr->gr_gid;
  70. #if defined(SAVE_LINKS) && !defined(_APPLE)
  71. if (lchown(NameA,OwnerID,GroupID)!=0)
  72. #else
  73. if (chown(NameA,OwnerID,GroupID)!=0)
  74. #endif
  75. {
  76. uiMsg(UIERROR_UOWNERSET,Arc.FileName,FileName);
  77. ErrHandler.SetErrorCode(RARX_CREATE);
  78. }
  79. SetFileAttr(FileName,Attr);
  80. }
  81. void SetUnixOwner(Archive &Arc,const wchar *FileName)
  82. {
  83. char NameA[NM];
  84. WideToChar(FileName,NameA,ASIZE(NameA));
  85. // First, we try to resolve symbolic names. If they are missing or cannot
  86. // be resolved, we try to use numeric values if any. If numeric values
  87. // are missing too, function fails.
  88. FileHeader &hd=Arc.FileHead;
  89. if (*hd.UnixOwnerName!=0)
  90. {
  91. struct passwd *pw;
  92. if ((pw=getpwnam(hd.UnixOwnerName))==NULL)
  93. {
  94. if (!hd.UnixOwnerNumeric)
  95. {
  96. uiMsg(UIERROR_UOWNERGETOWNERID,Arc.FileName,GetWide(hd.UnixOwnerName));
  97. ErrHandler.SetErrorCode(RARX_WARNING);
  98. return;
  99. }
  100. }
  101. else
  102. hd.UnixOwnerID=pw->pw_uid;
  103. }
  104. if (*hd.UnixGroupName!=0)
  105. {
  106. struct group *gr;
  107. if ((gr=getgrnam(hd.UnixGroupName))==NULL)
  108. {
  109. if (!hd.UnixGroupNumeric)
  110. {
  111. uiMsg(UIERROR_UOWNERGETGROUPID,Arc.FileName,GetWide(hd.UnixGroupName));
  112. ErrHandler.SetErrorCode(RARX_WARNING);
  113. return;
  114. }
  115. }
  116. else
  117. hd.UnixGroupID=gr->gr_gid;
  118. }
  119. #if defined(SAVE_LINKS) && !defined(_APPLE)
  120. if (lchown(NameA,hd.UnixOwnerID,hd.UnixGroupID)!=0)
  121. #else
  122. if (chown(NameA,hd.UnixOwnerID,hd.UnixGroupID)!=0)
  123. #endif
  124. {
  125. uiMsg(UIERROR_UOWNERSET,Arc.FileName,FileName);
  126. ErrHandler.SetErrorCode(RARX_CREATE);
  127. }
  128. }